This could / should / I just wish it does / who the hell knows / whatever / <insert...
[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 #include "client.h"
26 #include "cliparser.h"
27 #include "qtui.h"
28 #include "sessionsettings.h"
29
30 QtUiApplication::QtUiApplication(int &argc, char **argv)
31 #ifdef HAVE_KDE
32   : KApplication(),
33 #else
34   : QApplication(argc, argv),
35 #endif
36     Quassel(),
37     _aboutToQuit(false)
38 {
39 #ifdef HAVE_KDE
40   Q_UNUSED(argc)
41   Q_UNUSED(argv)
42 #endif
43
44   setRunMode(Quassel::ClientOnly);
45
46   qInstallMsgHandler(Client::logMessage);
47 }
48
49 bool QtUiApplication::init() {
50   if(Quassel::init()) {
51     // session resume
52     QtUi *gui = new QtUi();
53     Client::init(gui);
54     // init gui only after the event loop has started
55     // QTimer::singleShot(0, gui, SLOT(init()));
56     gui->init();
57     resumeSessionIfPossible();
58     return true;
59   }
60   return false;
61 }
62
63 QtUiApplication::~QtUiApplication() {
64   Client::destroy();
65 }
66
67 void QtUiApplication::commitData(QSessionManager &manager) {
68   _aboutToQuit = true;
69 }
70
71 void QtUiApplication::saveState(QSessionManager & manager) {
72   //qDebug() << QString("saving session state to id %1").arg(manager.sessionId());
73   AccountId activeCore = Client::currentCoreAccount();
74   SessionSettings s(manager.sessionId());
75   s.setSessionAge(0);
76   emit saveStateToSession(manager.sessionId());
77   emit saveStateToSessionSettings(s);
78 }
79
80 void QtUiApplication::resumeSessionIfPossible() {
81   // load all sessions
82   if(isSessionRestored()) {
83     qDebug() << QString("restoring from session %1").arg(sessionId());
84     SessionSettings s(sessionId());
85     s.sessionAging();
86     s.setSessionAge(0);
87     emit resumeFromSession(sessionId());
88     emit resumeFromSessionSettings(s);
89     s.cleanup();
90   } else {
91     SessionSettings s(QString("1"));
92     s.sessionAging();
93     s.cleanup();
94   }
95 }
96
97