Browse Source

Implement base classes for application components

PR #17219.
adaptive-webui-19844
Vladimir Golovnev 2 years ago committed by GitHub
parent
commit
f8a304abdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      src/app/application.cpp
  2. 4
      src/app/application.h
  3. 2
      src/base/CMakeLists.txt
  4. 39
      src/base/applicationcomponent.cpp
  5. 47
      src/base/applicationcomponent.h
  6. 2
      src/base/base.pri
  7. 2
      src/gui/CMakeLists.txt
  8. 11
      src/gui/advancedsettings.cpp
  9. 6
      src/gui/advancedsettings.h
  10. 2
      src/gui/gui.pri
  11. 39
      src/gui/guiapplicationcomponent.cpp
  12. 42
      src/gui/guiapplicationcomponent.h
  13. 2
      src/gui/interfaces/iguiapplication.h
  14. 5
      src/gui/mainwindow.cpp
  15. 5
      src/gui/mainwindow.h
  16. 37
      src/gui/optionsdialog.cpp
  17. 6
      src/gui/optionsdialog.h
  18. 5
      src/webui/api/apicontroller.cpp
  19. 6
      src/webui/api/apicontroller.h
  20. 4
      src/webui/api/appcontroller.cpp
  21. 4
      src/webui/api/authcontroller.cpp
  22. 2
      src/webui/api/authcontroller.h
  23. 4
      src/webui/api/synccontroller.cpp
  24. 2
      src/webui/api/synccontroller.h
  25. 12
      src/webui/webapplication.cpp
  26. 12
      src/webui/webapplication.h
  27. 7
      src/webui/webui.cpp
  28. 10
      src/webui/webui.h

6
src/app/application.cpp

@ -194,7 +194,7 @@ Application::~Application()
} }
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
QPointer<MainWindow> Application::mainWindow() MainWindow *Application::mainWindow()
{ {
return m_window; return m_window;
} }
@ -611,7 +611,7 @@ int Application::exec(const QStringList &params)
TorrentFilesWatcher::initInstance(); TorrentFilesWatcher::initInstance();
#ifndef DISABLE_WEBUI #ifndef DISABLE_WEBUI
m_webui = new WebUI; m_webui = new WebUI(this);
#ifdef DISABLE_GUI #ifdef DISABLE_GUI
if (m_webui->isErrored()) if (m_webui->isErrored())
return 1; return 1;
@ -658,7 +658,7 @@ int Application::exec(const QStringList &params)
#endif // DISABLE_WEBUI #endif // DISABLE_WEBUI
#else #else
UIThemeManager::initInstance(); UIThemeManager::initInstance();
m_window = new MainWindow; m_window = new MainWindow(this);
#endif // DISABLE_GUI #endif // DISABLE_GUI
m_running = true; m_running = true;

4
src/app/application.h

@ -118,7 +118,7 @@ public:
void setMemoryWorkingSetLimit(int size) override; void setMemoryWorkingSetLimit(int size) override;
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
QPointer<MainWindow> mainWindow() override; MainWindow *mainWindow() override;
#endif #endif
private slots: private slots:
@ -167,7 +167,7 @@ private:
SettingValue<int> m_storeMemoryWorkingSetLimit; SettingValue<int> m_storeMemoryWorkingSetLimit;
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
QPointer<MainWindow> m_window; MainWindow *m_window = nullptr;
#endif #endif
#ifndef DISABLE_WEBUI #ifndef DISABLE_WEBUI

2
src/base/CMakeLists.txt

@ -2,6 +2,7 @@ add_library(qbt_base STATIC
# headers # headers
3rdparty/expected.hpp 3rdparty/expected.hpp
algorithm.h algorithm.h
applicationcomponent.h
asyncfilestorage.h asyncfilestorage.h
bittorrent/abstractfilestorage.h bittorrent/abstractfilestorage.h
bittorrent/addtorrentparams.h bittorrent/addtorrentparams.h
@ -104,6 +105,7 @@ add_library(qbt_base STATIC
version.h version.h
# sources # sources
applicationcomponent.cpp
asyncfilestorage.cpp asyncfilestorage.cpp
bittorrent/abstractfilestorage.cpp bittorrent/abstractfilestorage.cpp
bittorrent/bandwidthscheduler.cpp bittorrent/bandwidthscheduler.cpp

39
src/base/applicationcomponent.cpp

@ -0,0 +1,39 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2022 Vladimir Golovnev <glassez@yandex.ru>
*
* 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;
}

47
src/base/applicationcomponent.h

@ -0,0 +1,47 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2022 Vladimir Golovnev <glassez@yandex.ru>
*
* 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 <QtGlobal>
#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;
};

