Added SIGCHLD handling to avoid zombie processes.
[python-purple] / purple.pyx
index 3686593..17411e7 100644 (file)
@@ -23,10 +23,16 @@ cdef extern from "c_purple.h":
     glib.guint glib_input_add(glib.gint fd, eventloop.PurpleInputCondition condition, eventloop.PurpleInputFunction function, glib.gpointer data)
 
 import ecore
+import signal
 
-__DEFAULT_PATH__ = "/tmp"
-__APP_NAME__ = "carman-purple-python"
-__APP_VERSION__ = "0.1"
+cdef glib.GHashTable *c_ui_info
+
+c_ui_info = NULL
+
+cdef char *c_ui_name
+cdef char *c_ui_version
+cdef char *c_ui_website
+cdef char *c_ui_dev_website
 
 cdef account.PurpleAccountUiOps c_account_ui_ops
 cdef blist.PurpleBlistUiOps c_blist_ui_ops
@@ -40,10 +46,6 @@ cdef notify.PurpleNotifyUiOps c_notify_ui_ops
 cdef request.PurpleRequestUiOps c_request_ui_ops
 #cdef roomlist.PurpleRoomlistUiOps c_rlist_ui_ops
 
-cdef glib.GHashTable *c_ui_info
-
-c_ui_info = NULL
-
 include "account_cbs.pxd"
 include "blist_cbs.pxd"
 include "connection_cbs.pxd"
@@ -58,34 +60,59 @@ include "signal_cbs.pxd"
 include "util.pxd"
 
 cdef class Purple:
-    """ Purple class.
+    '''Purple class.
+
+    @param ui_name ID of the UI using the purple.
+        This should be a unique ID, registered with the purple team.
+    @param ui_version UI version.
+    @param ui_website UI website.
+    @param ui_dev_website UI development website.
+    @param debug_enabled True to enable debug messages.
+    @param default_path Custom settings directory
+    '''
 
-    @parm debug_enabled: Toggle debug messages.
-    @parm app_name: Set application name.
-    @parm default_path: Full path for libpurple user files.
-    """
 
-    def __init__(self, debug_enabled=True, app_name=__APP_NAME__, default_path=__DEFAULT_PATH__):
-        if app_name is not __APP_NAME__:
-            __APP_NAME__ = app_name
+    def __init__(self, ui_name, ui_version, ui_website, ui_dev_website, \
+            debug_enabled=None, default_path=None):
 
-        if default_path is not __DEFAULT_PATH__:
-            __DEFAULT_PATH__ = default_path
+        global c_ui_name
+        global c_ui_version
+        global c_ui_website
+        global c_ui_dev_website
 
-        debug.purple_debug_set_enabled(debug_enabled)
-        util.purple_util_set_user_dir(default_path)
-        plugin.purple_plugins_add_search_path(default_path)
+        c_ui_name = ui_name
+        c_ui_version = ui_version
+        c_ui_website = ui_website
+        c_ui_dev_website = ui_dev_website
+
+        if debug_enabled:
+            debug.purple_debug_set_enabled(debug_enabled)
+
+        if default_path:
+            util.purple_util_set_user_dir(default_path)
 
         # adds glib iteration inside ecore main loop
         ecore.timer_add(0.001, self.__glib_iteration_when_idle)
 
-    def __get_ui_name(self):
-        return __APP_NAME__
-    ui_name = property(__get_ui_name)
+        # libpurple's built-in DNS resolution forks processes to perform
+        # blocking lookups without blocking the main process.  It does not
+        # handle SIGCHLD itself, so if the UI does not you quickly get an army
+        # of zombie subprocesses marching around.
+        signal.signal(signal.SIGCHLD, signal.SIG_IGN)
 
     def destroy(self):
         core.purple_core_quit()
 
+    def __get_ui_name(self):
+        '''Returns the UI name.
+
+        @return UI name.
+        '''
+
+        global c_ui_name
+        return str(c_ui_name)
+    ui_name = property(__get_ui_name)
+
     cdef void __core_ui_ops_ui_prefs_init(self):
         debug.purple_debug_info("core_ui_ops", "%s", "ui_prefs_init\n")
         prefs.purple_prefs_load()
@@ -112,8 +139,6 @@ cdef class Purple:
     cdef void __core_ui_ops_quit(self):
         debug.purple_debug_info("core_ui_ops", "%s", "quit\n")
 
-        global c_ui_info
-
         account.purple_accounts_set_ui_ops(NULL)
         connection.purple_connections_set_ui_ops(NULL)
         blist.purple_blist_set_ui_ops(NULL)
@@ -124,17 +149,24 @@ cdef class Purple:
         #ft.purple_xfers_set_ui_ops(NULL)
         #roomlist.purple_roomlist_set_ui_ops(NULL)
 
