Better handling of c/python strings.
[python-purple] / nullclient-ecore.py
index fdaadd0..50e21a0 100644 (file)
@@ -3,8 +3,6 @@ import ecore
 import purple
 
 cbs = {}
-acc_cbs = {}
-blist_cbs = {}
 conn_cbs = {}
 conv_cbs = {}
 notify_cbs = {}
@@ -13,30 +11,9 @@ request_cbs = {}
 def account_callback(name):
     print "---- account callback example: %s" % name
 
-acc_cbs["notify_added"] = account_callback
-acc_cbs["status_changed"] = account_callback
-acc_cbs["request_add"] = account_callback
-acc_cbs["request_authorize"] = account_callback
-acc_cbs["close_account_request"] = account_callback
-
-cbs["account"] = acc_cbs
-
 def blist_callback(name):
     print "---- blist callback example: %s" % name
 
-#blist_cbs["new_list"] = blist_callback
-#blist_cbs["new_node"] = blist_callback
-#blist_cbs["show"] = blist_callback
-#blist_cbs["update"] = blist_callback
-#blist_cbs["remove"] = blist_callback
-#blist_cbs["destroy"] = blist_callback
-blist_cbs["set_visible"] = blist_callback
-blist_cbs["request_add_buddy"] = blist_callback
-blist_cbs["request_add_chat"] = blist_callback
-blist_cbs["request_add_group"] = blist_callback
-
-cbs["blist"] = blist_cbs
-
 def conn_callback(name):
     print "---- connection callback example: %s" % name
 
@@ -211,9 +188,9 @@ class MainWindow:
             if acc:
                 return acc
             else:
-                return "None"
+                return None
         except:
-            return "None"
+            return None
 
     def _new_account(self, pointer):
         if self.new_acc_bt_cbs.has_key("on_clicked"):
@@ -258,12 +235,11 @@ class MainWindow:
         if callable(cb):
             self.quit_cb = cb
 
-    def _write_im_cb(self, name, who, message):
-        if who:
-            w = who.split("/")[0]
-            self.txt_area.text +=  w + ": " + str(message) + "<br> "
+    def _write_im_cb(self, sender, alias, message):
+        if alias:
+            self.txt_area.text += alias + ": " + message + "<br> "
         else:
-            self.txt_area.text += str(name) + ": " + str(message) + "<br> "
+            self.txt_area.text += sender + ": " + message + "<br> "
         self._window.show_all()
 
 
@@ -274,12 +250,23 @@ class NullClientPurple:
         self.buddies = {} #all buddies
         self.conversations = {}
         self.protocol_id = "prpl-jabber"
+        self.account = None
         self.accs = None
 
-        global cbs
-        cbs["blist"]["update"] = self._purple_update_blist_cb
+
+        self.p.add_account_cb("notify_added", account_callback)
+        self.p.add_account_cb("status_changed", account_callback)
+        self.p.add_account_cb("request_add", account_callback)
+        self.p.add_account_cb("request_authorize", account_callback)
+        self.p.add_account_cb("close_account_request", account_callback)
+
+        self.p.add_blist_cb("set_visible", blist_callback)
+        self.p.add_blist_cb("request_add_buddy", blist_callback)
+        self.p.add_blist_cb("request_add_chat", blist_callback)
+        self.p.add_blist_cb("request_add_group", blist_callback)
+        self.p.add_blist_cb("update", self._purple_update_blist_cb)
+
         self.p.purple_init(cbs)
-        self.p.accounts_init()
 
         #Initializing UI
         self.window.add_bt_conn_cb(self.connect)
@@ -287,22 +274,23 @@ class NullClientPurple:
         self.window.add_account_cb(self.add_account)
         self.window.init_window()
 
-    def _purple_update_blist_cb(self, type, name=None, alias=None, totalsize=None,\
-                             currentsize=None, online=None):
-        if type == 2:
+    def _purple_update_blist_cb(self, type, name=None, alias=None, \
+                                totalsize=None, currentsize=None, \
+                                online=None):
+        if self.account and name != None and type == 2:
             if not self.buddies.has_key(name):
                 b = purple.Buddy()
                 b.new_buddy(self.account, name, alias)
                 self.buddies[name] = b
-            elif self.buddies[name].online is True:
+            elif self.buddies[name].online:
                 self.window.new_buddy(name)
 
-    def _purple_signal_sign_off_cb(self, name, bname):
-        if self.buddies.has_key(bname):
-            self.buddies[bname] = None
-            self.buddies.pop(bname)
+    def _purple_signal_buddy_signed_off_cb(self, name, alias):
+        if self.buddies.has_key(name):
+            self.buddies[name] = None
+            self.buddies.pop(name)
             print "[DEBUG]: Buddy removed!"
-        self.window.remove_buddy(bname)
+        self.window.remove_buddy(name)
 
     def _purple_create_conv_cb(self, name, type):
         bname = name.split("/")[0]
@@ -313,19 +301,20 @@ class NullClientPurple:
 
     def connect(self, password):
         username_acc = self.window.selected_accs()
-        self.account = self.p.account_verify(username_acc)
-        self.account.get_protocol_options()
-        self.account.set_enabled("carman-purple-python", True)
-        self.account.password = password
-        self.p.connect()
-        self.p.signal_connect("buddy-signed-off", self._purple_signal_sign_off_cb)
+        if username_acc:
+            self.account = self.p.account_verify(username_acc)
+            self.account.get_protocol_options()
+            self.account.set_enabled("carman-purple-python", True)
+            self.account.password = password
+            self.p.connect()
+            self.p.signal_connect("buddy-signed-off", self._purple_signal_buddy_signed_off_cb)
 
     def add_account(self):
         username = "carmanplugintest@gmail.com"
         host = "172.18.216.211"
         port = 8080
         self.p.account_add(username, self.protocol_id, host, port)
-        self.accs = self.p.accounts_get_dict()
+        self.accs = self.p.accounts
         for acc in self.accs.keys():
             self.window.new_account(acc)
 
@@ -346,6 +335,5 @@ class NullClientPurple:
         ecore.main_loop_quit()
 
 if __name__ == '__main__':
-
     nullpurple = NullClientPurple()
     ecore.main_loop_begin()