Enhancement - Hooks
- Implemented all command hooks. - Implemented several field hooks. - Implemented several task hooks. - Reorganized event validation code. - Finalized Hooks -> API::call* mechanism. - Implemented several hook unit tests. - Corrected unit tests that didn't specify rc.hooks=on. - Corrected builds that include Lua.
This commit is contained in:
252
src/Hooks.cpp
252
src/Hooks.cpp
@@ -139,14 +139,7 @@ void Hooks::initialize ()
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void Hooks::setTaskId (int id)
|
||||
{
|
||||
#ifdef HAVE_LIBLUA
|
||||
task_id = id;
|
||||
#endif
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Program hooks.
|
||||
bool Hooks::trigger (const std::string& event)
|
||||
{
|
||||
#ifdef HAVE_LIBLUA
|
||||
@@ -157,24 +150,14 @@ bool Hooks::trigger (const std::string& event)
|
||||
{
|
||||
Timer timer (std::string ("Hooks::trigger ") + event);
|
||||
|
||||
bool rc = true;
|
||||
std::string type;
|
||||
if (eventType (event, type))
|
||||
if (validProgramEvent (event))
|
||||
{
|
||||
context.debug (std::string ("Event ") + event + " triggered");
|
||||
|
||||
// Figure out where to get the calling-context info from.
|
||||
if (type == "program") rc = api.callProgramHook (it->file, it->function);
|
||||
else if (type == "list") rc = api.callListHook (it->file, it->function/*, tasks*/);
|
||||
else if (type == "task") rc = api.callTaskHook (it->file, it->function, task_id);
|
||||
else if (type == "field") rc = api.callFieldHook (it->file, it->function, "field", "value");
|
||||
if (! api.callProgramHook (it->file, it->function))
|
||||
return false;
|
||||
}
|
||||
else
|
||||
throw std::string ("Unrecognized hook event '") + event + "'";
|
||||
|
||||
// If any hook returns false, stop.
|
||||
if (!rc)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -183,43 +166,212 @@ bool Hooks::trigger (const std::string& event)
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
bool Hooks::eventType (const std::string& event, std::string& type)
|
||||
// List hooks.
|
||||
bool Hooks::trigger (const std::string& event, std::vector <Task>& tasks)
|
||||
{
|
||||
if (event == "post-start" ||
|
||||
event == "pre-exit" ||
|
||||
event == "pre-debug" || event == "post-debug" ||
|
||||
event == "pre-header" || event == "post-header" ||
|
||||
event == "pre-footnote" || event == "post-footnote" ||
|
||||
event == "pre-output" || event == "post-output" ||
|
||||
event == "pre-dispatch" || event == "post-dispatch" ||
|
||||
event == "pre-gc" || event == "post-gc" ||
|
||||
event == "pre-undo" || event == "post-undo" ||
|
||||
event == "pre-file-lock" || event == "post-file-lock" ||
|
||||
event == "pre-add-command" || event == "post-add-command" ||
|
||||
event == "pre-delete-command" || event == "post-delete-command" ||
|
||||
event == "pre-info-command" || event == "post-info-command")
|
||||
#ifdef HAVE_LIBLUA
|
||||
std::vector <Hook>::iterator it;
|
||||
for (it = all.begin (); it != all.end (); ++it)
|
||||
{
|
||||
type = "program";
|
||||
return true;
|
||||
if (it->event == event)
|
||||
{
|
||||
Timer timer (std::string ("Hooks::trigger ") + event);
|
||||
|
||||
if (validListEvent (event))
|
||||
{
|
||||
context.debug (std::string ("Event ") + event + " triggered");
|
||||
if (! api.callListHook (it->file, it->function, tasks))
|
||||
return false;
|
||||
}
|
||||
else
|
||||
throw std::string ("Unrecognized hook event '") + event + "'";
|
||||
}
|
||||
}
|
||||
else if (event == "?")
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Task hooks.
|
||||
bool Hooks::trigger (const std::string& event, Task& task)
|
||||
{
|
||||
#ifdef HAVE_LIBLUA
|
||||
std::vector <Hook>::iterator it;
|
||||
for (it = all.begin (); it != all.end (); ++it)
|
||||
{
|
||||
type = "list";
|
||||
return true;
|
||||
if (it->event == event)
|
||||
{
|
||||
Timer timer (std::string ("Hooks::trigger ") + event);
|
||||
|
||||
if (validTaskEvent (event))
|
||||
{
|
||||
context.debug (std::string ("Event ") + event + " triggered");
|
||||
if (! api.callTaskHook (it->file, it->function, task))
|
||||
return false;
|
||||
}
|
||||
else
|
||||
throw std::string ("Unrecognized hook event '") + event + "'";
|
||||
}
|
||||
}
|
||||
else if (event == "pre-tag" || event == "post-tag" ||
|
||||
event == "pre-detag" || event == "post-detag" ||
|
||||
event == "pre-delete" || event == "post-delete" ||
|
||||
event == "pre-completed" || event == "post-completed")
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Field hooks.
|
||||
bool Hooks::trigger (
|
||||
const std::string& event,
|
||||
const std::string& name,
|
||||
std::string& value)
|
||||
{
|
||||
#ifdef HAVE_LIBLUA
|
||||
std::vector <Hook>::iterator it;
|
||||
for (it = all.begin (); it != all.end (); ++it)
|
||||
{
|
||||
type = "task";
|
||||
return true;
|
||||
if (it->event == event)
|
||||
{
|
||||
Timer timer (std::string ("Hooks::trigger ") + event);
|
||||
|
||||
if (validFieldEvent (event))
|
||||
{
|
||||
context.debug (std::string ("Event ") + event + " triggered");
|
||||
if (! api.callFieldHook (it->file, it->function, name, value))
|
||||
return false;
|
||||
}
|
||||
else
|
||||
throw std::string ("Unrecognized hook event '") + event + "'";
|
||||
}
|
||||
}
|
||||
else if (event == "?")
|
||||
{
|
||||
type = "field";
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
bool Hooks::validProgramEvent (const std::string& event)
|
||||
{
|
||||
if (event == "post-start" ||
|
||||
event == "pre-fatal-error" ||
|
||||
event == "pre-exit" ||
|
||||
event == "pre-command-line" || event == "post-command-line" ||
|
||||
event == "pre-command-line-override" || event == "post-command-line-override" ||
|
||||
event == "pre-config-create" || event == "post-config-create" ||
|
||||
event == "pre-config-read" || event == "post-config-read" ||
|
||||
event == "pre-config-value-read" || event == "post-config-value-read" ||
|
||||
event == "pre-config-value-write" || event == "post-config-value-write" ||
|
||||
event == "pre-file-lock" || event == "post-file-lock" ||
|
||||
event == "pre-file-unlock" || event == "post-file-unlock" ||
|
||||
event == "pre-file-read" || event == "post-file-read" ||
|
||||
event == "pre-file-write" || event == "post-file-write" ||
|
||||
event == "pre-output" || event == "post-output" ||
|
||||
event == "pre-debug" || event == "post-debug" ||
|
||||
event == "pre-header" || event == "post-header" ||
|
||||
event == "pre-footnote" || event == "post-footnote" ||
|
||||
event == "pre-dispatch" || event == "post-dispatch" ||
|
||||
event == "pre-gc" || event == "post-gc" ||
|
||||
event == "pre-archive" || event == "post-archive" ||
|
||||
event == "pre-purge" || event == "post-purge" ||
|
||||
event == "pre-recurrence" || event == "post-recurrence" ||
|
||||
event == "pre-interactive" || event == "post-interactive" ||
|
||||
event == "pre-undo" || event == "post-undo" ||
|
||||
event == "pre-confirm" || event == "post-confirm" ||
|
||||
event == "pre-shell-prompt" || event == "post-shell-prompt" ||
|
||||
event == "pre-add-command" || event == "post-add-command" ||
|
||||
event == "pre-annotate-command" || event == "post-annotate-command" ||
|
||||
event == "pre-append-command" || event == "post-append-command" ||
|
||||
event == "pre-calendar-command" || event == "post-calendar-command" ||
|
||||
event == "pre-color-command" || event == "post-color-command" ||
|
||||
event == "pre-config-command" || event == "post-config-command" ||
|
||||
event == "pre-custom-report-command" || event == "post-custom-report-command" ||
|
||||
event == "pre-default-command" || event == "post-default-command" ||
|
||||
event == "pre-delete-command" || event == "post-delete-command" ||
|
||||
event == "pre-done-command" || event == "post-done-command" ||
|
||||
event == "pre-duplicate-command" || event == "post-duplicate-command" ||
|
||||
event == "pre-edit-command" || event == "post-edit-command" ||
|
||||
event == "pre-export-command" || event == "post-export-command" ||
|
||||
event == "pre-ghistory-command" || event == "post-ghistory-command" ||
|
||||
event == "pre-history-command" || event == "post-history-command" ||
|
||||
event == "pre-import-command" || event == "post-import-command" ||
|
||||
event == "pre-info-command" || event == "post-info-command" ||
|
||||
event == "pre-prepend-command" || event == "post-prepend-command" ||
|
||||
event == "pre-projects-command" || event == "post-projects-command" ||
|
||||
event == "pre-shell-command" || event == "post-shell-command" ||
|
||||
event == "pre-start-command" || event == "post-start-command" ||
|
||||
event == "pre-stats-command" || event == "post-stats-command" ||
|
||||
event == "pre-stop-command" || event == "post-stop-command" ||
|
||||
event == "pre-summary-command" || event == "post-summary-command" ||
|
||||
event == "pre-tags-command" || event == "post-tags-command" ||
|
||||
event == "pre-timesheet-command" || event == "post-timesheet-command" ||
|
||||
event == "pre-undo-command" || event == "post-undo-command" ||
|
||||
event == "pre-usage-command" || event == "post-usage-command" ||
|
||||
event == "pre-version-command" || event == "post-version-command")
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Hooks::validListEvent (const std::string& event)
|
||||
{
|
||||
if (event == "pre-filter" || event == "post-filter")
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Hooks::validTaskEvent (const std::string& event)
|
||||
{
|
||||
if (event == "pre-display" ||
|
||||
event == "pre-modification" || event == "post-modification" ||
|
||||
event == "pre-delete" || event == "post-delete" ||
|
||||
event == "pre-add" || event == "post-add" ||
|
||||
event == "pre-undo" || event == "post-undo" ||
|
||||
event == "pre-wait" || event == "post-wait" ||
|
||||
event == "pre-unwait" || event == "post-unwait" ||
|
||||
event == "pre-completed" || event == "post-completed" ||
|
||||
event == "pre-priority-change" || event == "post-priority-change" ||
|
||||
event == "pre-project-change" || event == "post-project-change" ||
|
||||
event == "pre-substitution" || event == "post-substitution" ||
|
||||
event == "pre-annotation" || event == "post-annotation" ||
|
||||
event == "pre-tag" || event == "post-tag" ||
|
||||
event == "pre-detag" || event == "post-detag" ||
|
||||
event == "pre-colorization" || event == "post-colorization")
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Hooks::validFieldEvent (const std::string& event)
|
||||
{
|
||||
if (
|
||||
event == "format-number" ||
|
||||
event == "format-date" ||
|
||||
event == "format-duration" ||
|
||||
event == "format-text" ||
|
||||
event == "format-id" ||
|
||||
event == "format-uuid" ||
|
||||
event == "format-project" ||
|
||||
event == "format-priority" ||
|
||||
event == "format-priority_long" ||
|
||||
event == "format-entry" ||
|
||||
event == "format-entry_time" ||
|
||||
event == "format-start" ||
|
||||
event == "format-start_time" ||
|
||||
event == "format-end" ||
|
||||
event == "format-end_time" ||
|
||||
event == "format-due" ||
|
||||
event == "format-age" ||
|
||||
event == "format-age_compact" ||
|
||||
event == "format-active" ||
|
||||
event == "format-tags" ||
|
||||
event == "format-recur" ||
|
||||
event == "format-recurrence_indicator" ||
|
||||
event == "format-tag_indicator" ||
|
||||
event == "format-description_only" ||
|
||||
event == "format-description" ||
|
||||
event == "format-wait")
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user