-        if c_ui_info:
-            glib.g_hash_table_destroy(c_ui_info)
+        if self.c_ui_info:
+            glib.g_hash_table_destroy(<glib.GHashTable *> self.c_ui_info)
 
     cdef glib.GHashTable *__core_ui_ops_get_ui_info(self):
         global c_ui_info
+        global c_ui_name
+        global c_ui_version
+        global c_ui_website
+        global c_ui_dev_website
 
         if c_ui_info == NULL:
-            c_ui_info = glib.g_hash_table_new(glib.g_str_hash, glib.g_str_equal)
+            c_ui_info = glib.g_hash_table_new(glib.g_str_hash, \
+                    glib.g_str_equal)
 
-            glib.g_hash_table_insert(c_ui_info, "name", <glib.gpointer> __APP_NAME__)
-            glib.g_hash_table_insert(c_ui_info, "version", <glib.gpointer> __APP_VERSION__)
+            glib.g_hash_table_insert(c_ui_info, "name", c_ui_name)
+            glib.g_hash_table_insert(c_ui_info, "version", c_ui_version)
+            glib.g_hash_table_insert(c_ui_info, "website", c_ui_website)
+            glib.g_hash_table_insert(c_ui_info, "dev_website", c_ui_dev_website)
         return c_ui_info
 
     def __glib_iteration_when_idle(self):
@@ -142,7 +174,12 @@ cdef class Purple:
         return True
 
     def purple_init(self):
-        """ Initializes libpurple """
+        '''Initializes the purple.
+
+        This will setup preferences for all the core subsystems.
+        '''
+
+        global c_ui_name
 
         c_account_ui_ops.notify_added = notify_added
         c_account_ui_ops.status_changed = status_changed
@@ -221,7 +258,7 @@ cdef class Purple:
         eventloop.purple_eventloop_set_ui_ops(&c_eventloop_ui_ops)
 
         # initialize purple core
-        ret = core.purple_core_init(__APP_NAME__)
+        ret = core.purple_core_init(c_ui_name)
         if ret is False:
             debug.purple_debug_fatal("main", "%s", "libpurple " \
                                        "initialization failed.\n")
@@ -244,13 +281,13 @@ cdef class Purple:
         return ret
 
     def add_callback(self, type, name, callback):
-        """
-        Adds a callback with given name inside callback's type.
+        '''Adds a callback with given name inside callback's type.
 
         @param type     Callback type (e.g. "account")
         @param name     Callback name (e.g. "notify-added")
         @param callback Callback to be called
-        """
+        '''
+
         global account_cbs
         global blist_cbs
         global connection_cbs
@@ -266,6 +303,14 @@ cdef class Purple:
           "request": request_cbs }[type][name] = callback
 
     def signal_connect(self, name=None, cb=None):
+        '''Connects a signal handler to a signal for a particular object.
+        Take care not to register a handler function twice. Purple will
+        not correct any mistakes for you in this area.
+
+        @param name Name of the signal to connect.
+        @param cb Callback function.
+        '''
+
         cdef int handle
         cdef plugin.PurplePlugin *jabber
 
@@ -315,6 +360,11 @@ cdef class Purple:
                     <signals.PurpleCallback> jabber_receiving_xmlnode_cb, NULL)
 
     def accounts_get_all(self):
+        '''Returns a list of all accounts.
+
+        @return A list of all accounts.
+        '''
+
         cdef glib.GList *iter
         cdef account.PurpleAccount *acc
         cdef char *username
@@ -338,11 +388,19 @@ cdef class Purple:
         return account_list
 
     def accounts_get_all_active(self):
+        '''Returns a list of all enabled accounts.
+
+        @return A list of all enabled accounts.
+        '''
+
         cdef glib.GList *iter
         cdef account.PurpleAccount *acc
         cdef char *username
         cdef char *protocol_id
 
+        #FIXME: The list is owned by the caller, and must be g_list_free()d
+        #       to avoid leaking the nodes.
+
         iter = account.purple_accounts_get_all_active()
         account_list = []
 
@@ -361,6 +419,11 @@ cdef class Purple:
         return account_list
 
     def protocols_get_all(self):
+        '''Returns a list of all protocols.
+
+        @return A list of all protocols.
+        '''
+
         cdef glib.GList *iter
         cdef plugin.PurplePlugin *pp
 
@@ -373,6 +436,9 @@ cdef class Purple:
             iter = iter.next
         return protocol_list
 
+    def call_action(self, i):
+        __call_action(i)
+
 include "protocol.pyx"
 #include "plugin.pyx"
 include "proxy.pyx"