Various smaller stuff, some parts of the new identity settingspage, plus Quassel
[quassel.git] / src / common / main.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 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 "global.h"
22 #include "settings.h"
23 #include <QString>
24 #include <QTranslator>
25
26 #if defined BUILD_CORE
27 #include <QCoreApplication>
28 #include <QDir>
29 #include "core.h"
30 #include "message.h"
31
32 #elif defined BUILD_QTUI
33 #include <QApplication>
34 #include "client.h"
35 #include "qtui.h"
36
37 #elif defined BUILD_MONO
38 #include <QApplication>
39 #include "client.h"
40 #include "core.h"
41 #include "coresession.h"
42 #include "qtui.h"
43
44 #else
45 #error "Something is wrong - you need to #define a build mode!"
46 #endif
47
48 #include <signal.h>
49
50 //! Signal handler for graceful shutdown.
51 void handle_signal(int sig) {
52   qWarning(QString("Caught signal %1 - exiting.").arg(sig).toAscii());
53   QCoreApplication::quit();
54 }
55
56 int main(int argc, char **argv) {
57   // We catch SIGTERM and SIGINT (caused by Ctrl+C) to graceful shutdown Quassel.
58   signal(SIGTERM, handle_signal);
59   signal(SIGINT, handle_signal);
60
61   qRegisterMetaType<QVariant>("QVariant");
62   qRegisterMetaType<Message>("Message");
63   qRegisterMetaType<BufferInfo>("BufferInfo");
64   qRegisterMetaTypeStreamOperators<QVariant>("QVariant");
65   qRegisterMetaTypeStreamOperators<Message>("Message");
66   qRegisterMetaTypeStreamOperators<BufferInfo>("BufferInfo");
67
68
69 #if defined BUILD_CORE
70   Global::runMode = Global::CoreOnly;
71   QCoreApplication app(argc, argv);
72 #elif defined BUILD_QTUI
73   Global::runMode = Global::ClientOnly;
74   QApplication app(argc, argv);
75 #else
76   Global::runMode = Global::Monolithic;
77   QApplication app(argc, argv);
78 #endif
79
80   // Set up i18n support
81   QLocale locale = QLocale::system();
82   QTranslator translator;
83   translator.load(QString(":i18n/quassel_%1").arg(locale.name()));
84   app.installTranslator(&translator);
85
86   QCoreApplication::setOrganizationDomain("quassel-irc.org");
87   QCoreApplication::setApplicationName("Quassel IRC");
88   QCoreApplication::setOrganizationName("Quassel IRC Development Team");  // FIXME
89
90   // Check if a non-standard core port is requested
91   QStringList args = QCoreApplication::arguments();
92
93   Global::defaultPort = 4242;
94   int idx;
95   if((idx = args.indexOf("-p")) > 0 && idx < args.count() - 1) {
96     int port = args[idx+1].toInt();
97     if(port >= 1024 && port < 65536) Global::defaultPort = port;
98   }
99
100 #ifndef BUILD_QTUI
101   Core::instance();  // create and init the core
102 #endif
103
104   //Settings::init();
105
106 #ifndef BUILD_CORE
107   QtUi *gui = new QtUi();
108   Client::init(gui);
109   gui->init();
110 #endif
111
112 #ifndef BUILD_QTUI
113   if(!QCoreApplication::arguments().contains("--norestore")) {
114     Core::restoreState();
115   }
116 #endif
117
118   int exitCode = app.exec();
119
120 #ifndef BUILD_QTUI
121   Core::saveState();
122 #endif
123
124 #ifndef BUILD_CORE
125   // the mainWin has to be deleted before the Core
126   // if not Quassel will crash on exit under certain conditions since the gui
127   // still wants to access clientdata
128   delete gui;
129   Client::destroy();
130 #endif
131 #ifndef BUILD_QTUI
132   Core::destroy();
133 #endif
134
135   return exitCode;
136 }