-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
main.cpp
576 lines (483 loc) · 16 KB
/
main.cpp
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/* Copyright (C) 2005-2011, Thorvald Natvig <thorvald@natvig.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
- Neither the name of the Mumble Developers nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "mumble_pch.hpp"
#include "Overlay.h"
#include "MainWindow.h"
#include "ServerHandler.h"
#include "AudioInput.h"
#include "AudioOutput.h"
#include "AudioWizard.h"
#include "Cert.h"
#include "Database.h"
#include "Log.h"
#include "Plugins.h"
#include "Global.h"
#include "LCD.h"
#ifdef USE_BONJOUR
#include "BonjourClient.h"
#endif
#ifdef USE_DBUS
#include "DBus.h"
#endif
#ifdef USE_VLD
#include "vld.h"
#endif
#include "VersionCheck.h"
#include "NetworkConfig.h"
#include "CrashReporter.h"
#include "SocketRPC.h"
#include "MumbleApplication.h"
#include "ApplicationPalette.h"
#if defined(USE_STATIC_QT_PLUGINS) && QT_VERSION < 0x050000
Q_IMPORT_PLUGIN(qtaccessiblewidgets)
Q_IMPORT_PLUGIN(qico)
Q_IMPORT_PLUGIN(qsvg)
Q_IMPORT_PLUGIN(qsvgicon)
# ifdef Q_OS_MAC
Q_IMPORT_PLUGIN(qicnsicon)
# endif
#endif
#ifdef BOOST_NO_EXCEPTIONS
namespace boost {
void throw_exception(std::exception const &) {
qFatal("Boost exception caught!");
}
}
#endif
extern void os_init();
extern char *os_lang;
#if defined(Q_OS_WIN) && !defined(QT_NO_DEBUG)
extern "C" _declspec(dllexport) int main(int argc, char **argv) {
#else
int main(int argc, char **argv) {
#endif
int res = 0;
QT_REQUIRE_VERSION(argc, argv, "4.4.0");
#if defined(Q_OS_WIN)
SetDllDirectory(L"");
#else
#ifndef Q_OS_MAC
setenv("AVAHI_COMPAT_NOWARN", "1", 1);
#endif
#endif
// Initialize application object.
MumbleApplication a(argc, argv);
a.setApplicationName(QLatin1String("Mumble"));
a.setOrganizationName(QLatin1String("Mumble"));
a.setOrganizationDomain(QLatin1String("mumble.sourceforge.net"));
a.setQuitOnLastWindowClosed(false);
#if QT_VERSION >= 0x050100
a.setAttribute(Qt::AA_UseHighDpiPixmaps);
#endif
#if QT_VERSION >= 0x050000 && defined(Q_OS_WIN)
a.installNativeEventFilter(&a);
#endif
#ifdef USE_SBCELT
{
// For now, force Mumble to use sbcelt-helper from the same directory as the 'mumble' executable.
QDir d(a.applicationDirPath());
QString helper = d.absoluteFilePath(QString::fromLatin1("sbcelt-helper"));
setenv("SBCELT_HELPER_BINARY", helper.toUtf8().constData(), 1);
}
#endif
Global::g_global_struct = new Global();
qsrand(QDateTime::currentDateTime().toTime_t());
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
os_init();
#endif
bool bAllowMultiple = false;
bool bRpcMode = false;
QString rpcCommand;
QUrl url;
if (a.arguments().count() > 1) {
QStringList args = a.arguments();
for (int i = 1; i < args.count(); ++i) {
if (args.at(i) == QLatin1String("-h") || args.at(i) == QLatin1String("--help")
#if defined(Q_OS_WIN)
|| args.at(i) == QLatin1String("/?")
#endif
) {
QString helpMessage = MainWindow::tr(
"Usage: mumble [options] [<url>]\n"
"\n"
"<url> specifies a URL to connect to after startup instead of showing\n"
"the connection window, and has the following form:\n"
"mumble://[<username>[:<password>]@]<host>[:<port>][/<channel>[/<subchannel>...]][?version=<x.y.z>]\n"
"\n"
"The version query parameter has to be set in order to invoke the\n"
"correct client version. It currently defaults to 1.2.0.\n"
"\n"
"Valid options are:\n"
" -h, --help Show this help text and exit.\n"
" -m, --multiple\n"
" Allow multiple instances of the client to be started.\n"
" -n, --noidentity\n"
" Suppress loading of identity files (i.e., certificates.)\n"
"\n"
);
QString rpcHelpBanner = MainWindow::tr(
"Remote controlling Mumble:\n"
"\n"
);
QString rpcHelpMessage = MainWindow::tr(
"Usage: mumble rpc <action> [options]\n"
"\n"
"It is possible to remote control a running instance of Mumble by using\n"
"the 'mumble rpc' command.\n"
"\n"
"Valid actions are:\n"
" mute\n"
" Mute self\n"
" unmute\n"
" Unmute self\n"
" deaf\n"
" Deafen self\n"
" undeaf\n"
" Undeafen self\n"
"\n"
);
QString helpOutput = helpMessage + rpcHelpBanner + rpcHelpMessage;
if (bRpcMode) {
helpOutput = rpcHelpMessage;
}
#if defined(Q_OS_WIN)
QMessageBox::information(NULL, MainWindow::tr("Invocation"), helpOutput);
#else
printf("%s", qPrintable(helpOutput));
#endif
return 1;
} else if (args.at(i) == QLatin1String("-m") || args.at(i) == QLatin1String("--multiple")) {
bAllowMultiple = true;
} else if (args.at(i) == QLatin1String("-n") || args.at(i) == QLatin1String("--noidentity")) {
g.s.bSuppressIdentity = true;
} else if (args.at(i) == QLatin1String("rpc")) {
bRpcMode = true;
if (args.count() - 1 > i) {
rpcCommand = QString(args.at(i + 1));
}
else {
QString rpcError = MainWindow::tr("Error: No RPC command specified");
#if defined(Q_OS_WIN)
QMessageBox::information(NULL, MainWindow::tr("RPC"), rpcError);
#else
printf("%s\n", qPrintable(rpcError));
#endif
return 1;
}
} else {
if (!bRpcMode) {
QUrl u = QUrl::fromEncoded(args.at(i).toUtf8());
if (u.isValid() && (u.scheme() == QLatin1String("mumble"))) {
url = u;
} else {
QFile f(args.at(i));
if (f.exists()) {
url = QUrl::fromLocalFile(f.fileName());
}
}
}
}
}
}
#ifdef USE_DBUS
#ifdef Q_OS_WIN
// By default, windbus expects the path to dbus-daemon to be in PATH, and the path
// should contain bin\\, and the path to the config is hardcoded as ..\etc
{
size_t reqSize;
_wgetenv_s(&reqSize, NULL, 0, L"PATH");
if (reqSize > 0) {
STACKVAR(wchar_t, buff, reqSize+1);
_wgetenv_s(&reqSize, buff, reqSize, L"PATH");
QString path = QString::fromLatin1("%1;%2").arg(QDir::toNativeSeparators(a.applicationDirPath())).arg(QString::fromWCharArray(buff));
STACKVAR(wchar_t, buffout, path.length() + 1);
path.toWCharArray(buffout);
_wputenv_s(L"PATH", buffout);
}
}
#endif
#endif
if (bRpcMode) {
bool sent = false;
QMap<QString, QVariant> param;
param.insert(rpcCommand, rpcCommand);
sent = SocketRPC::send(QLatin1String("Mumble"), QLatin1String("self"), param);
if (sent) {
return 0;
} else {
return 1;
}
}
if (! bAllowMultiple) {
if (url.isValid()) {
#ifndef USE_DBUS
QMap<QString, QVariant> param;
param.insert(QLatin1String("href"), url);
#endif
bool sent = false;
#ifdef USE_DBUS
QDBusInterface qdbi(QLatin1String("net.sourceforge.mumble.mumble"), QLatin1String("/"), QLatin1String("net.sourceforge.mumble.Mumble"));
QDBusMessage reply=qdbi.call(QLatin1String("openUrl"), QLatin1String(url.toEncoded()));
sent = (reply.type() == QDBusMessage::ReplyMessage);
#else
sent = SocketRPC::send(QLatin1String("Mumble"), QLatin1String("url"), param);
#endif
if (sent)
return 0;
} else {
bool sent = false;
#ifdef USE_DBUS
QDBusInterface qdbi(QLatin1String("net.sourceforge.mumble.mumble"), QLatin1String("/"), QLatin1String("net.sourceforge.mumble.Mumble"));
QDBusMessage reply=qdbi.call(QLatin1String("focus"));
sent = (reply.type() == QDBusMessage::ReplyMessage);
#else
sent = SocketRPC::send(QLatin1String("Mumble"), QLatin1String("focus"));
#endif
if (sent)
return 0;
}
}
// Load preferences
g.s.load();
// Check whether we need to enable accessibility features
#ifdef Q_OS_WIN
// Only windows for now. Could not find any information on how to query this for osx or linux
{
HIGHCONTRAST hc;
hc.cbSize = sizeof(HIGHCONTRAST);
SystemParametersInfo(SPI_GETHIGHCONTRAST, sizeof(HIGHCONTRAST), &hc, 0);
if (hc.dwFlags & HCF_HIGHCONTRASTON)
g.s.bHighContrast = true;
}
#endif
DeferInit::run_initializers();
ApplicationPalette applicationPalette;
if (! g.s.qsStyle.isEmpty()) {
a.setStyle(g.s.qsStyle);
g.qsCurrentStyle = g.s.qsStyle;
}
if (! g.s.qsSkin.isEmpty()) {
QFile file(g.s.qsSkin);
file.open(QFile::ReadOnly);
QString styleSheet=QLatin1String(file.readAll());
if (! styleSheet.isEmpty()) {
QFileInfo fi(g.s.qsSkin);
QDir::addSearchPath(QLatin1String("skin"), fi.path());
a.setStyleSheet(styleSheet);
}
} else {
a.setStyleSheet(MainWindow::defaultStyleSheet);
}
QDir::addSearchPath(QLatin1String("skin"),QLatin1String(":/"));
QDir::addSearchPath(QLatin1String("translation"), QLatin1String(":/"));
QString qsSystemLocale = QLocale::system().name();
#ifdef Q_OS_MAC
if (os_lang) {
qWarning("Using Mac OS X system langauge as locale name");
qsSystemLocale = QLatin1String(os_lang);
}
#endif
const QString locale = g.s.qsLanguage.isEmpty() ? qsSystemLocale : g.s.qsLanguage;
qWarning("Locale is \"%s\" (System: \"%s\")", qPrintable(locale), qPrintable(qsSystemLocale));
QTranslator translator;
if (translator.load(QLatin1String("translation:mumble_") + locale))
a.installTranslator(&translator);
QTranslator loctranslator;
if (loctranslator.load(QLatin1String("mumble_") + locale, a.applicationDirPath()))
a.installTranslator(&loctranslator);
QTranslator qttranslator;
if (qttranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + QLatin1String("/qt_") + locale))
a.installTranslator(&qttranslator);
else if (qttranslator.load(QLatin1String("translation:qt_") + locale))
a.installTranslator(&qttranslator);
if (g.s.qsRegionalHost.isEmpty()) {
g.s.qsRegionalHost = qsSystemLocale;
g.s.qsRegionalHost = g.s.qsRegionalHost.remove(QRegExp(QLatin1String("^.+_"))).toLower() + QLatin1String(".mumble.info");
}
// Initialize proxy settings
NetworkConfig::SetupProxy();
g.nam = new QNetworkAccessManager();
#ifndef NO_CRASH_REPORT
CrashReporter *cr = new CrashReporter();
cr->run();
delete cr;
#endif
// Initialize logger
g.l = new Log();
// Initialize database
g.db = new Database();
#ifdef USE_BONJOUR
// Initialize bonjour
g.bc = new BonjourClient();
#endif
//TODO: This already loads up the DLL and does some initial hooking, even
// when the OL is disabled. This should either not be done (object instantiation)
// or the dll loading and preparation be delayed to first use.
g.o = new Overlay();
g.o->setActive(g.s.os.bEnable);
g.lcd = new LCD();
// Process any waiting events before initializing our MainWindow.
// The mumble:// URL support for Mac OS X happens through AppleEvents,
// so we need to loop a little before we begin.
a.processEvents();
// Main Window
g.mw=new MainWindow(NULL);
g.mw->show();
#ifdef USE_DBUS
new MumbleDBus(g.mw);
QDBusConnection::sessionBus().registerObject(QLatin1String("/"), g.mw);
QDBusConnection::sessionBus().registerService(QLatin1String("net.sourceforge.mumble.mumble"));
#endif
SocketRPC *srpc = new SocketRPC(QLatin1String("Mumble"));
g.l->log(Log::Information, MainWindow::tr("Welcome to Mumble."));
// Plugins
g.p = new Plugins(NULL);
g.p->rescanPlugins();
Audio::start();
a.setQuitOnLastWindowClosed(false);
// Configuration updates
bool runaudiowizard = false;
if (g.s.uiUpdateCounter == 0) {
// Previous version was an pre 1.2.3 release or this is the first run
runaudiowizard = true;
} else if (g.s.uiUpdateCounter == 1) {
// Previous versions used old idle action style, convert it
if (g.s.iIdleTime == 5 * 60) { // New default
g.s.iaeIdleAction = Settings::Nothing;
} else {
g.s.iIdleTime = 60 * qRound(g.s.iIdleTime / 60.); // Round to minutes
g.s.iaeIdleAction = Settings::Deafen; // Old behavior
}
}
if (runaudiowizard) {
AudioWizard *aw = new AudioWizard(g.mw);
aw->exec();
delete aw;
}
g.s.uiUpdateCounter = 2;
if (! CertWizard::validateCert(g.s.kpCertificate)) {
#if QT_VERSION >= 0x050000
QDir qd(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation));
#else
QDir qd(QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation));
#endif
QFile qf(qd.absoluteFilePath(QLatin1String("MumbleAutomaticCertificateBackup.p12")));
if (qf.open(QIODevice::ReadOnly | QIODevice::Unbuffered)) {
Settings::KeyPair kp = CertWizard::importCert(qf.readAll());
qf.close();
if (CertWizard::validateCert(kp))
g.s.kpCertificate = kp;
}
if (! CertWizard::validateCert(g.s.kpCertificate)) {
CertWizard *cw = new CertWizard(g.mw);
cw->exec();
delete cw;
if (! CertWizard::validateCert(g.s.kpCertificate)) {
g.s.kpCertificate = CertWizard::generateNewCert();
if (qf.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Unbuffered)) {
qf.write(CertWizard::exportCert(g.s.kpCertificate));
qf.close();
}
}
}
}
if (QDateTime::currentDateTime().daysTo(g.s.kpCertificate.first.first().expiryDate()) < 14)
g.l->log(Log::Warning, CertWizard::tr("<b>Certificate Expiry:</b> Your certificate is about to expire. You need to renew it, or you will no longer be able to connect to servers you are registered on."));
#ifdef QT_NO_DEBUG
#ifndef SNAPSHOT_BUILD
if (g.s.bUpdateCheck)
#endif
new VersionCheck(true, g.mw);
#ifdef SNAPSHOT_BUILD
new VersionCheck(false, g.mw, true);
#endif
#else
g.mw->msgBox(MainWindow::tr("Skipping version check in debug mode."));
#endif
if (g.s.bPluginCheck) {
g.p->checkUpdates();
}
if (url.isValid()) {
OpenURLEvent *oue = new OpenURLEvent(url);
qApp->postEvent(g.mw, oue);
#ifdef Q_OS_MAC
} else if (! a.quLaunchURL.isEmpty()) {
OpenURLEvent *oue = new OpenURLEvent(a.quLaunchURL);
qApp->postEvent(g.mw, oue);
#endif
} else {
g.mw->on_qaServerConnect_triggered(true);
}
if (! g.bQuit)
res=a.exec();
g.s.save();
ServerHandlerPtr sh = g.sh;
if (sh && sh->isRunning())
Database::setShortcuts(g.sh->qbaDigest, g.s.qlShortcuts);
Audio::stop();
if (sh)
sh->disconnect();
delete srpc;
g.sh.reset();
while (sh && ! sh.unique())
QThread::yieldCurrentThread();
sh.reset();
delete g.mw;
delete g.nam;
delete g.lcd;
delete g.db;
delete g.p;
delete g.l;
#ifdef USE_BONJOUR
delete g.bc;
#endif
delete g.o;
DeferInit::run_destroyers();
delete Global::g_global_struct;
Global::g_global_struct = NULL;
#ifndef QT_NO_DEBUG
// Hide Qt memory leak.
QSslSocket::setDefaultCaCertificates(QList<QSslCertificate>());
// Release global protobuf memory allocations.
#if (GOOGLE_PROTOBUF_VERSION >= 2001000)
google::protobuf::ShutdownProtobufLibrary();
#endif
#endif
return res;
}
#if defined(Q_OS_WIN) && defined(QT_NO_DEBUG)
extern void qWinMain(HINSTANCE, HINSTANCE, LPSTR, int, int &, QVector<char *> &);
extern "C" _declspec(dllexport) int MumbleMain(HINSTANCE instance, HINSTANCE prevInstance, LPSTR cmdArg, int cmdShow) {
Q_UNUSED(cmdArg);
QByteArray cmdParam = QString::fromWCharArray(GetCommandLine()).toLocal8Bit();
int argc = 0;
// qWinMain takes argv as a reference.
QVector<char *> argv;
qWinMain(instance, prevInstance, cmdParam.data(), cmdShow, argc, argv);
int result = main(argc, argv.data());
return result;
}
#endif