g_timeout_add_seconds() deleted from eventloop_ui struct
[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["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 def buddy_signed_off_cb(name, bname):
107     print "---- sign off from buddy %s" % bname
108
109 def receiving_im_msg_cb(sender, name, message):
110     print "---- receiving IM message from %s: %s" % (name, message)
111     return False
112
113 #signal_cbs["buddy_signed_off"] = buddy_signed_off_cb
114 signal_cbs["receiving_im_msg"] = receiving_im_msg_cb
115
116
117 class MainWindow:
118     def __init__(self, quit_cb):
119         global conv_cbs
120         self.bt_cbs = {}
121         self.quit_cb = quit_cb
122         conv_cbs["write_im"] = self._write_im_cb
123
124     def init_window(self):
125         # Main vbox
126         vbox = etk.VBox(homogeneous=False)
127
128         hbox_cmd = etk.HBox(homogeneous=False)
129         self.cmd_entry = etk.Entry()
130         lcmd = etk.Label(text="Type your message: ")
131         hbox_cmd.append(lcmd, etk.HBox.START, etk.HBox.START, 0)
132         hbox_cmd.append(self.cmd_entry, etk.HBox.START, etk.HBox.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._conn_bt_cb)
139         hbox_buttons.append(send_bt, etk.HBox.START, etk.HBox.NONE, 0)
140         hbox_buttons.append(conn_bt, etk.HBox.START, etk.HBox.NONE, 0)
141
142         hbox_panel = etk.HBox()
143
144         vbox_buddies = etk.VBox()
145         self.blistmodel = etk.ListModel()
146         self.blist = etk.List(model=self.blistmodel,\
147                                      columns=[(10, etk.TextRenderer(slot=0), False)],\
148                                      selectable=True, animated_changes=True)
149         vbox_buddies.append(self.blist, etk.VBox.START, etk.VBox.EXPAND_FILL, 0)
150
151         vbox_txt_area = etk.VBox()
152         self.txt_area = etk.Label()
153         self.txt_area.text = "<br> "
154
155         vbox_txt_area.append(self.txt_area, etk.VBox.START, etk.VBox.EXPAND_FILL, 0)
156
157         hbox_panel.append(vbox_txt_area, etk.HBox.START, etk.HBox.EXPAND_FILL, 0)
158         hbox_panel.append(vbox_buddies, etk.HBox.END, etk.HBox.EXPAND_FILL, 0)
159
160         self.lstatus = etk.Label(text="Connection status")
161
162         vbox.append(hbox_panel, etk.VBox.START, etk.VBox.EXPAND_FILL, 0)
163         vbox.append(hbox_cmd, etk.VBox.END, etk.VBox.FILL, 0)
164         vbox.append(hbox_buttons, etk.VBox.END, etk.VBox.NONE, 5)
165         vbox.append(self.lstatus, etk.VBox.END, etk.VBox.FILL, 0)
166
167         self._window = etk.Window(title="NullClient-Etk", size_request=(600, 600), child=vbox)
168         self._window.on_destroyed(self.quit_cb)
169         self.set_global_callbacks()
170         self._window.show_all()
171
172     def set_global_callbacks(self):
173         global cbs
174         cbs["connection"]["connect_progress"] = self._purple_conn_status_cb
175         cbs["connection"]["disconnected"] = self._purple_disconnected_status_cb
176         cbs["connection"]["connected"] = self._purple_connected_cb
177
178     def _conn_bt_cb(self, pointer):
179         if self.bt_cbs.has_key("on_clicked"):
180             self.bt_cbs["on_clicked"]()
181
182     def _send_bt_cb(self, pointer):
183         bname = self.blist.selected_rows[0][0]
184         if bname:
185             print "ITEM: %s" % bname
186
187     def _purple_conn_status_cb(self, txt, step, step_count):
188             self.lstatus.text = txt
189
190     def _purple_connected_cb(self):
191         self.lstatus.text = "Connected"
192
193     def new_buddy(self, b):
194             if [b] not in self.blistmodel.elements:
195                 self.blistmodel.append([b])
196
197     def remove_buddy(self, bname):
198         self.blistmodel.remove([bname])
199
200     def _purple_disconnected_status_cb(self, pointer):
201         self.lstatus.text = "Disconnected"
202
203     def set_panel_text(self, txt):
204         self.txt_area = txt
205
206     def add_bt_conn_cb(self, cb):
207         if callable(cb):
208             self.bt_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, message):
215         self.txt_area.text +=  str(name) + ": " + str(message) + "<br> "
216         self._window.show_all()
217
218
219 class NullClientPurple:
220     def __init__(self):
221         self.p = purple.Purple(debug_enabled=False)
222         self.window = MainWindow(self.quit)
223         self.buddies = [] #online buddies
224         self.account = None
225         self.protocol = None
226         self.username = "carmanplugintest@gmail.com"
227         self.password = "abc123def"
228
229
230         global cbs
231         global signal_cbs
232         cbs["blist"]["update"] = self._purple_update_blist_cb
233         signal_cbs["buddy_signed_off"] = self._purple_signal_sign_off_cb
234         self.p.purple_init(cbs)
235
236         #Initializing UI
237         self.window.add_bt_conn_cb(self.connect)
238         self.window.init_window()
239
240     def _purple_update_blist_cb(self, type, name=None, totalsize=None,\
241                              currentsize=None, online=None):
242         self.buddies = self.account.get_buddies_online()
243         if type == 2:
244             if name in self.buddies:
245                 self.buddies.append(name)
246                 self.window.new_buddy(name)
247
248     def _purple_signal_sign_off_cb(self, name, bname):
249         self.buddies.remove(bname)
250         self.window.remove_buddy(bname)
251
252     def set_protocol(self, protocol):
253         for p in self.p.get_protocols():
254             if p.get_name() == protocol:
255                 self.protocol = p
256                 return
257
258     def connect(self):
259         self.set_protocol("XMPP")
260         self.account = purple.Account(self.username, self.protocol.get_id())
261         self.account.set_password(self.password)
262
263         self.account.proxy.set_type(purple.ProxyInfoType().HTTP)
264         self.account.proxy.set_host("172.18.216.211")
265         self.account.proxy.set_port(8080)
266
267         self.account.get_protocol_options()
268
269         self.account.set_enabled("carman-purple-python", True)
270         self.p.connect()
271         self.p.attach_signals(signal_cbs)
272
273     def quit(self, o):
274         print "quitting"
275         self.p = None
276         ecore.main_loop_quit()
277
278 if __name__ == '__main__':
279
280     nullpurple = NullClientPurple()
281     ecore.main_loop_begin()