a46f0999a01d9175d7626f15305cb7937117ac43
[quassel.git] / src / common / main.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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 <QDateTime>
22 #include <QString>
23 #include <QTimer>
24 #include <QTranslator>
25 #include <QFile>
26
27 #include "global.h"
28 #include "logger.h"
29 #include "network.h"
30 #include "settings.h"
31 #include "cliparser.h"
32
33 #if defined BUILD_CORE
34 #include <QDir>
35 #include "core.h"
36 #include "message.h"
37
38 #elif defined BUILD_QTUI
39 #include "client.h"
40 #include "qtuiapplication.h"
41 #include "qtui.h"
42
43 #elif defined BUILD_MONO
44 #include "client.h"
45 #include "core.h"
46 #include "coresession.h"
47 #include "qtuiapplication.h"
48 #include "qtui.h"
49
50 #else
51 #error "Something is wrong - you need to #define a build mode!"
52 #endif
53
54
55 #include <signal.h>
56
57 //! Signal handler for graceful shutdown.
58 void handle_signal(int sig) {
59   qWarning("%s", qPrintable(QString("Caught signal %1 - exiting.").arg(sig)));
60   QCoreApplication::quit();
61 }
62
63 int main(int argc, char **argv) {
64   // We catch SIGTERM and SIGINT (caused by Ctrl+C) to graceful shutdown Quassel.
65   signal(SIGTERM, handle_signal);
66   signal(SIGINT, handle_signal);
67
68   Global::registerMetaTypes();
69   Global::setupVersion();
70
71 /*
72 #if defined BUILD_CORE
73   Global::runMode = Global::CoreOnly;
74   QCoreApplication app(argc, argv);
75 #elif defined BUILD_QTUI
76   Global::runMode = Global::ClientOnly;
77   QApplication app(argc, argv);
78 #else
79   Global::runMode = Global::Monolithic;
80   QApplication app(argc, argv);
81 #endif
82 */
83 #if defined BUILD_CORE
84   Global::runMode = Global::CoreOnly;
85   QCoreApplication app(argc, argv);
86 #elif defined BUILD_QTUI
87   Global::runMode = Global::ClientOnly;
88   QtUiApplication app(argc, argv);
89 #else
90   Global::runMode = Global::Monolithic;
91   QtUiApplication app(argc, argv);
92 #endif
93
94
95
96   Global::parser = CliParser(QCoreApplication::arguments());
97
98 #ifndef BUILD_QTUI
99 // put core-only arguments here
100   Global::parser.addOption("port",'p',"The port quasselcore will listen at",QString("4242"));
101   Global::parser.addSwitch("norestore", 'n', "Don't restore last core's state");
102   Global::parser.addOption("logfile",'l',"Path to logfile");
103   Global::parser.addOption("loglevel",'L',"Loglevel Debug|Info|Warning|Error","Info");
104   Global::parser.addOption("datadir", 0, "Specify the directory holding datafiles like the Sqlite DB and the SSL Cert");
105 #endif // BUILD_QTUI
106 #ifndef BUILD_CORE
107 // put client-only arguments here
108   Global::parser.addSwitch("debugbufferswitches",0,"Enables debugging for bufferswitches");
109   Global::parser.addSwitch("debugmodel",0,"Enables debugging for models");
110 #endif // BUILD_QTCORE
111 // put shared client&core arguments here
112   Global::parser.addSwitch("debug",'d',"Enable debug output");
113   Global::parser.addSwitch("help",'h', "Display this help and exit");
114
115   if(!Global::parser.parse() || Global::parser.isSet("help")) {
116     Global::parser.usage();
117     return 1;
118   }
119
120   /*
121    This is an initial check if logfile is writable since the warning would spam stdout if done
122    in current Logger implementation. Can be dropped whenever the logfile is only opened once.
123   */
124   if(Global::runMode != Global::ClientOnly) {
125     QFile logFile;
126     if(!Global::parser.value("logfile").isEmpty()) {
127       logFile.setFileName(Global::parser.value("logfile"));
128       if(!logFile.open(QIODevice::Append | QIODevice::Text))
129         qWarning("Warning: Couldn't open logfile '%s' - will log to stdout instead",qPrintable(logFile.fileName()));
130       else logFile.close();
131     }
132   }
133
134   qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
135
136   // Set up i18n support
137   QLocale locale = QLocale::system();
138
139   QTranslator qtTranslator(&app);
140   qtTranslator.setObjectName("QtTr");
141   qtTranslator.load(QString(":i18n/qt_%1").arg(locale.name()));
142   app.installTranslator(&qtTranslator);
143
144   QTranslator quasselTranslator(&app);
145   quasselTranslator.setObjectName("QuasselTr");
146   quasselTranslator.load(QString(":i18n/quassel_%1").arg(locale.name()));
147   app.installTranslator(&quasselTranslator);
148
149   Network::setDefaultCodecForServer("ISO-8859-1");
150   Network::setDefaultCodecForEncoding("UTF-8");
151   Network::setDefaultCodecForDecoding("ISO-8859-15");
152
153   QCoreApplication::setOrganizationDomain("quassel-irc.org");
154   QCoreApplication::setApplicationName("Quassel IRC");
155   QCoreApplication::setOrganizationName("Quassel Project");
156
157   
158 #ifndef BUILD_QTUI
159   Core::instance();  // create and init the core
160 #endif
161
162   //Settings::init();
163
164 #ifndef BUILD_CORE
165   // session resume
166   QtUi *gui = new QtUi();
167   Client::init(gui);
168   // init gui only after the event loop has started
169   QTimer::singleShot(0, gui, SLOT(init()));
170   //gui->init();
171 #endif
172
173 #ifndef BUILD_QTUI
174   if(!Global::parser.isSet("norestore")) {
175     Core::restoreState();
176   }
177 #endif
178
179 #ifndef BUILD_CORE 
180   app.resumeSessionIfPossible();
181 #endif
182   
183   int exitCode = app.exec();
184
185 #ifndef BUILD_QTUI
186   Core::saveState();
187 #endif
188
189 #ifndef BUILD_CORE
190   // the mainWin has to be deleted before the Core
191   // if not Quassel will crash on exit under certain conditions since the gui
192   // still wants to access clientdata
193   delete gui;
194   Client::destroy();
195 #endif
196 #ifndef BUILD_QTUI
197   Core::destroy();
198 #endif
199
200   return exitCode;
201 }