From: Ragner Magalhaes Date: Tue, 2 Dec 2008 20:06:42 +0000 (+0000) Subject: libpurple initialization X-Git-Url: http://git.maemo.org/git/?p=python-purple;a=commitdiff_plain;h=30f1e323b5bd76f5789b6251454e388cc511e2d5 libpurple initialization Using c_purple.[c,h] to encapsulate all glib functions and structs for libpurple intialization. Signed-off-by: Anderson Briglia git-svn-id: https://garage.maemo.org/svn/carman/branches/carman-0.7-beta2/python-purple@1270 596f6dd7-e928-0410-a184-9e12fd12cf7e --- diff --git a/c_purple.c b/c_purple.c new file mode 100644 index 0000000..e5d5fa0 --- /dev/null +++ b/c_purple.c @@ -0,0 +1,174 @@ +#include + +#include + +#include +#include +#include + +#include "c_purple.h" + +#define PURPLE_GLIB_READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR) +#define PURPLE_GLIB_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL) + +typedef struct _PurpleGLibIOClosure { + PurpleInputFunction function; + guint result; + gpointer data; +} PurpleGLibIOClosure; + +static void purple_glib_io_destroy(gpointer data) +{ + g_free(data); +} + +static gboolean purple_glib_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data) +{ + PurpleGLibIOClosure *closure = data; + PurpleInputCondition purple_cond = 0; + + if (condition & PURPLE_GLIB_READ_COND) + purple_cond |= PURPLE_INPUT_READ; + if (condition & PURPLE_GLIB_WRITE_COND) + purple_cond |= PURPLE_INPUT_WRITE; + + closure->function(closure->data, g_io_channel_unix_get_fd(source), + purple_cond); + + return TRUE; +} + +static guint glib_input_add(gint fd, PurpleInputCondition condition, PurpleInputFunction function, + gpointer data) +{ + PurpleGLibIOClosure *closure = g_new0(PurpleGLibIOClosure, 1); + GIOChannel *channel; + GIOCondition cond = 0; + + closure->function = function; + closure->data = data; + + if (condition & PURPLE_INPUT_READ) + cond |= PURPLE_GLIB_READ_COND; + if (condition & PURPLE_INPUT_WRITE) + cond |= PURPLE_GLIB_WRITE_COND; + + channel = g_io_channel_unix_new(fd); + closure->result = g_io_add_watch_full(channel, 0, cond, + purple_glib_io_invoke, closure, purple_glib_io_destroy); + + g_io_channel_unref(channel); + return closure->result; +} + +static PurpleEventLoopUiOps glib_eventloops = +{ + g_timeout_add, + g_source_remove, + glib_input_add, + g_source_remove, + NULL, +#if GLIB_CHECK_VERSION(2,14,0) + g_timeout_add_seconds, +#else + NULL, +#endif + + /* padding */ + NULL, + NULL, + NULL +}; +/*** End of the eventloop functions. ***/ + +/*** Conversation uiops ***/ +/* FIXME: Revisit this function. Is it needed? How it should be more general?*/ +static void +write_conv(PurpleConversation *conv, const char *who, const char *alias, + const char *message, PurpleMessageFlags flags, time_t mtime) +{ + const char *name; + if (alias && *alias) + name = alias; + else if (who && *who) + name = who; + else + name = NULL; + + printf("(%s) %s %s: %s\n", purple_conversation_get_name(conv), + purple_utf8_strftime("(%H:%M:%S)", localtime(&mtime)), + name, message); +} + +static PurpleConversationUiOps conv_uiops = +{ + NULL, /* create_conversation */ + NULL, /* destroy_conversation */ + NULL, /* write_chat */ + NULL, /* write_im */ + write_conv, /* write_conv */ + NULL, /* chat_add_users */ + NULL, /* chat_rename_user */ + NULL, /* chat_remove_users */ + NULL, /* chat_update_user */ + NULL, /* present */ + NULL, /* has_focus */ + NULL, /* custom_smiley_add */ + NULL, /* custom_smiley_write */ + NULL, /* custom_smiley_close */ + NULL, /* send_confirm */ + NULL, + NULL, + NULL, + NULL +}; + +/* FIXME: Is this a valid struct? */ +static void +ui_init(void) +{ + /** + * This should initialize the UI components for all the modules. Here we + * just initialize the UI for conversations. + */ + purple_conversations_set_ui_ops(&conv_uiops); +} + +static PurpleCoreUiOps core_uiops = +{ + NULL, + NULL, + ui_init, + NULL, + + /* padding */ + NULL, + NULL, + NULL, + NULL +}; + +void init_libpurple(void) +{ + /* Set a custom user directory (optional) */ + /* FIXME: Put a valid carman directory here*/ + purple_util_set_user_dir(CUSTOM_USER_DIRECTORY); + + /* We do not want any debugging for now to keep the noise to a minimum. */ + purple_debug_set_enabled(FALSE); + + purple_core_set_ui_ops(&core_uiops); + + purple_eventloop_set_ui_ops(&glib_eventloops); + + purple_plugins_add_search_path(CUSTOM_PLUGIN_PATH); + + if (!purple_core_init(UI_ID)) { + /* Initializing the core failed. Terminate. */ + fprintf(stderr, + "libpurple initialization failed. Dumping core.\n" + "Please report this!\n"); + abort(); + } + printf("libpurple initialized"); +} diff --git a/c_purple.h b/c_purple.h new file mode 100644 index 0000000..62dfe40 --- /dev/null +++ b/c_purple.h @@ -0,0 +1,9 @@ +#include + +#define CUSTOM_USER_DIRECTORY "/dev/null" +#define CUSTOM_PLUGIN_PATH "" +/* FIXME: Check this path */ +#define PLUGIN_SAVE_PREF "/purple/nullclient/plugins/saved" +#define UI_ID "carman-purple-client" + +void init_libpurple(void); diff --git a/c_purple.pxd b/c_purple.pxd new file mode 100644 index 0000000..e23697f --- /dev/null +++ b/c_purple.pxd @@ -0,0 +1,5 @@ +cdef extern from *: + ctypedef char* const_char_ptr "const char *" + +cdef extern from "c_purple.h": + void init_libpurple() diff --git a/purple.pyx b/purple.pyx index 4588029..ec21c97 100644 --- a/purple.pyx +++ b/purple.pyx @@ -23,15 +23,20 @@ cdef extern from *: cdef extern from "time.h": ctypedef long int time_t -include "glib.pxd" -include "core/account.pxd" -include "core/blist.pxd" -include "core/connection.pxd" -include "core/core.pxd" -include "core/debug.pxd" -include "core/eventloop.pxd" -include "core/idle.pxd" -include "core/plugin.pxd" -include "core/pounce.pxd" -include "core/prefs.pxd" -include "core/util.pxd" +class Core(object): + def init(self): + init_libpurple() + +include "c_purple.pxd" +#include "glib.pxd" +#include "core/account.pxd" +#include "core/blist.pxd" +#include "core/connection.pxd" +#include "core/core.pxd" +#include "core/debug.pxd" +#include "core/eventloop.pxd" +#include "core/idle.pxd" +#include "core/plugin.pxd" +#include "core/pounce.pxd" +#include "core/prefs.pxd" +#include "core/util.pxd" diff --git a/setup.py b/setup.py index 122bb5a..128df44 100755 --- a/setup.py +++ b/setup.py @@ -10,6 +10,7 @@ from subprocess import Popen, PIPE cflags = Popen(['pkg-config', '--cflags', 'purple'], stdout=PIPE).communicate()[0].split() ldflags = Popen(['pkg-config', '--libs', 'purple'], stdout=PIPE).communicate()[0].split() +print ldflags class pypurple_build_ext(build_ext): def finalize_options(self): @@ -23,7 +24,7 @@ setup( author ='Bruno Abinader', author_email='bruno.abinader@openbossa.org', ext_modules=[Extension('purple', - sources=['purple.pyx'], + sources=['c_purple.c','purple.pyx'], extra_compile_args=cflags, extra_link_args=ldflags)], cmdclass = {'build_ext': pypurple_build_ext},