diff --git a/src/app/application.cpp b/src/app/application.cpp index ef6ef9725..8300ceaa7 100644 --- a/src/app/application.cpp +++ b/src/app/application.cpp @@ -194,7 +194,7 @@ Application::~Application() } #ifndef DISABLE_GUI -QPointer Application::mainWindow() +MainWindow *Application::mainWindow() { return m_window; } @@ -611,7 +611,7 @@ int Application::exec(const QStringList ¶ms) TorrentFilesWatcher::initInstance(); #ifndef DISABLE_WEBUI - m_webui = new WebUI; + m_webui = new WebUI(this); #ifdef DISABLE_GUI if (m_webui->isErrored()) return 1; @@ -658,7 +658,7 @@ int Application::exec(const QStringList ¶ms) #endif // DISABLE_WEBUI #else UIThemeManager::initInstance(); - m_window = new MainWindow; + m_window = new MainWindow(this); #endif // DISABLE_GUI m_running = true; diff --git a/src/app/application.h b/src/app/application.h index 6f70ca1a5..c918145f8 100644 --- a/src/app/application.h +++ b/src/app/application.h @@ -118,7 +118,7 @@ public: void setMemoryWorkingSetLimit(int size) override; #ifndef DISABLE_GUI - QPointer mainWindow() override; + MainWindow *mainWindow() override; #endif private slots: @@ -167,7 +167,7 @@ private: SettingValue m_storeMemoryWorkingSetLimit; #ifndef DISABLE_GUI - QPointer m_window; + MainWindow *m_window = nullptr; #endif #ifndef DISABLE_WEBUI diff --git a/src/base/CMakeLists.txt b/src/base/CMakeLists.txt index 44c7c3210..3e8cd98e2 100644 --- a/src/base/CMakeLists.txt +++ b/src/base/CMakeLists.txt @@ -2,6 +2,7 @@ add_library(qbt_base STATIC # headers 3rdparty/expected.hpp algorithm.h + applicationcomponent.h asyncfilestorage.h bittorrent/abstractfilestorage.h bittorrent/addtorrentparams.h @@ -104,6 +105,7 @@ add_library(qbt_base STATIC version.h # sources + applicationcomponent.cpp asyncfilestorage.cpp bittorrent/abstractfilestorage.cpp bittorrent/bandwidthscheduler.cpp diff --git a/src/base/applicationcomponent.cpp b/src/base/applicationcomponent.cpp new file mode 100644 index 000000000..6495cb9dc --- /dev/null +++ b/src/base/applicationcomponent.cpp @@ -0,0 +1,39 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2022 Vladimir Golovnev + * + * 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. + */ + +#include "applicationcomponent.h" + +ApplicationComponent::ApplicationComponent(IApplication *app) + : m_app {app} +{ +} + +IApplication *ApplicationComponent::app() const +{ + return m_app; +} diff --git a/src/base/applicationcomponent.h b/src/base/applicationcomponent.h new file mode 100644 index 000000000..bbf530b46 --- /dev/null +++ b/src/base/applicationcomponent.h @@ -0,0 +1,47 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2022 Vladimir Golovnev + * + * 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 + +#include "interfaces/iapplication.h" + +class ApplicationComponent +{ + Q_DISABLE_COPY_MOVE(ApplicationComponent) + +public: + explicit ApplicationComponent(IApplication *app); + virtual ~ApplicationComponent() = default; + + virtual IApplication *app() const; + +private: + IApplication *m_app = nullptr; +}; diff --git a/src/base/base.pri b/src/base/base.pri index 4e1f3b77c..1f74e8288 100644 --- a/src/base/base.pri +++ b/src/base/base.pri @@ -1,6 +1,7 @@ HEADERS += \ $$PWD/3rdparty/expected.hpp \ $$PWD/algorithm.h \ + $$PWD/applicationcomponent.h \ $$PWD/asyncfilestorage.h \ $$PWD/bittorrent/abstractfilestorage.h \ $$PWD/bittorrent/addtorrentparams.h \ @@ -104,6 +105,7 @@ HEADERS += \ $$PWD/version.h SOURCES += \ + $$PWD/applicationcomponent.cpp \ $$PWD/asyncfilestorage.cpp \ $$PWD/bittorrent/abstractfilestorage.cpp \ $$PWD/bittorrent/bandwidthscheduler.cpp \ diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index eacdf58de..41194f61c 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -49,6 +49,7 @@ add_library(qbt_gui STATIC executionlogwidget.h fspathedit.h fspathedit_p.h + guiapplicationcomponent.h hidabletabwidget.h interfaces/iguiapplication.h ipsubnetwhitelistoptionsdialog.h @@ -131,6 +132,7 @@ add_library(qbt_gui STATIC executionlogwidget.cpp fspathedit.cpp fspathedit_p.cpp + guiapplicationcomponent.cpp hidabletabwidget.cpp ipsubnetwhitelistoptionsdialog.cpp lineedit.cpp diff --git a/src/gui/advancedsettings.cpp b/src/gui/advancedsettings.cpp index cc8c3665d..3d4b8d40d 100644 --- a/src/gui/advancedsettings.cpp +++ b/src/gui/advancedsettings.cpp @@ -151,8 +151,9 @@ namespace }; } -AdvancedSettings::AdvancedSettings(QWidget *parent) +AdvancedSettings::AdvancedSettings(IGUIApplication *app, QWidget *parent) : QTableWidget(parent) + , GUIApplicationComponent(app) { // column setColumnCount(COL_COUNT); @@ -178,7 +179,7 @@ void AdvancedSettings::saveAdvancedSettings() const session->setResumeDataStorageType(m_comboBoxResumeDataStorage.currentData().value()); // Physical memory (RAM) usage limit - dynamic_cast(QCoreApplication::instance())->setMemoryWorkingSetLimit(m_spinBoxMemoryWorkingSetLimit.value()); + app()->setMemoryWorkingSetLimit(m_spinBoxMemoryWorkingSetLimit.value()); #if defined(Q_OS_WIN) session->setOSMemoryPriority(m_comboBoxOSMemoryPriority.currentData().value()); #endif @@ -266,7 +267,7 @@ void AdvancedSettings::saveAdvancedSettings() const // Stop tracker timeout session->setStopTrackerTimeout(m_spinBoxStopTrackerTimeout.value()); // Program notification - MainWindow *mainWindow = dynamic_cast(QCoreApplication::instance())->mainWindow(); + MainWindow *mainWindow = app()->mainWindow(); mainWindow->setNotificationsEnabled(m_checkBoxProgramNotifications.isChecked()); mainWindow->setTorrentAddedNotificationsEnabled(m_checkBoxTorrentAddedNotifications.isChecked()); #if (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)) && defined(QT_DBUS_LIB) @@ -411,7 +412,7 @@ void AdvancedSettings::loadAdvancedSettings() m_spinBoxMemoryWorkingSetLimit.setMaximum(std::numeric_limits::max()); m_spinBoxMemoryWorkingSetLimit.setSuffix(tr(" MiB")); m_spinBoxMemoryWorkingSetLimit.setToolTip(tr("This option is less effective on Linux")); - m_spinBoxMemoryWorkingSetLimit.setValue(dynamic_cast(QCoreApplication::instance())->memoryWorkingSetLimit()); + m_spinBoxMemoryWorkingSetLimit.setValue(app()->memoryWorkingSetLimit()); addRow(MEMORY_WORKING_SET_LIMIT, (tr("Physical memory (RAM) usage limit") + u' ' + makeLink(u"https://wikipedia.org/wiki/Working_set", u"(?)")) , &m_spinBoxMemoryWorkingSetLimit); #if defined(Q_OS_WIN) @@ -670,7 +671,7 @@ void AdvancedSettings::loadAdvancedSettings() , &m_spinBoxStopTrackerTimeout); // Program notifications - const MainWindow *mainWindow = dynamic_cast(QCoreApplication::instance())->mainWindow(); + const MainWindow *mainWindow = app()->mainWindow(); m_checkBoxProgramNotifications.setChecked(mainWindow->isNotificationsEnabled()); addRow(PROGRAM_NOTIFICATIONS, tr("Display notifications"), &m_checkBoxProgramNotifications); // Torrent added notifications diff --git a/src/gui/advancedsettings.h b/src/gui/advancedsettings.h index 8d42c741c..44c7a789d 100644 --- a/src/gui/advancedsettings.h +++ b/src/gui/advancedsettings.h @@ -34,13 +34,15 @@ #include #include -class AdvancedSettings final : public QTableWidget +#include "guiapplicationcomponent.h" + +class AdvancedSettings final : public QTableWidget, public GUIApplicationComponent { Q_OBJECT Q_DISABLE_COPY_MOVE(AdvancedSettings) public: - AdvancedSettings(QWidget *parent); + explicit AdvancedSettings(IGUIApplication *app, QWidget *parent = nullptr); public slots: void saveAdvancedSettings() const; diff --git a/src/gui/gui.pri b/src/gui/gui.pri index 88c90098d..d5ebadae7 100644 --- a/src/gui/gui.pri +++ b/src/gui/gui.pri @@ -16,6 +16,7 @@ HEADERS += \ $$PWD/executionlogwidget.h \ $$PWD/fspathedit.h \ $$PWD/fspathedit_p.h \ + $$PWD/guiapplicationcomponent.h \ $$PWD/hidabletabwidget.h \ $$PWD/interfaces/iguiapplication.h \ $$PWD/ipsubnetwhitelistoptionsdialog.h \ @@ -98,6 +99,7 @@ SOURCES += \ $$PWD/executionlogwidget.cpp \ $$PWD/fspathedit.cpp \ $$PWD/fspathedit_p.cpp \ + $$PWD/guiapplicationcomponent.cpp \ $$PWD/hidabletabwidget.cpp \ $$PWD/ipsubnetwhitelistoptionsdialog.cpp \ $$PWD/lineedit.cpp \ diff --git a/src/gui/guiapplicationcomponent.cpp b/src/gui/guiapplicationcomponent.cpp new file mode 100644 index 000000000..a10f28621 --- /dev/null +++ b/src/gui/guiapplicationcomponent.cpp @@ -0,0 +1,39 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2022 Vladimir Golovnev + * + * 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. + */ + +#include "guiapplicationcomponent.h" + +GUIApplicationComponent::GUIApplicationComponent(IGUIApplication *app) + : ApplicationComponent(app) +{ +} + +IGUIApplication *GUIApplicationComponent::app() const +{ + return static_cast(ApplicationComponent::app()); +} diff --git a/src/gui/guiapplicationcomponent.h b/src/gui/guiapplicationcomponent.h new file mode 100644 index 000000000..3f31d671d --- /dev/null +++ b/src/gui/guiapplicationcomponent.h @@ -0,0 +1,42 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2022 Vladimir Golovnev + * + * 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/applicationcomponent.h" +#include "interfaces/iguiapplication.h" + +class GUIApplicationComponent : public ApplicationComponent +{ + Q_DISABLE_COPY_MOVE(GUIApplicationComponent) + +public: + explicit GUIApplicationComponent(IGUIApplication *app); + + IGUIApplication *app() const override; +}; diff --git a/src/gui/interfaces/iguiapplication.h b/src/gui/interfaces/iguiapplication.h index b88d3bc03..fdac0e0e4 100644 --- a/src/gui/interfaces/iguiapplication.h +++ b/src/gui/interfaces/iguiapplication.h @@ -39,5 +39,5 @@ class IGUIApplication : public IApplication public: virtual ~IGUIApplication() = default; - virtual QPointer mainWindow() = 0; + virtual MainWindow *mainWindow() = 0; }; diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index b7ab276f3..459bdf42a 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -138,8 +138,9 @@ namespace #endif } -MainWindow::MainWindow(QWidget *parent) +MainWindow::MainWindow(IGUIApplication *app, QWidget *parent) : QMainWindow(parent) + , GUIApplicationComponent(app) , m_ui(new Ui::MainWindow) , m_storeExecutionLogEnabled(EXECUTIONLOG_SETTINGS_KEY(u"Enabled"_qs)) , m_storeDownloadTrackerFavicon(SETTINGS_KEY(u"DownloadTrackerFavicon"_qs)) @@ -1833,7 +1834,7 @@ void MainWindow::on_actionOptions_triggered() } else { - m_options = new OptionsDialog(this); + m_options = new OptionsDialog(app(), this); m_options->setAttribute(Qt::WA_DeleteOnClose); m_options->open(); } diff --git a/src/gui/mainwindow.h b/src/gui/mainwindow.h index 0d9c7e58f..a890c0478 100644 --- a/src/gui/mainwindow.h +++ b/src/gui/mainwindow.h @@ -38,6 +38,7 @@ #include "base/bittorrent/torrent.h" #include "base/logger.h" #include "base/settingvalue.h" +#include "guiapplicationcomponent.h" class QCloseEvent; class QFileSystemWatcher; @@ -71,13 +72,13 @@ namespace Ui class MainWindow; } -class MainWindow final : public QMainWindow +class MainWindow final : public QMainWindow, public GUIApplicationComponent { Q_OBJECT Q_DISABLE_COPY_MOVE(MainWindow) public: - explicit MainWindow(QWidget *parent = nullptr); + explicit MainWindow(IGUIApplication *app, QWidget *parent = nullptr); ~MainWindow() override; QWidget *currentTabWidget() const; diff --git a/src/gui/optionsdialog.cpp b/src/gui/optionsdialog.cpp index d5b79355c..491343a59 100644 --- a/src/gui/optionsdialog.cpp +++ b/src/gui/optionsdialog.cpp @@ -175,8 +175,9 @@ private: }; // Constructor -OptionsDialog::OptionsDialog(QWidget *parent) - : QDialog {parent} +OptionsDialog::OptionsDialog(IGUIApplication *app, QWidget *parent) + : QDialog(parent) + , GUIApplicationComponent(app) , m_ui {new Ui::OptionsDialog} , m_storeDialogSize {SETTINGS_KEY(u"Size"_qs)} , m_storeHSplitterSize {SETTINGS_KEY(u"HorizontalSplitterSizes"_qs)} @@ -542,7 +543,7 @@ OptionsDialog::OptionsDialog(QWidget *parent) // Tab selection mechanism connect(m_ui->tabSelection, &QListWidget::currentItemChanged, this, &ThisType::changePage); // Load Advanced settings - m_advancedSettings = new AdvancedSettings(m_ui->tabAdvancedPage); + m_advancedSettings = new AdvancedSettings(app, m_ui->tabAdvancedPage); m_ui->advPageLayout->addWidget(m_advancedSettings); connect(m_advancedSettings, &AdvancedSettings::settingsChanged, this, &ThisType::enableApplyButton); @@ -723,14 +724,13 @@ void OptionsDialog::saveOptions() #endif session->setPerformanceWarningEnabled(m_ui->checkBoxPerformanceWarning->isChecked()); - 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); - app->setFileLoggerAge(m_ui->spinFileLogAge->value()); - app->setFileLoggerAgeType(m_ui->comboFileLogAgeType->currentIndex()); - app->setFileLoggerDeleteOld(m_ui->checkFileLogDelete->isChecked()); - app->setFileLoggerEnabled(m_ui->checkFileLog->isChecked()); + app()->setFileLoggerPath(m_ui->textFileLogPath->selectedPath()); + app()->setFileLoggerBackup(m_ui->checkFileLogBackup->isChecked()); + app()->setFileLoggerMaxSize(m_ui->spinFileLogSize->value() * 1024); + app()->setFileLoggerAge(m_ui->spinFileLogAge->value()); + app()->setFileLoggerAgeType(m_ui->comboFileLogAgeType->currentIndex()); + app()->setFileLoggerDeleteOld(m_ui->checkFileLogDelete->isChecked()); + app()->setFileLoggerEnabled(m_ui->checkFileLog->isChecked()); // End Behavior preferences RSS::Session::instance()->setRefreshInterval(m_ui->spinRSSRefreshInterval->value()); @@ -975,19 +975,18 @@ void OptionsDialog::loadOptions() #endif m_ui->checkBoxPerformanceWarning->setChecked(session->isPerformanceWarningEnabled()); - const auto *app = dynamic_cast(QCoreApplication::instance()); - m_ui->checkFileLog->setChecked(app->isFileLoggerEnabled()); - m_ui->textFileLogPath->setSelectedPath(app->fileLoggerPath()); - const bool fileLogBackup = app->isFileLoggerBackup(); + m_ui->checkFileLog->setChecked(app()->isFileLoggerEnabled()); + m_ui->textFileLogPath->setSelectedPath(app()->fileLoggerPath()); + const bool fileLogBackup = app()->isFileLoggerBackup(); m_ui->checkFileLogBackup->setChecked(fileLogBackup); m_ui->spinFileLogSize->setEnabled(fileLogBackup); - const bool fileLogDelete = app->isFileLoggerDeleteOld(); + const bool fileLogDelete = app()->isFileLoggerDeleteOld(); m_ui->checkFileLogDelete->setChecked(fileLogDelete); m_ui->spinFileLogAge->setEnabled(fileLogDelete); m_ui->comboFileLogAgeType->setEnabled(fileLogDelete); - m_ui->spinFileLogSize->setValue(app->fileLoggerMaxSize() / 1024); - m_ui->spinFileLogAge->setValue(app->fileLoggerAge()); - m_ui->comboFileLogAgeType->setCurrentIndex(app->fileLoggerAgeType()); + m_ui->spinFileLogSize->setValue(app()->fileLoggerMaxSize() / 1024); + m_ui->spinFileLogAge->setValue(app()->fileLoggerAge()); + m_ui->comboFileLogAgeType->setCurrentIndex(app()->fileLoggerAgeType()); // End Behavior preferences m_ui->checkRSSEnable->setChecked(RSS::Session::instance()->isProcessingEnabled()); diff --git a/src/gui/optionsdialog.h b/src/gui/optionsdialog.h index b15dd434c..32f7ee5bb 100644 --- a/src/gui/optionsdialog.h +++ b/src/gui/optionsdialog.h @@ -32,6 +32,7 @@ #include "base/pathfwd.h" #include "base/settingvalue.h" +#include "guiapplicationcomponent.h" class QListWidgetItem; @@ -57,7 +58,7 @@ namespace Ui class OptionsDialog; } -class OptionsDialog final : public QDialog +class OptionsDialog final : public QDialog, public GUIApplicationComponent { Q_OBJECT Q_DISABLE_COPY_MOVE(OptionsDialog) @@ -83,8 +84,7 @@ class OptionsDialog final : public QDialog }; public: - // Constructor / Destructor - OptionsDialog(QWidget *parent = nullptr); + explicit OptionsDialog(IGUIApplication *app, QWidget *parent = nullptr); ~OptionsDialog() override; public slots: diff --git a/src/webui/api/apicontroller.cpp b/src/webui/api/apicontroller.cpp index 2c6e61d5f..ac3942fe1 100644 --- a/src/webui/api/apicontroller.cpp +++ b/src/webui/api/apicontroller.cpp @@ -37,8 +37,9 @@ #include "apierror.h" -APIController::APIController(QObject *parent) - : QObject {parent} +APIController::APIController(IApplication *app, QObject *parent) + : QObject(parent) + , ApplicationComponent(app) { } diff --git a/src/webui/api/apicontroller.h b/src/webui/api/apicontroller.h index 4defaae48..e748bc99f 100644 --- a/src/webui/api/apicontroller.h +++ b/src/webui/api/apicontroller.h @@ -32,18 +32,20 @@ #include #include +#include "base/applicationcomponent.h" + class QString; using DataMap = QHash; using StringMap = QHash; -class APIController : public QObject +class APIController : public QObject, public ApplicationComponent { Q_OBJECT Q_DISABLE_COPY_MOVE(APIController) public: - explicit APIController(QObject *parent = nullptr); + explicit APIController(IApplication *app, QObject *parent = nullptr); QVariant run(const QString &action, const StringMap ¶ms, const DataMap &data = {}); diff --git a/src/webui/api/appcontroller.cpp b/src/webui/api/appcontroller.cpp index 1b27d923b..aebd94358 100644 --- a/src/webui/api/appcontroller.cpp +++ b/src/webui/api/appcontroller.cpp @@ -294,7 +294,7 @@ void AppController::preferencesAction() // Advanced settings // qBitorrent preferences // Physical memory (RAM) usage limit - data[u"memory_working_set_limit"_qs] = dynamic_cast(QCoreApplication::instance())->memoryWorkingSetLimit(); + data[u"memory_working_set_limit"_qs] = app()->memoryWorkingSetLimit(); // Current network interface data[u"current_network_interface"_qs] = session->networkInterface(); // Current network interface address @@ -758,7 +758,7 @@ void AppController::setPreferencesAction() // qBittorrent preferences // Physical memory (RAM) usage limit if (hasKey(u"memory_working_set_limit"_qs)) - dynamic_cast(QCoreApplication::instance())->setMemoryWorkingSetLimit(it.value().toInt()); + app()->setMemoryWorkingSetLimit(it.value().toInt()); // Current network interface if (hasKey(u"current_network_interface"_qs)) { diff --git a/src/webui/api/authcontroller.cpp b/src/webui/api/authcontroller.cpp index 77bd4ca61..8900a738f 100644 --- a/src/webui/api/authcontroller.cpp +++ b/src/webui/api/authcontroller.cpp @@ -37,8 +37,8 @@ #include "apierror.h" #include "isessionmanager.h" -AuthController::AuthController(ISessionManager *sessionManager, QObject *parent) - : APIController {parent} +AuthController::AuthController(ISessionManager *sessionManager, IApplication *app, QObject *parent) + : APIController(app, parent) , m_sessionManager {sessionManager} { } diff --git a/src/webui/api/authcontroller.h b/src/webui/api/authcontroller.h index ed9641cfb..c44d68753 100644 --- a/src/webui/api/authcontroller.h +++ b/src/webui/api/authcontroller.h @@ -43,7 +43,7 @@ class AuthController : public APIController Q_DISABLE_COPY_MOVE(AuthController) public: - explicit AuthController(ISessionManager *sessionManager, QObject *parent = nullptr); + explicit AuthController(ISessionManager *sessionManager, IApplication *app, QObject *parent = nullptr); private slots: void loginAction(); diff --git a/src/webui/api/synccontroller.cpp b/src/webui/api/synccontroller.cpp index d63c9f143..a97d544b9 100644 --- a/src/webui/api/synccontroller.cpp +++ b/src/webui/api/synccontroller.cpp @@ -366,8 +366,8 @@ namespace } } -SyncController::SyncController(QObject *parent) - : APIController(parent) +SyncController::SyncController(IApplication *app, QObject *parent) + : APIController(app, parent) { m_freeDiskSpaceThread = new QThread(this); m_freeDiskSpaceChecker = new FreeDiskSpaceChecker(); diff --git a/src/webui/api/synccontroller.h b/src/webui/api/synccontroller.h index 9350c6822..dfb21fd2f 100644 --- a/src/webui/api/synccontroller.h +++ b/src/webui/api/synccontroller.h @@ -45,7 +45,7 @@ class SyncController : public APIController public: using APIController::APIController; - explicit SyncController(QObject *parent = nullptr); + explicit SyncController(IApplication *app, QObject *parent = nullptr); ~SyncController() override; private slots: diff --git a/src/webui/webapplication.cpp b/src/webui/webapplication.cpp index 92d340f71..af2bee65a 100644 --- a/src/webui/webapplication.cpp +++ b/src/webui/webapplication.cpp @@ -116,10 +116,11 @@ namespace } } -WebApplication::WebApplication(QObject *parent) +WebApplication::WebApplication(IApplication *app, QObject *parent) : QObject(parent) + , ApplicationComponent(app) , m_cacheID {QString::number(Utils::Random::rand(), 36)} - , m_authController {new AuthController(this, this)} + , m_authController {new AuthController(this, app, this)} { declarePublicAPI(u"auth/login"_qs); @@ -600,7 +601,7 @@ void WebApplication::sessionStart() return false; }); - m_currentSession = new WebSession(generateSid()); + m_currentSession = new WebSession(generateSid(), app()); m_currentSession->registerAPIController(u"app"_qs); m_currentSession->registerAPIController(u"log"_qs); m_currentSession->registerAPIController(u"rss"_qs); @@ -753,8 +754,9 @@ QHostAddress WebApplication::resolveClientAddress() const // WebSession -WebSession::WebSession(const QString &sid) - : m_sid {sid} +WebSession::WebSession(const QString &sid, IApplication *app) + : ApplicationComponent(app) + , m_sid {sid} { updateTimestamp(); } diff --git a/src/webui/webapplication.h b/src/webui/webapplication.h index a1b3d4cfa..239951058 100644 --- a/src/webui/webapplication.h +++ b/src/webui/webapplication.h @@ -39,6 +39,7 @@ #include #include +#include "base/applicationcomponent.h" #include "base/global.h" #include "base/http/irequesthandler.h" #include "base/http/responsebuilder.h" @@ -54,10 +55,10 @@ class APIController; class AuthController; class WebApplication; -class WebSession final : public QObject, public ISession +class WebSession final : public QObject, public ApplicationComponent, public ISession { public: - explicit WebSession(const QString &sid); + explicit WebSession(const QString &sid, IApplication *app); QString id() const override; @@ -68,7 +69,7 @@ public: void registerAPIController(const QString &scope) { static_assert(std::is_base_of_v, "Class should be derived from APIController."); - m_apiControllers[scope] = new T(this); + m_apiControllers[scope] = new T(app(), this); } APIController *getAPIController(const QString &scope) const; @@ -80,14 +81,15 @@ private: }; class WebApplication final - : public QObject, public Http::IRequestHandler, public ISessionManager + : public QObject, public ApplicationComponent + , public Http::IRequestHandler, public ISessionManager , private Http::ResponseBuilder { Q_OBJECT Q_DISABLE_COPY_MOVE(WebApplication) public: - explicit WebApplication(QObject *parent = nullptr); + explicit WebApplication(IApplication *app, QObject *parent = nullptr); ~WebApplication() override; Http::Response processRequest(const Http::Request &request, const Http::Environment &env) override; diff --git a/src/webui/webui.cpp b/src/webui/webui.cpp index 3134d000a..e406b474b 100644 --- a/src/webui/webui.cpp +++ b/src/webui/webui.cpp @@ -39,9 +39,8 @@ #include "base/utils/net.h" #include "webapplication.h" -WebUI::WebUI() - : m_isErrored(false) - , m_port(0) +WebUI::WebUI(IApplication *app) + : ApplicationComponent(app) { configure(); connect(Preferences::instance(), &Preferences::changed, this, &WebUI::configure); @@ -77,7 +76,7 @@ void WebUI::configure() const QString serverAddressString = pref->getWebUiAddress(); if (!m_httpServer) { - m_webapp = new WebApplication(this); + m_webapp = new WebApplication(app(), this); m_httpServer = new Http::Server(m_webapp, this); } else diff --git a/src/webui/webui.h b/src/webui/webui.h index 2ebdfbaae..f3fd37422 100644 --- a/src/webui/webui.h +++ b/src/webui/webui.h @@ -31,6 +31,8 @@ #include #include +#include "base/applicationcomponent.h" + namespace Http { class Server; @@ -43,13 +45,13 @@ namespace Net class WebApplication; -class WebUI : public QObject +class WebUI : public QObject, public ApplicationComponent { Q_OBJECT Q_DISABLE_COPY_MOVE(WebUI) public: - WebUI(); + explicit WebUI(IApplication *app); bool isErrored() const; @@ -60,9 +62,9 @@ private slots: void configure(); private: - bool m_isErrored; + bool m_isErrored = false; QPointer m_httpServer; QPointer m_dnsUpdater; QPointer m_webapp; - quint16 m_port; + quint16 m_port = 0; };