Added setup_dist.py.
[python-purple] / purple.pyx
index bd2bed5..e69a966 100644 (file)
@@ -36,6 +36,7 @@ cdef core.PurpleCoreUiOps c_core_ui_ops
 cdef eventloop.PurpleEventLoopUiOps c_eventloop_ui_ops
 #cdef ft.PurpleXferUiOps c_ft_ui_ops
 cdef notify.PurpleNotifyUiOps c_notify_ui_ops
+#cdef privacy.PurplePrivacyUiOps c_privacy_ui_ops
 cdef request.PurpleRequestUiOps c_request_ui_ops
 #cdef roomlist.PurpleRoomlistUiOps c_rlist_ui_ops
 
@@ -49,8 +50,12 @@ include "connection_cbs.pxd"
 include "conversation_cbs.pxd"
 #include "xfer_cbs.pxd"
 include "notify_cbs.pxd"
+#include "privacy_cbs.pxd"
 include "request_cbs.pxd"
 #include "roomlist_cbs.pxd"
+include "signal_cbs.pxd"
+
+include "util.pxd"
 
 cdef class Purple:
     """ Purple class.
@@ -60,7 +65,10 @@ cdef class Purple:
     @parm default_path: Full path for libpurple user files.
     """
 
+    cdef object __accounts
+
     def __init__(self, debug_enabled=True, app_name=__APP_NAME__, default_path=__DEFAULT_PATH__):
+        self.__accounts = {}
         if app_name is not __APP_NAME__:
             __APP_NAME__ = app_name
 
@@ -72,34 +80,36 @@ cdef class Purple:
         plugin.c_purple_plugins_add_search_path(default_path)
 
         # adds glib iteration inside ecore main loop
-        ecore.idler_add(self.__glib_iteration_when_idle)
+        ecore.timer_add(0.001, self.__glib_iteration_when_idle)
 
-    def __del__(self):
+    def destroy(self):
         core.c_purple_core_quit()
 
     cdef void __core_ui_ops_ui_prefs_init(self):
-        debug.c_purple_debug(debug.PURPLE_DEBUG_INFO, "core_ui_ops", "ui_prefs_init\n")
+        debug.c_purple_debug_info("core_ui_ops", "%s", "ui_prefs_init\n")
         prefs.c_purple_prefs_load()
 
         prefs.c_purple_prefs_add_none("/carman")
 
     cdef void __core_ui_ops_debug_init(self):
-        debug.c_purple_debug(debug.PURPLE_DEBUG_INFO, "core_ui_ops", "debug_ui_init\n")
+        debug.c_purple_debug_info("core_ui_ops", "%s", "debug_ui_init\n")
+        pass
 
     cdef void __core_ui_ops_ui_init(self):
-        debug.c_purple_debug(debug.PURPLE_DEBUG_INFO, "core_ui_ops", "ui_init\n")
+        debug.c_purple_debug_info("core_ui_ops", "%s", "ui_init\n")
 
         account.c_purple_accounts_set_ui_ops(&c_account_ui_ops)
         connection.c_purple_connections_set_ui_ops(&c_conn_ui_ops)
         blist.c_purple_blist_set_ui_ops(&c_blist_ui_ops)
         conversation.c_purple_conversations_set_ui_ops(&c_conv_ui_ops)
         notify.c_purple_notify_set_ui_ops(&c_notify_ui_ops)
+        #privacy.c_purple_privacy_set_ui_ops(&c_privacy_ui_ops)
         request.c_purple_request_set_ui_ops(&c_request_ui_ops)
         #ft.c_purple_xfers_set_ui_ops(&c_ft_ui_ops)
         #roomlist.c_purple_roomlist_set_ui_ops(&c_rlist_ui_ops)
 
     cdef void __core_ui_ops_quit(self):
-        debug.c_purple_debug(debug.PURPLE_DEBUG_INFO, "core_ui_ops", "quit\n")
+        debug.c_purple_debug_info("core_ui_ops", "%s", "quit\n")
 
         global c_ui_info
 
