UI: ControlManager: Prepare service handling.

This commit is contained in:
Nodir Temirkhodjaev 2021-03-31 13:29:19 +03:00
parent b13f4cb84c
commit 98a5bd8667
8 changed files with 69 additions and 49 deletions

View File

@ -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}"; 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: "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}"; 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] [Icons]
; Start menu shortcut ; Start menu shortcut

View File

@ -1,5 +1,6 @@
#include "controlmanager.h" #include "controlmanager.h"
#include <QApplication>
#include <QLoggingCategory> #include <QLoggingCategory>
#include <QThreadPool> #include <QThreadPool>
@ -16,13 +17,12 @@ Q_LOGGING_CATEGORY(CLOG_CONTROL_MANAGER, "control")
#define logWarning() qCWarning(CLOG_CONTROL_MANAGER, ) #define logWarning() qCWarning(CLOG_CONTROL_MANAGER, )
#define logCritical() qCCritical(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), QObject(parent),
m_isClient(!command.isEmpty()), m_settings(settings),
m_command(command), m_semaphore(QApplication::applicationName() + QLatin1String("_ControlSemaphore"), 0,
m_semaphore(globalName + QLatin1String("_ControlSemaphore"), 0,
isClient() ? QSystemSemaphore::Open : QSystemSemaphore::Create), isClient() ? QSystemSemaphore::Open : QSystemSemaphore::Create),
m_sharedMemory(globalName + QLatin1String("_ControlSharedMemory")) m_sharedMemory(QApplication::applicationName() + QLatin1String("_ControlSharedMemory"))
{ {
} }
@ -31,6 +31,11 @@ ControlManager::~ControlManager()
abort(); abort();
} }
bool ControlManager::isClient() const
{
return !settings()->controlCommand().isEmpty();
}
bool ControlManager::listen(FortManager *fortManager) bool ControlManager::listen(FortManager *fortManager)
{ {
if (m_sharedMemory.size() > 0) if (m_sharedMemory.size() > 0)
@ -50,8 +55,12 @@ bool ControlManager::listen(FortManager *fortManager)
return true; return true;
} }
bool ControlManager::post(const QStringList &args) bool ControlManager::post()
{ {
if (settings()->isWindowControl()) {
// TODO
}
if (!m_sharedMemory.attach()) { if (!m_sharedMemory.attach()) {
logWarning() << "Shared Memory attach error:" << m_sharedMemory.errorString(); logWarning() << "Shared Memory attach error:" << m_sharedMemory.errorString();
return false; return false;
@ -59,15 +68,17 @@ bool ControlManager::post(const QStringList &args)
ControlWorker worker(&m_semaphore, &m_sharedMemory); 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; QString errorMessage;
if (!processCommand(command, args, errorMessage)) { if (!processCommand(command, args, errorMessage)) {
logWarning() << "Bad control command" << errorMessage << ':' << command << args; logWarning() << "Bad control command" << errorMessage << ':' << command << args;
return false;
} }
return true;
} }
bool ControlManager::processCommand( bool ControlManager::processCommand(
@ -82,7 +93,7 @@ bool ControlManager::processCommand(
return false; return false;
} }
auto conf = m_fortManager->conf(); auto conf = fortManager()->conf();
bool onlyFlags = true; bool onlyFlags = true;
const auto confPropName = args.at(0); const auto confPropName = args.at(0);
@ -103,7 +114,7 @@ bool ControlManager::processCommand(
} }
if (ok) { if (ok) {
m_fortManager->saveOriginConf(tr("Control command executed"), onlyFlags); fortManager()->saveOriginConf(tr("Control command executed"), onlyFlags);
} }
} else if (command == "prog") { } else if (command == "prog") {
if (argsSize < 1) { if (argsSize < 1) {
@ -119,7 +130,7 @@ bool ControlManager::processCommand(
return false; return false;
} }
ok = m_fortManager->showProgramEditForm(args.at(1)); ok = fortManager()->showProgramEditForm(args.at(1));
} }
} }

