void TDB2::update ( Task& task, const bool add_to_backlog, const bool addition /* = false */) { // Validate to add metadata. task.validate (false); // If the task already exists, it is a modification, else addition. Task original; if (not addition && get (task.get ("uuid"), original)) { // Update only if the tasks differ if (task == original) return; if (add_to_backlog) { // All locally modified tasks are timestamped, implicitly overwriting any // changes the user or hooks tried to apply to the "modified" attribute. task.setAsNow ("modified"); } // Update the task, wherever it is. if (!pending.modify_task (task)) completed.modify_task (task); // time <time> // old <task> // new <task> // --- undo.add_line ("time " + ISO8601d ().toEpochString () + "\n"); undo.add_line ("old " + original.composeF4 () + "\n"); undo.add_line ("new " + task.composeF4 () + "\n"); undo.add_line ("---\n"); } else { // Add new task to either pending or completed. std::string status = task.get ("status"); if (status == "completed" || status == "deleted") completed.add_task (task); else pending.add_task (task); // Add undo data lines: // time <time> // new <task> // --- undo.add_line ("time " + ISO8601d ().toEpochString () + "\n"); undo.add_line ("new " + task.composeF4 () + "\n"); undo.add_line ("---\n"); } // Add task to backlog. if (add_to_backlog) backlog.add_line (task.composeJSON () + "\n"); }
void TDB2::revert_backlog ( std::vector <std::string>& b, const std::string& uuid, const std::string& current, const std::string& prior) { std::string uuid_att = "\"uuid\":\"" + uuid + "\""; bool found = false; for (auto task = b.rbegin (); task != b.rend (); ++task) { if (task->find (uuid_att) != std::string::npos) { context.debug ("TDB::revert_backlog - task found in backlog.data"); found = true; // If this is a new task (no prior), then just remove it from the backlog. if (current != "" && prior == "") { // Yes, this is what is needed, when you want to erase using a reverse // iterator. b.erase ((++task).base ()); } // If this is a modification of some kind, add the prior to the backlog. else { Task t (prior); b.push_back (t.composeJSON ()); } break; } } if (!found) throw std::string (STRING_TDB2_UNDO_SYNCED); }