Removed deprecated code from Purple class.
[python-purple] / nullclient-ecore.py
1 #!/usr/bin/env python
2
3 import etk
4 import ecore
5 import purple
6 #from xml.dom import minidom
7
8 class MainWindow:
9     def __init__(self, quit_cb):
10         self.bt_cbs = {}
11         self.new_acc_bt_cbs = {}
12         self.send_cbs = {}
13         self.quit_cb = quit_cb
14
15     def init_window(self):
16         # Main vbox
17         vbox = etk.VBox(homogeneous=False)
18
19         hbox_cmd = etk.HBox(homogeneous=False)
20         self.cmd_entry = etk.Entry()
21         self.lcmd = etk.Label(text="Type your message: ")
22         hbox_cmd.append(self.lcmd, etk.HBox.START, etk.HBox.START, 0)
23         hbox_cmd.append(self.cmd_entry, etk.HBox.START, etk.HBox.EXPAND_FILL, 0)
24
25         vbox_accs = etk.VBox()
26         self.accslistmodel = etk.ListModel()
27         self.accslist = etk.List(model=self.accslistmodel,\
28                 columns=[(10, etk.TextRenderer(slot=0),\
29                 False)], selectable=True,\
30                 animated_changes=True)
31         vbox_accs.append(self.accslist, etk.VBox.START, etk.VBox.EXPAND_FILL, 0)
32
33         hbox_buttons = etk.HBox(homogeneous=False)
34         send_bt = etk.Button(label="Send")
35         send_bt.on_clicked(self._send_bt_cb)
36         conn_bt = etk.Button(label="Connect")
37         conn_bt.on_clicked(self.login_window)
38         new_account_bt = etk.Button(label="New Account")
39         new_account_bt.on_clicked(self._new_account)
40         hbox_buttons.append(send_bt, etk.HBox.START, etk.HBox.NONE, 0)
41         hbox_buttons.append(conn_bt, etk.HBox.START, etk.HBox.NONE, 0)
42         hbox_buttons.append(new_account_bt, etk.HBox.START, etk.HBox.NONE, 0)
43
44         hbox_panel = etk.HBox()
45
46         vbox_buddies = etk.VBox()
47         self.blistmodel = etk.ListModel()
48         self.blist = etk.List(model=self.blistmodel,\
49                                      columns=[(10, etk.TextRenderer(slot=0), False)],\
50                                      selectable=True, animated_changes=True)
51         vbox_buddies.append(self.blist, etk.VBox.START, etk.VBox.EXPAND_FILL, 0)
52
53         vbox_txt_area = etk.VBox()
54         self.txt_area = etk.Label()
55         self.txt_area.text = "<br> "
56
57         vbox_txt_area.append(self.txt_area, etk.VBox.START, etk.VBox.EXPAND_FILL, 0)
58
59         hbox_panel.append(vbox_txt_area, etk.HBox.START, etk.HBox.EXPAND_FILL, 0)
60         hbox_panel.append(vbox_buddies, etk.HBox.END, etk.HBox.EXPAND_FILL, 0)
61         hbox_panel.append(vbox_accs, etk.HBox.END, etk.HBox.EXPAND_FILL, 0)
62
63         self.lstatus = etk.Label(text="Connection status")
64
65         vbox.append(hbox_panel, etk.VBox.START, etk.VBox.EXPAND_FILL, 0)
66         vbox.append(hbox_cmd, etk.VBox.END, etk.VBox.FILL, 0)
67         vbox.append(hbox_buttons, etk.VBox.END, etk.VBox.NONE, 5)
68         vbox.append(self.lstatus, etk.VBox.END, etk.VBox.FILL, 0)
69
70         self._window = etk.Window(title="NullClient-Etk", size_request=(600, 600), child=vbox)
71         self._window.on_destroyed(self.quit_cb)
72         self._window.show_all()
73
74     def login_window(self, pointer):
75         self.login_password = etk.Entry()
76         confirm_login_bt = etk.Button(label="Ok")
77         confirm_login_bt.on_clicked(self._conn_bt_cb)
78         vbox_login =  etk.VBox()
79         vbox_login.append(self.login_password, etk.VBox.START, etk.VBox.FILL, 0)
80         vbox_login.append(confirm_login_bt, etk.VBox.END, etk.VBox.NONE, 0)
81         self.login_win = etk.Window(title="Password", size_request=(190, 80),
82                 child=vbox_login)
83         self.login_win.show_all()
84
85     def _conn_bt_cb(self, pointer):
86         if self.bt_cbs.has_key("on_clicked"):
87             self.bt_cbs["on_clicked"](self.login_password.text)
88             self.login_win.destroy()
89
90     def _send_bt_cb(self, pointer):
91         bname = self.blist.selected_rows[0][0]
92         msg = self.cmd_entry.text
93         if bname and msg != "":
94             if self.send_cbs.has_key("on_clicked"):
95                 self.send_cbs["on_clicked"](bname, msg)
96         else:
97             print "Buddy not selected!"
98         self.cmd_entry.text = ""
99
100     def selected_accs(self):
101         try:
102             acc = self.accslist.selected_rows[0][0]
103             if acc:
104                 return acc
105             else:
106                 return None
107         except:
108             return None
109
110     def _new_account(self, pointer):
111         if self.new_acc_bt_cbs.has_key("on_clicked"):
112             self.new_acc_bt_cbs["on_clicked"]()
113
114
115     def new_buddy(self, b):
116             if [b] not in self.blistmodel.elements:
117                 self.blistmodel.append([b])
118
119     def remove_buddy(self, bname):
120         self.blistmodel.remove([bname])
121
122     def new_account(self, a):
123         if [a] not in self.accslistmodel.elements:
124             self.accslistmodel.append([a])
125
126     def set_panel_text(self, txt):
127         self.txt_area = txt
128
129     def add_bt_conn_cb(self, cb):
130         if callable(cb):
131             self.bt_cbs["on_clicked"] = cb
132
133     def add_account_cb(self, cb):
134         if callable(cb):
135             self.new_acc_bt_cbs["on_clicked"] = cb
136
137     def add_send_cb(self, cb):
138         if callable(cb):
139             self.send_cbs["on_clicked"] = cb
140
141     def add_quit_cb(self, cb):
142         if callable(cb):
143             self.quit_cb = cb
144
145     def show(self):
146         if self._window:
147             self._window.show_all()
148
149 class NullClientPurple(object):
150     def __init__(self):
151         self.purple = purple.Purple(debug_enabled=False)
152         self.window = MainWindow(self.quit)
153         self.buddies = {} #all buddies
154         self.conversations = {}
155         self.protocol_id = "prpl-jabber"
156         self.account = None
157
158         self.purple.add_callback("blist", "update", self.__purple_update_blist_cb)
159         self.purple.add_callback("connection", "connect-progress", self.__purple_conn_progress_cb)
160         self.purple.add_callback("connection", "connected", self.__purple_connected_cb)
161         self.purple.add_callback("connection", "disconnected", self.__purple_disconnected_cb)
162         self.purple.add_callback("conversation", "write-im", self.__purple_write_im_cb)
163
164         self.purple.purple_init()
165
166         #Initializing UI
167         self.window.add_bt_conn_cb(self.connect)
168         self.window.add_send_cb(self.send_msg)
169         self.window.add_account_cb(self.add_account)
170         self.window.init_window()
171
172     def __purple_update_blist_cb(self, type, name=None, alias=None, \
173                                 totalsize=None, currentsize=None, \
174                                 online=None):
175         if self.account and name != None and type == 2:
176             if not self.buddies.has_key(name):
177                 b = purple.Buddy()
178                 b.new_buddy(self.account, name, alias)
179                 self.buddies[name] = b
180             elif self.buddies[name].online:
181                 self.window.new_buddy(name)
182
183     def __purple_conn_progress_cb(self, text, step, step_count):
184         if self.window:
185             self.window.lstatus.text = text
186
187     def __purple_connected_cb(self, *data):
188         if self.window:
189             self.window.lstatus.text = "Connected"
190
191     def __purple_disconnected_cb(self, *data):
192         if self.window:
193             self.window.lstatus.text = "Disconnected"
194
195     def __purple_write_im_cb(self, username, name, alias, message):
196         if self.window:
197             if alias:
198                 self.window.txt_area.text += alias + ": " + message + "<br> "
199             else:
200                 self.window.txt_area.text += name + ": " + message + "<br> "
201             self.window.show()
202
203     def __purple_signal_signed_on_cb(self, username, protocol_id):
204         print "[DEBUG] Signed on: %s %s" % (username, protocol_id)
205
206     def __purple_signal_signed_off_cb(self, username, protocol_id):
207         print "[DEBUG] Signed off: %s %s" % (username, protocol_id)
208
209     def __purple_signal_buddy_signed_on_cb(self, name, alias):
210         print "[DEBUG]: Buddy signed on: %s %s" % (name, alias)
211
212     def __purple_signal_buddy_signed_off_cb(self, name, alias):
213         if self.buddies.has_key(name):
214             self.buddies[name] = None
215             self.buddies.pop(name)
216             print "[DEBUG]: Buddy signed off: %s %s" % (name, alias)
217         self.window.remove_buddy(name)
218
219     """
220     def __purple_signal_jabber_receiving_xmlnode_cb(self, message):
221         xml = minidom.parse(message)
222         for msg in xml.getElementsByTagName("message"):
223             who = msg.getAttribute("from")
224             for geoloc in msg.getElementsByTagNameNS("http://jabber.org/protocol/geoloc", "geoloc"):
225                 lat = geoloc.getElementsByTagName("lat")[0].childNodes[0].nodeValue
226                 lon = geoloc.getElementsByTagName("lon")[0].childNodes[0].nodeValue
227                 print "who: %s lat: %s lon: %s" % (who, lat, lon)
228     """
229
230     def add_account(self):
231         username = "carmanplugintest@gmail.com"
232         host = "172.18.216.211"
233         port = 8080
234         self.purple.account_add(username, self.protocol_id, host, port)
235         for account in self.purple.accounts.keys():
236             self.window.new_account(account)
237
238     def connect(self, password):
239         username_acc = self.window.selected_accs()
240         if username_acc:
241             self.account = self.purple.account_verify(username_acc)
242             self.account.get_protocol_options()
243             self.account.set_enabled("carman-purple-python", True)
244             self.account.password = password
245             self.purple.signal_connect("signed-on", self.__purple_signal_signed_on_cb)
246             self.purple.signal_connect("signed-off", self.__purple_signal_signed_off_cb)
247             self.purple.signal_connect("buddy-signed-on", self.__purple_signal_buddy_signed_on_cb)
248             self.purple.signal_connect("buddy-signed-off", self.__purple_signal_buddy_signed_off_cb)
249             #self.purple.signal_connect("jabber-receiving-xmlnode", self.__purple_signal_jabber_receiving_xmlnode_cb)
250
251     def send_msg(self, name, msg):
252         if not self.conversations.has_key(name):
253             conv = purple.Conversation()
254             conv.initialize(self.account, "IM", name)
255             self.conversations[name] = conv
256         self.conversations[name].write(msg)
257
258     def quit(self, o):
259         print "[DEBUG]: quitting"
260         for i in self.conversations:
261             self.conversations[i].destroy()
262             self.conversations[i] = None
263         self.conversations = None
264         self.purple.destroy()
265         ecore.main_loop_quit()
266
267 if __name__ == '__main__':
268     nullpurple = NullClientPurple()
269     ecore.main_loop_begin()