View File

@ -9,28 +9,27 @@
class ControlWorker; class ControlWorker;
class FortManager; class FortManager;
class FortSettings;
class ControlManager : public QObject class ControlManager : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit ControlManager( explicit ControlManager(FortSettings *settings, QObject *parent = nullptr);
const QString &globalName, const QString &command, QObject *parent = nullptr);
~ControlManager() override; ~ControlManager() override;
CLASS_DELETE_COPY_MOVE(ControlManager) CLASS_DELETE_COPY_MOVE(ControlManager)
bool isClient() const { return m_isClient; } bool isClient() const;
bool listen(FortManager *fortManager); bool listen(FortManager *fortManager);
bool post(const QStringList &args); bool post();
signals: FortSettings *settings() const { return m_settings; }
FortManager *fortManager() const { return m_fortManager; }
public slots:
private slots: private slots:
void processRequest(const QString &command, const QStringList &args); bool processRequest(const QString &command, const QStringList &args);
private: private:
bool processCommand(const QString &command, const QStringList &args, QString &errorMessage); bool processCommand(const QString &command, const QStringList &args, QString &errorMessage);
@ -40,13 +39,10 @@ private:
void abort(); void abort();
private: private:
bool m_isClient = false; FortSettings *m_settings = nullptr;
FortManager *m_fortManager = nullptr; FortManager *m_fortManager = nullptr;
ControlWorker *m_worker = nullptr; ControlWorker *m_worker = nullptr;
QString m_command;
QSystemSemaphore m_semaphore; QSystemSemaphore m_semaphore;
QSharedMemory m_sharedMemory; QSharedMemory m_sharedMemory;
}; };

View File

