Implement base classes for application components

PR #17219.
This commit is contained in:
Vladimir Golovnev
2022-06-25 15:46:55 +03:00
committed by GitHub
parent 41a38428fc
commit f8a304abdc
28 changed files with 256 additions and 69 deletions

View File

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

View File

@@ -32,18 +32,20 @@
#include <QObject>
#include <QVariant>
#include "base/applicationcomponent.h"
class QString;
using DataMap = QHash<QString, QByteArray>;
using StringMap = QHash<QString, QString>;
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 &params, const DataMap &data = {});

View File

@@ -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<IApplication *>(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<IApplication *>(QCoreApplication::instance())->setMemoryWorkingSetLimit(it.value().toInt());
app()->setMemoryWorkingSetLimit(it.value().toInt());
// Current network interface
if (hasKey(u"current_network_interface"_qs))
{

View File

@@ -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}
{
}

View File

@@ -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();

View File

@@ -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();

View File

@@ -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:

View File

@@ -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<AppController>(u"app"_qs);
m_currentSession->registerAPIController<LogController>(u"log"_qs);
m_currentSession->registerAPIController<RSSController>(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();
}

View File

@@ -39,6 +39,7 @@
#include <QSet>
#include <QTranslator>
#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<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;
@@ -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;

View File

@@ -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

View File

@@ -31,6 +31,8 @@
#include <QObject>
#include <QPointer>
#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<Http::Server> m_httpServer;
QPointer<Net::DNSUpdater> m_dnsUpdater;
QPointer<WebApplication> m_webapp;
quint16 m_port;
quint16 m_port = 0;
};