From 6e649f3f45561229054cc30363d7b6aa2fafb2d3 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 11 Sep 2011 01:56:56 -0400 Subject: [PATCH] Recurring Tasks - Rewrote updateRecurrenceMask, which was hopelessly muddled after the TDB -> TDB2 change. - Modified associated code. - Improved signal to noise ratio in unit tests. --- src/commands/CmdDelete.cpp | 3 +- src/commands/CmdDone.cpp | 2 +- src/main.h | 2 +- src/recur.cpp | 74 +++++++++++++++----------------------- test/recur.until.t | 5 ++- 5 files changed, 36 insertions(+), 50 deletions(-) diff --git a/src/commands/CmdDelete.cpp b/src/commands/CmdDelete.cpp index 07a7ab952..d1f77214b 100644 --- a/src/commands/CmdDelete.cpp +++ b/src/commands/CmdDelete.cpp @@ -125,7 +125,7 @@ int CmdDelete::execute (std::string& output) { // Update mask in parent. task->setStatus (Task::deleted); - updateRecurrenceMask (siblings, *task); + updateRecurrenceMask (*task); // Don't want a 'delete' to clobber the end date that may have // been written by a 'done' command. @@ -183,7 +183,6 @@ int CmdDelete::execute (std::string& output) } context.tdb2.commit (); - output = out.str (); return rc; } diff --git a/src/commands/CmdDone.cpp b/src/commands/CmdDone.cpp index 69d874298..464cc7999 100644 --- a/src/commands/CmdDone.cpp +++ b/src/commands/CmdDone.cpp @@ -103,7 +103,7 @@ int CmdDone::execute (std::string& output) } } - updateRecurrenceMask (filtered, *task); + updateRecurrenceMask (*task); if (!nagged) nagged = nag (*task); } diff --git a/src/main.h b/src/main.h index cdcfaaf29..6efb7b7c6 100644 --- a/src/main.h +++ b/src/main.h @@ -41,7 +41,7 @@ void handleRecurrence (); Date getNextRecurrence (Date&, std::string&); bool generateDueDates (Task&, std::vector &); -void updateRecurrenceMask (std::vector &, Task&); +void updateRecurrenceMask (Task&); int getDueState (const std::string&); bool nag (Task&); diff --git a/src/recur.cpp b/src/recur.cpp index 1bbab72bf..f5a44da9d 100644 --- a/src/recur.cpp +++ b/src/recur.cpp @@ -56,7 +56,6 @@ extern Context context; void handleRecurrence () { std::vector tasks = context.tdb2.pending.get_tasks (); - std::vector modified; // Look at all tasks and find any recurring ones. std::vector ::iterator t; @@ -124,9 +123,6 @@ void handleRecurrence () rec.remove ("mask"); // Remove the mask of the parent. - // Add the new task to the vector, for immediate use. - modified.push_back (rec); - // Add the new task to the DB. context.tdb2.add (rec); } @@ -141,11 +137,7 @@ void handleRecurrence () context.tdb2.modify (*t); } } - else - modified.push_back (*t); } - - tasks = modified; } //////////////////////////////////////////////////////////////////////////////// @@ -345,47 +337,39 @@ Date getNextRecurrence (Date& current, std::string& period) //////////////////////////////////////////////////////////////////////////////// // When the status of a recurring child task changes, the parent task must // update it's mask. -void updateRecurrenceMask ( - std::vector & all, - Task& task) +void updateRecurrenceMask (Task& task) { - std::string parent = task.get ("parent"); - if (parent != "") + std::string uuid = task.get ("parent"); + Task parent; + + if (uuid != "" && + context.tdb2.get (uuid, parent)) { - std::vector ::iterator it; - for (it = all.begin (); it != all.end (); ++it) + unsigned int index = strtol (task.get ("imask").c_str (), NULL, 10); + std::string mask = parent.get ("mask"); + if (mask.length () > index) { - if (it->get ("uuid") == parent) - { - unsigned int index = atoi (task.get ("imask").c_str ()); - std::string mask = it->get ("mask"); - if (mask.length () > index) - { - mask[index] = (task.getStatus () == Task::pending) ? '-' - : (task.getStatus () == Task::completed) ? '+' - : (task.getStatus () == Task::deleted) ? 'X' - : (task.getStatus () == Task::waiting) ? 'W' - : '?'; - - it->set ("mask", mask); - context.tdb2.modify (*it); - } - else - { - std::string mask; - for (unsigned int i = 0; i < index; ++i) - mask += "?"; - - mask += (task.getStatus () == Task::pending) ? '-' - : (task.getStatus () == Task::completed) ? '+' - : (task.getStatus () == Task::deleted) ? 'X' - : (task.getStatus () == Task::waiting) ? 'W' - : '?'; - } - - return; // No point continuing the loop. - } + mask[index] = (task.getStatus () == Task::pending) ? '-' + : (task.getStatus () == Task::completed) ? '+' + : (task.getStatus () == Task::deleted) ? 'X' + : (task.getStatus () == Task::waiting) ? 'W' + : '?'; } + else + { + std::string mask; + for (unsigned int i = 0; i < index; ++i) + mask += "?"; + + mask += (task.getStatus () == Task::pending) ? '-' + : (task.getStatus () == Task::completed) ? '+' + : (task.getStatus () == Task::deleted) ? 'X' + : (task.getStatus () == Task::waiting) ? 'W' + : '?'; + } + + parent.set ("mask", mask); + context.tdb2.modify (parent); } } diff --git a/test/recur.until.t b/test/recur.until.t index 0763439a5..9a5d76fa4 100755 --- a/test/recur.until.t +++ b/test/recur.until.t @@ -48,7 +48,10 @@ like ($output, qr/^\s+3/ms, 'Found 3'); like ($output, qr/^\s+4/ms, 'Found 4'); like ($output, qr/^\s+5/ms, 'Found 5'); -qx{../src/task rc:recur.rc $_ do} for 1..5; +qx{../src/task rc:recur.rc 2 do}; +qx{../src/task rc:recur.rc 3 do}; +qx{../src/task rc:recur.rc 4 do}; +qx{../src/task rc:recur.rc 5 do}; $output = qx{../src/task rc:recur.rc list}; like ($output, qr/and has been deleted/, 'Parent task deleted');