UI: Add WindowManagerFake for service mode.

This commit is contained in:
Nodir Temirkhodjaev 2021-06-03 17:21:26 +03:00
parent c930b07704
commit 121619a939
11 changed files with 81 additions and 60 deletions

View File

@ -107,7 +107,7 @@ SOURCES += \
rpc/rpcmanager.cpp \ rpc/rpcmanager.cpp \
rpc/statmanagerrpc.cpp \ rpc/statmanagerrpc.cpp \
rpc/taskmanagerrpc.cpp \ rpc/taskmanagerrpc.cpp \
rpc/windowmanagerrpc.cpp \ rpc/windowmanagerfake.cpp \
serviceinfo/serviceinfomanager.cpp \ serviceinfo/serviceinfomanager.cpp \
stat/quotamanager.cpp \ stat/quotamanager.cpp \
stat/statmanager.cpp \ stat/statmanager.cpp \
@ -258,7 +258,7 @@ HEADERS += \
rpc/rpcmanager.h \ rpc/rpcmanager.h \
rpc/statmanagerrpc.h \ rpc/statmanagerrpc.h \
rpc/taskmanagerrpc.h \ rpc/taskmanagerrpc.h \
rpc/windowmanagerrpc.h \ rpc/windowmanagerfake.h \
serviceinfo/serviceinfomanager.h \ serviceinfo/serviceinfomanager.h \
stat/quotamanager.h \ stat/quotamanager.h \
stat/statmanager.h \ stat/statmanager.h \

View File

@ -411,11 +411,7 @@ IniUser *ConfManager::iniUser() const
void ConfManager::showErrorMessage(const QString &errorMessage) void ConfManager::showErrorMessage(const QString &errorMessage)
{ {
logWarning() << "Error:" << errorMessage;
if (!IoC<FortSettings>()->isService()) {
IoC<WindowManager>()->showErrorBox(errorMessage, tr("Configuration Error")); IoC<WindowManager>()->showErrorBox(errorMessage, tr("Configuration Error"));
}
} }
void ConfManager::setUp() void ConfManager::setUp()

View File