2
src/base/base.pri

@ -1,6 +1,7 @@
HEADERS += \ HEADERS += \
$$PWD/3rdparty/expected.hpp \ $$PWD/3rdparty/expected.hpp \
$$PWD/algorithm.h \ $$PWD/algorithm.h \
$$PWD/applicationcomponent.h \
$$PWD/asyncfilestorage.h \ $$PWD/asyncfilestorage.h \
$$PWD/bittorrent/abstractfilestorage.h \ $$PWD/bittorrent/abstractfilestorage.h \
$$PWD/bittorrent/addtorrentparams.h \ $$PWD/bittorrent/addtorrentparams.h \
@ -104,6 +105,7 @@ HEADERS += \
$$PWD/version.h $$PWD/version.h
SOURCES += \ SOURCES += \
$$PWD/applicationcomponent.cpp \
$$PWD/asyncfilestorage.cpp \ $$PWD/asyncfilestorage.cpp \
$$PWD/bittorrent/abstractfilestorage.cpp \ $$PWD/bittorrent/abstractfilestorage.cpp \
$$PWD/bittorrent/bandwidthscheduler.cpp \ $$PWD/bittorrent/bandwidthscheduler.cpp \

2
src/gui/CMakeLists.txt

@ -49,6 +49,7 @@ add_library(qbt_gui STATIC
executionlogwidget.h executionlogwidget.h
fspathedit.h fspathedit.h
fspathedit_p.h fspathedit_p.h
guiapplicationcomponent.h
hidabletabwidget.h hidabletabwidget.h
interfaces/iguiapplication.h interfaces/iguiapplication.h
ipsubnetwhitelistoptionsdialog.h ipsubnetwhitelistoptionsdialog.h
@ -131,6 +132,7 @@ add_library(qbt_gui STATIC
executionlogwidget.cpp executionlogwidget.cpp
fspathedit.cpp fspathedit.cpp
fspathedit_p.cpp fspathedit_p.cpp
guiapplicationcomponent.cpp
hidabletabwidget.cpp hidabletabwidget.cpp
ipsubnetwhitelistoptionsdialog.cpp ipsubnetwhitelistoptionsdialog.cpp
lineedit.cpp lineedit.cpp

11
src/gui/advancedsettings.cpp

@ -151,8 +151,9 @@ namespace
}; };
} }
AdvancedSettings::AdvancedSettings(QWidget *parent) AdvancedSettings::AdvancedSettings(IGUIApplication *app, QWidget *parent)
: QTableWidget(parent) : QTableWidget(parent)
, GUIApplicationComponent(app)
{ {
// column // column
setColumnCount(COL_COUNT); setColumnCount(COL_COUNT);
@ -178,7 +179,7 @@ void AdvancedSettings::saveAdvancedSettings() const
session->setResumeDataStorageType(m_comboBoxResumeDataStorage.currentData().value<BitTorrent::ResumeDataStorageType>()); session->setResumeDataStorageType(m_comboBoxResumeDataStorage.currentData().value<BitTorrent::ResumeDataStorageType>());
// Physical memory (RAM) usage limit // Physical memory (RAM) usage limit
dynamic_cast<IApplication *>(QCoreApplication::instance())->setMemoryWorkingSetLimit(m_spinBoxMemoryWorkingSetLimit.value()); app()->setMemoryWorkingSetLimit(m_spinBoxMemoryWorkingSetLimit.value());
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
session->setOSMemoryPriority(m_comboBoxOSMemoryPriority.currentData().value<BitTorrent::OSMemoryPriority>()); session->setOSMemoryPriority(m_comboBoxOSMemoryPriority.currentData().value<BitTorrent::OSMemoryPriority>());
#endif #endif
@ -266,7 +267,7 @@ void AdvancedSettings::saveAdvancedSettings() const
// Stop tracker timeout // Stop tracker timeout
session->setStopTrackerTimeout(m_spinBoxStopTrackerTimeout.value()); session->setStopTrackerTimeout(m_spinBoxStopTrackerTimeout.value());
// Program notification // Program notification
MainWindow *mainWindow = dynamic_cast<IGUIApplication *>(QCoreApplication::instance())->mainWindow(); MainWindow *mainWindow = app()->mainWindow();
mainWindow->setNotificationsEnabled(m_checkBoxProgramNotifications.isChecked()); mainWindow->setNotificationsEnabled(m_checkBoxProgramNotifications.isChecked());
mainWindow->setTorrentAddedNotificationsEnabled(m_checkBoxTorrentAddedNotifications.isChecked()); mainWindow->setTorrentAddedNotificationsEnabled(m_checkBoxTorrentAddedNotifications.isChecked());
#if (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)) && defined(QT_DBUS_LIB) #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<int>::max()); m_spinBoxMemoryWorkingSetLimit.setMaximum(std::numeric_limits<int>::max());
m_spinBoxMemoryWorkingSetLimit.setSuffix(tr(" MiB")); m_spinBoxMemoryWorkingSetLimit.setSuffix(tr(" MiB"));
m_spinBoxMemoryWorkingSetLimit.setToolTip(tr("This option is less effective on Linux")); m_spinBoxMemoryWorkingSetLimit.setToolTip(tr("This option is less effective on Linux"));
m_spinBoxMemoryWorkingSetLimit.setValue(dynamic_cast<IApplication *>(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"(?)")) addRow(MEMORY_WORKING_SET_LIMIT, (tr("Physical memory (RAM) usage limit") + u' ' + makeLink(u"https://wikipedia.org/wiki/Working_set", u"(?)"))
, &m_spinBoxMemoryWorkingSetLimit); , &m_spinBoxMemoryWorkingSetLimit);
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
@ -670,7 +671,7 @@ void AdvancedSettings::loadAdvancedSettings()
, &m_spinBoxStopTrackerTimeout); , &m_spinBoxStopTrackerTimeout);
// Program notifications // Program notifications
const MainWindow *mainWindow = dynamic_cast<IGUIApplication *>(QCoreApplication::instance())->mainWindow(); const MainWindow *mainWindow = app()->mainWindow();
m_checkBoxProgramNotifications.setChecked(mainWindow->isNotificationsEnabled()); m_checkBoxProgramNotifications.setChecked(mainWindow->isNotificationsEnabled());
addRow(PROGRAM_NOTIFICATIONS, tr("Display notifications"), &m_checkBoxProgramNotifications); addRow(PROGRAM_NOTIFICATIONS, tr("Display notifications"), &m_checkBoxProgramNotifications);
// Torrent added notifications // Torrent added notifications

