Some cleanups
[quassel.git] / src / core / coreapplication.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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 "coreapplication.h"
22
23 #include "core.h"
24 #include "logger.h"
25
26 CoreApplicationInternal::CoreApplicationInternal()
27     : _coreCreated(false)
28 {
29 }
30
31
32 CoreApplicationInternal::~CoreApplicationInternal()
33 {
34     if (_coreCreated) {
35         Core::saveState();
36         Core::destroy();
37     }
38 }
39
40
41 bool CoreApplicationInternal::init()
42 {
43     /* FIXME
44     This is an initial check if logfile is writable since the warning would spam stdout if done
45     in current Logger implementation. Can be dropped whenever the logfile is only opened once.
46     */
47     QFile logFile;
48     if (!Quassel::optionValue("logfile").isEmpty()) {
49         logFile.setFileName(Quassel::optionValue("logfile"));
50         if (!logFile.open(QIODevice::Append | QIODevice::Text))
51             qWarning("Warning: Couldn't open logfile '%s' - will log to stdout instead", qPrintable(logFile.fileName()));
52         else logFile.close();
53     }
54
55     Core::instance(); // create and init the core
56     _coreCreated = true;
57
58     Quassel::registerReloadHandler([]() {
59         // Currently, only reloading SSL certificates and the sysident cache is supported
60         Core::cacheSysIdent();
61         return Core::reloadCerts();
62     });
63
64     if (!Quassel::isOptionSet("norestore"))
65         Core::restoreState();
66
67     return true;
68 }
69
70
71 /*****************************************************************************/
72
73 CoreApplication::CoreApplication(int &argc, char **argv)
74     : QCoreApplication(argc, argv)
75 {
76 #ifdef Q_OS_MAC
77     Quassel::disableCrashHandler();
78 #endif /* Q_OS_MAC */
79
80     Quassel::setRunMode(Quassel::CoreOnly);
81     _internal = new CoreApplicationInternal();
82 }
83
84
85 CoreApplication::~CoreApplication()
86 {
87     delete _internal;
88     Quassel::destroy();
89 }
90
91
92 bool CoreApplication::init()
93 {
94     if (Quassel::init() && _internal->init()) {
95 #if QT_VERSION < 0x050000
96         qInstallMsgHandler(Logger::logMessage);
97 #else
98         qInstallMessageHandler(Logger::logMessage);
99 #endif
100         return true;
101     }
102     return false;
103 }