move check_privilege.* to main

This commit is contained in:
AdenKoperczak 2025-01-11 13:46:32 -05:00
parent 4a4075b50f
commit 2f9908b54e
4 changed files with 12 additions and 12 deletions

View file

@ -0,0 +1,135 @@
#include "scwx/qt/settings/general_settings.hpp"
#include "scwx/qt/main/check_privilege.hpp"
#include <QtGlobal>
#include <QStandardPaths>
#include <filesystem>
#include <QObject>
#include <QMessageBox>
#include <QCheckBox>
#ifdef _WIN32
# include <windows.h>
#else
# include <unistd.h>
#endif
namespace scwx::qt::main
{
bool is_high_privilege()
{
#if defined(_WIN32)
bool isAdmin = false;
HANDLE token = NULL;
TOKEN_ELEVATION elevation;
DWORD elevationSize = sizeof(TOKEN_ELEVATION);
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token))
{
return false;
}
if (!GetTokenInformation(
token, TokenElevation, &elevation, elevationSize, &elevationSize))
{
CloseHandle(token);
return false;
}
isAdmin = elevation.TokenIsElevated;
CloseHandle(token);
return isAdmin;
#elif defined(Q_OS_UNIX)
// On UNIX root is always uid 0. On Linux this is enforced by the kernel.
return geteuid() == 0;
#else
return false;
#endif
}
#if defined(_WIN32)
static const QString message = QObject::tr(
"Supercell Wx has been run with administrator permissions. It is "
"recommended to run it without administrator permissions Do you wish to "
"continue?");
#elif defined(Q_OS_UNIX)
static const QString message = QObject::tr(
"Supercell Wx has been run as root. It is recommended to run it as a normal "
"user. Do you wish to continue?");
#else
static const QString message = QObject::tr("");
#endif
static const QString title = QObject::tr("Supercell Wx");
static const QString checkBoxText =
QObject::tr("Do not show this warning again.");
class PrivilegeChecker::Impl
{
public:
explicit Impl() :
highPrivilege_ {is_high_privilege()},
dialog_ {QMessageBox::Icon::Warning, title, message},
checkBox_(new QCheckBox(checkBoxText, &dialog_))
{
const std::string appDataPath {
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)
.toStdString()};
hasAppData_ = std::filesystem::exists(appDataPath);
dialog_.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
dialog_.setDefaultButton(QMessageBox::No);
dialog_.setCheckBox(checkBox_);
};
bool hasAppData_;
bool firstCheckCheckBoxState_ {true};
bool highPrivilege_;
QMessageBox dialog_;
QCheckBox* checkBox_;
};
PrivilegeChecker::PrivilegeChecker() : p(std::make_unique<PrivilegeChecker::Impl>())
{
}
PrivilegeChecker::~PrivilegeChecker() = default;
bool PrivilegeChecker::first_check()
{
if (p->hasAppData_ || !p->highPrivilege_)
{
return false;
}
int result = p->dialog_.exec();
p->firstCheckCheckBoxState_ = p->checkBox_->isChecked();
return result != QMessageBox::Yes;
}
bool PrivilegeChecker::second_check()
{
auto& highPrivilegeWarningEnabled =
settings::GeneralSettings::Instance().high_privilege_warning_enabled();
if (!highPrivilegeWarningEnabled.GetValue() || !p->highPrivilege_)
{
return false;
}
else if (!p->hasAppData_)
{
highPrivilegeWarningEnabled.StageValue(!p->firstCheckCheckBoxState_);
return false;
}
switch (p->dialog_.exec())
{
case QMessageBox::Yes:
highPrivilegeWarningEnabled.StageValue(!p->checkBox_->isChecked());
return false;
case QMessageBox::No:
default:
return true;
}
}
} // namespace scwx::qt::main

View file

@ -0,0 +1,25 @@
#pragma once
#include <memory>
namespace scwx::qt::main
{
bool is_high_privilege();
class PrivilegeChecker
{
public:
explicit PrivilegeChecker();
~PrivilegeChecker();
// returning true means check failed.
bool first_check();
bool second_check();
private:
class Impl;
std::unique_ptr<Impl> p;
};
} // namespace scwx::qt::main

View file

@ -14,7 +14,7 @@
#include <scwx/qt/types/qt_types.hpp>
#include <scwx/qt/ui/high_privilege_dialog.hpp>
#include <scwx/qt/ui/setup/setup_wizard.hpp>
#include <scwx/qt/util/check_privilege.hpp>
#include <scwx/qt/main/check_privilege.hpp>
#include <scwx/network/cpr.hpp>
#include <scwx/util/environment.hpp>
#include <scwx/util/logger.hpp>
@ -22,7 +22,6 @@
#include <string>
#include <vector>
#include <filesystem>
#include <aws/core/Aws.h>
#include <boost/asio.hpp>
@ -82,7 +81,7 @@ int main(int argc, char* argv[])
}
// Test to see if scwx was run with high privilege
scwx::qt::util::PrivilegeChecker privilegeChecker;
scwx::qt::main::PrivilegeChecker privilegeChecker;
if (privilegeChecker.first_check())
{
return 0;