welcomemode.cpp 8.35 KB
Newer Older
con's avatar
con committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
** 
** Non-Open Source Usage  
** 
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
** agreement between you and Nokia.  
** 
** GNU General Public License Usage 
** 
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt GPL Exception version
** 1.2, included in the file GPL_EXCEPTION.txt in this package.  
** 
***************************************************************************/
#include "welcomemode.h"
#include "coreconstants.h"
#include "uniqueidmanager.h"
#include "coreimpl.h"
#include "modemanager.h"

#ifdef QT_WEBKIT
#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();
}

struct WelcomeModePrivate {
    WelcomeModePrivate();

    QWidget *m_widget;
#ifdef QT_WEBKIT
    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),
#ifdef QT_WEBKIT
    m_webview(new QWebView),
#else
    m_label(new QLabel),
#endif
    m_htmlTemplate(readFile(QLatin1String(":/qworkbench/html/welcome.html"))),
    m_sessionHtmlTemplate(readFile(QLatin1String(":/qworkbench/html/recent_sessions.html"))),
    m_projectHtmlTemplate(readFile(QLatin1String(":/qworkbench/html/recent_projects.html"))),
    m_baseUrl(QUrl(QLatin1String("qrc:/qworkbench/html/welcome.html")))
{
}

#ifndef QT_WEBKIT

const char *LABEL = "<center><table><tr><td><img src=\":/qworkbench/html/images/product_logo.png\"/></td><td width=300>"
                    "<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));
#ifdef QT_WEBKIT
    connect(m_d->m_webview, SIGNAL(linkClicked(QUrl)), this, SLOT(linkClicked(QUrl)));

    WelcomePageData welcomePageData;
    updateWelcomePage(welcomePageData);

    l->addWidget(m_d->m_webview);

#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
{
    return QIcon(QLatin1String(":/qworkbench/images/qtcreator_logo_32.png"));
}

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>()
                                 << CoreImpl::instance()->uniqueIDManager()->uniqueIdentifier(Constants::C_WELCOME_MODE);
    return contexts;
}

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

#ifndef QT_WEBKIT
    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();
    Core::ModeManager *modeManager = CoreImpl::instance()->modeManager();
    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