6
src/gui/advancedsettings.h

@ -34,13 +34,15 @@
#include <QSpinBox> #include <QSpinBox>
#include <QTableWidget> #include <QTableWidget>
class AdvancedSettings final : public QTableWidget #include "guiapplicationcomponent.h"
class AdvancedSettings final : public QTableWidget, public GUIApplicationComponent
{ {
Q_OBJECT Q_OBJECT
Q_DISABLE_COPY_MOVE(AdvancedSettings) Q_DISABLE_COPY_MOVE(AdvancedSettings)
public: public:
AdvancedSettings(QWidget *parent); explicit AdvancedSettings(IGUIApplication *app, QWidget *parent = nullptr);
public slots: public slots:
void saveAdvancedSettings() const; void saveAdvancedSettings() const;

2
src/gui/gui.pri

@ -16,6 +16,7 @@ HEADERS += \
$$PWD/executionlogwidget.h \ $$PWD/executionlogwidget.h \
$$PWD/fspathedit.h \ $$PWD/fspathedit.h \
$$PWD/fspathedit_p.h \ $$PWD/fspathedit_p.h \
$$PWD/guiapplicationcomponent.h \
$$PWD/hidabletabwidget.h \ $$PWD/hidabletabwidget.h \
$$PWD/interfaces/iguiapplication.h \ $$PWD/interfaces/iguiapplication.h \
$$PWD/ipsubnetwhitelistoptionsdialog.h \ $$PWD/ipsubnetwhitelistoptionsdialog.h \
@ -98,6 +99,7 @@ SOURCES += \
$$PWD/executionlogwidget.cpp \ $$PWD/executionlogwidget.cpp \
$$PWD/fspathedit.cpp \ $$PWD/fspathedit.cpp \
$$PWD/fspathedit_p.cpp \ $$PWD/fspathedit_p.cpp \
$$PWD/guiapplicationcomponent.cpp \
$$PWD/hidabletabwidget.cpp \ $$PWD/hidabletabwidget.cpp \
$$PWD/ipsubnetwhitelistoptionsdialog.cpp \ $$PWD/ipsubnetwhitelistoptionsdialog.cpp \
$$PWD/lineedit.cpp \ $$PWD/lineedit.cpp \

39
src/gui/guiapplicationcomponent.cpp

@ -0,0 +1,39 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2022 Vladimir Golovnev <glassez@yandex.ru>
*
* 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<IGUIApplication *>(ApplicationComponent::app());
}

42
src/gui/guiapplicationcomponent.h

@ -0,0 +1,42 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2022 Vladimir Golovnev <glassez@yandex.ru>
*
* 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;
};

