Categories in the settings dialog are now clickable
[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 #include "sessionsettings.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   setRunMode(Quassel::ClientOnly);
61
62   qInstallMsgHandler(Client::logMessage);
63 }
64
65 bool QtUiApplication::init() {
66   if(Quassel::init()) {
67
68     // FIXME: MIGRATION 0.3 -> 0.4: Move database and core config to new location
69     // Move settings, note this does not delete the old files
70 #ifdef Q_WS_MAC
71     QSettings newSettings("quassel-irc.org", "quasselclient");
72 #else
73
74 # ifdef Q_WS_WIN
75     QSettings::Format format = QSettings::IniFormat;
76 # else
77     QSettings::Format format = QSettings::NativeFormat;
78 # endif
79
80     QString newFilePath = Quassel::configDirPath() + "quasselclient"
81     + ((format == QSettings::NativeFormat) ? QLatin1String(".conf") : QLatin1String(".ini"));
82     QSettings newSettings(newFilePath, format);
83 #endif /* Q_WS_MAC */
84
85     if(newSettings.value("Config/Version").toUInt() == 0) {
86 #     ifdef Q_WS_MAC
87         QString org = "quassel-irc.org";
88 #     else
89         QString org = "Quassel Project";
90 #     endif
91       QSettings oldSettings(org, "Quassel Client");
92       if(oldSettings.allKeys().count()) {
93         qWarning() << "\n\n*** IMPORTANT: Config and data file locations have changed. Attempting to auto-migrate your client settings...";
94         foreach(QString key, oldSettings.allKeys())
95           newSettings.setValue(key, oldSettings.value(key));
96         newSettings.setValue("Config/Version", 1);
97         qWarning() << "*   Your client settings have been migrated to" << newSettings.fileName();
98         qWarning() << "*** Migration completed.\n\n";
99       }
100     }
101
102     // MIGRATION end
103
104     // check settings version
105     // so far, we only have 1
106     QtUiSettings s;
107     if(s.version() != 1) {
108       qCritical() << "Invalid client settings version, terminating!";
109       return false;
110     }
111
112     // session resume
113     QtUi *gui = new QtUi();
114     Client::init(gui);
115     // init gui only after the event loop has started
116     // QTimer::singleShot(0, gui, SLOT(init()));
117     gui->init();
118     resumeSessionIfPossible();
119     return true;
120   }
121   return false;
122 }
123
124 QtUiApplication::~QtUiApplication() {
125   Client::destroy();
126 }
127
128 void QtUiApplication::commitData(QSessionManager &manager) {
129   Q_UNUSED(manager)
130   _aboutToQuit = true;
131 }
132
133 void QtUiApplication::saveState(QSessionManager & manager) {
134   //qDebug() << QString("saving session state to id %1").arg(manager.sessionId());
135   AccountId activeCore = Client::currentCoreAccount();
136   SessionSettings s(manager.sessionId());
137   s.setSessionAge(0);
138   emit saveStateToSession(manager.sessionId());
139   emit saveStateToSessionSettings(s);
140 }
141
142 void QtUiApplication::resumeSessionIfPossible() {
143   // load all sessions
144   if(isSessionRestored()) {
145     qDebug() << QString("restoring from session %1").arg(sessionId());
146     SessionSettings s(sessionId());
147     s.sessionAging();
148     s.setSessionAge(0);
149     emit resumeFromSession(sessionId());
150     emit resumeFromSessionSettings(s);
151     s.cleanup();
152   } else {
153     SessionSettings s(QString("1"));
154     s.sessionAging();
155     s.cleanup();
156   }
157 }