aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSaturneric <[email protected]>2023-03-31 08:24:22 +0000
committerSaturneric <[email protected]>2023-03-31 08:24:22 +0000
commit0e4ef6e675dca771394b33752d3df8d712cd1a4a (patch)
tree66366ab08997ca91f3c777fb736dc7ebaced82bf
parentfix: set application recover limit (diff)
downloadGpgFrontend-0e4ef6e675dca771394b33752d3df8d712cd1a4a.tar.gz
GpgFrontend-0e4ef6e675dca771394b33752d3df8d712cd1a4a.zip
feat: add offline and auto import support in context
-rw-r--r--src/core/GpgContext.cpp30
-rw-r--r--src/core/GpgContext.h2
2 files changed, 19 insertions, 13 deletions
diff --git a/src/core/GpgContext.cpp b/src/core/GpgContext.cpp
index 5d3b53e5..f8dbebc6 100644
--- a/src/core/GpgContext.cpp
+++ b/src/core/GpgContext.cpp
@@ -43,6 +43,7 @@
#include "core/function/GlobalSettingStation.h"
#include "core/function/gpg/GpgCommandExecutor.h"
#include "core/thread/TaskRunnerGetter.h"
+#include "spdlog/spdlog.h"
#include "thread/Task.h"
#ifdef _WIN32
@@ -59,20 +60,12 @@ GpgContext::GpgContext(int channel)
* Set up gpgme-context, set paths to app-run path
*/
GpgContext::GpgContext(const GpgContextInitArgs &args) : args_(args) {
- static bool _first = true;
-
- if (_first) {
- /* Initialize the locale environment. */
- SPDLOG_DEBUG("locale: {}", setlocale(LC_CTYPE, nullptr));
- info_.GpgMEVersion = gpgme_check_version(nullptr);
- gpgme_set_locale(nullptr, LC_CTYPE, setlocale(LC_CTYPE, nullptr));
-#ifdef LC_MESSAGES
- gpgme_set_locale(nullptr, LC_MESSAGES, setlocale(LC_MESSAGES, nullptr));
-#endif
- _first = false;
- }
-
gpgme_ctx_t _p_ctx;
+
+ // get gpgme library version
+ info_.GpgMEVersion = gpgme_check_version(nullptr);
+
+ // create a new context
check_gpg_error(gpgme_new(&_p_ctx));
_ctx_ref = CtxRefHandler(_p_ctx);
@@ -84,6 +77,17 @@ GpgContext::GpgContext(const GpgContextInitArgs &args) : args_(args) {
assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR);
}
+ // set context offline mode
+ SPDLOG_DEBUG("gpg context offline mode: {}", args_.offline_mode);
+ gpgme_set_offline(_ctx_ref.get(), args_.offline_mode ? 1 : 0);
+
+ // set option auto import missing key
+ // invalid at offline mode
+ SPDLOG_DEBUG("gpg context auto import missing key: {}", args_.offline_mode);
+ if (!args.offline_mode && args.auto_import_missing_key)
+ check_gpg_error(gpgme_set_ctx_flag(_ctx_ref.get(), "auto-key-import", "1"));
+
+ // get engine info
auto engine_info = gpgme_ctx_get_engine_info(*this);
// Check ENV before running
bool check_passed = false, find_openpgp = false, find_gpgconf = false,
diff --git a/src/core/GpgContext.h b/src/core/GpgContext.h
index 384271a6..2ff87e6b 100644
--- a/src/core/GpgContext.h
+++ b/src/core/GpgContext.h
@@ -51,6 +51,8 @@ struct GpgContextInitArgs {
std::string gpg_path = {};
bool test_mode = false;
bool ascii = true;
+ bool offline_mode = false;
+ bool auto_import_missing_key = false;
GpgContextInitArgs() = default;
};