X-Git-Url: http://git.maemo.org/git/?p=python-purple;a=blobdiff_plain;f=nullclient-ecore.py;h=1f7d8441269f7c0ad8e553147e5bd72b682ee91e;hp=351e37cc4c9c5e5d316db3a1ad12e67bd4178bfe;hb=39c1c26cdc38fe7dc79122c45bba7731b08ed1cd;hpb=54c3652ebe4f40182214f4f37403ac13fcfeb7ff;ds=sidebyside diff --git a/nullclient-ecore.py b/nullclient-ecore.py index 351e37c..1f7d844 100644 --- a/nullclient-ecore.py +++ b/nullclient-ecore.py @@ -1,351 +1,402 @@ +# +# Copyright (c) 2008 INdT - Instituto Nokia de Tecnologia +# +# This file is part of python-purple. +# +# python-purple is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# python-purple is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + import etk import ecore import purple -cbs = {} -acc_cbs = {} -blist_cbs = {} -conn_cbs = {} -conv_cbs = {} -notify_cbs = {} -request_cbs = {} - -def account_callback(name): - print "---- account callback example: %s" % name - -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 - -def conn_progress_cb(data): - return "Connection in progress..." - -#conn_cbs["connect_progress"] = conn_progress_cb -#conn_cbs["connected"] = conn_callback -#conn_cbs["disconnected"] = conn_callback -conn_cbs["notice"] = conn_callback -conn_cbs["report_disconnect"] = conn_callback -conn_cbs["network_connected"] = conn_callback -conn_cbs["network_disconnected"] = conn_callback -conn_cbs["report_disconnect_reason"] = conn_callback - -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["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 -conv_cbs["chat_update_user"] = conv_callback -conv_cbs["present"] = conv_callback -conv_cbs["has_focus"] = conv_callback -conv_cbs["custom_smiley_add"] = conv_callback -conv_cbs["custom_smiley_write"] = conv_callback -conv_cbs["custom_smiley_close"] = conv_callback -conv_cbs["send_confirm"] = conv_callback - -cbs["conversation"] = conv_cbs - -def notify_callback(name): - print "---- notify callback example: %s" % name - -notify_cbs["notify_message"] = notify_callback -notify_cbs["notify_email"] = notify_callback -notify_cbs["notify_emails"] = notify_callback -notify_cbs["notify_formatted"] = notify_callback -notify_cbs["notify_searchresults"] = notify_callback -notify_cbs["notify_searchresults_new_rows"] = notify_callback -notify_cbs["notify_userinfo"] = notify_callback -notify_cbs["notify_uri"] = notify_callback -notify_cbs["close_notify"] = notify_callback - -cbs["notify"] = notify_cbs - -def request_callback(name): - print "---- request callback example: %s" % name - -request_cbs["request_input"] = request_callback -request_cbs["request_choice"] = request_callback -request_cbs["request_action"] = request_callback -request_cbs["request_fields"] = request_callback -request_cbs["request_file"] = request_callback -request_cbs["close_request"] = request_callback -request_cbs["request_folder"] = request_callback - -cbs["request"] = request_cbs - -class MainWindow: - def __init__(self, quit_cb): - global conv_cbs - global signal_cbs - self.bt_cbs = {} - self.new_acc_bt_cbs = {} - self.send_cbs = {} - self.quit_cb = quit_cb - conv_cbs["write_im"] = self._write_im_cb +# The information below is needed by libpurple +__NAME__ = "nullclient-ecore" +__VERSION__ = "0.1" +__WEBSITE__ = "N/A" +__DEV_WEBSITE__ = "N/A" - def init_window(self): - # Main vbox - vbox = etk.VBox(homogeneous=False) +class MainWindow(object): + def __init__(self): + # Connect button callback + self.__connect_button_cb = None - hbox_cmd = etk.HBox(homogeneous=False) - self.cmd_entry = etk.Entry() - 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) + # Send button callback + self.__send_button_cb = None - vbox_accs = etk.VBox() - self.accslistmodel = etk.ListModel() - self.accslist = etk.List(model=self.accslistmodel,\ - columns=[(10, etk.TextRenderer(slot=0),\ - False)], selectable=True,\ - animated_changes=True) - vbox_accs.append(self.accslist, etk.VBox.START, etk.VBox.EXPAND_FILL, 0) - - hbox_buttons = etk.HBox(homogeneous=False) - send_bt = etk.Button(label="Send") - send_bt.on_clicked(self._send_bt_cb) - conn_bt = etk.Button(label="Connect") - conn_bt.on_clicked(self.login_window) - new_account_bt = etk.Button(label="New Account") - new_account_bt.on_clicked(self._new_account) - hbox_buttons.append(send_bt, etk.HBox.START, etk.HBox.NONE, 0) - hbox_buttons.append(conn_bt, etk.HBox.START, etk.HBox.NONE, 0) - hbox_buttons.append(new_account_bt, etk.HBox.START, etk.HBox.NONE, 0) - - hbox_panel = etk.HBox() - - vbox_buddies = etk.VBox() - self.blistmodel = etk.ListModel() - self.blist = etk.List(model=self.blistmodel,\ - columns=[(10, etk.TextRenderer(slot=0), False)],\ - selectable=True, animated_changes=True) - vbox_buddies.append(self.blist, etk.VBox.START, etk.VBox.EXPAND_FILL, 0) + # New account callback + self.__new_account_cb = None - vbox_txt_area = etk.VBox() - self.txt_area = etk.Label() - self.txt_area.text = "
" + # Exit callback + self.__exit_cb = None - vbox_txt_area.append(self.txt_area, etk.VBox.START, etk.VBox.EXPAND_FILL, 0) + def __login_window_cb(self, pointer): + # Password entry + self.login_password = etk.Entry() - hbox_panel.append(vbox_txt_area, etk.HBox.START, etk.HBox.EXPAND_FILL, 0) - hbox_panel.append(vbox_buddies, etk.HBox.END, etk.HBox.EXPAND_FILL, 0) - hbox_panel.append(vbox_accs, etk.HBox.END, etk.HBox.EXPAND_FILL, 0) + # Confirm login button + confirm_login_bt = etk.Button(label='Ok') + confirm_login_bt.on_clicked(self.__connect_button_clicked) - self.lstatus = etk.Label(text="Connection status") + # Login VBox + vbox = etk.VBox() + vbox.append(self.login_password, etk.VBox.START, etk.VBox.FILL, 0) + vbox.append(confirm_login_bt, etk.VBox.END, etk.VBox.NONE, 0) - vbox.append(hbox_panel, etk.VBox.START, etk.VBox.EXPAND_FILL, 0) - vbox.append(hbox_cmd, etk.VBox.END, etk.VBox.FILL, 0) - vbox.append(hbox_buttons, etk.VBox.END, etk.VBox.NONE, 5) - vbox.append(self.lstatus, etk.VBox.END, etk.VBox.FILL, 0) + # Login window + self.login_window = etk.Window(title='Password', \ + size_request=(190, 80), child=vbox) + self.login_window.show_all() - self._window = etk.Window(title="NullClient-Etk", size_request=(600, 600), child=vbox) - self._window.on_destroyed(self.quit_cb) - self.set_global_callbacks() - self._window.show_all() + def __connect_button_clicked(self, pointer): + # Call connection button callback + if self.__connect_button_cb: + self.__connect_button_cb(self.login_password.text) + self.login_window.destroy() - def login_window(self, pointer): - self.login_password = etk.Entry() - confirm_login_bt = etk.Button(label="Ok") - confirm_login_bt.on_clicked(self._conn_bt_cb) - vbox_login = etk.VBox() - vbox_login.append(self.login_password, etk.VBox.START, etk.VBox.FILL, 0) - vbox_login.append(confirm_login_bt, etk.VBox.END, etk.VBox.NONE, 0) - self.login_win = etk.Window(title="Password", size_request=(190, 80), - child=vbox_login) - self.login_win.show_all() - - def set_global_callbacks(self): - global cbs - cbs["connection"]["connect_progress"] = self._purple_conn_status_cb - cbs["connection"]["disconnected"] = self._purple_disconnected_status_cb - cbs["connection"]["connected"] = self._purple_connected_cb - - def _conn_bt_cb(self, pointer): - if self.bt_cbs.has_key("on_clicked"): - self.bt_cbs["on_clicked"](self.login_password.text) - self.login_win.destroy() - - def _send_bt_cb(self, pointer): + def __send_button_clicked(self, pointer): + # Get selected buddy name from buddy list bname = self.blist.selected_rows[0][0] + + # Get message from command entry 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 selected_accs(self): - try: - acc = self.accslist.selected_rows[0][0] - if acc: - return acc - else: - return "None" - except: - return "None" - def _new_account(self, pointer): - if self.new_acc_bt_cbs.has_key("on_clicked"): - self.new_acc_bt_cbs["on_clicked"]() + if bname and msg != '': + # Call send button callback + if self.__send_button_cb: + self.__send_button_cb(bname, msg) + + # Clear text from command entry + self.cmd_entry.text = '' + + def __new_account_button_clicked(self, pointer): + # Username entry + self.login_username = etk.Entry() + + # Confirm username button + confirm_username_bt = etk.Button(label='Ok') + confirm_username_bt.on_clicked(self.add_account) + + # Username VBox + vbox = etk.VBox() + vbox.append(self.login_username, etk.VBox.START, etk.VBox.FILL, 0) + vbox.append(confirm_username_bt, etk.VBox.END, etk.VBox.NONE, 0) + + # Username window + self.username_window = etk.Window(title='Username', \ + size_request=(190, 80), child=vbox) + self.username_window.show_all() + + def __create_accounts_list(self): + # Accounts list + self.accslistmodel = etk.ListModel() + self.accslist = etk.List(model=self.accslistmodel, \ + columns=[(10, etk.TextRenderer(slot=0), False)], \ + selectable=True, animated_changes=True) + + #Appending accounts list to VBox + vbox = etk.VBox() + vbox.append(self.accslist, etk.VBox.START, etk.VBox.EXPAND_FILL, 0) + return vbox + + def __create_buddies_list(self): + # Buddies list + self.blistmodel = etk.ListModel() + self.blist = etk.List(model=self.blistmodel, \ + columns=[(10, etk.TextRenderer(slot=0), False)], \ + selectable=True, animated_changes=True) + + # Appending buddies list to VBox + vbox = etk.VBox() + vbox.append(self.blist, etk.VBox.START, etk.VBox.EXPAND_FILL, 0) + return vbox + + def __create_buttons_bar(self): + # Send button + send_button = etk.Button(label='Send') + send_button.on_clicked(self.__send_button_clicked) + + # Connect button + conn_button = etk.Button(label='Connect') + conn_button.on_clicked(self.__login_window_cb) + + # New account button + new_acc_button = etk.Button(label='New Account') + new_acc_button.on_clicked(self.__new_account_button_clicked) + + # Appending all buttons to HBox + hbox = etk.HBox(homogeneous=False) + hbox.append(send_button, etk.HBox.START, etk.HBox.NONE, 0) + hbox.append(conn_button, etk.HBox.START, etk.HBox.NONE, 0) + hbox.append(new_acc_button, etk.HBox.START, etk.HBox.NONE, 0) + return hbox + + def __create_command_entry_box(self): + # Command entry box + self.cmd_entry = etk.Entry() + self.cmd_label = etk.Label(text='Type your message: ') + + # appending command entry and label to HBox + hbox = etk.HBox(homogeneous=False) + hbox.append(self.cmd_label, etk.HBox.START, \ + etk.HBox.START, 0) + hbox.append(self.cmd_entry, etk.HBox.START, \ + etk.HBox.EXPAND_FILL, 0) + return hbox + + def __create_text_area(self): + # Text area (shows buddy messages) + self.txt_area = etk.Label() + self.txt_area.text = '
Nullclient-Ecore
' + + # Appending text area to VBox + vbox = etk.VBox() + vbox.append(self.txt_area, etk.VBox.START, etk.VBox.EXPAND_FILL, 0) + return vbox - def _purple_conn_status_cb(self, txt, step, step_count): - self.lstatus.text = txt + def __create_main_panel(self): + # Text box + txt_vbox = self.__create_text_area() - def _purple_connected_cb(self): - self.lstatus.text = "Connected" + # Buddies list + bdd_vbox = self.__create_buddies_list() - def new_buddy(self, b): - if [b] not in self.blistmodel.elements: - self.blistmodel.append([b]) + # Accounts list + acc_vbox = self.__create_accounts_list() - def remove_buddy(self, bname): - self.blistmodel.remove([bname]) + # Appending text area, buddies list and accounts list to HBox + hbox = etk.HBox() + hbox.append(txt_vbox, etk.HBox.START, etk.HBox.EXPAND_FILL, 0) + hbox.append(bdd_vbox, etk.HBox.END, etk.HBox.EXPAND_FILL, 0) + hbox.append(acc_vbox, etk.HBox.END, etk.HBox.EXPAND_FILL, 0) + return hbox - def new_account(self, a): - if [a] not in self.accslistmodel.elements: - self.accslistmodel.append([a]) + def __create_main_box(self): + # Main panel + panel_hbox = self.__create_main_panel() - def _purple_disconnected_status_cb(self): - self.lstatus.text = "Disconnected" + # Command entry + cmd_hbox = self.__create_command_entry_box() - def set_panel_text(self, txt): - self.txt_area = txt + # Buttons Bar + btn_hbox = self.__create_buttons_bar() - def add_bt_conn_cb(self, cb): + # Connection status + self.status = etk.Label(text='Connection status') + + # Main VBox + vbox = etk.VBox(homogeneous=False) + vbox.append(panel_hbox, etk.VBox.START, etk.VBox.EXPAND_FILL, 0) + vbox.append(cmd_hbox, etk.VBox.END, etk.VBox.FILL, 0) + vbox.append(btn_hbox, etk.VBox.END, etk.VBox.NONE, 5) + vbox.append(self.status, etk.VBox.END, etk.VBox.FILL, 0) + return vbox + + def get_selected_account(self): + # Catch selected account from accounts list + try: + account = self.accslist.selected_rows[0][0] + if account: + return account + else: + return None + except: + return None + + def add_buddy(self, name): + # Adds a new buddy into buddy list + if [name] not in self.blistmodel.elements: + self.blistmodel.append([name]) + + def remove_buddy(self, name): + # Removes a buddy from buddy list + self.blistmodel.remove([name]) + + def add_account(self, pointer): + # Adds a new account into accounts list + if [self.login_username.text] not in self.accslistmodel.elements: + self.accslistmodel.append([self.login_username.text]) + self.username_window.destroy() + self.window.show_all() + + def add_connection_button_cb(self, cb): if callable(cb): - self.bt_cbs["on_clicked"] = cb + self.__connect_button_cb = cb - def add_account_cb(self, cb): + def add_new_account_cb(self, cb): if callable(cb): - self.new_acc_bt_cbs["on_clicked"] = cb + self.__new_account_cb = cb - def add_send_cb(self, cb): + def add_send_button_cb(self, cb): if callable(cb): - self.send_cbs["on_clicked"] = cb + self.__send_button_cb = cb - def add_quit_cb(self, cb): + def add_exit_cb(self, cb): if callable(cb): - self.quit_cb = cb + self.__exit_cb = cb + + def init_window(self): + # Main box + main_box = self.__create_main_box() - def _write_im_cb(self, name, who, message): - if who: - w = who.split("/")[0] - self.txt_area.text += w + ": " + str(message) + "
" - else: - self.txt_area.text += str(name) + ": " + str(message) + "
" - self._window.show_all() + # Main Window + self.window = etk.Window(title='Nullclient-Ecore', \ + size_request=(600, 600), child=main_box) + self.window.on_destroyed(self.__exit_cb) + self.window.show_all() + def show(self): + if self.window: + self.window.show_all() -class NullClientPurple: +class NullClient(object): def __init__(self): - self.p = purple.Purple(debug_enabled=False) - self.window = MainWindow(self.quit) - self.buddies = {} #all buddies + # Sets initial parameters + self.core = purple.Purple(__NAME__, __VERSION__, __WEBSITE__, \ + __DEV_WEBSITE__, debug_enabled=True, default_path='/tmp') + self.account = None + self.buddies = {} self.conversations = {} - self.protocol_id = "prpl-jabber" - self.accs = None + self.protocol = purple.Protocol('prpl-jabber') + self.window = MainWindow() + + # Adds libpurple core callbacks + + # Updates buddy list + self.core.add_callback('blist', 'update', \ + self.__update_blist_cb) + + # Updates connection progress + self.core.add_callback('connection', 'connect-progress', \ + self.__connection_progress_cb) - global cbs - cbs["blist"]["update"] = self._purple_update_blist_cb + # Activates when an account is connected + self.core.add_callback('connection', 'connected', \ + self.__connected_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) + # Activates when an account is disconnected + self.core.add_callback('connection', 'disconnected', \ + self.__disconected_cb) + # Activates when a message is sent or received from conversation + self.core.add_callback('conversation', 'write-im', \ + self.__write_im_cb) - self.p.purple_init(cbs) - self.p.accounts_init() + # Signal when account signed on + self.core.signal_connect('signed-on', self.__signed_on_cb) - #Initializing UI - self.window.add_bt_conn_cb(self.connect) - self.window.add_send_cb(self.send_msg) - self.window.add_account_cb(self.add_account) + # Signal when account signed off + self.core.signal_connect('signed-off', self.__signed_off_cb) + + # Signal when buddy signed on + self.core.signal_connect('buddy-signed-on', self.__buddy_signed_on_cb) + + # Signed when buddy signed off + self.core.signal_connect('buddy-signed-off', self.__buddy_signed_off_cb) + + # Adds UI callbacks + self.window.add_connection_button_cb(self.connect) + self.window.add_send_button_cb(self.send_message) + self.window.add_exit_cb(self.exit) + + # Initializes libpurple + self.core.purple_init() + + # Initializes UI 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 __update_blist_cb(self, type, name=None, alias=None): + if self.account and name 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: - 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) - print "[DEBUG]: Buddy removed!" - 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 + self.buddies[name] = purple.Buddy(name, self.account) + if self.buddies[name].online: + self.window.add_buddy(name) + + def __connection_progress_cb(self, text, step, step_count): + if self.window: + self.window.status.text = text + + def __connected_cb(self, *data): + if self.window: + self.window.status.text = 'Connected' + + def __disconected_cb(): + if self.window: + self.window.status.text = 'Disconnected' + + def __write_im_cb(self, username, name, alias, message, flags): + if self.window: + if 'SEND' == flags: + self.window.txt_area.text += username + ": " + message + "
" + elif alias: + self.window.txt_area.text += alias + ": " + message + "
" + else: + self.window.txt_area.text += name + ": " + message + "
" + self.window.show() + + def __signed_on_cb(self, username, protocol_id): + if self.window: + self.window.txt_area += 'Signed on: %s (%s)' % (username, protocol_id) + self.window.show() + + def __signed_off_cb(self, username, protocol_id): + if self.window: + self.window.txt_area += 'Signed off: %s (%s)' % (username, protocol_id) + self.window.show() + + def __buddy_signed_on_cb(self, name, alias): + if self.window: + self.window.txt_area += 'Buddy signed on: %s (%s)' % (name, alias) + self.window.show() + + def __buddy_signed_off_cb(self, name, alias): + if self.buddies.has_key(name): + del self.buddies[name] + + if self.window: + self.window.txt_area += 'Buddy signed off: %s (%s)' % (name, alias) + self.window.remove_buddy(name) + self.window.show() 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) - - 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() - for acc in self.accs.keys(): - self.window.new_account(acc) - - def send_msg(self, name, msg): + username = self.window.get_selected_account() + if username and password: + self.account = purple.Account(username, self.protocol, self.core) + if not self.account.exists: + self.account.new() + info = {} + info['connect_server'] = 'talk.google.com' + info['port'] = '443' + info['old_ssl'] = True + self.account.set_protocol_options(info) + + self.account.set_password(password) + self.account.set_enabled(True) + + def send_message(self, name, message): + print name, message 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 "[DEBUG]: quitting" - for i in self.conversations: - self.conversations[i].destroy() - self.conversations[i] = None - self.conversations = None - self.p.destroy() + self.conversations[name] = purple.Conversation('IM', self.account, name) + self.conversations[name].new() + + self.conversations[name].im_send(message) + + def exit(self, pointer): ecore.main_loop_quit() if __name__ == '__main__': + client = NullClient() - nullpurple = NullClientPurple() + # Initializes ecore mainloop ecore.main_loop_begin()