2
src/gui/interfaces/iguiapplication.h

@ -39,5 +39,5 @@ class IGUIApplication : public IApplication
public: public:
virtual ~IGUIApplication() = default; virtual ~IGUIApplication() = default;
virtual QPointer<MainWindow> mainWindow() = 0; virtual MainWindow *mainWindow() = 0;
}; };

5
src/gui/mainwindow.cpp

@ -138,8 +138,9 @@ namespace
#endif #endif
} }
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(IGUIApplication *app, QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
, GUIApplicationComponent(app)
, m_ui(new Ui::MainWindow) , m_ui(new Ui::MainWindow)
, m_storeExecutionLogEnabled(EXECUTIONLOG_SETTINGS_KEY(u"Enabled"_qs)) , m_storeExecutionLogEnabled(EXECUTIONLOG_SETTINGS_KEY(u"Enabled"_qs))
, m_storeDownloadTrackerFavicon(SETTINGS_KEY(u"DownloadTrackerFavicon"_qs)) , m_storeDownloadTrackerFavicon(SETTINGS_KEY(u"DownloadTrackerFavicon"_qs))
@ -1833,7 +1834,7 @@ void MainWindow::on_actionOptions_triggered()
} }
else else
{ {
m_options = new OptionsDialog(this); m_options = new OptionsDialog(app(), this);
m_options->setAttribute(Qt::WA_DeleteOnClose); m_options->setAttribute(Qt::WA_DeleteOnClose);
m_options->open(); m_options->open();
} }

5
src/gui/mainwindow.h

