d7cd4e54df163d327e3571e7779fa2eda4830357
[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
26 #include "global.h"
27 #include "logger.h"
28 #include "network.h"
29 #include "settings.h"
30 #include "cliparser.h"
31
32 #if defined BUILD_CORE
33 #include <QCoreApplication>
34 #include <QDir>
35 #include "core.h"
36 #include "message.h"
37
38 #elif defined BUILD_QTUI
39 #include <QApplication>
40 #include "client.h"
41 #include "qtui.h"
42
43 #elif defined BUILD_MONO
44 #include <QApplication>
45 #include "client.h"
46 #include "core.h"
47 #include "coresession.h"
48 #include "qtui.h"
49
50 #else
51 #error "Something is wrong - you need to #define a build mode!"
52 #endif
53
54 #include <signal.h>
55
56 //! Signal handler for graceful shutdown.
57 void handle_signal(int sig) {
58   qWarning("%s", qPrintable(QString("Caught signal %1 - exiting.").arg(sig)));
59   QCoreApplication::quit();
60 }
61
62 int main(int argc, char **argv) {
63   // We catch SIGTERM and SIGINT (caused by Ctrl+C) to graceful shutdown Quassel.
64   signal(SIGTERM, handle_signal);
65   signal(SIGINT, handle_signal);
66
67   // Logger logger;
68
69   Global::registerMetaTypes();
70   Global::setupVersion();
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   Global::parser = CliParser(QCoreApplication::arguments());
84
85 #ifndef BUILD_QTUI
86 // put core-only arguments here
87   Global::parser.addOption("port",'p',"The port quasselcore will listen at",QString("4242"));
88   Global::parser.addSwitch("norestore", 'n', "Don't restore last core's state");
89 #endif // BUILD_QTUI
90 #ifndef BUILD_CORE
91 // put client-only arguments here
92   Global::parser.addSwitch("debugbufferswitches",0,"Enables debugging for bufferswitches");
93   Global::parser.addSwitch("debugmodel",0,"Enables debugging for models");
94 #endif // BUILD_QTCORE
95 // put shared client&core arguments here
96   Global::parser.addSwitch("debug",'d',"Enable debug output");
97   Global::parser.addSwitch("help",'h', "Display this help and exit");
98
99   if(!Global::parser.parse() || Global::parser.isSet("help")) {
100     Global::parser.usage();
101     return 1;
102   }
103
104   qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
105
106   // Set up i18n support
107   QLocale locale = QLocale::system();
108
109   QTranslator qtTranslator(&app);
110   qtTranslator.setObjectName("QtTr");
111   qtTranslator.load(QString(":i18n/qt_%1").arg(locale.name()));
112   app.installTranslator(&qtTranslator);
113
114   QTranslator quasselTranslator(&app);
115   quasselTranslator.setObjectName("QuasselTr");
116   quasselTranslator.load(QString(":i18n/quassel_%1").arg(locale.name()));
117   app.installTranslator(&quasselTranslator);
118
119   Network::setDefaultCodecForServer("ISO-8859-1");
120   Network::setDefaultCodecForEncoding("UTF-8");
121   Network::setDefaultCodecForDecoding("ISO-8859-15");
122
123   QCoreApplication::setOrganizationDomain("quassel-irc.org");
124   QCoreApplication::setApplicationName("Quassel IRC");
125   QCoreApplication::setOrganizationName("Quassel Project");
126
127 #ifndef BUILD_QTUI
128   Core::instance();  // create and init the core
129 #endif
130
131   //Settings::init();
132
133 #ifndef BUILD_CORE
134   QtUi *gui = new QtUi();
135   Client::init(gui);
136   // init gui only after the event loop has started
137   QTimer::singleShot(0, gui, SLOT(init()));
138   //gui->init();
139 #endif
140
141 #ifndef BUILD_QTUI
142   if(!Global::parser.isSet("norestore")) {
143     Core::restoreState();
144   }
145 #endif
146
147   int exitCode = app.exec();
148
149 #ifndef BUILD_QTUI
150   Core::saveState();
151 #endif
152
153 #ifndef BUILD_CORE
154   // the mainWin has to be deleted before the Core
155   // if not Quassel will crash on exit under certain conditions since the gui
156   // still wants to access clientdata
157   delete gui;
158   Client::destroy();
159 #endif
160 #ifndef BUILD_QTUI
161   Core::destroy();
162 #endif
163
164   return exitCode;
165 }