From 8683574b18336e0644800b22926b51f4ae8c6be4 Mon Sep 17 00:00:00 2001 From: Tomas Babej Date: Wed, 21 Jan 2015 07:59:27 +0100 Subject: [PATCH] Hooks: Make sure that original task is properly detected On modify event uses UUID of the original task being modified to determine which line should be interpreted as modification of the task. This was achieved by searching by a substring in a specific JSON format, which, consenquently, failed on JSON strings of other (valid) formats. --- src/Hooks.cpp | 28 ++++++++++++++++++++++++++-- src/Hooks.h | 1 + 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Hooks.cpp b/src/Hooks.cpp index 70438c40a..68e5db4d1 100644 --- a/src/Hooks.cpp +++ b/src/Hooks.cpp @@ -317,7 +317,6 @@ void Hooks::onModify (const Task& before, std::vector & tasks) { // Prepare invariants. std::string beforeJSON = before.composeJSON (); - std::string uuidPattern = "\"uuid\":\"" + before.get ("uuid") + "\""; // Convert vector of tasks to a vector of strings. std::vector input; @@ -345,7 +344,7 @@ void Hooks::onModify (const Task& before, std::vector & tasks) { if (status == 0) { - if (line->find (uuidPattern) != std::string::npos) + if (JSONContainsUUID((*line), before.get("uuid"))) input[1] = *line; // [1] original' else input.push_back (*line); // [n > 1] extras @@ -460,6 +459,31 @@ bool Hooks::isJSON (const std::string& input) const return false; } +//////////////////////////////////////////////////////////////////////////////// +// This method assumes that input has already been validated with isJSON method +bool Hooks::JSONContainsUUID(const std::string& input, const std::string& uuid) const +{ + bool foundUUID = false; + + // Parse the whole thing. + json::object* root_obj = (json::object*) (json::parse (input)); + + // For each object element... + json_object_iter i; + for (i = root_obj->_data.begin (); + i != root_obj->_data.end (); + ++i) + { + // If the attribute is a recognized column. + std::string type = Task::attributes[i->first]; + if (type == "string" && i->first == "uuid" && + json::decode (unquoteText (i->second->dump ())) == uuid) + foundUUID = true; + } + + return foundUUID; +} + //////////////////////////////////////////////////////////////////////////////// int Hooks::callHookScript ( const std::string& script, diff --git a/src/Hooks.h b/src/Hooks.h index 7989e64db..c2491aa9d 100644 --- a/src/Hooks.h +++ b/src/Hooks.h @@ -52,6 +52,7 @@ public: private: std::vector scripts (const std::string&); bool isJSON (const std::string&) const; + bool JSONContainsUUID (const std::string&, const std::string&) const; int callHookScript (const std::string&, const std::vector &, std::vector &); private: