Changing the behavior how Quassel Events are processed.
[quassel.git] / src / core / coreapplication.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 "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   if(_coreCreated) {
34     Core::saveState();
35     Core::destroy();
36   }
37 }
38
39 bool CoreApplicationInternal::init() {
40   /* FIXME
41   This is an initial check if logfile is writable since the warning would spam stdout if done
42   in current Logger implementation. Can be dropped whenever the logfile is only opened once.
43   */
44   QFile logFile;
45   if(!Quassel::optionValue("logfile").isEmpty()) {
46     logFile.setFileName(Quassel::optionValue("logfile"));
47     if(!logFile.open(QIODevice::Append | QIODevice::Text))
48       qWarning("Warning: Couldn't open logfile '%s' - will log to stdout instead",qPrintable(logFile.fileName()));
49     else logFile.close();
50   }
51
52   Core::instance();  // create and init the core
53   _coreCreated = true;
54
55   if(!Quassel::isOptionSet("norestore"))
56     Core::restoreState();
57
58   return true;
59 }
60
61 /*****************************************************************************/
62
63 CoreApplication::CoreApplication(int &argc, char **argv)
64   : QCoreApplication(argc, argv), Quassel()
65 {
66 #ifdef Q_OS_MAC
67   disableCrashhandler();
68 #endif /* Q_OS_MAC */
69
70   setRunMode(Quassel::CoreOnly);
71   _internal = new CoreApplicationInternal();
72 }
73
74 CoreApplication::~CoreApplication() {
75   delete _internal;
76 }
77
78 bool CoreApplication::init() {
79   if(Quassel::init() && _internal->init()) {
80     qInstallMsgHandler(Logger::logMessage);
81     return true;
82   }
83   return false;
84 }