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