~ubuntu-branches/ubuntu/saucy/quassel/saucy-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/***************************************************************************
 *   Copyright (C) 2005-2013 by the Quassel Project                        *
 *   devel@quassel-irc.org                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) version 3.                                           *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/

#include "quassel.h"

#include <iostream>
#include <signal.h>
#if !defined Q_OS_WIN32 && !defined Q_OS_MAC
#  include <sys/resource.h>
#endif

#include <QCoreApplication>
#include <QDateTime>
#include <QFileInfo>
#include <QLibraryInfo>
#include <QSettings>
#include <QTranslator>
#include <QHostAddress>

#include "message.h"
#include "identity.h"
#include "network.h"
#include "bufferinfo.h"
#include "types.h"
#include "syncableobject.h"
#include "logger.h"

Quassel::BuildInfo Quassel::_buildInfo;
AbstractCliParser *Quassel::_cliParser = 0;
Quassel::RunMode Quassel::_runMode;
QString Quassel::_configDirPath;
QString Quassel::_translationDirPath;
QStringList Quassel::_dataDirPaths;
bool Quassel::_initialized = false;
bool Quassel::DEBUG = false;
QString Quassel::_coreDumpFileName;
Quassel *Quassel::_instance = 0;
bool Quassel::_handleCrashes = true;
Quassel::LogLevel Quassel::_logLevel = InfoLevel;
QFile *Quassel::_logFile = 0;
bool Quassel::_logToSyslog = false;

Quassel::Quassel()
{
    Q_ASSERT(!_instance);
    _instance = this;

    // We catch SIGTERM and SIGINT (caused by Ctrl+C) to graceful shutdown Quassel.
    signal(SIGTERM, handleSignal);
    signal(SIGINT, handleSignal);
}


Quassel::~Quassel()
{
    if (logFile()) {
        logFile()->close();
        logFile()->deleteLater();
    }
    delete _cliParser;
}


bool Quassel::init()
{
    if (_initialized)
        return true;  // allow multiple invocations because of MonolithicApplication

    if (_handleCrashes) {
        // we have crashhandler for win32 and unix (based on execinfo).
#if defined(Q_OS_WIN32) || defined(HAVE_EXECINFO)
# ifndef Q_OS_WIN32
        // we only handle crashes ourselves if coredumps are disabled
        struct rlimit *limit = (rlimit *)malloc(sizeof(struct rlimit));
        int rc = getrlimit(RLIMIT_CORE, limit);

        if (rc == -1 || !((long)limit->rlim_cur > 0 || limit->rlim_cur == RLIM_INFINITY)) {
# endif /* Q_OS_WIN32 */
        signal(SIGABRT, handleSignal);
        signal(SIGSEGV, handleSignal);
# ifndef Q_OS_WIN32
        signal(SIGBUS, handleSignal);
    }
    free(limit);
# endif /* Q_OS_WIN32 */
#endif /* Q_OS_WIN32 || HAVE_EXECINFO */
    }

    _initialized = true;
    qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));

    registerMetaTypes();

    Network::setDefaultCodecForServer("ISO-8859-1");
    Network::setDefaultCodecForEncoding("UTF-8");
    Network::setDefaultCodecForDecoding("ISO-8859-15");

    if (isOptionSet("help")) {
        cliParser()->usage();
        return false;
    }

    if (isOptionSet("version")) {
        std::cout << qPrintable("Quassel IRC: " + Quassel::buildInfo().plainVersionString) << std::endl;
        return false;
    }

    DEBUG = isOptionSet("debug");

    // set up logging
    if (Quassel::runMode() != Quassel::ClientOnly) {
        if (isOptionSet("loglevel")) {
            QString level = optionValue("loglevel");

            if (level == "Debug") _logLevel = DebugLevel;
            else if (level == "Info") _logLevel = InfoLevel;
            else if (level == "Warning") _logLevel = WarningLevel;
            else if (level == "Error") _logLevel = ErrorLevel;
        }

        QString logfilename = optionValue("logfile");
        if (!logfilename.isEmpty()) {
            _logFile = new QFile(logfilename);
            if (!_logFile->open(QIODevice::Append | QIODevice::Text)) {
                qWarning() << "Could not open log file" << logfilename << ":" << _logFile->errorString();
                _logFile->deleteLater();
                _logFile = 0;
            }
        }
#ifdef HAVE_SYSLOG
        _logToSyslog = isOptionSet("syslog");
#endif
    }

    return true;
}


void Quassel::quit()
{
    QCoreApplication::quit();
}


