Enhancement - Hooks

- Implemented pre-delete, post-delete events.
- Implemented pre-delete-command, post-delete-command events.
This commit is contained in:
Paul Beckingham 2010-01-23 13:43:50 -05:00
parent b02374c3f5
commit d6daa336ca
2 changed files with 108 additions and 94 deletions

View file

@ -174,17 +174,18 @@ bool Hooks::trigger (const std::string& event)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
bool Hooks::eventType (const std::string& event, std::string& type) bool Hooks::eventType (const std::string& event, std::string& type)
{ {
if (event == "post-start" || if (event == "post-start" ||
event == "pre-exit" || event == "pre-exit" ||
event == "pre-debug" || event == "post-debug" || event == "pre-debug" || event == "post-debug" ||
event == "pre-header" || event == "post-header" || event == "pre-header" || event == "post-header" ||
event == "pre-footnote" || event == "post-footnote" || event == "pre-footnote" || event == "post-footnote" ||
event == "pre-output" || event == "post-output" || event == "pre-output" || event == "post-output" ||
event == "pre-dispatch" || event == "post-dispatch" || event == "pre-dispatch" || event == "post-dispatch" ||
event == "pre-gc" || event == "post-gc" || event == "pre-gc" || event == "post-gc" ||
event == "pre-undo" || event == "post-undo" || event == "pre-undo" || event == "post-undo" ||
event == "pre-file-lock" || event == "post-file-lock" || event == "pre-file-lock" || event == "post-file-lock" ||
event == "pre-add-command" || event == "post-add-command") event == "pre-add-command" || event == "post-add-command" ||
event == "pre-delete-command" || event == "post-delete-command")
{ {
type = "program"; type = "program";
return true; return true;
@ -196,6 +197,7 @@ bool Hooks::eventType (const std::string& event, std::string& type)
} }
else if (event == "pre-tag" || event == "post-tag" || else if (event == "pre-tag" || event == "post-tag" ||
event == "pre-detag" || event == "post-detag" || event == "pre-detag" || event == "post-detag" ||
event == "pre-delete" || event == "post-delete" ||
event == "pre-completed" || event == "post-completed") event == "pre-completed" || event == "post-completed")
{ {
type = "task"; type = "task";

View file

@ -742,104 +742,116 @@ int handleConfig (std::string &outs)
int handleDelete (std::string &outs) int handleDelete (std::string &outs)
{ {
int rc = 0; int rc = 0;
std::stringstream out;
context.disallowModification (); if (context.hooks.trigger ("pre-delete-command"))
std::vector <Task> tasks;
context.tdb.lock (context.config.getBoolean ("locking"));
Filter filter;
context.tdb.loadPending (tasks, filter);
// Filter sequence.
std::vector <Task> all = tasks;
context.filter.applySequence (tasks, context.sequence);
// Determine the end date.
char endTime[16];
sprintf (endTime, "%u", (unsigned int) time (NULL));
foreach (task, tasks)
{ {
std::stringstream question; std::stringstream out;
question << "Permanently delete task "
<< task->id
<< " '"
<< task->get ("description")
<< "'?";
if (!context.config.getBoolean ("confirmation") || confirm (question.str ())) context.disallowModification ();
std::vector <Task> tasks;
context.tdb.lock (context.config.getBoolean ("locking"));
Filter filter;
context.tdb.loadPending (tasks, filter);
// Filter sequence.
std::vector <Task> all = tasks;
context.filter.applySequence (tasks, context.sequence);
// Determine the end date.
char endTime[16];
sprintf (endTime, "%u", (unsigned int) time (NULL));
foreach (task, tasks)
{ {
// Check for the more complex case of a recurring task. If this is a context.hooks.setTaskId (task->id);
// recurring task, get confirmation to delete them all. if (context.hooks.trigger ("pre-delete"))
std::string parent = task->get ("parent");
if (parent != "")
{ {
if (confirm ("This is a recurring task. Do you want to delete all pending recurrences of this same task?")) std::stringstream question;
{ question << "Permanently delete task "
// Scan all pending tasks for siblings of this task, and the parent << task->id
// itself, and delete them. << " '"
foreach (sibling, all) << task->get ("description")
{ << "'?";
if (sibling->get ("parent") == parent ||
sibling->get ("uuid") == parent)
{
sibling->setStatus (Task::deleted);
sibling->set ("end", endTime);
context.tdb.update (*sibling);
if (context.config.getBoolean ("echo.command")) if (!context.config.getBoolean ("confirmation") || confirm (question.str ()))
out << "Deleting recurring task " {
<< sibling->id // Check for the more complex case of a recurring task. If this is a
<< " '" // recurring task, get confirmation to delete them all.
<< sibling->get ("description") std::string parent = task->get ("parent");
<< "'" if (parent != "")
<< std::endl; {
if (confirm ("This is a recurring task. Do you want to delete all pending recurrences of this same task?"))
{
// Scan all pending tasks for siblings of this task, and the parent
// itself, and delete them.
foreach (sibling, all)
{
if (sibling->get ("parent") == parent ||
sibling->get ("uuid") == parent)
{
sibling->setStatus (Task::deleted);
sibling->set ("end", endTime);
context.tdb.update (*sibling);
if (context.config.getBoolean ("echo.command"))
out << "Deleting recurring task "
<< sibling->id
<< " '"
<< sibling->get ("description")
<< "'"
<< std::endl;
}
}
}
else
{
// Update mask in parent.
task->setStatus (Task::deleted);
updateRecurrenceMask (all, *task);
task->set ("end", endTime);
context.tdb.update (*task);
out << "Deleting recurring task "
<< task->id
<< " '"
<< task->get ("description")
<< "'"
<< std::endl;
} }
} }
else
{
task->setStatus (Task::deleted);
task->set ("end", endTime);
context.tdb.update (*task);
if (context.config.getBoolean ("echo.command"))
out << "Deleting task "
<< task->id
<< " '"
<< task->get ("description")
<< "'"
<< std::endl;
}
} }
else else {
{ out << "Task not deleted." << std::endl;
// Update mask in parent. rc = 1;
task->setStatus (Task::deleted);
updateRecurrenceMask (all, *task);
task->set ("end", endTime);
context.tdb.update (*task);
out << "Deleting recurring task "
<< task->id
<< " '"
<< task->get ("description")
<< "'"
<< std::endl;
} }
}
else
{
task->setStatus (Task::deleted);
task->set ("end", endTime);
context.tdb.update (*task);
if (context.config.getBoolean ("echo.command")) context.hooks.trigger ("post-delete");
out << "Deleting task "
<< task->id
<< " '"
<< task->get ("description")
<< "'"
<< std::endl;
} }
} }
else {
out << "Task not deleted." << std::endl; context.tdb.commit ();
rc = 1; context.tdb.unlock ();
}
outs = out.str ();
context.hooks.trigger ("post-delete-command");
} }
context.tdb.commit ();
context.tdb.unlock ();
outs = out.str ();
return rc; return rc;
} }