86d874b520e80e3809dabaa5ef73cfff517003cb
[quassel.git] / src / common / main.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by The Quassel IRC Development 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) any later version.                                   *
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 "settings.h"
22 #include <QString>
23
24 #if defined BUILD_CORE
25 #include <QCoreApplication>
26 #include <QDir>
27 #include "core.h"
28
29 #elif defined BUILD_QTGUI
30 #include <QApplication>
31 #include "client.h"
32 #include "clientproxy.h"
33 #include "qtgui.h"
34 #include "style.h"
35
36 #elif defined BUILD_MONO
37 #include <QApplication>
38 #include "client.h"
39 #include "clientproxy.h"
40 #include "core.h"
41 #include "coresession.h"
42 #include "qtgui.h"
43 #include "style.h"
44
45 #else
46 #error "Something is wrong - you need to #define a build mode!"
47 #endif
48
49 #include <signal.h>
50
51 //! Signal handler for graceful shutdown.
52 void handle_signal(int sig) {
53   qWarning(QString("Caught signal %1 - exiting.").arg(sig).toAscii());
54   QCoreApplication::quit();
55 }
56
57 int main(int argc, char **argv) {
58   // We catch SIGTERM and SIGINT (caused by Ctrl+C) to graceful shutdown Quassel.
59   signal(SIGTERM, handle_signal);
60   signal(SIGINT, handle_signal);
61
62   qRegisterMetaType<Message>("Message");
63   qRegisterMetaType<BufferId>("BufferId");
64   qRegisterMetaTypeStreamOperators<Message>("Message");
65   qRegisterMetaTypeStreamOperators<BufferId>("BufferId");
66
67 #if defined BUILD_CORE
68   Global::runMode = Global::CoreOnly;
69   QCoreApplication app(argc, argv);
70 #elif defined BUILD_QTGUI
71   Global::runMode = Global::ClientOnly;
72   QApplication app(argc, argv);
73 #else
74   Global::runMode = Global::Monolithic;
75   QApplication app(argc, argv);
76 #endif
77   QCoreApplication::setOrganizationDomain("quassel-irc.org");
78   QCoreApplication::setApplicationName("Quassel IRC");
79   QCoreApplication::setOrganizationName("Quassel IRC Development Team");
80
81   Global::quasselDir = QDir::homePath() + "/.quassel";
82 #ifndef BUILD_QTGUI
83   Core::instance();  // create and init the core
84 #endif
85
86   //Settings::init();
87
88 #ifndef BUILD_CORE
89   Style::init();
90   QtGui *gui = new QtGui();
91   Client::init(gui);
92   gui->init();
93 #endif
94
95   int exitCode = app.exec();
96
97 #ifndef BUILD_CORE
98   // the mainWin has to be deleted before the Core
99   // if not Quassel will crash on exit under certain conditions since the gui
100   // still wants to access clientdata
101   delete gui;
102   Client::destroy();
103 #endif
104 #ifndef BUILD_QTGUI
105   Core::destroy();
106 #endif
107
108   return exitCode;
109 }
110
111 #ifdef BUILD_QTGUI
112 QVariant Client::connectToLocalCore(QString, QString) { return QVariant(); }
113 void Client::disconnectFromLocalCore() {}
114
115 #elif defined BUILD_MONO
116 QVariant Client::connectToLocalCore(QString user, QString passwd) {
117   // TODO catch exceptions
118   QVariant reply = Core::connectLocalClient(user, passwd);
119   QObject::connect(Core::localSession(), SIGNAL(proxySignal(CoreSignal, QVariant, QVariant, QVariant)), ClientProxy::instance(), SLOT(recv(CoreSignal, QVariant, QVariant, QVariant)));
120   QObject::connect(ClientProxy::instance(), SIGNAL(send(ClientSignal, QVariant, QVariant, QVariant)), Core::localSession(), SLOT(processSignal(ClientSignal, QVariant, QVariant, QVariant)));
121   return reply;
122 }
123
124 void Client::disconnectFromLocalCore() {
125   disconnect(Core::localSession(), 0, ClientProxy::instance(), 0);
126   disconnect(ClientProxy::instance(), 0, Core::localSession(), 0);
127   Core::disconnectLocalClient();
128 }
129
130 #endif