//! Register our custom types with Qt's Meta Object System.
/**  This makes them available for QVariant and in signals/slots, among other things.
*
*/
void Quassel::registerMetaTypes()
{
    // Complex types
    qRegisterMetaType<Message>("Message");
    qRegisterMetaType<BufferInfo>("BufferInfo");
    qRegisterMetaType<NetworkInfo>("NetworkInfo");
    qRegisterMetaType<Network::Server>("Network::Server");
    qRegisterMetaType<Identity>("Identity");

    qRegisterMetaTypeStreamOperators<Message>("Message");
    qRegisterMetaTypeStreamOperators<BufferInfo>("BufferInfo");
    qRegisterMetaTypeStreamOperators<NetworkInfo>("NetworkInfo");
    qRegisterMetaTypeStreamOperators<Network::Server>("Network::Server");
    qRegisterMetaTypeStreamOperators<Identity>("Identity");

    qRegisterMetaType<IdentityId>("IdentityId");
    qRegisterMetaType<BufferId>("BufferId");
    qRegisterMetaType<NetworkId>("NetworkId");
    qRegisterMetaType<UserId>("UserId");
    qRegisterMetaType<AccountId>("AccountId");
    qRegisterMetaType<MsgId>("MsgId");

    qRegisterMetaType<QHostAddress>("QHostAddress");

    qRegisterMetaTypeStreamOperators<IdentityId>("IdentityId");
    qRegisterMetaTypeStreamOperators<BufferId>("BufferId");
    qRegisterMetaTypeStreamOperators<NetworkId>("NetworkId");
    qRegisterMetaTypeStreamOperators<UserId>("UserId");
    qRegisterMetaTypeStreamOperators<AccountId>("AccountId");
    qRegisterMetaTypeStreamOperators<MsgId>("MsgId");

    // Versions of Qt prior to 4.7 didn't define QVariant as a meta type
    if (!QMetaType::type("QVariant")) {
        qRegisterMetaType<QVariant>("QVariant");
        qRegisterMetaTypeStreamOperators<QVariant>("QVariant");
    }
}


void Quassel::setupBuildInfo(const QString &generated)
{
    _buildInfo.applicationName = "Quassel IRC";
    _buildInfo.coreApplicationName = "quasselcore";
    _buildInfo.clientApplicationName = "quasselclient";
    _buildInfo.organizationName = "Quassel Project";
    _buildInfo.organizationDomain = "quassel-irc.org";

    QStringList gen = generated.split(',');
    Q_ASSERT(gen.count() == 10);
    _buildInfo.baseVersion = gen[0];
    _buildInfo.generatedVersion = gen[1];
    _buildInfo.isSourceDirty = !gen[2].isEmpty();
    _buildInfo.commitHash = gen[3];
    _buildInfo.commitDate = gen[4].toUInt();
    _buildInfo.protocolVersion = gen[5].toUInt();
    _buildInfo.clientNeedsProtocol = gen[6].toUInt();
    _buildInfo.coreNeedsProtocol = gen[7].toUInt();
    _buildInfo.buildDate = QString("%1 %2").arg(gen[8], gen[9]);
    // create a nice version string
    if (_buildInfo.generatedVersion.isEmpty()) {
        if (!_buildInfo.commitHash.isEmpty()) {
            // dist version
            _buildInfo.plainVersionString = QString("v%1 (dist-%2)")
                                            .arg(_buildInfo.baseVersion)
                                            .arg(_buildInfo.commitHash.left(7));
            _buildInfo.fancyVersionString = QString("v%1 (dist-<a href=\"http://git.quassel-irc.org/?p=quassel.git;a=commit;h=%3\">%2</a>)")
                                            .arg(_buildInfo.baseVersion)
                                            .arg(_buildInfo.commitHash.left(7))
                                            .arg(_buildInfo.commitHash);
        }
        else {
            // we only have a base version :(
            _buildInfo.plainVersionString = QString("v%1 (unknown rev)").arg(_buildInfo.baseVersion);
        }
    }
    else {
        // analyze what we got from git-describe
        QRegExp rx("(.*)-(\\d+)-g([0-9a-f]+)$");
        if (rx.exactMatch(_buildInfo.generatedVersion)) {
            QString distance = rx.cap(2) == "0" ? QString() : QString("%1+%2 ").arg(rx.cap(1), rx.cap(2));
            _buildInfo.plainVersionString = QString("v%1 (%2git-%3%4)")
                                            .arg(_buildInfo.baseVersion, distance, rx.cap(3))
                                            .arg(_buildInfo.isSourceDirty ? "*" : "");
            if (!_buildInfo.commitHash.isEmpty()) {
                _buildInfo.fancyVersionString = QString("v%1 (%2git-<a href=\"http://git.quassel-irc.org/?p=quassel.git;a=commit;h=%5\">%3</a>%4)")
                                                .arg(_buildInfo.baseVersion, distance, rx.cap(3))
                                                .arg(_buildInfo.isSourceDirty ? "*" : "")
                                                .arg(_buildInfo.commitHash);
            }
        }
        else {
            _buildInfo.plainVersionString = QString("v%1 (invalid rev)").arg(_buildInfo.baseVersion);
        }
    }
    if (_buildInfo.fancyVersionString.isEmpty())
        _buildInfo.fancyVersionString = _buildInfo.plainVersionString;
}


