mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 01:20:06 +00:00 
			
		
		
		
	Move dialog logic into check_privilege
This commit is contained in:
		
							parent
							
								
									6408e1b876
								
							
						
					
					
						commit
						4a4075b50f
					
				
					 4 changed files with 136 additions and 89 deletions
				
			
		|  | @ -46,9 +46,6 @@ static void OverrideDefaultStyle(const std::vector<std::string>& args); | ||||||
| 
 | 
 | ||||||
| int main(int argc, char* argv[]) | int main(int argc, char* argv[]) | ||||||
| { | { | ||||||
|    bool disableHighPrivilegeWarning = false; |  | ||||||
|    bool highPrivilegeChecked        = false; |  | ||||||
| 
 |  | ||||||
|    // Store arguments
 |    // Store arguments
 | ||||||
|    std::vector<std::string> args {}; |    std::vector<std::string> args {}; | ||||||
|    for (int i = 0; i < argc; ++i) |    for (int i = 0; i < argc; ++i) | ||||||
|  | @ -85,28 +82,11 @@ int main(int argc, char* argv[]) | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    // Test to see if scwx was run with high privilege
 |    // Test to see if scwx was run with high privilege
 | ||||||
|    const std::string appDataPath { |    scwx::qt::util::PrivilegeChecker privilegeChecker; | ||||||
|       QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) |    if (privilegeChecker.first_check()) | ||||||
|          .toStdString()}; |  | ||||||
| 
 |  | ||||||
|    // Check if high privilege before writing settings, assuming no settings
 |  | ||||||
|    // have been written
 |  | ||||||
|    if (!std::filesystem::exists(appDataPath) && |  | ||||||
|        scwx::qt::util::is_high_privilege()) |  | ||||||
|    { |    { | ||||||
|       auto dialog = |  | ||||||
|          scwx::qt::ui::HighPrivilegeDialog(); // TODO does this need cleaned up?
 |  | ||||||
|       const int result = dialog.exec(); |  | ||||||
| 
 |  | ||||||
|       disableHighPrivilegeWarning = dialog.disable_high_privilege_message(); |  | ||||||
|       highPrivilegeChecked        = true; |  | ||||||
| 
 |  | ||||||
|       if (result == QDialog::Rejected) |  | ||||||
|       { |  | ||||||
|          // TODO any other cleanup needed here?
 |  | ||||||
|       return 0; |       return 0; | ||||||
|    } |    } | ||||||
|    } |  | ||||||
| 
 | 
 | ||||||
|    // Start the io_context main loop
 |    // Start the io_context main loop
 | ||||||
|    boost::asio::io_context& ioContext = scwx::util::io_context(); |    boost::asio::io_context& ioContext = scwx::util::io_context(); | ||||||
|  | @ -146,47 +126,14 @@ int main(int argc, char* argv[]) | ||||||
| 
 | 
 | ||||||
|    // Check process modules for compatibility
 |    // Check process modules for compatibility
 | ||||||
|    scwx::qt::main::CheckProcessModules(); |    scwx::qt::main::CheckProcessModules(); | ||||||
|    auto& generalSettings = scwx::qt::settings::GeneralSettings::Instance(); |  | ||||||
| 
 | 
 | ||||||
|    if (!highPrivilegeChecked && |    int result = 0; | ||||||
|        generalSettings.high_privilege_warning_enabled().GetValue() && |    if (privilegeChecker.second_check()) | ||||||
|        scwx::qt::util::is_high_privilege()) |  | ||||||
|    { |    { | ||||||
|       auto dialog = |       result = 1; | ||||||
|          scwx::qt::ui::HighPrivilegeDialog(); // TODO does this need cleaned up?
 |    } | ||||||
|       const int result = dialog.exec(); |    else | ||||||
| 
 |  | ||||||
|       disableHighPrivilegeWarning = dialog.disable_high_privilege_message(); |  | ||||||
| 
 |  | ||||||
|       if (result == QDialog::Rejected) |  | ||||||
|    { |    { | ||||||
|          // Deinitialize application
 |  | ||||||
|          scwx::qt::manager::RadarProductManager::Cleanup(); |  | ||||||
| 
 |  | ||||||
|          // Stop Qt Threads
 |  | ||||||
|          scwx::qt::manager::ThreadManager::Instance().StopThreads(); |  | ||||||
| 
 |  | ||||||
|          // Gracefully stop the io_context main loop
 |  | ||||||
|          work.reset(); |  | ||||||
|          threadPool.join(); |  | ||||||
| 
 |  | ||||||
|          // Shutdown application
 |  | ||||||
|          scwx::qt::manager::ResourceManager::Shutdown(); |  | ||||||
|          scwx::qt::manager::SettingsManager::Instance().Shutdown(); |  | ||||||
| 
 |  | ||||||
|          // Shutdown AWS SDK
 |  | ||||||
|          Aws::ShutdownAPI(awsSdkOptions); |  | ||||||
|          return 0; |  | ||||||
|       } |  | ||||||
|    } |  | ||||||
| 
 |  | ||||||
|    // Save high privilege settings
 |  | ||||||
|    if (disableHighPrivilegeWarning) |  | ||||||
|    { |  | ||||||
|       generalSettings.high_privilege_warning_enabled().SetValue(false); |  | ||||||
|       scwx::qt::manager::SettingsManager::Instance().SaveSettings(); |  | ||||||
|    } |  | ||||||
| 
 |  | ||||||
|       // Run initial setup if required
 |       // Run initial setup if required
 | ||||||
|       if (scwx::qt::ui::setup::SetupWizard::IsSetupRequired()) |       if (scwx::qt::ui::setup::SetupWizard::IsSetupRequired()) | ||||||
|       { |       { | ||||||
|  | @ -196,12 +143,12 @@ int main(int argc, char* argv[]) | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       // Run Qt main loop
 |       // Run Qt main loop
 | ||||||
|    int result; |  | ||||||
|       { |       { | ||||||
|          scwx::qt::main::MainWindow w; |          scwx::qt::main::MainWindow w; | ||||||
|          w.show(); |          w.show(); | ||||||
|          result = a.exec(); |          result = a.exec(); | ||||||
|       } |       } | ||||||
|  |    } | ||||||
| 
 | 
 | ||||||
|    // Deinitialize application
 |    // Deinitialize application
 | ||||||
|    scwx::qt::manager::RadarProductManager::Cleanup(); |    scwx::qt::manager::RadarProductManager::Cleanup(); | ||||||
|  |  | ||||||
|  | @ -394,6 +394,7 @@ bool GeneralSettings::Shutdown() | ||||||
|    dataChanged |= p->loopTime_.Commit(); |    dataChanged |= p->loopTime_.Commit(); | ||||||
|    dataChanged |= p->processModuleWarningsEnabled_.Commit(); |    dataChanged |= p->processModuleWarningsEnabled_.Commit(); | ||||||
|    dataChanged |= p->trackLocation_.Commit(); |    dataChanged |= p->trackLocation_.Commit(); | ||||||
|  |    dataChanged |= p->highPrivilegeWarningEnabled_.Commit(); | ||||||
| 
 | 
 | ||||||
|    return dataChanged; |    return dataChanged; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,5 +1,11 @@ | ||||||
|  | #include "scwx/qt/settings/general_settings.hpp" | ||||||
| #include "scwx/qt/util/check_privilege.hpp" | #include "scwx/qt/util/check_privilege.hpp" | ||||||
| #include <QtGlobal> | #include <QtGlobal> | ||||||
|  | #include <QStandardPaths> | ||||||
|  | #include <filesystem> | ||||||
|  | #include <QObject> | ||||||
|  | #include <QMessageBox> | ||||||
|  | #include <QCheckBox> | ||||||
| 
 | 
 | ||||||
| #ifdef _WIN32 | #ifdef _WIN32 | ||||||
| #   include <windows.h> | #   include <windows.h> | ||||||
|  | @ -7,11 +13,7 @@ | ||||||
| #   include <unistd.h> | #   include <unistd.h> | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
| namespace scwx | namespace scwx::qt::util | ||||||
| { |  | ||||||
| namespace qt |  | ||||||
| { |  | ||||||
| namespace util |  | ||||||
| { | { | ||||||
| 
 | 
 | ||||||
| bool is_high_privilege() | bool is_high_privilege() | ||||||
|  | @ -38,9 +40,95 @@ bool is_high_privilege() | ||||||
| #elif defined(Q_OS_UNIX) | #elif defined(Q_OS_UNIX) | ||||||
|    // On UNIX root is always uid 0. On Linux this is enforced by the kernel.
 |    // On UNIX root is always uid 0. On Linux this is enforced by the kernel.
 | ||||||
|    return geteuid() == 0; |    return geteuid() == 0; | ||||||
|  | #else | ||||||
|  |    return false; | ||||||
| #endif | #endif | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| } // namespace util
 | #if defined(_WIN32) | ||||||
| } // namespace qt
 | static const QString message = QObject::tr( | ||||||
| } // namespace scwx
 |    "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_.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::util
 | ||||||
|  |  | ||||||
|  | @ -1,14 +1,25 @@ | ||||||
| #pragma once | #pragma once | ||||||
| 
 | 
 | ||||||
| namespace scwx | #include <memory> | ||||||
| { | 
 | ||||||
| namespace qt | namespace scwx::qt::util | ||||||
| { |  | ||||||
| namespace util |  | ||||||
| { | { | ||||||
| 
 | 
 | ||||||
| bool is_high_privilege(); | bool is_high_privilege(); | ||||||
| 
 | 
 | ||||||
| } // namespace util
 | class PrivilegeChecker | ||||||
| } // namespace qt
 | { | ||||||
| } // namespace scwx
 | 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::util
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 AdenKoperczak
						AdenKoperczak