From b2ba21c75d243d3ae360835fea80aad4063dacfe Mon Sep 17 00:00:00 2001
From: patrick96
Date: Sat, 25 Apr 2020 01:41:01 +0200
Subject: [PATCH] actions: Forward legacy actions to the right module
All the information about which action has to be delivered to which
module is kept in once place to make cleanup easier once the deprecated
actions are removed.
Right now only the date module is added as a proof of concept.
---
src/components/controller.cpp | 37 ++++++++++++++++++++++++++++++++++-
1 file changed, 36 insertions(+), 1 deletion(-)
diff --git a/src/components/controller.cpp b/src/components/controller.cpp
index f752a04a..37075be5 100644
--- a/src/components/controller.cpp
+++ b/src/components/controller.cpp
@@ -1,6 +1,7 @@
#include "components/controller.hpp"
#include
+#include
#include "components/bar.hpp"
#include "components/builder.hpp"
@@ -423,7 +424,7 @@ void controller::process_inputdata() {
for (auto&& handler : m_inputhandlers) {
if (handler->input_handler_name() == handler_name) {
if (!handler->input(string{action})) {
- m_log.warn("The '%s' module does not support the '%s' action.", handler_name, action);
+ m_log.err("The '%s' module does not support the '%s' action.", handler_name, action);
}
num_delivered++;
@@ -439,6 +440,40 @@ void controller::process_inputdata() {
return;
}
+ /*
+ * Maps legacy action names to a module type and the new action name in that module.
+ *
+ * The action will be delivered to the first module of that type so that it is consistent with existing behavior.
+ *
+ * TODO Remove when deprecated action names are removed
+ */
+ const std::map> legacy_actions {
+ {"datetoggle", {string(date_module::TYPE), "toggle"}},
+ };
+
+ auto it = legacy_actions.find(cmd);
+
+ if (it != legacy_actions.end()) {
+ auto action = it->second.second;
+
+ for (auto&& handler : m_inputhandlers) {
+ auto module_ptr = std::dynamic_pointer_cast(handler);
+
+ if (module_ptr && module_ptr->type() == it->second.first) {
+ // TODO make this message more descriptive and maybe link to some documentation
+ m_log.warn("The action '%s' is deprecated, use '#%s#%s' instead!", cmd, handler->input_handler_name(), action);
+ m_log.info("Forwarding legacy action '%s' to module '%s' as '%s'", cmd, handler->input_handler_name(), action);
+ if (!handler->input(string{action})) {
+ m_log.err("Failed to forward deprecated action to %s module. THIS IS A BUG!", it->second.first);
+ }
+ // Only deliver to the first matching module.
+ return;
+ }
+ }
+
+ // Fall through to running the action as a command to not break existing behavior.
+ }
+
try {
// Run input as command if it's not an input for a module
m_log.info("Forwarding command to shell... (input: %s)", cmd);