d2df7ff75fab11b39f10b1c20821fe53c758dbc4
[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.send_cbs = {}
112         self.quit_cb = quit_cb
113         conv_cbs["write_im"] = self._write_im_cb
114
115     def init_window(self):
116         # Main vbox
117         vbox = etk.VBox(homogeneous=False)
118
119         hbox_cmd = etk.HBox(homogeneous=False)
120         self.cmd_entry = etk.Entry()
121         self.lcmd = etk.Label(text="Type your message: ")
122         hbox_cmd.append(self.lcmd, etk.HBox.START, etk.HBox.START, 0)
123         hbox_cmd.append(self.cmd_entry, etk.HBox.START, etk.HBox.EXPAND_FILL, 0)
124
125         hbox_buttons = etk.HBox(homogeneous=False)
126         send_bt = etk.Button(label="Send")
127         send_bt.on_clicked(self._send_bt_cb)
128         conn_bt = etk.Button(label="Connect")
129         conn_bt.on_clicked(self._conn_bt_cb)
130         hbox_buttons.append(send_bt, etk.HBox.START, etk.HBox.NONE, 0)
131         hbox_buttons.append(conn_bt, etk.HBox.START, etk.HBox.NONE, 0)
132
133         hbox_panel = etk.HBox()
134
135         vbox_buddies = etk.VBox()
136         self.blistmodel = etk.ListModel()
137         self.blist = etk.List(model=self.blistmodel,\
138                                      columns=[(10, etk.TextRenderer(slot=0), False)],\
139                                      selectable=True, animated_changes=True)
140         vbox_buddies.append(self.blist, etk.VBox.START, etk.VBox.EXPAND_FILL, 0)
141
142         vbox_txt_area = etk.VBox()
143         self.txt_area = etk.Label()
144         self.txt_area.text = "<br> "
145
146         vbox_txt_area.append(self.txt_area, etk.VBox.START, etk.VBox.EXPAND_FILL, 0)
147
148         hbox_panel.append(vbox_txt_area, etk.HBox.START, etk.HBox.EXPAND_FILL, 0)
149         hbox_panel.append(vbox_buddies, etk.HBox.END, etk.HBox.EXPAND_FILL, 0)
150
151         self.lstatus = etk.Label(text="Connection status")
152
153         vbox.append(hbox_panel, etk.VBox.START, etk.VBox.EXPAND_FILL, 0)
154         vbox.append(hbox_cmd, etk.VBox.END, etk.VBox.FILL, 0)
155         vbox.append(hbox_buttons, etk.VBox.END, etk.VBox.NONE, 5)
156         vbox.append(self.lstatus, etk.VBox.END, etk.VBox.FILL, 0)
157
158         self._window = etk.Window(title="NullClient-Etk", size_request=(600, 600), child=vbox)
159         self._window.on_destroyed(self.quit_cb)
160         self.set_global_callbacks()
161         self._window.show_all()
162
163     def set_global_callbacks(self):
164         global cbs
165         cbs["connection"]["connect_progress"] = self._purple_conn_status_cb
166         cbs["connection"]["disconnected"] = self._purple_disconnected_status_cb
167         cbs["connection"]["connected"] = self._purple_connected_cb
168
169     def _conn_bt_cb(self, pointer):
170         if self.bt_cbs.has_key("on_clicked"):
171             self.bt_cbs["on_clicked"]()
172
173     def _send_bt_cb(self, pointer):
174         bname = self.blist.selected_rows[0][0]
175         msg = self.cmd_entry.text
176         if bname and msg != "":
177             if self.send_cbs.has_key("on_clicked"):
178                 self.send_cbs["on_clicked"](bname, msg)
179         else:
180             print "Buddy not selected!"
181         self.cmd_entry.text = ""
182
183     def _purple_conn_status_cb(self, txt, step, step_count):
184             self.lstatus.text = txt
185
186     def _purple_connected_cb(self):
187         self.lstatus.text = "Connected"
188
189     def new_buddy(self, b):
190             if [b] not in self.blistmodel.elements:
191                 self.blistmodel.append([b])
192
193     def remove_buddy(self, bname):
194         self.blistmodel.remove([bname])
195
196     def _purple_disconnected_status_cb(self):
197         self.lstatus.text = "Disconnected"
198
199     def set_panel_text(self, txt):
200         self.txt_area = txt
201
202     def add_bt_conn_cb(self, cb):
203         if callable(cb):
204             self.bt_cbs["on_clicked"] = cb
205
206     def add_send_cb(self, cb):
207         if callable(cb):
208             self.send_cbs["on_clicked"] = cb
209
210     def add_quit_cb(self, cb):
211         if callable(cb):
212             self.quit_cb = cb
213
214     def _write_im_cb(self, name, who, message):
215         if who:
216             w = who.split("/")[0]
217             self.txt_area.text +=  w + ": " + str(message) + "<br> "
218         else:
219             self.txt_area.text += str(name) + ": " + str(message) + "<br> "
220         self._window.show_all()
221
222
223 class NullClientPurple:
224     def __init__(self):
225         self.p = purple.Purple(debug_enabled=False)
226         self.window = MainWindow(self.quit)
227         self.buddies = {} #all buddies
228         self.conversations = {}
229         self.account = None
230         self.protocol_id = "prpl-jabber"
231         self.username = "carmanplugintest@gmail.com"
232         self.password = "abc123def"
233
234         global cbs
235         cbs["blist"]["update"] = self._purple_update_blist_cb
236         self.p.purple_init(cbs)
237
238         #Initializing UI
239         self.window.add_bt_conn_cb(self.connect)
240         self.window.add_send_cb(self.send_msg)
241         self.window.init_window()
242
243     def _purple_update_blist_cb(self, type, name=None, alias=None, totalsize=None,\
244                              currentsize=None, online=None):
245         if type == 2:
246             if not self.buddies.has_key(name):
247                 b = purple.Buddy()
248                 b.new_buddy(self.account, name, alias)
249                 self.buddies[name] = b
250             elif self.buddies[name].online is True:
251                 self.window.new_buddy(name)
252
253     def _purple_signal_sign_off_cb(self, name, bname):
254         if self.buddies.has_key(bname):
255             self.buddies[bname] = None
256             self.buddies.pop(bname)
257             print "[DEBUG]: Buddy removed!"
258         self.window.remove_buddy(bname)
259
260     def _purple_create_conv_cb(self, name, type):
261         bname = name.split("/")[0]
262         if bname in self.buddies and not self.conversations.has_key(name):
263             conv = purple.Conversation()
264             conv.initialize(self.account, "IM", bname)
265             self.conversations[bname] = conv
266
267     def connect(self):
268         self.account = purple.Account(self.username, self.protocol_id)
269         self.account.password = self.password
270
271         self.account.proxy.set_type(purple.ProxyInfoType().HTTP)
272         self.account.proxy.set_host("172.18.216.211")
273         self.account.proxy.set_port(8080)
274
275         self.account.get_protocol_options()
276
277         self.account.set_enabled("carman-purple-python", True)
278         self.p.connect()
279         self.p.signal_connect("buddy-signed-off", self._purple_signal_sign_off_cb)
280
281     def send_msg(self, name, msg):
282         if not self.conversations.has_key(name):
283             conv = purple.Conversation()
284             conv.initialize(self.account, "IM", name)
285             self.conversations[name] = conv
286         self.conversations[name].write(msg)
287
288     def quit(self, o):
289         print "[DEBUG]: quitting"
290         for i in self.conversations:
291             self.conversations[i].destroy()
292             self.conversations[i] = None
293         self.conversations = None
294         self.p.destroy()
295         ecore.main_loop_quit()
296
297 if __name__ == '__main__':
298
299     nullpurple = NullClientPurple()
300     ecore.main_loop_begin()