main.cpp 12 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
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
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
con's avatar
con committed
29
30
31
32
33
34
35
36

#include "qtsingleapplication.h"

#include <extensionsystem/pluginmanager.h>
#include <extensionsystem/pluginspec.h>
#include <extensionsystem/iplugin.h>

#include <QtCore/QDir>
37
#include <QtCore/QUrl>
con's avatar
con committed
38
39
40
41
#include <QtCore/QTextStream>
#include <QtCore/QFileInfo>
#include <QtCore/QDebug>
#include <QtCore/QTimer>
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
42
43
#include <QtCore/QLibraryInfo>
#include <QtCore/QTranslator>
44
#include <QtCore/QSettings>
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
45
#include <QtCore/QVariant>
46
#include <QtCore/QThreadPool>
con's avatar
con committed
47

48
49
#include <QtNetwork/QNetworkProxyFactory>

con's avatar
con committed
50
51
52
53
#include <QtGui/QMessageBox>
#include <QtGui/QApplication>
#include <QtGui/QMainWindow>

Marco Bubke's avatar
Marco Bubke committed
54
55
56
57
#ifdef ENABLE_QT_BREAKPAD
#include <qtsystemexceptionhandler.h>
#endif

hjk's avatar
hjk committed
58
enum { OptionIndent = 4, DescriptionIndent = 24 };
con's avatar
con committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

static const char *appNameC = "Qt Creator";
static const char *corePluginNameC = "Core";
static const char *fixedOptionsC =
" [OPTION]... [FILE]...\n"
"Options:\n"
"    -help               Display this help\n"
"    -version            Display program version\n"
"    -client             Attempt to connect to already running instance\n";

static const char *HELP_OPTION1 = "-h";
static const char *HELP_OPTION2 = "-help";
static const char *HELP_OPTION3 = "/h";
static const char *HELP_OPTION4 = "--help";
static const char *VERSION_OPTION = "-version";
static const char *CLIENT_OPTION = "-client";

76
typedef QList<ExtensionSystem::PluginSpec *> PluginSpecSet;
con's avatar
con committed
77
78

// Helpers for displaying messages. Note that there is no console on Windows.
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
79
#ifdef Q_OS_WIN
con's avatar
con committed
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
// Format as <pre> HTML
static inline void toHtml(QString &t)
{
    t.replace(QLatin1Char('&'), QLatin1String("&amp;"));
    t.replace(QLatin1Char('<'), QLatin1String("&lt;"));
    t.replace(QLatin1Char('>'), QLatin1String("&gt;"));
    t.insert(0, QLatin1String("<html><pre>"));
    t.append(QLatin1String("</pre></html>"));
}

static void displayHelpText(QString t) // No console on Windows.
{
    toHtml(t);
    QMessageBox::information(0, QLatin1String(appNameC), t);
}

static void displayError(const QString &t) // No console on Windows.
{
    QMessageBox::critical(0, QLatin1String(appNameC), t);
}

#else

static void displayHelpText(const QString &t)
{
105
    qWarning("%s", qPrintable(t));
con's avatar
con committed
106
107
108
109
}

static void displayError(const QString &t)
{
110
    qCritical("%s", qPrintable(t));
con's avatar
con committed
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
}

#endif

static void printVersion(const ExtensionSystem::PluginSpec *coreplugin,
                         const ExtensionSystem::PluginManager &pm)
{
    QString version;
    QTextStream str(&version);
    str << '\n' << appNameC << ' ' << coreplugin->version()<< " based on Qt " << qVersion() << "\n\n";
    pm.formatPluginVersions(str);
    str << '\n' << coreplugin->copyright() << '\n';
    displayHelpText(version);
}

static void printHelp(const QString &a0, const ExtensionSystem::PluginManager &pm)
{
    QString help;
    QTextStream str(&help);
    str << "Usage: " << a0  << fixedOptionsC;
    ExtensionSystem::PluginManager::formatOptions(str, OptionIndent, DescriptionIndent);
    pm.formatPluginOptions(str,  OptionIndent, DescriptionIndent);
    displayHelpText(help);
}

static inline QString msgCoreLoadFailure(const QString &why)
{
    return QCoreApplication::translate("Application", "Failed to load core: %1").arg(why);
}

static inline QString msgSendArgumentFailed()
{
    return QCoreApplication::translate("Application", "Unable to send command line arguments to the already running instance. It appears to be not responding.");
}

