diff options
author | saturneric <[email protected]> | 2024-04-21 19:49:04 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2024-04-21 19:49:04 +0000 |
commit | 571a2906063739067e40a02aadd8c265082359de (patch) | |
tree | 5ac57f777ec38f6a5f328da4e3df6ad6cc010aff /src/core/module/GlobalModuleContext.cpp | |
parent | fix: could not determine the path to the executable based on the desktop file (diff) | |
parent | feat: adapt linux appimage of modules loading (diff) | |
download | GpgFrontend-571a2906063739067e40a02aadd8c265082359de.tar.gz GpgFrontend-571a2906063739067e40a02aadd8c265082359de.zip |
Merge branch 'dev/2.1.2/module' into develop
Diffstat (limited to 'src/core/module/GlobalModuleContext.cpp')
-rw-r--r-- | src/core/module/GlobalModuleContext.cpp | 102 |
1 files changed, 93 insertions, 9 deletions
diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index 9bc4f06b..1744ba72 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -31,6 +31,7 @@ #include <set> #include <unordered_map> #include <unordered_set> +#include <utility> #include "core/module/Event.h" #include "core/module/Module.h" @@ -49,6 +50,18 @@ class GlobalModuleContext::Impl { acquired_channel_.insert(kGpgFrontendNonAsciiChannel); } + auto SearchModule(ModuleIdentifier module_id) -> ModulePtr { + // Search for the module in the register table. + auto module_info_opt = search_module_register_table(module_id); + if (!module_info_opt.has_value()) { + GF_CORE_LOG_ERROR("cannot find module id {} at register table", + module_id); + return nullptr; + } + + return module_info_opt.value()->module; + } + auto GetChannel(ModuleRawPtr module) -> int { // Search for the module in the register table. auto module_info_opt = @@ -97,7 +110,7 @@ class GlobalModuleContext::Impl { return false; } - if (!module->Register()) { + if (module->Register() != 0) { GF_CORE_LOG_ERROR("register module {} failed", module->GetModuleIdentifier()); return false; @@ -158,6 +171,15 @@ class GlobalModuleContext::Impl { auto ListenEvent(ModuleIdentifier module_id, EventIdentifier event) -> bool { GF_CORE_LOG_DEBUG("module: {} is attempting to listen to event {}", module_id, event); + + // module -> event + auto module_info_opt = search_module_register_table(module_id); + if (!module_info_opt.has_value()) { + GF_CORE_LOG_ERROR("cannot find module id {} at register table", + module_id); + return false; + } + // Check if the event exists, if not, create it. auto met_it = module_events_table_.find(event); if (met_it == module_events_table_.end()) { @@ -166,6 +188,8 @@ class GlobalModuleContext::Impl { GF_CORE_LOG_DEBUG("new event {} of module system created", event); } + module_info_opt.value()->listening_event_ids.push_back(event); + auto& listeners_set = met_it->second; // Add the listener (module) to the event. auto listener_it = listeners_set.find(module_id); @@ -176,7 +200,7 @@ class GlobalModuleContext::Impl { } auto DeactivateModule(ModuleIdentifier module_id) -> bool { - // Search for the module in the register table. + // search for the module in the register table. auto module_info_opt = search_module_register_table(module_id); if (!module_info_opt.has_value()) { GF_CORE_LOG_ERROR("cannot find module id {} at register table", @@ -185,8 +209,16 @@ class GlobalModuleContext::Impl { } auto module_info = module_info_opt.value(); - // Activate the module if it is not already deactive. - if (!module_info->activate && module_info->module->Deactive()) { + // activate the module if it is not already deactive. + if (module_info->activate && (module_info->module->Deactive() == 0)) { + for (const auto& event_ids : module_info->listening_event_ids) { + auto& modules = module_events_table_[event_ids]; + if (auto it = modules.find(module_id); it != modules.end()) { + modules.erase(it); + } + } + + module_info->listening_event_ids.clear(); module_info->activate = false; } @@ -222,6 +254,9 @@ class GlobalModuleContext::Impl { GF_CORE_LOG_DEBUG("event {}'s current listeners size: {}", event->GetIdentifier(), listeners_set.size()); + // register trigger id index table + module_on_triggering_events_table_[event->GetTriggerIdentifier()] = event; + // Iterate through each listener and execute the corresponding module for (const auto& listener_module_id : listeners_set) { // Search for the module's information in the registration table @@ -272,16 +307,43 @@ class GlobalModuleContext::Impl { return true; } - auto IsModuleActivated(const ModuleIdentifier& m_id) const -> bool { + auto SearchEvent(const EventTriggerIdentifier& trigger_id) + -> std::optional<EventRefrernce> { + if (module_on_triggering_events_table_.find(trigger_id) != + module_on_triggering_events_table_.end()) { + return module_on_triggering_events_table_[trigger_id]; + } + return {}; + } + + [[nodiscard]] auto IsModuleActivated(const ModuleIdentifier& m_id) const + -> bool { auto m = search_module_register_table(m_id); return m.has_value() && m->get()->activate; } + auto ListAllRegisteredModuleID() -> QList<ModuleIdentifier> { + QList<ModuleIdentifier> module_ids; + for (const auto& module : module_register_table_) { + module_ids.append(module.first); + } + module_ids.sort(); + return module_ids; + } + + auto GetModuleListening(const ModuleIdentifier& module_id) + -> QList<EventIdentifier> { + auto module_info = search_module_register_table(module_id); + if (!module_info.has_value()) return {}; + return module_info->get()->listening_event_ids; + } + private: struct ModuleRegisterInfo { int channel; ModulePtr module; bool activate; + QList<QString> listening_event_ids; }; using ModuleRegisterInfoPtr = std::shared_ptr<ModuleRegisterInfo>; @@ -290,6 +352,8 @@ class GlobalModuleContext::Impl { module_register_table_; std::map<EventIdentifier, std::unordered_set<ModuleIdentifier>> module_events_table_; + std::map<EventTriggerIdentifier, EventRefrernce> + module_on_triggering_events_table_; std::set<int> acquired_channel_; TaskRunnerPtr default_task_runner_; @@ -307,7 +371,8 @@ class GlobalModuleContext::Impl { } // Function to search for a module in the register table. - auto search_module_register_table(const ModuleIdentifier& identifier) const + [[nodiscard]] auto search_module_register_table( + const ModuleIdentifier& identifier) const -> std::optional<ModuleRegisterInfoPtr> { auto mrt_it = module_register_table_.find(identifier); if (mrt_it == module_register_table_.end()) { @@ -323,6 +388,11 @@ GlobalModuleContext::GlobalModuleContext() GlobalModuleContext::~GlobalModuleContext() = default; +auto GlobalModuleContext::SearchModule(ModuleIdentifier module_id) + -> ModulePtr { + return p_->SearchModule(std::move(module_id)); +} + // Function to get the task runner associated with a module. auto GlobalModuleContext::GetTaskRunner(ModuleRawPtr module) -> std::optional<TaskRunnerPtr> { @@ -342,7 +412,7 @@ auto GlobalModuleContext::GetGlobalTaskRunner() } auto GlobalModuleContext::RegisterModule(ModulePtr module) -> bool { - return p_->RegisterModule(std::move(module)); + return p_->RegisterModule(module); } auto GlobalModuleContext::ActiveModule(ModuleIdentifier module_id) -> bool { @@ -359,7 +429,12 @@ auto GlobalModuleContext::DeactivateModule(ModuleIdentifier module_id) -> bool { } auto GlobalModuleContext::TriggerEvent(EventRefrernce event) -> bool { - return p_->TriggerEvent(std::move(event)); + return p_->TriggerEvent(event); +} + +auto GlobalModuleContext::SearchEvent(EventTriggerIdentifier trigger_id) + -> std::optional<EventRefrernce> { + return p_->SearchEvent(trigger_id); } auto GlobalModuleContext::GetChannel(ModuleRawPtr module) -> int { @@ -371,7 +446,16 @@ auto GlobalModuleContext::GetDefaultChannel(ModuleRawPtr channel) -> int { } auto GlobalModuleContext::IsModuleActivated(ModuleIdentifier m_id) -> bool { - return p_->IsModuleActivated(std::move(m_id)); + return p_->IsModuleActivated(m_id); +} + +auto GlobalModuleContext::ListAllRegisteredModuleID() + -> QList<ModuleIdentifier> { + return p_->ListAllRegisteredModuleID(); } +auto GlobalModuleContext::GetModuleListening(ModuleIdentifier module_id) + -> QList<EventIdentifier> { + return p_->GetModuleListening(module_id); +} } // namespace GpgFrontend::Module |