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