From a2b85ba1fd95a17167dbeb7fff352271f10c1752 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Thu, 14 Apr 2022 12:04:58 +0800 Subject: [PATCH] Provide interface for Application class PR #16864. --- src/app/application.cpp | 14 ++- src/app/application.h | 128 +++++++++++++++------------ src/base/CMakeLists.txt | 1 + src/base/base.pri | 1 + src/base/interfaces/iapplication.h | 65 ++++++++++++++ src/gui/CMakeLists.txt | 1 + src/gui/advancedsettings.cpp | 10 +-- src/gui/gui.pri | 1 + src/gui/interfaces/iguiapplication.h | 43 +++++++++ src/gui/optionsdialog.cpp | 6 +- 10 files changed, 195 insertions(+), 75 deletions(-) create mode 100644 src/base/interfaces/iapplication.h create mode 100644 src/gui/interfaces/iguiapplication.h diff --git a/src/app/application.cpp b/src/app/application.cpp index 42732978b..7247ca1f0 100644 --- a/src/app/application.cpp +++ b/src/app/application.cpp @@ -29,8 +29,6 @@ #include "application.h" -#include - #include #ifdef DISABLE_GUI @@ -82,7 +80,6 @@ #include "base/torrentfileswatcher.h" #include "base/utils/compare.h" #include "base/utils/fs.h" -#include "base/path.h" #include "base/utils/misc.h" #include "base/version.h" #include "applicationinstancemanager.h" @@ -90,10 +87,10 @@ #ifndef DISABLE_GUI #include "gui/addnewtorrentdialog.h" -#include "gui/uithememanager.h" -#include "gui/utils.h" #include "gui/mainwindow.h" #include "gui/shutdownconfirmdialog.h" +#include "gui/uithememanager.h" +#include "gui/utils.h" #endif // DISABLE_GUI #ifndef DISABLE_WEBUI @@ -121,12 +118,8 @@ namespace Application::Application(int &argc, char **argv) : BaseApplication(argc, argv) - , m_running(false) , m_shutdownAct(ShutdownDialogAction::Exit) , m_commandLineArgs(parseCommandLine(this->arguments())) -#ifdef Q_OS_WIN - , m_storeMemoryWorkingSetLimit(SETTINGS_KEY(u"MemoryWorkingSetLimit"_qs)) -#endif , m_storeFileLoggerEnabled(FILELOGGER_SETTINGS_KEY(u"Enabled"_qs)) , m_storeFileLoggerBackup(FILELOGGER_SETTINGS_KEY(u"Backup"_qs)) , m_storeFileLoggerDeleteOld(FILELOGGER_SETTINGS_KEY(u"DeleteOld"_qs)) @@ -134,6 +127,9 @@ Application::Application(int &argc, char **argv) , m_storeFileLoggerAge(FILELOGGER_SETTINGS_KEY(u"Age"_qs)) , m_storeFileLoggerAgeType(FILELOGGER_SETTINGS_KEY(u"AgeType"_qs)) , m_storeFileLoggerPath(FILELOGGER_SETTINGS_KEY(u"Path"_qs)) +#ifdef Q_OS_WIN + , m_storeMemoryWorkingSetLimit(SETTINGS_KEY(u"MemoryWorkingSetLimit"_qs)) +#endif { qRegisterMetaType("Log::Msg"); qRegisterMetaType("Log::Peer"); diff --git a/src/app/application.h b/src/app/application.h index 1edf83e3f..d44ed9ab2 100644 --- a/src/app/application.h +++ b/src/app/application.h @@ -1,5 +1,6 @@ /* * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2022 Mike Tzou (Chocobo1) * Copyright (C) 2015, 2019 Vladimir Golovnev * Copyright (C) 2006 Christophe Dumez * @@ -29,31 +30,24 @@ #pragma once +#include +#include #include #include #include #ifndef DISABLE_GUI #include -using BaseApplication = QApplication; -class MainWindow; - -#ifdef Q_OS_WIN -class QSessionManager; -#endif // Q_OS_WIN - -#else -#include -using BaseApplication = QCoreApplication; -#endif // DISABLE_GUI +#endif +#include "base/interfaces/iapplication.h" #include "base/path.h" #include "base/settingvalue.h" #include "base/types.h" #include "cmdoptions.h" -#ifndef DISABLE_WEBUI -class WebUI; +#ifndef DISABLE_GUI +#include "gui/interfaces/iguiapplication.h" #endif class ApplicationInstanceManager; @@ -70,7 +64,25 @@ namespace RSS class AutoDownloader; } -class Application final : public BaseApplication +#ifndef DISABLE_GUI +class MainWindow; + +using BaseApplication = QApplication; +using BaseIApplication = IGUIApplication; + +#ifdef Q_OS_WIN +class QSessionManager; +#endif +#else // DISABLE_GUI +using BaseApplication = QCoreApplication; +using BaseIApplication = IApplication; +#endif // DISABLE_GUI + +#ifndef DISABLE_WEBUI +class WebUI; +#endif + +class Application final : public BaseApplication, public BaseIApplication { Q_OBJECT Q_DISABLE_COPY_MOVE(Application) @@ -79,42 +91,35 @@ public: Application(int &argc, char **argv); ~Application() override; - bool isRunning(); int exec(const QStringList ¶ms); + + bool isRunning(); bool sendParams(const QStringList ¶ms); - -#ifndef DISABLE_GUI - QPointer mainWindow(); -#endif - const QBtCommandLineParameters &commandLineArgs() const; -#ifdef Q_OS_WIN - int memoryWorkingSetLimit() const; - void setMemoryWorkingSetLimit(int size); -#endif - // FileLogger properties - bool isFileLoggerEnabled() const; - void setFileLoggerEnabled(bool value); - Path fileLoggerPath() const; - void setFileLoggerPath(const Path &path); - bool isFileLoggerBackup() const; - void setFileLoggerBackup(bool value); - bool isFileLoggerDeleteOld() const; - void setFileLoggerDeleteOld(bool value); - int fileLoggerMaxSize() const; - void setFileLoggerMaxSize(int bytes); - int fileLoggerAge() const; - void setFileLoggerAge(int value); - int fileLoggerAgeType() const; - void setFileLoggerAgeType(int value); + bool isFileLoggerEnabled() const override; + void setFileLoggerEnabled(bool value) override; + Path fileLoggerPath() const override; + void setFileLoggerPath(const Path &path) override; + bool isFileLoggerBackup() const override; + void setFileLoggerBackup(bool value) override; + bool isFileLoggerDeleteOld() const override; + void setFileLoggerDeleteOld(bool value) override; + int fileLoggerMaxSize() const override; + void setFileLoggerMaxSize(int bytes) override; + int fileLoggerAge() const override; + void setFileLoggerAge(int value) override; + int fileLoggerAgeType() const override; + void setFileLoggerAgeType(int value) override; -protected: -#ifndef DISABLE_GUI -#ifdef Q_OS_MACOS - bool event(QEvent *) override; +#ifdef Q_OS_WIN + int memoryWorkingSetLimit() const override; + void setMemoryWorkingSetLimit(int size) override; #endif + +#ifndef DISABLE_GUI + QPointer mainWindow() override; #endif private slots: @@ -122,31 +127,30 @@ private slots: void torrentFinished(BitTorrent::Torrent *const torrent); void allTorrentsFinished(); void cleanup(); + #if (!defined(DISABLE_GUI) && defined(Q_OS_WIN)) void shutdownCleanup(QSessionManager &manager); #endif private: -#ifdef Q_OS_WIN - void applyMemoryWorkingSetLimit(); -#endif void initializeTranslation(); void processParams(const QStringList ¶ms); void runExternalProgram(const BitTorrent::Torrent *torrent) const; void sendNotificationEmail(const BitTorrent::Torrent *torrent); - - ApplicationInstanceManager *m_instanceManager = nullptr; - bool m_running; - ShutdownDialogAction m_shutdownAct; - QBtCommandLineParameters m_commandLineArgs; +#ifdef Q_OS_WIN + void applyMemoryWorkingSetLimit(); +#endif #ifndef DISABLE_GUI - QPointer m_window; +#ifdef Q_OS_MACOS + bool event(QEvent *) override; +#endif #endif -#ifndef DISABLE_WEBUI - WebUI *m_webui = nullptr; -#endif + ApplicationInstanceManager *m_instanceManager = nullptr; + bool m_running = false; + ShutdownDialogAction m_shutdownAct; + QBtCommandLineParameters m_commandLineArgs; // FileLog QPointer m_fileLogger; @@ -155,9 +159,6 @@ private: QTranslator m_translator; QStringList m_paramsQueue; -#ifdef Q_OS_WIN - SettingValue m_storeMemoryWorkingSetLimit; -#endif SettingValue m_storeFileLoggerEnabled; SettingValue m_storeFileLoggerBackup; SettingValue m_storeFileLoggerDeleteOld; @@ -165,4 +166,15 @@ private: SettingValue m_storeFileLoggerAge; SettingValue m_storeFileLoggerAgeType; SettingValue m_storeFileLoggerPath; +#ifdef Q_OS_WIN + SettingValue m_storeMemoryWorkingSetLimit; +#endif + +#ifndef DISABLE_GUI + QPointer m_window; +#endif + +#ifndef DISABLE_WEBUI + WebUI *m_webui = nullptr; +#endif }; diff --git a/src/base/CMakeLists.txt b/src/base/CMakeLists.txt index 07bb95439..e59b7431c 100644 --- a/src/base/CMakeLists.txt +++ b/src/base/CMakeLists.txt @@ -51,6 +51,7 @@ add_library(qbt_base STATIC http/types.h iconprovider.h indexrange.h + interfaces/iapplication.h interfaces/istringable.h logger.h net/dnsupdater.h diff --git a/src/base/base.pri b/src/base/base.pri index 01c5b9ccc..fff9e3ace 100644 --- a/src/base/base.pri +++ b/src/base/base.pri @@ -50,6 +50,7 @@ HEADERS += \ $$PWD/http/types.h \ $$PWD/iconprovider.h \ $$PWD/indexrange.h \ + $$PWD/interfaces/iapplication.h \ $$PWD/interfaces/istringable.h \ $$PWD/logger.h \ $$PWD/net/dnsupdater.h \ diff --git a/src/base/interfaces/iapplication.h b/src/base/interfaces/iapplication.h new file mode 100644 index 000000000..440290c2c --- /dev/null +++ b/src/base/interfaces/iapplication.h @@ -0,0 +1,65 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2022 Mike Tzou (Chocobo1) + * Copyright (C) 2015, 2019 Vladimir Golovnev + * Copyright (C) 2006 Christophe Dumez + * + * 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; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * In addition, as a special exception, the copyright holders give permission to + * link this program with the OpenSSL project's "OpenSSL" library (or with + * modified versions of it that use the same license as the "OpenSSL" library), + * and distribute the linked executables. You must obey the GNU General Public + * License in all respects for all of the code used other than "OpenSSL". If you + * modify file(s), you may extend this exception to your version of the file(s), + * but you are not obligated to do so. If you do not wish to do so, delete this + * exception statement from your version. + */ + +#pragma once + +#include + +class QString; + +class Path; +struct QBtCommandLineParameters; + +class IApplication +{ +public: + virtual ~IApplication() = default; + + // FileLogger properties + virtual bool isFileLoggerEnabled() const = 0; + virtual void setFileLoggerEnabled(bool value) = 0; + virtual Path fileLoggerPath() const = 0; + virtual void setFileLoggerPath(const Path &path) = 0; + virtual bool isFileLoggerBackup() const = 0; + virtual void setFileLoggerBackup(bool value) = 0; + virtual bool isFileLoggerDeleteOld() const = 0; + virtual void setFileLoggerDeleteOld(bool value) = 0; + virtual int fileLoggerMaxSize() const = 0; + virtual void setFileLoggerMaxSize(int bytes) = 0; + virtual int fileLoggerAge() const = 0; + virtual void setFileLoggerAge(int value) = 0; + virtual int fileLoggerAgeType() const = 0; + virtual void setFileLoggerAgeType(int value) = 0; + +#ifdef Q_OS_WIN + virtual int memoryWorkingSetLimit() const = 0; + virtual void setMemoryWorkingSetLimit(int size) = 0; +#endif +}; diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index af2ab6bc7..7745e66e1 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -50,6 +50,7 @@ add_library(qbt_gui STATIC fspathedit.h fspathedit_p.h hidabletabwidget.h + interfaces/iguiapplication.h ipsubnetwhitelistoptionsdialog.h lineedit.h log/logfiltermodel.h diff --git a/src/gui/advancedsettings.cpp b/src/gui/advancedsettings.cpp index c0c057060..01438c1db 100644 --- a/src/gui/advancedsettings.cpp +++ b/src/gui/advancedsettings.cpp @@ -39,9 +39,9 @@ #include "base/global.h" #include "base/preferences.h" #include "base/unicodestrings.h" -#include "app/application.h" #include "gui/addnewtorrentdialog.h" #include "gui/mainwindow.h" +#include "interfaces/iguiapplication.h" namespace { @@ -200,7 +200,7 @@ void AdvancedSettings::saveAdvancedSettings() } session->setOSMemoryPriority(prio); - static_cast(QCoreApplication::instance())->setMemoryWorkingSetLimit(m_spinBoxMemoryWorkingSetLimit.value()); + dynamic_cast(QCoreApplication::instance())->setMemoryWorkingSetLimit(m_spinBoxMemoryWorkingSetLimit.value()); #endif // Async IO threads session->setAsyncIOThreads(m_spinBoxAsyncIOThreads.value()); @@ -292,7 +292,7 @@ void AdvancedSettings::saveAdvancedSettings() // Stop tracker timeout session->setStopTrackerTimeout(m_spinBoxStopTrackerTimeout.value()); // Program notification - MainWindow *const mainWindow = static_cast(QCoreApplication::instance())->mainWindow(); + MainWindow *mainWindow = dynamic_cast(QCoreApplication::instance())->mainWindow(); mainWindow->setNotificationsEnabled(m_checkBoxProgramNotifications.isChecked()); mainWindow->setTorrentAddedNotificationsEnabled(m_checkBoxTorrentAddedNotifications.isChecked()); #if (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)) && defined(QT_DBUS_LIB) @@ -449,7 +449,7 @@ void AdvancedSettings::loadAdvancedSettings() m_spinBoxMemoryWorkingSetLimit.setMinimum(1); m_spinBoxMemoryWorkingSetLimit.setMaximum(std::numeric_limits::max()); m_spinBoxMemoryWorkingSetLimit.setSuffix(tr(" MiB")); - m_spinBoxMemoryWorkingSetLimit.setValue(static_cast(QCoreApplication::instance())->memoryWorkingSetLimit()); + m_spinBoxMemoryWorkingSetLimit.setValue(dynamic_cast(QCoreApplication::instance())->memoryWorkingSetLimit()); addRow(MEMORY_WORKING_SET_LIMIT, (tr("Physical memory (RAM) usage limit") + u' ' + makeLink(u"https://wikipedia.org/wiki/Working_set", u"(?)")) @@ -694,7 +694,7 @@ void AdvancedSettings::loadAdvancedSettings() , &m_spinBoxStopTrackerTimeout); // Program notifications - const MainWindow *const mainWindow = static_cast(QCoreApplication::instance())->mainWindow(); + const MainWindow *mainWindow = dynamic_cast(QCoreApplication::instance())->mainWindow(); m_checkBoxProgramNotifications.setChecked(mainWindow->isNotificationsEnabled()); addRow(PROGRAM_NOTIFICATIONS, tr("Display notifications"), &m_checkBoxProgramNotifications); // Torrent added notifications diff --git a/src/gui/gui.pri b/src/gui/gui.pri index 6d8a1d84f..6afc436fb 100644 --- a/src/gui/gui.pri +++ b/src/gui/gui.pri @@ -17,6 +17,7 @@ HEADERS += \ $$PWD/fspathedit.h \ $$PWD/fspathedit_p.h \ $$PWD/hidabletabwidget.h \ + $$PWD/interfaces/iguiapplication.h \ $$PWD/ipsubnetwhitelistoptionsdialog.h \ $$PWD/lineedit.h \ $$PWD/log/logfiltermodel.h \ diff --git a/src/gui/interfaces/iguiapplication.h b/src/gui/interfaces/iguiapplication.h new file mode 100644 index 000000000..b88d3bc03 --- /dev/null +++ b/src/gui/interfaces/iguiapplication.h @@ -0,0 +1,43 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2022 Mike Tzou (Chocobo1) + * Copyright (C) 2015, 2019 Vladimir Golovnev + * Copyright (C) 2006 Christophe Dumez + * + * 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; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * In addition, as a special exception, the copyright holders give permission to + * link this program with the OpenSSL project's "OpenSSL" library (or with + * modified versions of it that use the same license as the "OpenSSL" library), + * and distribute the linked executables. You must obey the GNU General Public + * License in all respects for all of the code used other than "OpenSSL". If you + * modify file(s), you may extend this exception to your version of the file(s), + * but you are not obligated to do so. If you do not wish to do so, delete this + * exception statement from your version. + */ + +#pragma once + +#include "base/interfaces/iapplication.h" + +class MainWindow; + +class IGUIApplication : public IApplication +{ +public: + virtual ~IGUIApplication() = default; + + virtual QPointer mainWindow() = 0; +}; diff --git a/src/gui/optionsdialog.cpp b/src/gui/optionsdialog.cpp index 2ae72ddf3..41eaaa2af 100644 --- a/src/gui/optionsdialog.cpp +++ b/src/gui/optionsdialog.cpp @@ -61,8 +61,8 @@ #include "base/utils/random.h" #include "addnewtorrentdialog.h" #include "advancedsettings.h" -#include "app/application.h" #include "banlistoptionsdialog.h" +#include "interfaces/iguiapplication.h" #include "ipsubnetwhitelistoptionsdialog.h" #include "rss/automatedrssdownloader.h" #include "ui_optionsdialog.h" @@ -719,7 +719,7 @@ void OptionsDialog::saveOptions() #endif session->setPerformanceWarningEnabled(m_ui->checkBoxPerformanceWarning->isChecked()); - auto *const app = static_cast(QCoreApplication::instance()); + auto *app = dynamic_cast(QCoreApplication::instance()); app->setFileLoggerPath(m_ui->textFileLogPath->selectedPath()); app->setFileLoggerBackup(m_ui->checkFileLogBackup->isChecked()); app->setFileLoggerMaxSize(m_ui->spinFileLogSize->value() * 1024); @@ -969,7 +969,7 @@ void OptionsDialog::loadOptions() #endif m_ui->checkBoxPerformanceWarning->setChecked(session->isPerformanceWarningEnabled()); - const Application *const app = static_cast(QCoreApplication::instance()); + const auto *app = dynamic_cast(QCoreApplication::instance()); m_ui->checkFileLog->setChecked(app->isFileLoggerEnabled()); m_ui->textFileLogPath->setSelectedPath(app->fileLoggerPath()); const bool fileLogBackup = app->isFileLoggerBackup();