1 /***************************************************************************
2 * Copyright (C) 2005-2016 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>
28 # include <KStandardDirs>
32 #include "cliparser.h"
35 #include "qtuisettings.h"
37 QtUiApplication::QtUiApplication(int &argc, char **argv)
39 : KApplication(), // KApplication is deprecated in KF5
41 : QApplication(argc, argv),
47 Q_UNUSED(argc); Q_UNUSED(argv);
49 // Setup KDE's data dirs
50 // Because we can't use KDE stuff in (the class) Quassel directly, we need to do this here...
51 QStringList dataDirs = KGlobal::dirs()->findDirs("data", "");
53 // Just in case, also check our install prefix
54 dataDirs << QCoreApplication::applicationDirPath() + "/../share/apps/";
56 // Normalize and append our application name
57 for (int i = 0; i < dataDirs.count(); i++)
58 dataDirs[i] = QDir::cleanPath(dataDirs.at(i)) + "/quassel/";
60 // Add resource path and just in case.
61 // Workdir should have precedence
62 dataDirs.prepend(QCoreApplication::applicationDirPath() + "/data/");
63 dataDirs.append(":/data/");
65 // Append trailing '/' and check for existence
66 auto iter = dataDirs.begin();
67 while (iter != dataDirs.end()) {
68 if (!iter->endsWith(QDir::separator()) && !iter->endsWith('/'))
69 iter->append(QDir::separator());
70 if (!QFile::exists(*iter))
71 iter = dataDirs.erase(iter);
76 dataDirs.removeDuplicates();
77 setDataDirPaths(dataDirs);
81 setDataDirPaths(findDataDirPaths());
83 #endif /* HAVE_KDE4 */
85 #if defined(HAVE_KDE4) || defined(Q_OS_MAC)
86 disableCrashhandler();
87 #endif /* HAVE_KDE4 || Q_OS_MAC */
88 setRunMode(Quassel::ClientOnly);
90 #if QT_VERSION < 0x050000
91 qInstallMsgHandler(Client::logMessage);
93 qInstallMessageHandler(Client::logMessage);
94 connect(this, &QGuiApplication::commitDataRequest, this, &QtUiApplication::commitData, Qt::DirectConnection);
95 connect(this, &QGuiApplication::saveStateRequest, this, &QtUiApplication::saveState, Qt::DirectConnection);
98 #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
99 QGuiApplication::setFallbackSessionManagementEnabled(false);
104 bool QtUiApplication::init()
106 if (Quassel::init()) {
107 // FIXME: MIGRATION 0.3 -> 0.4: Move database and core config to new location
108 // Move settings, note this does not delete the old files
110 QSettings newSettings("quassel-irc.org", "quasselclient");
114 QSettings::Format format = QSettings::IniFormat;
116 QSettings::Format format = QSettings::NativeFormat;
119 QString newFilePath = Quassel::configDirPath() + "quasselclient"
120 + ((format == QSettings::NativeFormat) ? QLatin1String(".conf") : QLatin1String(".ini"));
121 QSettings newSettings(newFilePath, format);
122 #endif /* Q_OS_MAC */
124 if (newSettings.value("Config/Version").toUInt() == 0) {
126 QString org = "quassel-irc.org";
128 QString org = "Quassel Project";
130 QSettings oldSettings(org, "Quassel Client");
131 if (oldSettings.allKeys().count()) {
132 qWarning() << "\n\n*** IMPORTANT: Config and data file locations have changed. Attempting to auto-migrate your client settings...";
133 foreach(QString key, oldSettings.allKeys())
134 newSettings.setValue(key, oldSettings.value(key));
135 newSettings.setValue("Config/Version", 1);
136 qWarning() << "* Your client settings have been migrated to" << newSettings.fileName();
137 qWarning() << "*** Migration completed.\n\n";
143 // Settings upgrade/downgrade handling
144 if (!migrateSettings()) {
145 qCritical() << "Could not load or upgrade client settings, terminating!";
149 // Checking if settings Icon Theme is valid
150 QString savedIcontheme = QtUiSettings().value("IconTheme", QVariant("")).toString();
152 if (savedIcontheme == "oxygen")
153 QtUiSettings().remove("IconTheme");
156 if (savedIcontheme == "breeze")
157 QtUiSettings().remove("IconTheme");
159 #ifndef WITH_BREEZE_DARK
160 if (savedIcontheme == "breezedark")
161 QtUiSettings().remove("IconTheme");
164 // Set the icon theme
165 if (Quassel::isOptionSet("icontheme"))
166 QIcon::setThemeName(Quassel::optionValue("icontheme"));
167 else if (QtUiSettings().value("IconTheme", QVariant("")).toString() != "")
168 QIcon::setThemeName(QtUiSettings().value("IconTheme").toString());
169 else if (QIcon::themeName().isEmpty())
170 // Some platforms don't set a default icon theme; chances are we can find our bundled theme though
171 QIcon::setThemeName("breeze");
174 QtUi *gui = new QtUi();
176 // init gui only after the event loop has started
177 // QTimer::singleShot(0, gui, SLOT(init()));
179 resumeSessionIfPossible();
186 QtUiApplication::~QtUiApplication()
192 void QtUiApplication::quit()
194 QtUi::mainWindow()->quit();
198 bool QtUiApplication::migrateSettings()
201 // Check major settings version. This represents incompatible changes between settings
202 // versions. So far, we only have 1.
204 uint versionMajor = s.version();
205 if (versionMajor != 1) {
206 qCritical() << qPrintable(QString("Invalid client settings version '%1'")
212 // Check minor settings version, handling upgrades/downgrades as needed
213 // Current minor version
215 // NOTE: If you increase the minor version, you MUST ALSO add new version upgrade logic in
216 // applySettingsMigration()! Otherwise, settings upgrades will fail.
217 const uint VERSION_MINOR_CURRENT = 7;
218 // Stored minor version
219 uint versionMinor = s.versionMinor();
221 if (versionMinor == VERSION_MINOR_CURRENT) {
222 // At latest version, no need to migrate defaults or other settings
224 } else if (versionMinor == 0) {
225 // New configuration, store as current version
226 qDebug() << qPrintable(QString("Set up new client settings v%1.%2")
227 .arg(versionMajor).arg(VERSION_MINOR_CURRENT));
228 s.setVersionMinor(VERSION_MINOR_CURRENT);
230 // Update the settings stylesheet for first setup. We don't know if older content exists,
231 // if the configuration got erased separately, etc.
233 qtUiStyle.generateSettingsQss();
235 } else if (versionMinor < VERSION_MINOR_CURRENT) {
236 // We're upgrading - apply the neccessary upgrades from each interim version
237 // curVersion will never equal VERSION_MINOR_CURRENT, as it represents the version before
238 // the most recent applySettingsMigration() call.
239 for (uint curVersion = versionMinor; curVersion < VERSION_MINOR_CURRENT; curVersion++) {
240 if (!applySettingsMigration(s, curVersion + 1)) {
241 // Something went wrong, time to bail out
242 qCritical() << qPrintable(QString("Could not migrate client settings from v%1.%2 "
244 .arg(versionMajor).arg(curVersion).arg(curVersion + 1));
245 // Keep track of the last successful upgrade to avoid repeating it on next start
246 s.setVersionMinor(curVersion);
250 // Migration successful!
251 qDebug() << qPrintable(QString("Successfully migrated client settings from v%1.%2 to "
253 .arg(versionMajor).arg(versionMinor).arg(VERSION_MINOR_CURRENT));
254 // Store the new minor version
255 s.setVersionMinor(VERSION_MINOR_CURRENT);
258 // versionMinor > VERSION_MINOR_CURRENT
259 // The user downgraded to an older version of Quassel. Let's hope for the best.
260 // Don't change the minorVersion as the newer version's upgrade logic has already run.
261 qWarning() << qPrintable(QString("Client settings v%1.%2 is newer than latest known v%1.%3,"
262 " things might not work!")
263 .arg(versionMajor).arg(versionMinor).arg(VERSION_MINOR_CURRENT));
269 bool QtUiApplication::applySettingsMigration(QtUiSettings settings, const uint newVersion)
271 switch (newVersion) {
272 // Version 0 and 1 aren't valid upgrade paths - one represents no version, the other is the
273 // oldest version. Ignore those, start from 2 and higher.
274 // Each missed version will be called in sequence. E.g. to upgrade from '1' to '3', this
275 // function will be called with '2', then '3'.
276 // Use explicit scope via { ... } to avoid cross-initialization
278 // In most cases, the goal is to preserve the older default values for keys that haven't been
279 // saved. Exceptions will be noted below.
280 // NOTE: If you add new upgrade logic here, you MUST ALSO increase VERSION_MINOR_CURRENT in
281 // migrateSettings()! Otherwise, your upgrade logic won't ever be called.
284 // New default changes: UseProxy is no longer used in CoreAccountSettings
285 CoreAccountSettings s;
286 for (auto &&accountId : s.knownAccounts()) {
287 auto map = s.retrieveAccountData(accountId);
288 if (!map.value("UseProxy", false).toBool()) {
289 map["ProxyType"] = static_cast<int>(QNetworkProxy::ProxyType::NoProxy);
291 map.remove("UseProxy");
292 s.storeAccountData(accountId, map);
295 // Migration complete!
300 // New default changes: sender colors switched around to Tango-ish theme
303 // QtUiStyle settings
304 QtUiStyleSettings settingsUiStyleColors("Colors");
305 // Preserve the old default values for all variants
306 const QColor oldDefaultSenderColorSelf = QColor(0, 0, 0);
307 const QList<QColor> oldDefaultSenderColors = QList<QColor> {
308 QColor(204, 13, 127), /// Sender00
309 QColor(142, 85, 233), /// Sender01
310 QColor(179, 14, 14), /// Sender02
311 QColor( 23, 179, 57), /// Sender03
312 QColor( 88, 175, 179), /// Sender04
313 QColor(157, 84, 179), /// Sender05
314 QColor(179, 151, 117), /// Sender06
315 QColor( 49, 118, 179), /// Sender07
316 QColor(233, 13, 127), /// Sender08
317 QColor(142, 85, 233), /// Sender09
318 QColor(179, 14, 14), /// Sender10
319 QColor( 23, 179, 57), /// Sender11
320 QColor( 88, 175, 179), /// Sender12
321 QColor(157, 84, 179), /// Sender13
322 QColor(179, 151, 117), /// Sender14
323 QColor( 49, 118, 179), /// Sender15
325 if (!settingsUiStyleColors.valueExists("SenderSelf")) {
326 // Preserve the old default sender color if none set
327 settingsUiStyleColors.setValue("SenderSelf", oldDefaultSenderColorSelf);
329 QString senderColorId;
330 for (int i = 0; i < oldDefaultSenderColors.count(); i++) {
331 // Get the sender color ID for each available color
332 QString dez = QString::number(i);
333 if (dez.length() == 1) dez.prepend('0');
334 senderColorId = QString("Sender" + dez);
335 if (!settingsUiStyleColors.valueExists(senderColorId)) {
336 // Preserve the old default sender color if none set
337 settingsUiStyleColors.setValue(senderColorId, oldDefaultSenderColors[i]);
341 // Update the settings stylesheet with old defaults
343 qtUiStyle.generateSettingsQss();
346 // Migration complete!
351 // New default changes: sender colors apply to nearly all messages with nicks
354 // QtUiStyle settings
355 QtUiStyleSettings settingsUiStyleColors("Colors");
356 const QString useNickGeneralColorsId = "UseNickGeneralColors";
357 if (!settingsUiStyleColors.valueExists(useNickGeneralColorsId)) {
358 // New default is true, preserve previous behavior by setting to false
359 settingsUiStyleColors.setValue(useNickGeneralColorsId, false);
362 // Update the settings stylesheet with old defaults
364 qtUiStyle.generateSettingsQss();
367 // Migration complete!
372 // New default changes: system locale used to generate a timestamp format string, deciding
373 // 24-hour or 12-hour timestamp.
377 const QString useCustomTimestampFormatId = "ChatView/__default__/UseCustomTimestampFormat";
378 if (!settings.valueExists(useCustomTimestampFormatId)) {
379 // New default value is false, preserve previous behavior by setting to true
380 settings.setValue(useCustomTimestampFormatId, true);
384 // Migration complete!
389 // New default changes: per-chat history and line wrapping enabled by default.
392 // InputWidget settings
393 UiSettings settingsInputWidget("InputWidget");
394 const QString enableInputPerChatId = "EnablePerChatHistory";
395 if (!settingsInputWidget.valueExists(enableInputPerChatId)) {
396 // New default value is true, preserve previous behavior by setting to false
397 settingsInputWidget.setValue(enableInputPerChatId, false);
400 const QString enableInputLinewrap = "EnableLineWrap";
401 if (!settingsInputWidget.valueExists(enableInputLinewrap)) {
402 // New default value is true, preserve previous behavior by setting to false
403 settingsInputWidget.setValue(enableInputLinewrap, false);
407 // Migration complete!
412 // New default changes: sender <nick> brackets disabled, sender colors and sender CTCP
417 const QString timestampFormatId = "ChatView/__default__/TimestampFormat";
418 if (!settings.valueExists(timestampFormatId)) {
419 // New default value is " hh:mm:ss", preserve old default of "[hh:mm:ss]"
420 settings.setValue(timestampFormatId, "[hh:mm:ss]");
423 const QString showSenderBracketsId = "ChatView/__default__/ShowSenderBrackets";
424 if (!settings.valueExists(showSenderBracketsId)) {
425 // New default is false, preserve previous behavior by setting to true
426 settings.setValue(showSenderBracketsId, true);
431 // QtUiStyle settings
432 QtUiStyleSettings settingsUiStyleColors("Colors");
433 const QString useSenderColorsId = "UseSenderColors";
434 if (!settingsUiStyleColors.valueExists(useSenderColorsId)) {
435 // New default is true, preserve previous behavior by setting to false
436 settingsUiStyleColors.setValue(useSenderColorsId, false);
439 const QString useSenderActionColorsId = "UseSenderActionColors";
440 if (!settingsUiStyleColors.valueExists(useSenderActionColorsId)) {
441 // New default is true, preserve previous behavior by setting to false
442 settingsUiStyleColors.setValue(useSenderActionColorsId, false);
445 // Update the settings stylesheet with old defaults
447 qtUiStyle.generateSettingsQss();
450 // Migration complete!
454 // Something unexpected happened
460 void QtUiApplication::commitData(QSessionManager &manager)
467 void QtUiApplication::saveState(QSessionManager &manager)
469 //qDebug() << QString("saving session state to id %1").arg(manager.sessionId());
470 // AccountId activeCore = Client::currentCoreAccount().accountId(); // FIXME store this!
471 SessionSettings s(manager.sessionId());
473 QtUi::mainWindow()->saveStateToSettings(s);
477 void QtUiApplication::resumeSessionIfPossible()
480 if (isSessionRestored()) {
481 qDebug() << QString("restoring from session %1").arg(sessionId());
482 SessionSettings s(sessionId());
485 QtUi::mainWindow()->restoreStateFromSettings(s);
489 SessionSettings s(QString("1"));