@@ -108,6 +118,7 @@ cdef class Purple:
         blist.c_purple_blist_set_ui_ops(NULL)
         conversation.c_purple_conversations_set_ui_ops(NULL)
         notify.c_purple_notify_set_ui_ops(NULL)
+        #privacy.c_purple_privacy_set_ui_ops(NULL)
         request.c_purple_request_set_ui_ops(NULL)
         #ft.c_purple_xfers_set_ui_ops(NULL)
         #roomlist.c_purple_roomlist_set_ui_ops(NULL)
@@ -129,24 +140,9 @@ cdef class Purple:
         glib.g_main_context_iteration(NULL, False)
         return True
 
-    def purple_init(self, callbacks_dict=None):
+    def purple_init(self):
         """ Initializes libpurple """
 
-        if callbacks_dict is not None:
-            global account_cbs
-            global blist_cbs
-            global connection_cbs
-            global conversation_cbs
-            global notify_cbs
-            global request_cbs
-
-            account_cbs = callbacks_dict["account"]
-            blist_cbs = callbacks_dict["blist"]
-            connection_cbs = callbacks_dict["connection"]
-            conversation_cbs = callbacks_dict["conversation"]
-            notify_cbs = callbacks_dict["notify"]
-            request_cbs = callbacks_dict["request"]
-
         c_account_ui_ops.notify_added = notify_added
         c_account_ui_ops.status_changed = status_changed
         c_account_ui_ops.request_add = request_add
@@ -218,7 +214,7 @@ cdef class Purple:
         c_eventloop_ui_ops.input_add = glib_input_add
         c_eventloop_ui_ops.input_remove = glib.g_source_remove
         c_eventloop_ui_ops.input_get_error = NULL
-        c_eventloop_ui_ops.timeout_add_seconds = glib.g_timeout_add_seconds
+        c_eventloop_ui_ops.timeout_add_seconds = NULL
 
         core.c_purple_core_set_ui_ops(&c_core_ui_ops)
         eventloop.c_purple_eventloop_set_ui_ops(&c_eventloop_ui_ops)
@@ -226,12 +222,14 @@ cdef class Purple:
         # initialize purple core
         ret = core.c_purple_core_init(__APP_NAME__)
         if ret is False:
-            debug.c_purple_debug(debug.PURPLE_DEBUG_INFO, "main", "Exiting because libpurple initialization failed.\n")
+            debug.c_purple_debug_fatal("main", "%s", "libpurple " \
+                                       "initialization failed.\n")
             return False
 
         # check if there is another instance of libpurple running
         if core.c_purple_core_ensure_single_instance() == False:
-            debug.c_purple_debug(debug.PURPLE_DEBUG_INFO, "main", "Exiting because another instance of libpurple is already running.\n")
+            debug.c_purple_debug_fatal("main", "%s", "Another instance of " \
+                                      "libpurple is already running.\n")
             core.c_purple_core_quit()
             return False
 
@@ -242,26 +240,120 @@ cdef class Purple:
         # load pounces
         pounce.c_purple_pounces_load()
 
+        # initialize accounts
+        self.load_accounts()
+
         return ret
 
