welcomemode.cpp 8.51 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
7
8
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
25
26
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
31
32
33
34
#include "welcomemode.h"
#include "coreconstants.h"
#include "uniqueidmanager.h"
#include "modemanager.h"

35
#if !defined(QT_NO_WEBKIT)
con's avatar
con committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <QtWebKit/QWebView>
#include <QtGui/QApplication>
#include <QtCore/QFileInfo>
#else
#include <QtGui/QLabel>
#endif
#include <QtGui/QToolBar>
#include <QtGui/QDesktopServices>

#include <QtCore/QFile>
#include <QtCore/QTextStream>
#include <QtCore/QDebug>
#include <QtCore/QUrl>

namespace Core {
namespace Internal {

static QString readFile(const QString &name)
{
    QFile f(name);
    if (!f.open(QIODevice::ReadOnly)) {
        qWarning("Unable to open %s: %s", name.toUtf8().constData(), f.errorString().toUtf8().constData());
        return QString();
    }
    QTextStream ts(&f);
    return ts.readAll();
}

hjk's avatar
hjk committed
64
65
struct WelcomeModePrivate
{
con's avatar
con committed
66
67
68
    WelcomeModePrivate();

    QWidget *m_widget;
69
#if !defined(QT_NO_WEBKIT)
con's avatar
con committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    QWebView *m_webview;
#else
    QLabel *m_label;
#endif

    WelcomeMode::WelcomePageData lastData;