static inline QStringList getPluginPaths()
{
    QStringList rc;
    // Figure out root:  Up one from 'bin'
    QDir rootDir = QApplication::applicationDirPath();
    rootDir.cdUp();
152
    const QString rootDirPath = rootDir.canonicalPath();
153
    // 1) "plugins" (Win/Linux)
con's avatar
con committed
154
    QString pluginPath = rootDirPath;
155
    pluginPath += QLatin1Char('/');
156
    pluginPath += QLatin1String(IDE_LIBRARY_BASENAME);
157
    pluginPath += QLatin1Char('/');
158
    pluginPath += QLatin1String("qtcreator");
159
    pluginPath += QLatin1Char('/');
160
    pluginPath += QLatin1String("plugins");
con's avatar
con committed
161
    rc.push_back(pluginPath);
162
    // 2) "PlugIns" (OS X)
con's avatar
con committed
163
    pluginPath = rootDirPath;
164
    pluginPath += QLatin1Char('/');
con's avatar
con committed
165
166
167
168
169
    pluginPath += QLatin1String("PlugIns");
    rc.push_back(pluginPath);
    return rc;
}

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
170
171
172
173
174
175
#ifdef Q_OS_MAC
#  define SHARE_PATH "/../Resources"
#else
#  define SHARE_PATH "/../share/qtcreator"
#endif

