coreplugin.cpp 9.41 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
Eike Ziller's avatar
Eike Ziller committed
3
4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8
9
10
11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
Eike Ziller's avatar
Eike Ziller committed
12
13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18
19
20
21
22
23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25
26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
con's avatar
con committed
27
28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
hjk's avatar
hjk committed
30

con's avatar
con committed
31
#include "coreplugin.h"
32
#include "designmode.h"
con's avatar
con committed
33
#include "editmode.h"
34
#include "idocument.h"
35
36
37
#include "helpmanager.h"
#include "mainwindow.h"
#include "modemanager.h"
38
#include "infobar.h"
39
#include "iwizardfactory.h"
40

41
42
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/editormanager/editormanager.h>
43
#include <coreplugin/find/findplugin.h>
hjk's avatar
hjk committed
44
#include <coreplugin/locator/locator.h>
45
#include <coreplugin/coreconstants.h>
con's avatar
con committed
46

47
48
#include <extensionsystem/pluginerroroverview.h>
#include <extensionsystem/pluginmanager.h>
49
#include <utils/macroexpander.h>
50
#include <utils/savefile.h>
51
52
#include <utils/stringutils.h>
#include <utils/theme/theme.h>
53
#include <utils/theme/theme_p.h>
54

55
56
#include <QtPlugin>
#include <QDebug>
57
#include <QDateTime>
58
#include <QDir>
con's avatar
con committed
59

Lasse Holmstedt's avatar
Lasse Holmstedt committed
60
using namespace Core;
con's avatar
con committed
61
using namespace Core::Internal;
62
using namespace Utils;
con's avatar
con committed
63

64
65
66
67
68
69
CorePlugin::CorePlugin()
  : m_mainWindow(0)
  , m_editMode(0)
  , m_designMode(0)
  , m_findPlugin(0)
  , m_locator(0)
con's avatar
con committed
70
{
71
    qRegisterMetaType<Id>();
con's avatar
con committed
72
73
74
75
}

CorePlugin::~CorePlugin()
{
76
77
    IWizardFactory::destroyFeatureProvider();

78
    delete m_findPlugin;
hjk's avatar
hjk committed
79
    delete m_locator;
80

con's avatar
con committed
81
82
83
84
85
    if (m_editMode) {
        removeObject(m_editMode);
        delete m_editMode;
    }

Lasse Holmstedt's avatar
Lasse Holmstedt committed
86
    if (m_designMode) {
87
88
        if (m_designMode->designModeIsRequired())
            removeObject(m_designMode);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
89
90
91
        delete m_designMode;
    }

con's avatar
con committed
92
    delete m_mainWindow;
Orgad Shaneh's avatar
Orgad Shaneh committed
93
    setCreatorTheme(0);
con's avatar
con committed
94
95
}

Sergey Belyashov's avatar
Sergey Belyashov committed
96
static QString absoluteThemePath(const QString &themeName, bool userProvidedTheme)
97
98
99
100
101
{
    if (themeName.isEmpty())
        return themeName;
    QString res = QDir::fromNativeSeparators(themeName);
    QFileInfo fi(res);
Sergey Belyashov's avatar
Sergey Belyashov committed
102
    bool tryRawName = userProvidedTheme || fi.isAbsolute();
103
    // Try the given name
Sergey Belyashov's avatar
Sergey Belyashov committed
104
    if (tryRawName && fi.exists())
105
106
107
108
109
110
        return fi.absoluteFilePath();
    const QString suffix = QLatin1String("creatortheme");
    // Try name.creatortheme
    if (fi.suffix() != suffix) {
        res = themeName + QLatin1Char('.') + suffix;
        fi.setFile(res);
Sergey Belyashov's avatar
Sergey Belyashov committed
111
        if (tryRawName && fi.exists())
112
113
114
115
116
117
118
119
120
            return fi.absoluteFilePath();
    }
    if (fi.path().isEmpty())
        return QString(); // absolute/relative path, but not found
    // If only name was given, look it up in qtcreator/themes
    res.prepend(ICore::resourcePath() + QLatin1String("/themes/"));
    return QFileInfo::exists(res) ? res : QString();
}

121
122
void CorePlugin::parseArguments(const QStringList &arguments)
{
123
124
125
    const QString defaultTheme = QLatin1String("default");
    QString themeName = ICore::settings()->value(
                QLatin1String(Constants::SETTINGS_THEME), defaultTheme).toString();
126
    QColor overrideColor;
127
    bool presentationMode = false;
Sergey Belyashov's avatar
Sergey Belyashov committed
128
    bool userProvidedTheme = false;
129

Tobias Hunger's avatar
Tobias Hunger committed
130
    for (int i = 0; i < arguments.size(); ++i) {
131
132
        if (arguments.at(i) == QLatin1String("-color")) {
            const QString colorcode(arguments.at(i + 1));
133
            overrideColor = QColor(colorcode);
134
135
            i++; // skip the argument
        }
Tobias Hunger's avatar
Tobias Hunger committed
136
        if (arguments.at(i) == QLatin1String("-presentationMode"))
137
            presentationMode = true;
138
139
        if (arguments.at(i) == QLatin1String("-theme")) {
            themeName = arguments.at(i + 1);
Sergey Belyashov's avatar
Sergey Belyashov committed
140
            userProvidedTheme = true;
141
142
            i++;
        }
143
    }
144

Sergey Belyashov's avatar
Sergey Belyashov committed
145
    QString themeURI = absoluteThemePath(themeName, userProvidedTheme);
146
147
148
149
    if (themeURI.isEmpty()) {
        themeName = defaultTheme;
        themeURI = QStringLiteral("%1/themes/%2.creatortheme").arg(ICore::resourcePath()).arg(themeName);
        if (themeURI.isEmpty()) {
jkobus's avatar
jkobus committed
150
            qCritical("%s", qPrintable(QCoreApplication::translate("Application", "No valid theme \"%1\"")
151
152
153
154
155
                                       .arg(themeName)));
        }
    }

    QSettings themeSettings(themeURI, QSettings::IniFormat);
156
    Theme *theme = new Theme(themeName, qApp);
157
    theme->readSettings(themeSettings);
158
    if (theme->flag(Theme::ApplyThemePaletteGlobally))
159
160
        QApplication::setPalette(theme->palette());
    setCreatorTheme(theme);
161
162
163
164

    // defer creation of these widgets until here,
    // because they need a valid theme set
    m_mainWindow = new MainWindow;
165
    ActionManager::setPresentationModeEnabled(presentationMode);
166
167
168
169
170
    m_findPlugin = new FindPlugin;
    m_locator = new Locator;

    if (overrideColor.isValid())
        m_mainWindow->setOverrideColor(overrideColor);
171
172
}

