Modified account functions.
[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 acc_cbs["notify_added"] = account_callback
17 acc_cbs["status_changed"] = account_callback
18 acc_cbs["request_add"] = account_callback
19 acc_cbs["request_authorize"] = account_callback
20 acc_cbs["close_account_request"] = account_callback
21
22 cbs["account"] = acc_cbs
23
24 def blist_callback(name):
25     print "---- blist callback example: %s" % name
26
27 #blist_cbs["new_list"] = blist_callback
28 #blist_cbs["new_node"] = blist_callback
29 #blist_cbs["show"] = blist_callback
30 #blist_cbs["update"] = blist_callback
31 #blist_cbs["remove"] = blist_callback
32 #blist_cbs["destroy"] = blist_callback
33 blist_cbs["set_visible"] = blist_callback
34 blist_cbs["request_add_buddy"] = blist_callback
35 blist_cbs["request_add_chat"] = blist_callback
36 blist_cbs["request_add_group"] = blist_callback
37
38 cbs["blist"] = blist_cbs
39
40 def conn_callback(name):
41     print "---- connection callback example: %s" % name
42
43 def conn_progress_cb(data):
44     return "Connection in progress..."
45
46 #conn_cbs["connect_progress"] = conn_progress_cb
47 #conn_cbs["connected"] = conn_callback
48 #conn_cbs["disconnected"] = conn_callback
49 conn_cbs["notice"] = conn_callback
50 conn_cbs["report_disconnect"] = conn_callback
51 conn_cbs["network_connected"] = conn_callback
52 conn_cbs["network_disconnected"] = conn_callback
53 conn_cbs["report_disconnect_reason"] = conn_callback
54
55 cbs["connection"] = conn_cbs
56
57 def conv_callback(name):
58     print "---- conversation callback example: %s" % name
59
60 #conv_cbs["create_conversation"] = conv_callback
61 #conv_cbs["destroy_conversation"] = conv_callback
62 conv_cbs["write_chat"] = conv_callback
63 conv_cbs["write_conv"] = conv_callback
64 #conv_cbs["write_im"] = conv_callback
65 conv_cbs["chat_add_users"] = conv_callback
66 conv_cbs["chat_rename_user"] = conv_callback
67 conv_cbs["chat_remove_users"] = conv_callback
68 conv_cbs["chat_update_user"] = conv_callback
69 conv_cbs["present"] = conv_callback
70 conv_cbs["has_focus"] = conv_callback
71 conv_cbs["custom_smiley_add"] = conv_callback
72 conv_cbs["custom_smiley_write"] = conv_callback
73 conv_cbs["custom_smiley_close"] = conv_callback
74 conv_cbs["send_confirm"] = conv_callback
75
76 cbs["conversation"] = conv_cbs
77
78 def notify_callback(name):
79     print "----  notify callback example: %s" % name
80
81 notify_cbs["notify_message"] = notify_callback
82 notify_cbs["notify_email"] = notify_callback
83 notify_cbs["notify_emails"] = notify_callback
84 notify_cbs["notify_formatted"] = notify_callback
85 notify_cbs["notify_searchresults"] = notify_callback
86 notify_cbs["notify_searchresults_new_rows"] = notify_callback
87 notify_cbs["notify_userinfo"] = notify_callback
88 notify_cbs["notify_uri"] = notify_callback
89 notify_cbs["close_notify"] = notify_callback
90
91 cbs["notify"] = notify_cbs
92
93 def request_callback(name):
94     print "---- request callback example: %s" % name
95
96 request_cbs["request_input"] = request_callback
97 request_cbs["request_choice"] = request_callback
98 request_cbs["request_action"] = request_callback
99 request_cbs["request_fields"] = request_callback
100 request_cbs["request_file"] = request_callback
101 request_cbs["close_request"] = request_callback
102 request_cbs["request_folder"] = request_callback
103
104 cbs["request"] = request_cbs
105
106 class MainWindow:
107     def __init__(self, quit_cb):
108         global conv_cbs
109         global signal_cbs
110         self.bt_cbs = {}
111         self.new_acc_bt_cbs = {}
112         self.send_cbs = {}
113         self.quit_cb = quit_cb
114         conv_cbs["write_im"] = self._write_im_cb
115
116     def init_window(self):
117         # Main vbox
118         vbox = etk.VBox(homogeneous=False)
119
120         hbox_cmd = etk.HBox(homogeneous=False)
121         self.cmd_entry = etk.Entry()
122         self.lcmd = etk.Label(text="Type your message: ")
123         hbox_cmd.append(self.lcmd, etk.HBox.START, etk.HBox.START, 0)
124         hbox_cmd.append(self.cmd_entry, etk.HBox.START, etk.HBox.EXPAND_FILL, 0)
125
126         vbox_accs = etk.VBox()
127         self.accslistmodel = etk.ListModel()
128         self.accslist = etk.List(model=self.accslistmodel,\
129                 columns=[(10, etk.TextRenderer(slot=0),\
130                 False)], selectable=True,\
131                 animated_changes=True)
132         vbox_accs.append(self.accslist, etk.VBox.START, etk.VBox.EXPAND_FILL, 0)
133
134         hbox_buttons = etk.HBox(homogeneous=False)
135         send_bt = etk.Button(label="Send")
136         send_bt.on_clicked(self._send_bt_cb)
137         conn_bt = etk.Button(label="Connect")
138         conn_bt.on_clicked(self.login_window)
139         new_account_bt = etk.Button(label="New Account")
140         new_account_bt.on_clicked(self._new_account)
141         hbox_buttons.append(send_bt, etk.HBox.START, etk.HBox.NONE, 0)
142         hbox_buttons.append(conn_bt, etk.HBox.START, etk.HBox.NONE, 0)
143         hbox_buttons.append(new_account_bt, etk.HBox.START, etk.HBox.NONE, 0)
144
145         hbox_panel = etk.HBox()
146
147         vbox_buddies = etk.VBox()
148         self.blistmodel = etk.ListModel()
149         self.blist = etk.List(model=self.blistmodel,\
150                                      columns=[(10, etk.TextRenderer(slot=0), False)],\
151                                      selectable=True, animated_changes=True)
152         vbox_buddies.append(self.blist, etk.VBox.START, etk.VBox.EXPAND_FILL, 0)
153
154         vbox_txt_area = etk.VBox()
155         self.txt_area = etk.Label()
156         self.txt_area.text = "<br> "
157
158         vbox_txt_area.append(self.txt_area, etk.VBox.START, etk.VBox.EXPAND_FILL, 0)
159
160         hbox_panel.append(vbox_txt_area, etk.HBox.START, etk.HBox.EXPAND_FILL, 0)
161         hbox_panel.append(vbox_buddies, etk.HBox.END, etk.HBox.EXPAND_FILL, 0)
162         hbox_panel.append(vbox_accs, etk.HBox.END, etk.HBox.EXPAND_FILL, 0)
163
164         self.lstatus = etk.Label(text="Connection status")
165
166         vbox.append(hbox_panel, etk.VBox.START, etk.VBox.EXPAND_FILL, 0)
167         vbox.append(hbox_cmd, etk.VBox.END, etk.VBox.FILL, 0)
168         vbox.append(hbox_buttons, etk.VBox.END, etk.VBox.NONE, 5)
169         vbox.append(self.lstatus, etk.VBox.END, etk.VBox.FILL, 0)
170
171         self._window = etk.Window(title="NullClient-Etk", size_request=(600, 600), child=vbox)
172         self._window.on_destroyed(self.quit_cb)
173         self.set_global_callbacks()
174         self._window.show_all()
175
176     def login_window(self, pointer):
177         self.login_password = etk.Entry()
178         confirm_login_bt = etk.Button(label="Ok")
179         confirm_login_bt.on_clicked(self._conn_bt_cb)
180         vbox_login =  etk.VBox()
181         vbox_login.append(self.login_password, etk.VBox.START, etk.VBox.FILL, 0)
182         vbox_login.append(confirm_login_bt, etk.VBox.END, etk.VBox.NONE, 0)
183         self.login_win = etk.Window(title="Password", size_request=(190, 80),
184                 child=vbox_login)
185         self.login_win.show_all()
186
187     def set_global_callbacks(self):
188         global cbs
189         cbs["connection"]["connect_progress"] = self._purple_conn_status_cb
190         cbs["connection"]["disconnected"] = self._purple_disconnected_status_cb
191         cbs["connection"]["connected"] = self._purple_connected_cb
192
193     def _conn_bt_cb(self, pointer):
194         if self.bt_cbs.has_key("on_clicked"):
195             self.bt_cbs["on_clicked"](self.login_password.text)
196             self.login_win.destroy()
197
198     def _send_bt_cb(self, pointer):
199         bname = self.blist.selected_rows[0][0]
200         msg = self.cmd_entry.text
201         if bname and msg != "":
202             if self.send_cbs.has_key("on_clicked"):
203                 self.send_cbs["on_clicked"](bname, msg)
204         else:
205             print "Buddy not selected!"
206         self.cmd_entry.text = ""
207
208     def selected_accs(self):
209         try:
210             acc = self.accslist.selected_rows[0][0]
211             if acc:
212                 return acc
213             else:
214                 return "None"
215         except:
216             return "None"
217
218     def _new_account(self, pointer):
219         if self.new_acc_bt_cbs.has_key("on_clicked"):
220             self.new_acc_bt_cbs["on_clicked"]()
221
222     def _purple_conn_status_cb(self, txt, step, step_count):
223             self.lstatus.text = txt
224
225     def _purple_connected_cb(self):
226         self.lstatus.text = "Connected"
227
228     def new_buddy(self, b):
229             if [b] not in self.blistmodel.elements:
230                 self.blistmodel.append([b])
231
232     def remove_buddy(self, bname):
233         self.blistmodel.remove([bname])
234
235     def new_account(self, a):
236         if [a] not in self.accslistmodel.elements:
237             self.accslistmodel.append([a])
238
239     def _purple_disconnected_status_cb(self):
240         self.lstatus.text = "Disconnected"
241
242     def set_panel_text(self, txt):
243         self.txt_area = txt
244
245     def add_bt_conn_cb(self, cb):
246         if callable(cb):
247             self.bt_cbs["on_clicked"] = cb
248
249     def add_account_cb(self, cb):
250         if callable(cb):
251             self.new_acc_bt_cbs["on_clicked"] = cb
252
253     def add_send_cb(self, cb):
254         if callable(cb):
255             self.send_cbs["on_clicked"] = cb
256
257     def add_quit_cb(self, cb):
258         if callable(cb):
259             self.quit_cb = cb
260
261     def _write_im_cb(self, name, who, message):
262         if who:
263             w = who.split("/")[0]
264             self.txt_area.text +=  w + ": " + str(message) + "<br> "
265         else:
266             self.txt_area.text += str(name) + ": " + str(message) + "<br> "
267         self._window.show_all()
268
269
270 class NullClientPurple:
271     def __init__(self):
272         self.p = purple.Purple(debug_enabled=False)
273         self.window = MainWindow(self.quit)
274         self.buddies = {} #all buddies
275         self.conversations = {}
276         self.protocol_id = "prpl-jabber"
277         self.accs = None
278
279         global cbs
280         cbs["blist"]["update"] = self._purple_update_blist_cb
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()