From bbd0f131da05d704e16ad19669fe7ef27a9ad6e0 Mon Sep 17 00:00:00 2001 From: Ragner Magalhaes Date: Tue, 2 Dec 2008 20:43:58 +0000 Subject: [PATCH] Conversation support - Added conversation support. Applications using python-purple must keep a conversations' list. See nullclient-ecore.py as reference. - Added destroy() to purple.pyx - See quit() in nullclient-ecore.py as example of how destroy python-purple. Signed-off-by: Anderson Briglia git-svn-id: https://garage.maemo.org/svn/carman/branches/carman-0.7-beta2/python-purple@1344 596f6dd7-e928-0410-a184-9e12fd12cf7e --- account.pyx | 1 - conversation.pyx | 42 +++++++++++++++++++++++--------- conversation_cbs.pxd | 2 +- libpurple/conversation.pxd | 2 ++ nullclient-ecore.py | 57 ++++++++++++++++++++++++++++++++++---------- purple.pyx | 7 ++++-- 6 files changed, 84 insertions(+), 27 deletions(-) diff --git a/account.pyx b/account.pyx index 6de9af8..033b6bd 100644 --- a/account.pyx +++ b/account.pyx @@ -45,7 +45,6 @@ cdef class Account: self.__proxy = ProxyInfo() self.__proxy.c_proxyinfo = c_proxyinfo - def set_password(self, password): account.c_purple_account_set_password(self.c_account, password) diff --git a/conversation.pyx b/conversation.pyx index c13578a..f743e41 100644 --- a/conversation.pyx +++ b/conversation.pyx @@ -22,12 +22,37 @@ cimport purple cdef class Conversation: """ Conversation class """ cdef conversation.PurpleConversation *__conv + cdef Account __acc + + cdef object name def __init__(self): conversation.c_purple_conversations_init() + self.name = None - def conversation_new(self, type, acc, char *name): - self.__conv = conversation.c_purple_conversation_new(type, acc.__account, name) + def initialize(self, acc, type, char *name): + self.__acc = acc + if type == "UNKNOWN": + self.__conv =\ + conversation.c_purple_conversation_new(conversation.PURPLE_CONV_TYPE_UNKNOWN,\ + self.__acc.c_account, name) + elif type == "IM": + self.__conv =\ + conversation.c_purple_conversation_new(conversation.PURPLE_CONV_TYPE_IM,\ + self.__acc.c_account, name) + elif type == "CHAT": + self.__conv =\ + conversation.c_purple_conversation_new(conversation.PURPLE_CONV_TYPE_CHAT,\ + self.__acc.c_account, name) + elif type == "MISC": + self.__conv =\ + conversation.c_purple_conversation_new(conversation.PURPLE_CONV_TYPE_MISC,\ + self.__acc.c_account, name) + elif type == "ANY": + self.__conv =\ + conversation.c_purple_conversation_new(conversation.PURPLE_CONV_TYPE_ANY,\ + self.__acc.c_account, name) + self.name = name def conversation_set_ui_ops(self): cdef conversation.PurpleConversationUiOps c_conv_ui_ops @@ -49,16 +74,11 @@ cdef class Conversation: conversation.c_purple_conversation_set_ui_ops(self.__conv, &c_conv_ui_ops) - def conversation_write(self, char *message): + def write(self, char *message): conversation.c_purple_conv_im_send(conversation.c_purple_conversation_get_im_data(self.__conv), message) - def conversation_destroy(self): - conversation.c_purple_conversation_destroy(self.__conv) - - def conversation_get_handle(self): + def get_handle(self): conversation.c_purple_conversations_get_handle() - def send_message(self, buddy, char *message): - self.conversation_new(1, buddy.account, buddy.name) - self.conversation_set_ui_ops() - self.conversation_write(message) + def destroy(self): + conversation.c_purple_conversation_destroy(self.__conv) diff --git a/conversation_cbs.pxd b/conversation_cbs.pxd index 8e68883..53729d2 100644 --- a/conversation_cbs.pxd +++ b/conversation_cbs.pxd @@ -30,7 +30,7 @@ cdef void create_conversation (conversation.PurpleConversation *conv): debug.c_purple_debug(debug.PURPLE_DEBUG_INFO, "conversation", "create_conversation\n") try: - (conversation_cbs["create_conversation"])("create_conversation") + (conversation_cbs["create_conversation"])(conv.name, conv.type) except KeyError: pass diff --git a/libpurple/conversation.pxd b/libpurple/conversation.pxd index 88eb23a..663d530 100644 --- a/libpurple/conversation.pxd +++ b/libpurple/conversation.pxd @@ -162,5 +162,7 @@ cdef extern from "libpurple/conversation.h": void c_purple_conversation_set_ui_ops "purple_conversation_set_ui_ops" (PurpleConversation *conv, PurpleConversationUiOps *ops) void c_purple_conversations_set_ui_ops "purple_conversations_set_ui_ops" (PurpleConversationUiOps *ops) PurpleConvIm *c_purple_conversation_get_im_data "purple_conversation_get_im_data" (PurpleConversation *conv) + char *c_purple_conversation_get_name "purple_conversation_get_name" (PurpleConversation *conv) + account.PurpleAccount *c_purple_conversation_get_account "purple_conversation_get_account" (PurpleConversation *conv) void c_purple_conv_im_send "purple_conv_im_send" (PurpleConvIm *im, char *message) void c_purple_conversation_destroy "purple_conversation_destroy" (PurpleConversation *conv) diff --git a/nullclient-ecore.py b/nullclient-ecore.py index f2e9dd9..94d8f92 100644 --- a/nullclient-ecore.py +++ b/nullclient-ecore.py @@ -27,10 +27,10 @@ def blist_callback(name): #blist_cbs["new_list"] = blist_callback #blist_cbs["new_node"] = blist_callback -blist_cbs["show"] = 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 +58,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 @@ -117,7 +118,9 @@ 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 +130,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 +184,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 +205,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,6 +215,10 @@ 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 @@ -221,6 +233,7 @@ class NullClientPurple: self.p = purple.Purple(debug_enabled=False) self.window = MainWindow(self.quit) self.buddies = [] #online buddies + self.conversations = {} self.account = None self.protocol_id = "prpl-jabber" self.username = "carmanplugintest@gmail.com" @@ -231,10 +244,12 @@ class NullClientPurple: global signal_cbs cbs["blist"]["update"] = self._purple_update_blist_cb signal_cbs["buddy_signed_off"] = self._purple_signal_sign_off_cb + cbs["conversation"]["create_conversation"] = self._purple_create_conv_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_update_blist_cb(self, type, name=None, totalsize=None,\ @@ -249,6 +264,13 @@ class NullClientPurple: self.buddies.remove(bname) self.window.remove_buddy(bname) + 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.account = purple.Account(self.username, self.protocol_id) self.account.set_password(self.password) @@ -263,9 +285,20 @@ class NullClientPurple: self.p.connect() self.p.attach_signals(signal_cbs) + 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__': diff --git a/purple.pyx b/purple.pyx index 00b739e..a011ff0 100644 --- a/purple.pyx +++ b/purple.pyx @@ -62,7 +62,6 @@ cdef class Purple: @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 @@ -77,7 +76,7 @@ cdef class Purple: # adds glib iteration inside ecore main loop 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): @@ -269,6 +268,10 @@ cdef class Purple: "receiving-im-msg", &handle, signal_receiving_im_msg_cb, NULL) + def new_account(self, username, protocol_id): + acc = Account(username, protocol_id) + return acc + include "proxy.pyx" include "account.pyx" include "buddy.pyx" -- 1.7.9.5