88b8bdb18544472030c42f64c98b201dee1c9b28
[quassel.git] / src / qtui / qtuiapplication.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2015 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "qtuiapplication.h"
22
23 #include <QIcon>
24 #include <QStringList>
25
26 #ifdef HAVE_KDE4
27 #  include <KStandardDirs>
28 #endif
29
30 #include "client.h"
31 #include "cliparser.h"
32 #include "mainwin.h"
33 #include "qtui.h"
34 #include "qtuisettings.h"
35
36
37 QtUiApplication::QtUiApplication(int &argc, char **argv)
38 #ifdef HAVE_KDE4
39     : KApplication(),  // KApplication is deprecated in KF5
40 #else
41     : QApplication(argc, argv),
42 #endif
43     Quassel(),
44     _aboutToQuit(false)
45 {
46 #ifdef HAVE_KDE4
47     Q_UNUSED(argc); Q_UNUSED(argv);
48
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", "");
52
53     // Just in case, also check our install prefix
54     dataDirs << QCoreApplication::applicationDirPath() + "/../share/apps/";
55
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/";
59
60     // Add resource path and just in case.
61     // Workdir should have precedence
62     dataDirs.prepend(QCoreApplication::applicationDirPath() + "/data/");
63     dataDirs.append(":/data/");
64
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);
72         else
73             ++iter;
74     }
75
76     dataDirs.removeDuplicates();
77     setDataDirPaths(dataDirs);
78
79 #else /* HAVE_KDE4 */
80
81     setDataDirPaths(findDataDirPaths());
82
83 #endif /* HAVE_KDE4 */
84
85 #if defined(HAVE_KDE4) || defined(Q_OS_MAC)
86     disableCrashhandler();
87 #endif /* HAVE_KDE4 || Q_OS_MAC */
88     setRunMode(Quassel::ClientOnly);
89
90 #if QT_VERSION < 0x050000
91     qInstallMsgHandler(Client::logMessage);
92 #else
93     qInstallMessageHandler(Client::logMessage);
94 #endif
95 }
96
97
98 bool QtUiApplication::init()
99 {
100     if (Quassel::init()) {
101         // FIXME: MIGRATION 0.3 -> 0.4: Move database and core config to new location
102         // Move settings, note this does not delete the old files
103 #ifdef Q_OS_MAC
104         QSettings newSettings("quassel-irc.org", "quasselclient");
105 #else
106
107 # ifdef Q_OS_WIN
108         QSettings::Format format = QSettings::IniFormat;
109 # else
110         QSettings::Format format = QSettings::NativeFormat;
111 # endif
112
113         QString newFilePath = Quassel::configDirPath() + "quasselclient"
114                               + ((format == QSettings::NativeFormat) ? QLatin1String(".conf") : QLatin1String(".ini"));
115         QSettings newSettings(newFilePath, format);
116 #endif /* Q_OS_MAC */
117
118         if (newSettings.value("Config/Version").toUInt() == 0) {
119 #     ifdef Q_OS_MAC
120             QString org = "quassel-irc.org";
121 #     else
122             QString org = "Quassel Project";
123 #     endif
124             QSettings oldSettings(org, "Quassel Client");
125             if (oldSettings.allKeys().count()) {
126                 qWarning() << "\n\n*** IMPORTANT: Config and data file locations have changed. Attempting to auto-migrate your client settings...";
127                 foreach(QString key, oldSettings.allKeys())
128                 newSettings.setValue(key, oldSettings.value(key));
129                 newSettings.setValue("Config/Version", 1);
130                 qWarning() << "*   Your client settings have been migrated to" << newSettings.fileName();
131                 qWarning() << "*** Migration completed.\n\n";
132             }
133         }
134
135         // MIGRATION end
136
137         // check settings version
138         // so far, we only have 1
139         QtUiSettings s;
140         if (s.version() != 1) {
141             qCritical() << "Invalid client settings version, terminating!";
142             return false;
143         }
144
145         // Set the icon theme
146         if (Quassel::isOptionSet("icontheme"))
147             QIcon::setThemeName(Quassel::optionValue("icontheme"));
148         else if (QIcon::themeName().isEmpty())
149             // Some platforms don't set a default icon theme; chances are we can find our bundled Oxygen theme though
150             QIcon::setThemeName("oxygen");
151
152         // session resume
153         QtUi *gui = new QtUi();
154         Client::init(gui);
155         // init gui only after the event loop has started
156         // QTimer::singleShot(0, gui, SLOT(init()));
157         gui->init();
158         resumeSessionIfPossible();
159         return true;
160     }
161     return false;
162 }
163
164
165 QtUiApplication::~QtUiApplication()
166 {
167     Client::destroy();
168 }
169
170
171 void QtUiApplication::quit()
172 {
173     QtUi::mainWindow()->quit();
174 }
175
176
177 void QtUiApplication::commitData(QSessionManager &manager)
178 {
179     Q_UNUSED(manager)
180     _aboutToQuit = true;
181 }
182
183
184 void QtUiApplication::saveState(QSessionManager &manager)
185 {
186     //qDebug() << QString("saving session state to id %1").arg(manager.sessionId());
187     // AccountId activeCore = Client::currentCoreAccount().accountId(); // FIXME store this!
188     SessionSettings s(manager.sessionId());
189     s.setSessionAge(0);
190     QtUi::mainWindow()->saveStateToSettings(s);
191 }
192
193
194 void QtUiApplication::resumeSessionIfPossible()
195 {
196     // load all sessions
197     if (isSessionRestored()) {
198         qDebug() << QString("restoring from session %1").arg(sessionId());
199         SessionSettings s(sessionId());
200         s.sessionAging();
201         s.setSessionAge(0);
202         QtUi::mainWindow()->restoreStateFromSettings(s);
203         s.cleanup();
204     }
205     else {
206         SessionSettings s(QString("1"));
207         s.sessionAging();
208         s.cleanup();
209     }
210 }