cd95678c8e6ad06bb80ad804ca39d04058cdaae8
[quassel.git] / src / qtui / qtuiapplication.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2014 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 <QStringList>
24
25 #ifdef HAVE_KDE
26 #  include <KStandardDirs>
27 #endif
28
29 #include "client.h"
30 #include "cliparser.h"
31 #include "mainwin.h"
32 #include "qtui.h"
33 #include "qtuisettings.h"
34
35 QtUiApplication::QtUiApplication(int &argc, char **argv)
36 #ifdef HAVE_KDE
37     : KApplication(),
38 #else
39     : QApplication(argc, argv),
40 #endif
41     Quassel(),
42     _aboutToQuit(false)
43 {
44 #ifdef HAVE_KDE
45     Q_UNUSED(argc); Q_UNUSED(argv);
46
47     // We need to setup KDE's data dirs
48     QStringList dataDirs = KGlobal::dirs()->findDirs("data", "");
49     for (int i = 0; i < dataDirs.count(); i++)
50         dataDirs[i].append("quassel/");
51     dataDirs.append(":/data/");
52     setDataDirPaths(dataDirs);
53
54 #else /* HAVE_KDE */
55
56     setDataDirPaths(findDataDirPaths());
57
58 #endif /* HAVE_KDE */
59
60 #if defined(HAVE_KDE) || defined(Q_OS_MAC)
61     disableCrashhandler();
62 #endif /* HAVE_KDE || Q_OS_MAC */
63     setRunMode(Quassel::ClientOnly);
64
65 #if QT_VERSION < 0x050000
66     qInstallMsgHandler(Client::logMessage);
67 #else
68     qInstallMessageHandler(Client::logMessage);
69 #endif
70 }
71
72
73 bool QtUiApplication::init()
74 {
75     if (Quassel::init()) {
76         // FIXME: MIGRATION 0.3 -> 0.4: Move database and core config to new location
77         // Move settings, note this does not delete the old files
78 #ifdef Q_OS_MAC
79         QSettings newSettings("quassel-irc.org", "quasselclient");
80 #else
81
82 # ifdef Q_OS_WIN
83         QSettings::Format format = QSettings::IniFormat;
84 # else
85         QSettings::Format format = QSettings::NativeFormat;
86 # endif
87
88         QString newFilePath = Quassel::configDirPath() + "quasselclient"
89                               + ((format == QSettings::NativeFormat) ? QLatin1String(".conf") : QLatin1String(".ini"));
90         QSettings newSettings(newFilePath, format);
91 #endif /* Q_OS_MAC */
92
93         if (newSettings.value("Config/Version").toUInt() == 0) {
94 #     ifdef Q_OS_MAC
95             QString org = "quassel-irc.org";
96 #     else
97             QString org = "Quassel Project";
98 #     endif
99             QSettings oldSettings(org, "Quassel Client");
100             if (oldSettings.allKeys().count()) {
101                 qWarning() << "\n\n*** IMPORTANT: Config and data file locations have changed. Attempting to auto-migrate your client settings...";
102                 foreach(QString key, oldSettings.allKeys())
103                 newSettings.setValue(key, oldSettings.value(key));
104                 newSettings.setValue("Config/Version", 1);
105                 qWarning() << "*   Your client settings have been migrated to" << newSettings.fileName();
106                 qWarning() << "*** Migration completed.\n\n";
107             }
108         }
109
110         // MIGRATION end
111
112         // check settings version
113         // so far, we only have 1
114         QtUiSettings s;
115         if (s.version() != 1) {
116             qCritical() << "Invalid client settings version, terminating!";
117             return false;
118         }
119
120         // session resume
121         QtUi *gui = new QtUi();
122         Client::init(gui);
123         // init gui only after the event loop has started
124         // QTimer::singleShot(0, gui, SLOT(init()));
125         gui->init();
126         resumeSessionIfPossible();
127         return true;
128     }
129     return false;
130 }
131
132
133 QtUiApplication::~QtUiApplication()
134 {
135     Client::destroy();
136 }
137
138
139 void QtUiApplication::quit()
140 {
141     QtUi::mainWindow()->quit();
142 }
143
144
145 void QtUiApplication::commitData(QSessionManager &manager)
146 {
147     Q_UNUSED(manager)
148     _aboutToQuit = true;
149 }
150
151
152 void QtUiApplication::saveState(QSessionManager &manager)
153 {
154     //qDebug() << QString("saving session state to id %1").arg(manager.sessionId());
155     // AccountId activeCore = Client::currentCoreAccount().accountId(); // FIXME store this!
156     SessionSettings s(manager.sessionId());
157     s.setSessionAge(0);
158     QtUi::mainWindow()->saveStateToSettings(s);
159 }
160
161
162 void QtUiApplication::resumeSessionIfPossible()
163 {
164     // load all sessions
165     if (isSessionRestored()) {
166         qDebug() << QString("restoring from session %1").arg(sessionId());
167         SessionSettings s(sessionId());
168         s.sessionAging();
169         s.setSessionAge(0);
170         QtUi::mainWindow()->restoreStateFromSettings(s);
171         s.cleanup();
172     }
173     else {
174         SessionSettings s(QString("1"));
175         s.sessionAging();
176         s.cleanup();
177     }
178 }