@ -38,6 +38,7 @@
#include "base/bittorrent/torrent.h" #include "base/bittorrent/torrent.h"
#include "base/logger.h" #include "base/logger.h"
#include "base/settingvalue.h" #include "base/settingvalue.h"
#include "guiapplicationcomponent.h"
class QCloseEvent; class QCloseEvent;
class QFileSystemWatcher; class QFileSystemWatcher;
@ -71,13 +72,13 @@ namespace Ui
class MainWindow; class MainWindow;
} }
class MainWindow final : public QMainWindow class MainWindow final : public QMainWindow, public GUIApplicationComponent
{ {
Q_OBJECT Q_OBJECT
Q_DISABLE_COPY_MOVE(MainWindow) Q_DISABLE_COPY_MOVE(MainWindow)
public: public:
explicit MainWindow(QWidget *parent = nullptr); explicit MainWindow(IGUIApplication *app, QWidget *parent = nullptr);
~MainWindow() override; ~MainWindow() override;
QWidget *currentTabWidget() const; QWidget *currentTabWidget() const;

37
src/gui/optionsdialog.cpp

@ -175,8 +175,9 @@ private:
}; };
// Constructor // Constructor
OptionsDialog::OptionsDialog(QWidget *parent) OptionsDialog::OptionsDialog(IGUIApplication *app, QWidget *parent)
: QDialog {parent} : QDialog(parent)
, GUIApplicationComponent(app)
, m_ui {new Ui::OptionsDialog} , m_ui {new Ui::OptionsDialog}
, m_storeDialogSize {SETTINGS_KEY(u"Size"_qs)} , m_storeDialogSize {SETTINGS_KEY(u"Size"_qs)}
, m_storeHSplitterSize {SETTINGS_KEY(u"HorizontalSplitterSizes"_qs)} , m_storeHSplitterSize {SETTINGS_KEY(u"HorizontalSplitterSizes"_qs)}
@ -542,7 +543,7 @@ OptionsDialog::OptionsDialog(QWidget *parent)
// Tab selection mechanism // Tab selection mechanism
connect(m_ui->tabSelection, &QListWidget::currentItemChanged, this, &ThisType::changePage); connect(m_ui->tabSelection, &QListWidget::currentItemChanged, this, &ThisType::changePage);
// Load Advanced settings // Load Advanced settings
m_advancedSettings = new AdvancedSettings(m_ui->tabAdvancedPage); m_advancedSettings = new AdvancedSettings(app, m_ui->tabAdvancedPage);
m_ui->advPageLayout->addWidget(m_advancedSettings); m_ui->advPageLayout->addWidget(m_advancedSettings);
connect(m_advancedSettings, &AdvancedSettings::settingsChanged, this, &ThisType::enableApplyButton); connect(m_advancedSettings, &AdvancedSettings::settingsChanged, this, &ThisType::enableApplyButton);
@ -723,14 +724,13 @@ void OptionsDialog::saveOptions()
#endif #endif
session->setPerformanceWarningEnabled(m_ui->checkBoxPerformanceWarning->isChecked()); session->setPerformanceWarningEnabled(m_ui->checkBoxPerformanceWarning->isChecked());
auto *app = dynamic_cast<IApplication *>(QCoreApplication::instance()); app()->setFileLoggerPath(m_ui->textFileLogPath->selectedPath());
app->setFileLoggerPath(m_ui->textFileLogPath->selectedPath()); app()->setFileLoggerBackup(m_ui->checkFileLogBackup->isChecked());
app->setFileLoggerBackup(m_ui->checkFileLogBackup->isChecked()); app()->setFileLoggerMaxSize(m_ui->spinFileLogSize->value() * 1024);
app->setFileLoggerMaxSize(m_ui->spinFileLogSize->value() * 1024); app()->setFileLoggerAge(m_ui->spinFileLogAge->value());
app->setFileLoggerAge(m_ui->spinFileLogAge->value()); app()->setFileLoggerAgeType(m_ui->comboFileLogAgeType->currentIndex());
app->setFileLoggerAgeType(m_ui->comboFileLogAgeType->currentIndex()); app()->setFileLoggerDeleteOld(m_ui->checkFileLogDelete->isChecked());
app->setFileLoggerDeleteOld(m_ui->checkFileLogDelete->isChecked()); app()->setFileLoggerEnabled(m_ui->checkFileLog->isChecked());
app->setFileLoggerEnabled(m_ui->checkFileLog->isChecked());
// End Behavior preferences // End Behavior preferences
RSS::Session::instance()->setRefreshInterval(m_ui->spinRSSRefreshInterval->value()); RSS::Session::instance()->setRefreshInterval(m_ui->spinRSSRefreshInterval->value());
@ -975,19 +975,18 @@ void OptionsDialog::loadOptions()
#endif #endif
m_ui->checkBoxPerformanceWarning->setChecked(session->isPerformanceWarningEnabled()); m_ui->checkBoxPerformanceWarning->setChecked(session->isPerformanceWarningEnabled());
const auto *app = dynamic_cast<IApplication *>(QCoreApplication::instance()); m_ui->checkFileLog->setChecked(app()->isFileLoggerEnabled());
m_ui->checkFileLog->setChecked(app->isFileLoggerEnabled()); m_ui->textFileLogPath->setSelectedPath(app()->fileLoggerPath());
m_ui->textFileLogPath->setSelectedPath(app->fileLoggerPath()); const bool fileLogBackup = app()->isFileLoggerBackup();
const bool fileLogBackup = app->isFileLoggerBackup();
m_ui->checkFileLogBackup->setChecked(fileLogBackup); m_ui->checkFileLogBackup->setChecked(fileLogBackup);
m_ui->spinFileLogSize->setEnabled(fileLogBackup); m_ui->spinFileLogSize->setEnabled(fileLogBackup);
const bool fileLogDelete = app->isFileLoggerDeleteOld(); const bool fileLogDelete = app()->isFileLoggerDeleteOld();
m_ui->checkFileLogDelete->setChecked(fileLogDelete); m_ui->checkFileLogDelete->setChecked(fileLogDelete);
m_ui->spinFileLogAge->setEnabled(fileLogDelete); m_ui->spinFileLogAge->setEnabled(fileLogDelete);
m_ui->comboFileLogAgeType->setEnabled(fileLogDelete); m_ui->comboFileLogAgeType->setEnabled(fileLogDelete);
m_ui->spinFileLogSize->setValue(app->fileLoggerMaxSize() / 1024); m_ui->spinFileLogSize->setValue(app()->fileLoggerMaxSize() / 1024);
m_ui->spinFileLogAge->setValue(app->fileLoggerAge()); m_ui->spinFileLogAge->setValue(app()->fileLoggerAge());
m_ui->comboFileLogAgeType->setCurrentIndex(app->fileLoggerAgeType()); m_ui->comboFileLogAgeType->setCurrentIndex(app()->fileLoggerAgeType());
// End Behavior preferences // End Behavior preferences
m_ui->checkRSSEnable->setChecked(RSS::Session::instance()->isProcessingEnabled()); m_ui->checkRSSEnable->setChecked(RSS::Session::instance()->isProcessingEnabled());

