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