//! Signal handler for graceful shutdown.
void Quassel::handleSignal(int sig)
{
    switch (sig) {
    case SIGTERM:
    case SIGINT:
        qWarning("%s", qPrintable(QString("Caught signal %1 - exiting.").arg(sig)));
        if (_instance)
            _instance->quit();
        else
            QCoreApplication::quit();
        break;
    case SIGABRT:
    case SIGSEGV:
#ifndef Q_OS_WIN32
    case SIGBUS:
#endif
        logBacktrace(coreDumpFileName());
        exit(EXIT_FAILURE);
        break;
    default:
        break;
    }
}


void Quassel::logFatalMessage(const char *msg)
{
#ifdef Q_OS_MAC
    Q_UNUSED(msg)
#else
    QFile dumpFile(coreDumpFileName());
    dumpFile.open(QIODevice::Append);
    QTextStream dumpStream(&dumpFile);

    dumpStream << "Fatal: " << msg << '\n';
    dumpStream.flush();
    dumpFile.close();
#endif
}


Quassel::Features Quassel::features()
{
    Features feats = 0;
    for (int i = 1; i <= NumFeatures; i <<= 1)
        feats |= (Feature)i;

    return feats;
}


const QString &Quassel::coreDumpFileName()
{
    if (_coreDumpFileName.isEmpty()) {
        QDir configDir(configDirPath());
        _coreDumpFileName = configDir.absoluteFilePath(QString("Quassel-Crash-%1.log").arg(QDateTime::currentDateTime().toString("yyyyMMdd-hhmm")));
        QFile dumpFile(_coreDumpFileName);
        dumpFile.open(QIODevice::Append);
        QTextStream dumpStream(&dumpFile);
        dumpStream << "Quassel IRC: " << _buildInfo.baseVersion << ' ' << _buildInfo.commitHash << '\n';
        qDebug() << "Quassel IRC: " << _buildInfo.baseVersion << ' ' << _buildInfo.commitHash;
        dumpStream.flush();
        dumpFile.close();
    }
    return _coreDumpFileName;
}


QString Quassel::configDirPath()
{
    if (!_configDirPath.isEmpty())
        return _configDirPath;

    if (Quassel::isOptionSet("datadir")) {
        qWarning() << "Obsolete option --datadir used!";
        _configDirPath = Quassel::optionValue("datadir");
    }
    else if (Quassel::isOptionSet("configdir")) {
        _configDirPath = Quassel::optionValue("configdir");
    }
    else {
#ifdef Q_WS_MAC
        // On Mac, the path is always the same
        _configDirPath = QDir::homePath() + "/Library/Application Support/Quassel/";
#else
        // We abuse QSettings to find us a sensible path on the other platforms
#  ifdef Q_WS_WIN
        // don't use the registry
        QSettings::Format format = QSettings::IniFormat;
#  else
        QSettings::Format format = QSettings::NativeFormat;
#  endif
        QSettings s(format, QSettings::UserScope, QCoreApplication::organizationDomain(), buildInfo().applicationName);
        QFileInfo fileInfo(s.fileName());
        _configDirPath = fileInfo.dir().absolutePath();
#endif /* Q_WS_MAC */
    }

    if (!_configDirPath.endsWith(QDir::separator()) && !_configDirPath.endsWith('/'))
        _configDirPath += QDir::separator();

    QDir qDir(_configDirPath);
    if (!qDir.exists(_configDirPath)) {
        if (!qDir.mkpath(_configDirPath)) {
            qCritical() << "Unable to create Quassel config directory:" << qPrintable(qDir.absolutePath());
            return QString();
        }
    }

    return _configDirPath;
}


QStringList Quassel::dataDirPaths()
{
    return _dataDirPaths;
}


