1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "qtuiapplication.h"
25 #include <QStringList>
27 #include "chatviewsettings.h"
28 #include "logmessage.h"
31 #include "qtuisettings.h"
34 QtUiApplication::QtUiApplication(int &argc, char **argv)
35 : QApplication(argc, argv)
37 connect(this, &QGuiApplication::commitDataRequest, this, &QtUiApplication::commitData, Qt::DirectConnection);
38 connect(this, &QGuiApplication::saveStateRequest, this, &QtUiApplication::saveState, Qt::DirectConnection);
40 #if QT_VERSION >= 0x050600
41 QGuiApplication::setFallbackSessionManagementEnabled(false);
46 void QtUiApplication::init()
48 // Settings upgrade/downgrade handling
49 if (!migrateSettings()) {
50 throw ExitException{EXIT_FAILURE, tr("Could not load or upgrade client settings!")};
53 _client.reset(new Client(std::unique_ptr<QtUi>(new QtUi()))); // TODO C++14: std::make_unique
55 // Init UI only after the event loop has started
56 // TODO Qt5: Make this a lambda
57 QTimer::singleShot(0, this, SLOT(initUi()));
61 void QtUiApplication::initUi()
63 QtUi::instance()->init();
65 // Needs to happen after UI init, so the MainWin quit handler is registered first
66 Quassel::registerQuitHandler(quitHandler());
68 resumeSessionIfPossible();
72 Quassel::QuitHandler QtUiApplication::quitHandler()
74 // Wait until the Client instance is destroyed before quitting the event loop
76 quInfo() << "Client shutting down...";
77 connect(_client.get(), SIGNAL(destroyed()), QCoreApplication::instance(), SLOT(quit()));
78 _client.release()->deleteLater();
83 bool QtUiApplication::migrateSettings()
86 // Check major settings version. This represents incompatible changes between settings
87 // versions. So far, we only have 1.
89 uint versionMajor = s.version();
90 if (versionMajor != 1) {
91 qCritical() << qPrintable(QString("Invalid client settings version '%1'")
97 // Check minor settings version, handling upgrades/downgrades as needed
98 // Current minor version
100 // NOTE: If you increase the minor version, you MUST ALSO add new version upgrade logic in
101 // applySettingsMigration()! Otherwise, settings upgrades will fail.
102 const uint VERSION_MINOR_CURRENT = 9;
103 // Stored minor version
104 uint versionMinor = s.versionMinor();
106 if (versionMinor == VERSION_MINOR_CURRENT) {
107 // At latest version, no need to migrate defaults or other settings
109 } else if (versionMinor == 0) {
110 // New configuration, store as current version
111 qDebug() << qPrintable(QString("Set up new client settings v%1.%2")
112 .arg(versionMajor).arg(VERSION_MINOR_CURRENT));
113 s.setVersionMinor(VERSION_MINOR_CURRENT);
115 // Update the settings stylesheet for first setup. We don't know if older content exists,
116 // if the configuration got erased separately, etc.
118 qtUiStyle.generateSettingsQss();
120 } else if (versionMinor < VERSION_MINOR_CURRENT) {
121 // We're upgrading - apply the neccessary upgrades from each interim version
122 // curVersion will never equal VERSION_MINOR_CURRENT, as it represents the version before
123 // the most recent applySettingsMigration() call.
124 for (uint curVersion = versionMinor; curVersion < VERSION_MINOR_CURRENT; curVersion++) {
125 if (!applySettingsMigration(s, curVersion + 1)) {
126 // Something went wrong, time to bail out
127 qCritical() << qPrintable(QString("Could not migrate client settings from v%1.%2 "
129 .arg(versionMajor).arg(curVersion).arg(curVersion + 1));
130 // Keep track of the last successful upgrade to avoid repeating it on next start
131 s.setVersionMinor(curVersion);
135 // Migration successful!
136 qDebug() << qPrintable(QString("Successfully migrated client settings from v%1.%2 to "
138 .arg(versionMajor).arg(versionMinor).arg(VERSION_MINOR_CURRENT));
139 // Store the new minor version
140 s.setVersionMinor(VERSION_MINOR_CURRENT);
143 // versionMinor > VERSION_MINOR_CURRENT
144 // The user downgraded to an older version of Quassel. Let's hope for the best.
145 // Don't change the minorVersion as the newer version's upgrade logic has already run.
146 qWarning() << qPrintable(QString("Client settings v%1.%2 is newer than latest known v%1.%3,"
147 " things might not work!")
148 .arg(versionMajor).arg(versionMinor).arg(VERSION_MINOR_CURRENT));
154 bool QtUiApplication::applySettingsMigration(QtUiSettings settings, const uint newVersion)
156 switch (newVersion) {
157 // Version 0 and 1 aren't valid upgrade paths - one represents no version, the other is the
158 // oldest version. Ignore those, start from 2 and higher.
159 // Each missed version will be called in sequence. E.g. to upgrade from '1' to '3', this
160 // function will be called with '2', then '3'.
161 // Use explicit scope via { ... } to avoid cross-initialization
163 // In most cases, the goal is to preserve the older default values for keys that haven't been
164 // saved. Exceptions will be noted below.
165 // NOTE: If you add new upgrade logic here, you MUST ALSO increase VERSION_MINOR_CURRENT in
166 // migrateSettings()! Otherwise, your upgrade logic won't ever be called.
169 // New default changes: show highest sender prefix mode, if available
173 ChatViewSettings chatViewSettings;
174 const QString senderPrefixModeId = "SenderPrefixMode";
175 if (!chatViewSettings.valueExists(senderPrefixModeId)) {
176 // New default is HighestMode, preserve previous behavior by setting to NoModes
177 chatViewSettings.setValue(senderPrefixModeId,
178 static_cast<int>(UiStyle::SenderPrefixMode::NoModes));
182 // Migration complete!
188 // New default changes: RegEx checkbox now toggles Channel regular expressions, too
190 // This only affects local highlights. Core-side highlights weren't released in stable when
191 // this change was made, so no need to migrate those.
194 // NotificationSettings
195 NotificationSettings notificationSettings;
197 // Check each highlight rule for a "Channel" field. If one exists, convert to RegEx mode.
198 // This might be more efficient with std::transform() or such. It /is/ only run once...
199 auto highlightList = notificationSettings.highlightList();
200 bool changesMade = false;
201 for (int index = 0; index < highlightList.count(); ++index)
203 // Load the highlight rule...
204 auto highlightRule = highlightList[index].toMap();
206 // Check if "Channel" has anything set and RegEx is disabled
207 if (!highlightRule["Channel"].toString().isEmpty()
208 && highlightRule["RegEx"].toBool() == false) {
209 // We have a rule to convert
211 // Mark as a regular expression, allowing the Channel filtering to work the same as
212 // before the upgrade
213 highlightRule["RegEx"] = true;
215 // Convert the main rule to regular expression, mirroring the conversion to wildcard
216 // format from QtUiMessageProcessor::checkForHighlight()
217 highlightRule["Name"] =
218 "(^|\\W)" + QRegExp::escape(highlightRule["Name"].toString()) + "(\\W|$)";
220 // Save the rule back
221 highlightList[index] = highlightRule;
226 // Save the modified rules if any changes were made
228 notificationSettings.setHighlightList(highlightList);
232 // Migration complete!
237 // New default changes: UseProxy is no longer used in CoreAccountSettings
238 CoreAccountSettings s;
239 for (auto &&accountId : s.knownAccounts()) {
240 auto map = s.retrieveAccountData(accountId);
241 if (!map.value("UseProxy", false).toBool()) {
242 map["ProxyType"] = static_cast<int>(QNetworkProxy::ProxyType::NoProxy);
244 map.remove("UseProxy");
245 s.storeAccountData(accountId, map);
248 // Migration complete!
253 // New default changes: sender colors switched around to Tango-ish theme
256 // QtUiStyle settings
257 QtUiStyleSettings settingsUiStyleColors("Colors");
258 // Preserve the old default values for all variants
259 const QColor oldDefaultSenderColorSelf = QColor(0, 0, 0);
260 const QList<QColor> oldDefaultSenderColors = QList<QColor> {
261 QColor(204, 13, 127), /// Sender00
262 QColor(142, 85, 233), /// Sender01
263 QColor(179, 14, 14), /// Sender02
264 QColor( 23, 179, 57), /// Sender03
265 QColor( 88, 175, 179), /// Sender04
266 QColor(157, 84, 179), /// Sender05
267 QColor(179, 151, 117), /// Sender06
268 QColor( 49, 118, 179), /// Sender07
269 QColor(233, 13, 127), /// Sender08
270 QColor(142, 85, 233), /// Sender09
271 QColor(179, 14, 14), /// Sender10
272 QColor( 23, 179, 57), /// Sender11
273 QColor( 88, 175, 179), /// Sender12
274 QColor(157, 84, 179), /// Sender13
275 QColor(179, 151, 117), /// Sender14
276 QColor( 49, 118, 179), /// Sender15
278 if (!settingsUiStyleColors.valueExists("SenderSelf")) {
279 // Preserve the old default sender color if none set
280 settingsUiStyleColors.setValue("SenderSelf", oldDefaultSenderColorSelf);
282 QString senderColorId;
283 for (int i = 0; i < oldDefaultSenderColors.count(); i++) {
284 // Get the sender color ID for each available color
285 QString dez = QString::number(i);
286 if (dez.length() == 1) dez.prepend('0');
287 senderColorId = QString("Sender" + dez);
288 if (!settingsUiStyleColors.valueExists(senderColorId)) {
289 // Preserve the old default sender color if none set
290 settingsUiStyleColors.setValue(senderColorId, oldDefaultSenderColors[i]);
294 // Update the settings stylesheet with old defaults
296 qtUiStyle.generateSettingsQss();
299 // Migration complete!
304 // New default changes: sender colors apply to nearly all messages with nicks
307 // QtUiStyle settings
308 QtUiStyleSettings settingsUiStyleColors("Colors");
309 const QString useNickGeneralColorsId = "UseNickGeneralColors";
310 if (!settingsUiStyleColors.valueExists(useNickGeneralColorsId)) {
311 // New default is true, preserve previous behavior by setting to false
312 settingsUiStyleColors.setValue(useNickGeneralColorsId, false);
315 // Update the settings stylesheet with old defaults
317 qtUiStyle.generateSettingsQss();
320 // Migration complete!
325 // New default changes: system locale used to generate a timestamp format string, deciding
326 // 24-hour or 12-hour timestamp.
330 const QString useCustomTimestampFormatId = "ChatView/__default__/UseCustomTimestampFormat";
331 if (!settings.valueExists(useCustomTimestampFormatId)) {
332 // New default value is false, preserve previous behavior by setting to true
333 settings.setValue(useCustomTimestampFormatId, true);
337 // Migration complete!
342 // New default changes: per-chat history and line wrapping enabled by default.
345 // InputWidget settings
346 UiSettings settingsInputWidget("InputWidget");
347 const QString enableInputPerChatId = "EnablePerChatHistory";
348 if (!settingsInputWidget.valueExists(enableInputPerChatId)) {
349 // New default value is true, preserve previous behavior by setting to false
350 settingsInputWidget.setValue(enableInputPerChatId, false);
353 const QString enableInputLinewrap = "EnableLineWrap";
354 if (!settingsInputWidget.valueExists(enableInputLinewrap)) {
355 // New default value is true, preserve previous behavior by setting to false
356 settingsInputWidget.setValue(enableInputLinewrap, false);
360 // Migration complete!
365 // New default changes: sender <nick> brackets disabled, sender colors and sender CTCP
370 const QString timestampFormatId = "ChatView/__default__/TimestampFormat";
371 if (!settings.valueExists(timestampFormatId)) {
372 // New default value is " hh:mm:ss", preserve old default of "[hh:mm:ss]"
373 settings.setValue(timestampFormatId, "[hh:mm:ss]");
376 const QString showSenderBracketsId = "ChatView/__default__/ShowSenderBrackets";
377 if (!settings.valueExists(showSenderBracketsId)) {
378 // New default is false, preserve previous behavior by setting to true
379 settings.setValue(showSenderBracketsId, true);
384 // QtUiStyle settings
385 QtUiStyleSettings settingsUiStyleColors("Colors");
386 const QString useSenderColorsId = "UseSenderColors";
387 if (!settingsUiStyleColors.valueExists(useSenderColorsId)) {
388 // New default is true, preserve previous behavior by setting to false
389 settingsUiStyleColors.setValue(useSenderColorsId, false);
392 const QString useSenderActionColorsId = "UseSenderActionColors";
393 if (!settingsUiStyleColors.valueExists(useSenderActionColorsId)) {
394 // New default is true, preserve previous behavior by setting to false
395 settingsUiStyleColors.setValue(useSenderActionColorsId, false);
398 // Update the settings stylesheet with old defaults
400 qtUiStyle.generateSettingsQss();
403 // Migration complete!
407 // Something unexpected happened
413 void QtUiApplication::commitData(QSessionManager &manager)
420 void QtUiApplication::saveState(QSessionManager &manager)
422 //qDebug() << QString("saving session state to id %1").arg(manager.sessionId());
423 // AccountId activeCore = Client::currentCoreAccount().accountId(); // FIXME store this!
424 SessionSettings s(manager.sessionId());
426 QtUi::mainWindow()->saveStateToSettings(s);
430 void QtUiApplication::resumeSessionIfPossible()
433 if (isSessionRestored()) {
434 qDebug() << QString("restoring from session %1").arg(sessionId());
435 SessionSettings s(sessionId());
438 QtUi::mainWindow()->restoreStateFromSettings(s);
442 SessionSettings s(QString("1"));