From 6cb32996a53d1e09b20300e5cdcf1a9b6782ce2e Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 3 Aug 2013 00:22:00 +0100 Subject: [PATCH] Added on_connect event to plugins api --- plugins/helloworld.py | 3 +++ src/api/api.c | 31 ++++++++++++++++++++++++++++--- src/api/api.h | 2 ++ src/profanity.c | 2 ++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/plugins/helloworld.py b/plugins/helloworld.py index 33ecea68..1ef28b52 100644 --- a/plugins/helloworld.py +++ b/plugins/helloworld.py @@ -14,6 +14,9 @@ def prof_init(version, status): def prof_on_start(): helloworld() +def prof_on_connect(): + helloworld(); + # local functions def helloworld(): diff --git a/src/api/api.c b/src/api/api.c index 2850a463..3df8becc 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -22,13 +22,15 @@ #include +#include "api/api.h" #include "ui/ui.h" static GSList* _get_module_names(void); static void _init(void); static void _on_start(void); -static GSList* plugins; +static GSList* plugins; +static PyObject *prof_module; // API static PyObject* @@ -56,7 +58,7 @@ api_init(void) GSList *module_names = _get_module_names(); Py_Initialize(); - Py_InitModule("prof", apiMethods); + prof_module = Py_InitModule("prof", apiMethods); // TODO change to use XDG spec PySys_SetPath("$PYTHONPATH:./plugins/"); @@ -81,10 +83,15 @@ api_init(void) _init(); _on_start(); } - Py_Finalize(); return; } +void +api_shutdown(void) +{ + Py_Finalize(); +} + static GSList * _get_module_names(void) { @@ -146,3 +153,21 @@ _on_start(void) plugin = g_slist_next(plugin); } } + +void +plugins_on_connect(void) +{ + GSList *plugin = plugins; + PyObject *p_prof_on_connect; + + while (plugin != NULL) { + PyObject *module = plugin->data; + p_prof_on_connect = PyObject_GetAttrString(module, "prof_on_connect"); + if (p_prof_on_connect && PyCallable_Check(p_prof_on_connect)) { + PyObject_CallObject(p_prof_on_connect, NULL); + Py_XDECREF(p_prof_on_connect); + } + + plugin = g_slist_next(plugin); + } +} diff --git a/src/api/api.h b/src/api/api.h index 38f77dfe..99e56227 100644 --- a/src/api/api.h +++ b/src/api/api.h @@ -24,5 +24,7 @@ #define API_H void api_init(void); +void plugins_on_connect(void); +void api_shutdown(void); #endif diff --git a/src/profanity.c b/src/profanity.c index 5a6d4836..d8097107 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -260,6 +260,7 @@ prof_handle_login_account_success(char *account_name) status_bar_refresh(); accounts_free_account(account); + plugins_on_connect(); } void @@ -651,6 +652,7 @@ _shutdown(void) accounts_close(); cmd_close(); log_close(); + api_shutdown(); } static void