Use KCmdLineArgs, KApplication and KMainWindow
[quassel.git] / src / common / quassel.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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 "quassel.h"
22
23 #include <signal.h>
24
25 #include <QCoreApplication>
26 #include <QDateTime>
27 #include <QObject>
28 #include <QMetaType>
29
30 #include "message.h"
31 #include "identity.h"
32 #include "network.h"
33 #include "bufferinfo.h"
34 #include "types.h"
35 #include "syncableobject.h"
36
37 Quassel::BuildInfo Quassel::_buildInfo;
38 CliParser *Quassel::_cliParser = 0;
39 Quassel::RunMode Quassel::_runMode;
40 bool Quassel::_initialized = false;
41 bool Quassel::DEBUG = false;
42 QString Quassel::_coreDumpFileName;
43
44 Quassel::Quassel() {
45   Q_INIT_RESOURCE(i18n);
46
47   // We catch SIGTERM and SIGINT (caused by Ctrl+C) to graceful shutdown Quassel.
48   signal(SIGTERM, handleSignal);
49   signal(SIGINT, handleSignal);
50
51   // we have crashhandler for win32 and unix (based on execinfo).
52   // on mac os we use it's integrated backtrace generator
53 #if defined(Q_OS_WIN32) || (defined(HAVE_EXECINFO) && !defined(Q_OS_MAC))
54   signal(SIGABRT, handleSignal);
55   signal(SIGSEGV, handleSignal);
56 #  ifndef Q_OS_WIN32
57   signal(SIGBUS, handleSignal);
58 #  endif
59 #endif
60
61 }
62
63 Quassel::~Quassel() {
64   delete _cliParser;
65 }
66
67 bool Quassel::init() {
68   if(_initialized)
69     return true;  // allow multiple invocations because of MonolithicApplication
70
71   _initialized = true;
72   qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
73
74   registerMetaTypes();
75
76   QCoreApplication::setApplicationName(buildInfo().applicationName);
77   QCoreApplication::setOrganizationName(buildInfo().organizationName);
78   QCoreApplication::setOrganizationDomain(buildInfo().organizationDomain);
79
80   Network::setDefaultCodecForServer("ISO-8859-1");
81   Network::setDefaultCodecForEncoding("UTF-8");
82   Network::setDefaultCodecForDecoding("ISO-8859-15");
83
84   if(isOptionSet("help")) {
85     cliParser()->usage();
86     return false;
87   }
88
89   DEBUG = isOptionSet("debug");
90   return true;
91 }
92
93 //! Register our custom types with Qt's Meta Object System.
94 /**  This makes them available for QVariant and in signals/slots, among other things.
95 *
96 */
97 void Quassel::registerMetaTypes() {
98   // Complex types
99   qRegisterMetaType<QVariant>("QVariant");
100   qRegisterMetaType<Message>("Message");
101   qRegisterMetaType<BufferInfo>("BufferInfo");
102   qRegisterMetaType<NetworkInfo>("NetworkInfo");
103   qRegisterMetaType<Network::Server>("Network::Server");
104   qRegisterMetaType<Identity>("Identity");
105   qRegisterMetaType<Network::ConnectionState>("Network::ConnectionState");
106
107   qRegisterMetaTypeStreamOperators<QVariant>("QVariant");
108   qRegisterMetaTypeStreamOperators<Message>("Message");
109   qRegisterMetaTypeStreamOperators<BufferInfo>("BufferInfo");
110   qRegisterMetaTypeStreamOperators<NetworkInfo>("NetworkInfo");
111   qRegisterMetaTypeStreamOperators<Network::Server>("Network::Server");
112   qRegisterMetaTypeStreamOperators<Identity>("Identity");
113   qRegisterMetaTypeStreamOperators<qint8>("Network::ConnectionState");
114
115   qRegisterMetaType<IdentityId>("IdentityId");
116   qRegisterMetaType<BufferId>("BufferId");
117   qRegisterMetaType<NetworkId>("NetworkId");
118   qRegisterMetaType<UserId>("UserId");
119   qRegisterMetaType<AccountId>("AccountId");
120   qRegisterMetaType<MsgId>("MsgId");
121
122   qRegisterMetaTypeStreamOperators<IdentityId>("IdentityId");
123   qRegisterMetaTypeStreamOperators<BufferId>("BufferId");
124   qRegisterMetaTypeStreamOperators<NetworkId>("NetworkId");
125   qRegisterMetaTypeStreamOperators<UserId>("UserId");
126   qRegisterMetaTypeStreamOperators<AccountId>("AccountId");
127   qRegisterMetaTypeStreamOperators<MsgId>("MsgId");
128 }
129
130 void Quassel::setupBuildInfo(const QString &generated) {
131   _buildInfo.applicationName = "Quassel IRC";
132   _buildInfo.coreApplicationName = "Quassel Core";
133   _buildInfo.clientApplicationName = "Quassel Client";
134   _buildInfo.organizationName = "Quassel Project";
135   _buildInfo.organizationDomain = "quassel-irc.org";
136
137   QStringList gen = generated.split(',');
138   Q_ASSERT(gen.count() == 10);
139   _buildInfo.baseVersion = gen[0];
140   _buildInfo.generatedVersion = gen[1];
141   _buildInfo.isSourceDirty = !gen[2].isEmpty();
142   _buildInfo.commitHash = gen[3];
143   _buildInfo.commitDate = gen[4].toUInt();
144   _buildInfo.protocolVersion = gen[5].toUInt();
145   _buildInfo.clientNeedsProtocol = gen[6].toUInt();
146   _buildInfo.coreNeedsProtocol = gen[7].toUInt();
147   _buildInfo.buildDate = QString("%1 %2").arg(gen[8], gen[9]);
148   // create a nice version string
149   if(_buildInfo.generatedVersion.isEmpty()) {
150     if(!_buildInfo.commitHash.isEmpty()) {
151       // dist version
152       _buildInfo.plainVersionString = QString("v%1 (dist-%2)")
153                                         .arg(_buildInfo.baseVersion)
154                                         .arg(_buildInfo.commitHash.left(7));
155                                         _buildInfo.fancyVersionString
156                                            = QString("v%1 (dist-<a href=\"http://git.quassel-irc.org/?p=quassel.git;a=commit;h=%3\">%2</a>)")
157                                         .arg(_buildInfo.baseVersion)
158                                         .arg(_buildInfo.commitHash.left(7))
159                                         .arg(_buildInfo.commitHash);
160     } else {
161     // we only have a base version :(
162       _buildInfo.plainVersionString = QString("v%1 (unknown rev)").arg(_buildInfo.baseVersion);
163     }
164   } else {
165     // analyze what we got from git-describe
166     QRegExp rx("(.*)-(\\d+)-g([0-9a-f]+)$");
167     if(rx.exactMatch(_buildInfo.generatedVersion)) {
168       QString distance = rx.cap(2) == "0" ? QString() : QString(" [+%1]").arg(rx.cap(2));
169       _buildInfo.plainVersionString = QString("v%1%2 (git-%3%4)")
170                                         .arg(rx.cap(1), distance, rx.cap(3))
171                                         .arg(_buildInfo.isSourceDirty ? "*" : "");
172       if(!_buildInfo.commitHash.isEmpty()) {
173         _buildInfo.fancyVersionString = QString("v%1%2 (git-<a href=\"http://git.quassel-irc.org/?p=quassel.git;a=commit;h=%5\">%3</a>%4)")
174                                           .arg(rx.cap(1), distance, rx.cap(3))
175                                           .arg(_buildInfo.isSourceDirty ? "*" : "")
176                                           .arg(_buildInfo.commitHash);
177       }
178     } else {
179       _buildInfo.plainVersionString = QString("v%1 (invalid rev)").arg(_buildInfo.baseVersion);
180     }
181   }
182   if(_buildInfo.fancyVersionString.isEmpty())
183     _buildInfo.fancyVersionString = _buildInfo.plainVersionString;
184 }
185
186 //! Signal handler for graceful shutdown.
187 void Quassel::handleSignal(int sig) {
188   switch(sig) {
189   case SIGTERM:
190   case SIGINT:
191     qWarning("%s", qPrintable(QString("Caught signal %1 - exiting.").arg(sig)));
192     QCoreApplication::quit();
193     break;
194   case SIGABRT:
195   case SIGSEGV:
196 #ifndef Q_OS_WIN32
197   case SIGBUS:
198 #endif
199     logBacktrace(coreDumpFileName());
200     exit(EXIT_FAILURE);
201     break;
202   default:
203     break;
204   }
205 }
206
207 void Quassel::logFatalMessage(const char *msg) {
208 #ifdef Q_OS_MAC
209   Q_UNUSED(msg)
210 #else
211   QFile dumpFile(coreDumpFileName());
212   dumpFile.open(QIODevice::Append);
213   QTextStream dumpStream(&dumpFile);
214
215   dumpStream << "Fatal: " << msg << '\n';
216   dumpStream.flush();
217   dumpFile.close();
218 #endif
219 }
220
221 const QString &Quassel::coreDumpFileName() {
222   if(_coreDumpFileName.isEmpty()) {
223     _coreDumpFileName = QString("Quassel-Crash-%1.log").arg(QDateTime::currentDateTime().toString("yyyyMMdd-hhmm"));
224     QFile dumpFile(_coreDumpFileName);
225     dumpFile.open(QIODevice::Append);
226     QTextStream dumpStream(&dumpFile);
227     dumpStream << "Quassel IRC: " << _buildInfo.baseVersion << ' ' << _buildInfo.commitHash << '\n';
228     qDebug() << "Quassel IRC: " << _buildInfo.baseVersion << ' ' << _buildInfo.commitHash;
229     dumpStream.flush();
230     dumpFile.close();
231   }
232   return _coreDumpFileName;
233 }