From 98316f7ab12f2a4ed5dbf729d090cf9a063705f1 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Mon, 15 Jun 2009 01:44:42 -0400 Subject: [PATCH] Enhancement - append - Implemented append command. --- src/Context.cpp | 4 +- src/command.cpp | 102 ++++++++++++++++++++++++------------------------ src/main.h | 10 ++--- 3 files changed, 59 insertions(+), 57 deletions(-) diff --git a/src/Context.cpp b/src/Context.cpp index b2ed08e82..a3964ad24 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -162,7 +162,7 @@ std::string Context::dispatch () int gcMod = 0; // Change occurred by way of gc. std::string out; - // TODO Just look at this thing. It just cries out for a dispatch table. + // TODO Just look at this thing. It cries out for a dispatch table. if (cmd.command == "projects") { out = handleProjects (); } else if (cmd.command == "tags") { out = handleTags (); } else if (cmd.command == "colors") { out = handleColor (); } @@ -178,7 +178,9 @@ std::string Context::dispatch () else if (cmd.command == "add") { out = handleAdd (); } /* else if (cmd.command == "" && task.getId ()) { out = handleModify (); } +*/ else if (cmd.command == "append") { out = handleAppend (); } +/* else if (cmd.command == "annotate") { out = handleAnnotate (); } else if (cmd.command == "done") { out = handleDone (); } else if (cmd.command == "undelete") { out = handleUndelete (); } diff --git a/src/command.cpp b/src/command.cpp index be85c3def..1775af503 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -745,42 +745,48 @@ std::string handleModify () //////////////////////////////////////////////////////////////////////////////// std::string handleAppend () { -/* int count = 0; -*/ std::stringstream out; -/* - std::vector all; - tdb.allPendingT (all); - std::vector filtered = all; - filterSequence (filtered, task); - foreach (seq, filtered) + std::vector tasks; + context.tdb.lock (context.config.get ("locking", true)); + context.tdb.loadPending (tasks, context.filter); + handleRecurrence (tasks); + + // Filter sequence. + std::vector all = tasks; + context.filter.applySequence (tasks, context.sequence); + + foreach (task, tasks) { foreach (other, all) { - if (other->getId () == seq->getId () || // Self - (seq->has ("parent") && - seq->getAttribute ("parent") == other->getAttribute ("parent")) || // Sibling - other->getUUID () == seq->getAttribute ("parent")) // Parent + if (other->id == task->id || // Self + (task->has ("parent") && + task->get ("parent") == other->get ("parent")) || // Sibling + other->get ("uuid") == task->get ("parent")) // Parent { // A non-zero value forces a file write. int changes = 0; // Apply other deltas. - changes += deltaAppend (*other, task); - changes += deltaTags (*other, task); - changes += deltaAttributes (*other, task); + changes += deltaAppend (*other); + context.task.remove ("description"); + + changes += deltaTags (*other); + context.task.remove ("tags"); + + changes += deltaAttributes (*other); if (changes) { - tdb.modifyT (*other); + context.tdb.update (*other); if (context.config.get ("echo.command", true)) out << "Appended '" - << task.getDescription () + << context.task.get ("description") << "' to task " - << other->getId () + << other->id << std::endl; } @@ -789,9 +795,12 @@ std::string handleAppend () } } + context.tdb.commit (); + context.tdb.unlock (); + if (context.config.get ("echo.command", true)) out << "Appended " << count << " task" << (count == 1 ? "" : "s") << std::endl; -*/ + return out.str (); } @@ -974,12 +983,12 @@ std::string handleAnnotate () } //////////////////////////////////////////////////////////////////////////////// -int deltaAppend (Task& task, Task& delta) +int deltaAppend (Task& task) { - if (delta.has ("description")) + if (context.task.has ("description")) { task.set ("description", - task.get ("description") + " " + delta.get ("description")); + task.get ("description") + " " + context.task.get ("description")); return 1; } @@ -987,11 +996,11 @@ int deltaAppend (Task& task, Task& delta) } //////////////////////////////////////////////////////////////////////////////// -int deltaDescription (Task& task, Task& delta) +int deltaDescription (Task& task) { - if (delta.has ("description")) + if (context.task.has ("description")) { - task.set ("description", delta.get ("description")); + task.set ("description", context.task.get ("description")); return 1; } @@ -999,60 +1008,51 @@ int deltaDescription (Task& task, Task& delta) } //////////////////////////////////////////////////////////////////////////////// -int deltaTags (Task& task, Task& delta) +int deltaTags (Task& task) { int changes = 0; // Apply or remove tags, if any. std::vector tags; - delta.getTags (tags); - for (unsigned int i = 0; i < tags.size (); ++i) + context.task.getTags (tags); + foreach (tag, tags) { - if (tags[i][0] == '+') - task.addTag (tags[i].substr (1, std::string::npos)); - else - task.addTag (tags[i]); - + task.addTag (*tag); ++changes; } -/* - // TODO Needs Task::getRemoveTags - delta.getRemoveTags (tags); - for (unsigned int i = 0; i < tags.size (); ++i) + foreach (tag, context.tagRemovals) { - if (tags[i][0] == '-') - task.removeTag (tags[i].substr (1, std::string::npos)); - else - task.removeTag (tags[i]); - + task.removeTag (*tag); ++changes; } -*/ return changes; } //////////////////////////////////////////////////////////////////////////////// -int deltaAttributes (Task& task, Task& delta) +int deltaAttributes (Task& task) { int changes = 0; - foreach (att, delta) + foreach (att, context.task) { - if (att->second.value () == "") - task.remove (att->first); - else - task.set (att->first, att->second.value ()); + if (att->first != "uuid") + { + if (att->second.value () == "") + task.remove (att->first); + else + task.set (att->first, att->second.value ()); - ++changes; + ++changes; + } } return changes; } //////////////////////////////////////////////////////////////////////////////// -int deltaSubstitutions (Task& task, Task& delta) +int deltaSubstitutions (Task& task) { std::string description = task.get ("description"); std::vector annotations; diff --git a/src/main.h b/src/main.h index b1471c82a..d630c7385 100644 --- a/src/main.h +++ b/src/main.h @@ -69,11 +69,11 @@ std::string handleUndo (); std::string handleColor (); std::string handleAnnotate (); std::string handleDuplicate (); -int deltaAppend (Task&, Task&); -int deltaDescription (Task&, Task&); -int deltaTags (Task&, Task&); -int deltaAttributes (Task&, Task&); -int deltaSubstitutions (Task&, Task&); +int deltaAppend (Task&); +int deltaDescription (Task&); +int deltaTags (Task&); +int deltaAttributes (Task&); +int deltaSubstitutions (Task&); // edit.cpp std::string handleEdit ();