QStringList Quassel::findDataDirPaths() const
{
    QStringList dataDirNames = QString(qgetenv("XDG_DATA_DIRS")).split(':', QString::SkipEmptyParts);

    if (!dataDirNames.isEmpty()) {
        for (int i = 0; i < dataDirNames.count(); i++)
            dataDirNames[i].append("/apps/quassel/");
    }
    else {
        // Provide a fallback
#ifdef Q_OS_WIN32
        dataDirNames << qgetenv("APPDATA") + QCoreApplication::organizationDomain() + "/share/apps/quassel/"
                     << qgetenv("APPDATA") + QCoreApplication::organizationDomain()
                     << QCoreApplication::applicationDirPath();
    }
#elif defined Q_WS_MAC
        dataDirNames << QDir::homePath() + "/Library/Application Support/Quassel/"
                     << QCoreApplication::applicationDirPath();
    }
#else
        dataDirNames.append("/usr/share/apps/quassel/");
    }
    // on UNIX, we always check our install prefix
    QString appDir = QCoreApplication::applicationDirPath();
    int binpos = appDir.lastIndexOf("/bin");
    if (binpos >= 0) {
        appDir.replace(binpos, 4, "/share");
        appDir.append("/apps/quassel/");
        if (!dataDirNames.contains(appDir))
            dataDirNames.append(appDir);
    }
#endif

    // add resource path and workdir just in case
    dataDirNames << QCoreApplication::applicationDirPath() + "/data/"
                 << ":/data/";

    // append trailing '/' and check for existence
    QStringList::Iterator iter = dataDirNames.begin();
    while (iter != dataDirNames.end()) {
        if (!iter->endsWith(QDir::separator()) && !iter->endsWith('/'))
            iter->append(QDir::separator());
        if (!QFile::exists(*iter))
            iter = dataDirNames.erase(iter);
        else
            ++iter;
    }

    return dataDirNames;
}


QString Quassel::findDataFilePath(const QString &fileName)
{
    QStringList dataDirs = dataDirPaths();
    foreach(QString dataDir, dataDirs) {
        QString path = dataDir + fileName;
        if (QFile::exists(path))
            return path;
    }
    return QString();
}


QStringList Quassel::scriptDirPaths()
{
    QStringList res(configDirPath() + "scripts/");
    foreach(QString path, dataDirPaths())
    res << path + "scripts/";
    return res;
}


QString Quassel::translationDirPath()
{
    if (_translationDirPath.isEmpty()) {
        // We support only one translation dir; fallback mechanisms wouldn't work else.
        // This means that if we have a $data/translations dir, the internal :/i18n resource won't be considered.
        foreach(const QString &dir, dataDirPaths()) {
            if (QFile::exists(dir + "translations/")) {
                _translationDirPath = dir + "translations/";
                break;
            }
        }
        if (_translationDirPath.isEmpty())
            _translationDirPath = ":/i18n/";
    }
    return _translationDirPath;
}


void Quassel::loadTranslation(const QLocale &locale)
{
    QTranslator *qtTranslator = QCoreApplication::instance()->findChild<QTranslator *>("QtTr");
    QTranslator *quasselTranslator = QCoreApplication::instance()->findChild<QTranslator *>("QuasselTr");

    if (qtTranslator)
        qApp->removeTranslator(qtTranslator);
    if (quasselTranslator)
        qApp->removeTranslator(quasselTranslator);

    // We use QLocale::C to indicate that we don't want a translation
    if (locale.language() == QLocale::C)
        return;

    qtTranslator = new QTranslator(qApp);
    qtTranslator->setObjectName("QtTr");
    qApp->installTranslator(qtTranslator);

    quasselTranslator = new QTranslator(qApp);
    quasselTranslator->setObjectName("QuasselTr");
    qApp->installTranslator(quasselTranslator);

#if QT_VERSION >= 0x040800 && !defined Q_OS_MAC
    bool success = qtTranslator->load(locale, QString("qt_"), translationDirPath());
    if (!success)
        qtTranslator->load(locale, QString("qt_"), QLibraryInfo::location(QLibraryInfo::TranslationsPath));
    quasselTranslator->load(locale, QString(""), translationDirPath());
#else
    bool success = qtTranslator->load(QString("qt_%1").arg(locale.name()), translationDirPath());
    if (!success)
        qtTranslator->load(QString("qt_%1").arg(locale.name()), QLibraryInfo::location(QLibraryInfo::TranslationsPath));
    quasselTranslator->load(QString("%1").arg(locale.name()), translationDirPath());
#endif
}