@ -1,6 +1,8 @@
#include "fortmanager.h" #include "fortmanager.h"
#include <QApplication> #include <QApplication>
#include <QDebug>
#include <QMessageBox>
#include <QProcess> #include <QProcess>
#include <QThreadPool> #include <QThreadPool>
@ -26,7 +28,7 @@
#include "rpc/rpcmanager.h" #include "rpc/rpcmanager.h"
#include "rpc/statmanagerrpc.h" #include "rpc/statmanagerrpc.h"
#include "rpc/taskmanagerrpc.h" #include "rpc/taskmanagerrpc.h"
#include "rpc/windowmanagerrpc.h" #include "rpc/windowmanagerfake.h"
#include "task/taskinfozonedownloader.h" #include "task/taskinfozonedownloader.h"
#include "user/usersettings.h" #include "user/usersettings.h"
#include "util/dateutil.h" #include "util/dateutil.h"
@ -48,16 +50,20 @@ FortManager::~FortManager()
OsUtil::closeMutex(m_instanceMutex); OsUtil::closeMutex(m_instanceMutex);
} }
bool FortManager::checkRunningInstance() bool FortManager::checkRunningInstance(bool isService)
{ {
bool isSingleInstance; bool isSingleInstance;
m_instanceMutex = OsUtil::createMutex( m_instanceMutex =
IoC<FortSettings>()->isService() ? "Global\\" APP_BASE : APP_BASE, isSingleInstance); OsUtil::createMutex(isService ? "Global\\" APP_BASE : APP_BASE, isSingleInstance);
if (isSingleInstance)
return true;
if (!isSingleInstance) { if (isService) {
IoC<WindowManager>()->showErrorBox(tr("Application is already running!")); qWarning() << "Quit due Service is already running!";
} else {
QMessageBox::warning(nullptr, QString(), tr("Application is already running!"));
} }
return isSingleInstance; return false;
} }
void FortManager::initialize() void FortManager::initialize()
@ -133,7 +139,6 @@ void FortManager::createManagers()
appInfoManager = new AppInfoManager(settings->cacheFilePath()); appInfoManager = new AppInfoManager(settings->cacheFilePath());
logManager = new LogManager(); logManager = new LogManager();
taskManager = new TaskManager(); taskManager = new TaskManager();
windowManager = new WindowManager();
} else { } else {
confManager = new ConfManagerRpc(settings->confFilePath()); confManager = new ConfManagerRpc(settings->confFilePath());
quotaManager = new QuotaManagerRpc(); quotaManager = new QuotaManagerRpc();
@ -142,7 +147,16 @@ void FortManager::createManagers()
appInfoManager = new AppInfoManagerRpc(settings->cacheFilePath()); appInfoManager = new AppInfoManagerRpc(settings->cacheFilePath());
logManager = new LogManagerRpc(); logManager = new LogManagerRpc();
taskManager = new TaskManagerRpc(); taskManager = new TaskManagerRpc();
windowManager = new WindowManagerRpc(); }
if (settings->isService()) {
windowManager = new WindowManagerFake();
} else {
windowManager = new WindowManager();
ioc->setService(new HotKeyManager());
ioc->setService(new UserSettings());
ioc->setService(new TranslationManager());
} }
ioc->setService(confManager); ioc->setService(confManager);
@ -154,19 +168,11 @@ void FortManager::createManagers()
ioc->setService(taskManager); ioc->setService(taskManager);
ioc->setService(windowManager); ioc->setService(windowManager);
ioc->setService(new NativeEventFilter());
ioc->setService(new AppInfoCache()); ioc->setService(new AppInfoCache());
ioc->setService(new HostInfoCache()); ioc->setService(new HostInfoCache());
ioc->setService(new NativeEventFilter());
ioc->setService(new HotKeyManager());
ioc->setService(new ZoneListModel()); ioc->setService(new ZoneListModel());
if (!settings->isService()) {
ioc->setService(new UserSettings());
ioc->setService(new TranslationManager());
}
ioc->setUpAll(); ioc->setUpAll();
} }
@ -254,12 +260,10 @@ void FortManager::setupConfManager()
void FortManager::setupQuotaManager() void FortManager::setupQuotaManager()
{ {
if (!IoC<FortSettings>()->isService()) {
connect(IoC<QuotaManager>(), &QuotaManager::alert, this, [&](qint8 alertType) { connect(IoC<QuotaManager>(), &QuotaManager::alert, this, [&](qint8 alertType) {
IoC<WindowManager>()->showInfoBox( IoC<WindowManager>()->showInfoBox(
QuotaManager::alertTypeText(alertType), tr("Quota Alert")); QuotaManager::alertTypeText(alertType), tr("Quota Alert"));
}); });
}
} }
void FortManager::setupTaskManager() void FortManager::setupTaskManager()
@ -314,7 +318,7 @@ void FortManager::loadConf()
QString viaVersion; QString viaVersion;
if (!settings->canMigrate(viaVersion)) { if (!settings->canMigrate(viaVersion)) {
IoC<WindowManager>()->showInfoBox( IoC<WindowManager>()->showErrorBox(
tr("Please first install Fort Firewall v%1 and save Options from it.") tr("Please first install Fort Firewall v%1 and save Options from it.")
.arg(viaVersion)); .arg(viaVersion));
abort(); // Abort the program abort(); // Abort the program

View File

@ -16,7 +16,7 @@ public:
~FortManager() override; ~FortManager() override;
CLASS_DELETE_COPY_MOVE(FortManager) CLASS_DELETE_COPY_MOVE(FortManager)
bool checkRunningInstance(); bool checkRunningInstance(bool isService);
void initialize(); void initialize();

View File

@ -1,6 +1,7 @@
#include "windowmanager.h" #include "windowmanager.h"
#include <QApplication> #include <QApplication>
#include <QDebug>
#include <QMessageBox> #include <QMessageBox>
#include <QMouseEvent> #include <QMouseEvent>
#include <QStyle> #include <QStyle>
@ -43,6 +44,11 @@ void WindowManager::setUp()
connect(qApp, &QCoreApplication::aboutToQuit, this, &WindowManager::closeAll); connect(qApp, &QCoreApplication::aboutToQuit, this, &WindowManager::closeAll);
} }
void WindowManager::tearDown()
{
closeAll();
}
void WindowManager::setupMainWindow() void WindowManager::setupMainWindow()
{ {
m_mainWindow = new MainWindow(); m_mainWindow = new MainWindow();
@ -66,7 +72,8 @@ void WindowManager::closeMainWindow()
nativeEventFilter->unregisterHotKeys(); nativeEventFilter->unregisterHotKeys();
nativeEventFilter->unregisterSessionNotification(mainWindow()->winId()); nativeEventFilter->unregisterSessionNotification(mainWindow()->winId());
delete mainWindow(); m_mainWindow->deleteLater();
m_mainWindow = nullptr;
} }
void WindowManager::setupTrayIcon() void WindowManager::setupTrayIcon()

