diff options
author | saturneric <[email protected]> | 2024-07-12 18:38:16 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2024-07-12 18:38:16 +0000 |
commit | d1d6859e2a50a78f57388ebf0a06f0636d4d0910 (patch) | |
tree | 5ac6a57abc73902aba8979ed9b67816b1e85fb9a | |
parent | feat: adjust modules loading path (diff) | |
download | GpgFrontend-d1d6859e2a50a78f57388ebf0a06f0636d4d0910.tar.gz GpgFrontend-d1d6859e2a50a78f57388ebf0a06f0636d4d0910.zip |
feat: add some ui apis to sdk
-rw-r--r-- | CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/core/module/Module.cpp | 2 | ||||
-rw-r--r-- | src/core/utils/CommonUtils.cpp | 4 | ||||
-rw-r--r-- | src/core/utils/CommonUtils.h | 8 | ||||
-rw-r--r-- | src/sdk/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/sdk/GFSDKBasic.cpp | 28 | ||||
-rw-r--r-- | src/sdk/GFSDKBasic.h | 17 | ||||
-rw-r--r-- | src/sdk/GFSDKExtra.cpp | 13 | ||||
-rw-r--r-- | src/sdk/GFSDKModule.cpp | 45 | ||||
-rw-r--r-- | src/sdk/GFSDKModule.h | 3 | ||||
-rw-r--r-- | src/sdk/GFSDKUI.cpp | 39 | ||||
-rw-r--r-- | src/sdk/GFSDKUI.h | 19 | ||||
-rw-r--r-- | src/sdk/private/CommonUtils.cpp | 82 | ||||
-rw-r--r-- | src/sdk/private/CommonUtils.h | 69 | ||||
-rw-r--r-- | src/ui/GpgFrontendUIInit.cpp | 27 | ||||
-rw-r--r-- | src/ui/GpgFrontendUIInit.h | 7 | ||||
-rw-r--r-- | src/ui/UIModuleManager.cpp | 93 | ||||
-rw-r--r-- | src/ui/UIModuleManager.h | 104 | ||||
-rw-r--r-- | src/ui/dialog/help/AboutDialog.cpp | 146 | ||||
-rw-r--r-- | src/ui/dialog/help/AboutDialog.h | 41 | ||||
-rw-r--r-- | src/ui/struct/UIMountPoint.h | 58 |
21 files changed, 586 insertions, 225 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index d0628ae3..8e21e33f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -367,7 +367,6 @@ if (STABLE_BUILD_APPLICATION) message("[+] Build Stable Application") set(BUILD_CORE 1) set(BUILD_UI 1) - set(BUILD_MODULE 1) set(BUILD_TEST 1) set(BUILD_APPLICATION 1) set(SUPPORT_MULTI_LANG 1) @@ -375,7 +374,7 @@ if (STABLE_BUILD_APPLICATION) elseif (STABLE_BUILD_SDK_ONLY) message("[+] Build SDK") set(BUILD_CORE 1) - set(BUILD_MODULE 1) + set(BUILD_UI 1) set(BUILD_SDK 1) endif () diff --git a/src/core/module/Module.cpp b/src/core/module/Module.cpp index 202f9d81..8faae4e0 100644 --- a/src/core/module/Module.cpp +++ b/src/core/module/Module.cpp @@ -34,6 +34,8 @@ #include "sdk/GFSDKModule.h" #include "utils/BuildInfoUtils.h" + + namespace GpgFrontend::Module { class Module::Impl { diff --git a/src/core/utils/CommonUtils.cpp b/src/core/utils/CommonUtils.cpp index d1704312..941ea39f 100644 --- a/src/core/utils/CommonUtils.cpp +++ b/src/core/utils/CommonUtils.cpp @@ -82,13 +82,13 @@ auto GFStrDup(const QString& str) -> char* { return c_str; } -auto GPGFRONTEND_CORE_EXPORT GFUnStrDup(char* str) -> QString { +auto GFUnStrDup(char* str) -> QString { auto qt_str = QString::fromUtf8(str); SecureFree(static_cast<void*>(const_cast<char*>(str))); return qt_str; } -auto GPGFRONTEND_CORE_EXPORT GFUnStrDup(const char* str) -> QString { +auto GFUnStrDup(const char* str) -> QString { return GFUnStrDup(const_cast<char*>(str)); } diff --git a/src/core/utils/CommonUtils.h b/src/core/utils/CommonUtils.h index 6ef38fad..bd546a11 100644 --- a/src/core/utils/CommonUtils.h +++ b/src/core/utils/CommonUtils.h @@ -48,21 +48,21 @@ auto GPGFRONTEND_CORE_EXPORT BeautifyFingerprint(QString fingerprint) * @param b * @return int */ -auto GPGFRONTEND_CORE_EXPORT GFCompareSoftwareVersion(const QString& a, - const QString& b) -> int; +auto GPGFRONTEND_CORE_EXPORT GFCompareSoftwareVersion(const QString &a, + const QString &b) -> int; /** * @brief * * @return char* */ -auto GPGFRONTEND_CORE_EXPORT GFStrDup(const QString&) -> char*; +auto GFStrDup(const QString &) -> char *; /** * @brief * * @return QString */ -auto GPGFRONTEND_CORE_EXPORT GFUnStrDup(const char*) -> QString; +auto GFUnStrDup(const char *) -> QString; } // namespace GpgFrontend
\ No newline at end of file diff --git a/src/sdk/CMakeLists.txt b/src/sdk/CMakeLists.txt index 59f673e0..765c0e73 100644 --- a/src/sdk/CMakeLists.txt +++ b/src/sdk/CMakeLists.txt @@ -28,6 +28,7 @@ set(CMAKE_VISIBILITY_INLINES_HIDDEN 1) # define libgpgfrontend_module_sdk aux_source_directory(. MODULE_SDK_SOURCE) +aux_source_directory(private MODULE_SDK_SOURCE) add_library(gpgfrontend_module_sdk SHARED ${MODULE_SDK_SOURCE}) set(_export_file_sdk "${CMAKE_CURRENT_SOURCE_DIR}/GFSDKExport.h") @@ -39,7 +40,7 @@ target_include_directories(gpgfrontend_module_sdk PRIVATE target_include_directories(gpgfrontend_module_sdk PUBLIC sdk) # link module system -target_link_libraries(gpgfrontend_module_sdk PRIVATE gpgfrontend_core) +target_link_libraries(gpgfrontend_module_sdk PRIVATE gpgfrontend_core gpgfrontend_ui) # sdk export headers file(GLOB _headerPath "${CMAKE_CURRENT_SOURCE_DIR}/*.h") diff --git a/src/sdk/GFSDKBasic.cpp b/src/sdk/GFSDKBasic.cpp index 4d8af6c8..aefa022a 100644 --- a/src/sdk/GFSDKBasic.cpp +++ b/src/sdk/GFSDKBasic.cpp @@ -31,7 +31,8 @@ #include "core/function/SecureMemoryAllocator.h" #include "core/function/gpg/GpgCommandExecutor.h" #include "core/utils/BuildInfoUtils.h" -#include "core/utils/CommonUtils.h" +#include "sdk/private/CommonUtils.h" +#include "ui/GpgFrontendUIInit.h" auto GFAllocateMemory(uint32_t size) -> void* { return GpgFrontend::SecureMemoryAllocator::Allocate(size); @@ -42,18 +43,16 @@ void GFFreeMemory(void* ptr) { } auto GFProjectVersion() -> const char* { - return GpgFrontend::GFStrDup(GpgFrontend::GetProjectVersion()); + return GFStrDup(GpgFrontend::GetProjectVersion()); } -auto GFQtEnvVersion() -> const char* { - return GpgFrontend::GFStrDup(QT_VERSION_STR); -} +auto GFQtEnvVersion() -> const char* { return GFStrDup(QT_VERSION_STR); } void GFExecuteCommandSync(const char* cmd, int32_t argc, const char** argv, GFCommandExeucteCallback cb, void* data) { QStringList args; for (int i = 0; i < argc; i++) { - args.append(GpgFrontend::GFUnStrDup(argv[i])); + args.append(GFUnStrDup(argv[i])); } GpgFrontend::GpgCommandExecutor::ExecuteContext const context{ @@ -74,7 +73,7 @@ void GFExecuteCommandBatchSync(int32_t context_size, QStringList args; const char** argv = exec_context.argv; for (int j = 0; j < exec_context.argc; j++) { - args.append(GpgFrontend::GFUnStrDup(argv[j])); + args.append(GFUnStrDup(argv[j])); } contexts.append( @@ -94,7 +93,7 @@ auto StrlenSafe(const char* str, size_t max_len) -> size_t { return end - str; } -auto GPGFRONTEND_MODULE_SDK_EXPORT GFModuleStrDup(const char* src) -> char* { +auto GFModuleStrDup(const char* src) -> char* { auto len = StrlenSafe(src, kGfStrlenMax); if (len > kGfStrlenMax) return nullptr; @@ -103,4 +102,15 @@ auto GPGFRONTEND_MODULE_SDK_EXPORT GFModuleStrDup(const char* src) -> char* { dst[len] = '\0'; return dst; -}
\ No newline at end of file +} + +auto GFAppActiveLocale() -> char* { return GFStrDup(QLocale().name()); } + +auto GFAppRegisterTranslator(char* data, int size) -> int { + auto b = QByteArray(data, size); + QMetaObject::invokeMethod(QApplication::instance()->thread(), [b]() { + GpgFrontend::UI::InstallTranslatorFromQMData(b); + }); + GFFreeMemory(data); + return 0; +} diff --git a/src/sdk/GFSDKBasic.h b/src/sdk/GFSDKBasic.h index 07ff6ed7..2a3071cd 100644 --- a/src/sdk/GFSDKBasic.h +++ b/src/sdk/GFSDKBasic.h @@ -104,4 +104,21 @@ void GPGFRONTEND_MODULE_SDK_EXPORT GFExecuteCommandBatchSync( * @return char* */ auto GPGFRONTEND_MODULE_SDK_EXPORT GFModuleStrDup(const char*) -> char*; + +/** + * @brief + * + * @return char* + */ +auto GPGFRONTEND_MODULE_SDK_EXPORT GFAppActiveLocale() -> char*; + +/** + * @brief + * + * @param data + * @param size + * @return auto + */ +auto GPGFRONTEND_MODULE_SDK_EXPORT GFAppRegisterTranslator(char* data, int size) + -> int; }
\ No newline at end of file diff --git a/src/sdk/GFSDKExtra.cpp b/src/sdk/GFSDKExtra.cpp index bbfa8575..00e734da 100644 --- a/src/sdk/GFSDKExtra.cpp +++ b/src/sdk/GFSDKExtra.cpp @@ -28,15 +28,16 @@ #include "GFSDKExtra.h" -#include "core/utils/BuildInfoUtils.h" -#include "core/utils/CommonUtils.h" +#include <core/utils/BuildInfoUtils.h> +#include <core/utils/CommonUtils.h> + +#include "sdk/private/CommonUtils.h" auto GFCompareSoftwareVersion(const char *current_version, const char *latest_version) -> int { - return GpgFrontend::GFCompareSoftwareVersion( - GpgFrontend::GFUnStrDup(current_version), - GpgFrontend::GFUnStrDup(latest_version)); + return GpgFrontend::GFCompareSoftwareVersion(GFUnStrDup(current_version), + GFUnStrDup(latest_version)); } auto GFHttpRequestUserAgent() -> const char * { - return GpgFrontend::GFStrDup(GpgFrontend::GetHttpRequestUserAgent()); + return GFStrDup(GpgFrontend::GetHttpRequestUserAgent()); }
\ No newline at end of file diff --git a/src/sdk/GFSDKModule.cpp b/src/sdk/GFSDKModule.cpp index 00594488..c0215a8b 100644 --- a/src/sdk/GFSDKModule.cpp +++ b/src/sdk/GFSDKModule.cpp @@ -29,45 +29,40 @@ #include "GFSDKModule.h" #include <core/module/ModuleManager.h> -#include <core/utils/CommonUtils.h> +#include <sdk/private/CommonUtils.h> #include "GFSDKBasic.h" void GFModuleListenEvent(const char *module_id, const char *event_id) { return GpgFrontend::Module::ModuleManager::GetInstance().ListenEvent( - GpgFrontend::GFUnStrDup(module_id).toLower(), - GpgFrontend::GFUnStrDup(event_id).toUpper()); + GFUnStrDup(module_id).toLower(), GFUnStrDup(event_id).toUpper()); } auto GFModuleRetrieveRTValueOrDefault(const char *namespace_, const char *key, const char *default_value) -> const char * { - return GpgFrontend::GFStrDup( - GpgFrontend::Module::RetrieveRTValueTypedOrDefault( - GpgFrontend::GFUnStrDup(namespace_), GpgFrontend::GFUnStrDup(key), - GpgFrontend::GFUnStrDup(default_value))); + return GFStrDup(GpgFrontend::Module::RetrieveRTValueTypedOrDefault( + GFUnStrDup(namespace_), GFUnStrDup(key), GFUnStrDup(default_value))); } void GFModuleUpsertRTValue(const char *namespace_, const char *key, const char *vaule) { - GpgFrontend::Module::UpsertRTValue( - GpgFrontend::GFUnStrDup(namespace_).toLower(), - GpgFrontend::GFUnStrDup(key).toLower(), GpgFrontend::GFUnStrDup(vaule)); + GpgFrontend::Module::UpsertRTValue(GFUnStrDup(namespace_).toLower(), + GFUnStrDup(key).toLower(), + GFUnStrDup(vaule)); } void GFModuleUpsertRTValueBool(const char *namespace_, const char *key, int value) { - GpgFrontend::Module::UpsertRTValue( - GpgFrontend::GFUnStrDup(namespace_).toLower(), - GpgFrontend::GFUnStrDup(key).toLower(), value != 0); + GpgFrontend::Module::UpsertRTValue(GFUnStrDup(namespace_).toLower(), + GFUnStrDup(key).toLower(), value != 0); } auto GFModuleListRTChildKeys(const char *namespace_, const char *key, char ***child_keys) -> int32_t { *child_keys = nullptr; auto keys = GpgFrontend::Module::ListRTChildKeys( - GpgFrontend::GFUnStrDup(namespace_).toLower(), - GpgFrontend::GFUnStrDup(key).toLower()); + GFUnStrDup(namespace_).toLower(), GFUnStrDup(key).toLower()); if (keys.empty()) return 0; @@ -75,7 +70,7 @@ auto GFModuleListRTChildKeys(const char *namespace_, const char *key, static_cast<char **>(GFAllocateMemory(sizeof(char **) * keys.size())); for (int i = 0; i < keys.size(); i++) { - (*child_keys)[i] = GpgFrontend::GFStrDup(keys[i]); + (*child_keys)[i] = GFStrDup(keys[i]); } return static_cast<int32_t>(keys.size()); @@ -86,13 +81,21 @@ void GFModuleTriggerModuleEventCallback(GFModuleEvent *module_event, char **argv) { auto data_object = GpgFrontend::TransferParams(); for (int i = 0; i < argc; i++) { - data_object->AppendObject(GpgFrontend::GFUnStrDup(argv[i])); + data_object->AppendObject(GFUnStrDup(argv[i])); } auto event = GpgFrontend::Module::ModuleManager::GetInstance().SearchEvent( - GpgFrontend::GFUnStrDup(module_event->trigger_id).toLower()); + GFUnStrDup(module_event->trigger_id).toLower()); if (!event) return; - event.value()->ExecuteCallback(GpgFrontend::GFUnStrDup(module_id), - data_object); -}
\ No newline at end of file + event.value()->ExecuteCallback(GFUnStrDup(module_id), data_object); +} + +auto GFModuleRetrieveRTValueOrDefaultBool(const char *namespace_, + const char *key, int default_value) + -> const int { + return static_cast<const int>( + GpgFrontend::Module::RetrieveRTValueTypedOrDefault( + GFUnStrDup(namespace_), GFUnStrDup(key), + static_cast<bool>(default_value))); +} diff --git a/src/sdk/GFSDKModule.h b/src/sdk/GFSDKModule.h index 67c1f492..271a4ac4 100644 --- a/src/sdk/GFSDKModule.h +++ b/src/sdk/GFSDKModule.h @@ -79,6 +79,9 @@ auto GPGFRONTEND_MODULE_SDK_EXPORT GFModuleRetrieveRTValueOrDefault( const char *namespace_, const char *key, const char *default_value) -> const char *; +auto GPGFRONTEND_MODULE_SDK_EXPORT GFModuleRetrieveRTValueOrDefaultBool( + const char *namespace_, const char *key, int default_value) -> const int; + void GPGFRONTEND_MODULE_SDK_EXPORT GFModuleUpsertRTValue(const char *namespace_, const char *key, const char *vaule); diff --git a/src/sdk/GFSDKUI.cpp b/src/sdk/GFSDKUI.cpp index 63859763..2222fca2 100644 --- a/src/sdk/GFSDKUI.cpp +++ b/src/sdk/GFSDKUI.cpp @@ -24,4 +24,41 @@ * * SPDX-License-Identifier: GPL-3.0-or-later * - */
\ No newline at end of file + */ + +#include "GFSDKUI.h" + +#include <core/utils/CommonUtils.h> + +#include <QMap> +#include <QString> + +#include "sdk/private/CommonUtils.h" +#include "ui/UIModuleManager.h" + +auto MetaDataArrayToQMap(MetaData** meta_data_array, int size) + -> QMap<QString, QString> { + QMap<QString, QString> map; + + for (int i = 0; i < size; ++i) { + QString const key = GFUnStrDup(meta_data_array[i]->key); + QString const value = GFUnStrDup(meta_data_array[i]->value); + map.insert(key, value); + + GpgFrontend::SecureFree(meta_data_array[i]); + } + + GpgFrontend::SecureFree(meta_data_array); + return map; +} + +auto GFUIMountEntry(const char* id, MetaData** meta_data_array, + int meta_data_array_size, EntryFactory factory) -> int { + if (id == nullptr || factory == nullptr) return -1; + + auto meta_data = MetaDataArrayToQMap(meta_data_array, meta_data_array_size); + return GpgFrontend::UI::UIModuleManager::GetInstance().MountEntry( + GFUnStrDup(id), meta_data, factory) + ? 0 + : -1; +} diff --git a/src/sdk/GFSDKUI.h b/src/sdk/GFSDKUI.h index 0702632a..ba9a3490 100644 --- a/src/sdk/GFSDKUI.h +++ b/src/sdk/GFSDKUI.h @@ -26,4 +26,21 @@ * */ -#pragma once
\ No newline at end of file +#pragma once + +#include "GFSDKExport.h" + +extern "C" { + +using EntryFactory = void* (*)(const char*); + +struct MetaData { + const char* key; + const char* value; +}; + +auto GPGFRONTEND_MODULE_SDK_EXPORT GFUIMountEntry(const char* id, + MetaData** meta_data_array, + int meta_data_array_size, + EntryFactory factory) -> int; +}
\ No newline at end of file diff --git a/src/sdk/private/CommonUtils.cpp b/src/sdk/private/CommonUtils.cpp new file mode 100644 index 00000000..7d72415e --- /dev/null +++ b/src/sdk/private/CommonUtils.cpp @@ -0,0 +1,82 @@ +/** + * 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 "CommonUtils.h" + +#include <core/utils/MemoryUtils.h> + +auto GFStrDup(const QString& str) -> char* { + auto utf8_str = str.toUtf8(); + auto* c_str = static_cast<char*>( + GpgFrontend::SecureMalloc((utf8_str.size() + 1) * sizeof(char))); + + memcpy(c_str, utf8_str.constData(), utf8_str.size()); + c_str[utf8_str.size()] = '\0'; + return c_str; +} + +auto GFUnStrDup(char* str) -> QString { + auto qt_str = QString::fromUtf8(str); + GpgFrontend::SecureFree(static_cast<void*>(const_cast<char*>(str))); + return qt_str; +} + +auto GFUnStrDup(const char* str) -> QString { + return GFUnStrDup(const_cast<char*>(str)); +} + +auto CharArrayToQMap(char** char_array, int size) -> QMap<QString, QString> { + QMap<QString, QString> map; + for (int i = 0; i < size; i += 2) { + QString const key = GFUnStrDup(char_array[i]); + QString const value = QString::fromUtf8(char_array[i + 1]); + map.insert(key, value); + } + return map; +} + +auto QMapToCharArray(const QMap<QString, QString>& map, int& size) -> char** { + size = map.size() * 2; + char** char_array = new char*[size]; + + int index = 0; + for (auto it = map.begin(); it != map.end(); ++it) { + QByteArray const key = it.key().toUtf8(); + QByteArray const value = it.value().toUtf8(); + + char_array[index] = new char[key.size() + 1]; + std::strcpy(char_array[index], key.constData()); + index++; + + char_array[index] = new char[value.size() + 1]; + std::strcpy(char_array[index], value.constData()); + index++; + } + + return char_array; +}
\ No newline at end of file diff --git a/src/sdk/private/CommonUtils.h b/src/sdk/private/CommonUtils.h new file mode 100644 index 00000000..85704797 --- /dev/null +++ b/src/sdk/private/CommonUtils.h @@ -0,0 +1,69 @@ +/** + * 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 + +/** + * @brief + * + * @return char* + */ +auto GFStrDup(const QString &) -> char *; + +/** + * @brief + * + * @param str + * @return QString + */ +auto GFUnStrDup(char *str) -> QString; + +/** + * @brief + * + * @return QString + */ +auto GFUnStrDup(const char *) -> QString; + +/** + * @brief + * + * @param char_array + * @param size + * @return QMap<QString, QString> + */ +auto CharArrayToQMap(char **char_array, int size) -> QMap<QString, QString>; + +/** + * @brief + * + * @param map + * @param size + * @return char** + */ +auto QMapToCharArray(const QMap<QString, QString> &map, int &size) -> char **;
\ No newline at end of file diff --git a/src/ui/GpgFrontendUIInit.cpp b/src/ui/GpgFrontendUIInit.cpp index 046c25d7..b0b9c28e 100644 --- a/src/ui/GpgFrontendUIInit.cpp +++ b/src/ui/GpgFrontendUIInit.cpp @@ -30,6 +30,7 @@ #include <QtNetwork> +#include "UIModuleManager.h" #include "core/GpgConstants.h" #include "core/function/CoreSignalStation.h" #include "core/function/GlobalSettingStation.h" @@ -42,6 +43,7 @@ namespace GpgFrontend::UI { QList<QTranslator*> registered_translators; +QList<QByteArray> loaded_qm_datum; extern void InitUITranslations(); @@ -104,7 +106,13 @@ void WaitEnvCheckingProcess() { looper.exec(); } -void PreInitGpgFrontendUI() { CommonUtils::GetInstance(); } +void PreInitGpgFrontendUI() { + CommonUtils::GetInstance(); + + // declare module ui entry mount points + UIModuleManager::GetInstance().DeclareMountPoint("AboutDialogTabs", "QWidget", + {}); +} void InitGpgFrontendUI(QApplication* /*app*/) { // init locale @@ -228,6 +236,7 @@ void InitUITranslations() { QCoreApplication::removeTranslator(translator); } registered_translators.clear(); + loaded_qm_datum.clear(); auto* translator = new QTranslator(QCoreApplication::instance()); if (translator->load(QLocale(), QLatin1String("qt"), QLatin1String("_"), @@ -259,4 +268,20 @@ void InitUITranslations() { } } +auto InstallTranslatorFromQMData(const QByteArray& data) -> bool { + auto* translator = new QTranslator(QCoreApplication::instance()); + if (translator->load(reinterpret_cast<uchar*>(const_cast<char*>(data.data())), + data.size())) { + GF_UI_LOG_DEBUG("load target translation file done, locale: {}", + QLocale().name()); + QCoreApplication::installTranslator(translator); + registered_translators.append(translator); + loaded_qm_datum.append(data); + + return true; + } + + return false; +} + } // namespace GpgFrontend::UI diff --git a/src/ui/GpgFrontendUIInit.h b/src/ui/GpgFrontendUIInit.h index fd62f3f6..686b3558 100644 --- a/src/ui/GpgFrontendUIInit.h +++ b/src/ui/GpgFrontendUIInit.h @@ -55,4 +55,11 @@ void GPGFRONTEND_UI_EXPORT DestroyGpgFrontendUI(); */ auto GPGFRONTEND_UI_EXPORT RunGpgFrontendUI(QApplication *) -> int; +/** + * @brief + * + */ +auto GPGFRONTEND_UI_EXPORT InstallTranslatorFromQMData(const QByteArray &data) + -> bool; + }; // namespace GpgFrontend::UI diff --git a/src/ui/UIModuleManager.cpp b/src/ui/UIModuleManager.cpp new file mode 100644 index 00000000..8d5e3ca6 --- /dev/null +++ b/src/ui/UIModuleManager.cpp @@ -0,0 +1,93 @@ +/** + * 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 "UIModuleManager.h" + +#include <utility> + +#include "core/module/ModuleManager.h" + +namespace GpgFrontend::UI { + +UIModuleManager::UIModuleManager(int channel) + : SingletonFunctionObject<UIModuleManager>(channel) {} + +UIModuleManager::~UIModuleManager() = default; + +auto UIModuleManager::DeclareMountPoint(const QString& id, + const QString& entry_type, + QMap<QString, QVariant> meta_data_desc) + -> bool { + if (id.isEmpty() || mount_points_.contains(id)) return false; + + UIMountPoint point; + point.id = id; + point.entry_type = entry_type; + point.meta_data_desc = std::move(meta_data_desc); + + mount_points_[id] = point; + + auto grt_key = QString("mount_points.%1").arg(id); + GpgFrontend::Module::UpsertRTValue( + "ui", grt_key, QString(QJsonDocument(point.ToJson()).toJson())); + + return true; +} + +auto UIModuleManager::MountEntry(const QString& id, + QMap<QString, QString> meta_data, + EntryFactory factory) -> bool { + if (id.isEmpty() || !mount_points_.contains(id)) return false; + + if (factory == nullptr) return false; + + MountedUIEntry m_entry; + m_entry.id_ = id; + m_entry.meta_data_ = std::move(meta_data); + m_entry.factory_ = factory; + + mounted_entries_[id].append(m_entry); + return true; +} + +auto UIModuleManager::QueryMountedEntries(QString id) -> QList<MountedUIEntry> { + if (id.isEmpty() || !mount_points_.contains(id)) return {}; + return mounted_entries_[id]; +} + +auto MountedUIEntry::GetWidget() const -> QWidget* { + return qobject_cast<QWidget*>(static_cast<QObject*>(factory_(id_.toUtf8()))); +} + +auto MountedUIEntry::GetMetaDataByDefault(const QString& key, + QString default_value) const + -> QString { + if (!meta_data_.contains(key)) return default_value; + return meta_data_[key]; +} +} // namespace GpgFrontend::UI
\ No newline at end of file diff --git a/src/ui/UIModuleManager.h b/src/ui/UIModuleManager.h new file mode 100644 index 00000000..88096578 --- /dev/null +++ b/src/ui/UIModuleManager.h @@ -0,0 +1,104 @@ +/** + * 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 "GpgFrontendUIExport.h" +#include "core/function/basic/GpgFunctionObject.h" +#include "sdk/GFSDKUI.h" +#include "ui/struct/UIMountPoint.h" + +namespace GpgFrontend::UI { + +struct MountedUIEntry { + QString id_; + QMap<QString, QString> meta_data_; + EntryFactory factory_; + + MountedUIEntry() = default; + + [[nodiscard]] auto GetWidget() const -> QWidget*; + + [[nodiscard]] auto GetMetaDataByDefault(const QString& key, + QString default_value) const + -> QString; +}; + +class GPGFRONTEND_UI_EXPORT UIModuleManager + : public SingletonFunctionObject<UIModuleManager> { + public: + /** + * @brief Construct a new UIModuleManager object + * + * @param channel + */ + explicit UIModuleManager(int channel); + + /** + * @brief Destroy the UIModuleManager object + * + */ + virtual ~UIModuleManager() override; + /** + * @brief + * + * @param id + * @param entry_type + * @param metadata_desc + * @return true + * @return false + */ + auto DeclareMountPoint(const QString& id, const QString& entry_type, + QMap<QString, QVariant> meta_data_desc) -> bool; + + /** + * @brief + * + * @param id + * @param meta_data + * @param entry + * @return true + * @return false + */ + auto MountEntry(const QString& id, QMap<QString, QString> meta_data, + EntryFactory factory) -> bool; + + /** + * @brief + * + * @param id + * @return QList<MountedUIEntry> + */ + auto QueryMountedEntries(QString id) -> QList<MountedUIEntry>; + + private: + QMap<QString, UIMountPoint> mount_points_; + QMap<QString, QList<MountedUIEntry>> mounted_entries_; +}; + +} // namespace GpgFrontend::UI
\ No newline at end of file diff --git a/src/ui/dialog/help/AboutDialog.cpp b/src/ui/dialog/help/AboutDialog.cpp index 6fe56d76..307dfca6 100644 --- a/src/ui/dialog/help/AboutDialog.cpp +++ b/src/ui/dialog/help/AboutDialog.cpp @@ -32,6 +32,7 @@ #include "core/module/ModuleManager.h" #include "core/utils/BuildInfoUtils.h" +#include "ui/UIModuleManager.h" #include "ui/dialog/help/GnupgTab.h" namespace GpgFrontend::UI { @@ -53,9 +54,15 @@ AboutDialog::AboutDialog(const QString& default_tab_name, QWidget* parent) tab_widget->addTab(translators_tab, tr("Translators")); - if (Module::IsModuleActivate(kVersionCheckingModuleID)) { - auto* update_tab = new UpdateTab(); - tab_widget->addTab(update_tab, tr("Update")); + auto entries = + UIModuleManager::GetInstance().QueryMountedEntries("AboutDialogTabs"); + + for (const auto& entry : entries) { + auto* widget = entry.GetWidget(); + if (widget != nullptr) { + tab_widget->addTab(widget, + entry.GetMetaDataByDefault("TabTitle", tr("Unnamed"))); + } } connect(tab_widget, &QTabWidget::currentChanged, this, @@ -152,137 +159,4 @@ TranslatorsTab::TranslatorsTab(QWidget* parent) : QWidget(parent) { setLayout(main_layout); } -UpdateTab::UpdateTab(QWidget* parent) : QWidget(parent) { - auto* layout = new QGridLayout(); - - current_version_ = GetProjectVersion(); - - auto* tips_label = new QLabel(); - tips_label->setText( - "<center>" + - tr("It is recommended that you always check the version " - "of GpgFrontend and upgrade to the latest version.") + - "</center><center>" + - tr("New versions not only represent new features, but " - "also often represent functional and security fixes.") + - "</center>"); - tips_label->setWordWrap(true); - - current_version_label_ = new QLabel(); - current_version_label_->setText("<center>" + tr("Current Version") + - tr(": ") + "<b>" + current_version_ + - "</b></center>"); - current_version_label_->setWordWrap(true); - - latest_version_label_ = new QLabel(); - latest_version_label_->setWordWrap(true); - - upgrade_label_ = new QLabel(); - upgrade_label_->setWordWrap(true); - upgrade_label_->setOpenExternalLinks(true); - upgrade_label_->setHidden(true); - - pb_ = new QProgressBar(); - pb_->setRange(0, 0); - pb_->setTextVisible(false); - - layout->addWidget(tips_label, 1, 0, 1, -1); - layout->addWidget(current_version_label_, 2, 0, 1, -1); - layout->addWidget(latest_version_label_, 3, 0, 1, -1); - layout->addWidget(upgrade_label_, 4, 0, 1, -1); - layout->addWidget(pb_, 5, 0, 1, -1); - layout->addItem( - new QSpacerItem(20, 10, QSizePolicy::Minimum, QSizePolicy::Fixed), 2, 1, - 1, 1); - - setLayout(layout); -} - -void UpdateTab::showEvent(QShowEvent* event) { - QWidget::showEvent(event); - GF_UI_LOG_DEBUG("loading version loading info from rt"); - - auto is_loading_done = Module::RetrieveRTValueTypedOrDefault<>( - kVersionCheckingModuleID, "version.loading_done", false); - - if (!is_loading_done) { - Module::ListenRTPublishEvent( - this, kVersionCheckingModuleID, "version.loading_done", - [=](Module::Namespace, Module::Key, int, std::any) { - GF_UI_LOG_DEBUG( - "version_checking module version.loading_done changed, calling " - "slot version upgrade"); - this->slot_show_version_status(); - }); - Module::TriggerEvent("CHECK_APPLICATION_VERSION"); - } else { - slot_show_version_status(); - } -} - -void UpdateTab::slot_show_version_status() { - GF_UI_LOG_DEBUG("loading version info from rt"); - this->pb_->setHidden(true); - - auto is_loading_done = Module::RetrieveRTValueTypedOrDefault<>( - kVersionCheckingModuleID, "version.loading_done", false); - - if (!is_loading_done) { - GF_UI_LOG_DEBUG("version info loading haven't been done yet."); - return; - } - - auto is_need_upgrade = Module::RetrieveRTValueTypedOrDefault<>( - kVersionCheckingModuleID, "version.need_upgrade", false); - - auto is_current_a_withdrawn_version = Module::RetrieveRTValueTypedOrDefault<>( - kVersionCheckingModuleID, "version.current_a_withdrawn_version", false); - - auto is_current_version_released = Module::RetrieveRTValueTypedOrDefault<>( - kVersionCheckingModuleID, "version.current_version_released", false); - - auto latest_version = Module::RetrieveRTValueTypedOrDefault<>( - kVersionCheckingModuleID, "version.latest_version", QString{}); - - latest_version_label_->setText("<center><b>" + - tr("Latest Version From Github") + ": " + - latest_version + "</b></center>"); - - if (is_need_upgrade) { - upgrade_label_->setText( - "<center>" + - tr("The current version is less than the latest version on " - "github.") + - "</center><center>" + tr("Please click") + - " <a " - "href=\"https://www.gpgfrontend.bktus.com/#/downloads\">" + - tr("Here") + "</a> " + tr("to download the latest stable version.") + - "</center>"); - upgrade_label_->show(); - } else if (is_current_a_withdrawn_version) { - upgrade_label_->setText( - "<center>" + - tr("This version has serious problems and has been withdrawn. " - "Please stop using it immediately.") + - "</center><center>" + tr("Please click") + - " <a " - "href=\"https://github.com/saturneric/GpgFrontend/releases\">" + - tr("Here") + "</a> " + tr("to download the latest stable version.") + - "</center>"); - upgrade_label_->show(); - } else if (!is_current_version_released) { - upgrade_label_->setText( - "<center>" + - tr("This version has not been released yet, it may be a beta " - "version. If you are not a tester and care about version " - "stability, please do not use this version.") + - "</center><center>" + tr("Please click") + - " <a " - "href=\"https://www.gpgfrontend.bktus.com/#/downloads\">" + - tr("Here") + "</a> " + tr("to download the latest stable version.") + - "</center>"); - upgrade_label_->show(); - } -} - } // namespace GpgFrontend::UI diff --git a/src/ui/dialog/help/AboutDialog.h b/src/ui/dialog/help/AboutDialog.h index 6d152afb..0b9ca5ae 100644 --- a/src/ui/dialog/help/AboutDialog.h +++ b/src/ui/dialog/help/AboutDialog.h @@ -66,47 +66,6 @@ class TranslatorsTab : public QWidget { }; /** - * @brief Class containing the main tab of about dialog - * - */ -class UpdateTab : public QWidget { - Q_OBJECT - - QLabel* current_version_label_; ///< - QLabel* latest_version_label_; ///< - QLabel* upgrade_label_; ///< - QProgressBar* pb_; ///< - QString current_version_; ///< - - public: - /** - * @brief Construct a new Update Tab object - * - * @param parent - */ - explicit UpdateTab(QWidget* parent = nullptr); - - protected: - void showEvent(QShowEvent* event) override; - - private slots: - /** - * @brief - * - * @param version - */ - void slot_show_version_status(); - - signals: - /** - * @brief - * - * @param data - */ - void SignalReplyFromUpdateServer(QByteArray data); -}; - -/** * @brief Class for handling the about dialog * */ diff --git a/src/ui/struct/UIMountPoint.h b/src/ui/struct/UIMountPoint.h new file mode 100644 index 00000000..61ac83c6 --- /dev/null +++ b/src/ui/struct/UIMountPoint.h @@ -0,0 +1,58 @@ +/** + * 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 + +struct UIMountPoint { + QString id; + QString entry_type; + QMap<QString, QVariant> meta_data_desc; + + UIMountPoint() = default; + + explicit UIMountPoint(const QJsonObject& j) { + if (const auto v = j["id"]; v.isDouble()) { + id = v.toString(); + } + if (const auto v = j["entry_type"]; v.isDouble()) { + entry_type = v.toString(); + } + if (const auto v = j["meta_data_desc"]; v.isDouble()) { + meta_data_desc = v.toVariant().toMap(); + } + } + + [[nodiscard]] auto ToJson() const -> QJsonObject { + QJsonObject j; + j["id"] = id; + j["entry_type"] = entry_type; + j["meta_data_desc"] = QJsonObject::fromVariantMap(meta_data_desc); + + return j; + } +};
\ No newline at end of file |