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