From 38e54206d57870096c6348cf55910a7c795c9b77 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Thu, 27 Feb 2020 08:42:47 +0300 Subject: [PATCH] Allow single app instance per configuration --- src/app/application.cpp | 10 +++++++--- src/app/application.h | 4 ++-- src/app/cmdoptions.h | 17 +++++++++++++---- src/app/main.cpp | 7 ++----- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/app/application.cpp b/src/app/application.cpp index d3555e840..0fa8b3475 100644 --- a/src/app/application.cpp +++ b/src/app/application.cpp @@ -126,9 +126,8 @@ namespace #endif } -Application::Application(const QString &id, int &argc, char **argv) +Application::Application(int &argc, char **argv) : BaseApplication(argc, argv) - , m_instanceManager(new ApplicationInstanceManager {id, this}) , m_running(false) , m_shutdownAct(ShutdownDialogAction::Exit) , m_commandLineArgs(parseCommandLine(this->arguments())) @@ -144,11 +143,16 @@ Application::Application(const QString &id, int &argc, char **argv) QPixmapCache::setCacheLimit(PIXMAP_CACHE_SIZE); #endif - const bool portableModeEnabled = m_commandLineArgs.profileDir.isEmpty() && QDir(QCoreApplication::applicationDirPath()).exists(DEFAULT_PORTABLE_MODE_PROFILE_DIR); + const bool portableModeEnabled = m_commandLineArgs.profileDir.isEmpty() + && QDir(QCoreApplication::applicationDirPath()).exists(DEFAULT_PORTABLE_MODE_PROFILE_DIR); const QString profileDir = portableModeEnabled ? QDir(QCoreApplication::applicationDirPath()).absoluteFilePath(DEFAULT_PORTABLE_MODE_PROFILE_DIR) : m_commandLineArgs.profileDir; + const QString appId = QLatin1String("qBittorrent-") + Utils::Misc::getUserIDString() + '@' + profileDir + + (m_commandLineArgs.configurationName.isEmpty() ? QString {} : ('/' + m_commandLineArgs.configurationName)); + m_instanceManager = new ApplicationInstanceManager {appId, this}; + Profile::initInstance(profileDir, m_commandLineArgs.configurationName, (m_commandLineArgs.relativeFastresumePaths || portableModeEnabled)); diff --git a/src/app/application.h b/src/app/application.h index da9dc4168..777a6ec97 100644 --- a/src/app/application.h +++ b/src/app/application.h @@ -74,7 +74,7 @@ class Application : public BaseApplication Q_DISABLE_COPY(Application) public: - Application(const QString &id, int &argc, char **argv); + Application(int &argc, char **argv); ~Application() override; bool isRunning(); @@ -120,7 +120,7 @@ private slots: #endif private: - ApplicationInstanceManager *m_instanceManager; + ApplicationInstanceManager *m_instanceManager = nullptr; bool m_running; ShutdownDialogAction m_shutdownAct; QBtCommandLineParameters m_commandLineArgs; diff --git a/src/app/cmdoptions.h b/src/app/cmdoptions.h index 5ae297f8d..d4168a14d 100644 --- a/src/app/cmdoptions.h +++ b/src/app/cmdoptions.h @@ -42,7 +42,11 @@ class QProcessEnvironment; struct QBtCommandLineParameters { - bool showHelp, relativeFastresumePaths, skipChecking, sequential, firstLastPiecePriority; + bool showHelp; + bool relativeFastresumePaths; + bool skipChecking; + bool sequential; + bool firstLastPiecePriority; #if !defined(Q_OS_WIN) || defined(DISABLE_GUI) bool showVersion; #endif @@ -52,11 +56,16 @@ struct QBtCommandLineParameters bool shouldDaemonize; #endif int webUiPort; - TriStateBool addPaused, skipDialog; + TriStateBool addPaused; + TriStateBool skipDialog; QStringList torrents; - QString profileDir, configurationName, savePath, category, unknownParameter; + QString profileDir; + QString configurationName; + QString savePath; + QString category; + QString unknownParameter; - explicit QBtCommandLineParameters(const QProcessEnvironment&); + explicit QBtCommandLineParameters(const QProcessEnvironment &); QStringList paramList() const; }; diff --git a/src/app/main.cpp b/src/app/main.cpp index 50a7d7604..72011dc4f 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -78,7 +78,6 @@ Q_IMPORT_PLUGIN(QICOPlugin) #include "base/preferences.h" #include "base/profile.h" -#include "base/utils/misc.h" #include "application.h" #include "cmdoptions.h" #include "upgrade.h" @@ -135,11 +134,9 @@ int main(int argc, char *argv[]) try { // Create Application - const QString appId = QLatin1String("qBittorrent-") + Utils::Misc::getUserIDString(); - auto app = std::make_unique(appId, argc, argv); + auto app = std::make_unique(argc, argv); const QBtCommandLineParameters params = app->commandLineArgs(); - if (!params.unknownParameter.isEmpty()) { throw CommandLineParameterError(QObject::tr("%1 is an unknown command line parameter.", "--random-parameter is an unknown command line parameter.") @@ -258,7 +255,7 @@ int main(int argc, char *argv[]) if (params.shouldDaemonize) { app.reset(); // Destroy current application if (daemon(1, 0) == 0) { - app = std::make_unique(appId, argc, argv); + app = std::make_unique(argc, argv); if (app->isRunning()) { // Another instance had time to start. return EXIT_FAILURE;