diff options
author | Saturneric <[email protected]> | 2023-07-13 06:51:32 +0000 |
---|---|---|
committer | Saturneric <[email protected]> | 2023-07-13 06:51:32 +0000 |
commit | 7c96f052da1f73cbcd2f0671640d1019eaaa906a (patch) | |
tree | 814d98407a0a3bc8adb32f4b8065d8428f016eb9 /src/ui/UserInterfaceUtils.cpp | |
parent | feat: add CacheManager to deal with cache (diff) | |
download | GpgFrontend-7c96f052da1f73cbcd2f0671640d1019eaaa906a.tar.gz GpgFrontend-7c96f052da1f73cbcd2f0671640d1019eaaa906a.zip |
feat: support marking a key as favourite
Diffstat (limited to 'src/ui/UserInterfaceUtils.cpp')
-rw-r--r-- | src/ui/UserInterfaceUtils.cpp | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/ui/UserInterfaceUtils.cpp b/src/ui/UserInterfaceUtils.cpp index 614d5bc9..3154aa2b 100644 --- a/src/ui/UserInterfaceUtils.cpp +++ b/src/ui/UserInterfaceUtils.cpp @@ -34,6 +34,7 @@ #include "core/GpgConstants.h" #include "core/common/CoreCommonUtil.h" +#include "core/function/CacheManager.h" #include "core/function/CoreSignalStation.h" #include "core/function/FileOperator.h" #include "core/function/GlobalSettingStation.h" @@ -481,4 +482,41 @@ bool CommonUtils::isApplicationNeedRestart() { return application_need_to_restart_at_once_; } +bool CommonUtils::KeyExistsinFavouriteList(const GpgKey &key) { + // load cache + auto key_array = CacheManager::GetInstance().LoadCache("favourite_key_pair"); + if (!key_array.is_array()) { + CacheManager::GetInstance().SaveCache("favourite_key_pair", + nlohmann::json::array()); + } + return std::find(key_array.begin(), key_array.end(), key.GetFingerprint()) != + key_array.end(); +} + +void CommonUtils::AddKey2Favourtie(const GpgKey &key) { + auto key_array = CacheManager::GetInstance().LoadCache("favourite_key_pair"); + if (!key_array.is_array()) { + CacheManager::GetInstance().SaveCache("favourite_key_pair", + nlohmann::json::array()); + } + key_array.push_back(key.GetFingerprint()); + CacheManager::GetInstance().SaveCache("favourite_key_pair", key_array); +} + +void CommonUtils::RemoveKeyFromFavourite(const GpgKey &key) { + auto key_array = CacheManager::GetInstance().LoadCache("favourite_key_pair"); + if (!key_array.is_array()) { + CacheManager::GetInstance().SaveCache("favourite_key_pair", + nlohmann::json::array()); + return; + } + auto it = std::find(key_array.begin(), key_array.end(), key.GetFingerprint()); + if (it != key_array.end()) { + auto rm_it = + std::remove(key_array.begin(), key_array.end(), key.GetFingerprint()); + key_array.erase(rm_it, key_array.end()); + CacheManager::GetInstance().SaveCache("favourite_key_pair", key_array); + } +} + } // namespace GpgFrontend::UI
\ No newline at end of file |