Merge branch 'seezer'
[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
31 #if defined BUILD_CORE
32 #include <QCoreApplication>
33 #include <QDir>
34 #include "core.h"
35 #include "message.h"
36
37 #elif defined BUILD_QTUI
38 #include <QApplication>
39 #include "client.h"
40 #include "qtui.h"
41
42 #elif defined BUILD_MONO
43 #include <QApplication>
44 #include "client.h"
45 #include "core.h"
46 #include "coresession.h"
47 #include "qtui.h"
48
49 #else
50 #error "Something is wrong - you need to #define a build mode!"
51 #endif
52
53 #include <signal.h>
54
55 //! Signal handler for graceful shutdown.
56 void handle_signal(int sig) {
57   qWarning("%s", qPrintable(QString("Caught signal %1 - exiting.").arg(sig)));
58   QCoreApplication::quit();
59 }
60
61 int main(int argc, char **argv) {
62   // We catch SIGTERM and SIGINT (caused by Ctrl+C) to graceful shutdown Quassel.
63   signal(SIGTERM, handle_signal);
64   signal(SIGINT, handle_signal);
65
66   // Logger logger;
67
68   Global::registerMetaTypes();
69   Global::setupVersion();
70
71 #if defined BUILD_CORE
72   Global::runMode = Global::CoreOnly;
73   QCoreApplication app(argc, argv);
74 #elif defined BUILD_QTUI
75   Global::runMode = Global::ClientOnly;
76   QApplication app(argc, argv);
77 #else
78   Global::runMode = Global::Monolithic;
79   QApplication app(argc, argv);
80 #endif
81
82   qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
83
84   // Set up i18n support
85   QLocale locale = QLocale::system();
86
87   QTranslator qtTranslator;
88   qtTranslator.load(QString(":i18n/qt_%1").arg(locale.name()));
89   app.installTranslator(&qtTranslator);
90
91   QTranslator quasselTranslator;
92   quasselTranslator.load(QString(":i18n/quassel_%1").arg(locale.name()));
93   app.installTranslator(&quasselTranslator);
94
95   Network::setDefaultCodecForServer("ISO-8859-1");
96   Network::setDefaultCodecForEncoding("UTF-8");
97   Network::setDefaultCodecForDecoding("ISO-8859-15");
98
99   QCoreApplication::setOrganizationDomain("quassel-irc.org");
100   QCoreApplication::setApplicationName("Quassel IRC");
101   QCoreApplication::setOrganizationName("Quassel Project");
102
103   // Check if a non-standard core port is requested
104   QStringList args = QCoreApplication::arguments();  // TODO Build a CLI parser
105   Global::DEBUG = args.contains("--debug"); // This enables various debug features.
106
107   Global::defaultPort = 4242;
108   int idx;
109   if((idx = args.indexOf("-p")) > 0 && idx < args.count() - 1) {
110     int port = args[idx+1].toInt();
111     if(port >= 1024 && port < 65536) Global::defaultPort = port;
112   }
113
114 #ifndef BUILD_QTUI
115   Core::instance();  // create and init the core
116 #endif
117
118   //Settings::init();
119
120 #ifndef BUILD_CORE
121   QtUi *gui = new QtUi();
122   Client::init(gui);
123   // init gui only after the event loop has started
124   QTimer::singleShot(0, gui, SLOT(init()));
125   //gui->init();
126 #endif
127
128 #ifndef BUILD_QTUI
129   if(!args.contains("--norestore")) {
130     Core::restoreState();
131   }
132 #endif
133
134   int exitCode = app.exec();
135
136 #ifndef BUILD_QTUI
137   Core::saveState();
138 #endif
139
140 #ifndef BUILD_CORE
141   // the mainWin has to be deleted before the Core
142   // if not Quassel will crash on exit under certain conditions since the gui
143   // still wants to access clientdata
144   delete gui;
145   Client::destroy();
146 #endif
147 #ifndef BUILD_QTUI
148   Core::destroy();
149 #endif
150
151   return exitCode;
152 }