Introduce QtUiStyleSettings and make highlight color configurable again
[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 #include <QFile>
26
27 #include "global.h"
28 #include "logger.h"
29 #include "network.h"
30 #include "settings.h"
31 #include "cliparser.h"
32
33 #if defined BUILD_CORE
34 #include <QCoreApplication>
35 #include <QDir>
36 #include "core.h"
37 #include "message.h"
38
39 #elif defined BUILD_QTUI
40 #include <QApplication>
41 #include "client.h"
42 #include "qtui.h"
43
44 #elif defined BUILD_MONO
45 #include <QApplication>
46 #include "client.h"
47 #include "core.h"
48 #include "coresession.h"
49 #include "qtui.h"
50
51 #else
52 #error "Something is wrong - you need to #define a build mode!"
53 #endif
54
55 #include <signal.h>
56
57 //! Signal handler for graceful shutdown.
58 void handle_signal(int sig) {
59   qWarning("%s", qPrintable(QString("Caught signal %1 - exiting.").arg(sig)));
60   QCoreApplication::quit();
61 }
62
63 int main(int argc, char **argv) {
64   // We catch SIGTERM and SIGINT (caused by Ctrl+C) to graceful shutdown Quassel.
65   signal(SIGTERM, handle_signal);
66   signal(SIGINT, handle_signal);
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   Global::parser = CliParser(QCoreApplication::arguments());
83
84 #ifndef BUILD_QTUI
85 // put core-only arguments here
86   Global::parser.addOption("port",'p',"The port quasselcore will listen at",QString("4242"));
87   Global::parser.addSwitch("norestore", 'n', "Don't restore last core's state");
88   Global::parser.addOption("logfile",'l',"Path to logfile");
89   Global::parser.addOption("loglevel",'L',"Loglevel Debug|Info|Warning|Error","Info");
90   Global::parser.addOption("datadir", 0, "Specify the directory holding datafiles like the Sqlite DB and the SSL Cert");
91 #endif // BUILD_QTUI
92 #ifndef BUILD_CORE
93 // put client-only arguments here
94   Global::parser.addSwitch("debugbufferswitches",0,"Enables debugging for bufferswitches");
95   Global::parser.addSwitch("debugmodel",0,"Enables debugging for models");
96 #endif // BUILD_QTCORE
97 // put shared client&core arguments here
98   Global::parser.addSwitch("debug",'d',"Enable debug output");
99   Global::parser.addSwitch("help",'h', "Display this help and exit");
100
101   if(!Global::parser.parse() || Global::parser.isSet("help")) {
102     Global::parser.usage();
103     return 1;
104   }
105
106   /*
107    This is an initial check if logfile is writable since the warning would spam stdout if done
108    in current Logger implementation. Can be dropped whenever the logfile is only opened once.
109   */
110   if(Global::runMode != Global::ClientOnly) {
111     QFile logFile;
112     if(!Global::parser.value("logfile").isEmpty()) {
113       logFile.setFileName(Global::parser.value("logfile"));
114       if(!logFile.open(QIODevice::Append | QIODevice::Text))
115         qWarning("Warning: Couldn't open logfile '%s' - will log to stdout instead",qPrintable(logFile.fileName()));
116       else logFile.close();
117     }
118   }
119
120   qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
121
122   // Set up i18n support
123   QLocale locale = QLocale::system();
124
125   QTranslator qtTranslator(&app);
126   qtTranslator.setObjectName("QtTr");
127   qtTranslator.load(QString(":i18n/qt_%1").arg(locale.name()));
128   app.installTranslator(&qtTranslator);
129
130   QTranslator quasselTranslator(&app);
131   quasselTranslator.setObjectName("QuasselTr");
132   quasselTranslator.load(QString(":i18n/quassel_%1").arg(locale.name()));
133   app.installTranslator(&quasselTranslator);
134
135   Network::setDefaultCodecForServer("ISO-8859-1");
136   Network::setDefaultCodecForEncoding("UTF-8");
137   Network::setDefaultCodecForDecoding("ISO-8859-15");
138
139   QCoreApplication::setOrganizationDomain("quassel-irc.org");
140   QCoreApplication::setApplicationName("Quassel IRC");
141   QCoreApplication::setOrganizationName("Quassel Project");
142
143 #ifndef BUILD_QTUI
144   Core::instance();  // create and init the core
145 #endif
146
147   //Settings::init();
148
149 #ifndef BUILD_CORE
150   QtUi *gui = new QtUi();
151   Client::init(gui);
152   // init gui only after the event loop has started
153   QTimer::singleShot(0, gui, SLOT(init()));
154   //gui->init();
155 #endif
156
157 #ifndef BUILD_QTUI
158   if(!Global::parser.isSet("norestore")) {
159     Core::restoreState();
160   }
161 #endif
162
163   int exitCode = app.exec();
164
165 #ifndef BUILD_QTUI
166   Core::saveState();
167 #endif
168
169 #ifndef BUILD_CORE
170   // the mainWin has to be deleted before the Core
171   // if not Quassel will crash on exit under certain conditions since the gui
172   // still wants to access clientdata
173   delete gui;
174   Client::destroy();
175 #endif
176 #ifndef BUILD_QTUI
177   Core::destroy();
178 #endif
179
180   return exitCode;
181 }