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