6
src/gui/optionsdialog.h

@ -32,6 +32,7 @@
#include "base/pathfwd.h" #include "base/pathfwd.h"
#include "base/settingvalue.h" #include "base/settingvalue.h"
#include "guiapplicationcomponent.h"
class QListWidgetItem; class QListWidgetItem;
@ -57,7 +58,7 @@ namespace Ui
class OptionsDialog; class OptionsDialog;
} }
class OptionsDialog final : public QDialog class OptionsDialog final : public QDialog, public GUIApplicationComponent
{ {
Q_OBJECT Q_OBJECT
Q_DISABLE_COPY_MOVE(OptionsDialog) Q_DISABLE_COPY_MOVE(OptionsDialog)
@ -83,8 +84,7 @@ class OptionsDialog final : public QDialog
}; };
public: public:
// Constructor / Destructor explicit OptionsDialog(IGUIApplication *app, QWidget *parent = nullptr);
OptionsDialog(QWidget *parent = nullptr);
~OptionsDialog() override; ~OptionsDialog() override;
public slots: public slots:

5
src/webui/api/apicontroller.cpp

@ -37,8 +37,9 @@
#include "apierror.h" #include "apierror.h"
APIController::APIController(QObject *parent) APIController::APIController(IApplication *app, QObject *parent)
: QObject {parent} : QObject(parent)
, ApplicationComponent(app)
{ {
} }

6
src/webui/api/apicontroller.h