-    def get_protocols(self):
+    def add_callback(self, type, name, callback):
+        """
+        Adds a callback with given name inside callback's type.
+        Example: add_callback("account", "notify-added", notify_added_cb)
+        """
+        global account_cbs
+        global blist_cbs
+        global connection_cbs
+        global conversation_cbs
+        global notify_cbs
+        global request_cbs
+
+        { "account": account_cbs,
+          "blist": blist_cbs,
+          "connection": connection_cbs,
+          "conversation": conversation_cbs,
+          "notify": notify_cbs,
+          "request": request_cbs }[type][name] = callback
+
+    def signal_connect(self, name=None, cb=None):
+        cdef int handle
+        cdef plugin.PurplePlugin *jabber
+
+        if name is None:
+            return
+
+        jabber = prpl.c_purple_find_prpl("prpl-jabber")
+        if jabber == NULL:
+            return
+
+        global signal_cbs
+        signal_cbs[name] = cb
+
+        if name == "signed-on":
+            signals.c_purple_signal_connect(
+                    connection.c_purple_connections_get_handle(),
+                    "signed-on", &handle,
+                    <signals.PurpleCallback> signal_signed_on_cb, NULL)
+        elif name == "signed-off":
+            signals.c_purple_signal_connect(
+                    connection.c_purple_connections_get_handle(),
+                    "signed-off", &handle,
+                    <signals.PurpleCallback> signal_signed_off_cb, NULL)
+        elif name == "connection-error":
+            signals.c_purple_signal_connect(
+                    connection.c_purple_connections_get_handle(),
+                    "connection-error", &handle,
+                    <signals.PurpleCallback> signal_connection_error_cb, NULL)
+        elif name == "buddy-signed-on":
+            signals.c_purple_signal_connect(
+                    blist.c_purple_blist_get_handle(),
+                    "buddy-signed-on", &handle,
+                    <signals.PurpleCallback> signal_buddy_signed_on_cb, NULL)
+        elif name == "buddy-signed-off":
+            signals.c_purple_signal_connect(
+                    blist.c_purple_blist_get_handle(),
+                    "buddy-signed-off", &handle,
+                    <signals.PurpleCallback> signal_buddy_signed_off_cb, NULL)
+        elif name == "receiving-im-msg":
+            signals.c_purple_signal_connect(
+                    conversation.c_purple_conversations_get_handle(),
+                    "receiving-im-msg", &handle,
+                    <signals.PurpleCallback> signal_receiving_im_msg_cb, NULL)
+        elif name == "jabber-receiving-xmlnode":
+            signals.c_purple_signal_connect(
+                    jabber, "jabber-receiving-xmlnode", &handle,
+                    <signals.PurpleCallback> jabber_receiving_xmlnode_cb, NULL)
+
+    def __get_accounts(self):
+        return self.__accounts
+    accounts = property(__get_accounts)
+
+    def new_account(self, username, protocol_id):
+        acc = Account(username, protocol_id)
+        return acc
+
+    def load_accounts(self):
         cdef glib.GList *iter
-        cdef plugin.PurplePlugin *__plugin
-        protocols = []
-        iter = plugin.c_purple_plugins_get_protocols()
+        cdef account.PurpleAccount *acc
+        iter = account.c_purple_accounts_get_all()
         while iter:
-            __plugin = <plugin.PurplePlugin*> iter.data
-            if __plugin.info and __plugin.info.name:
-                protocols += [(__plugin.info.id, __plugin.info.name)]
+            acc = <account.PurpleAccount *> iter.data
+            if <account.PurpleAccount *>acc:
+                username = account.c_purple_account_get_username(acc)
+                protocol_id = account.c_purple_account_get_protocol_id(acc)
+                self.account_add(username.split("/")[0], protocol_id, "172.18.216.211", 8080)
             iter = iter.next
-        return protocols
-
-    def connect(self):
-        conn = Connection()
-        conn.connect()
 
+    def account_add(self, username, protocol_id, host, port):
+        if not self.account_verify(username):
+            acc = purple.Account(username, protocol_id)
+            self.__accounts[username] = acc
+            if not account.c_purple_accounts_find(username, protocol_id):
+                acc.proxy.set_type(purple.ProxyInfoType().HTTP)
+                acc.proxy.set_host(host)
+                acc.proxy.set_port(port)
+                acc.save_into_xml()
+
+    def account_verify(self, acc_username):
+        if self.__accounts:
+            for username in self.__accounts.keys():
+                if acc_username == username:
+                    return self.__accounts[username]
+        else:
+            return None
+
+include "plugin.pyx"
 include "proxy.pyx"
 include "account.pyx"
 include "buddy.pyx"
-include "connection.pyx"
+#include "connection.pyx"
 include "conversation.pyx"