diff options
author | saturneric <[email protected]> | 2023-12-04 13:43:03 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2023-12-04 13:43:33 +0000 |
commit | 33544f343df4e834730d9b0c0916433a51738d55 (patch) | |
tree | fdab9c9971e041aacf9ddf4269c3def512c0f627 | |
parent | fix: common utils should be created before core initialization (diff) | |
download | GpgFrontend-33544f343df4e834730d9b0c0916433a51738d55.tar.gz GpgFrontend-33544f343df4e834730d9b0c0916433a51738d55.zip |
feat: add reasons explanation to env loading failed message box
-rw-r--r-- | src/core/GpgCoreInit.cpp | 11 | ||||
-rw-r--r-- | src/core/function/CoreSignalStation.h | 2 | ||||
-rw-r--r-- | src/core/function/gpg/GpgKeyGetter.cpp | 7 | ||||
-rw-r--r-- | src/core/function/gpg/GpgKeyGetter.h | 2 | ||||
-rw-r--r-- | src/core/thread/CtxCheckTask.cpp | 60 | ||||
-rw-r--r-- | src/core/thread/CtxCheckTask.h | 62 | ||||
-rw-r--r-- | src/ui/GpgFrontendUIInit.cpp | 22 | ||||
-rw-r--r-- | src/ui/UserInterfaceUtils.cpp | 71 | ||||
-rw-r--r-- | src/ui/UserInterfaceUtils.h | 2 |
9 files changed, 64 insertions, 175 deletions
diff --git a/src/core/GpgCoreInit.cpp b/src/core/GpgCoreInit.cpp index dcd0dcfb..6d96a50d 100644 --- a/src/core/GpgCoreInit.cpp +++ b/src/core/GpgCoreInit.cpp @@ -221,7 +221,8 @@ void InitGpgFrontendCore() { // initialize library gpgme if (!InitGpgME()) { - CoreSignalStation::GetInstance()->SignalBadGnupgEnv(); + CoreSignalStation::GetInstance()->SignalBadGnupgEnv( + _("GpgME inilization failed")); return; } @@ -330,7 +331,8 @@ void InitGpgFrontendCore() { // exit if failed if (!ctx.Good()) { SPDLOG_ERROR("default gnupg context init error, abort"); - CoreSignalStation::GetInstance()->SignalBadGnupgEnv(); + CoreSignalStation::GetInstance()->SignalBadGnupgEnv( + _("GpgME Context inilization failed")); return -1; } Module::UpsertRTValue("core", "env.state.ctx", std::string{"1"}); @@ -376,7 +378,10 @@ void InitGpgFrontendCore() { Module::UpsertRTValue("core", "env.state.all", std::string{"1"}); } - GpgKeyGetter::GetInstance().FlushKeyCache(); + if (!GpgKeyGetter::GetInstance().FlushKeyCache()) { + CoreSignalStation::GetInstance()->SignalBadGnupgEnv( + _("Gpg Key Detabase inilization failed")); + }; SPDLOG_INFO( "basic env checking finished, including gpgme, ctx, and key " "infos"); diff --git a/src/core/function/CoreSignalStation.h b/src/core/function/CoreSignalStation.h index 57bd4e44..f4d6a142 100644 --- a/src/core/function/CoreSignalStation.h +++ b/src/core/function/CoreSignalStation.h @@ -66,7 +66,7 @@ class GPGFRONTEND_CORE_EXPORT CoreSignalStation : public QObject { * @brief * */ - void SignalBadGnupgEnv(); + void SignalBadGnupgEnv(QString); /** * @brief diff --git a/src/core/function/gpg/GpgKeyGetter.cpp b/src/core/function/gpg/GpgKeyGetter.cpp index a60b66c9..0cd6741c 100644 --- a/src/core/function/gpg/GpgKeyGetter.cpp +++ b/src/core/function/gpg/GpgKeyGetter.cpp @@ -87,7 +87,7 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject<GpgKeyGetter::Impl> { return keys_list; } - void FlushKeyCache() { + auto FlushKeyCache() -> bool { SPDLOG_DEBUG("flush key channel called, channel: {}", GetChannel()); // clear the keys cache @@ -100,7 +100,7 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject<GpgKeyGetter::Impl> { assert(CheckGpgError(err) == GPG_ERR_NO_ERROR); // return when error - if (CheckGpgError(err) != GPG_ERR_NO_ERROR) return; + if (CheckGpgError(err) != GPG_ERR_NO_ERROR) return false; { // get the lock @@ -131,6 +131,7 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject<GpgKeyGetter::Impl> { assert(CheckGpgError2ErrCode(err, GPG_ERR_EOF) == GPG_ERR_NO_ERROR); SPDLOG_DEBUG("flush key channel done, channel: {}", GetChannel()); + return true; } auto GetKeys(const KeyIdArgsListPtr& ids) -> KeyListPtr { @@ -217,7 +218,7 @@ auto GpgKeyGetter::GetPubkey(const std::string& key_id, bool use_cache) return p_->GetPubkey(key_id, use_cache); } -void GpgKeyGetter::FlushKeyCache() { p_->FlushKeyCache(); } +auto GpgKeyGetter::FlushKeyCache() -> bool { return p_->FlushKeyCache(); } auto GpgKeyGetter::GetKeys(const KeyIdArgsListPtr& ids) -> KeyListPtr { return p_->GetKeys(ids); diff --git a/src/core/function/gpg/GpgKeyGetter.h b/src/core/function/gpg/GpgKeyGetter.h index b3e6523e..48ffb0fd 100644 --- a/src/core/function/gpg/GpgKeyGetter.h +++ b/src/core/function/gpg/GpgKeyGetter.h @@ -88,7 +88,7 @@ class GPGFRONTEND_CORE_EXPORT GpgKeyGetter * @brief flush the keys in the cache * */ - void FlushKeyCache(); + auto FlushKeyCache() -> bool; /** * @brief Get the Keys Copy object diff --git a/src/core/thread/CtxCheckTask.cpp b/src/core/thread/CtxCheckTask.cpp deleted file mode 100644 index 88e60f79..00000000 --- a/src/core/thread/CtxCheckTask.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/** - * Copyright (C) 2021 Saturneric <[email protected]> - * - * This file is part of GpgFrontend. - * - * GpgFrontend is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * GpgFrontend is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. - * - * The initial version of the source code is inherited from - * the gpg4usb project, which is under GPL-3.0-or-later. - * - * All the source code of GpgFrontend was modified and released by - * Saturneric <[email protected]> starting on May 12, 2021. - * - * SPDX-License-Identifier: GPL-3.0-or-later - * - */ - -#include "core/thread/CtxCheckTask.h" - -#include "core/GpgCoreInit.h" -#include "core/function/CoreSignalStation.h" -#include "core/function/gpg/GpgContext.h" -#include "core/function/gpg/GpgKeyGetter.h" -#include "thread/Task.h" - -namespace GpgFrontend { - -Thread::CoreInitTask::CoreInitTask() : Task("ctx_check_task") { - connect(this, &CoreInitTask::SignalBadGnupgEnv, - CoreSignalStation::GetInstance(), - &CoreSignalStation::SignalBadGnupgEnv); -} - -void Thread::CoreInitTask::Run() { - // Init GpgFrontend Core - InitGpgFrontendCore(); - - // Create & Check Gnupg Context Status - if (!GpgContext::GetInstance().Good()) { - emit SignalBadGnupgEnv(); - } - // Try flushing key cache - else { - GpgKeyGetter::GetInstance().FlushKeyCache(); - } - - SPDLOG_DEBUG("ctx check task runnable done"); -} -} // namespace GpgFrontend diff --git a/src/core/thread/CtxCheckTask.h b/src/core/thread/CtxCheckTask.h deleted file mode 100644 index 4e5f0d84..00000000 --- a/src/core/thread/CtxCheckTask.h +++ /dev/null @@ -1,62 +0,0 @@ -/** - * Copyright (C) 2021 Saturneric <[email protected]> - * - * This file is part of GpgFrontend. - * - * GpgFrontend is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * GpgFrontend is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. - * - * The initial version of the source code is inherited from - * the gpg4usb project, which is under GPL-3.0-or-later. - * - * All the source code of GpgFrontend was modified and released by - * Saturneric <[email protected]> starting on May 12, 2021. - * - * SPDX-License-Identifier: GPL-3.0-or-later - * - */ - -#pragma once - -#include "core/GpgFrontendCore.h" -#include "core/thread/Task.h" - -namespace GpgFrontend::Thread { -/** - * @brief - * - */ -class GPGFRONTEND_CORE_EXPORT CoreInitTask : public Task { - Q_OBJECT - public: - /** - * @brief Construct a new Ctx Check Thread object - * - */ - CoreInitTask(); - - signals: - /** - * @brief - * - */ - void SignalBadGnupgEnv(); - - protected: - /** - * @brief - * - */ - void Run() override; -}; -} // namespace GpgFrontend::Thread diff --git a/src/ui/GpgFrontendUIInit.cpp b/src/ui/GpgFrontendUIInit.cpp index e8b3663c..9849fab2 100644 --- a/src/ui/GpgFrontendUIInit.cpp +++ b/src/ui/GpgFrontendUIInit.cpp @@ -42,7 +42,6 @@ #include "core/function/CoreSignalStation.h" #include "core/function/GlobalSettingStation.h" #include "core/module/ModuleManager.h" -#include "core/thread/CtxCheckTask.h" #include "core/thread/TaskRunnerGetter.h" #include "ui/UISignalStation.h" #include "ui/UserInterfaceUtils.h" @@ -118,7 +117,7 @@ void WaitEnvCheckingProcess() { void PreInitGpgFrontendUI() { CommonUtils::GetInstance(); } -void InitGpgFrontendUI(QApplication* app) { +void InitGpgFrontendUI(QApplication* /*app*/) { // init locale InitLocale(); @@ -179,10 +178,12 @@ void InitGpgFrontendUI(QApplication* app) { proxy.setType(proxy_type_qt); proxy.setHostName(QString::fromStdString(proxy_host)); proxy.setPort(proxy_port); - if (!proxy_username.empty()) + if (!proxy_username.empty()) { proxy.setUser(QString::fromStdString(proxy_username)); - if (!proxy_password.empty()) + } + if (!proxy_password.empty()) { proxy.setPassword(QString::fromStdString(proxy_password)); + } } else { proxy.setType(proxy_type_qt); } @@ -226,9 +227,6 @@ auto RunGpgFrontendUI(QApplication* app) -> int { } void InitUILoggingSystem(spdlog::level::level_enum level) { - using namespace boost::posix_time; - using namespace boost::gregorian; - // get the log directory auto logfile_path = (GlobalSettingStation::GetInstance().GetLogDir() / "ui"); logfile_path.replace_extension(".log"); @@ -283,13 +281,15 @@ void InitLocale() { // create general settings if not exist if (!settings.exists("general") || - settings.lookup("general").getType() != libconfig::Setting::TypeGroup) + settings.lookup("general").getType() != libconfig::Setting::TypeGroup) { settings.add("general", libconfig::Setting::TypeGroup); + } // set system default at first auto& general = settings["general"]; - if (!general.exists("lang")) + if (!general.exists("lang")) { general.add("lang", libconfig::Setting::TypeString) = ""; + } // sync the settings to the file GpgFrontend::GlobalSettingStation::GetInstance().SyncSettings(); @@ -316,12 +316,12 @@ void InitLocale() { // set LC_ALL auto* locale_name = setlocale(LC_ALL, lc.c_str()); if (locale_name == nullptr) SPDLOG_WARN("set LC_ALL failed, lc: {}", lc); - auto language = getenv("LANGUAGE"); + auto* language = getenv("LANGUAGE"); // set LANGUAGE std::string language_env = language == nullptr ? "en" : language; language_env.insert(0, lang + ":"); SPDLOG_DEBUG("language env: {}", language_env); - if (setenv("LANGUAGE", language_env.c_str(), 1)) { + if (setenv("LANGUAGE", language_env.c_str(), 1) != 0) { SPDLOG_WARN("set LANGUAGE {} failed", language_env); }; } diff --git a/src/ui/UserInterfaceUtils.cpp b/src/ui/UserInterfaceUtils.cpp index 7c42e86d..cebd4854 100644 --- a/src/ui/UserInterfaceUtils.cpp +++ b/src/ui/UserInterfaceUtils.cpp @@ -32,6 +32,7 @@ #include <qdialog.h> #include <QtNetwork> +#include <boost/format/format_fwd.hpp> #include <string> #include <utility> #include <vector> @@ -184,39 +185,43 @@ CommonUtils::CommonUtils() : QWidget(nullptr) { &UISignalStation::SignalRestartApplication, this, &CommonUtils::SlotRestartApplication); - connect(this, &CommonUtils::SignalBadGnupgEnv, this, [=]() { - QMessageBox msg_box; - msg_box.setText(_("GnuPG Context Loading Failed")); - msg_box.setInformativeText( - _("Gnupg(gpg) is not installed correctly, please follow " - "<a href='https://www.gpgfrontend.bktus.com/#/" - "faq?id=how-to-deal-with-39env-loading-failed39'>this notes</a>" - " in FAQ to install Gnupg and then open " - "GpgFrontend. Or, you can open GnuPG Controller to set a custom " - "GnuPG " - "which GpgFrontend should use. Then, GpgFrontend will restart.")); - msg_box.setStandardButtons(QMessageBox::Open | QMessageBox::Cancel); - msg_box.setDefaultButton(QMessageBox::Save); - int ret = msg_box.exec(); - - switch (ret) { - case QMessageBox::Open: - (new GnuPGControllerDialog(this))->exec(); - // restart application when loop start - application_need_to_restart_at_once_ = true; - // restart application, core and ui - emit SignalRestartApplication(kDeepRestartCode); - break; - case QMessageBox::Cancel: - // close application - emit SignalRestartApplication(0); - break; - default: - // close application - emit SignalRestartApplication(0); - break; - } - }); + connect( + this, &CommonUtils::SignalBadGnupgEnv, this, [=](const QString &reason) { + QMessageBox msg_box; + msg_box.setText(_("GnuPG Context Loading Failed")); + msg_box.setInformativeText( + QString(_("Gnupg(gpg) is not installed correctly, please follow " + "<a href='https://www.gpgfrontend.bktus.com/#/" + "faq?id=how-to-deal-with-39env-loading-failed39'>this " + "notes</a> in FAQ to install Gnupg and then open " + "GpgFrontend. <br />" + "Or, you can open GnuPG Controller to set a " + "custom GnuPG which GpgFrontend should use. Then, " + "GpgFrontend will restart. <br /><br />" + "Breif Reason: %1")) + .arg(reason)); + msg_box.setStandardButtons(QMessageBox::Open | QMessageBox::Cancel); + msg_box.setDefaultButton(QMessageBox::Save); + int ret = msg_box.exec(); + + switch (ret) { + case QMessageBox::Open: + (new GnuPGControllerDialog(this))->exec(); + // restart application when loop start + application_need_to_restart_at_once_ = true; + // restart application, core and ui + emit SignalRestartApplication(kDeepRestartCode); + break; + case QMessageBox::Cancel: + // close application + emit SignalRestartApplication(0); + break; + default: + // close application + emit SignalRestartApplication(0); + break; + } + }); } void CommonUtils::WaitForOpera(QWidget *parent, diff --git a/src/ui/UserInterfaceUtils.h b/src/ui/UserInterfaceUtils.h index f9210125..2bbaa106 100644 --- a/src/ui/UserInterfaceUtils.h +++ b/src/ui/UserInterfaceUtils.h @@ -207,7 +207,7 @@ class CommonUtils : public QWidget { * @brief * */ - void SignalBadGnupgEnv(); + void SignalBadGnupgEnv(QString); /** * @brief emit when the key database is refreshed |