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