2cdc5a6071643e213744b2b7835cfc637f25065f
[quassel.git] / src / core / coreapplication.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel IRC Team                         *
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   Q_INIT_RESOURCE(sql);
30
31   // put core-only arguments here
32   CliParser *parser = Quassel::cliParser();
33   parser->addOption("port",'p', tr("The port quasselcore will listen at"), QString("4242"));
34   parser->addSwitch("norestore", 'n', tr("Don't restore last core's state"));
35   parser->addOption("logfile", 'l', tr("Path to logfile"));
36   parser->addOption("loglevel", 'L', tr("Loglevel Debug|Info|Warning|Error"), "Info");
37   parser->addOption("datadir", 0, tr("Specify the directory holding datafiles like the Sqlite DB and the SSL Cert"));
38 }
39
40 CoreApplicationInternal::~CoreApplicationInternal() {
41   if(_coreCreated) {
42     Core::saveState();
43     Core::destroy();
44   }
45 }
46
47 bool CoreApplicationInternal::init() {
48   /* FIXME
49   This is an initial check if logfile is writable since the warning would spam stdout if done
50   in current Logger implementation. Can be dropped whenever the logfile is only opened once.
51   */
52   QFile logFile;
53   if(!Quassel::optionValue("logfile").isEmpty()) {
54     logFile.setFileName(Quassel::optionValue("logfile"));
55     if(!logFile.open(QIODevice::Append | QIODevice::Text))
56       qWarning("Warning: Couldn't open logfile '%s' - will log to stdout instead",qPrintable(logFile.fileName()));
57     else logFile.close();
58   }
59
60   Core::instance();  // create and init the core
61   _coreCreated = true;
62
63   if(!Quassel::isOptionSet("norestore")) {
64     Core::restoreState();
65   }
66   return true;
67 }
68
69 /*****************************************************************************/
70
71 CoreApplication::CoreApplication(int &argc, char **argv)
72   : QCoreApplication(argc, argv),
73     Quassel()
74 {
75   setRunMode(Quassel::CoreOnly);
76   _internal = new CoreApplicationInternal();
77
78   qInstallMsgHandler(Logger::logMessage);
79 }
80
81 CoreApplication::~CoreApplication() {
82   delete _internal;
83 }
84
85 bool CoreApplication::init() {
86   if(Quassel::init())
87     return _internal->init();
88   return false;
89 }