diff options
author | Saturneric <[email protected]> | 2023-04-05 08:48:32 +0000 |
---|---|---|
committer | Saturneric <[email protected]> | 2023-04-05 08:48:32 +0000 |
commit | cd24624e94551193f9e541e69642e84e89999f78 (patch) | |
tree | 2595a7bf86231991bbf1ea2104aad816286a2365 /src | |
parent | fix: solve custom gpgconf path wrong issue (diff) | |
download | GpgFrontend-cd24624e94551193f9e541e69642e84e89999f78.tar.gz GpgFrontend-cd24624e94551193f9e541e69642e84e89999f78.zip |
feat: add check for key db and improve ui
Diffstat (limited to 'src')
-rw-r--r-- | src/core/GpgCoreInit.cpp | 13 | ||||
-rw-r--r-- | src/ui/dialog/gnupg/GnuPGControllerDialog.cpp | 145 | ||||
-rw-r--r-- | src/ui/dialog/gnupg/GnuPGControllerDialog.h | 5 |
3 files changed, 101 insertions, 62 deletions
diff --git a/src/core/GpgCoreInit.cpp b/src/core/GpgCoreInit.cpp index 243dfb5e..6d782439 100644 --- a/src/core/GpgCoreInit.cpp +++ b/src/core/GpgCoreInit.cpp @@ -163,6 +163,19 @@ void init_gpgfrontend_core() { custom_gnupg_install_fs_path.u8string()); } + // check key database path + std::filesystem::path custom_key_database_fs_path = custom_key_database_path; + if (!custom_key_database_fs_path.is_absolute() || + !std::filesystem::exists(custom_key_database_fs_path) || + !std::filesystem::is_directory(custom_key_database_fs_path)) { + use_custom_key_database_path = false; + SPDLOG_ERROR("core loaded custom gpg key database is illegal: {}", + custom_key_database_fs_path.u8string()); + } else { + SPDLOG_DEBUG("core loaded custom gpg key database path: {}", + custom_key_database_fs_path.u8string()); + } + // init default channel auto& default_ctx = GpgFrontend::GpgContext::CreateInstance( GPGFRONTEND_DEFAULT_CHANNEL, [=]() -> std::unique_ptr<ChannelObject> { diff --git a/src/ui/dialog/gnupg/GnuPGControllerDialog.cpp b/src/ui/dialog/gnupg/GnuPGControllerDialog.cpp index 91f637d4..efc7be5e 100644 --- a/src/ui/dialog/gnupg/GnuPGControllerDialog.cpp +++ b/src/ui/dialog/gnupg/GnuPGControllerDialog.cpp @@ -98,17 +98,8 @@ GnuPGControllerDialog::GnuPGControllerDialog(QWidget* parent) SPDLOG_DEBUG("key databse path selected: {}", selected_custom_key_database_path.toStdString()); - if (selected_custom_key_database_path.isEmpty()) { - QMessageBox::critical(this, _("Illegal GnuPG Key Database Path"), - _("Target Path is empty.")); - return; - } - - QFileInfo dir_info(selected_custom_key_database_path); - if (!dir_info.exists() || !dir_info.isReadable() || !dir_info.isDir()) { - QMessageBox::critical( - this, _("Illegal GnuPG Key Database Path"), - _("Target Path is not an exists readable directory.")); + if (!check_custom_gnupg_key_database_path( + selected_custom_key_database_path.toStdString())) { return; } @@ -143,37 +134,9 @@ GnuPGControllerDialog::GnuPGControllerDialog(QWidget* parent) SPDLOG_DEBUG("gnupg install path selected: {}", selected_custom_gnupg_install_path.toStdString()); - if (selected_custom_gnupg_install_path.isEmpty()) { - QMessageBox::critical(this, _("Illegal GnuPG Path"), - _("Target Path is empty.")); - return; - } - - QFileInfo dir_info(selected_custom_gnupg_install_path); - if (!dir_info.exists() || !dir_info.isReadable() || !dir_info.isDir()) { - QMessageBox::critical( - this, _("Illegal GnuPG Path"), - _("Target Path is not an exists readable directory.")); - return; - } - - QDir dir(selected_custom_gnupg_install_path); - if (!dir.isAbsolute()) { - QMessageBox::critical(this, _("Illegal GnuPG Path"), - _("Target Path is not an absolute path.")); - } -#ifdef WINDOWS - QFileInfo gpgconf_info(selected_custom_gnupg_install_path + - "/gpgconf.exe"); -#else - QFileInfo gpgconf_info(selected_custom_gnupg_install_path + "/gpgconf"); -#endif - - if (!gpgconf_info.exists() || !gpgconf_info.isExecutable() || - !gpgconf_info.isFile()) { - QMessageBox::critical( - this, _("Illegal GnuPG Path"), - _("Target Path contains no \"gpgconf\" executable.")); + // notify the user and precheck + if (!check_custom_gnupg_path( + selected_custom_gnupg_install_path.toStdString())) { return; } @@ -240,6 +203,9 @@ void GnuPGControllerDialog::SlotAccept() { void GnuPGControllerDialog::slot_update_custom_key_database_path_label( int state) { + // announce the restart + this->slot_set_restart_needed(DEEP_RESTART_CODE); + if (state != Qt::CheckState::Checked) { ui_->currentKeyDatabasePathLabel->setText(QString::fromStdString( GpgContext::GetInstance().GetInfo(false).DatabasePath)); @@ -248,20 +214,16 @@ void GnuPGControllerDialog::slot_update_custom_key_database_path_label( this->ui_->currentKeyDatabasePathLabel->setHidden(true); } else { // read from settings file - std::string custom_key_database_path; - try { - auto& settings = - GpgFrontend::GlobalSettingStation::GetInstance().GetUISettings(); - custom_key_database_path = static_cast<std::string>( - settings.lookup("general.custom_key_database_path")); - - } catch (...) { - SPDLOG_ERROR("setting operation error: custom_key_database_path"); - } + std::string custom_key_database_path = + GlobalSettingStation::GetInstance().LookupSettings( + "general.custom_key_database_path", std::string{}); SPDLOG_DEBUG("selected_custom_key_database_path from settings: {}", custom_key_database_path); + // notify the user + check_custom_gnupg_key_database_path(custom_key_database_path); + // set label value if (!custom_key_database_path.empty()) { ui_->currentKeyDatabasePathLabel->setText( @@ -275,6 +237,9 @@ void GnuPGControllerDialog::slot_update_custom_key_database_path_label( void GnuPGControllerDialog::slot_update_custom_gnupg_install_path_label( int state) { + // announce the restart + this->slot_set_restart_needed(DEEP_RESTART_CODE); + if (state != Qt::CheckState::Checked) { ui_->currentCustomGnuPGInstallPathLabel->setText(QString::fromStdString( GpgContext::GetInstance().GetInfo(false).GnuPGHomePath)); @@ -283,20 +248,16 @@ void GnuPGControllerDialog::slot_update_custom_gnupg_install_path_label( this->ui_->currentCustomGnuPGInstallPathLabel->setHidden(true); } else { // read from settings file - std::string custom_gnupg_install_path; - try { - auto& settings = - GpgFrontend::GlobalSettingStation::GetInstance().GetUISettings(); - custom_gnupg_install_path = static_cast<std::string>( - settings.lookup("general.custom_gnupg_install_path")); - - } catch (...) { - SPDLOG_ERROR("setting operation error: custom_gnupg_install_path"); - } + std::string custom_gnupg_install_path = + GlobalSettingStation::GetInstance().LookupSettings( + "general.custom_gnupg_install_path", std::string{}); SPDLOG_DEBUG("custom_gnupg_install_path from settings: {}", custom_gnupg_install_path); + // notify the user + check_custom_gnupg_path(custom_gnupg_install_path); + // set label value if (!custom_gnupg_install_path.empty()) { ui_->currentCustomGnuPGInstallPathLabel->setText( @@ -411,4 +372,64 @@ void GnuPGControllerDialog::slot_set_restart_needed(int mode) { this->restart_needed_ = mode; } +bool GnuPGControllerDialog::check_custom_gnupg_path(std::string path) { + QString path_qstr = QString::fromStdString(path); + + if (path_qstr.isEmpty()) { + QMessageBox::critical(this, _("Illegal GnuPG Path"), + _("Target GnuPG Path is empty.")); + return false; + } + + QFileInfo dir_info(path_qstr); + if (!dir_info.exists() || !dir_info.isReadable() || !dir_info.isDir()) { + QMessageBox::critical( + this, _("Illegal GnuPG Path"), + _("Target GnuPG Path is not an exists readable directory.")); + return false; + } + + QDir dir(path_qstr); + if (!dir.isAbsolute()) { + QMessageBox::critical(this, _("Illegal GnuPG Path"), + _("Target GnuPG Path is not an absolute path.")); + } +#ifdef WINDOWS + QFileInfo gpgconf_info(selected_custom_gnupg_install_path + "/gpgconf.exe"); +#else + QFileInfo gpgconf_info(path_qstr + "/gpgconf"); +#endif + + if (!gpgconf_info.exists() || !gpgconf_info.isExecutable() || + !gpgconf_info.isFile()) { + QMessageBox::critical( + this, _("Illegal GnuPG Path"), + _("Target GnuPG Path contains no \"gpgconf\" executable.")); + return false; + } + + return true; +} + +bool GnuPGControllerDialog::check_custom_gnupg_key_database_path( + std::string path) { + QString selected_custom_key_database_path = QString::fromStdString(path); + + if (selected_custom_key_database_path.isEmpty()) { + QMessageBox::critical(this, _("Illegal GnuPG Key Database Path"), + _("Target GnuPG Key Database Path is empty.")); + return false; + } + + QFileInfo dir_info(selected_custom_key_database_path); + if (!dir_info.exists() || !dir_info.isReadable() || !dir_info.isDir()) { + QMessageBox::critical(this, _("Illegal GnuPG Key Database Path"), + _("Target GnuPG Key Database Path is not an " + "exists readable directory.")); + return false; + } + + return true; +} + } // namespace GpgFrontend::UI diff --git a/src/ui/dialog/gnupg/GnuPGControllerDialog.h b/src/ui/dialog/gnupg/GnuPGControllerDialog.h index 497f293a..9d53cf46 100644 --- a/src/ui/dialog/gnupg/GnuPGControllerDialog.h +++ b/src/ui/dialog/gnupg/GnuPGControllerDialog.h @@ -29,6 +29,7 @@ #ifndef GPGFRONTEND_GNUPGCONTROLLERDIALOGLOG_H #define GPGFRONTEND_GNUPGCONTROLLERDIALOGLOG_H +#include <string> #include "ui/GpgFrontendUI.h" #include "ui/dialog/GeneralDialog.h" @@ -98,6 +99,10 @@ class GnuPGControllerDialog : public GeneralDialog { void set_settings(); void apply_settings(); + + bool check_custom_gnupg_path(std::string); + + bool check_custom_gnupg_key_database_path(std::string); }; } // namespace GpgFrontend::UI |