diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/core/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/core/plugin/Event.cpp | 61 | ||||
-rw-r--r-- | src/core/plugin/GlobalPluginContext.cpp | 265 | ||||
-rw-r--r-- | src/core/plugin/Plugin.cpp | 72 | ||||
-rw-r--r-- | src/core/plugin/PluginManager.cpp | 87 | ||||
-rw-r--r-- | src/core/thread/DataObject.cpp (renamed from src/plugin/sdk/Task.cpp) | 35 | ||||
-rw-r--r-- | src/core/thread/DataObject.h | 137 | ||||
-rw-r--r-- | src/core/thread/Task.h | 110 | ||||
-rw-r--r-- | src/core/thread/TaskRunnerGetter.cpp | 2 | ||||
-rw-r--r-- | src/plugin/CMakeLists.txt | 33 | ||||
-rw-r--r-- | src/plugin/GpgFrontendPluginExport.h | 29 | ||||
-rw-r--r-- | src/plugin/module/version_checking_plugin/CMakeLists.txt (renamed from src/plugin/integrated_plugins/version_checking_plugin/CMakeLists.txt) | 0 | ||||
-rw-r--r-- | src/plugin/module/version_checking_plugin/SoftwareVersion.cpp (renamed from src/plugin/integrated_plugins/version_checking_plugin/SoftwareVersion.cpp) | 19 | ||||
-rw-r--r-- | src/plugin/module/version_checking_plugin/SoftwareVersion.h (renamed from src/plugin/integrated_plugins/version_checking_plugin/SoftwareVersion.h) | 8 | ||||
-rw-r--r-- | src/plugin/module/version_checking_plugin/VersionCheckTask.cpp (renamed from src/plugin/integrated_plugins/version_checking_plugin/VersionCheckTask.cpp) | 6 | ||||
-rw-r--r-- | src/plugin/module/version_checking_plugin/VersionCheckTask.h (renamed from src/plugin/integrated_plugins/version_checking_plugin/VersionCheckTask.h) | 9 | ||||
-rw-r--r-- | src/plugin/module/version_checking_plugin/VersionCheckingPlugin.cpp (renamed from src/plugin/integrated_plugins/version_checking_plugin/VersionCheckingPlugin.cpp) | 19 | ||||
-rw-r--r-- | src/plugin/module/version_checking_plugin/VersionCheckingPlugin.h (renamed from src/plugin/integrated_plugins/version_checking_plugin/VersionCheckingPlugin.h) | 12 | ||||
-rw-r--r-- | src/plugin/sdk/Basic.h | 4 | ||||
-rw-r--r-- | src/plugin/sdk/Gpg.h | 4 | ||||
-rw-r--r-- | src/plugin/sdk/GpgFrontendPluginSDK.h | 5 | ||||
-rw-r--r-- | src/plugin/sdk/GpgFrontendPluginSDKExport.h | 29 | ||||
-rw-r--r-- | src/plugin/sdk/Plugin.h | 79 | ||||
-rw-r--r-- | src/plugin/sdk/Task.h | 80 | ||||
-rw-r--r-- | src/plugin/sdk/UI.h | 4 | ||||
-rw-r--r-- | src/plugin/system/Event.cpp | 108 | ||||
-rw-r--r-- | src/plugin/system/Event.h (renamed from src/core/plugin/Event.h) | 60 | ||||
-rw-r--r-- | src/plugin/system/GlobalPluginContext.cpp | 336 | ||||
-rw-r--r-- | src/plugin/system/GlobalPluginContext.h (renamed from src/core/plugin/GlobalPluginContext.h) | 46 | ||||
-rw-r--r-- | src/plugin/system/GpgFrontendPluginSystem.h (renamed from src/plugin/sdk/Plugin.cpp) | 38 | ||||
-rw-r--r-- | src/plugin/system/GpgFrontendPluginSystemExport.h | 42 | ||||
-rw-r--r-- | src/plugin/system/Plugin.cpp | 103 | ||||
-rw-r--r-- | src/plugin/system/Plugin.h (renamed from src/core/plugin/Plugin.h) | 41 | ||||
-rw-r--r-- | src/plugin/system/PluginManager.cpp | 109 | ||||
-rw-r--r-- | src/plugin/system/PluginManager.h (renamed from src/core/plugin/PluginManager.h) | 12 |
36 files changed, 964 insertions, 1042 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 776fbdb7..36165e73 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -93,6 +93,7 @@ endif () if (BUILD_PLUGIN_MODULE) message("[*] Build Plugin Module") + add_subdirectory(plugin) endif () if (BUILD_APPLICATION) diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index c58cabcb..96dd7ffe 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -31,7 +31,6 @@ aux_source_directory(./function CORE_SOURCE) aux_source_directory(./thread CORE_SOURCE) aux_source_directory(./model CORE_SOURCE) aux_source_directory(./common CORE_SOURCE) -aux_source_directory(./plugin CORE_SOURCE) aux_source_directory(. CORE_SOURCE) # define libgpgfrontend_core diff --git a/src/core/plugin/Event.cpp b/src/core/plugin/Event.cpp deleted file mode 100644 index 2ca7d4ad..00000000 --- a/src/core/plugin/Event.cpp +++ /dev/null @@ -1,61 +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 "Event.h" - -namespace GpgFrontend::Plugin { - -Event::Event(const std::string& event_dientifier, - std::initializer_list<ParameterInitializer> params_init_list) - : event_identifier_(event_dientifier) { - for (const auto& param : params_init_list) { - AddParameter(param); - } -} - -bool Event::Event::operator==(const Event& other) const { - return event_identifier_ == other.event_identifier_; -} - -bool Event::Event::operator!=(const Event& other) const { - return !(*this == other); -} - -bool Event::Event::operator<(const Event& other) const { - return !(*this < other); -} - -bool Event::Event::operator<=(const Event& other) const { - return !(*this <= other); -} - -Event::Event::operator std::string() const { return event_identifier_; } - -EventIdentifier Event::Event::GetIdentifier() { return event_identifier_; } - -} // namespace GpgFrontend::Plugin
\ No newline at end of file diff --git a/src/core/plugin/GlobalPluginContext.cpp b/src/core/plugin/GlobalPluginContext.cpp deleted file mode 100644 index 365d0e2d..00000000 --- a/src/core/plugin/GlobalPluginContext.cpp +++ /dev/null @@ -1,265 +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 "GlobalPluginContext.h" - -#include <memory> -#include <optional> -#include <unordered_set> - -#include "GpgConstants.h" -#include "plugin/Event.h" -#include "plugin/Plugin.h" -#include "spdlog/spdlog.h" -#include "thread/Task.h" - -namespace GpgFrontend::Plugin { - -// Constructor for GlobalPluginContext, takes a TaskRunnerPtr as an argument. -GlobalPluginContext::GlobalPluginContext(TaskRunnerPtr task_runner) - : default_task_runner_(task_runner), - random_gen_((boost::posix_time::microsec_clock::universal_time() - - boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))) - .total_milliseconds()) { - // Initialize acquired channels with default values. - acquired_channel_.insert(GPGFRONTEND_DEFAULT_CHANNEL); - acquired_channel_.insert(GPGFRONTEND_NON_ASCII_CHANNEL); -} - -// Function to acquire a new unique channel. -int GlobalPluginContext::acquire_new_unique_channel() { - boost::random::uniform_int_distribution<> dist(1, 65535); - - int random_channel = dist(random_gen_); - // Ensure the acquired channel is unique. - while (acquired_channel_.find(random_channel) != acquired_channel_.end()) { - random_channel = dist(random_gen_); - } - - // Add the acquired channel to the set. - acquired_channel_.insert(random_channel); - return random_channel; -} - -// Function to search for a plugin in the register table. -std::optional<PluginRegisterInfoPtr> -GlobalPluginContext::search_plugin_register_table(PluginIdentifier identifier) { - auto it = plugin_register_table_.find(identifier); - if (it == plugin_register_table_.end()) { - return std::nullopt; - } - return it->second; -} - -// Function to get the task runner associated with a plugin. -std::optional<TaskRunnerPtr> GlobalPluginContext::GetTaskRunner( - PluginPtr plugin) { - auto opt = search_plugin_register_table(plugin->gpc_get_identifier()); - if (!opt.has_value()) { - return std::nullopt; - } - return opt.value()->task_runner; -} - -// Function to get the task runner associated with a plugin. -std::optional<TaskRunnerPtr> GlobalPluginContext::GetTaskRunner( - PluginIdentifier plugin) { - // Search for the plugin in the register table. - auto plugin_info_opt = search_plugin_register_table(plugin); - if (!plugin_info_opt.has_value()) { - SPDLOG_ERROR("cannot find plugin id {} at register table", plugin); - return std::nullopt; - } - return plugin_info_opt.value()->task_runner; -} - -// Function to get the global task runner. -std::optional<TaskRunnerPtr> GlobalPluginContext::GetGlobalTaskRunner() { - return default_task_runner_; -} - -bool GlobalPluginContext::RegisterPlugin(PluginPtr plugin) { - SPDLOG_DEBUG("attempting to register plugin: {}", - plugin->gpc_get_identifier()); - // Check if the plugin is null or already registered. - if (plugin == nullptr || - plugin_register_table_.find(plugin->gpc_get_identifier()) != - plugin_register_table_.end()) { - SPDLOG_ERROR("plugin is null or have already registered this plugin"); - return false; - } - - PluginRegisterInfo register_info; - register_info.plugin = plugin; - register_info.channel = acquire_new_unique_channel(); - register_info.task_runner = std::make_shared<Thread::TaskRunner>(); - - // Register the plugin with its identifier. - plugin_register_table_[plugin->gpc_get_identifier()] = - std::make_shared<PluginRegisterInfo>(std::move(register_info)); - - SPDLOG_DEBUG("successfully registered plugin: {}", - plugin->gpc_get_identifier()); - return true; -} - -bool GlobalPluginContext::ActivePlugin(PluginIdentifier plugin_id) { - SPDLOG_DEBUG("attempting to activate plugin: {}", plugin_id); - - // Search for the plugin in the register table. - auto plugin_info_opt = search_plugin_register_table(plugin_id); - if (!plugin_info_opt.has_value()) { - SPDLOG_ERROR("cannot find plugin id {} at register table", plugin_id); - return false; - } - - auto plugin_info = plugin_info_opt.value(); - // Activate the plugin if it is not already active. - if (plugin_info->activate && plugin_info->plugin->Active()) { - plugin_info->activate = true; - } - - SPDLOG_DEBUG("plugin activation status: {}", plugin_info->activate); - return plugin_info->activate; -} - -bool GlobalPluginContext::ListenEvent(PluginIdentifier plugin_id, - EventIdentifier event) { - SPDLOG_DEBUG("plugin: {} is attempting to listen to event {}", plugin_id, - event); - // Check if the event exists, if not, create it. - auto it = plugin_events_table_.find(event); - if (it == plugin_events_table_.end()) { - plugin_events_table_[event] = std::unordered_set<PluginIdentifier>(); - it = plugin_events_table_.find(event); - SPDLOG_INFO("new event {} of plugin system created", event); - } - - auto& listeners_set = it->second; - // Add the listener (plugin) to the event. - auto listener_it = - std::find(listeners_set.begin(), listeners_set.end(), plugin_id); - if (listener_it == listeners_set.end()) { - listeners_set.insert(plugin_id); - } - return true; -} - -bool GlobalPluginContext::DeactivatePlugin(PluginIdentifier plugin_id) { - // Search for the plugin in the register table. - auto plugin_info_opt = search_plugin_register_table(plugin_id); - if (!plugin_info_opt.has_value()) { - SPDLOG_ERROR("cannot find plugin id {} at register table", plugin_id); - return false; - } - - auto plugin_info = plugin_info_opt.value(); - // Activate the plugin if it is not already deactive. - if (!plugin_info->activate && plugin_info->plugin->Deactive()) { - plugin_info->activate = false; - } - - return !plugin_info->activate; -} - -bool GlobalPluginContext::TriggerEvent(EventRefrernce event) { - SPDLOG_DEBUG("attempting to trigger event: {}", event->GetIdentifier()); - - // Find the set of listeners associated with the given event in the table - auto it = plugin_events_table_.find(event->GetIdentifier()); - if (it == plugin_events_table_.end()) { - // Log a warning if the event is not registered and nobody is listening - SPDLOG_WARN( - "event {} is not listening by anyone and not registered as well", - event->GetIdentifier()); - return false; - } - - // Retrieve the set of listeners for this event - auto& listeners_set = it->second; - - // Check if the set of listeners is empty - if (listeners_set.empty()) { - // Log a warning if nobody is listening to this event - SPDLOG_WARN("event {} is not listening by anyone", event->GetIdentifier()); - return false; - } - - // Log the number of listeners for this event - SPDLOG_DEBUG("event {}'s current listeners size: {}", event->GetIdentifier(), - listeners_set.size()); - - // Iterate through each listener and execute the corresponding plugin - for (auto& listener_plugin_id : listeners_set) { - // Search for the plugin's information in the registration table - auto plugin_info_opt = search_plugin_register_table(listener_plugin_id); - - // Log an error if the plugin is not found in the registration table - if (!plugin_info_opt.has_value()) { - SPDLOG_ERROR("cannot find plugin id {} at register table", - listener_plugin_id); - } - - // Retrieve the plugin's information - auto plugin_info = plugin_info_opt.value(); - - // Check if the plugin is activated - if (!plugin_info->activate) continue; - - // Execute the plugin and check if it fails - if (plugin_info->plugin->Exec(event)) { - // Log an error if the plugin execution fails - SPDLOG_ERROR("plugin {} executed failed", listener_plugin_id); - } - } - - // Return true to indicate successful execution of all plugins - return true; -} - -int GlobalPluginContext::GetChannel(PluginPtr plugin) { - // Search for the plugin in the register table. - auto plugin_info_opt = - search_plugin_register_table(plugin->gpc_get_identifier()); - if (!plugin_info_opt.has_value()) { - SPDLOG_ERROR( - "cannot find plugin id {} at register table, fallbacking to default " - "channel", - plugin->gpc_get_identifier()); - return GetDefaultChannel(plugin); - } - - auto plugin_info = plugin_info_opt.value(); - return plugin_info->channel; -} - -int GlobalPluginContext::GetDefaultChannel(PluginPtr) { - return GPGFRONTEND_DEFAULT_CHANNEL; -} - -} // namespace GpgFrontend::Plugin diff --git a/src/core/plugin/Plugin.cpp b/src/core/plugin/Plugin.cpp deleted file mode 100644 index e1a35d9a..00000000 --- a/src/core/plugin/Plugin.cpp +++ /dev/null @@ -1,72 +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 "Plugin.h" - -#include "GpgConstants.h" -#include "core/GpgFrontendCore.h" -#include "core/plugin/GlobalPluginContext.h" - -namespace GpgFrontend::Plugin { - -Plugin::Plugin(PluginIdentifier id, PluginVersion version, - PluginMetaData meta_data) - : self_shared_ptr_(this), - identifier_((boost::format("__plugin_%1%") % id).str()), - version_(version), - meta_data_(meta_data) {} - -const GlobalPluginContextPtr -GpgFrontend::Plugin::Plugin::get_global_plugin_context() { - if (global_plugin_context_ == nullptr) { - throw std::runtime_error("plugin is not registered by plugin manager"); - } - return global_plugin_context_; -} - -int Plugin::getChannel() { - return get_global_plugin_context()->GetChannel(self_shared_ptr_); -} - -int Plugin::getDefaultChannel() { - return get_global_plugin_context()->GetDefaultChannel(self_shared_ptr_); -} - -std::optional<TaskRunnerPtr> Plugin::getTaskRunner() { - return get_global_plugin_context()->GetTaskRunner(self_shared_ptr_); -} - -bool Plugin::listenEvent(EventIdentifier event) { - return get_global_plugin_context()->ListenEvent(gpc_get_identifier(), event); -} - -PluginIdentifier Plugin::GetPluginIdentifier() const { return identifier_; } - -PluginIdentifier Plugin::gpc_get_identifier() { return identifier_; } - -} // namespace GpgFrontend::Plugin
\ No newline at end of file diff --git a/src/core/plugin/PluginManager.cpp b/src/core/plugin/PluginManager.cpp deleted file mode 100644 index b56f7fc8..00000000 --- a/src/core/plugin/PluginManager.cpp +++ /dev/null @@ -1,87 +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 "PluginManager.h" - -#include <memory> - -#include "core/plugin/Event.h" -#include "core/plugin/GlobalPluginContext.h" -#include "core/plugin/Plugin.h" -#include "core/thread/TaskRunner.h" - -namespace GpgFrontend::Plugin { - -PluginMangerPtr PluginManager::global_plugin_manager_ = nullptr; - -PluginManager::PluginManager() - : task_runner_(std::make_shared<Thread::TaskRunner>()), - global_plugin_context_( - std::make_shared<GlobalPluginContext>(task_runner_)) {} - -PluginMangerPtr PluginManager::GetInstance() { - if (global_plugin_manager_ == nullptr) { - global_plugin_manager_ = - std::shared_ptr<PluginManager>(new PluginManager()); - } - return global_plugin_manager_; -} - -void PluginManager::RegisterPlugin(PluginPtr plugin) { - task_runner_->PostTask(new Thread::Task( - std::move([=](GpgFrontend::Thread::Task::DataObjectPtr) -> int { - global_plugin_context_->RegisterPlugin(plugin); - return 0; - }), - __func__, nullptr, true)); -} - -void PluginManager::TriggerEvent(EventRefrernce event) { - task_runner_->PostTask(new Thread::Task( - std::move([=](GpgFrontend::Thread::Task::DataObjectPtr) -> int { - global_plugin_context_->TriggerEvent(event); - return 0; - }), - __func__, nullptr, true)); -} - -void PluginManager::ActivePlugin(PluginIdentifier identifier) { - task_runner_->PostTask(new Thread::Task( - std::move([=](GpgFrontend::Thread::Task::DataObjectPtr) -> int { - global_plugin_context_->ActivePlugin(identifier); - return 0; - }), - __func__, nullptr, true)); -} - -std::optional<TaskRunnerPtr> PluginManager::GetTaskRunner( - PluginIdentifier plugin_id) { - return global_plugin_context_->GetTaskRunner(plugin_id); -} - -} // namespace GpgFrontend::Plugin
\ No newline at end of file diff --git a/src/plugin/sdk/Task.cpp b/src/core/thread/DataObject.cpp index 96ccfd45..c99e0bfd 100644 --- a/src/plugin/sdk/Task.cpp +++ b/src/core/thread/DataObject.cpp @@ -26,37 +26,4 @@ * */ -#include "Task.h" - -#include "core/thread/Task.h" - -namespace GpgFrontend::Plugin::SDK { -/** - * @brief Construct a new Task object - * - */ -STask::STask(STaskName name) : Thread::Task(name) {} - -/** - * @brief Construct a new Task object - * - * @param callback The callback function to be executed. - */ -STask::STask(STaskName name, STaskRunnable runnable, SDataObjectPtr data_object) - : Thread::Task(runnable, name, data_object, true) {} - -/** - * @brief Construct a new Task object - * - * @param runnable - */ -STask::STask(STaskName name, STaskRunnable runnable, SDataObjectPtr data_object, - STaskCallback callback) - : Thread::Task(runnable, name, data_object, callback, true) {} - -void PostTask(Thread::TaskRunner *runner, STask *task) { - if (runner == nullptr || task == nullptr) return; - runner->PostTask(task); -} - -} // namespace GpgFrontend::Plugin::SDK
\ No newline at end of file +#include "DataObject.h"
\ No newline at end of file diff --git a/src/core/thread/DataObject.h b/src/core/thread/DataObject.h new file mode 100644 index 00000000..885b09ec --- /dev/null +++ b/src/core/thread/DataObject.h @@ -0,0 +1,137 @@ +/** + * 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 DataObject to be passed to the callback function. + * + */ + class GPGFRONTEND_CORE_EXPORT DataObject { + public: + struct Destructor { + const void *p_obj; + void (*destroy)(const void *); + }; + + /** + * @brief Get the Objects Size + * + * @return size_t + */ + size_t GetObjectSize(); + + /** + * @brief + * + * @tparam T + * @param ptr + */ + template <typename T> + void AppendObject(T &&obj) { + SPDLOG_TRACE("append object: {}", static_cast<void *>(this)); + auto *obj_dstr = this->get_heap_ptr(sizeof(T)); + new ((void *)obj_dstr->p_obj) T(std::forward<T>(obj)); + + if (std::is_class_v<T>) { + auto destructor = [](const void *x) { + static_cast<const T *>(x)->~T(); + }; + obj_dstr->destroy = destructor; + } else { + obj_dstr->destroy = nullptr; + } + + data_objects_.push(obj_dstr); + } + + /** + * @brief + * + * @tparam T + * @param ptr + */ + template <typename T> + void AppendObject(T *obj) { + SPDLOG_TRACE("called: {}", static_cast<void *>(this)); + auto *obj_dstr = this->get_heap_ptr(sizeof(T)); + auto *ptr_heap = new ((void *)obj_dstr->p_obj) T(std::move(*obj)); + if (std::is_class_v<T>) { + SPDLOG_TRACE("is class"); + auto destructor = [](const void *x) { + static_cast<const T *>(x)->~T(); + }; + obj_dstr->destroy = destructor; + } else { + obj_dstr->destroy = nullptr; + } + data_objects_.push(std::move(obj_dstr)); + } + + /** + * @brief + * + * @tparam T + * @return std::shared_ptr<T> + */ + template <typename T> + T PopObject() { + SPDLOG_TRACE("pop object: {}", static_cast<void *>(this)); + if (data_objects_.empty()) throw std::runtime_error("No object to pop"); + auto *obj_dstr = data_objects_.top(); + auto *heap_ptr = (T *)obj_dstr->p_obj; + auto obj = std::move(*(T *)(heap_ptr)); + this->free_heap_ptr(obj_dstr); + data_objects_.pop(); + return obj; + } + + /** + * @brief Destroy the Data Object object + * + */ + ~DataObject(); + + private: + std::stack<Destructor *> data_objects_; ///< + + /** + * @brief Get the heap ptr object + * + * @param bytes_size + * @return void* + */ + Destructor *get_heap_ptr(size_t bytes_size); + + /** + * @brief + * + * @param heap_ptr + */ + void free_heap_ptr(Destructor *); + };
\ No newline at end of file diff --git a/src/core/thread/Task.h b/src/core/thread/Task.h index e6a56219..2e812c3f 100644 --- a/src/core/thread/Task.h +++ b/src/core/thread/Task.h @@ -55,114 +55,6 @@ class GPGFRONTEND_CORE_EXPORT Task : public QObject, public QRunnable { friend class TaskRunner; /** - * @brief DataObject to be passed to the callback function. - * - */ - class GPGFRONTEND_CORE_EXPORT DataObject { - public: - struct Destructor { - const void *p_obj; - void (*destroy)(const void *); - }; - - /** - * @brief Get the Objects Size - * - * @return size_t - */ - size_t GetObjectSize(); - - /** - * @brief - * - * @tparam T - * @param ptr - */ - template <typename T> - void AppendObject(T &&obj) { - SPDLOG_TRACE("append object: {}", static_cast<void *>(this)); - auto *obj_dstr = this->get_heap_ptr(sizeof(T)); - new ((void *)obj_dstr->p_obj) T(std::forward<T>(obj)); - - if (std::is_class_v<T>) { - auto destructor = [](const void *x) { - static_cast<const T *>(x)->~T(); - }; - obj_dstr->destroy = destructor; - } else { - obj_dstr->destroy = nullptr; - } - - data_objects_.push(obj_dstr); - } - - /** - * @brief - * - * @tparam T - * @param ptr - */ - template <typename T> - void AppendObject(T *obj) { - SPDLOG_TRACE("called: {}", static_cast<void *>(this)); - auto *obj_dstr = this->get_heap_ptr(sizeof(T)); - auto *ptr_heap = new ((void *)obj_dstr->p_obj) T(std::move(*obj)); - if (std::is_class_v<T>) { - SPDLOG_TRACE("is class"); - auto destructor = [](const void *x) { - static_cast<const T *>(x)->~T(); - }; - obj_dstr->destroy = destructor; - } else { - obj_dstr->destroy = nullptr; - } - data_objects_.push(std::move(obj_dstr)); - } - - /** - * @brief - * - * @tparam T - * @return std::shared_ptr<T> - */ - template <typename T> - T PopObject() { - SPDLOG_TRACE("pop object: {}", static_cast<void *>(this)); - if (data_objects_.empty()) throw std::runtime_error("No object to pop"); - auto *obj_dstr = data_objects_.top(); - auto *heap_ptr = (T *)obj_dstr->p_obj; - auto obj = std::move(*(T *)(heap_ptr)); - this->free_heap_ptr(obj_dstr); - data_objects_.pop(); - return obj; - } - - /** - * @brief Destroy the Data Object object - * - */ - ~DataObject(); - - private: - std::stack<Destructor *> data_objects_; ///< - - /** - * @brief Get the heap ptr object - * - * @param bytes_size - * @return void* - */ - Destructor *get_heap_ptr(size_t bytes_size); - - /** - * @brief - * - * @param heap_ptr - */ - void free_heap_ptr(Destructor *); - }; - - /** * @brief Construct a new Task object * */ @@ -256,6 +148,8 @@ class GPGFRONTEND_CORE_EXPORT Task : public QObject, public QRunnable { void SetRTN(int rtn); private: + class Impl; + std::unique_ptr<Impl>; const std::string uuid_; const std::string name_; const bool sequency_ = true; ///< must run in the same thread diff --git a/src/core/thread/TaskRunnerGetter.cpp b/src/core/thread/TaskRunnerGetter.cpp index 3888b439..9288c2cd 100644 --- a/src/core/thread/TaskRunnerGetter.cpp +++ b/src/core/thread/TaskRunnerGetter.cpp @@ -45,4 +45,4 @@ GpgFrontend::Thread::TaskRunnerGetter::GetTaskRunner( continue; } } -} +}
\ No newline at end of file diff --git a/src/plugin/CMakeLists.txt b/src/plugin/CMakeLists.txt index a844d54f..de5bd7ac 100644 --- a/src/plugin/CMakeLists.txt +++ b/src/plugin/CMakeLists.txt @@ -24,31 +24,42 @@ # SPDX-License-Identifier: GPL-3.0-or-later -# define libgpgfrontend_plugin_sdk -aux_source_directory(sdk PLUGIN_SDK_SOURCE) -add_library(gpgfrontend_plugin_sdk SHARED ${PLUGIN_SDK_SOURCE}) -set(_export_file "${CMAKE_CURRENT_SOURCE_DIR}/sdk/GpgFrontendPluginSDKExport.h") -generate_export_header(gpgfrontend_plugin_sdk EXPORT_FILE_NAME "${_export_file}") -target_include_directories(gpgfrontend_plugin_sdk PUBLIC sdk) +# define libgpgfrontend_plugin_system +aux_source_directory(system PLUGIN_SYSTEM_SOURCE) +add_library(gpgfrontend_plugin_system SHARED ${PLUGIN_SYSTEM_SOURCE}) +set(_export_file_system "${CMAKE_CURRENT_SOURCE_DIR}/system/GpgFrontendPluginSystemExport.h") +generate_export_header(gpgfrontend_plugin_system EXPORT_FILE_NAME "${_export_file_system}") # link json -target_link_libraries(gpgfrontend_plugin_sdk +target_link_libraries(gpgfrontend_plugin_system PUBLIC nlohmann_json::nlohmann_json) # spdlog -target_link_libraries(gpgfrontend_plugin_sdk PUBLIC spdlog) +target_link_libraries(gpgfrontend_plugin_system PUBLIC spdlog) # link Qt core if(Qt6_DIR) - target_link_libraries(gpgfrontend_plugin_sdk PUBLIC Qt6::Core) + target_link_libraries(gpgfrontend_plugin_system PUBLIC Qt6::Core) else() - target_link_libraries(gpgfrontend_plugin_sdk PUBLIC Qt5::Core) + target_link_libraries(gpgfrontend_plugin_system PUBLIC Qt5::Core) endif() # link core and ui module -target_link_libraries(gpgfrontend_plugin_sdk PRIVATE +target_link_libraries(gpgfrontend_plugin_system PUBLIC gpgfrontend_core gpgfrontend_ui) +# define libgpgfrontend_plugin_sdk +aux_source_directory(sdk PLUGIN_SDK_SOURCE) + +add_library(gpgfrontend_plugin_sdk SHARED ${PLUGIN_SDK_SOURCE}) +set(_export_file_sdk "${CMAKE_CURRENT_SOURCE_DIR}/sdk/GpgFrontendPluginSDKExport.h") +generate_export_header(gpgfrontend_plugin_sdk EXPORT_FILE_NAME "${_export_file_sdk}") +target_include_directories(gpgfrontend_plugin_sdk PUBLIC sdk) + +# link plugin system +target_link_libraries(gpgfrontend_plugin_sdk + PRIVATE gpgfrontend_plugin_system) + # tracking integrated plugins set(all_integrated_plugins_libraries "") file(GLOB children LIST_DIRECTORIES true "integrated_plugins/*") diff --git a/src/plugin/GpgFrontendPluginExport.h b/src/plugin/GpgFrontendPluginExport.h index 7c908150..a0eaa86c 100644 --- a/src/plugin/GpgFrontendPluginExport.h +++ b/src/plugin/GpgFrontendPluginExport.h @@ -1,31 +1,4 @@ -/** - * 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 - * - */ - + #ifndef GPGFRONTEND_PLUGIN_EXPORT_H #define GPGFRONTEND_PLUGIN_EXPORT_H diff --git a/src/plugin/integrated_plugins/version_checking_plugin/CMakeLists.txt b/src/plugin/module/version_checking_plugin/CMakeLists.txt index 85d533da..85d533da 100644 --- a/src/plugin/integrated_plugins/version_checking_plugin/CMakeLists.txt +++ b/src/plugin/module/version_checking_plugin/CMakeLists.txt diff --git a/src/plugin/integrated_plugins/version_checking_plugin/SoftwareVersion.cpp b/src/plugin/module/version_checking_plugin/SoftwareVersion.cpp index ae366d68..8626c822 100644 --- a/src/plugin/integrated_plugins/version_checking_plugin/SoftwareVersion.cpp +++ b/src/plugin/module/version_checking_plugin/SoftwareVersion.cpp @@ -28,9 +28,10 @@ #include "SoftwareVersion.h" -int GpgFrontend::Plugin::IntegradedPlugin::VersionCheckingPlugin:: - SoftwareVersion::version_compare(const std::string& a, - const std::string& b) { +namespace GpgFrontend::Plugin::Module { + +int VersionCheckingPlugin::SoftwareVersion::version_compare( + const std::string& a, const std::string& b) { auto temp_a = a, temp_b = b; if (!temp_a.empty() && temp_a.front() == 'v') { @@ -81,8 +82,7 @@ int GpgFrontend::Plugin::IntegradedPlugin::VersionCheckingPlugin:: return 0; } -bool GpgFrontend::Plugin::IntegradedPlugin::VersionCheckingPlugin:: - SoftwareVersion::NeedUpgrade() const { +bool VersionCheckingPlugin::SoftwareVersion::NeedUpgrade() const { SPDLOG_DEBUG("compair version current {} latest {}, result {}", current_version, latest_version, version_compare(current_version, latest_version)); @@ -93,13 +93,12 @@ bool GpgFrontend::Plugin::IntegradedPlugin::VersionCheckingPlugin:: version_compare(current_version, latest_version) < 0; } -bool GpgFrontend::Plugin::IntegradedPlugin::VersionCheckingPlugin:: - SoftwareVersion::VersionWithDrawn() const { +bool VersionCheckingPlugin::SoftwareVersion::VersionWithDrawn() const { return load_info_done && !current_version_found && current_prerelease && !current_draft; } -bool GpgFrontend::Plugin::IntegradedPlugin::VersionCheckingPlugin:: - SoftwareVersion::CurrentVersionReleased() const { +bool VersionCheckingPlugin::SoftwareVersion::CurrentVersionReleased() const { return load_info_done && current_version_found; -}
\ No newline at end of file +} +} // namespace GpgFrontend::Plugin::Module
\ No newline at end of file diff --git a/src/plugin/integrated_plugins/version_checking_plugin/SoftwareVersion.h b/src/plugin/module/version_checking_plugin/SoftwareVersion.h index ac2d6da1..6aac3894 100644 --- a/src/plugin/integrated_plugins/version_checking_plugin/SoftwareVersion.h +++ b/src/plugin/module/version_checking_plugin/SoftwareVersion.h @@ -29,11 +29,9 @@ #ifndef GPGFRONTEND_PLUGIN_SOFTWAREVERSION_H #define GPGFRONTEND_PLUGIN_SOFTWAREVERSION_H -#include <GpgFrontendPluginSDK.h> +#include <plugin/sdk/GpgFrontendPluginSDK.h> -#include <boost/date_time.hpp> - -namespace GpgFrontend::Plugin::IntegradedPlugin::VersionCheckingPlugin { +namespace GpgFrontend::Plugin::Module::VersionCheckingPlugin { /** * @brief * @@ -85,6 +83,6 @@ struct SoftwareVersion { private: static int version_compare(const std::string& a, const std::string& b); }; -} // namespace GpgFrontend::Plugin::IntegradedPlugin::VersionCheckingPlugin +} // namespace GpgFrontend::Plugin::Module::VersionCheckingPlugin #endif // GPGFRONTEND_PLUGIN_SOFTWAREVERSION_H diff --git a/src/plugin/integrated_plugins/version_checking_plugin/VersionCheckTask.cpp b/src/plugin/module/version_checking_plugin/VersionCheckTask.cpp index e798f008..2a26a53c 100644 --- a/src/plugin/integrated_plugins/version_checking_plugin/VersionCheckTask.cpp +++ b/src/plugin/module/version_checking_plugin/VersionCheckTask.cpp @@ -34,10 +34,10 @@ #include "GpgFrontendBuildInfo.h" -namespace GpgFrontend::Plugin::IntegradedPlugin::VersionCheckingPlugin { +namespace GpgFrontend::Plugin::Module::VersionCheckingPlugin { VersionCheckTask::VersionCheckTask() - : STask("version_check_task"), + : Task("version_check_task"), network_manager_(new QNetworkAccessManager(this)), current_version_(std::string("v") + std::to_string(VERSION_MAJOR) + "." + std::to_string(VERSION_MINOR) + "." + @@ -176,4 +176,4 @@ void VersionCheckTask::slot_parse_current_version_info() { emit SignalTaskRunnableEnd(0); } -} // namespace GpgFrontend::Plugin::IntegradedPlugin::VersionCheckingPlugin +} // namespace GpgFrontend::Plugin::Module::VersionCheckingPlugin diff --git a/src/plugin/integrated_plugins/version_checking_plugin/VersionCheckTask.h b/src/plugin/module/version_checking_plugin/VersionCheckTask.h index 021d1e66..d5155cd9 100644 --- a/src/plugin/integrated_plugins/version_checking_plugin/VersionCheckTask.h +++ b/src/plugin/module/version_checking_plugin/VersionCheckTask.h @@ -29,21 +29,20 @@ #ifndef GPGFRONTEND_VERSIONCHECKTHREAD_H #define GPGFRONTEND_VERSIONCHECKTHREAD_H -#include <GpgFrontendPluginSDK.h> -#include <Task.h> +#include <plugin/sdk/GpgFrontendPluginSDK.h> #include "SoftwareVersion.h" class QNetworkReply; class QNetworkAccessManager; -namespace GpgFrontend::Plugin::IntegradedPlugin::VersionCheckingPlugin { +namespace GpgFrontend::Plugin::Module::VersionCheckingPlugin { /** * @brief * */ -class VersionCheckTask : public SDK::STask { +class VersionCheckTask : public Thread::Task { Q_OBJECT public: /** @@ -93,7 +92,7 @@ class VersionCheckTask : public SDK::STask { SoftwareVersion version_; }; -} // namespace GpgFrontend::Plugin::IntegradedPlugin::VersionCheckingPlugin +} // namespace GpgFrontend::Plugin::Module::VersionCheckingPlugin // GpgFrontend::Plugin::Custom::IntegradedPlugin::VersionCheckingPlugin #endif // GPGFRONTEND_VERSIONCHECKTHREAD_H diff --git a/src/plugin/integrated_plugins/version_checking_plugin/VersionCheckingPlugin.cpp b/src/plugin/module/version_checking_plugin/VersionCheckingPlugin.cpp index b63cbe91..4d18b01c 100644 --- a/src/plugin/integrated_plugins/version_checking_plugin/VersionCheckingPlugin.cpp +++ b/src/plugin/module/version_checking_plugin/VersionCheckingPlugin.cpp @@ -28,18 +28,14 @@ #include "VersionCheckingPlugin.h" -#include "Plugin.h" -#include "Task.h" #include "VersionCheckTask.h" -namespace GpgFrontend::Plugin::IntegradedPlugin::VersionCheckingPlugin { +namespace GpgFrontend::Plugin::Module::VersionCheckingPlugin { VersionCheckingPlugin::VersionCheckingPlugin() - : SPlugin("com.bktus.gpgfrontend.plugin.integrated.versionchecking", - "1.0.0", - SDK::SPluginMetaData{ - {"description", "try to check gpgfrontend version"}, - {"author", "saturneric"}}) {} + : Plugin("com.bktus.gpgfrontend.plugin.integrated.versionchecking", "1.0.0", + PluginMetaData{{"description", "try to check gpgfrontend version"}, + {"author", "saturneric"}}) {} bool VersionCheckingPlugin::Register() { SPDLOG_INFO("version checking plugin registering"); @@ -53,11 +49,12 @@ bool VersionCheckingPlugin::Active() { return true; } -int VersionCheckingPlugin::Exec(SDK::SEventRefrernce event) { +int VersionCheckingPlugin::Exec(EventRefrernce event) { SPDLOG_INFO("version checking plugin ececuting"); - SDK::PostTask(SDK::GetPluginTaskRunner(this), new VersionCheckTask()); + + getTaskRunner()->PostTask(new VersionCheckTask()); return 0; } bool VersionCheckingPlugin::Deactive() { return true; } -} // namespace GpgFrontend::Plugin::IntegradedPlugin::VersionCheckingPlugin +} // namespace GpgFrontend::Plugin::Module::VersionCheckingPlugin diff --git a/src/plugin/integrated_plugins/version_checking_plugin/VersionCheckingPlugin.h b/src/plugin/module/version_checking_plugin/VersionCheckingPlugin.h index 4a9f0512..7d489efd 100644 --- a/src/plugin/integrated_plugins/version_checking_plugin/VersionCheckingPlugin.h +++ b/src/plugin/module/version_checking_plugin/VersionCheckingPlugin.h @@ -29,15 +29,13 @@ #ifndef GPGFRONTEND_PLUGIN_VERSIONCHECKINGPLUGIN_H #define GPGFRONTEND_PLUGIN_VERSIONCHECKINGPLUGIN_H -#include <GpgFrontendPluginSDK.h> -#include <Plugin.h> +#include <plugin/sdk/GpgFrontendPluginSDK.h> #include "SoftwareVersion.h" -namespace GpgFrontend::Plugin::IntegradedPlugin::VersionCheckingPlugin { +namespace GpgFrontend::Plugin::Module::VersionCheckingPlugin { -class GPGFRONTEND_PLUGIN_SDK_EXPORT VersionCheckingPlugin - : public SDK::SPlugin { +class GPGFRONTEND_PLUGIN_SDK_EXPORT VersionCheckingPlugin : public Plugin { public: VersionCheckingPlugin(); @@ -45,10 +43,10 @@ class GPGFRONTEND_PLUGIN_SDK_EXPORT VersionCheckingPlugin virtual bool Active() override; - virtual int Exec(SDK::SEventRefrernce) override; + virtual int Exec(EventRefrernce) override; virtual bool Deactive() override; }; -} // namespace GpgFrontend::Plugin::IntegradedPlugin::VersionCheckingPlugin +} // namespace GpgFrontend::Plugin::Module::VersionCheckingPlugin #endif // GPGFRONTEND_PLUGIN_VERSIONCHECKINGPLUGIN_H
\ No newline at end of file diff --git a/src/plugin/sdk/Basic.h b/src/plugin/sdk/Basic.h index 63859763..0702632a 100644 --- a/src/plugin/sdk/Basic.h +++ b/src/plugin/sdk/Basic.h @@ -24,4 +24,6 @@ * * SPDX-License-Identifier: GPL-3.0-or-later * - */
\ No newline at end of file + */ + +#pragma once
\ No newline at end of file diff --git a/src/plugin/sdk/Gpg.h b/src/plugin/sdk/Gpg.h index 63859763..0702632a 100644 --- a/src/plugin/sdk/Gpg.h +++ b/src/plugin/sdk/Gpg.h @@ -24,4 +24,6 @@ * * SPDX-License-Identifier: GPL-3.0-or-later * - */
\ No newline at end of file + */ + +#pragma once
\ No newline at end of file diff --git a/src/plugin/sdk/GpgFrontendPluginSDK.h b/src/plugin/sdk/GpgFrontendPluginSDK.h index 5d2ca1f1..4decf317 100644 --- a/src/plugin/sdk/GpgFrontendPluginSDK.h +++ b/src/plugin/sdk/GpgFrontendPluginSDK.h @@ -26,6 +26,7 @@ * */ -#include <core/GpgFrontendCore.h> +#pragma once -#include "GpgFrontendPluginSDKExport.h"
\ No newline at end of file +#include <plugin/sdk/GpgFrontendPluginSDKExport.h> +#include <plugin/system/GpgFrontendPluginSystem.h>
\ No newline at end of file diff --git a/src/plugin/sdk/GpgFrontendPluginSDKExport.h b/src/plugin/sdk/GpgFrontendPluginSDKExport.h index f54ed49e..8e508e35 100644 --- a/src/plugin/sdk/GpgFrontendPluginSDKExport.h +++ b/src/plugin/sdk/GpgFrontendPluginSDKExport.h @@ -1,31 +1,4 @@ -/** - * 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 - * - */ - + #ifndef GPGFRONTEND_PLUGIN_SDK_EXPORT_H #define GPGFRONTEND_PLUGIN_SDK_EXPORT_H diff --git a/src/plugin/sdk/Plugin.h b/src/plugin/sdk/Plugin.h deleted file mode 100644 index f9756b0b..00000000 --- a/src/plugin/sdk/Plugin.h +++ /dev/null @@ -1,79 +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 - * - */ - -#ifndef GPGFRONTEND_SDK_PLUGIN_H -#define GPGFRONTEND_SDK_PLUGIN_H - -#include <core/plugin/Plugin.h> -#include <core/plugin/PluginManager.h> - -#include "GpgFrontendPluginSDK.h" -#include "Task.h" - -namespace GpgFrontend::Plugin::SDK { - -class SPlugin; - -using SEventRefrernce = std::shared_ptr<Event>; -using SEventIdentifier = std::string; -using SPluginIdentifier = std::string; -using SPluginVersion = std::string; -using SPluginMetaData = std::map<std::string, std::string>; -using SEvnets = std::vector<Event>; -using GlobalPluginContextPtr = std::shared_ptr<GlobalPluginContext>; -using SPluginPtr = std::shared_ptr<SPlugin>; -using SPluginList = std::list<std::string>; - -class GPGFRONTEND_PLUGIN_SDK_EXPORT SPlugin - : public GpgFrontend::Plugin::Plugin { - Q_OBJECT - public: - SPlugin(SPluginIdentifier, SPluginVersion, SPluginMetaData); - - virtual ~SPlugin() = default; - - SPluginIdentifier GetSPluginIdentifier() const; - - virtual bool Register() override; - - virtual bool Active() override; - - virtual int Exec(SEventRefrernce) override; - - virtual bool Deactive() override; -}; - -class GPGFRONTEND_PLUGIN_SDK_EXPORT SEvent - : protected GpgFrontend::Plugin::Event {}; - -bool GPGFRONTEND_PLUGIN_SDK_EXPORT ListenEvent(SPluginPtr, SEvent); - -Thread::TaskRunner* GetPluginTaskRunner(SPlugin*); -} // namespace GpgFrontend::Plugin::SDK - -#endif // GPGFRONTEND_SDK_PLUGIN_H
\ No newline at end of file diff --git a/src/plugin/sdk/Task.h b/src/plugin/sdk/Task.h deleted file mode 100644 index 60e63620..00000000 --- a/src/plugin/sdk/Task.h +++ /dev/null @@ -1,80 +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 - * - */ - -#ifndef GPGFRONTEND_SDK_TASK_H -#define GPGFRONTEND_SDK_TASK_H - -#include <string> - -#include "GpgFrontendPluginSDK.h" -#include "core/thread/Task.h" -#include "core/thread/TaskRunner.h" - -namespace GpgFrontend::Plugin::SDK { - -using STaskRunnerPtr = std::shared_ptr<Thread::TaskRunner>; - -class GPGFRONTEND_PLUGIN_SDK_EXPORT STask : public Thread::Task { - Q_OBJECT - public: - using STaskName = std::string; - using SDataObjectPtr = std::shared_ptr<DataObject>; ///< - using STaskRunnable = std::function<int(SDataObjectPtr)>; ///< - using STaskCallback = std::function<void(int, SDataObjectPtr)>; ///< - - /** - * @brief Construct a new Task object - * - */ - STask(STaskName name = DEFAULT_TASK_NAME); - - /** - * @brief Construct a new Task object - * - * @param callback The callback function to be executed. - */ - explicit STask(STaskName name, STaskRunnable runnable, - SDataObjectPtr data_object = nullptr); - - /** - * @brief Construct a new Task object - * - * @param runnable - */ - explicit STask( - STaskName name, STaskRunnable runnable, SDataObjectPtr data_object, - STaskCallback callback = [](int, const SDataObjectPtr &) {}); - - virtual ~STask() = default; -}; - -void PostTask(Thread::TaskRunner *, STask *); - -} // namespace GpgFrontend::Plugin::SDK - -#endif // GPGFRONTEND_SDK_TASK_H diff --git a/src/plugin/sdk/UI.h b/src/plugin/sdk/UI.h index 63859763..0702632a 100644 --- a/src/plugin/sdk/UI.h +++ b/src/plugin/sdk/UI.h @@ -24,4 +24,6 @@ * * SPDX-License-Identifier: GPL-3.0-or-later * - */
\ No newline at end of file + */ + +#pragma once
\ No newline at end of file diff --git a/src/plugin/system/Event.cpp b/src/plugin/system/Event.cpp new file mode 100644 index 00000000..b899e7e3 --- /dev/null +++ b/src/plugin/system/Event.cpp @@ -0,0 +1,108 @@ +/** + * 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 "Event.h" + +#include <memory> + +namespace GpgFrontend::Plugin { + +class Event::Impl { + public: + Impl(const std::string& event_dientifier, + std::initializer_list<ParameterInitializer> params_init_list = {}) + : event_identifier_(event_dientifier) { + for (const auto& param : params_init_list) { + AddParameter(param); + } + } + + std::optional<ParameterValue> operator[](const std::string& key) const { + auto it = data_.find(key); + if (it != data_.end()) { + return it->second; + } + return std::nullopt; + } + + bool operator==(const Event& other) const { + return event_identifier_ == other.p_->event_identifier_; + } + + bool operator!=(const Event& other) const { return !(*this == other); } + + bool operator<(const Event& other) const { + return this->event_identifier_ < other.p_->event_identifier_; + } + + operator std::string() const { return event_identifier_; } + + EventIdentifier GetIdentifier() { return event_identifier_; } + + void AddParameter(const std::string& key, const ParameterValue& value) { + data_[key] = value; + } + + void AddParameter(ParameterInitializer param) { + AddParameter(param.key, param.value); + } + + private: + EventIdentifier event_identifier_; + std::map<std::string, ParameterValue> data_; +}; + +Event::Event(const std::string& event_dientifier, + std::initializer_list<ParameterInitializer> params_init_list) + : p_(std::make_unique<Impl>(event_dientifier, params_init_list)) {} + +Event::~Event() = default; + +bool Event::Event::operator==(const Event& other) const { + return this->p_ == other.p_; +} + +bool Event::Event::operator!=(const Event& other) const { + return this->p_ != other.p_; +} + +bool Event::Event::operator<(const Event& other) const { + return this->p_ < other.p_; +} + +Event::Event::operator std::string() const { + return static_cast<std::string>(*p_); +} + +EventIdentifier Event::Event::GetIdentifier() { return p_->GetIdentifier(); } + +void Event::AddParameter(const std::string& key, const ParameterValue& value) { + p_->AddParameter(key, value); +} + +} // namespace GpgFrontend::Plugin
\ No newline at end of file diff --git a/src/core/plugin/Event.h b/src/plugin/system/Event.h index 8b6a537a..e9c1c78a 100644 --- a/src/core/plugin/Event.h +++ b/src/plugin/system/Event.h @@ -29,7 +29,11 @@ #ifndef GPGFRONTEND_EVENT_H #define GPGFRONTEND_EVENT_H +#include <memory> + +#include "GpgFrontendPluginSystemExport.h" #include "core/GpgFrontendCore.h" +#include "nlohmann/json_fwd.hpp" namespace GpgFrontend::Plugin { @@ -41,67 +45,37 @@ using Evnets = std::vector<Event>; class Event { public: - class ParameterBase { - public: - virtual ~ParameterBase() = default; - }; - + using ParameterValue = std::variant<int, float, std::string, nlohmann::json>; + using EventIdentifier = std::string; struct ParameterInitializer { std::string key; - std::shared_ptr<ParameterBase> value; + ParameterValue value; }; Event(const std::string& event_dientifier, std::initializer_list<ParameterInitializer> params_init_list = {}); - template <typename T> - std::optional<T> operator[](const std::string& key) const { - return GetParameter<T>(key); - } + ~Event(); + + std::optional<ParameterValue> operator[](const std::string& key) const; bool operator==(const Event& other) const; + bool operator!=(const Event& other) const; + bool operator<(const Event& other) const; + bool operator<=(const Event& other) const; + operator std::string() const; EventIdentifier GetIdentifier(); - template <typename T> - void AddParameter(const std::string& key, const T& value) { - data_[key] = std::make_shared<ParameterValue<T>>(value); - } - - void AddParameter(const ParameterInitializer& init) { - data_[init.key] = init.value; - } - - template <typename T> - std::optional<T> GetParameter(const std::string& key) const { - if (data_.find(key) == data_.end()) { - throw std::nullopt; - } - auto value = std::dynamic_pointer_cast<ParameterValue<T>>(data_.at(key)); - if (!value) { - throw std::nullopt; - } - return value->GetValue(); - } + void AddParameter(const std::string& key, const ParameterValue& value); private: - template <typename T> - class ParameterValue : public ParameterBase { - public: - ParameterValue(const T& value) : value_(value) {} - - T GetValue() const { return value_; } - - private: - T value_; - }; - - EventIdentifier event_identifier_; - std::map<std::string, std::shared_ptr<ParameterBase>> data_; + class Impl; + std::unique_ptr<Impl> p_; }; } // namespace GpgFrontend::Plugin diff --git a/src/plugin/system/GlobalPluginContext.cpp b/src/plugin/system/GlobalPluginContext.cpp new file mode 100644 index 00000000..528fa8d7 --- /dev/null +++ b/src/plugin/system/GlobalPluginContext.cpp @@ -0,0 +1,336 @@ +/** + * 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 "GlobalPluginContext.h" + +#include <boost/random/mersenne_twister.hpp> +#include <boost/random/uniform_int_distribution.hpp> +#include <memory> +#include <optional> +#include <string> +#include <unordered_map> +#include <unordered_set> + +#include "core/thread/Task.h" +#include "plugin/system/Event.h" +#include "plugin/system/Plugin.h" + +namespace GpgFrontend::Plugin { + +class GlobalPluginContext::Impl { + public: + Impl(TaskRunnerPtr task_runner) + : default_task_runner_(task_runner), + random_gen_( + (boost::posix_time::microsec_clock::universal_time() - + boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))) + .total_milliseconds()) { + // Initialize acquired channels with default values. + acquired_channel_.insert(GPGFRONTEND_DEFAULT_CHANNEL); + acquired_channel_.insert(GPGFRONTEND_NON_ASCII_CHANNEL); + } + + int GetChannel(PluginPtr plugin) { + // Search for the plugin in the register table. + auto plugin_info_opt = + search_plugin_register_table(plugin->GetPluginIdentifier()); + if (!plugin_info_opt.has_value()) { + SPDLOG_ERROR( + "cannot find plugin id {} at register table, fallbacking to " + "default " + "channel", + plugin->GetPluginIdentifier()); + return GetDefaultChannel(plugin); + } + + auto plugin_info = plugin_info_opt.value(); + return plugin_info->channel; + } + + int GetDefaultChannel(PluginPtr) { return GPGFRONTEND_DEFAULT_CHANNEL; } + + std::optional<TaskRunnerPtr> GetTaskRunner(PluginPtr plugin) { + auto opt = search_plugin_register_table(plugin->GetPluginIdentifier()); + if (!opt.has_value()) { + return std::nullopt; + } + return opt.value()->task_runner; + } + + std::optional<TaskRunnerPtr> GetTaskRunner(PluginIdentifier plugin_id) { + // Search for the plugin in the register table. + auto plugin_info_opt = search_plugin_register_table(plugin_id); + if (!plugin_info_opt.has_value()) { + SPDLOG_ERROR("cannot find plugin id {} at register table", plugin_id); + return std::nullopt; + } + return plugin_info_opt.value()->task_runner; + } + + std::optional<TaskRunnerPtr> GetGlobalTaskRunner() { + return default_task_runner_; + } + + bool RegisterPlugin(PluginPtr plugin) { + SPDLOG_DEBUG("attempting to register plugin: {}", + plugin->GetPluginIdentifier()); + // Check if the plugin is null or already registered. + if (plugin == nullptr || + plugin_register_table_.find(plugin->GetPluginIdentifier()) != + plugin_register_table_.end()) { + SPDLOG_ERROR("plugin is null or have already registered this plugin"); + return false; + } + + PluginRegisterInfo register_info; + register_info.plugin = plugin; + register_info.channel = acquire_new_unique_channel(); + register_info.task_runner = std::make_shared<Thread::TaskRunner>(); + + // Register the plugin with its identifier. + plugin_register_table_[plugin->GetPluginIdentifier()] = + std::make_shared<PluginRegisterInfo>(std::move(register_info)); + + SPDLOG_DEBUG("successfully registered plugin: {}", + plugin->GetPluginIdentifier()); + return true; + } + + bool ActivePlugin(PluginIdentifier plugin_id) { + SPDLOG_DEBUG("attempting to activate plugin: {}", plugin_id); + + // Search for the plugin in the register table. + auto plugin_info_opt = search_plugin_register_table(plugin_id); + if (!plugin_info_opt.has_value()) { + SPDLOG_ERROR("cannot find plugin id {} at register table", plugin_id); + return false; + } + + auto plugin_info = plugin_info_opt.value(); + // Activate the plugin if it is not already active. + if (plugin_info->activate && plugin_info->plugin->Active()) { + plugin_info->activate = true; + } + + SPDLOG_DEBUG("plugin activation status: {}", plugin_info->activate); + return plugin_info->activate; + } + + bool ListenEvent(PluginIdentifier plugin_id, EventIdentifier event) { + SPDLOG_DEBUG("plugin: {} is attempting to listen to event {}", plugin_id, + event); + // Check if the event exists, if not, create it. + auto it = plugin_events_table_.find(event); + if (it == plugin_events_table_.end()) { + plugin_events_table_[event] = std::unordered_set<PluginIdentifier>(); + it = plugin_events_table_.find(event); + SPDLOG_INFO("new event {} of plugin system created", event); + } + + auto& listeners_set = it->second; + // Add the listener (plugin) to the event. + auto listener_it = + std::find(listeners_set.begin(), listeners_set.end(), plugin_id); + if (listener_it == listeners_set.end()) { + listeners_set.insert(plugin_id); + } + return true; + } + + bool DeactivatePlugin(PluginIdentifier plugin_id) { + // Search for the plugin in the register table. + auto plugin_info_opt = search_plugin_register_table(plugin_id); + if (!plugin_info_opt.has_value()) { + SPDLOG_ERROR("cannot find plugin id {} at register table", plugin_id); + return false; + } + + auto plugin_info = plugin_info_opt.value(); + // Activate the plugin if it is not already deactive. + if (!plugin_info->activate && plugin_info->plugin->Deactive()) { + plugin_info->activate = false; + } + + return !plugin_info->activate; + } + + bool TriggerEvent(EventRefrernce event) { + SPDLOG_DEBUG("attempting to trigger event: {}", event->GetIdentifier()); + + // Find the set of listeners associated with the given event in the table + auto it = plugin_events_table_.find(event->GetIdentifier()); + if (it == plugin_events_table_.end()) { + // Log a warning if the event is not registered and nobody is listening + SPDLOG_WARN( + "event {} is not listening by anyone and not registered as well", + event->GetIdentifier()); + return false; + } + + // Retrieve the set of listeners for this event + auto& listeners_set = it->second; + + // Check if the set of listeners is empty + if (listeners_set.empty()) { + // Log a warning if nobody is listening to this event + SPDLOG_WARN("event {} is not listening by anyone", + event->GetIdentifier()); + return false; + } + + // Log the number of listeners for this event + SPDLOG_DEBUG("event {}'s current listeners size: {}", + event->GetIdentifier(), listeners_set.size()); + + // Iterate through each listener and execute the corresponding plugin + for (auto& listener_plugin_id : listeners_set) { + // Search for the plugin's information in the registration table + auto plugin_info_opt = search_plugin_register_table(listener_plugin_id); + + // Log an error if the plugin is not found in the registration table + if (!plugin_info_opt.has_value()) { + SPDLOG_ERROR("cannot find plugin id {} at register table", + listener_plugin_id); + } + + // Retrieve the plugin's information + auto plugin_info = plugin_info_opt.value(); + + // Check if the plugin is activated + if (!plugin_info->activate) continue; + + // Execute the plugin and check if it fails + if (plugin_info->plugin->Exec(event)) { + // Log an error if the plugin execution fails + SPDLOG_ERROR("plugin {} executed failed", listener_plugin_id); + } + } + + // Return true to indicate successful execution of all plugins + return true; + } + + private: + struct PluginRegisterInfo { + int channel; + TaskRunnerPtr task_runner; + PluginPtr plugin; + bool activate; + }; + + using PluginRegisterInfoPtr = std::shared_ptr<PluginRegisterInfo>; + + std::unordered_map<PluginIdentifier, PluginRegisterInfoPtr> + plugin_register_table_; + std::map<EventIdentifier, std::unordered_set<PluginIdentifier>> + plugin_events_table_; + + std::set<int> acquired_channel_; + boost::random::mt19937 random_gen_; + TaskRunnerPtr default_task_runner_; + + int acquire_new_unique_channel() { + boost::random::uniform_int_distribution<> dist(1, 65535); + + int random_channel = dist(random_gen_); + // Ensure the acquired channel is unique. + while (acquired_channel_.find(random_channel) != acquired_channel_.end()) { + random_channel = dist(random_gen_); + } + + // Add the acquired channel to the set. + acquired_channel_.insert(random_channel); + return random_channel; + } + + // Function to search for a plugin in the register table. + std::optional<PluginRegisterInfoPtr> search_plugin_register_table( + PluginIdentifier identifier) { + auto it = plugin_register_table_.find(identifier); + if (it == plugin_register_table_.end()) { + return std::nullopt; + } + return it->second; + } + + std::list<PluginIdentifier>& search_plugin_events_table(PluginIdentifier); +}; + +// Constructor for GlobalPluginContext, takes a TaskRunnerPtr as an argument. +GlobalPluginContext::GlobalPluginContext(TaskRunnerPtr task_runner) + : p_(std::make_unique<Impl>(task_runner)) {} + +GlobalPluginContext::~GlobalPluginContext() = default; + +// Function to get the task runner associated with a plugin. +std::optional<TaskRunnerPtr> GlobalPluginContext::GetTaskRunner( + PluginPtr plugin) { + return p_->GetTaskRunner(plugin); +} + +// Function to get the task runner associated with a plugin. +std::optional<TaskRunnerPtr> GlobalPluginContext::GetTaskRunner( + PluginIdentifier plugin_id) { + return p_->GetTaskRunner(plugin_id); +} + +// Function to get the global task runner. +std::optional<TaskRunnerPtr> GlobalPluginContext::GetGlobalTaskRunner() { + return p_->GetGlobalTaskRunner(); +} + +bool GlobalPluginContext::RegisterPlugin(PluginPtr plugin) { + return p_->RegisterPlugin(plugin); +} + +bool GlobalPluginContext::ActivePlugin(PluginIdentifier plugin_id) { + return p_->ActivePlugin(plugin_id); +} + +bool GlobalPluginContext::ListenEvent(PluginIdentifier plugin_id, + EventIdentifier event) { + return p_->ListenEvent(plugin_id, event); +} + +bool GlobalPluginContext::DeactivatePlugin(PluginIdentifier plugin_id) { + return p_->DeactivatePlugin(plugin_id); +} + +bool GlobalPluginContext::TriggerEvent(EventRefrernce event) { + return p_->TriggerEvent(event); +} + +int GlobalPluginContext::GetChannel(PluginPtr plugin) { + return p_->GetChannel(plugin); +} + +int GlobalPluginContext::GetDefaultChannel(PluginPtr _) { + return p_->GetDefaultChannel(_); +} + +} // namespace GpgFrontend::Plugin diff --git a/src/core/plugin/GlobalPluginContext.h b/src/plugin/system/GlobalPluginContext.h index d4a379c0..e9c218b0 100644 --- a/src/core/plugin/GlobalPluginContext.h +++ b/src/plugin/system/GlobalPluginContext.h @@ -29,39 +29,32 @@ #ifndef GPGFRONTEND_GLOBALPLUGINCONTEXT_H #define GPGFRONTEND_GLOBALPLUGINCONTEXT_H -#include <boost/random/mersenne_twister.hpp> -#include <boost/random/uniform_int_distribution.hpp> -#include <memory> -#include <string> -#include <unordered_map> -#include <unordered_set> - +#include "GpgFrontendPluginSystemExport.h" #include "core/GpgFrontendCore.h" -#include "core/plugin/Event.h" -#include "core/plugin/Plugin.h" #include "core/thread/TaskRunner.h" +#include "plugin/system/Event.h" namespace GpgFrontend::Plugin { +class GlobalPluginContext; + class Plugin; class PluginManager; - +using PluginIdentifier = std::string; +using PluginPtr = std::shared_ptr<Plugin>; using PluginList = std::list<std::string>; -struct PluginRegisterInfo { - int channel; - TaskRunnerPtr task_runner; - PluginPtr plugin; - bool activate; -}; +using GlobalPluginContextPtr = std::shared_ptr<GlobalPluginContext>; -using PluginRegisterInfoPtr = std::shared_ptr<PluginRegisterInfo>; +using TaskRunnerPtr = std::shared_ptr<Thread::TaskRunner>; -class GPGFRONTEND_CORE_EXPORT GlobalPluginContext : public QObject { +class GPGFRONTEND_PLUGIN_SYSTEM_EXPORT GlobalPluginContext : public QObject { Q_OBJECT public: GlobalPluginContext(TaskRunnerPtr); + ~GlobalPluginContext(); + int GetChannel(PluginPtr); int GetDefaultChannel(PluginPtr); @@ -83,21 +76,8 @@ class GPGFRONTEND_CORE_EXPORT GlobalPluginContext : public QObject { bool TriggerEvent(EventRefrernce); private: - std::unordered_map<PluginIdentifier, PluginRegisterInfoPtr> - plugin_register_table_; - std::map<EventIdentifier, std::unordered_set<PluginIdentifier>> - plugin_events_table_; - - std::set<int> acquired_channel_; - boost::random::mt19937 random_gen_; - TaskRunnerPtr default_task_runner_; - - int acquire_new_unique_channel(); - - std::optional<PluginRegisterInfoPtr> search_plugin_register_table( - PluginIdentifier); - - std::list<PluginIdentifier> &search_plugin_events_table(PluginIdentifier); + class Impl; + std::unique_ptr<Impl> p_; }; } // namespace GpgFrontend::Plugin diff --git a/src/plugin/sdk/Plugin.cpp b/src/plugin/system/GpgFrontendPluginSystem.h index b8095316..b4861e76 100644 --- a/src/plugin/sdk/Plugin.cpp +++ b/src/plugin/system/GpgFrontendPluginSystem.h @@ -26,37 +26,9 @@ * */ -#include "Plugin.h" +#pragma once -#include <core/plugin/PluginManager.h> - -namespace GpgFrontend::Plugin::SDK { - -SPlugin::SPlugin(SPluginIdentifier id, SPluginVersion version, - SPluginMetaData meta_data) - : Plugin::Plugin(id, version, meta_data) {} - -bool SPlugin::Register() { return true; } - -bool SPlugin::Active() { return true; } - -int SPlugin::Exec(SEventRefrernce) { return 0; } - -bool SPlugin::Deactive() { return true; } - -SPluginIdentifier SPlugin::GetSPluginIdentifier() const { - return GetPluginIdentifier(); -} - -Thread::TaskRunner* GetPluginTaskRunner(SPlugin* plugin) { - if (plugin == nullptr) return nullptr; - - auto opt = GpgFrontend::Plugin::PluginManager::GetInstance()->GetTaskRunner( - plugin->GetSPluginIdentifier()); - if (!opt.has_value()) { - return nullptr; - } - - return opt.value().get(); -} -} // namespace GpgFrontend::Plugin::SDK +#include <core/GpgFrontendCore.h> +#include <plugin/system/Event.h> +#include <plugin/system/Plugin.h> +#include <plugin/system/PluginManager.h>
\ No newline at end of file diff --git a/src/plugin/system/GpgFrontendPluginSystemExport.h b/src/plugin/system/GpgFrontendPluginSystemExport.h new file mode 100644 index 00000000..e096f0dc --- /dev/null +++ b/src/plugin/system/GpgFrontendPluginSystemExport.h @@ -0,0 +1,42 @@ + +#ifndef GPGFRONTEND_PLUGIN_SYSTEM_EXPORT_H +#define GPGFRONTEND_PLUGIN_SYSTEM_EXPORT_H + +#ifdef GPGFRONTEND_PLUGIN_SYSTEM_STATIC_DEFINE +# define GPGFRONTEND_PLUGIN_SYSTEM_EXPORT +# define GPGFRONTEND_PLUGIN_SYSTEM_NO_EXPORT +#else +# ifndef GPGFRONTEND_PLUGIN_SYSTEM_EXPORT +# ifdef gpgfrontend_plugin_system_EXPORTS + /* We are building this library */ +# define GPGFRONTEND_PLUGIN_SYSTEM_EXPORT __attribute__((visibility("default"))) +# else + /* We are using this library */ +# define GPGFRONTEND_PLUGIN_SYSTEM_EXPORT __attribute__((visibility("default"))) +# endif +# endif + +# ifndef GPGFRONTEND_PLUGIN_SYSTEM_NO_EXPORT +# define GPGFRONTEND_PLUGIN_SYSTEM_NO_EXPORT __attribute__((visibility("hidden"))) +# endif +#endif + +#ifndef GPGFRONTEND_PLUGIN_SYSTEM_DEPRECATED +# define GPGFRONTEND_PLUGIN_SYSTEM_DEPRECATED __attribute__ ((__deprecated__)) +#endif + +#ifndef GPGFRONTEND_PLUGIN_SYSTEM_DEPRECATED_EXPORT +# define GPGFRONTEND_PLUGIN_SYSTEM_DEPRECATED_EXPORT GPGFRONTEND_PLUGIN_SYSTEM_EXPORT GPGFRONTEND_PLUGIN_SYSTEM_DEPRECATED +#endif + +#ifndef GPGFRONTEND_PLUGIN_SYSTEM_DEPRECATED_NO_EXPORT +# define GPGFRONTEND_PLUGIN_SYSTEM_DEPRECATED_NO_EXPORT GPGFRONTEND_PLUGIN_SYSTEM_NO_EXPORT GPGFRONTEND_PLUGIN_SYSTEM_DEPRECATED +#endif + +#if 0 /* DEFINE_NO_DEPRECATED */ +# ifndef GPGFRONTEND_PLUGIN_SYSTEM_NO_DEPRECATED +# define GPGFRONTEND_PLUGIN_SYSTEM_NO_DEPRECATED +# endif +#endif + +#endif /* GPGFRONTEND_PLUGIN_SYSTEM_EXPORT_H */ diff --git a/src/plugin/system/Plugin.cpp b/src/plugin/system/Plugin.cpp new file mode 100644 index 00000000..9f975925 --- /dev/null +++ b/src/plugin/system/Plugin.cpp @@ -0,0 +1,103 @@ +/** + * 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 "Plugin.h" + +#include "plugin/system/GlobalPluginContext.h" + +namespace GpgFrontend::Plugin { + +class Plugin::Impl { + public: + friend class GlobalPluginContext; + + Impl(PluginIdentifier id, PluginVersion version, PluginMetaData meta_data) + : identifier_((boost::format("__plugin_%1%") % id).str()), + version_(version), + meta_data_(meta_data) {} + + int GetChannel() { + return get_global_plugin_context()->GetChannel(self_shared_ptr_); + } + + int GetDefaultChannel() { + return get_global_plugin_context()->GetDefaultChannel(self_shared_ptr_); + } + + std::optional<TaskRunnerPtr> GetTaskRunner() { + return get_global_plugin_context()->GetTaskRunner(self_shared_ptr_); + } + + bool ListenEvent(EventIdentifier event) { + return get_global_plugin_context()->ListenEvent(gpc_get_identifier(), + event); + } + + PluginIdentifier GetPluginIdentifier() const { return identifier_; } + + PluginIdentifier SetGPC(GlobalPluginContextPtr gpc) { gpc_ = gpc; } + + private: + GlobalPluginContextPtr gpc_; + const std::shared_ptr<Plugin> self_shared_ptr_; + const PluginIdentifier identifier_; + const PluginVersion version_; + const PluginMetaData meta_data_; + + PluginIdentifier gpc_get_identifier() { return identifier_; } + + const GlobalPluginContextPtr get_global_plugin_context() { + if (gpc_ == nullptr) { + throw std::runtime_error("plugin is not registered by plugin manager"); + } + return gpc_; + } +}; + +Plugin::Plugin(PluginIdentifier id, PluginVersion version, + PluginMetaData meta_data) + : s_(this) {} + +int Plugin::getChannel() { return p_->GetChannel(); } + +int Plugin::getDefaultChannel() { return p_->GetDefaultChannel(); } + +TaskRunnerPtr Plugin::getTaskRunner() { + return p_->GetTaskRunner().value_or(nullptr); +} + +bool Plugin::listenEvent(EventIdentifier event) { + return p_->ListenEvent(event); +} + +PluginIdentifier Plugin::GetPluginIdentifier() const { + return p_->GetPluginIdentifier(); +} + +PluginIdentifier Plugin::SetGPC(GlobalPluginContextPtr gpc) { p_->SetGPC(gpc); } +} // namespace GpgFrontend::Plugin
\ No newline at end of file diff --git a/src/core/plugin/Plugin.h b/src/plugin/system/Plugin.h index 8e7ab58f..7c538b3f 100644 --- a/src/core/plugin/Plugin.h +++ b/src/plugin/system/Plugin.h @@ -29,14 +29,15 @@ #ifndef GPGFRONTEND_PLUGIN_H #define GPGFRONTEND_PLUGIN_H -#include "core/plugin/Event.h" +#include <memory> + +#include "GpgFrontendPluginSystemExport.h" #include "core/thread/Task.h" #include "core/thread/TaskRunner.h" +#include "plugin/system/Event.h" namespace GpgFrontend::Plugin { -using TaskRunnerPtr = std::shared_ptr<Thread::TaskRunner>; - class Plugin; class GlobalPluginContext; class PluginManager; @@ -45,15 +46,13 @@ using PluginIdentifier = std::string; using PluginVersion = std::string; using PluginMetaData = std::map<std::string, std::string>; using PluginPtr = std::shared_ptr<Plugin>; - using GlobalPluginContextPtr = std::shared_ptr<GlobalPluginContext>; -class GPGFRONTEND_CORE_EXPORT Plugin : public QObject { +using TaskRunnerPtr = std::shared_ptr<Thread::TaskRunner>; + +class GPGFRONTEND_PLUGIN_SYSTEM_EXPORT Plugin : public QObject { Q_OBJECT public: - friend class PluginManager; - friend class GlobalPluginContext; - Plugin(PluginIdentifier, PluginVersion, PluginMetaData); virtual bool Register() = 0; @@ -66,35 +65,21 @@ class GPGFRONTEND_CORE_EXPORT Plugin : public QObject { PluginIdentifier GetPluginIdentifier() const; + PluginIdentifier SetGPC(GlobalPluginContextPtr); + protected: int getChannel(); int getDefaultChannel(); - std::optional<TaskRunnerPtr> getTaskRunner(); + TaskRunnerPtr getTaskRunner(); bool listenEvent(EventIdentifier); private: - const GlobalPluginContextPtr global_plugin_context_; - const std::shared_ptr<Plugin> self_shared_ptr_; - const PluginIdentifier identifier_; - const PluginVersion version_; - const PluginMetaData meta_data_; - - void pm_set_global_plugin_cotext(GlobalPluginContextPtr); - - PluginIdentifier gpc_get_identifier(); - - bool gpc_register_plugin(); - - bool gpc_active_plugin(); - - bool gpc_deactive_plugin(); - - int gpc_exec_plugin(); - - const GlobalPluginContextPtr get_global_plugin_context(); + class Impl; + std::unique_ptr<Impl> p_; + const std::shared_ptr<Plugin> s_; }; } // namespace GpgFrontend::Plugin diff --git a/src/plugin/system/PluginManager.cpp b/src/plugin/system/PluginManager.cpp new file mode 100644 index 00000000..c1348948 --- /dev/null +++ b/src/plugin/system/PluginManager.cpp @@ -0,0 +1,109 @@ +/** + * 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 "PluginManager.h" + +#include "core/thread/TaskRunner.h" +#include "plugin/system/GlobalPluginContext.h" +#include "plugin/system/Plugin.h" + +namespace GpgFrontend::Plugin { + +PluginMangerPtr PluginManager::g_ = nullptr; + +class PluginManager::Impl { + public: + Impl() + : task_runner_(std::make_shared<Thread::TaskRunner>()), + global_plugin_context_( + std::make_shared<GlobalPluginContext>(task_runner_)) {} + + void RegisterPlugin(PluginPtr plugin) { + task_runner_->PostTask(new Thread::Task( + std::move([=](GpgFrontend::Thread::Task::DataObjectPtr) -> int { + global_plugin_context_->RegisterPlugin(plugin); + return 0; + }), + __func__, nullptr, true)); + } + + void TriggerEvent(EventRefrernce event) { + task_runner_->PostTask(new Thread::Task( + std::move([=](GpgFrontend::Thread::Task::DataObjectPtr) -> int { + global_plugin_context_->TriggerEvent(event); + return 0; + }), + __func__, nullptr, true)); + } + + void ActivePlugin(PluginIdentifier identifier) { + task_runner_->PostTask(new Thread::Task( + std::move([=](GpgFrontend::Thread::Task::DataObjectPtr) -> int { + global_plugin_context_->ActivePlugin(identifier); + return 0; + }), + __func__, nullptr, true)); + } + + std::optional<TaskRunnerPtr> GetTaskRunner(PluginIdentifier plugin_id) { + return global_plugin_context_->GetTaskRunner(plugin_id); + } + + private: + static PluginMangerPtr global_plugin_manager_; + TaskRunnerPtr task_runner_; + GlobalPluginContextPtr global_plugin_context_; +}; + +PluginManager::PluginManager() : p_(std::make_unique<Impl>()) {} + +PluginManager::~PluginManager() = default; + +PluginMangerPtr PluginManager::GetInstance() { + if (g_ == nullptr) g_ = std::shared_ptr<PluginManager>(new PluginManager()); + return g_; +} + +void PluginManager::RegisterPlugin(PluginPtr plugin) { + return p_->RegisterPlugin(plugin); +} + +void PluginManager::TriggerEvent(EventRefrernce event) { + return p_->TriggerEvent(event); +} + +void PluginManager::ActivePlugin(PluginIdentifier identifier) { + return p_->ActivePlugin(identifier); +} + +std::optional<TaskRunnerPtr> PluginManager::GetTaskRunner( + PluginIdentifier plugin_id) { + return p_->GetTaskRunner(plugin_id); +} + +} // namespace GpgFrontend::Plugin
\ No newline at end of file diff --git a/src/core/plugin/PluginManager.h b/src/plugin/system/PluginManager.h index 4b36eb12..cad0d93c 100644 --- a/src/core/plugin/PluginManager.h +++ b/src/plugin/system/PluginManager.h @@ -29,8 +29,10 @@ #ifndef GPGFRONTEND_PLUGINMANAGER_H #define GPGFRONTEND_PLUGINMANAGER_H +#include <memory> #include <string> +#include "GpgFrontendPluginSystemExport.h" #include "core/GpgFrontendCore.h" #include "core/thread/Task.h" @@ -53,7 +55,7 @@ using PluginPtr = std::shared_ptr<Plugin>; using PluginMangerPtr = std::shared_ptr<PluginManager>; using GlobalPluginContextPtr = std::shared_ptr<GlobalPluginContext>; -class GPGFRONTEND_CORE_EXPORT PluginManager : public QObject { +class GPGFRONTEND_PLUGIN_SYSTEM_EXPORT PluginManager : public QObject { Q_OBJECT public: static PluginMangerPtr GetInstance(); @@ -69,11 +71,13 @@ class GPGFRONTEND_CORE_EXPORT PluginManager : public QObject { std::optional<TaskRunnerPtr> GetTaskRunner(PluginIdentifier); private: + class Impl; + std::unique_ptr<Impl> p_; + static PluginMangerPtr g_; + PluginManager(); - static PluginMangerPtr global_plugin_manager_; - TaskRunnerPtr task_runner_; - GlobalPluginContextPtr global_plugin_context_; + ~PluginManager(); }; } // namespace GpgFrontend::Plugin |