Bring copyright headers into 2016
[quassel.git] / src / qtui / qtuiapplication.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2016 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "qtuiapplication.h"
22
23 #include <QIcon>
24 #include <QStringList>
25
26 #ifdef HAVE_KDE4
27 #  include <KStandardDirs>
28 #endif
29
30 #include "client.h"
31 #include "cliparser.h"
32 #include "mainwin.h"
33 #include "qtui.h"
34 #include "qtuisettings.h"
35
36 QtUiApplication::QtUiApplication(int &argc, char **argv)
37 #ifdef HAVE_KDE4
38     : KApplication(),  // KApplication is deprecated in KF5
39 #else
40     : QApplication(argc, argv),
41 #endif
42     Quassel(),
43     _aboutToQuit(false)
44 {
45 #ifdef HAVE_KDE4
46     Q_UNUSED(argc); Q_UNUSED(argv);
47
48     // Setup KDE's data dirs
49     // Because we can't use KDE stuff in (the class) Quassel directly, we need to do this here...
50     QStringList dataDirs = KGlobal::dirs()->findDirs("data", "");
51
52     // Just in case, also check our install prefix
53     dataDirs << QCoreApplication::applicationDirPath() + "/../share/apps/";
54
55     // Normalize and append our application name
56     for (int i = 0; i < dataDirs.count(); i++)
57         dataDirs[i] = QDir::cleanPath(dataDirs.at(i)) + "/quassel/";
58
59     // Add resource path and just in case.
60     // Workdir should have precedence
61     dataDirs.prepend(QCoreApplication::applicationDirPath() + "/data/");
62     dataDirs.append(":/data/");
63
64     // Append trailing '/' and check for existence
65     auto iter = dataDirs.begin();
66     while (iter != dataDirs.end()) {
67         if (!iter->endsWith(QDir::separator()) && !iter->endsWith('/'))
68             iter->append(QDir::separator());
69         if (!QFile::exists(*iter))
70             iter = dataDirs.erase(iter);
71         else
72             ++iter;
73     }
74
75     dataDirs.removeDuplicates();
76     setDataDirPaths(dataDirs);
77
78 #else /* HAVE_KDE4 */
79
80     setDataDirPaths(findDataDirPaths());
81
82 #endif /* HAVE_KDE4 */
83
84 #if defined(HAVE_KDE4) || defined(Q_OS_MAC)
85     disableCrashhandler();
86 #endif /* HAVE_KDE4 || Q_OS_MAC */
87     setRunMode(Quassel::ClientOnly);
88
89 #if QT_VERSION < 0x050000
90     qInstallMsgHandler(Client::logMessage);
91 #else
92     qInstallMessageHandler(Client::logMessage);
93     connect(this, &QGuiApplication::commitDataRequest, this, &QtUiApplication::commitData, Qt::DirectConnection);
94     connect(this, &QGuiApplication::saveStateRequest, this, &QtUiApplication::saveState, Qt::DirectConnection);
95 #endif
96
97 #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
98     QGuiApplication::setFallbackSessionManagementEnabled(false);
99 #endif
100 }
101
102
103 bool QtUiApplication::init()
104 {
105     if (Quassel::init()) {
106         // FIXME: MIGRATION 0.3 -> 0.4: Move database and core config to new location
107         // Move settings, note this does not delete the old files
108 #ifdef Q_OS_MAC
109         QSettings newSettings("quassel-irc.org", "quasselclient");
110 #else
111
112 # ifdef Q_OS_WIN
113         QSettings::Format format = QSettings::IniFormat;
114 # else
115         QSettings::Format format = QSettings::NativeFormat;
116 # endif
117
118         QString newFilePath = Quassel::configDirPath() + "quasselclient"
119                               + ((format == QSettings::NativeFormat) ? QLatin1String(".conf") : QLatin1String(".ini"));
120         QSettings newSettings(newFilePath, format);
121 #endif /* Q_OS_MAC */
122
123         if (newSettings.value("Config/Version").toUInt() == 0) {
124 #     ifdef Q_OS_MAC
125             QString org = "quassel-irc.org";
126 #     else
127             QString org = "Quassel Project";
128 #     endif
129             QSettings oldSettings(org, "Quassel Client");
130             if (oldSettings.allKeys().count()) {
131                 qWarning() << "\n\n*** IMPORTANT: Config and data file locations have changed. Attempting to auto-migrate your client settings...";
132                 foreach(QString key, oldSettings.allKeys())
133                 newSettings.setValue(key, oldSettings.value(key));
134                 newSettings.setValue("Config/Version", 1);
135                 qWarning() << "*   Your client settings have been migrated to" << newSettings.fileName();
136                 qWarning() << "*** Migration completed.\n\n";
137             }
138         }
139
140         // MIGRATION end
141
142         // check settings version
143         // so far, we only have 1
144         QtUiSettings s;
145         if (s.version() != 1) {
146             qCritical() << "Invalid client settings version, terminating!";
147             return false;
148         }
149
150         // Set the icon theme
151         if (Quassel::isOptionSet("icontheme"))
152             QIcon::setThemeName(Quassel::optionValue("icontheme"));
153         else if (QIcon::themeName().isEmpty())
154             // Some platforms don't set a default icon theme; chances are we can find our bundled Oxygen theme though
155             QIcon::setThemeName("oxygen");
156
157         // session resume
158         QtUi *gui = new QtUi();
159         Client::init(gui);
160         // init gui only after the event loop has started
161         // QTimer::singleShot(0, gui, SLOT(init()));
162         gui->init();
163         resumeSessionIfPossible();
164         return true;
165     }
166     return false;
167 }
168
169
170 QtUiApplication::~QtUiApplication()
171 {
172     Client::destroy();
173 }
174
175
176 void QtUiApplication::quit()
177 {
178     QtUi::mainWindow()->quit();
179 }
180
181
182 void QtUiApplication::commitData(QSessionManager &manager)
183 {
184     Q_UNUSED(manager)
185     _aboutToQuit = true;
186 }
187
188
189 void QtUiApplication::saveState(QSessionManager &manager)
190 {
191     //qDebug() << QString("saving session state to id %1").arg(manager.sessionId());
192     // AccountId activeCore = Client::currentCoreAccount().accountId(); // FIXME store this!
193     SessionSettings s(manager.sessionId());
194     s.setSessionAge(0);
195     QtUi::mainWindow()->saveStateToSettings(s);
196 }
197
198
199 void QtUiApplication::resumeSessionIfPossible()
200 {
201     // load all sessions
202     if (isSessionRestored()) {
203         qDebug() << QString("restoring from session %1").arg(sessionId());
204         SessionSettings s(sessionId());
205         s.sessionAging();
206         s.setSessionAge(0);
207         QtUi::mainWindow()->restoreStateFromSettings(s);
208         s.cleanup();
209     }
210     else {
211         SessionSettings s(QString("1"));
212         s.sessionAging();
213         s.cleanup();
214     }
215 }