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