    const QString m_htmlTemplate;
    const QString m_sessionHtmlTemplate;
    const QString m_projectHtmlTemplate;
    const QUrl m_baseUrl;
};

WelcomeModePrivate::WelcomeModePrivate() :
    m_widget(new QWidget),
85
#if !defined(QT_NO_WEBKIT)
con's avatar
con committed
86
87
88
89
    m_webview(new QWebView),
#else
    m_label(new QLabel),
#endif
90
91
92
93
    m_htmlTemplate(readFile(QLatin1String(":/core/html/welcome.html"))),
    m_sessionHtmlTemplate(readFile(QLatin1String(":/core/html/recent_sessions.html"))),
    m_projectHtmlTemplate(readFile(QLatin1String(":/core/html/recent_projects.html"))),
    m_baseUrl(QUrl(QLatin1String("qrc:/core/html/welcome.html")))
con's avatar
con committed
94
{
95
#if !defined(QT_NO_WEBKIT)
96
    m_webview->setContextMenuPolicy(Qt::NoContextMenu);
97
#endif   
con's avatar
con committed
98
99
}

100
#if defined(QT_NO_WEBKIT)
con's avatar
con committed
101

102
const char *LABEL = "<center><table><tr><td><img src=\":/core/html/images/product_logo.png\"/></td><td width=300>"
con's avatar
con committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
                    "<h2><br/><br/>Welcome</h2><p> Qt Creator is an intuitive, modern cross platform IDE that enables "
                    "developers to create graphically appealing applications for desktop, "
                    "embedded, and mobile devices. "
                    "<p><font color=\"red\">(This startup page lacks features due to disabled webkit support)</font>"
                    "</td></tr></table>";

#endif
// ---  WelcomePageData

bool WelcomeMode::WelcomePageData::operator==(const WelcomePageData &rhs) const
{
    return previousSession == rhs.previousSession
        && activeSession   == rhs.activeSession
        && sessionList     == rhs.sessionList
        && projectList     == rhs.projectList;
}

bool WelcomeMode::WelcomePageData::operator!=(const WelcomePageData &rhs) const
{
    return previousSession != rhs.previousSession
        || activeSession   != rhs.activeSession
        || sessionList     != rhs.sessionList
        || projectList     != rhs.projectList;
}

QDebug operator<<(QDebug dgb, const WelcomeMode::WelcomePageData &d)
{
    dgb.nospace() << "PreviousSession=" << d.previousSession
        << " activeSession=" << d.activeSession
        << " sessionList=" << d.sessionList
        << " projectList=" << d.projectList;
    return dgb;
}

// ---  WelcomeMode
WelcomeMode::WelcomeMode() :
    m_d(new WelcomeModePrivate)
{
    QVBoxLayout *l = new QVBoxLayout(m_d->m_widget);
    l->setMargin(0);
    l->setSpacing(0);
    l->addWidget(new QToolBar(m_d->m_widget));
145
#if !defined(QT_NO_WEBKIT)
con's avatar
con committed
146
147
148
149
150
151
    connect(m_d->m_webview, SIGNAL(linkClicked(QUrl)), this, SLOT(linkClicked(QUrl)));

    WelcomePageData welcomePageData;
    updateWelcomePage(welcomePageData);

    l->addWidget(m_d->m_webview);
152
    m_d->m_webview->setAcceptDrops(false);
153
154
    m_d->m_webview->settings()->setAttribute(QWebSettings::PluginsEnabled, false);
    m_d->m_webview->settings()->setAttribute(QWebSettings::JavaEnabled, false);
con's avatar
con committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

#else
    m_d->m_label->setWordWrap(true);
    m_d->m_label->setAlignment(Qt::AlignCenter);
    m_d->m_label->setText(LABEL);
    l->addWidget(m_d->m_label);
#endif
}

WelcomeMode::~WelcomeMode()
{
    delete m_d;
}

QString WelcomeMode::name() const
{
    return QLatin1String("Welcome");
}

QIcon WelcomeMode::icon() const
{
176
    return QIcon(QLatin1String(":/core/images/qtcreator_logo_32.png"));
con's avatar
con committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
}

int WelcomeMode::priority() const
{
    return Constants::P_MODE_WELCOME;
}

QWidget* WelcomeMode::widget()
{
    return m_d->m_widget;
}

const char* WelcomeMode::uniqueModeName() const
{
    return Constants::MODE_WELCOME;
}

QList<int> WelcomeMode::context() const
{
    static QList<int> contexts = QList<int>()
197
                                 << UniqueIDManager::instance()->uniqueIdentifier(Constants::C_WELCOME_MODE);
con's avatar
con committed
198
199
200
201
202
203
204
    return contexts;
}

void WelcomeMode::updateWelcomePage(const WelcomePageData &welcomePageData)
{
// should really only modify the DOM tree

205
#if defined(QT_NO_WEBKIT)
con's avatar
con committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
    Q_UNUSED(welcomePageData);
#else

    // Update only if data are modified
    if (welcomePageData == m_d->lastData)
        return;
    m_d->lastData = welcomePageData;

    QString html = m_d->m_htmlTemplate;

    if (!welcomePageData.previousSession.isEmpty() || !welcomePageData.projectList.isEmpty()) {
        QString sessionHtml = m_d->m_sessionHtmlTemplate;
        sessionHtml.replace(QLatin1String("LAST_SESSION"), welcomePageData.previousSession);

        if (welcomePageData.sessionList.count() > 1) {
            QString sessions;
            foreach (QString s, welcomePageData.sessionList) {
                QString last;
                if (s == welcomePageData.previousSession)
                    last = tr(" (last session)");
                sessions += QString::fromLatin1("<li><p><a href=\"gh-session:%1\">%2%3</a></p></li>").arg(s, s, last);
            }
            sessionHtml.replace(QLatin1String("<!-- RECENT SESSIONS LIST -->"), sessions);
        }
        html.replace(QLatin1String("<!-- RECENT SESSIONS -->"), sessionHtml);

        QString projectHtml = m_d->m_projectHtmlTemplate;
        {
            QString projects;
            QTextStream str(&projects);
            foreach (const QString &s, welcomePageData.projectList) {
                const QFileInfo fi(s);
                str << "<li><p><a href=\"gh-project:" << s << "\" title=\""
                    << fi.absolutePath() << "\">" << fi.fileName() << "</a></p></li>\n";
            }
            projectHtml.replace(QLatin1String("<!-- RECENT PROJECTS LIST -->"), projects);
        }
        html.replace(QLatin1String("<!-- RECENT PROJECTS -->"), projectHtml);
    }

    m_d->m_webview->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
    m_d->m_webview->setHtml(html, m_d->m_baseUrl);
#endif
}

void WelcomeMode::linkClicked(const QUrl &url)
{
    QString scheme = url.scheme();
254
    Core::ModeManager *modeManager = ModeManager::instance();
con's avatar
con committed
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
    if (scheme.startsWith(QLatin1String("gh"))) {
        QString s = url.toString(QUrl::RemoveScheme);
        if (scheme == QLatin1String("gh")) {
            emit requestHelp(s);
        } else if (scheme == QLatin1String("gh-project")) {
            emit requestProject(s);
            if (modeManager->currentMode() == this)
                modeManager->activateMode(Core::Constants::MODE_EDIT);
        } else if (scheme == QLatin1String("gh-session")) {
            emit requestSession(s);
            if (modeManager->currentMode() == this)
                modeManager->activateMode(Core::Constants::MODE_EDIT);
        }
    } else {
        QDesktopServices::openUrl(url);
    }
}

} // namespace Internal
} // namespace Core