refactor: use checkbox for elevate remove Settings::Core::ElevateMode and use Daemon::Elevate to hold the value

This commit is contained in:
sithlord48
2025-04-02 18:00:06 -04:00
committed by Chris Rizzitello
parent 5ad2c9283d
commit 6e6892b6e7
7 changed files with 47 additions and 118 deletions

View File

@ -110,12 +110,8 @@ QVariant Settings::defaultValue(const QString &key)
if (key == Server::Binary)
return kServerBinName;
if (key == Core::ElevateMode) {
if (instance()->isNativeMode())
return Settings::ElevateMode::Always;
else
return Settings::ElevateMode::Never;
}
if (key == Daemon::Elevate)
return instance()->isNativeMode();
if (key == Core::UpdateUrl)
return kUrlUpdateCheck;
@ -141,9 +137,6 @@ QVariant Settings::defaultValue(const QString &key)
#endif
}
if (key == Daemon::Elevate)
return true;
return QVariant();
}

View File

@ -41,7 +41,6 @@ public:
struct Core
{
inline static const auto CoreMode = QStringLiteral("core/coreMode");
inline static const auto ElevateMode = QStringLiteral("core/elevateMode");
inline static const auto Interface = QStringLiteral("core/interface");
inline static const auto LastVersion = QStringLiteral("core/lastVersion");
inline static const auto Port = QStringLiteral("core/port");
@ -104,28 +103,6 @@ public:
};
Q_ENUM(ProcessMode)
/**
* @brief The elevate mode tristate determines two behaviors on Windows.
* The matrix for these two behaviors is as follows:
* | sods | elevate |
* |-----------|------------|
* kAutomatic | true | false |
* kAlways | false | true |
* kNever | false | false |
* The first, --stop-on-desk-switch (sods), is passed through the daemon as a
* command line argument to the server/client, and determines if it restarts
* when switching Windows desktops (e.g. when Windows UAC dialog pops up).
* The second, elevate, is passed as a boolean flag to the daemon over IPC,
* and determines whether the server/client should be started with elevated privileges.
*/
enum ElevateMode
{
Automatic = 0,
Always = 1,
Never = 2
};
Q_ENUM(ElevateMode)
enum CoreMode
{
None,
@ -182,7 +159,6 @@ private:
, Settings::Client::LanguageSync
, Settings::Client::RemoteHost
, Settings::Core::CoreMode
, Settings::Core::ElevateMode
, Settings::Core::Interface
, Settings::Core::LastVersion
, Settings::Core::Port

View File

@ -264,8 +264,7 @@ void CoreProcess::startProcessFromDaemon(const QString &app, const QStringList &
qInfo("running command: %s", qPrintable(commandQuoted));
auto elevateMode = Settings::value(Settings::Core::ElevateMode).value<Settings::ElevateMode>();
if (!m_daemonIpcClient->sendStartProcess(commandQuoted, elevateMode)) {
if (!m_daemonIpcClient->sendStartProcess(commandQuoted, Settings::value(Settings::Daemon::Elevate).toBool())) {
qCritical("cannot start process, ipc command failed");
return;
}

View File

@ -68,6 +68,25 @@ void SettingsDialog::initConnections()
connect(ui->btnTlsCertPath, &QPushButton::clicked, this, &SettingsDialog::browseCertificatePath);
connect(ui->btnBrowseLog, &QPushButton::clicked, this, &SettingsDialog::browseLogPath);
connect(ui->cbLogToFile, &QCheckBox::toggled, this, &SettingsDialog::setLogToFile);
connect(ui->cbElevateDaemon, &QCheckBox::toggled, this, [&](bool checked) {
if (!checked)
return;
if (ui->cbStopOnDeskSwitch->isChecked()) {
blockSignals(true);
ui->cbStopOnDeskSwitch->setChecked(false);
blockSignals(false);
}
});
connect(ui->cbStopOnDeskSwitch, &QCheckBox::toggled, this, [&](bool checked) {
if (!checked)
return;
if (ui->cbElevateDaemon->isChecked()) {
blockSignals(true);
ui->cbElevateDaemon->setChecked(false);
blockSignals(false);
}
});
}
void SettingsDialog::regenCertificates()
@ -133,7 +152,7 @@ void SettingsDialog::accept()
Settings::setValue(Settings::Log::ToFile, ui->cbLogToFile->isChecked());
Settings::setValue(Settings::Log::File, ui->lineLogFilename->text());
Settings::setValue(Settings::Core::StopOnDeskSwitch, ui->cbStopOnDeskSwitch->isChecked());
Settings::setValue(Settings::Core::ElevateMode, ui->comboElevate->currentIndex());
Settings::setValue(Settings::Daemon::Elevate, ui->cbElevateDaemon->isChecked());
Settings::setValue(Settings::Gui::Autohide, ui->cbAutoHide->isChecked());
Settings::setValue(Settings::Gui::AutoUpdateCheck, ui->cbAutoUpdate->isChecked());
Settings::setValue(Settings::Core::PreventSleep, ui->cbPreventSleep->isChecked());
@ -169,7 +188,7 @@ void SettingsDialog::loadFromConfig()
ui->cbScrollDirection->setChecked(Settings::value(Settings::Client::InvertScrollDirection).toBool());
ui->cbCloseToTray->setChecked(Settings::value(Settings::Gui::CloseToTray).toBool());
ui->cbStopOnDeskSwitch->setChecked(Settings::value(Settings::Core::StopOnDeskSwitch).toBool());
ui->comboElevate->setCurrentIndex(Settings::value(Settings::Core::ElevateMode).toInt());
ui->cbElevateDaemon->setChecked(Settings::value(Settings::Daemon::Elevate).toBool());
ui->cbAutoUpdate->setChecked(Settings::value(Settings::Gui::AutoUpdateCheck).toBool());
const auto processMode = Settings::value(Settings::Core::ProcessMode).value<Settings::ProcessMode>();
@ -268,7 +287,7 @@ void SettingsDialog::updateControls()
// Handle enable and disable of service items
if (Settings::isNativeMode()) {
ui->groupService->setEnabled(writable);
ui->widgetElevate->setEnabled(writable && serviceChecked);
ui->cbElevateDaemon->setEnabled(writable && serviceChecked);
ui->cbStopOnDeskSwitch->setEnabled(writable && serviceChecked);
} else if (ui->groupService->isVisibleTo(ui->tabAdvanced)) {
ui->groupService->setVisible(false);

View File

@ -147,6 +147,9 @@
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>false</bool>
</property>
<layout class="QVBoxLayout" name="_11">
<item>
<widget class="QWidget" name="widgetTlsCert" native="true">
@ -538,6 +541,9 @@
</item>
<item>
<widget class="QGroupBox" name="groupService">
<property name="enabled">
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
@ -554,81 +560,6 @@
<bool>false</bool>
</property>
<layout class="QGridLayout" name="_6">
<item row="1" column="0">
<widget class="QWidget" name="widgetElevate" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QHBoxLayout" name="_7">
<item>
<widget class="QLabel" name="lblElevate">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Launch with elevated privileges</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboElevate">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="toolTip">
<string>Specify when the Windows background service should run the server or client
process at an elevated privilege level.</string>
</property>
<property name="whatsThis">
<string>
&lt;p&gt;You may want to alter whether or not the privilege level of the server or client
process is automatically changed depending on your use case. In some cases it can help
to diagnose or solve some problems related to elevated processes in Windows.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Automatic: Elevate when the window session changes to secure mode&lt;/li&gt;
&lt;li&gt;Always elevate: Always run in elevated mode (could be unsafe)&lt;/li&gt;
&lt;li&gt;Never elevate: Turn off compatibility with login screen and UAC&lt;/li&gt;
&lt;/ul&gt;
</string>
</property>
<property name="currentIndex">
<number>0</number>
</property>
<item>
<property name="text">
<string>Automatic (as needed)</string>
</property>
</item>
<item>
<property name="text">
<string>Always elevate</string>
</property>
</item>
<item>
<property name="text">
<string>Never elevate</string>
</property>
</item>
</widget>
</item>
</layout>
</widget>
</item>
<item row="2" column="0">
<widget class="QCheckBox" name="cbStopOnDeskSwitch">
<property name="toolTip">
@ -637,6 +568,19 @@
<property name="text">
<string>Run as system only when at the login screen and UAC prompt</string>
</property>
<property name="autoExclusive">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="cbElevateDaemon">
<property name="text">
<string>Always run as system (work at login screen and UAC)</string>
</property>
<property name="autoExclusive">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
@ -680,7 +624,6 @@
<tabstop>comboLogLevel</tabstop>
<tabstop>lineLogFilename</tabstop>
<tabstop>btnBrowseLog</tabstop>
<tabstop>comboElevate</tabstop>
</tabstops>
<resources/>
<connections/>

View File

@ -132,13 +132,12 @@ bool DaemonIpcClient::sendLogLevel(const QString &logLevel)
return true;
}
bool DaemonIpcClient::sendStartProcess(const QString &command, Settings::ElevateMode elevateMode)
bool DaemonIpcClient::sendStartProcess(const QString &command, bool elevate)
{
if (!keepAlive())
return false;
using ElevateMode = Settings::ElevateMode;
if (!sendMessage("elevate=" + (elevateMode == ElevateMode::Always ? QStringLiteral("yes") : QStringLiteral("no")))) {
if (!sendMessage("elevate=" + (elevate ? QStringLiteral("yes") : QStringLiteral("no")))) {
return false;
}

View File

@ -22,7 +22,7 @@ public:
explicit DaemonIpcClient(QObject *parent = nullptr);
bool connectToServer();
bool sendLogLevel(const QString &logLevel);
bool sendStartProcess(const QString &command, Settings::ElevateMode elevateMode);
bool sendStartProcess(const QString &command, bool elevate);
bool sendStopProcess();
bool sendClearSettings();
QString requestLogPath();