@ -32,18 +32,20 @@
#include <QObject> #include <QObject>
#include <QVariant> #include <QVariant>
#include "base/applicationcomponent.h"
class QString; class QString;
using DataMap = QHash<QString, QByteArray>; using DataMap = QHash<QString, QByteArray>;
using StringMap = QHash<QString, QString>; using StringMap = QHash<QString, QString>;
class APIController : public QObject class APIController : public QObject, public ApplicationComponent
{ {
Q_OBJECT Q_OBJECT
Q_DISABLE_COPY_MOVE(APIController) Q_DISABLE_COPY_MOVE(APIController)
public: public:
explicit APIController(QObject *parent = nullptr); explicit APIController(IApplication *app, QObject *parent = nullptr);
QVariant run(const QString &action, const StringMap &params, const DataMap &data = {}); QVariant run(const QString &action, const StringMap &params, const DataMap &data = {});

4
src/webui/api/appcontroller.cpp

@ -294,7 +294,7 @@ void AppController::preferencesAction()
// Advanced settings // Advanced settings
// qBitorrent preferences // qBitorrent preferences
// Physical memory (RAM) usage limit // Physical memory (RAM) usage limit
data[u"memory_working_set_limit"_qs] = dynamic_cast<IApplication *>(QCoreApplication::instance())->memoryWorkingSetLimit(); data[u"memory_working_set_limit"_qs] = app()->memoryWorkingSetLimit();
// Current network interface // Current network interface
data[u"current_network_interface"_qs] = session->networkInterface(); data[u"current_network_interface"_qs] = session->networkInterface();
// Current network interface address // Current network interface address
@ -758,7 +758,7 @@ void AppController::setPreferencesAction()
// qBittorrent preferences // qBittorrent preferences
// Physical memory (RAM) usage limit // Physical memory (RAM) usage limit
if (hasKey(u"memory_working_set_limit"_qs)) if (hasKey(u"memory_working_set_limit"_qs))
dynamic_cast<IApplication *>(QCoreApplication::instance())->setMemoryWorkingSetLimit(it.value().toInt()); app()->setMemoryWorkingSetLimit(it.value().toInt());
// Current network interface // Current network interface
if (hasKey(u"current_network_interface"_qs)) if (hasKey(u"current_network_interface"_qs))
{ {

4
src/webui/api/authcontroller.cpp

@ -37,8 +37,8 @@
#include "apierror.h" #include "apierror.h"
#include "isessionmanager.h" #include "isessionmanager.h"
AuthController::AuthController(ISessionManager *sessionManager, QObject *parent) AuthController::AuthController(ISessionManager *sessionManager, IApplication *app, QObject *parent)
: APIController {parent} : APIController(app, parent)
, m_sessionManager {sessionManager} , m_sessionManager {sessionManager}
{ {
} }

2
src/webui/api/authcontroller.h

@ -43,7 +43,7 @@ class AuthController : public APIController
Q_DISABLE_COPY_MOVE(AuthController) Q_DISABLE_COPY_MOVE(AuthController)
public: public:
explicit AuthController(ISessionManager *sessionManager, QObject *parent = nullptr); explicit AuthController(ISessionManager *sessionManager, IApplication *app, QObject *parent = nullptr);
private slots: private slots:
void loginAction(); void loginAction();

4
src/webui/api/synccontroller.cpp

@ -366,8 +366,8 @@ namespace
} }
} }
SyncController::SyncController(QObject *parent) SyncController::SyncController(IApplication *app, QObject *parent)
: APIController(parent) : APIController(app, parent)
{ {
m_freeDiskSpaceThread = new QThread(this); m_freeDiskSpaceThread = new QThread(this);
m_freeDiskSpaceChecker = new FreeDiskSpaceChecker(); m_freeDiskSpaceChecker = new FreeDiskSpaceChecker();

2
src/webui/api/synccontroller.h

@ -45,7 +45,7 @@ class SyncController : public APIController
public: public:
using APIController::APIController; using APIController::APIController;
explicit SyncController(QObject *parent = nullptr); explicit SyncController(IApplication *app, QObject *parent = nullptr);
~SyncController() override; ~SyncController() override;
private slots: private slots:

12
src/webui/webapplication.cpp

@ -116,10 +116,11 @@ namespace
} }
} }
WebApplication::WebApplication(QObject *parent) WebApplication::WebApplication(IApplication *app, QObject *parent)
: QObject(parent) : QObject(parent)
, ApplicationComponent(app)
, m_cacheID {QString::number(Utils::Random::rand(), 36)} , 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); declarePublicAPI(u"auth/login"_qs);
@ -600,7 +601,7 @@ void WebApplication::sessionStart()
return false; return false;
}); });
m_currentSession = new WebSession(generateSid()); m_currentSession = new WebSession(generateSid(), app());
m_currentSession->registerAPIController<AppController>(u"app"_qs); m_currentSession->registerAPIController<AppController>(u"app"_qs);
m_currentSession->registerAPIController<LogController>(u"log"_qs); m_currentSession->registerAPIController<LogController>(u"log"_qs);
m_currentSession->registerAPIController<RSSController>(u"rss"_qs); m_currentSession->registerAPIController<RSSController>(u"rss"_qs);
@ -753,8 +754,9 @@ QHostAddress WebApplication::resolveClientAddress() const
// WebSession // WebSession
WebSession::WebSession(const QString &sid) WebSession::WebSession(const QString &sid, IApplication *app)
: m_sid {sid} : ApplicationComponent(app)
, m_sid {sid}
{ {
updateTimestamp(); updateTimestamp();
} }

