From 98a5bd8667658b93c02374990e38990c44ce89b1 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Wed, 31 Mar 2021 13:29:19 +0300 Subject: [PATCH] UI: ControlManager: Prepare service handling. --- deploy/FortFirewall.iss | 2 +- src/ui/control/controlmanager.cpp | 33 ++++++++++++++++++++----------- src/ui/control/controlmanager.h | 20 ++++++++----------- src/ui/fortmanager.cpp | 22 ++++++++++++--------- src/ui/fortmanager.h | 9 ++++++--- src/ui/fortsettings.cpp | 26 +++++++++++++----------- src/ui/fortsettings.h | 2 ++ src/ui/main.cpp | 4 ++-- 8 files changed, 69 insertions(+), 49 deletions(-) diff --git a/deploy/FortFirewall.iss b/deploy/FortFirewall.iss index 25ac405d..20a3d6f5 100644 --- a/deploy/FortFirewall.iss +++ b/deploy/FortFirewall.iss @@ -58,7 +58,7 @@ Root: HKCR; Subkey: "{#REG_SHELL}"; Flags: uninsdeletekeyifempty; Tasks: explore Root: HKCR; Subkey: "{#REG_SHELL_MENU}"; Flags: deletekey uninsdeletekey Root: HKCR; Subkey: "{#REG_SHELL_MENU}"; ValueType: string; ValueName: "icon"; ValueData: "{#APP_EXE}"; Tasks: explorer Root: HKCR; Subkey: "{#REG_SHELL_MENU}"; ValueType: string; ValueName: "MUIVerb"; ValueData: "Fort Firewall ..."; Tasks: explorer -Root: HKCR; Subkey: "{#REG_SHELL_MENU}\command"; ValueType: string; ValueData: """{#APP_EXE}"" -c prog add ""%1"""; Tasks: explorer +Root: HKCR; Subkey: "{#REG_SHELL_MENU}\command"; ValueType: string; ValueData: """{#APP_EXE}"" -w -c prog add ""%1"""; Tasks: explorer [Icons] ; Start menu shortcut diff --git a/src/ui/control/controlmanager.cpp b/src/ui/control/controlmanager.cpp index 4ced7bd2..b022039f 100644 --- a/src/ui/control/controlmanager.cpp +++ b/src/ui/control/controlmanager.cpp @@ -1,5 +1,6 @@ #include "controlmanager.h" +#include #include #include @@ -16,13 +17,12 @@ Q_LOGGING_CATEGORY(CLOG_CONTROL_MANAGER, "control") #define logWarning() qCWarning(CLOG_CONTROL_MANAGER, ) #define logCritical() qCCritical(CLOG_CONTROL_MANAGER, ) -ControlManager::ControlManager(const QString &globalName, const QString &command, QObject *parent) : +ControlManager::ControlManager(FortSettings *settings, QObject *parent) : QObject(parent), - m_isClient(!command.isEmpty()), - m_command(command), - m_semaphore(globalName + QLatin1String("_ControlSemaphore"), 0, + m_settings(settings), + m_semaphore(QApplication::applicationName() + QLatin1String("_ControlSemaphore"), 0, isClient() ? QSystemSemaphore::Open : QSystemSemaphore::Create), - m_sharedMemory(globalName + QLatin1String("_ControlSharedMemory")) + m_sharedMemory(QApplication::applicationName() + QLatin1String("_ControlSharedMemory")) { } @@ -31,6 +31,11 @@ ControlManager::~ControlManager() abort(); } +bool ControlManager::isClient() const +{ + return !settings()->controlCommand().isEmpty(); +} + bool ControlManager::listen(FortManager *fortManager) { if (m_sharedMemory.size() > 0) @@ -50,8 +55,12 @@ bool ControlManager::listen(FortManager *fortManager) return true; } -bool ControlManager::post(const QStringList &args) +bool ControlManager::post() { + if (settings()->isWindowControl()) { + // TODO + } + if (!m_sharedMemory.attach()) { logWarning() << "Shared Memory attach error:" << m_sharedMemory.errorString(); return false; @@ -59,15 +68,17 @@ bool ControlManager::post(const QStringList &args) ControlWorker worker(&m_semaphore, &m_sharedMemory); - return worker.post(m_command, args); + return worker.post(settings()->controlCommand(), settings()->args()); } -void ControlManager::processRequest(const QString &command, const QStringList &args) +bool ControlManager::processRequest(const QString &command, const QStringList &args) { QString errorMessage; if (!processCommand(command, args, errorMessage)) { logWarning() << "Bad control command" << errorMessage << ':' << command << args; + return false; } + return true; } bool ControlManager::processCommand( @@ -82,7 +93,7 @@ bool ControlManager::processCommand( return false; } - auto conf = m_fortManager->conf(); + auto conf = fortManager()->conf(); bool onlyFlags = true; const auto confPropName = args.at(0); @@ -103,7 +114,7 @@ bool ControlManager::processCommand( } if (ok) { - m_fortManager->saveOriginConf(tr("Control command executed"), onlyFlags); + fortManager()->saveOriginConf(tr("Control command executed"), onlyFlags); } } else if (command == "prog") { if (argsSize < 1) { @@ -119,7 +130,7 @@ bool ControlManager::processCommand( return false; } - ok = m_fortManager->showProgramEditForm(args.at(1)); + ok = fortManager()->showProgramEditForm(args.at(1)); } } diff --git a/src/ui/control/controlmanager.h b/src/ui/control/controlmanager.h index f85cfacf..3bae6780 100644 --- a/src/ui/control/controlmanager.h +++ b/src/ui/control/controlmanager.h @@ -9,28 +9,27 @@ class ControlWorker; class FortManager; +class FortSettings; class ControlManager : public QObject { Q_OBJECT public: - explicit ControlManager( - const QString &globalName, const QString &command, QObject *parent = nullptr); + explicit ControlManager(FortSettings *settings, QObject *parent = nullptr); ~ControlManager() override; CLASS_DELETE_COPY_MOVE(ControlManager) - bool isClient() const { return m_isClient; } + bool isClient() const; bool listen(FortManager *fortManager); - bool post(const QStringList &args); + bool post(); -signals: - -public slots: + FortSettings *settings() const { return m_settings; } + FortManager *fortManager() const { return m_fortManager; } private slots: - void processRequest(const QString &command, const QStringList &args); + bool processRequest(const QString &command, const QStringList &args); private: bool processCommand(const QString &command, const QStringList &args, QString &errorMessage); @@ -40,13 +39,10 @@ private: void abort(); private: - bool m_isClient = false; - + FortSettings *m_settings = nullptr; FortManager *m_fortManager = nullptr; ControlWorker *m_worker = nullptr; - QString m_command; - QSystemSemaphore m_semaphore; QSharedMemory m_sharedMemory; }; diff --git a/src/ui/fortmanager.cpp b/src/ui/fortmanager.cpp index 5ca5b0cf..4e37b2c1 100644 --- a/src/ui/fortmanager.cpp +++ b/src/ui/fortmanager.cpp @@ -16,6 +16,7 @@ #include "conf/firewallconf.h" #include "driver/drivermanager.h" #include "form/conn/connectionswindow.h" +#include "form/controls/mainwindow.h" #include "form/graph/graphwindow.h" #include "form/opt/optionswindow.h" #include "form/prog/programswindow.h" @@ -50,20 +51,21 @@ #include "util/stringutil.h" #include "util/window/widgetwindowstatewatcher.h" -FortManager::FortManager(FortSettings *fortSettings, EnvManager *envManager, QObject *parent) : +FortManager::FortManager(FortSettings *settings, EnvManager *envManager, QObject *parent) : QObject(parent), + m_mainWindow(new MainWindow()), m_trayIcon(new QSystemTrayIcon(this)), m_progWindowState(new WidgetWindowStateWatcher(this)), m_optWindowState(new WidgetWindowStateWatcher(this)), m_zoneWindowState(new WidgetWindowStateWatcher(this)), m_graphWindowState(new WidgetWindowStateWatcher(this)), m_connWindowState(new WidgetWindowStateWatcher(this)), - m_settings(fortSettings), + m_settings(settings), m_envManager(envManager), - m_quotaManager(new QuotaManager(fortSettings, this)), - m_statManager(new StatManager(fortSettings->statFilePath(), m_quotaManager, this)), + m_quotaManager(new QuotaManager(settings, this)), + m_statManager(new StatManager(settings->statFilePath(), m_quotaManager, this)), m_driverManager(new DriverManager(this)), - m_confManager(new ConfManager(fortSettings->confFilePath(), this, this)), + m_confManager(new ConfManager(settings->confFilePath(), this, this)), m_logManager(new LogManager(this, this)), m_nativeEventFilter(new NativeEventFilter(this)), m_hotKeyManager(new HotKeyManager(m_nativeEventFilter, this)), @@ -103,6 +105,8 @@ FortManager::~FortManager() closeDriver(); closeLogManager(); + + delete m_mainWindow; } FirewallConf *FortManager::conf() const @@ -555,8 +559,8 @@ bool FortManager::checkPassword() g_passwordDialogOpened = true; - const QString password = QInputDialog::getText( - &m_window, tr("Password input"), tr("Please enter the password"), QLineEdit::Password); + const QString password = QInputDialog::getText(m_mainWindow, tr("Password input"), + tr("Please enter the password"), QLineEdit::Password); g_passwordDialogOpened = false; @@ -796,7 +800,7 @@ void FortManager::createTrayMenu() { const bool hotKeyEnabled = settings()->hotKeyEnabled(); - QMenu *menu = new QMenu(&m_window); + QMenu *menu = new QMenu(m_mainWindow); m_programsAction = addAction(menu, IconCache::icon(":/icons/window.png"), QString(), this, SLOT(showProgramsWindow())); @@ -949,7 +953,7 @@ void FortManager::removeHotKeys() QWidget *FortManager::focusWidget() { auto w = QApplication::focusWidget(); - return w ? w : &m_window; + return w ? w : m_mainWindow; } QAction *FortManager::addAction(QWidget *widget, const QIcon &icon, const QString &text, diff --git a/src/ui/fortmanager.h b/src/ui/fortmanager.h index e3639cbe..222a7be1 100644 --- a/src/ui/fortmanager.h +++ b/src/ui/fortmanager.h @@ -3,9 +3,10 @@ #include -#include "form/controls/mainwindow.h" #include "util/classhelpers.h" +QT_FORWARD_DECLARE_CLASS(QAction) +QT_FORWARD_DECLARE_CLASS(QMouseEvent) QT_FORWARD_DECLARE_CLASS(QSystemTrayIcon) class AppInfoCache; @@ -22,6 +23,7 @@ class GraphWindow; class HostInfoCache; class HotKeyManager; class LogManager; +class MainWindow; class NativeEventFilter; class OptionsWindow; class ProgramsWindow; @@ -196,11 +198,12 @@ private: const QObject *receiver = nullptr, const char *member = nullptr); private: - MainWindow m_window; // dummy window for tray icon + bool m_trayTriggered = false; TrayMessageType m_lastMessageType = MessageOptions; - bool m_trayTriggered = false; + MainWindow *m_mainWindow = nullptr; // dummy window for tray icon + QSystemTrayIcon *m_trayIcon = nullptr; ProgramsWindow *m_progWindow = nullptr; diff --git a/src/ui/fortsettings.cpp b/src/ui/fortsettings.cpp index 6c487f7a..59c4519f 100644 --- a/src/ui/fortsettings.cpp +++ b/src/ui/fortsettings.cpp @@ -32,6 +32,7 @@ FortSettings::FortSettings(QObject *parent) : m_iniExists(false), m_noCache(false), m_isService(false), + m_isWindowControl(false), m_bulkUpdating(false), m_bulkIniChanged(false) { @@ -81,29 +82,31 @@ void FortSettings::processArguments(const QStringList &args, EnvManager *envMana "Directory to store settings.", "profile"); parser.addOption(profileOption); - const QCommandLineOption statOption(QStringList() << "s" - << "stat", - "Directory to store statistics.", "stat"); + const QCommandLineOption statOption("stat", "Directory to store statistics.", "stat"); parser.addOption(statOption); - const QCommandLineOption logsOption( - QStringList() << "logs", "Directory to store logs.", "logs"); + const QCommandLineOption logsOption("logs", "Directory to store logs.", "logs"); parser.addOption(logsOption); - const QCommandLineOption cacheOption( - QStringList() << "cache", "Directory to store cache.", "cache"); + const QCommandLineOption cacheOption("cache", "Directory to store cache.", "cache"); parser.addOption(cacheOption); - const QCommandLineOption noCacheOption(QStringList() << "no-cache", "Don't use cache on disk."); + const QCommandLineOption uninstallOption("u", "Uninstall booted provider and startup entries."); + parser.addOption(uninstallOption); + + const QCommandLineOption noCacheOption("no-cache", "Don't use cache on disk."); parser.addOption(noCacheOption); - const QCommandLineOption langOption(QStringList() << "lang", "Default language.", "lang", "en"); + const QCommandLineOption langOption("lang", "Default language.", "lang", "en"); parser.addOption(langOption); - const QCommandLineOption serviceOption( - QStringList() << "service", "Is running as a service?", "service"); + const QCommandLineOption serviceOption("service", "Is running as a service?"); parser.addOption(serviceOption); + const QCommandLineOption windowControlOption( + "w", "Control running instance's window by sending the command."); + parser.addOption(windowControlOption); + const QCommandLineOption controlOption(QStringList() << "c" << "control", "Control running instance by executing the command.", "control"); @@ -174,6 +177,7 @@ void FortSettings::processArguments(const QStringList &args, EnvManager *envMana } // Control command + m_isWindowControl = parser.isSet(windowControlOption); m_controlCommand = parser.value(controlOption); // Other Arguments diff --git a/src/ui/fortsettings.h b/src/ui/fortsettings.h index 8f112a4e..49a8edf1 100644 --- a/src/ui/fortsettings.h +++ b/src/ui/fortsettings.h @@ -19,6 +19,7 @@ public: bool noCache() const { return m_noCache; } bool isService() const { return m_isService; } + bool isWindowControl() const { return m_isWindowControl; } bool debug() const { return iniBool("base/debug"); } void setDebug(bool on) { setIniValue("base/debug", on); } @@ -278,6 +279,7 @@ private: bool m_iniExists : 1; bool m_noCache : 1; bool m_isService : 1; + bool m_isWindowControl : 1; bool m_bulkUpdating : 1; bool m_bulkIniChanged : 1; diff --git a/src/ui/main.cpp b/src/ui/main.cpp index d699b740..3a6b26b6 100644 --- a/src/ui/main.cpp +++ b/src/ui/main.cpp @@ -50,11 +50,11 @@ int main(int argc, char *argv[]) fortSettings.initialize(QCoreApplication::arguments(), &envManager); #ifdef USE_CONTROL_COMMANDS - ControlManager controlManager(QApplication::applicationName(), fortSettings.controlCommand()); + ControlManager controlManager(&fortSettings); // Send control request to running instance if (controlManager.isClient()) { - return controlManager.post(fortSettings.args()) ? 0 : FORT_ERROR_CONTROL; + return controlManager.post() ? 0 : FORT_ERROR_CONTROL; } #endif