con's avatar
con committed
176
177
int main(int argc, char **argv)
{
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
178
#ifdef Q_OS_MAC
con's avatar
con committed
179
180
181
    // increase the number of file that can be opened in Qt Creator.
    struct rlimit rl;
    getrlimit(RLIMIT_NOFILE, &rl);
182
183

    rl.rlim_cur = qMin((rlim_t)OPEN_MAX, rl.rlim_max);
con's avatar
con committed
184
185
186
187
    setrlimit(RLIMIT_NOFILE, &rl);
#endif

    SharedTools::QtSingleApplication app((QLatin1String(appNameC)), argc, argv);
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
188

189
190
191
192
    const int threadCount = QThreadPool::globalInstance()->maxThreadCount();
    QThreadPool::globalInstance()->setMaxThreadCount(qMax(4, 2 * threadCount));


Marco Bubke's avatar
Marco Bubke committed
193
194
195
196
#ifdef ENABLE_QT_BREAKPAD
    QtSystemExceptionHandler systemExceptionHandler;
#endif

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
197
198
    QTranslator translator;
    QTranslator qtTranslator;
con's avatar
con committed
199
    QString locale = QLocale::system().name();
200
201

    // Must be done before any QSettings class is created
con's avatar
con committed
202
203
    QSettings::setPath(QSettings::IniFormat, QSettings::SystemScope,
            QCoreApplication::applicationDirPath()+QLatin1String(SHARE_PATH));
204
205
206
207
208
    // keep this in sync with the MainWindow ctor in coreplugin/mainwindow.cpp
    const QSettings settings(QSettings::IniFormat, QSettings::UserScope,
                                 QLatin1String("Nokia"), QLatin1String("QtCreator"));
    locale = settings.value("General/OverrideLanguage", locale).toString();

209
210
211
212
213
214
215
    const QString &creatorTrPath = QCoreApplication::applicationDirPath()
                        + QLatin1String(SHARE_PATH "/translations");
    if (translator.load(QLatin1String("qtcreator_") + locale, creatorTrPath)) {
        const QString &qtTrPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
        const QString &qtTrFile = QLatin1String("qt_") + locale;
        // Binary installer puts Qt tr files into creatorTrPath
        if (qtTranslator.load(qtTrFile, qtTrPath) || qtTranslator.load(qtTrFile, creatorTrPath)) {
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
216
217
218
219
220
221
222
223
            app.installTranslator(&translator);
            app.installTranslator(&qtTranslator);
            app.setProperty("qtc_locale", locale);
        } else {
            translator.load(QString()); // unload()
        }
    }

224
    // Make sure we honor the system's proxy settings
225
226
227
228
229
230
231
232
#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
    QUrl proxyUrl(QString::fromLatin1(qgetenv("http_proxy")));
    if (proxyUrl.isValid()) {
        QNetworkProxy proxy(QNetworkProxy::HttpCachingProxy, proxyUrl.host(),
                            proxyUrl.port(), proxyUrl.userName(), proxyUrl.password());
        QNetworkProxy::setApplicationProxy(proxy);
    }
#else
233
    QNetworkProxyFactory::setUseSystemConfiguration(true);
234
#endif
con's avatar
con committed
235
236
237
238
239
240
241
242
    // Load
    ExtensionSystem::PluginManager pluginManager;
    pluginManager.setFileExtension(QLatin1String("pluginspec"));

    const QStringList pluginPaths = getPluginPaths();
    pluginManager.setPluginPaths(pluginPaths);

    const QStringList arguments = app.arguments();
hjk's avatar
hjk committed
243
    QMap<QString, QString> foundAppOptions;
con's avatar
con committed
244
    if (arguments.size() > 1) {
hjk's avatar
hjk committed
245
        QMap<QString, bool> appOptions;
con's avatar
con committed
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
        appOptions.insert(QLatin1String(HELP_OPTION1), false);
        appOptions.insert(QLatin1String(HELP_OPTION2), false);
        appOptions.insert(QLatin1String(HELP_OPTION3), false);
        appOptions.insert(QLatin1String(HELP_OPTION4), false);
        appOptions.insert(QLatin1String(VERSION_OPTION), false);
        appOptions.insert(QLatin1String(CLIENT_OPTION), false);
        QString errorMessage;
        if (!pluginManager.parseOptions(arguments,
                                        appOptions,
                                        &foundAppOptions,
                                        &errorMessage)) {
            displayError(errorMessage);
            printHelp(QFileInfo(app.applicationFilePath()).baseName(), pluginManager);
            return -1;
        }
    }

    const PluginSpecSet plugins = pluginManager.plugins();
    ExtensionSystem::PluginSpec *coreplugin = 0;
    foreach (ExtensionSystem::PluginSpec *spec, plugins) {
        if (spec->name() == QLatin1String(corePluginNameC)) {
            coreplugin = spec;
            break;
        }
    }
    if (!coreplugin) {
272
        QString nativePaths = QDir::toNativeSeparators(pluginPaths.join(QLatin1String(",")));
273
        const QString reason = QCoreApplication::translate("Application", "Could not find 'Core.pluginspec' in %1").arg(nativePaths);
con's avatar
con committed
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
        displayError(msgCoreLoadFailure(reason));
        return 1;
    }
    if (coreplugin->hasError()) {
        displayError(msgCoreLoadFailure(coreplugin->errorString()));
        return 1;
    }
    if (foundAppOptions.contains(QLatin1String(VERSION_OPTION))) {
        printVersion(coreplugin, pluginManager);
        return 0;
    }
    if (foundAppOptions.contains(QLatin1String(HELP_OPTION1))
            || foundAppOptions.contains(QLatin1String(HELP_OPTION2))
            || foundAppOptions.contains(QLatin1String(HELP_OPTION3))
            || foundAppOptions.contains(QLatin1String(HELP_OPTION4))) {
        printHelp(QFileInfo(app.applicationFilePath()).baseName(), pluginManager);
        return 0;
    }

    const bool isFirstInstance = !app.isRunning();
294
295
296
297
298
299
300
    if (!isFirstInstance && foundAppOptions.contains(QLatin1String(CLIENT_OPTION))) {
        if (!app.sendMessage(pluginManager.serializedArguments())) {
            displayError(msgSendArgumentFailed());
            return -1;
        }
        return 0;
    }
con's avatar
con committed
301
302
303
304
305
306

    pluginManager.loadPlugins();
    if (coreplugin->hasError()) {
        displayError(msgCoreLoadFailure(coreplugin->errorString()));
        return 1;
    }
307
308
309
    {
        QStringList errors;
        foreach (ExtensionSystem::PluginSpec *p, pluginManager.plugins())
310
            // only show errors on startup if plugin is enabled.
311
            if (p->hasError() && p->isEnabled() && !p->isDisabledIndirectly())
312
                errors.append(p->name() + "\n" + p->errorString());
313
314
315
316
317
318
        if (!errors.isEmpty())
            QMessageBox::warning(0,
                QCoreApplication::translate("Application", "Qt Creator - Plugin loader messages"),
                errors.join(QString::fromLatin1("\n\n")));
    }

con's avatar
con committed
319
320
321
322
323
    if (isFirstInstance) {
        // Set up lock and remote arguments for the first instance only.
        // Silently fallback to unconnected instances for any subsequent
        // instances.
        app.initialize();
324
325
        QObject::connect(&app, SIGNAL(messageReceived(QString)),
                         &pluginManager, SLOT(remoteArguments(QString)));
con's avatar
con committed
326
    }
327
    QObject::connect(&app, SIGNAL(fileOpenRequest(QString)), coreplugin->plugin(), SLOT(fileOpenRequest(QString)));
con's avatar
con committed
328

329
330
331
    // shutdown plugin manager on the exit
    QObject::connect(&app, SIGNAL(aboutToQuit()), &pluginManager, SLOT(shutdown()));

con's avatar
con committed
332
333
    // Do this after the event loop has started
    QTimer::singleShot(100, &pluginManager, SLOT(startTests()));
334

335
    return app.exec();
con's avatar
con committed
336
}
337