12
src/webui/webapplication.h

@ -39,6 +39,7 @@
#include <QSet> #include <QSet>
#include <QTranslator> #include <QTranslator>
#include "base/applicationcomponent.h"
#include "base/global.h" #include "base/global.h"
#include "base/http/irequesthandler.h" #include "base/http/irequesthandler.h"
#include "base/http/responsebuilder.h" #include "base/http/responsebuilder.h"
@ -54,10 +55,10 @@ class APIController;
class AuthController; class AuthController;
class WebApplication; class WebApplication;
class WebSession final : public QObject, public ISession class WebSession final : public QObject, public ApplicationComponent, public ISession
{ {
public: public:
explicit WebSession(const QString &sid); explicit WebSession(const QString &sid, IApplication *app);
QString id() const override; QString id() const override;
@ -68,7 +69,7 @@ public:
void registerAPIController(const QString &scope) void registerAPIController(const QString &scope)
{ {
static_assert(std::is_base_of_v<APIController, T>, "Class should be derived from APIController."); static_assert(std::is_base_of_v<APIController, T>, "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; APIController *getAPIController(const QString &scope) const;
@ -80,14 +81,15 @@ private:
}; };
class WebApplication final class WebApplication final
: public QObject, public Http::IRequestHandler, public ISessionManager : public QObject, public ApplicationComponent
, public Http::IRequestHandler, public ISessionManager
, private Http::ResponseBuilder , private Http::ResponseBuilder
{ {
Q_OBJECT Q_OBJECT
Q_DISABLE_COPY_MOVE(WebApplication) Q_DISABLE_COPY_MOVE(WebApplication)
public: public:
explicit WebApplication(QObject *parent = nullptr); explicit WebApplication(IApplication *app, QObject *parent = nullptr);
~WebApplication() override; ~WebApplication() override;
Http::Response processRequest(const Http::Request &request, const Http::Environment &env) override; Http::Response processRequest(const Http::Request &request, const Http::Environment &env) override;

7
src/webui/webui.cpp

@ -39,9 +39,8 @@
#include "base/utils/net.h" #include "base/utils/net.h"
#include "webapplication.h" #include "webapplication.h"
WebUI::WebUI() WebUI::WebUI(IApplication *app)
: m_isErrored(false) : ApplicationComponent(app)
, m_port(0)
{ {
configure(); configure();
connect(Preferences::instance(), &Preferences::changed, this, &WebUI::configure); connect(Preferences::instance(), &Preferences::changed, this, &WebUI::configure);
@ -77,7 +76,7 @@ void WebUI::configure()
const QString serverAddressString = pref->getWebUiAddress(); const QString serverAddressString = pref->getWebUiAddress();
if (!m_httpServer) if (!m_httpServer)
{ {
m_webapp = new WebApplication(this); m_webapp = new WebApplication(app(), this);
m_httpServer = new Http::Server(m_webapp, this); m_httpServer = new Http::Server(m_webapp, this);
} }
else else

10
src/webui/webui.h

@ -31,6 +31,8 @@
#include <QObject> #include <QObject>
#include <QPointer> #include <QPointer>
#include "base/applicationcomponent.h"
namespace Http namespace Http
{ {
class Server; class Server;
@ -43,13 +45,13 @@ namespace Net
class WebApplication; class WebApplication;
class WebUI : public QObject class WebUI : public QObject, public ApplicationComponent
{ {
Q_OBJECT Q_OBJECT
Q_DISABLE_COPY_MOVE(WebUI) Q_DISABLE_COPY_MOVE(WebUI)
public: public:
WebUI(); explicit WebUI(IApplication *app);
bool isErrored() const; bool isErrored() const;
@ -60,9 +62,9 @@ private slots:
void configure(); void configure();
private: private:
bool m_isErrored; bool m_isErrored = false;
QPointer<Http::Server> m_httpServer; QPointer<Http::Server> m_httpServer;
QPointer<Net::DNSUpdater> m_dnsUpdater; QPointer<Net::DNSUpdater> m_dnsUpdater;
QPointer<WebApplication> m_webapp; QPointer<WebApplication> m_webapp;
quint16 m_port; quint16 m_port = 0;
}; };

Loading…
Cancel
Save