strawberry-audio-player-win.../src/main.cpp

296 lines
9.4 KiB
C++
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
* Copyright 2018, Jonas Kvinge <jonas@jkvinge.net>
2018-02-27 18:06:05 +01:00
*
* Strawberry 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 3 of the License, or
* (at your option) any later version.
*
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#include "config.h"
2018-04-02 18:35:58 +02:00
#include "version.h"
2018-02-27 18:06:05 +01:00
2018-07-03 17:51:52 +02:00
#include <QtGlobal>
#include <glib.h>
2020-06-14 23:54:18 +02:00
#include <cstdlib>
2018-02-27 18:06:05 +01:00
#include <memory>
2020-06-14 23:54:18 +02:00
#include <ctime>
2018-02-27 18:06:05 +01:00
#ifdef Q_OS_UNIX
# include <unistd.h>
#endif
2018-07-01 22:26:46 +02:00
#ifdef Q_OS_MACOS
# include <sys/resource.h>
# include <sys/sysctl.h>
2018-02-27 18:06:05 +01:00
#endif
#ifdef Q_OS_WIN32
2018-04-02 18:35:58 +02:00
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0600
#endif
2018-02-27 18:06:05 +01:00
#include <windows.h>
#include <iostream>
#endif // Q_OS_WIN32
2018-07-03 17:51:52 +02:00
#include <QObject>
#include <QApplication>
2019-01-11 01:04:13 +01:00
#include <QCoreApplication>
2019-07-26 19:24:16 +02:00
#include <QSysInfo>
2018-07-03 17:51:52 +02:00
#include <QStandardPaths>
#include <QLibraryInfo>
2018-07-03 17:51:52 +02:00
#include <QFileDevice>
#include <QIODevice>
#include <QByteArray>
#include <QNetworkProxy>
#include <QFile>
#include <QDir>
2018-07-03 17:51:52 +02:00
#include <QString>
#include <QImage>
#include <QSettings>
2018-10-20 22:13:51 +02:00
#include <QLoggingCategory>
2018-07-03 17:51:52 +02:00
#include <QtDebug>
#ifdef HAVE_TRANSLATIONS
# include <QTranslator>
#endif
2018-07-03 17:51:52 +02:00
#include "main.h"
#include "core/logging.h"
#include <singleapplication.h>
2019-01-11 01:04:13 +01:00
#include <singlecoreapplication.h>
2018-02-27 18:06:05 +01:00
#ifdef HAVE_QTSPARKLE
# if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
# include <qtsparkle-qt6/Updater>
# else
# include <qtsparkle-qt5/Updater>
# endif
#endif // HAVE_QTSPARKLE
#ifdef HAVE_DBUS
2019-01-01 21:48:50 +01:00
# include "core/mpris.h"
#endif
2019-01-01 21:48:50 +01:00
#include "core/utilities.h"
#include "core/metatypes.h"
#include "core/iconloader.h"
#include "core/mainwindow.h"
#include "core/commandlineoptions.h"
#include "core/systemtrayicon.h"
#include "core/application.h"
#include "core/networkproxyfactory.h"
#ifdef HAVE_TRANSLATIONS
2019-07-22 20:53:05 +02:00
# include "core/translations.h"
#endif
#include "settings/behavioursettingspage.h"
2018-04-02 18:35:58 +02:00
2020-08-09 01:37:00 +02:00
#ifdef HAVE_DBUS
# include "osd/osddbus.h"
#elif defined(Q_OS_MACOS)
# include "osd/osdmac.h"
#else
# include "osd/osdbase.h"
#endif
2018-02-27 18:06:05 +01:00
int main(int argc, char* argv[]) {
2018-07-01 22:26:46 +02:00
#ifdef Q_OS_MACOS
2018-02-27 18:06:05 +01:00
// Do Mac specific startup to get media keys working.
// This must go before QApplication initialisation.
mac::MacMain();
#endif
2018-07-01 22:26:46 +02:00
#if defined(Q_OS_WIN32) || defined(Q_OS_MACOS)
2018-02-27 18:06:05 +01:00
QCoreApplication::setApplicationName("Strawberry");
QCoreApplication::setOrganizationName("Strawberry");
#else
QCoreApplication::setApplicationName("strawberry");
QCoreApplication::setOrganizationName("strawberry");
#endif
QCoreApplication::setApplicationVersion(STRAWBERRY_VERSION_DISPLAY);
2019-09-24 00:06:37 +02:00
QCoreApplication::setOrganizationDomain("strawberrymusicplayer.org");
2018-02-27 18:06:05 +01:00
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
2020-03-08 18:36:48 +01:00
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#endif
2020-03-08 18:36:48 +01:00
2019-11-06 21:53:27 +01:00
// This makes us show up nicely in gnome-volume-control
2018-02-27 18:06:05 +01:00
g_set_application_name(QCoreApplication::applicationName().toLocal8Bit());
RegisterMetaTypes();
// Initialise logging. Log levels are set after the commandline options are parsed below.
2018-02-27 18:06:05 +01:00
logging::Init();
g_log_set_default_handler(reinterpret_cast<GLogFunc>(&logging::GLog), nullptr);
CommandlineOptions options(argc, argv);
{
2019-01-13 14:46:22 +01:00
// Only start a core application now so we can check if there's another instance without requiring an X server.
// This MUST be done before parsing the commandline options so QTextCodec gets the right system locale for filenames.
SingleCoreApplication core_app(argc, argv, true, SingleCoreApplication::Mode::User | SingleCoreApplication::Mode::ExcludeAppVersion | SingleCoreApplication::Mode::ExcludeAppPath);
// Parse commandline options - need to do this before starting the full QApplication so it works without an X server
2018-02-27 18:06:05 +01:00
if (!options.Parse()) return 1;
logging::SetLevels(options.log_levels());
2019-01-13 14:46:22 +01:00
if (core_app.isSecondary()) {
if (options.is_empty()) {
qLog(Info) << "Strawberry is already running - activating existing window (1)";
}
if (!core_app.sendMessage(options.Serialize(), 5000)) {
qLog(Error) << "Could not send message to primary instance.";
}
return 0;
2019-01-13 14:46:22 +01:00
}
2018-02-27 18:06:05 +01:00
}
2018-07-01 22:26:46 +02:00
#ifdef Q_OS_MACOS
2018-02-27 18:06:05 +01:00
// Must happen after QCoreApplication::setOrganizationName().
setenv("XDG_CONFIG_HOME", QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation).toLocal8Bit().constData(), 1);
2018-02-27 18:06:05 +01:00
#endif
// Output the version, so when people attach log output to bug reports they don't have to tell us which version they're using.
2018-02-27 18:06:05 +01:00
qLog(Info) << "Strawberry" << STRAWBERRY_VERSION_DISPLAY;
2019-07-26 19:24:16 +02:00
qLog(Info) << QString("%1 %2 - (%3 %4) [%5]").arg(QSysInfo::prettyProductName()).arg(QSysInfo::productVersion()).arg(QSysInfo::kernelType()).arg(QSysInfo::kernelVersion()).arg(QSysInfo::currentCpuArchitecture());
2018-02-27 18:06:05 +01:00
// Seed the random number generators.
time_t t = time(nullptr);
srand(t);
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
2018-02-27 18:06:05 +01:00
qsrand(t);
#endif
2018-02-27 18:06:05 +01:00
Utilities::IncreaseFDLimit();
2019-01-13 14:46:22 +01:00
// important: Do not remove this.
// This must also be done as a SingleApplication, in case SingleCoreApplication was compiled with a different appdata.
SingleApplication a(argc, argv, true, SingleApplication::Mode::User | SingleApplication::Mode::ExcludeAppVersion | SingleApplication::Mode::ExcludeAppPath);
2019-01-11 20:07:59 +01:00
if (a.isSecondary()) {
if (options.is_empty()) {
2019-01-13 14:46:22 +01:00
qLog(Info) << "Strawberry is already running - activating existing window (2)";
2019-01-11 20:07:59 +01:00
}
if (!a.sendMessage(options.Serialize(), 5000)) {
qLog(Error) << "Could not send message to primary instance.";
}
return 0;
2019-01-11 20:07:59 +01:00
}
2018-02-27 18:06:05 +01:00
#if defined(USE_BUNDLE) && (defined(Q_OS_LINUX) || defined(Q_OS_MACOS))
2019-01-04 00:35:10 +01:00
qLog(Debug) << "Looking for resources in" << QCoreApplication::applicationDirPath() + "/" + USE_BUNDLE_DIR;
2019-01-03 21:19:07 +01:00
QCoreApplication::setLibraryPaths(QStringList() << QCoreApplication::applicationDirPath() + "/" + USE_BUNDLE_DIR);
2018-02-27 18:06:05 +01:00
#endif
// Gnome on Ubuntu has menu icons disabled by default. I think that's a bad idea, and makes some menus in Strawberry look confusing.
2018-02-27 18:06:05 +01:00
QCoreApplication::setAttribute(Qt::AA_DontShowIconsInMenus, false);
// 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.
2018-02-27 18:06:05 +01:00
#ifdef Q_OS_UNIX
{
QSettings s;
// Create the file if it doesn't exist already
if (!QFile::exists(s.fileName())) {
QFile file(s.fileName());
file.open(QIODevice::WriteOnly);
}
// Set -rw-------
QFile::setPermissions(s.fileName(), QFile::ReadOwner | QFile::WriteOwner);
}
#endif
// Resources
Q_INIT_RESOURCE(data);
2018-10-14 00:08:33 +02:00
Q_INIT_RESOURCE(icons);
#if defined(HAVE_TRANSLATIONS) && !defined(INSTALL_TRANSLATIONS)
Q_INIT_RESOURCE(translations);
#endif
2018-02-27 18:06:05 +01:00
#ifdef DEBUG
2018-10-20 22:13:51 +02:00
QLoggingCategory::defaultCategory()->setEnabled(QtDebugMsg, true);
#endif
2018-10-20 22:13:51 +02:00
IconLoader::Init();
#ifdef HAVE_TRANSLATIONS
QString override_language = options.language();
if (override_language.isEmpty()) {
QSettings s;
s.beginGroup(BehaviourSettingsPage::kSettingsGroup);
override_language = s.value("language").toString();
s.endGroup();
}
2019-07-22 20:53:05 +02:00
QString system_language = QLocale::system().uiLanguages().empty() ? QLocale::system().name() : QLocale::system().uiLanguages().first();
// uiLanguages returns strings with "-" as separators for language/region; however QTranslator needs "_" separators
system_language.replace("-", "_");
2019-07-22 20:53:05 +02:00
const QString language = override_language.isEmpty() ? system_language : override_language;
std::unique_ptr<Translations> translations(new Translations);
2020-09-17 17:00:45 +02:00
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
translations->LoadTranslation("qt", QLibraryInfo::path(QLibraryInfo::TranslationsPath), language);
#else
2019-07-22 20:53:05 +02:00
translations->LoadTranslation("qt", QLibraryInfo::location(QLibraryInfo::TranslationsPath), language);
2020-09-17 17:00:45 +02:00
#endif
2019-07-22 20:53:05 +02:00
translations->LoadTranslation("strawberry", ":/translations", language);
translations->LoadTranslation("strawberry", TRANSLATIONS_DIR, language);
2019-07-22 20:53:05 +02:00
translations->LoadTranslation("strawberry", a.applicationDirPath(), language);
translations->LoadTranslation("strawberry", QDir::currentPath(), language);
#ifdef HAVE_QTSPARKLE
qtsparkle::LoadTranslations(language);
#endif
#endif
2018-02-27 18:06:05 +01:00
Application app;
// Network proxy
QNetworkProxyFactory::setApplicationProxyFactory(NetworkProxyFactory::Instance());
// Create the tray icon and OSD
std::unique_ptr<SystemTrayIcon> tray_icon(SystemTrayIcon::CreateSystemTrayIcon());
2020-08-09 01:37:00 +02:00
#ifdef HAVE_DBUS
OSDDBus osd(tray_icon.get(), &app);
#elif defined(Q_OS_MACOS)
OSDMac osd(tray_icon.get(), &app);
#else
OSDBase osd(tray_icon.get(), &app);
#endif
2018-02-27 18:06:05 +01:00
#ifdef HAVE_DBUS
mpris::Mpris mpris(&app);
#endif
// Window
MainWindow w(&app, tray_icon.get(), &osd, options);
2018-07-01 22:26:46 +02:00
#ifdef Q_OS_MACOS
2018-02-27 18:06:05 +01:00
mac::EnableFullScreen(w);
2018-07-01 22:26:46 +02:00
#endif // Q_OS_MACOS
2018-02-27 18:06:05 +01:00
#ifdef HAVE_DBUS
QObject::connect(&mpris, SIGNAL(RaiseMainWindow()), &w, SLOT(Raise()));
#endif
QObject::connect(&a, SIGNAL(receivedMessage(quint32, QByteArray)), &w, SLOT(CommandlineOptionsReceived(quint32, QByteArray)));
2018-02-27 18:06:05 +01:00
int ret = a.exec();
return ret;
}