Provide Quassel::configDirPath() and Quassel::findDataFilePath()
[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 "sessionsettings.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     // session resume
67     QtUi *gui = new QtUi();
68     Client::init(gui);
69     // init gui only after the event loop has started
70     // QTimer::singleShot(0, gui, SLOT(init()));
71     gui->init();
72     resumeSessionIfPossible();
73     return true;
74   }
75   return false;
76 }
77
78 QtUiApplication::~QtUiApplication() {
79   Client::destroy();
80 }
81
82 void QtUiApplication::commitData(QSessionManager &manager) {
83   _aboutToQuit = true;
84 }
85
86 void QtUiApplication::saveState(QSessionManager & manager) {
87   //qDebug() << QString("saving session state to id %1").arg(manager.sessionId());
88   AccountId activeCore = Client::currentCoreAccount();
89   SessionSettings s(manager.sessionId());
90   s.setSessionAge(0);
91   emit saveStateToSession(manager.sessionId());
92   emit saveStateToSessionSettings(s);
93 }
94
95 void QtUiApplication::resumeSessionIfPossible() {
96   // load all sessions
97   if(isSessionRestored()) {
98     qDebug() << QString("restoring from session %1").arg(sessionId());
99     SessionSettings s(sessionId());
100     s.sessionAging();
101     s.setSessionAge(0);
102     emit resumeFromSession(sessionId());
103     emit resumeFromSessionSettings(s);
104     s.cleanup();
105   } else {
106     SessionSettings s(QString("1"));
107     s.sessionAging();
108     s.cleanup();
109   }
110 }
111
112