Use Qt's native icon theme support rather than our own
[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 <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     // Some platforms don't set a default icon theme; chances are we can find our bundled Oxygen theme though
73     if (QIcon::themeName().isEmpty())
74         QIcon::setThemeName("oxygen");
75 }
76
77
78 bool QtUiApplication::init()
79 {
80     if (Quassel::init()) {
81         // FIXME: MIGRATION 0.3 -> 0.4: Move database and core config to new location
82         // Move settings, note this does not delete the old files
83 #ifdef Q_OS_MAC
84         QSettings newSettings("quassel-irc.org", "quasselclient");
85 #else
86
87 # ifdef Q_OS_WIN
88         QSettings::Format format = QSettings::IniFormat;
89 # else
90         QSettings::Format format = QSettings::NativeFormat;
91 # endif
92
93         QString newFilePath = Quassel::configDirPath() + "quasselclient"
94                               + ((format == QSettings::NativeFormat) ? QLatin1String(".conf") : QLatin1String(".ini"));
95         QSettings newSettings(newFilePath, format);
96 #endif /* Q_OS_MAC */
97
98         if (newSettings.value("Config/Version").toUInt() == 0) {
99 #     ifdef Q_OS_MAC
100             QString org = "quassel-irc.org";
101 #     else
102             QString org = "Quassel Project";
103 #     endif
104             QSettings oldSettings(org, "Quassel Client");
105             if (oldSettings.allKeys().count()) {
106                 qWarning() << "\n\n*** IMPORTANT: Config and data file locations have changed. Attempting to auto-migrate your client settings...";
107                 foreach(QString key, oldSettings.allKeys())
108                 newSettings.setValue(key, oldSettings.value(key));
109                 newSettings.setValue("Config/Version", 1);
110                 qWarning() << "*   Your client settings have been migrated to" << newSettings.fileName();
111                 qWarning() << "*** Migration completed.\n\n";
112             }
113         }
114
115         // MIGRATION end
116
117         // check settings version
118         // so far, we only have 1
119         QtUiSettings s;
120         if (s.version() != 1) {
121             qCritical() << "Invalid client settings version, terminating!";
122             return false;
123         }
124
125         // session resume
126         QtUi *gui = new QtUi();
127         Client::init(gui);
128         // init gui only after the event loop has started
129         // QTimer::singleShot(0, gui, SLOT(init()));
130         gui->init();
131         resumeSessionIfPossible();
132         return true;
133     }
134     return false;
135 }
136
137
138 QtUiApplication::~QtUiApplication()
139 {
140     Client::destroy();
141 }
142
143
144 void QtUiApplication::quit()
145 {
146     QtUi::mainWindow()->quit();
147 }
148
149
150 void QtUiApplication::commitData(QSessionManager &manager)
151 {
152     Q_UNUSED(manager)
153     _aboutToQuit = true;
154 }
155
156
157 void QtUiApplication::saveState(QSessionManager &manager)
158 {
159     //qDebug() << QString("saving session state to id %1").arg(manager.sessionId());
160     // AccountId activeCore = Client::currentCoreAccount().accountId(); // FIXME store this!
161     SessionSettings s(manager.sessionId());
162     s.setSessionAge(0);
163     QtUi::mainWindow()->saveStateToSettings(s);
164 }
165
166
167 void QtUiApplication::resumeSessionIfPossible()
168 {
169     // load all sessions
170     if (isSessionRestored()) {
171         qDebug() << QString("restoring from session %1").arg(sessionId());
172         SessionSettings s(sessionId());
173         s.sessionAging();
174         s.setSessionAge(0);
175         QtUi::mainWindow()->restoreStateFromSettings(s);
176         s.cleanup();
177     }
178     else {
179         SessionSettings s(QString("1"));
180         s.sessionAging();
181         s.cleanup();
182     }
183 }