Added local ez_setup.py.
[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 signal_cbs = {}
13
14 def account_callback(name):
15     print "---- account callback example: %s" % name
16
17 acc_cbs["notify_added"] = account_callback
18 acc_cbs["status_changed"] = account_callback
19 acc_cbs["request_add"] = account_callback
20 acc_cbs["request_authorize"] = account_callback
21 acc_cbs["close_account_request"] = account_callback
22
23 cbs["account"] = acc_cbs
24
25 def blist_callback(name):
26     print "---- blist callback example: %s" % name
27
28 #blist_cbs["new_list"] = blist_callback
29 #blist_cbs["new_node"] = blist_callback
30 #blist_cbs["show"] = blist_callback
31 #blist_cbs["update"] = blist_callback
32 #blist_cbs["remove"] = blist_callback
33 #blist_cbs["destroy"] = blist_callback
34 blist_cbs["set_visible"] = blist_callback
35 blist_cbs["request_add_buddy"] = blist_callback
36 blist_cbs["request_add_chat"] = blist_callback
37 blist_cbs["request_add_group"] = blist_callback
38
39 cbs["blist"] = blist_cbs
40
41 def conn_callback(name):
42     print "---- connection callback example: %s" % name
43
44 def conn_progress_cb(data):
45     return "Connection in progress..."
46
47 #conn_cbs["connect_progress"] = conn_progress_cb
48 #conn_cbs["connected"] = conn_callback
49 #conn_cbs["disconnected"] = conn_callback
50 conn_cbs["notice"] = conn_callback
51 conn_cbs["report_disconnect"] = conn_callback
52 conn_cbs["network_connected"] = conn_callback
53 conn_cbs["network_disconnected"] = conn_callback
54 conn_cbs["report_disconnect_reason"] = conn_callback
55
56 cbs["connection"] = conn_cbs
57
58 def conv_callback(name):
59     print "---- conversation callback example: %s" % name
60
61 #conv_cbs["create_conversation"] = conv_callback
62 #conv_cbs["destroy_conversation"] = conv_callback
63 conv_cbs["write_chat"] = conv_callback
64 conv_cbs["write_conv"] = conv_callback
65 #conv_cbs["write_im"] = conv_callback
66 conv_cbs["chat_add_users"] = conv_callback
67 conv_cbs["chat_rename_user"] = conv_callback
68 conv_cbs["chat_remove_users"] = conv_callback
69 conv_cbs["chat_update_user"] = conv_callback
70 conv_cbs["present"] = conv_callback
71 conv_cbs["has_focus"] = conv_callback
72 conv_cbs["custom_smiley_add"] = conv_callback
73 conv_cbs["custom_smiley_write"] = conv_callback
74 conv_cbs["custom_smiley_close"] = conv_callback
75 conv_cbs["send_confirm"] = conv_callback
76
77 cbs["conversation"] = conv_cbs
78
79 def notify_callback(name):
80     print "----  notify callback example: %s" % name
81
82 notify_cbs["notify_message"] = notify_callback
83 notify_cbs["notify_email"] = notify_callback
84 notify_cbs["notify_emails"] = notify_callback
85 notify_cbs["notify_formatted"] = notify_callback
86 notify_cbs["notify_searchresults"] = notify_callback
87 notify_cbs["notify_searchresults_new_rows"] = notify_callback
88 notify_cbs["notify_userinfo"] = notify_callback
89 notify_cbs["notify_uri"] = notify_callback
90 notify_cbs["close_notify"] = notify_callback
91
92 cbs["notify"] = notify_cbs
93
94 def request_callback(name):
95     print "---- request callback example: %s" % name
96
97 request_cbs["request_input"] = request_callback
98 request_cbs["request_choice"] = request_callback
99 request_cbs["request_action"] = request_callback
100 request_cbs["request_fields"] = request_callback
101 request_cbs["request_file"] = request_callback
102 request_cbs["close_request"] = request_callback
103 request_cbs["request_folder"] = request_callback
104
105 cbs["request"] = request_cbs
106
107 def buddy_signed_off_cb(name, bname):
108     print "---- sign off from buddy %s" % bname
109
110 def receiving_im_msg_cb(sender, name, message):
111     print "---- receiving IM message from %s: %s" % (name, message)
112     return False
113
114 #signal_cbs["buddy_signed_off"] = buddy_signed_off_cb
115 signal_cbs["receiving_im_msg"] = receiving_im_msg_cb
116
117
118 class MainWindow:
119     def __init__(self, quit_cb):
120         global conv_cbs
121         global signal_cbs
122         self.bt_cbs = {}
123         self.send_cbs = {}
124         self.quit_cb = quit_cb
125         conv_cbs["write_im"] = self._write_im_cb
126
127     def init_window(self):
128         # Main vbox
129         vbox = etk.VBox(homogeneous=False)
130
131         hbox_cmd = etk.HBox(homogeneous=False)
132         self.cmd_entry = etk.Entry()
133         self.lcmd = etk.Label(text="Type your message: ")
134         hbox_cmd.append(self.lcmd, etk.HBox.START, etk.HBox.START, 0)
135         hbox_cmd.append(self.cmd_entry, etk.HBox.START, etk.HBox.EXPAND_FILL, 0)
136
137         hbox_buttons = etk.HBox(homogeneous=False)
138         send_bt = etk.Button(label="Send")
139         send_bt.on_clicked(self._send_bt_cb)
140         conn_bt = etk.Button(label="Connect")
141         conn_bt.on_clicked(self._conn_bt_cb)
142         hbox_buttons.append(send_bt, etk.HBox.START, etk.HBox.NONE, 0)
143         hbox_buttons.append(conn_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
163         self.lstatus = etk.Label(text="Connection status")
164
165         vbox.append(hbox_panel, etk.VBox.START, etk.VBox.EXPAND_FILL, 0)
166         vbox.append(hbox_cmd, etk.VBox.END, etk.VBox.FILL, 0)
167         vbox.append(hbox_buttons, etk.VBox.END, etk.VBox.NONE, 5)
168         vbox.append(self.lstatus, etk.VBox.END, etk.VBox.FILL, 0)
169
170         self._window = etk.Window(title="NullClient-Etk", size_request=(600, 600), child=vbox)
171         self._window.on_destroyed(self.quit_cb)
172         self.set_global_callbacks()
173         self._window.show_all()
174
175     def set_global_callbacks(self):
176         global cbs
177         cbs["connection"]["connect_progress"] = self._purple_conn_status_cb
178         cbs["connection"]["disconnected"] = self._purple_disconnected_status_cb
179         cbs["connection"]["connected"] = self._purple_connected_cb
180
181     def _conn_bt_cb(self, pointer):
182         if self.bt_cbs.has_key("on_clicked"):
183             self.bt_cbs["on_clicked"]()
184
185     def _send_bt_cb(self, pointer):
186         bname = self.blist.selected_rows[0][0]
187         msg = self.cmd_entry.text
188         if bname and msg != "":
189             if self.send_cbs.has_key("on_clicked"):
190                 self.send_cbs["on_clicked"](bname, msg)
191         else:
192             print "Buddy not selected!"
193         self.cmd_entry.text = ""
194
195     def _purple_conn_status_cb(self, txt, step, step_count):
196             self.lstatus.text = txt
197
198     def _purple_connected_cb(self):
199         self.lstatus.text = "Connected"
200
201     def new_buddy(self, b):
202             if [b] not in self.blistmodel.elements:
203                 self.blistmodel.append([b])
204
205     def remove_buddy(self, bname):
206         self.blistmodel.remove([bname])
207
208     def _purple_disconnected_status_cb(self):
209         self.lstatus.text = "Disconnected"
210
211     def set_panel_text(self, txt):
212         self.txt_area = txt
213
214     def add_bt_conn_cb(self, cb):
215         if callable(cb):
216             self.bt_cbs["on_clicked"] = cb
217
218     def add_send_cb(self, cb):
219         if callable(cb):
220             self.send_cbs["on_clicked"] = cb
221
222     def add_quit_cb(self, cb):
223         if callable(cb):
224             self.quit_cb = cb
225
226     def _write_im_cb(self, name, message):
227         self.txt_area.text +=  str(name) + ": " + str(message) + "<br> "
228         self._window.show_all()
229
230
231 class NullClientPurple:
232     def __init__(self):
233         self.p = purple.Purple(debug_enabled=False)
234         self.window = MainWindow(self.quit)
235         self.buddies = [] #online buddies
236         self.conversations = {}
237         self.account = None
238         self.protocol_id = "prpl-jabber"
239         self.username = "carmanplugintest@gmail.com"
240         self.password = "abc123def"
241
242
243         global cbs
244         global signal_cbs
245         cbs["blist"]["update"] = self._purple_update_blist_cb
246         signal_cbs["buddy_signed_off"] = self._purple_signal_sign_off_cb
247         cbs["conversation"]["create_conversation"] = self._purple_create_conv_cb
248         self.p.purple_init(cbs)
249
250         #Initializing UI
251         self.window.add_bt_conn_cb(self.connect)
252         self.window.add_send_cb(self.send_msg)
253         self.window.init_window()
254
255     def _purple_update_blist_cb(self, type, name=None, totalsize=None,\
256                              currentsize=None, online=None):
257         self.buddies = self.account.get_buddies_online()
258         if type == 2:
259             if name in self.buddies:
260                 self.buddies.append(name)
261                 self.window.new_buddy(name)
262
263     def _purple_signal_sign_off_cb(self, name, bname):
264         self.buddies.remove(bname)
265         self.window.remove_buddy(bname)
266
267     def _purple_create_conv_cb(self, name, type):
268         bname = name.split("/")[0]
269         if bname in self.buddies and not self.conversations.has_key(name):
270             conv = purple.Conversation()
271             conv.initialize(self.account, "IM", bname)
272             self.conversations[bname] = conv
273
274     def connect(self):
275         self.account = purple.Account(self.username, self.protocol_id)
276         self.account.set_password(self.password)
277
278         self.account.proxy.set_type(purple.ProxyInfoType().HTTP)
279         self.account.proxy.set_host("172.18.216.211")
280         self.account.proxy.set_port(8080)
281
282         self.account.get_protocol_options()
283
284         self.account.set_enabled("carman-purple-python", True)
285         self.p.connect()
286         self.p.attach_signals(signal_cbs)
287
288     def send_msg(self, name, msg):
289         if not self.conversations.has_key(name):
290             conv = purple.Conversation()
291             conv.initialize(self.account, "IM", name)
292             self.conversations[name] = conv
293         self.conversations[name].write(msg)
294
295     def quit(self, o):
296         print "[DEBUG]: quitting"
297         for i in self.conversations:
298             self.conversations[i].destroy()
299             self.conversations[i] = None
300         self.conversations = None
301         self.p.destroy()
302         ecore.main_loop_quit()
303
304 if __name__ == '__main__':
305
306     nullpurple = NullClientPurple()
307     ecore.main_loop_begin()