From 1a5fb437a56d9ef8dea6f56cf088e64256482f16 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Tue, 7 Oct 2014 00:24:03 -0400 Subject: [PATCH] Task - Upgrades legacy recur values. --- src/TDB2.cpp | 1 + src/Task.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++----- src/Task.h | 1 + 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/TDB2.cpp b/src/TDB2.cpp index 64b9621ff..1e8a9d0a8 100644 --- a/src/TDB2.cpp +++ b/src/TDB2.cpp @@ -580,6 +580,7 @@ void TDB2::modify (Task& task, bool add_to_backlog /* = true */) { // Ensure the task is consistent, and provide defaults if necessary. task.validate (false); + task.upgradeLegacyValues (); std::string uuid = task.get ("uuid"); // Get the unmodified task as reference, so the hook can compare. diff --git a/src/Task.cpp b/src/Task.cpp index 59be9f934..8493b7fae 100644 --- a/src/Task.cpp +++ b/src/Task.cpp @@ -143,12 +143,13 @@ bool Task::operator== (const Task& other) //////////////////////////////////////////////////////////////////////////////// Task::Task (const std::string& input) { - id = 0; - urgency_value = 0.0; - recalc_urgency = true; - is_blocked = false; - is_blocking = false; + id = 0; + urgency_value = 0.0; + recalc_urgency = true; + is_blocked = false; + is_blocking = false; annotation_count = 0; + parse (input); } @@ -586,6 +587,8 @@ void Task::parse (const std::string& input) parseJSON (copy); else throw std::string (STRING_RECORD_NOT_FF4); + + upgradeLegacyValues (); } catch (const std::string&) @@ -716,6 +719,8 @@ void Task::parseJSON (const std::string& line) } } } + + upgradeLegacyValues (); } } @@ -2164,3 +2169,41 @@ void Task::modify (modType type, bool text_required /* = false */) } //////////////////////////////////////////////////////////////////////////////// +void Task::upgradeLegacyValues () +{ + // 2.4.0 Update recurrence values. + if (has ("recur")) + { + std::string value = get ("recur"); + std::string new_value = ""; + std::string::size_type len = value.length (); + std::string::size_type p; + + if (value == "-") new_value = "0s"; + else if ((p = value.find ("hr")) != std::string::npos && p == len - 2) new_value = value.substr (0, p) + "h"; + else if ((p = value.find ("hrs")) != std::string::npos && p == len - 3) new_value = value.substr (0, p) + "h"; + else if ((p = value.find ("mins")) != std::string::npos && p == len - 4) new_value = value.substr (0, p) + "min"; + else if ((p = value.find ("mnths")) != std::string::npos && p == len - 5) new_value = value.substr (0, p) + "mo"; + else if ((p = value.find ("mos")) != std::string::npos && p == len - 3) new_value = value.substr (0, p) + "mo"; + else if ((p = value.find ("mth")) != std::string::npos && p == len - 3) new_value = value.substr (0, p) + "mo"; + else if ((p = value.find ("mths")) != std::string::npos && p == len - 4) new_value = value.substr (0, p) + "mo"; + else if ((p = value.find ("qrtrs")) != std::string::npos && p == len - 5) new_value = value.substr (0, p) + "q"; + else if ((p = value.find ("qtr")) != std::string::npos && p == len - 3) new_value = value.substr (0, p) + "q"; + else if ((p = value.find ("qtrs")) != std::string::npos && p == len - 4) new_value = value.substr (0, p) + "q"; + else if ((p = value.find ("sec")) != std::string::npos && p == len - 3) new_value = value.substr (0, p) + "s"; + else if ((p = value.find ("secs")) != std::string::npos && p == len - 4) new_value = value.substr (0, p) + "s"; + else if ((p = value.find ("wk")) != std::string::npos && p == len - 2) new_value = value.substr (0, p) + "w"; + else if ((p = value.find ("wks")) != std::string::npos && p == len - 3) new_value = value.substr (0, p) + "w"; + else if ((p = value.find ("yr")) != std::string::npos && p == len - 2) new_value = value.substr (0, p) + "y"; + else if ((p = value.find ("yrs")) != std::string::npos && p == len - 3) new_value = value.substr (0, p) + "y"; + + if (new_value != "" && + new_value != value) + { + set ("recur", new_value); + context.debug (format ("Legacy upgrade: recur {1} --> {2}", value, new_value)); + } + } +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/Task.h b/src/Task.h index dd4644c89..901d16a79 100644 --- a/src/Task.h +++ b/src/Task.h @@ -154,6 +154,7 @@ public: enum modType {modReplace, modPrepend, modAppend, modAnnotate}; void modify (modType, bool text_required = false); + void upgradeLegacyValues (); private: int determineVersion (const std::string&);