Enhancement - gc/shadow

- shadow file update is now triggered by the type of command, not
  explicitly by name.
- Context::inShadow now keeps track of whether it is running a shadow
  file update to prevent recursion.
This commit is contained in:
Paul Beckingham 2009-06-12 22:45:42 -04:00
parent 788e264378
commit 5288e167d0
3 changed files with 63 additions and 66 deletions

View file

@ -107,13 +107,11 @@ void Cmd::loadCommands ()
{ {
if (commands.size () == 0) if (commands.size () == 0)
{ {
commands.push_back (context.stringtable.get (CMD_ACTIVE, "active"));
commands.push_back (context.stringtable.get (CMD_ADD, "add")); commands.push_back (context.stringtable.get (CMD_ADD, "add"));
commands.push_back (context.stringtable.get (CMD_APPEND, "append")); commands.push_back (context.stringtable.get (CMD_APPEND, "append"));
commands.push_back (context.stringtable.get (CMD_ANNOTATE, "annotate")); commands.push_back (context.stringtable.get (CMD_ANNOTATE, "annotate"));
commands.push_back (context.stringtable.get (CMD_CALENDAR, "calendar")); commands.push_back (context.stringtable.get (CMD_CALENDAR, "calendar"));
commands.push_back (context.stringtable.get (CMD_COLORS, "colors")); commands.push_back (context.stringtable.get (CMD_COLORS, "colors"));
commands.push_back (context.stringtable.get (CMD_COMPLETED, "completed"));
commands.push_back (context.stringtable.get (CMD_DELETE, "delete")); commands.push_back (context.stringtable.get (CMD_DELETE, "delete"));
commands.push_back (context.stringtable.get (CMD_DONE, "done")); commands.push_back (context.stringtable.get (CMD_DONE, "done"));
commands.push_back (context.stringtable.get (CMD_DUPLICATE, "duplicate")); commands.push_back (context.stringtable.get (CMD_DUPLICATE, "duplicate"));
@ -125,7 +123,6 @@ void Cmd::loadCommands ()
commands.push_back (context.stringtable.get (CMD_IMPORT, "import")); commands.push_back (context.stringtable.get (CMD_IMPORT, "import"));
commands.push_back (context.stringtable.get (CMD_INFO, "info")); commands.push_back (context.stringtable.get (CMD_INFO, "info"));
commands.push_back (context.stringtable.get (CMD_NEXT, "next")); commands.push_back (context.stringtable.get (CMD_NEXT, "next"));
commands.push_back (context.stringtable.get (CMD_OVERDUE, "overdue"));
commands.push_back (context.stringtable.get (CMD_PROJECTS, "projects")); commands.push_back (context.stringtable.get (CMD_PROJECTS, "projects"));
commands.push_back (context.stringtable.get (CMD_START, "start")); commands.push_back (context.stringtable.get (CMD_START, "start"));
commands.push_back (context.stringtable.get (CMD_STATS, "stats")); commands.push_back (context.stringtable.get (CMD_STATS, "stats"));
@ -176,23 +173,21 @@ void Cmd::allCustomReports (std::vector <std::string>& all) const
// Commands that do not directly modify the data files. // Commands that do not directly modify the data files.
bool Cmd::isReadOnlyCommand () bool Cmd::isReadOnlyCommand ()
{ {
if (command == context.stringtable.get (CMD_ACTIVE, "active") || // R if (command == context.stringtable.get (CMD_CALENDAR, "calendar") ||
command == context.stringtable.get (CMD_CALENDAR, "calendar") || // R command == context.stringtable.get (CMD_COLORS, "colors") ||
command == context.stringtable.get (CMD_COLORS, "colors") || // R command == context.stringtable.get (CMD_EXPORT, "export") ||
command == context.stringtable.get (CMD_COMPLETED, "completed") || // R command == context.stringtable.get (CMD_HELP, "help") ||
command == context.stringtable.get (CMD_EXPORT, "export") || // R command == context.stringtable.get (CMD_HISTORY, "history") ||
command == context.stringtable.get (CMD_HELP, "help") || // R command == context.stringtable.get (CMD_GHISTORY, "ghistory") ||
command == context.stringtable.get (CMD_HISTORY, "history") || // R command == context.stringtable.get (CMD_INFO, "info") ||
command == context.stringtable.get (CMD_GHISTORY, "ghistory") || // R command == context.stringtable.get (CMD_NEXT, "next") ||
command == context.stringtable.get (CMD_INFO, "info") || // R command == context.stringtable.get (CMD_PROJECTS, "projects") ||
command == context.stringtable.get (CMD_NEXT, "next") || // R command == context.stringtable.get (CMD_STATS, "stats") ||
command == context.stringtable.get (CMD_OVERDUE, "overdue") || // R command == context.stringtable.get (CMD_SUMMARY, "summary") ||
command == context.stringtable.get (CMD_PROJECTS, "projects") || // R command == context.stringtable.get (CMD_TAGS, "tags") ||
command == context.stringtable.get (CMD_STATS, "stats") || // R command == context.stringtable.get (CMD_TIMESHEET, "timesheet") ||
command == context.stringtable.get (CMD_SUMMARY, "summary") || // R command == context.stringtable.get (CMD_VERSION, "version") ||
command == context.stringtable.get (CMD_TAGS, "tags") || // R validCustom (command))
command == context.stringtable.get (CMD_TIMESHEET, "timesheet") || // R
command == context.stringtable.get (CMD_VERSION, "version")) // R
return true; return true;
return false; return false;
@ -202,18 +197,18 @@ bool Cmd::isReadOnlyCommand ()
// Commands that directly modify the data files. // Commands that directly modify the data files.
bool Cmd::isWriteCommand () bool Cmd::isWriteCommand ()
{ {
if (command == context.stringtable.get (CMD_ADD, "add") || // W if (command == context.stringtable.get (CMD_ADD, "add") ||
command == context.stringtable.get (CMD_APPEND, "append") || // W command == context.stringtable.get (CMD_APPEND, "append") ||
command == context.stringtable.get (CMD_ANNOTATE, "annotate") || // W command == context.stringtable.get (CMD_ANNOTATE, "annotate") ||
command == context.stringtable.get (CMD_DELETE, "delete") || // W command == context.stringtable.get (CMD_DELETE, "delete") ||
command == context.stringtable.get (CMD_DONE, "done") || // W command == context.stringtable.get (CMD_DONE, "done") ||
command == context.stringtable.get (CMD_DUPLICATE, "duplicate") || // W command == context.stringtable.get (CMD_DUPLICATE, "duplicate") ||
command == context.stringtable.get (CMD_EDIT, "edit") || // W command == context.stringtable.get (CMD_EDIT, "edit") ||
command == context.stringtable.get (CMD_IMPORT, "import") || // W command == context.stringtable.get (CMD_IMPORT, "import") ||
command == context.stringtable.get (CMD_START, "start") || // W command == context.stringtable.get (CMD_START, "start") ||
command == context.stringtable.get (CMD_STOP, "stop") || // W command == context.stringtable.get (CMD_STOP, "stop") ||
command == context.stringtable.get (CMD_UNDELETE, "undelete") || // W command == context.stringtable.get (CMD_UNDELETE, "undelete") ||
command == context.stringtable.get (CMD_UNDO, "undo")) // W command == context.stringtable.get (CMD_UNDO, "undo"))
return true; return true;
return false; return false;

View file

@ -49,6 +49,7 @@ Context::Context ()
, stringtable () , stringtable ()
, program ("") , program ("")
, cmd () , cmd ()
, inShadow (false)
{ {
// Set up randomness. // Set up randomness.
#ifdef HAVE_SRANDOM #ifdef HAVE_SRANDOM
@ -158,61 +159,57 @@ int Context::run ()
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
std::string Context::dispatch () std::string Context::dispatch ()
{ {
bool gc = true; // TODO Should be false for shadow file updates. int gcMod = 0; // Change occurred by way of gc.
bool gcMod = false; // Change occurred by way of gc.
bool cmdMod = false; // Change occurred by way of command type.
std::string out; std::string out;
/*
// Read-only commands with no side effects. // Read-only commands with no side effects.
if (command == "export") { out = handleExport (); } /*
if (cmd.command == "export") { out = handleExport (); }
*/ */
if (cmd.command == "projects") { out = handleProjects (); } if (cmd.command == "projects") { out = handleProjects (); }
else if (cmd.command == "tags") { out = handleTags (); } else if (cmd.command == "tags") { out = handleTags (); }
else if (cmd.command == "colors") { out = handleColor (); } else if (cmd.command == "colors") { out = handleColor (); }
else if (cmd.command == "version") { out = handleVersion (); } else if (cmd.command == "version") { out = handleVersion (); }
else if (cmd.command == "help") { out = longUsage (); } else if (cmd.command == "help") { out = longUsage (); }
else if (cmd.command == "stats") { out = handleReportStats (); } else if (cmd.command == "stats") { out = handleReportStats (); }
/* /*
else if (command == "info") { out = handleInfo (); } else if (cmd.command == "info") { out = handleInfo (); }
else if (command == "history") { out = handleReportHistory (); } else if (cmd.command == "history") { out = handleReportHistory (); }
else if (command == "ghistory") { out = handleReportGHistory (); } else if (cmd.command == "ghistory") { out = handleReportGHistory (); }
else if (command == "calendar") { out = handleReportCalendar (); } else if (cmd.command == "calendar") { out = handleReportCalendar (); }
else if (command == "summary") { out = handleReportSummary (); } else if (cmd.command == "summary") { out = handleReportSummary (); }
else if (command == "timesheet") { out = handleReportTimesheet (); } else if (cmd.command == "timesheet") { out = handleReportTimesheet (); }
*/ */
// Commands that cause updates. // Commands that cause updates.
else if (cmd.command == "add") { cmdMod = true; out = handleAdd (); } else if (cmd.command == "add") { out = handleAdd (); }
/* /*
else if (command == "" && task.getId ()) { cmdMod = true; out = handleModify (); } else if (command == "" && task.getId ()) { out = handleModify (); }
else if (command == "append") { cmdMod = true; out = handleAppend (); } else if (command == "append") { out = handleAppend (); }
else if (command == "annotate") { cmdMod = true; out = handleAnnotate (); } else if (command == "annotate") { out = handleAnnotate (); }
else if (command == "done") { cmdMod = true; out = handleDone (); } else if (command == "done") { out = handleDone (); }
else if (command == "undelete") { cmdMod = true; out = handleUndelete (); } else if (command == "undelete") { out = handleUndelete (); }
else if (command == "delete") { cmdMod = true; out = handleDelete (); } else if (command == "delete") { out = handleDelete (); }
else if (command == "start") { cmdMod = true; out = handleStart (); } else if (command == "start") { out = handleStart (); }
else if (command == "stop") { cmdMod = true; out = handleStop (); } else if (command == "stop") { out = handleStop (); }
else if (command == "undo") { cmdMod = true; out = handleUndo (); } else if (command == "undo") { out = handleUndo (); }
else if (command == "import") { cmdMod = true; out = handleImport (); } else if (command == "import") { out = handleImport (); }
else if (command == "duplicate") { cmdMod = true; out = handleDuplicate (); } else if (command == "duplicate") { out = handleDuplicate (); }
else if (command == "edit") { cmdMod = true; out = handleEdit (); } else if (command == "edit") { out = handleEdit (); }
*/ */
// Command that display IDs and therefore need TDB::gc first. // Command that display IDs and therefore need TDB::gc first.
/* /*
else if (command == "next") { if (gc) gcMod = tdb.gc (); out = handleReportNext (); } else if (command == "next") { if (!inShadow) gcMod = tdb.gc (); out = handleReportNext (); }
*/ */
else if (cmd.validCustom (cmd.command)) { if (gc) gcMod = tdb.gc (); out = handleCustomReport (cmd.command); } else if (cmd.validCustom (cmd.command)) { if (!inShadow) gcMod = tdb.gc (); out = handleCustomReport (cmd.command); }
// If the command is not recognized, display usage. // If the command is not recognized, display usage.
else { out = shortUsage (); } else { out = shortUsage (); }
// Only update the shadow file if such an update was not suppressed (shadow), // Only update the shadow file if such an update was not suppressed (shadow),
// and if an actual change occurred (gcMod || cmdMod).
// TODO // TODO
// if (shadow && (gcMod || cmdMod)) // if (cmd.isWriteCommand (cmd.command) && !inShadow))
// shadow (); // shadow ();
return out; return out;
@ -225,6 +222,8 @@ void Context::shadow ()
std::string shadowFile = expandPath (config.get ("shadow.file")); std::string shadowFile = expandPath (config.get ("shadow.file"));
if (shadowFile != "") if (shadowFile != "")
{ {
inShadow = true; // Prevents recursion in case shadow command writes.
// TODO Reinstate these checks. // TODO Reinstate these checks.
/* /*
// Check for silly shadow file settings. // Check for silly shadow file settings.
@ -267,6 +266,8 @@ void Context::shadow ()
// Optionally display a notification that the shadow file was updated. // Optionally display a notification that the shadow file was updated.
if (config.get (std::string ("shadow.notify"), false)) if (config.get (std::string ("shadow.notify"), false))
footnote (std::string ("[Shadow file '") + shadowFile + "' updated]"); footnote (std::string ("[Shadow file '") + shadowFile + "' updated]");
inShadow = false;
} }
} }

View file

@ -78,6 +78,7 @@ public:
private: private:
std::vector <std::string> messages; std::vector <std::string> messages;
std::vector <std::string> footnotes; std::vector <std::string> footnotes;
bool inShadow;
}; };
#endif #endif