From c29682b91f1012cd1586cb655a2ebb46576fa5f8 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 28 Jun 2009 12:23:46 -0400 Subject: [PATCH] Enhancement - undo - Now creates/opens/closes undo.data file. - All new tasks are logged in undo.data file. --- src/Location.cpp | 3 +++ src/Location.h | 1 + src/TDB.cpp | 42 ++++++++++++++++++++++++++++++++++++------ src/TDB.h | 1 + 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/Location.cpp b/src/Location.cpp index 984a2da98..667eaebc4 100644 --- a/src/Location.cpp +++ b/src/Location.cpp @@ -32,6 +32,7 @@ Location::Location () : path ("") , pending (NULL) , completed (NULL) +, undo (NULL) { } @@ -47,6 +48,7 @@ Location::Location (const Location& other) path = other.path; pending = other.pending; completed = other.completed; + undo = other.undo; } //////////////////////////////////////////////////////////////////////////////// @@ -57,6 +59,7 @@ Location& Location::operator= (const Location& other) path = other.path; pending = other.pending; completed = other.completed; + undo = other.undo; } return *this; diff --git a/src/Location.h b/src/Location.h index 4a93ea7df..65cb66ee7 100644 --- a/src/Location.h +++ b/src/Location.h @@ -43,6 +43,7 @@ public: std::string path; FILE* pending; FILE* completed; + FILE* undo; }; #endif diff --git a/src/TDB.cpp b/src/TDB.cpp index f5efb43f0..81f1f21d5 100644 --- a/src/TDB.cpp +++ b/src/TDB.cpp @@ -124,6 +124,7 @@ void TDB::lock (bool lockFile /* = true */) { location->pending = openAndLock (location->path + "/pending.data"); location->completed = openAndLock (location->path + "/completed.data"); + location->undo = openAndLock (location->path + "/undo.data"); } mAllOpenAndLocked = true; @@ -147,6 +148,10 @@ void TDB::unlock () fflush (location->completed); fclose (location->completed); location->completed = NULL; + + fflush (location->undo); + fclose (location->undo); + location->completed = NULL; } mAllOpenAndLocked = false; @@ -275,9 +280,6 @@ int TDB::loadCompleted (std::vector & tasks, Filter& filter) char line[T_LINE_MAX]; foreach (location, mLocations) { - // TODO If the filter contains Status:x where x is not deleted or - // completed, then this can be skipped. - line_number = 1; file = location->path + "/completed.data"; @@ -315,7 +317,6 @@ int TDB::loadCompleted (std::vector & tasks, Filter& filter) } //////////////////////////////////////////////////////////////////////////////// -// TODO Write to transaction log. // Note: mLocations[0] is where all tasks are written. void TDB::add (const Task& task) { @@ -324,14 +325,12 @@ void TDB::add (const Task& task) } //////////////////////////////////////////////////////////////////////////////// -// TODO Write to transaction log. void TDB::update (const Task& task) { mModified.push_back (task); } //////////////////////////////////////////////////////////////////////////////// -// TODO Writes all, including comments // Interestingly, only the pending file gets written to. The completed file is // only modified by TDB::gc. int TDB::commit () @@ -351,6 +350,10 @@ int TDB::commit () fputs (task->composeF4 ().c_str (), mLocations[0].pending); } + fseek (mLocations[0].undo, 0, SEEK_END); + foreach (task, mNew) + writeUndo (*task, mLocations[0].undo); + mNew.clear (); return quantity; } @@ -377,6 +380,13 @@ int TDB::commit () foreach (task, mPending) fputs (task->composeF4 ().c_str (), mLocations[0].pending); } + +/* + // Update the undo log. + fseek (mLocations[0].undo, 0, SEEK_END); + foreach (task, mPending) + writeUndo (*task, mLocations[0].undo); +*/ } return quantity; @@ -498,3 +508,23 @@ FILE* TDB::openAndLock (const std::string& file) } //////////////////////////////////////////////////////////////////////////////// +void TDB::writeUndo (const Task& after, FILE* file) +{ + Timer t ("TDB::writeUndo"); + + // TODO Locate "before" task (match on uuid). + + fprintf (file, + "time %u\nnew %s\n---\n", + (unsigned int) time (NULL), + after.composeF4 ().c_str ()); +/* + fprintf (file, + "time %u\nold %s\nnew %s\n---\n", + (unsigned int) time (NULL), + before.composeF4 ().c_str (), + after.composeF4 ().c_str ()); +*/ +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/TDB.h b/src/TDB.h index 5112e4ad9..f4a42bf5b 100644 --- a/src/TDB.h +++ b/src/TDB.h @@ -64,6 +64,7 @@ public: private: FILE* openAndLock (const std::string&); + void writeUndo (const Task&, FILE*); private: std::vector mLocations;