More build system tweaking, plus making icons work
[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 #if defined(HAVE_EXECINFO) and not defined(Q_OS_MAC)
38 #  define BUILD_CRASHHANDLER
39 #  include <execinfo.h>
40 #  include <dlfcn.h>
41 #  include <cxxabi.h>
42 #endif
43
44 Quassel::BuildInfo Quassel::_buildInfo;
45 CliParser *Quassel::_cliParser = 0;
46 Quassel::RunMode Quassel::_runMode;
47 bool Quassel::_initialized = false;
48 bool Quassel::DEBUG = false;
49
50 Quassel::Quassel() {
51   Q_INIT_RESOURCE(i18n);
52
53   // We catch SIGTERM and SIGINT (caused by Ctrl+C) to graceful shutdown Quassel.
54   signal(SIGTERM, handleSignal);
55   signal(SIGINT, handleSignal);
56
57 #ifdef BUILD_CRASHHANDLER
58   signal(SIGABRT, handleSignal);
59   signal(SIGBUS, handleSignal);
60   signal(SIGSEGV, handleSignal);
61 #endif // #if defined(HAVE_EXECINFO) and not defined(Q_OS_MAC)
62
63   _cliParser = new CliParser();
64
65   // put shared client&core arguments here
66   cliParser()->addSwitch("debug",'d', tr("Enable debug output"));
67   cliParser()->addSwitch("help",'h', tr("Display this help and exit"));
68 }
69
70 Quassel::~Quassel() {
71   delete _cliParser;
72 }
73
74 bool Quassel::init() {
75   if(_initialized) return true;  // allow multiple invocations because of MonolithicApplication
76
77   qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
78
79   registerMetaTypes();
80   setupBuildInfo();
81   Global::setupVersion();
82   setupTranslations();
83
84   QCoreApplication::setApplicationName(buildInfo().applicationName);
85   QCoreApplication::setOrganizationName(buildInfo().organizationName);
86   QCoreApplication::setOrganizationDomain(buildInfo().organizationDomain);
87
88   Network::setDefaultCodecForServer("ISO-8859-1");
89   Network::setDefaultCodecForEncoding("UTF-8");
90   Network::setDefaultCodecForDecoding("ISO-8859-15");
91
92   if(!cliParser()->parse(QCoreApplication::arguments()) || isOptionSet("help")) {
93     cliParser()->usage();
94     return false;
95   }
96   DEBUG = isOptionSet("debug");
97   return true;
98 }
99
100 //! Register our custom types with Qt's Meta Object System.
101 /**  This makes them available for QVariant and in signals/slots, among other things.
102 *
103 */
104 void Quassel::registerMetaTypes() {
105   // Complex types
106   qRegisterMetaType<QVariant>("QVariant");
107   qRegisterMetaType<Message>("Message");
108   qRegisterMetaType<BufferInfo>("BufferInfo");
109   qRegisterMetaType<NetworkInfo>("NetworkInfo");
110   qRegisterMetaType<Identity>("Identity");
111   qRegisterMetaType<Network::ConnectionState>("Network::ConnectionState");
112
113   qRegisterMetaTypeStreamOperators<QVariant>("QVariant");
114   qRegisterMetaTypeStreamOperators<Message>("Message");
115   qRegisterMetaTypeStreamOperators<BufferInfo>("BufferInfo");
116   qRegisterMetaTypeStreamOperators<NetworkInfo>("NetworkInfo");
117   qRegisterMetaTypeStreamOperators<Identity>("Identity");
118   qRegisterMetaTypeStreamOperators<qint8>("Network::ConnectionState");
119
120   qRegisterMetaType<IdentityId>("IdentityId");
121   qRegisterMetaType<BufferId>("BufferId");
122   qRegisterMetaType<NetworkId>("NetworkId");
123   qRegisterMetaType<UserId>("UserId");
124   qRegisterMetaType<AccountId>("AccountId");
125   qRegisterMetaType<MsgId>("MsgId");
126
127   qRegisterMetaTypeStreamOperators<IdentityId>("IdentityId");
128   qRegisterMetaTypeStreamOperators<BufferId>("BufferId");
129   qRegisterMetaTypeStreamOperators<NetworkId>("NetworkId");
130   qRegisterMetaTypeStreamOperators<UserId>("UserId");
131   qRegisterMetaTypeStreamOperators<AccountId>("AccountId");
132   qRegisterMetaTypeStreamOperators<MsgId>("MsgId");
133 }
134
135 void Quassel::setupTranslations() {
136   // Set up i18n support
137   QLocale locale = QLocale::system();
138
139   QTranslator *qtTranslator = new QTranslator(qApp);
140   qtTranslator->setObjectName("QtTr");
141   qtTranslator->load(QString(":i18n/qt_%1").arg(locale.name()));
142   qApp->installTranslator(qtTranslator);
143
144   QTranslator *quasselTranslator = new QTranslator(qApp);
145   quasselTranslator->setObjectName("QuasselTr");
146   quasselTranslator->load(QString(":i18n/quassel_%1").arg(locale.name()));
147   qApp->installTranslator(quasselTranslator);
148 }
149
150 void Quassel::setupBuildInfo() {
151   _buildInfo.applicationName = "Quassel IRC";
152   _buildInfo.coreApplicationName = "Quassel Core";
153   _buildInfo.clientApplicationName = "Quassel Client";
154   _buildInfo.organizationName = "Quassel Project";
155   _buildInfo.organizationDomain = "quassel-irc.org";
156 /*
157 #  include "version.inc"
158 #  include "version.gen"
159
160   if(quasselGeneratedVersion.isEmpty()) {
161     if(quasselCommit.isEmpty())
162       quasselVersion = QString("v%1 (unknown rev)").arg(quasselBaseVersion);
163     else
164       quasselVersion = QString("v%1 (dist-%2, %3)").arg(quasselBaseVersion).arg(quasselCommit.left(7))
165       .arg(QDateTime::fromTime_t(quasselArchiveDate).toLocalTime().toString("yyyy-MM-dd"));
166   } else {
167     QStringList parts = quasselGeneratedVersion.split(':');
168     quasselVersion = QString("v%1").arg(parts[0]);
169     if(parts.count() >= 2) quasselVersion.append(QString(" (%1)").arg(parts[1]));
170   }
171   quasselBuildDate = __DATE__;
172   quasselBuildTime = __TIME__;
173   */
174 }
175
176 //! Signal handler for graceful shutdown.
177 void Quassel::handleSignal(int sig) {
178   switch(sig) {
179     case SIGTERM:
180     case SIGINT:
181       qWarning("%s", qPrintable(QString("Caught signal %1 - exiting.").arg(sig)));
182       QCoreApplication::quit();
183       break;
184
185 #ifdef BUILD_CRASHHANDLER
186     case SIGABRT:
187     case SIGBUS:
188     case SIGSEGV:
189       handleCrash();
190 #endif
191       break;
192     default:
193       break;
194   }
195 }
196
197 void Quassel::handleCrash() {
198 #ifdef BUILD_CRASHHANDLER
199   void* callstack[128];
200   int i, frames = backtrace(callstack, 128);
201
202   QFile dumpFile(QString("Quassel-Crash-%1").arg(QDateTime::currentDateTime().toString("yyyyMMdd-hhmm.log")));
203   dumpFile.open(QIODevice::WriteOnly);
204   QTextStream dumpStream(&dumpFile);
205
206   for (i = 0; i < frames; ++i) {
207     Dl_info info;
208     dladdr (callstack[i], &info);
209     // as a reference:
210     //     typedef struct
211     //     {
212       //       __const char *dli_fname;   /* File name of defining object.  */
213     //       void *dli_fbase;           /* Load address of that object.  */
214     //       __const char *dli_sname;   /* Name of nearest symbol.  */
215     //       void *dli_saddr;           /* Exact value of nearest symbol.  */
216     //     } Dl_info;
217
218     #if __LP64__
219     int addrSize = 16;
220     #else
221     int addrSize = 8;
222     #endif
223
224     QString funcName;
225     if(info.dli_sname) {
226       char *func = abi::__cxa_demangle(info.dli_sname, 0, 0, 0);
227       if(func) {
228         funcName = QString(func);
229         free(func);
230       } else {
231         funcName = QString(info.dli_sname);
232       }
233     } else {
234       funcName = QString("0x%1").arg((long)info.dli_saddr, addrSize, QLatin1Char('0'));
235     }
236
237     // prettificating the filename
238     QString fileName("???");
239     if(info.dli_fname) {
240       fileName = QString(info.dli_fname);
241       int slashPos = fileName.lastIndexOf('/');
242       if(slashPos != -1)
243         fileName = fileName.mid(slashPos + 1);
244       if(fileName.count() < 20)
245         fileName += QString(20 - fileName.count(), ' ');
246     }
247
248     QString debugLine = QString("#%1 %2 0x%3 %4").arg(i, 3, 10)
249     .arg(fileName)
250     .arg((long)(callstack[i]), addrSize, 16, QLatin1Char('0'))
251     .arg(funcName);
252
253     dumpStream << debugLine << "\n";
254     qDebug() << qPrintable(debugLine);
255   }
256   dumpFile.close();
257   exit(27);
258 #endif /* BUILD_CRASHHANDLER */
259 }
260
261 // FIXME temporary
262
263 void Global::setupVersion() {
264
265   #  include "version.inc"
266   #  include "version.gen"
267
268   if(quasselGeneratedVersion.isEmpty()) {
269     if(quasselCommit.isEmpty())
270       quasselVersion = QString("v%1 (unknown rev)").arg(quasselBaseVersion);
271     else
272       quasselVersion = QString("v%1 (dist-%2, %3)").arg(quasselBaseVersion).arg(quasselCommit.left(7))
273       .arg(QDateTime::fromTime_t(quasselArchiveDate).toLocalTime().toString("yyyy-MM-dd"));
274   } else {
275     QStringList parts = quasselGeneratedVersion.split(':');
276     quasselVersion = QString("v%1").arg(parts[0]);
277     if(parts.count() >= 2) quasselVersion.append(QString(" (%1)").arg(parts[1]));
278   }
279   quasselBuildDate = __DATE__;
280   quasselBuildTime = __TIME__;
281 }
282
283 QString Global::quasselVersion;
284 QString Global::quasselBaseVersion;
285 QString Global::quasselGeneratedVersion;
286 QString Global::quasselBuildDate;
287 QString Global::quasselBuildTime;
288 QString Global::quasselCommit;
289 uint Global::quasselArchiveDate;
290 uint Global::protocolVersion;
291 uint Global::clientNeedsProtocol;
292 uint Global::coreNeedsProtocol;