173
bool CorePlugin::initialize(const QStringList &arguments, QString *errorMessage)
con's avatar
con committed
174
{
175
    new ActionManager(this);
Orgad Shaneh's avatar
Orgad Shaneh committed
176
    Theme::initialPalette(); // Initialize palette before setting it
177
    qsrand(QDateTime::currentDateTime().toTime_t());
178
    parseArguments(arguments);
179
    const bool success = m_mainWindow->init(errorMessage);
con's avatar
con committed
180
    if (success) {
hjk's avatar
hjk committed
181
        m_editMode = new EditMode;
con's avatar
con committed
182
        addObject(m_editMode);
183
        ModeManager::activateMode(m_editMode->id());
hjk's avatar
hjk committed
184
        m_designMode = new DesignMode;
185
        InfoBar::initializeGloballySuppressed();
con's avatar
con committed
186
    }
187

188
189
    IWizardFactory::initialize();

190
    // Make sure we respect the process's umask when creating new files
191
    SaveFile::initializeUmask();
192

193
    m_findPlugin->initialize(arguments, errorMessage);
hjk's avatar
hjk committed
194
    m_locator->initialize(this, arguments, errorMessage);
195

196
    MacroExpander *expander = Utils::globalMacroExpander();
197
198
199
200
201
202
203
204
205
206
207
208
    expander->registerVariable("CurrentDate:ISO", tr("The current date (ISO)."),
                               []() { return QDate::currentDate().toString(Qt::ISODate); });
    expander->registerVariable("CurrentTime:ISO", tr("The current time (ISO)."),
                               []() { return QTime::currentTime().toString(Qt::ISODate); });
    expander->registerVariable("CurrentDate:RFC", tr("The current date (RFC2822)."),
                               []() { return QDate::currentDate().toString(Qt::RFC2822Date); });
    expander->registerVariable("CurrentTime:RFC", tr("The current time (RFC2822)."),
                               []() { return QTime::currentTime().toString(Qt::RFC2822Date); });
    expander->registerVariable("CurrentDate:Locale", tr("The current date (Locale)."),
                               []() { return QDate::currentDate().toString(Qt::DefaultLocaleShortDate); });
    expander->registerVariable("CurrentTime:Locale", tr("The current time (Locale)."),
                               []() { return QTime::currentTime().toString(Qt::DefaultLocaleShortDate); });
Jarek Kobus's avatar
Jarek Kobus committed
209
    expander->registerPrefix("CurrentDate:", tr("The current date (QDate formatstring)."),
210
                             [](const QString &fmt) { return QDate::currentDate().toString(fmt); });
Jarek Kobus's avatar
Jarek Kobus committed
211
    expander->registerPrefix("CurrentTime:", tr("The current time (QTime formatstring)."),
212
213
                             [](const QString &fmt) { return QTime::currentTime().toString(fmt); });

con's avatar
con committed
214
215
216
217
218
    return success;
}

void CorePlugin::extensionsInitialized()
{
219
220
    if (m_designMode->designModeIsRequired())
        addObject(m_designMode);
221
    m_findPlugin->extensionsInitialized();
hjk's avatar
hjk committed
222
    m_locator->extensionsInitialized();
223
    m_mainWindow->extensionsInitialized();
224
225
226
227
228
229
    if (ExtensionSystem::PluginManager::hasError()) {
        auto errorOverview = new ExtensionSystem::PluginErrorOverview(m_mainWindow);
        errorOverview->setAttribute(Qt::WA_DeleteOnClose);
        errorOverview->setModal(true);
        errorOverview->show();
    }
con's avatar
con committed
230
231
}

232
233
bool CorePlugin::delayedInitialize()
{
234
    HelpManager::setupHelpManager();
hjk's avatar
hjk committed
235
    m_locator->delayedInitialize();
236
237
238
    return true;
}

239
240
241
QObject *CorePlugin::remoteCommand(const QStringList & /* options */,
                                   const QString &workingDirectory,
                                   const QStringList &args)
con's avatar
con committed
242
{
243
    IDocument *res = m_mainWindow->openFiles(
244
                args, ICore::OpenFilesFlags(ICore::SwitchMode | ICore::CanContainLineAndColumnNumbers),
245
                workingDirectory);
246
    m_mainWindow->raiseWindow();
247
    return res;
248
249
250
251
}

void CorePlugin::fileOpenRequest(const QString &f)
{
252
    remoteCommand(QStringList(), QString(), QStringList(f));
con's avatar
con committed
253
254
}

255
ExtensionSystem::IPlugin::ShutdownFlag CorePlugin::aboutToShutdown()
256
{
257
    m_findPlugin->aboutToShutdown();
258
    m_mainWindow->aboutToShutdown();
259
    return SynchronousShutdown;
260
}