File: main.cpp

package info (click to toggle)
cantata 3.4.0.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,428 kB
  • sloc: cpp: 109,584; perl: 1,366; xml: 722; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (464 lines) | stat: -rw-r--r-- 15,085 bytes parent folder | download
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
/*
 * Cantata
 *
 * Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
 *
 * ----
 *
 * 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) any later version.
 *
 * 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; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "application.h"
#include "config.h"
#include "db/librarydb.h"
#include "initialsettingswizard.h"
#include "mainwindow.h"
#include "mpd-interface/song.h"
#include "settings.h"
#include "support/thread.h"
#include "support/utils.h"
#include <QDir>
#include <QFile>
#include <QLibraryInfo>
#include <QSettings>
#include <QTranslator>

// To enable debug...
#include "context/lastfmengine.h"
#include "context/metaengine.h"
#include "context/wikipediaengine.h"
#include "covers.h"
#include "mpd-interface/cuefile.h"
#include "mpd-interface/mpdconnection.h"
#include "mpd-interface/mpdparseutils.h"
#include "playlists/dynamicplaylists.h"
#ifdef ENABLE_DEVICES_SUPPORT
#include "models/devicesmodel.h"
#endif
#include "context/contextwidget.h"
#include "context/ultimatelyricsprovider.h"
#include "http/httpserver.h"
#include "network/networkaccessmanager.h"
#include "streams/streamfetcher.h"
#include "tags/taghelperiface.h"
#include "widgets/songdialog.h"
#ifdef ENABLE_SCROBBLING
#include "scrobbling/scrobbler.h"
#endif
#include "gui/mediakeys.h"
#ifdef ENABLE_HTTP_STREAM_PLAYBACK
#include "mpd-interface/httpstream.h"
#endif
#ifdef Avahi_FOUND
#include "avahidiscovery.h"
#endif
#include "customactions.h"

#include <QByteArray>
#include <QCommandLineParser>
#include <QDateTime>
#include <QMutex>
#include <QMutexLocker>
#include <QTextStream>
#include <iostream>

static QMutex msgMutex;
static bool firstMsg = true;
static bool debugToFile = false;
static void cantataQtMsgHandler(QtMsgType, const QMessageLogContext&, const QString& msg)
{
	if (debugToFile) {
		QMutexLocker locker(&msgMutex);
		QFile f(Utils::cacheDir(QString(), true) + "cantata.log");
		if (f.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) {
			QTextStream stream(&f);
			if (firstMsg) {
				stream << "------------START------------" << CANTATA_ENDL;
				firstMsg = false;
			}
			stream << QDateTime::currentDateTime().toString(Qt::ISODate).replace("T", " ") << " - " << msg << CANTATA_ENDL;
		}
	}
	else {
		std::cout << QDateTime::currentDateTime().toString(Qt::ISODate).replace("T", " ").toLatin1().constData()
				  << " - " << msg.toLocal8Bit().constData() << std::endl;
	}
}

static void loadTranslation(const QString& prefix, const QString& path, const QString& overrideLanguage = QString())
{
	QString language = overrideLanguage.isEmpty() ? QLocale::system().name() : overrideLanguage;
	QTranslator* t = new QTranslator;
	if (t->load(prefix + "_" + language, path)) {
		QCoreApplication::installTranslator(t);
	}
	else {
		delete t;
	}
}

static void removeOldFiles(const QString& d, const QStringList& types)
{
	if (!d.isEmpty()) {
		QDir dir(d);
		if (dir.exists()) {
			QFileInfoList files = dir.entryInfoList(types, QDir::Files | QDir::NoDotAndDotDot);
			for (const QFileInfo& file : files) {
				QFile::remove(file.absoluteFilePath());
			}
			QString dirName = dir.dirName();
			if (!dirName.isEmpty()) {
				dir.cdUp();
				dir.rmdir(dirName);
			}
		}
	}
}

static void removeOldFiles()
{
	// Remove Cantata 1.x XML cache files
	removeOldFiles(Utils::cacheDir("library"), QStringList() << "*.xml");
}

static QString debugAreas()
{
	return QObject::tr("mpd - MPD communication") + QLatin1Char('\n')
			+ QObject::tr("mpdparse - Parsing of MPD response") + QLatin1Char('\n')
			+ QObject::tr("cue - Cue file parsing") + QLatin1Char('\n')
			+ QObject::tr("covers - Cover fetching, and loading") + QLatin1Char('\n')
			+ QObject::tr("covers-verbose - Cover fetching, and loading (verbose) ") + QLatin1Char('\n')
			+ QObject::tr("context-wikipedia - Wikipedia info in context view") + QLatin1Char('\n')
			+ QObject::tr("context-lastfm - Last.fm info in context view") + QLatin1Char('\n')
			+ QObject::tr("context-widget - General debug in context view") + QLatin1Char('\n')
			+ QObject::tr("context-lyrics - Lyrics in context view") + QLatin1Char('\n')
			+ QObject::tr("dynamic - Dynamic playlists") + QLatin1Char('\n')
			+ QObject::tr("stream-fetcher - Fetching of stream URLs") + QLatin1Char('\n')
			+ QObject::tr("http-server - Built-in HTTP server") + QLatin1Char('\n')
			+ QObject::tr("song-dialog - Song dialogs (tags, replaygain, organiser)") + QLatin1Char('\n')
			+ QObject::tr("network-access - Network access") + QLatin1Char('\n')
			+ QObject::tr("threads - Threads") + QLatin1Char('\n')
#ifdef ENABLE_SCROBBLING
			+ QObject::tr("scrobbler - Scrobbling") + QLatin1Char('\n')
#endif
			+ QObject::tr("sql - SQL access") + QLatin1Char('\n')
			+ QObject::tr("media-keys - Media-keys") + QLatin1Char('\n')
			+ QObject::tr("custom-actions - Custom actions") + QLatin1Char('\n')
#ifdef TagLib_FOUND
			+ QObject::tr("tags - Tag reading/writing") + QLatin1Char('\n')
#endif
#ifdef ENABLE_DEVICES_SUPPORT
			+ QObject::tr("devices - Device support") + QLatin1Char('\n')
#endif
#ifdef ENABLE_HTTP_STREAM_PLAYBACK
			+ QObject::tr("http-stream - Playback of MPD output streams") + QLatin1Char('\n')
#endif
#ifdef Avahi_FOUND
			+ QObject::tr("avahi - Auto-discovery of MPD servers") + QLatin1Char('\n')
#endif
			+ QObject::tr("all - Enable all debug") + QLatin1Char('\n');
}

static void installDebugMessageHandler(const QString& cmdLine)
{
	QStringList items = cmdLine.split(",", CANTATA_SKIP_EMPTY);

	for (const auto& area : items) {
		bool all = QLatin1String("all") == area;
		if (all || QLatin1String("mpd") == area) {
			MPDConnection::enableDebug();
		}
		if (all || QLatin1String("mpdparse") == area) {
			MPDParseUtils::enableDebug();
		}
		if (all || QLatin1String("cue") == area) {
			CueFile::enableDebug();
		}
		if (all || QLatin1String("covers") == area) {
			Covers::enableDebug(false);
		}
		if (all || QLatin1String("covers-verbose") == area) {
			Covers::enableDebug(true);
		}
		if (all || QLatin1String("context-wikipedia") == area) {
			WikipediaEngine::enableDebug();
		}
		if (all || QLatin1String("context-lastfm") == area) {
			LastFmEngine::enableDebug();
		}
		if (all || QLatin1String("context-info") == area) {
			MetaEngine::enableDebug();
		}
		if (all || QLatin1String("context-widget") == area) {
			ContextWidget::enableDebug();
		}
		if (all || QLatin1String("dynamic") == area) {
			DynamicPlaylists::enableDebug();
		}
		if (all || QLatin1String("stream-fetcher") == area) {
			StreamFetcher::enableDebug();
		}
		if (all || QLatin1String("http-server") == area) {
			HttpServer::enableDebug();
		}
		if (all || QLatin1String("song-dialog") == area) {
			SongDialog::enableDebug();
		}
		if (all || QLatin1String("network-access") == area) {
			NetworkAccessManager::enableDebug();
		}
		if (all || QLatin1String("context-lyrics") == area) {
			UltimateLyricsProvider::enableDebug();
		}
		if (all || QLatin1String("threads") == area) {
			ThreadCleaner::enableDebug();
		}
#ifdef ENABLE_SCROBBLING
		if (all || QLatin1String("scrobbler") == area) {
			Scrobbler::enableDebug();
		}
#endif
		if (all || QLatin1String("sql") == area) {
			LibraryDb::enableDebug();
		}
		if (all || QLatin1String("media-keys") == area) {
			MediaKeys::enableDebug();
		}
		if (all || QLatin1String("custom-actions") == area) {
			CustomActions::enableDebug();
		}
#ifdef TagLib_FOUND
		if (all || QLatin1String("tags") == area) {
			TagHelperIface::enableDebug();
		}
#endif
#ifdef ENABLE_DEVICES_SUPPORT
		if (all || QLatin1String("devices") == area) {
			DevicesModel::enableDebug();
		}
#endif
#ifdef ENABLE_HTTP_STREAM_PLAYBACK
		if (all || QLatin1String("http-stream") == area) {
			HttpStream::enableDebug();
		}
#endif
#ifdef Avahi_FOUND
		if (all || QLatin1String("avahi") == area) {
			AvahiDiscovery::enableDebug();
		}
#endif
	}
	qInstallMessageHandler(cantataQtMsgHandler);
}

#if defined Q_OS_LINUX && defined __GNUC__ && defined __GLIBC__
#include <cxxabi.h>
#include <execinfo.h>
#include <signal.h>
#include <unistd.h>

static void sigHandler(int /*i*/)
{
	// Adapted from: https://panthema.net/2008/0901-stacktrace-demangled/

	// stacktrace.h (c) 2008, Timo Bingmann from http://idlebox.net/
	// published under the WTFPL v2.0
	static const int constMaxFuncNameLen = 256;
	static const int constNumLevels = 15;
	void* addrlist[constNumLevels + 1];

	fprintf(stderr, "Unfortunately Cantata has crashed. Please report a bug at \n"
	                "https://github.com/nullobsi/cantata/issues/ and include the following stack trace:\n\n");
	// retrieve current stack addresses
	int addrlen = backtrace(addrlist, sizeof(addrlist) / sizeof(void*));
	if (!addrlen) {
		fprintf(stderr, "Failed to produce stack trace!\n");
		_exit(0);
	}

	char** symbolList = backtrace_symbols(addrlist, addrlen);
	char funcName[constMaxFuncNameLen];

	// iterate over the returned symbol lines. skip the first, it is the
	// address of this function.
	for (int i = 1; i < addrlen; i++) {
		char* beginName = nullptr;
		char* beginOffset = nullptr;
		char* endOffset = nullptr;

		// find parentheses and +address offset surrounding the mangled name:
		// ./module(function+0x15c) [0x8048a6d]
		for (char* p = symbolList[i]; *p; ++p) {
			if (*p == '(') {
				beginName = p;
			}
			else if (*p == '+') {
				beginOffset = p;
			}
			else if (*p == ')' && beginOffset) {
				endOffset = p;
				break;
			}
		}

		if (beginName && beginOffset && endOffset && beginName < beginOffset) {
			*beginName++ = '\0';
			*beginOffset++ = '\0';
			*endOffset = '\0';

			// mangled name is now in [begin_name, begin_offset) and caller
			// offset in [begin_offset, end_offset). now apply
			// __cxa_demangle():

			int status = 0;
			size_t nameLen = constMaxFuncNameLen;
			char* ret = abi::__cxa_demangle(beginName, funcName, &nameLen, &status);
			if (!status) {
				fprintf(stderr, "  %s : %s+%s\n", symbolList[i], ret, beginOffset);
			}
			else {
				// demangling failed. Output function name as a C function with
				// no arguments.
				fprintf(stderr, "  %s : %s()+%s\n", symbolList[i], beginName, beginOffset);
			}
		}
		else {
			// couldn't parse the line? print the whole line.
			fprintf(stderr, "  %s\n", symbolList[i]);
		}
	}

	free(symbolList);
	_exit(1);
}
#endif

