Conversation: who added
[python-purple] / nullclient-ecore.py
index 207d42d..cd99eec 100644 (file)
@@ -9,7 +9,6 @@ conn_cbs = {}
 conv_cbs = {}
 notify_cbs = {}
 request_cbs = {}
-signal_cbs = {}
 
 def account_callback(name):
     print "---- account callback example: %s" % name
@@ -25,12 +24,12 @@ 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["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["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
@@ -58,10 +57,11 @@ cbs["connection"] = conn_cbs
 def conv_callback(name):
     print "---- conversation callback example: %s" % name
 
-conv_cbs["create_conversation"] = conv_callback
-conv_cbs["destroy_conversation"] = conv_callback
+#conv_cbs["create_conversation"] = conv_callback
+#conv_cbs["destroy_conversation"] = conv_callback
 conv_cbs["write_chat"] = conv_callback
 conv_cbs["write_conv"] = conv_callback
+#conv_cbs["write_im"] = conv_callback
 conv_cbs["chat_add_users"] = conv_callback
 conv_cbs["chat_rename_user"] = conv_callback
 conv_cbs["chat_remove_users"] = conv_callback
@@ -103,21 +103,12 @@ request_cbs["request_folder"] = request_callback
 
 cbs["request"] = request_cbs
 
-def buddy_signed_off_cb(name, bname):
-    print "---- sign off from buddy %s" % bname
-
-def receiving_im_msg_cb(sender, name, message):
-    print "---- receiving IM message from %s: %s" % (name, message)
-    return False
-
-#signal_cbs["buddy_signed_off"] = buddy_signed_off_cb
-signal_cbs["receiving_im_msg"] = receiving_im_msg_cb
-
-
 class MainWindow:
     def __init__(self, quit_cb):
         global conv_cbs
+        global signal_cbs
         self.bt_cbs = {}
+        self.send_cbs = {}
         self.quit_cb = quit_cb
         conv_cbs["write_im"] = self._write_im_cb
 
@@ -127,8 +118,8 @@ class MainWindow:
 
         hbox_cmd = etk.HBox(homogeneous=False)
         self.cmd_entry = etk.Entry()
-        lcmd = etk.Label(text="Type your message: ")
-        hbox_cmd.append(lcmd, etk.HBox.START, etk.HBox.START, 0)
+        self.lcmd = etk.Label(text="Type your message: ")
+        hbox_cmd.append(self.lcmd, etk.HBox.START, etk.HBox.START, 0)
         hbox_cmd.append(self.cmd_entry, etk.HBox.START, etk.HBox.EXPAND_FILL, 0)
 
         hbox_buttons = etk.HBox(homogeneous=False)
@@ -181,8 +172,13 @@ class MainWindow:
 
     def _send_bt_cb(self, pointer):
         bname = self.blist.selected_rows[0][0]
-        if bname:
-            print "ITEM: %s" % bname
+        msg = self.cmd_entry.text
+        if bname and msg != "":
+            if self.send_cbs.has_key("on_clicked"):
+                self.send_cbs["on_clicked"](bname, msg)
+        else:
+            print "Buddy not selected!"
+        self.cmd_entry.text = ""
 
     def _purple_conn_status_cb(self, txt, step, step_count):
             self.lstatus.text = txt
@@ -197,7 +193,7 @@ class MainWindow:
     def remove_buddy(self, bname):
         self.blistmodel.remove([bname])
 
-    def _purple_disconnected_status_cb(self, pointer):
+    def _purple_disconnected_status_cb(self):
         self.lstatus.text = "Disconnected"
 
     def set_panel_text(self, txt):
@@ -207,12 +203,20 @@ class MainWindow:
         if callable(cb):
             self.bt_cbs["on_clicked"] = cb
 
+    def add_send_cb(self, cb):
+        if callable(cb):
+            self.send_cbs["on_clicked"] = cb
+
     def add_quit_cb(self, cb):
         if callable(cb):
             self.quit_cb = cb
 
-    def _write_im_cb(self, name, message):
-        self.txt_area.text +=  str(name) + ": " + str(message) + "<br> "
+    def _write_im_cb(self, name, who, message):
+        if who:
+            w = who.split("/")[0]
+            self.txt_area.text +=  w + ": " + str(message) + "<br> "
+        else:
+            self.txt_area.text += str(name) + ": " + str(message) + "<br> "
         self._window.show_all()
 
 
@@ -220,44 +224,43 @@ class NullClientPurple:
     def __init__(self):
         self.p = purple.Purple(debug_enabled=False)
         self.window = MainWindow(self.quit)
-        self.buddies = []
+        self.buddies = [] #online buddies
+        self.conversations = {}
         self.account = None
-        self.protocol = None
+        self.protocol_id = "prpl-jabber"
         self.username = "carmanplugintest@gmail.com"
         self.password = "abc123def"
 
-
         global cbs
-        global signal_cbs
-        cbs["blist"]["update"] = self._purple_blist_new_cb
-        signal_cbs["buddy_signed_off"] = self._purple_signal_sign_off_cb
+        cbs["blist"]["update"] = self._purple_update_blist_cb
         self.p.purple_init(cbs)
 
         #Initializing UI
         self.window.add_bt_conn_cb(self.connect)
+        self.window.add_send_cb(self.send_msg)
         self.window.init_window()
 
-    def _purple_blist_new_cb(self, pointer):
-        """ FIXME: Hack! to fill blist on UI """
-        buddies = self.account.get_buddies_online()
-        for i in buddies:
-            if i not in self.buddies:
-                self.buddies.append(i)
-                self.window.new_buddy(i)
+    def _purple_update_blist_cb(self, type, name=None, totalsize=None,\
+                             currentsize=None, online=None):
+        self.buddies = self.account.get_buddies_online()
+        if type == 2:
+            if name in self.buddies:
+                self.buddies.append(name)
+                self.window.new_buddy(name)
 
     def _purple_signal_sign_off_cb(self, name, bname):
         self.buddies.remove(bname)
         self.window.remove_buddy(bname)
 
-    def set_protocol(self, protocol):
-        for p in self.p.get_protocols():
-            if p.get_name() == protocol:
-                self.protocol = p
-                return
+    def _purple_create_conv_cb(self, name, type):
+        bname = name.split("/")[0]
+        if bname in self.buddies and not self.conversations.has_key(name):
+            conv = purple.Conversation()
+            conv.initialize(self.account, "IM", bname)
+            self.conversations[bname] = conv
 
     def connect(self):
-        self.set_protocol("XMPP")
-        self.account = purple.Account(self.username, self.protocol.get_id())
+        self.account = purple.Account(self.username, self.protocol_id)
         self.account.set_password(self.password)
 
         self.account.proxy.set_type(purple.ProxyInfoType().HTTP)
@@ -268,11 +271,22 @@ class NullClientPurple:
 
         self.account.set_enabled("carman-purple-python", True)
         self.p.connect()
-        self.p.attach_signals(signal_cbs)
+        self.p.signal_connect("buddy-signed-off", self._purple_signal_sign_off_cb)
+
+    def send_msg(self, name, msg):
+        if not self.conversations.has_key(name):
+            conv = purple.Conversation()
+            conv.initialize(self.account, "IM", name)
+            self.conversations[name] = conv
+        self.conversations[name].write(msg)
 
     def quit(self, o):
-        print "quitting"
-        self.p = None
+        print "[DEBUG]: quitting"
+        for i in self.conversations:
+            self.conversations[i].destroy()
+            self.conversations[i] = None
+        self.conversations = None
+        self.p.destroy()
         ecore.main_loop_quit()
 
 if __name__ == '__main__':