Updated internal class attributes with correct naming.
[python-purple] / nullclient-ecore.py
1 import etk
2 import ecore
3 import purple
4
5 cbs = {}
6 acc_cbs = {}
7 blist_cbs = {}
8 conn_cbs = {}
9 conv_cbs = {}
10 notify_cbs = {}
11 request_cbs = {}
12
13 def account_callback(name):
14     print "---- account callback example: %s" % name
15
16 def blist_callback(name):
17     print "---- blist callback example: %s" % name
18
19 #blist_cbs["new_list"] = blist_callback
20 #blist_cbs["new_node"] = blist_callback
21 #blist_cbs["show"] = blist_callback
22 #blist_cbs["update"] = blist_callback
23 #blist_cbs["remove"] = blist_callback
24 #blist_cbs["destroy"] = blist_callback
25 blist_cbs["set_visible"] = blist_callback
26 blist_cbs["request_add_buddy"] = blist_callback
27 blist_cbs["request_add_chat"] = blist_callback
28 blist_cbs["request_add_group"] = blist_callback
29
30 cbs["blist"] = blist_cbs
31
32 def conn_callback(name):
33     print "---- connection callback example: %s" % name
34
35 def conn_progress_cb(data):
36     return "Connection in progress..."
37
38 #conn_cbs["connect_progress"] = conn_progress_cb
39 #conn_cbs["connected"] = conn_callback
40 #conn_cbs["disconnected"] = conn_callback
41 conn_cbs["notice"] = conn_callback
42 conn_cbs["report_disconnect"] = conn_callback
43 conn_cbs["network_connected"] = conn_callback
44 conn_cbs["network_disconnected"] = conn_callback
45 conn_cbs["report_disconnect_reason"] = conn_callback
46
47 cbs["connection"] = conn_cbs
48
49 def conv_callback(name):
50     print "---- conversation callback example: %s" % name
51
52 #conv_cbs["create_conversation"] = conv_callback
53 #conv_cbs["destroy_conversation"] = conv_callback
54 conv_cbs["write_chat"] = conv_callback
55 conv_cbs["write_conv"] = conv_callback
56 #conv_cbs["write_im"] = conv_callback
57 conv_cbs["chat_add_users"] = conv_callback
58 conv_cbs["chat_rename_user"] = conv_callback
59 conv_cbs["chat_remove_users"] = conv_callback
60 conv_cbs["chat_update_user"] = conv_callback
61 conv_cbs["present"] = conv_callback
62 conv_cbs["has_focus"] = conv_callback
63 conv_cbs["custom_smiley_add"] = conv_callback
64 conv_cbs["custom_smiley_write"] = conv_callback
65 conv_cbs["custom_smiley_close"] = conv_callback
66 conv_cbs["send_confirm"] = conv_callback
67
68 cbs["conversation"] = conv_cbs
69
70 def notify_callback(name):
71     print "----  notify callback example: %s" % name
72
73 notify_cbs["notify_message"] = notify_callback
74 notify_cbs["notify_email"] = notify_callback
75 notify_cbs["notify_emails"] = notify_callback
76 notify_cbs["notify_formatted"] = notify_callback
77 notify_cbs["notify_searchresults"] = notify_callback
78 notify_cbs["notify_searchresults_new_rows"] = notify_callback
79 notify_cbs["notify_userinfo"] = notify_callback
80 notify_cbs["notify_uri"] = notify_callback
81 notify_cbs["close_notify"] = notify_callback
82
83 cbs["notify"] = notify_cbs
84
85 def request_callback(name):
86     print "---- request callback example: %s" % name
87
88 request_cbs["request_input"] = request_callback
89 request_cbs["request_choice"] = request_callback
90 request_cbs["request_action"] = request_callback
91 request_cbs["request_fields"] = request_callback
92 request_cbs["request_file"] = request_callback
93 request_cbs["close_request"] = request_callback
94 request_cbs["request_folder"] = request_callback
95
96 cbs["request"] = request_cbs
97
98 class MainWindow:
99     def __init__(self, quit_cb):
100         global conv_cbs
101         global signal_cbs
102         self.bt_cbs = {}
103         self.new_acc_bt_cbs = {}
104         self.send_cbs = {}
105         self.quit_cb = quit_cb
106         conv_cbs["write_im"] = self._write_im_cb
107
108     def init_window(self):
109         # Main vbox
110         vbox = etk.VBox(homogeneous=False)
111
112         hbox_cmd = etk.HBox(homogeneous=False)
113         self.cmd_entry = etk.Entry()
114         self.lcmd = etk.Label(text="Type your message: ")
115         hbox_cmd.append(self.lcmd, etk.HBox.START, etk.HBox.START, 0)
116         hbox_cmd.append(self.cmd_entry, etk.HBox.START, etk.HBox.EXPAND_FILL, 0)
117
118         vbox_accs = etk.VBox()
119         self.accslistmodel = etk.ListModel()
120         self.accslist = etk.List(model=self.accslistmodel,\
121                 columns=[(10, etk.TextRenderer(slot=0),\
122                 False)], selectable=True,\
123                 animated_changes=True)
124         vbox_accs.append(self.accslist, etk.VBox.START, etk.VBox.EXPAND_FILL, 0)
125
126         hbox_buttons = etk.HBox(homogeneous=False)
127         send_bt = etk.Button(label="Send")
128         send_bt.on_clicked(self._send_bt_cb)
129         conn_bt = etk.Button(label="Connect")
130         conn_bt.on_clicked(self.login_window)
131         new_account_bt = etk.Button(label="New Account")
132         new_account_bt.on_clicked(self._new_account)
133         hbox_buttons.append(send_bt, etk.HBox.START, etk.HBox.NONE, 0)
134         hbox_buttons.append(conn_bt, etk.HBox.START, etk.HBox.NONE, 0)
135         hbox_buttons.append(new_account_bt, etk.HBox.START, etk.HBox.NONE, 0)
136
137         hbox_panel = etk.HBox()
138
139         vbox_buddies = etk.VBox()
140         self.blistmodel = etk.ListModel()
141         self.blist = etk.List(model=self.blistmodel,\
142                                      columns=[(10, etk.TextRenderer(slot=0), False)],\
143                                      selectable=True, animated_changes=True)
144         vbox_buddies.append(self.blist, etk.VBox.START, etk.VBox.EXPAND_FILL, 0)
145
146         vbox_txt_area = etk.VBox()
147         self.txt_area = etk.Label()
148         self.txt_area.text = "<br> "
149
150         vbox_txt_area.append(self.txt_area, etk.VBox.START, etk.VBox.EXPAND_FILL, 0)
151
152         hbox_panel.append(vbox_txt_area, etk.HBox.START, etk.HBox.EXPAND_FILL, 0)
153         hbox_panel.append(vbox_buddies, etk.HBox.END, etk.HBox.EXPAND_FILL, 0)
154         hbox_panel.append(vbox_accs, etk.HBox.END, etk.HBox.EXPAND_FILL, 0)
155
156         self.lstatus = etk.Label(text="Connection status")
157
158         vbox.append(hbox_panel, etk.VBox.START, etk.VBox.EXPAND_FILL, 0)
159         vbox.append(hbox_cmd, etk.VBox.END, etk.VBox.FILL, 0)
160         vbox.append(hbox_buttons, etk.VBox.END, etk.VBox.NONE, 5)
161         vbox.append(self.lstatus, etk.VBox.END, etk.VBox.FILL, 0)
162
163         self._window = etk.Window(title="NullClient-Etk", size_request=(600, 600), child=vbox)
164         self._window.on_destroyed(self.quit_cb)
165         self.set_global_callbacks()
166         self._window.show_all()
167
168     def login_window(self, pointer):
169         self.login_password = etk.Entry()
170         confirm_login_bt = etk.Button(label="Ok")
171         confirm_login_bt.on_clicked(self._conn_bt_cb)
172         vbox_login =  etk.VBox()
173         vbox_login.append(self.login_password, etk.VBox.START, etk.VBox.FILL, 0)
174         vbox_login.append(confirm_login_bt, etk.VBox.END, etk.VBox.NONE, 0)
175         self.login_win = etk.Window(title="Password", size_request=(190, 80),
176                 child=vbox_login)
177         self.login_win.show_all()
178
179     def set_global_callbacks(self):
180         global cbs
181         cbs["connection"]["connect_progress"] = self._purple_conn_status_cb
182         cbs["connection"]["disconnected"] = self._purple_disconnected_status_cb
183         cbs["connection"]["connected"] = self._purple_connected_cb
184
185     def _conn_bt_cb(self, pointer):
186         if self.bt_cbs.has_key("on_clicked"):
187             self.bt_cbs["on_clicked"](self.login_password.text)
188             self.login_win.destroy()
189
190     def _send_bt_cb(self, pointer):
191         bname = self.blist.selected_rows[0][0]
192         msg = self.cmd_entry.text
193         if bname and msg != "":
194             if self.send_cbs.has_key("on_clicked"):
195                 self.send_cbs["on_clicked"](bname, msg)
196         else:
197             print "Buddy not selected!"
198         self.cmd_entry.text = ""
199
200     def selected_accs(self):
201         try:
202             acc = self.accslist.selected_rows[0][0]
203             if acc:
204                 return acc
205             else:
206                 return "None"
207         except:
208             return "None"
209
210     def _new_account(self, pointer):
211         if self.new_acc_bt_cbs.has_key("on_clicked"):
212             self.new_acc_bt_cbs["on_clicked"]()
213
214     def _purple_conn_status_cb(self, txt, step, step_count):
215             self.lstatus.text = txt
216
217     def _purple_connected_cb(self):
218         self.lstatus.text = "Connected"
219
220     def new_buddy(self, b):
221             if [b] not in self.blistmodel.elements:
222                 self.blistmodel.append([b])
223
224     def remove_buddy(self, bname):
225         self.blistmodel.remove([bname])
226
227     def new_account(self, a):
228         if [a] not in self.accslistmodel.elements:
229             self.accslistmodel.append([a])
230
231     def _purple_disconnected_status_cb(self):
232         self.lstatus.text = "Disconnected"
233
234     def set_panel_text(self, txt):
235         self.txt_area = txt
236
237     def add_bt_conn_cb(self, cb):
238         if callable(cb):
239             self.bt_cbs["on_clicked"] = cb
240
241     def add_account_cb(self, cb):
242         if callable(cb):
243             self.new_acc_bt_cbs["on_clicked"] = cb
244
245     def add_send_cb(self, cb):
246         if callable(cb):
247             self.send_cbs["on_clicked"] = cb
248
249     def add_quit_cb(self, cb):
250         if callable(cb):
251             self.quit_cb = cb
252
253     def _write_im_cb(self, name, who, message):
254         if who:
255             w = who.split("/")[0]
256             self.txt_area.text +=  w + ": " + str(message) + "<br> "
257         else:
258             self.txt_area.text += str(name) + ": " + str(message) + "<br> "
259         self._window.show_all()
260
261
262 class NullClientPurple:
263     def __init__(self):
264         self.p = purple.Purple(debug_enabled=False)
265         self.window = MainWindow(self.quit)
266         self.buddies = {} #all buddies
267         self.conversations = {}
268         self.protocol_id = "prpl-jabber"
269         self.accs = None
270
271         global cbs
272         cbs["blist"]["update"] = self._purple_update_blist_cb
273
274         self.p.add_account_cb("notify_added", account_callback)
275         self.p.add_account_cb("status_changed", account_callback)
276         self.p.add_account_cb("request_add", account_callback)
277         self.p.add_account_cb("request_authorize", account_callback)
278         self.p.add_account_cb("close_account_request", account_callback)
279
280
281         self.p.purple_init(cbs)
282         self.p.accounts_init()
283
284         #Initializing UI
285         self.window.add_bt_conn_cb(self.connect)
286         self.window.add_send_cb(self.send_msg)
287         self.window.add_account_cb(self.add_account)
288         self.window.init_window()
289
290     def _purple_update_blist_cb(self, type, name=None, alias=None, totalsize=None,\
291                              currentsize=None, online=None):
292         if type == 2:
293             if not self.buddies.has_key(name):
294                 b = purple.Buddy()
295                 b.new_buddy(self.account, name, alias)
296                 self.buddies[name] = b
297             elif self.buddies[name].online is True:
298                 self.window.new_buddy(name)
299
300     def _purple_signal_sign_off_cb(self, name, bname):
301         if self.buddies.has_key(bname):
302             self.buddies[bname] = None
303             self.buddies.pop(bname)
304             print "[DEBUG]: Buddy removed!"
305         self.window.remove_buddy(bname)
306
307     def _purple_create_conv_cb(self, name, type):
308         bname = name.split("/")[0]
309         if bname in self.buddies and not self.conversations.has_key(name):
310             conv = purple.Conversation()
311             conv.initialize(self.account, "IM", bname)
312             self.conversations[bname] = conv
313
314     def connect(self, password):
315         username_acc = self.window.selected_accs()
316         self.account = self.p.account_verify(username_acc)
317         self.account.get_protocol_options()
318         self.account.set_enabled("carman-purple-python", True)
319         self.account.password = password
320         self.p.connect()
321         self.p.signal_connect("buddy-signed-off", self._purple_signal_sign_off_cb)
322
323     def add_account(self):
324         username = "carmanplugintest@gmail.com"
325         host = "172.18.216.211"
326         port = 8080
327         self.p.account_add(username, self.protocol_id, host, port)
328         self.accs = self.p.accounts_get_dict()
329         for acc in self.accs.keys():
330             self.window.new_account(acc)
331
332     def send_msg(self, name, msg):
333         if not self.conversations.has_key(name):
334             conv = purple.Conversation()
335             conv.initialize(self.account, "IM", name)
336             self.conversations[name] = conv
337         self.conversations[name].write(msg)
338
339     def quit(self, o):
340         print "[DEBUG]: quitting"
341         for i in self.conversations:
342             self.conversations[i].destroy()
343             self.conversations[i] = None
344         self.conversations = None
345         self.p.destroy()
346         ecore.main_loop_quit()
347
348 if __name__ == '__main__':
349
350     nullpurple = NullClientPurple()
351     ecore.main_loop_begin()