int main(int argc, char* argv[])
{
#if defined Q_OS_LINUX && defined __GNUC__ && defined __GLIBC__
	signal(SIGSEGV, sigHandler);
#endif
	QThread::currentThread()->setObjectName("GUI");
	QCoreApplication::setApplicationName(PACKAGE_NAME);
	QCoreApplication::setOrganizationName(ORGANIZATION_NAME);

	Application app(argc, argv);
	app.setApplicationVersion(PACKAGE_VERSION_STRING);

	QCommandLineParser cmdLineParser;
	cmdLineParser.setApplicationDescription(QObject::tr("MPD Client"));
	cmdLineParser.addHelpOption();
	cmdLineParser.addVersionOption();
	QCommandLineOption debugOption(QStringList() << "d"
	                                             << "debug",
	                               QObject::tr("Comma-separated list of debug areas - possible values:\n") + debugAreas(), "debug", "");
	QCommandLineOption debugToFileOption(QStringList() << "f"
	                                                   << "debug-to-file",
	                                     QObject::tr("Log debug messages to %1").arg(Utils::cacheDir(QString(), true) + "cantata.log"), "", "false");
	QCommandLineOption noNetworkOption(QStringList() << "n"
	                                                 << "no-network",
	                                   QObject::tr("Disable network access"), "", "false");
	QCommandLineOption collectionOption(QStringList() << "c"
	                                                  << "collection",
	                                    QObject::tr("Collection name"), "collection", "");
	QCommandLineOption fullscreenOption(QStringList() << "F"
	                                                  << "fullscreen",
	                                    QObject::tr("Start full screen"), "", "false");
	cmdLineParser.addOption(debugOption);
	cmdLineParser.addOption(debugToFileOption);
	cmdLineParser.addOption(noNetworkOption);
	cmdLineParser.addOption(collectionOption);
	cmdLineParser.addOption(fullscreenOption);
	cmdLineParser.process(app);
	QStringList files = cmdLineParser.positionalArguments();

	if (!app.start(files)) {
		return 0;
	}

	if (cmdLineParser.isSet(noNetworkOption)) {
		NetworkAccessManager::disableNetworkAccess();
	}

// Set the permissions on the config file on Unix - it can contain passwords
// for internet services so it's important that other users can't read it.
// On Windows these are stored in the registry instead.
#ifdef Q_OS_UNIX
	QSettings s;

	// Create the file if it doesn't exist already
	if (!QFile::exists(s.fileName())) {
		QFile file(s.fileName());
		if (!file.open(QIODevice::WriteOnly)) {
			qWarning() << "Failed to open settings file: " << s.fileName();
		}
	}

	// Set -rw-------
	QFile::setPermissions(s.fileName(), QFile::ReadOwner | QFile::WriteOwner);
#endif

	removeOldFiles();
	if (cmdLineParser.isSet(debugOption)) {
		installDebugMessageHandler(cmdLineParser.value(debugOption));
		debugToFile = cmdLineParser.isSet(debugToFileOption);
	}

	// Translations
	QString lang = Settings::self()->lang();
#if defined Q_OS_WIN || defined Q_OS_MAC
	loadTranslation("qt", CANTATA_SYS_TRANS_DIR, lang);
#else
	loadTranslation("qt", QLibraryInfo::path(QLibraryInfo::TranslationsPath), lang);
#endif
	QString local = Utils::fixPath(QCoreApplication::applicationDirPath()) + "translations";
	loadTranslation("cantata", QDir(local).exists() ? local : CANTATA_SYS_TRANS_DIR, lang);

	Application::init();
	// Ensure QColator gets initialised...
	Utils::compare(QString(), QString());

	if (Settings::self()->firstRun()) {
		InitialSettingsWizard wz;
		if (QDialog::Rejected == wz.exec()) {
			return 0;
		}
		Settings::self()->save();
	}
	else if (cmdLineParser.isSet(collectionOption)) {
		QString col = cmdLineParser.value(collectionOption);
		if (!col.isEmpty() && col != Settings::self()->currentConnection()) {
			auto collections = Settings::self()->allConnections();
			for (const auto& c : collections) {
				if (c.name == col) {
					Settings::self()->saveCurrentConnection(col);
					break;
				}
			}
		}
	}
	MainWindow mw;
#if defined Q_OS_WIN || defined Q_OS_MAC
	app.setActivationWindow(&mw);
#endif// !defined Q_OS_MAC
	app.loadFiles(files);

	if (cmdLineParser.isSet(fullscreenOption)) {
		mw.fullScreen();
	}

	return app.exec();
}