Enhancement - Hooks

- Implemented API::callTaskHook.
- Implemented Hook object inside Hooks.cpp, not Hooks.h.
- Implemented Hooks.setTaskId to provide context for task hooks.
- Implemented pre-tag, post-tag, pre-detag, post-detag
  events.
- Implemented pre-file-lock, post-file-lock, pre-file-unlock, post-file-unlock
  events.
This commit is contained in:
Paul Beckingham 2010-01-23 12:47:48 -05:00
parent 21d5607af2
commit 03f7e0686f
6 changed files with 137 additions and 58 deletions

View file

@ -539,12 +539,44 @@ bool API::callTaskHook (
{ {
loadFile (file); loadFile (file);
// TODO Get function. // Get function.
// TODO Prepare args. lua_getglobal (L, function.c_str ());
// TODO Make call. if (!lua_isfunction (L, -1))
// TODO Get exit status. {
lua_pop (L, 1);
throw std::string ("The Lua function '") + function + "' was not found.";
}
return true; // Prepare args.
lua_pushnumber (L, id);
// Make call.
if (lua_pcall (L, 1, 2, 0) != 0)
throw std::string ("Error calling '") + function + "' - " + lua_tostring (L, -1);
// Call successful - get return values.
if (!lua_isnumber (L, -2))
throw std::string ("Error: '") + function + "' did not return a success indicator";
if (!lua_isstring (L, -1) && !lua_isnil (L, -1))
throw std::string ("Error: '") + function + "' did not return a message or nil";
int rc = lua_tointeger (L, -2);
const char* message = lua_tostring (L, -1);
if (rc == 0)
{
if (message)
context.footnote (std::string ("Warning: ") + message);
}
else
{
if (message)
throw std::string (message);
}
lua_pop (L, 1);
return rc == 0 ? true : false;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -567,6 +599,7 @@ bool API::callFieldHook (
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void API::loadFile (const std::string& file) void API::loadFile (const std::string& file)
{ {
// If the file is not loaded.
if (std::find (loaded.begin (), loaded.end (), file) == loaded.end ()) if (std::find (loaded.begin (), loaded.end (), file) == loaded.end ())
{ {
// Load the file, if possible. // Load the file, if possible.

View file

@ -31,6 +31,43 @@
extern Context context; extern Context context;
////////////////////////////////////////////////////////////////////////////////
Hook::Hook ()
: event ("")
, file ("")
, function ("")
{
}
////////////////////////////////////////////////////////////////////////////////
Hook::Hook (const std::string& e, const std::string& f, const std::string& fn)
: event (e)
, file (f)
, function (fn)
{
}
////////////////////////////////////////////////////////////////////////////////
Hook::Hook (const Hook& other)
{
event = other.event;
file = other.file;
function = other.function;
}
////////////////////////////////////////////////////////////////////////////////
Hook& Hook::operator= (const Hook& other)
{
if (this != &other)
{
event = other.event;
file = other.file;
function = other.function;
}
return *this;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Hooks::Hooks () Hooks::Hooks ()
{ {
@ -92,6 +129,14 @@ void Hooks::initialize ()
} }
} }
////////////////////////////////////////////////////////////////////////////////
void Hooks::setTaskId (int id)
{
#ifdef HAVE_LIBLUA
task_id = id;
#endif
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
bool Hooks::trigger (const std::string& event) bool Hooks::trigger (const std::string& event)
{ {
@ -105,10 +150,10 @@ bool Hooks::trigger (const std::string& event)
std::string type; std::string type;
if (eventType (event, type)) if (eventType (event, type))
{ {
// TODO Figure out where to get the calling-context info from. // Figure out where to get the calling-context info from.
if (type == "program") rc = api.callProgramHook (it->file, it->function); 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 == "list") rc = api.callListHook (it->file, it->function/*, tasks*/);
else if (type == "task") rc = api.callTaskHook (it->file, it->function, 0); 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"); else if (type == "field") rc = api.callFieldHook (it->file, it->function, "field", "value");
} }
else else
@ -136,6 +181,8 @@ bool Hooks::eventType (const std::string& event, std::string& type)
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-unlock" || event == "post-file-unlock" ||
event == "pre-add-command" || event == "post-add-command") event == "pre-add-command" || event == "post-add-command")
{ {
type = "program"; type = "program";
@ -146,7 +193,8 @@ bool Hooks::eventType (const std::string& event, std::string& type)
type = "list"; type = "list";
return true; return true;
} }
else if (event == "?") else if (event == "pre-tag" || event == "post-tag" ||
event == "pre-detag" || event == "post-detag")
{ {
type = "task"; type = "task";
return true; return true;

View file

@ -32,42 +32,14 @@
#include "API.h" #include "API.h"
#include "auto.h" #include "auto.h"
// Hook class representing a single hook. // Hook class representing a single hook, which is just a three-way map.
class Hook class Hook
{ {
public: public:
Hook () Hook ();
: event ("") Hook (const std::string&, const std::string&, const std::string&);
, file ("") Hook (const Hook&);
, function ("") Hook& operator= (const Hook&);
{
}
Hook (const std::string& e, const std::string& f, const std::string& fn)
: event (e)
, file (f)
, function (fn)
{
}
Hook (const Hook& other)
{
event = other.event;
file = other.file;
function = other.function;
}
Hook& operator= (const Hook& other)
{
if (this != &other)
{
event = other.event;
file = other.file;
function = other.function;
}
return *this;
}
public: public:
std::string event; std::string event;
@ -85,7 +57,13 @@ public:
Hooks& operator= (const Hooks&); // Deliberately unimplemented Hooks& operator= (const Hooks&); // Deliberately unimplemented
void initialize (); void initialize ();
void setTaskId (int);
// void setField (const std::string&, const std::string&);
// void setTaskList (const std::vector <int>&);
bool trigger (const std::string&); bool trigger (const std::string&);
private:
bool eventType (const std::string&, std::string&); bool eventType (const std::string&, std::string&);
private: private:
@ -93,6 +71,9 @@ private:
API api; API api;
#endif #endif
std::vector <Hook> all; // All current hooks. std::vector <Hook> all; // All current hooks.
#ifdef HAVE_LIBLUA
int task_id;
#endif
}; };
#endif #endif

View file

@ -121,26 +121,30 @@ void TDB::location (const std::string& path)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void TDB::lock (bool lockFile /* = true */) void TDB::lock (bool lockFile /* = true */)
{ {
mLock = lockFile; if (context.hooks.trigger ("pre-file-lock"))
mPending.clear ();
mNew.clear ();
mPending.clear ();
foreach (location, mLocations)
{ {
location->pending = openAndLock (location->path + "/pending.data"); mLock = lockFile;
location->completed = openAndLock (location->path + "/completed.data");
location->undo = openAndLock (location->path + "/undo.data");
}
mAllOpenAndLocked = true; mPending.clear ();
mNew.clear ();
mPending.clear ();
foreach (location, mLocations)
{
location->pending = openAndLock (location->path + "/pending.data");
location->completed = openAndLock (location->path + "/completed.data");
location->undo = openAndLock (location->path + "/undo.data");
}
mAllOpenAndLocked = true;
context.hooks.trigger ("post-file-lock");
}
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void TDB::unlock () void TDB::unlock ()
{ {
if (mAllOpenAndLocked) if (mAllOpenAndLocked && context.hooks.trigger ("pre-file-unlock"))
{ {
mPending.clear (); mPending.clear ();
mNew.clear (); mNew.clear ();
@ -162,6 +166,7 @@ void TDB::unlock ()
} }
mAllOpenAndLocked = false; mAllOpenAndLocked = false;
context.hooks.trigger ("post-file-unlock");
} }
} }

View file

@ -27,6 +27,7 @@
#include <sstream> #include <sstream>
#include <algorithm> #include <algorithm>
#include "Context.h"
#include "Nibbler.h" #include "Nibbler.h"
#include "Date.h" #include "Date.h"
#include "Duration.h" #include "Duration.h"
@ -34,6 +35,8 @@
#include "text.h" #include "text.h"
#include "util.h" #include "util.h"
extern Context context;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Task::Task () Task::Task ()
: id (0) : id (0)

View file

@ -1701,20 +1701,29 @@ int deltaDescription (Task& task)
int deltaTags (Task& task) int deltaTags (Task& task)
{ {
int changes = 0; int changes = 0;
context.hooks.setTaskId (task.id);
// Apply or remove tags, if any. // Apply or remove tags, if any.
std::vector <std::string> tags; std::vector <std::string> tags;
context.task.getTags (tags); context.task.getTags (tags);
foreach (tag, tags) foreach (tag, tags)
{ {
task.addTag (*tag); if (context.hooks.trigger ("pre-tag"))
++changes; {
task.addTag (*tag);
++changes;
context.hooks.trigger ("post-tag");
}
} }
foreach (tag, context.tagRemovals) foreach (tag, context.tagRemovals)
{ {
task.removeTag (*tag); if (context.hooks.trigger ("pre-detag"))
++changes; {
task.removeTag (*tag);
++changes;
context.hooks.trigger ("post-detag");
}
} }
return changes; return changes;