View File

@ -31,6 +31,7 @@ public:
GraphWindow *graphWindow() const { return m_graphWindow; } GraphWindow *graphWindow() const { return m_graphWindow; }
void setUp() override; void setUp() override;
void tearDown() override;
signals: signals:
void optWindowChanged(bool visible); void optWindowChanged(bool visible);
@ -66,8 +67,8 @@ public slots:
void quitByCheckPassword(); void quitByCheckPassword();
bool checkPassword(); bool checkPassword();
void showErrorBox(const QString &text, const QString &title = QString()); virtual void showErrorBox(const QString &text, const QString &title = QString());
void showInfoBox(const QString &text, const QString &title = QString()); virtual void showInfoBox(const QString &text, const QString &title = QString());
bool showQuestionBox(const QString &text, const QString &title = QString()); bool showQuestionBox(const QString &text, const QString &title = QString());
bool showYesNoBox(const QString &text, const QString &yesText, const QString &noText, bool showYesNoBox(const QString &text, const QString &yesText, const QString &noText,
const QString &title = QString()); const QString &title = QString());
@ -90,10 +91,6 @@ private:
static void activateModalWidget(); static void activateModalWidget();
private: private:
bool m_initialized : 1;
void *m_instanceMutex = nullptr;
TrayMessageType m_lastMessageType = MessageOptions; TrayMessageType m_lastMessageType = MessageOptions;
TrayIcon *m_trayIcon = nullptr; TrayIcon *m_trayIcon = nullptr;

View File

@ -0,0 +1,15 @@
#include "windowmanagerfake.h"
#include <QDebug>
WindowManagerFake::WindowManagerFake(QObject *parent) : WindowManager(parent) { }
void WindowManagerFake::showErrorBox(const QString &text, const QString &title)
{
qWarning() << title << ":" << text;
}
void WindowManagerFake::showInfoBox(const QString &text, const QString &title)
{
qInfo() << title << ":" << text;
}

View File

@ -0,0 +1,21 @@
#ifndef WINDOWMANAGERFAKE_H
#define WINDOWMANAGERFAKE_H
#include "../manager/windowmanager.h"
class WindowManagerFake : public WindowManager
{
Q_OBJECT
public:
explicit WindowManagerFake(QObject *parent = nullptr);
void setUp() override { }
void tearDown() override { }
public slots:
void showErrorBox(const QString &text, const QString &title = QString()) override;
void showInfoBox(const QString &text, const QString &title = QString()) override;
};
#endif // WINDOWMANAGERFAKE_H

View File

@ -1,3 +0,0 @@
#include "windowmanagerrpc.h"
WindowManagerRpc::WindowManagerRpc(QObject *parent) : WindowManager(parent) { }

View File

@ -1,16 +0,0 @@
#ifndef WINDOWMANAGERRPC_H
#define WINDOWMANAGERRPC_H
#include "../manager/windowmanager.h"
class WindowManagerRpc : public WindowManager
{
Q_OBJECT
public:
explicit WindowManagerRpc(QObject *parent = nullptr);
void setUp() override { }
};
#endif // WINDOWMANAGERRPC_H

View File

@ -128,7 +128,7 @@ int main(int argc, char *argv[])
ioc.set<FortManager>(fortManager); ioc.set<FortManager>(fortManager);
// Check running instance // Check running instance
if (!fortManager.checkRunningInstance()) if (!fortManager.checkRunningInstance(settings.isService()))
return FORT_ERROR_INSTANCE; return FORT_ERROR_INSTANCE;
fortManager.initialize(); fortManager.initialize();