@ -16,6 +16,7 @@
#include "conf/firewallconf.h" #include "conf/firewallconf.h"
#include "driver/drivermanager.h" #include "driver/drivermanager.h"
#include "form/conn/connectionswindow.h" #include "form/conn/connectionswindow.h"
#include "form/controls/mainwindow.h"
#include "form/graph/graphwindow.h" #include "form/graph/graphwindow.h"
#include "form/opt/optionswindow.h" #include "form/opt/optionswindow.h"
#include "form/prog/programswindow.h" #include "form/prog/programswindow.h"
@ -50,20 +51,21 @@
#include "util/stringutil.h" #include "util/stringutil.h"
#include "util/window/widgetwindowstatewatcher.h" #include "util/window/widgetwindowstatewatcher.h"
FortManager::FortManager(FortSettings *fortSettings, EnvManager *envManager, QObject *parent) : FortManager::FortManager(FortSettings *settings, EnvManager *envManager, QObject *parent) :
QObject(parent), QObject(parent),
m_mainWindow(new MainWindow()),
m_trayIcon(new QSystemTrayIcon(this)), m_trayIcon(new QSystemTrayIcon(this)),
m_progWindowState(new WidgetWindowStateWatcher(this)), m_progWindowState(new WidgetWindowStateWatcher(this)),
m_optWindowState(new WidgetWindowStateWatcher(this)), m_optWindowState(new WidgetWindowStateWatcher(this)),
m_zoneWindowState(new WidgetWindowStateWatcher(this)), m_zoneWindowState(new WidgetWindowStateWatcher(this)),
m_graphWindowState(new WidgetWindowStateWatcher(this)), m_graphWindowState(new WidgetWindowStateWatcher(this)),
m_connWindowState(new WidgetWindowStateWatcher(this)), m_connWindowState(new WidgetWindowStateWatcher(this)),
m_settings(fortSettings), m_settings(settings),
m_envManager(envManager), m_envManager(envManager),
m_quotaManager(new QuotaManager(fortSettings, this)), m_quotaManager(new QuotaManager(settings, this)),
m_statManager(new StatManager(fortSettings->statFilePath(), m_quotaManager, this)), m_statManager(new StatManager(settings->statFilePath(), m_quotaManager, this)),
m_driverManager(new DriverManager(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_logManager(new LogManager(this, this)),
m_nativeEventFilter(new NativeEventFilter(this)), m_nativeEventFilter(new NativeEventFilter(this)),
m_hotKeyManager(new HotKeyManager(m_nativeEventFilter, this)), m_hotKeyManager(new HotKeyManager(m_nativeEventFilter, this)),
@ -103,6 +105,8 @@ FortManager::~FortManager()
closeDriver(); closeDriver();
closeLogManager(); closeLogManager();
delete m_mainWindow;
} }
FirewallConf *FortManager::conf() const FirewallConf *FortManager::conf() const
@ -555,8 +559,8 @@ bool FortManager::checkPassword()
g_passwordDialogOpened = true; g_passwordDialogOpened = true;
const QString password = QInputDialog::getText( const QString password = QInputDialog::getText(m_mainWindow, tr("Password input"),
&m_window, tr("Password input"), tr("Please enter the password"), QLineEdit::Password); tr("Please enter the password"), QLineEdit::Password);
g_passwordDialogOpened = false; g_passwordDialogOpened = false;
@ -796,7 +800,7 @@ void FortManager::createTrayMenu()
{ {
const bool hotKeyEnabled = settings()->hotKeyEnabled(); 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, m_programsAction = addAction(menu, IconCache::icon(":/icons/window.png"), QString(), this,
SLOT(showProgramsWindow())); SLOT(showProgramsWindow()));
@ -949,7 +953,7 @@ void FortManager::removeHotKeys()
QWidget *FortManager::focusWidget() QWidget *FortManager::focusWidget()
{ {
auto w = QApplication::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, QAction *FortManager::addAction(QWidget *widget, const QIcon &icon, const QString &text,

View File

@ -3,9 +3,10 @@
#include <QObject> #include <QObject>
#include "form/controls/mainwindow.h"
#include "util/classhelpers.h" #include "util/classhelpers.h"
QT_FORWARD_DECLARE_CLASS(QAction)
QT_FORWARD_DECLARE_CLASS(QMouseEvent)
QT_FORWARD_DECLARE_CLASS(QSystemTrayIcon) QT_FORWARD_DECLARE_CLASS(QSystemTrayIcon)
class AppInfoCache; class AppInfoCache;
@ -22,6 +23,7 @@ class GraphWindow;
class HostInfoCache; class HostInfoCache;
class HotKeyManager; class HotKeyManager;
class LogManager; class LogManager;
class MainWindow;
class NativeEventFilter; class NativeEventFilter;
class OptionsWindow; class OptionsWindow;
class ProgramsWindow; class ProgramsWindow;
@ -196,11 +198,12 @@ private:
const QObject *receiver = nullptr, const char *member = nullptr); const QObject *receiver = nullptr, const char *member = nullptr);
private: private:
MainWindow m_window; // dummy window for tray icon bool m_trayTriggered = false;
TrayMessageType m_lastMessageType = MessageOptions; TrayMessageType m_lastMessageType = MessageOptions;
bool m_trayTriggered = false; MainWindow *m_mainWindow = nullptr; // dummy window for tray icon
QSystemTrayIcon *m_trayIcon = nullptr; QSystemTrayIcon *m_trayIcon = nullptr;
ProgramsWindow *m_progWindow = nullptr; ProgramsWindow *m_progWindow = nullptr;

View File

@ -32,6 +32,7 @@ FortSettings::FortSettings(QObject *parent) :
m_iniExists(false), m_iniExists(false),
m_noCache(false), m_noCache(false),
m_isService(false), m_isService(false),
m_isWindowControl(false),
m_bulkUpdating(false), m_bulkUpdating(false),
m_bulkIniChanged(false) m_bulkIniChanged(false)
{ {
@ -81,29 +82,31 @@ void FortSettings::processArguments(const QStringList &args, EnvManager *envMana
"Directory to store settings.", "profile"); "Directory to store settings.", "profile");
parser.addOption(profileOption); parser.addOption(profileOption);
const QCommandLineOption statOption(QStringList() << "s" const QCommandLineOption statOption("stat", "Directory to store statistics.", "stat");
<< "stat",
"Directory to store statistics.", "stat");
parser.addOption(statOption); parser.addOption(statOption);
const QCommandLineOption logsOption( const QCommandLineOption logsOption("logs", "Directory to store logs.", "logs");
QStringList() << "logs", "Directory to store logs.", "logs");
parser.addOption(logsOption); parser.addOption(logsOption);
const QCommandLineOption cacheOption( const QCommandLineOption cacheOption("cache", "Directory to store cache.", "cache");
QStringList() << "cache", "Directory to store cache.", "cache");
parser.addOption(cacheOption); 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); parser.addOption(noCacheOption);
const QCommandLineOption langOption(QStringList() << "lang", "Default language.", "lang", "en"); const QCommandLineOption langOption("lang", "Default language.", "lang", "en");
parser.addOption(langOption); parser.addOption(langOption);
const QCommandLineOption serviceOption( const QCommandLineOption serviceOption("service", "Is running as a service?");
QStringList() << "service", "Is running as a service?", "service");
parser.addOption(serviceOption); parser.addOption(serviceOption);
const QCommandLineOption windowControlOption(
"w", "Control running instance's window by sending the command.");
parser.addOption(windowControlOption);
const QCommandLineOption controlOption(QStringList() << "c" const QCommandLineOption controlOption(QStringList() << "c"
<< "control", << "control",
"Control running instance by executing the command.", "control"); "Control running instance by executing the command.", "control");
@ -174,6 +177,7 @@ void FortSettings::processArguments(const QStringList &args, EnvManager *envMana
} }
// Control command // Control command
m_isWindowControl = parser.isSet(windowControlOption);
m_controlCommand = parser.value(controlOption); m_controlCommand = parser.value(controlOption);
// Other Arguments // Other Arguments

View File

@ -19,6 +19,7 @@ public:
bool noCache() const { return m_noCache; } bool noCache() const { return m_noCache; }
bool isService() const { return m_isService; } bool isService() const { return m_isService; }
bool isWindowControl() const { return m_isWindowControl; }
bool debug() const { return iniBool("base/debug"); } bool debug() const { return iniBool("base/debug"); }
void setDebug(bool on) { setIniValue("base/debug", on); } void setDebug(bool on) { setIniValue("base/debug", on); }
@ -278,6 +279,7 @@ private:
bool m_iniExists : 1; bool m_iniExists : 1;
bool m_noCache : 1; bool m_noCache : 1;
bool m_isService : 1; bool m_isService : 1;
bool m_isWindowControl : 1;
bool m_bulkUpdating : 1; bool m_bulkUpdating : 1;
bool m_bulkIniChanged : 1; bool m_bulkIniChanged : 1;

View File

@ -50,11 +50,11 @@ int main(int argc, char *argv[])
fortSettings.initialize(QCoreApplication::arguments(), &envManager); fortSettings.initialize(QCoreApplication::arguments(), &envManager);
#ifdef USE_CONTROL_COMMANDS #ifdef USE_CONTROL_COMMANDS
ControlManager controlManager(QApplication::applicationName(), fortSettings.controlCommand()); ControlManager controlManager(&fortSettings);
// Send control request to running instance // Send control request to running instance
if (controlManager.isClient()) { if (controlManager.isClient()) {
return controlManager.post(fortSettings.args()) ? 0 : FORT_ERROR_CONTROL; return controlManager.post() ? 0 : FORT_ERROR_CONTROL;
} }
#endif #endif