Mercurial > libervia-backend
annotate frontends/wix/main_window.py @ 18:6928e3cb73a8
refactoring: using xml params part II
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 06 Nov 2009 23:31:00 +0100 |
parents | 37153f3a3dc1 |
children | bb72c29f3432 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 wix: a SAT frontend | |
6 Copyright (C) 2009 Jérôme Poisson (goffi@goffi.org) | |
7 | |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 | |
23 import wx | |
24 from chat import Chat | |
25 from param import Param | |
26 import gobject | |
27 import os.path | |
28 import pdb | |
29 from tools.jid import JID | |
30 from logging import debug, info, error | |
31 from quick_frontend.quick_chat_list import QuickChatList | |
32 from quick_frontend.quick_contact_list import QuickContactList | |
33 from quick_frontend.quick_app import QuickApp | |
34 | |
35 | |
36 msgOFFLINE = "offline" | |
37 msgONLINE = "online" | |
38 idCONNECT = 1 | |
1 | 39 idDISCONNECT = 2 |
40 idEXIT = 3 | |
41 idPARAM = 4 | |
42 idADD_CONTACT = 5 | |
43 idREMOVE_CONTACT = 6 | |
0 | 44 const_DEFAULT_GROUP = "Unclassed" |
45 const_STATUS = {"Online":"", | |
46 "Want to discuss":"chat", | |
47 "AFK":"away", | |
48 "Do Not Disturb":"dnd", | |
49 "Away":"xa"} | |
50 | |
51 class ChatList(QuickChatList): | |
52 """This class manage the list of chat windows""" | |
53 | |
54 def __init__(self, host): | |
55 QuickChatList.__init__(self, host) | |
56 | |
57 def createChat(self, name): | |
58 return Chat(name, self.host) | |
59 | |
60 | |
61 | |
62 class ContactList(wx.TreeCtrl, QuickContactList): | |
63 """Customized control to manage contacts.""" | |
64 | |
65 def __init__(self, parent): | |
66 wx.TreeCtrl.__init__(self, parent, style = wx.TR_HIDE_ROOT | wx.TR_HAS_BUTTONS) | |
67 QuickContactList.__init__(self) | |
68 self.jid_ids={} | |
69 self.groups={} | |
70 self.root=self.AddRoot("") | |
71 self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.onActivated, self) | |
72 | |
73 #icons | |
74 isz = (16,16) | |
75 il = wx.ImageList(isz[0], isz[1]) | |
76 self.icon_online = il.Add(wx.ArtProvider_GetBitmap(wx.ART_TICK_MARK, wx.ART_OTHER, isz)) | |
77 self.icon_unavailable = il.Add(wx.ArtProvider_GetBitmap(wx.ART_CROSS_MARK, wx.ART_OTHER, isz)) | |
78 self.AssignImageList(il) | |
79 | |
80 self.__addNode(const_DEFAULT_GROUP) | |
81 | |
82 def __addNode(self, label): | |
83 """Add an item container""" | |
84 ret=self.AppendItem(self.root, label) | |
85 self.SetPyData(ret, "[node]") | |
86 self.SetItemBold(ret) | |
87 self.groups[label]=ret | |
88 | |
89 def replace(self, jid, name="", show="", status="", group=""): | |
90 debug("status = %s show = %s",status, show) | |
91 if not self.jid_ids.has_key(jid): | |
92 self.add(jid, name, show, status, group) | |
93 else: | |
94 debug ("updating %s",jid) | |
95 self.__presentItem(jid, name, show, status, group) | |
96 | |
97 def __presentItem(self, jid, name, show, status, group): | |
98 """Make a nice presentation of the contact in the list.""" | |
99 id=self.jid_ids[jid] | |
100 label= "%s [%s] \n %s" % ((name or jid), (show or "online"), status) | |
101 self.SetItemText(id, label) | |
102 | |
103 # icon | |
104 if not show or show=="chat": | |
105 self.SetItemImage(id, self.icon_online) | |
106 else: | |
107 self.SetItemImage(id, self.icon_unavailable) | |
108 | |
109 #colour | |
110 if not show: | |
111 self.SetItemTextColour(id, wx.BLACK) | |
112 elif show=="chat": | |
113 self.SetItemTextColour(id, wx.GREEN) | |
114 elif show=="away": | |
115 self.SetItemTextColour(id, wx.BLUE) | |
116 else: | |
117 self.SetItemTextColour(id, wx.RED) | |
118 | |
119 def add(self, jid, name="", show="", status="", group=""): | |
120 """add a contact to the list""" | |
121 debug ("adding %s",jid) | |
122 dest_group=group or const_DEFAULT_GROUP | |
123 if not self.groups.has_key(dest_group): | |
124 self.__addNode(dest_group) | |
125 self.jid_ids[jid]=self.AppendItem(self.groups[dest_group], "") | |
126 self.__presentItem(jid, name, show, status, group) | |
127 self.SetPyData(self.jid_ids[jid], "[contact]"+jid) | |
128 self.EnsureVisible(self.jid_ids[jid]) | |
129 self.Refresh() #FIXME: Best way ? | |
130 | |
131 def remove(self, jid): | |
132 """remove a contact from the list""" | |
133 debug ("removing %s",jid) | |
134 self.Delete(self.jid_ids[jid]) | |
135 del self.jid_ids[jid] | |
136 self.Refresh() #FIXME: Best way ? | |
137 | |
138 def onActivated(self, event): | |
139 """Called when a contact is clicked or activated with keyboard.""" | |
140 if self.GetPyData(event.GetItem()).startswith("[contact]"): | |
141 self.onActivatedCB(self.GetPyData(event.GetItem())[9:]) | |
142 else: | |
143 event.Skip() | |
144 | |
145 def getSelection(self): | |
146 """Return the selected contact, or an empty string if there is not""" | |
147 data = self.GetPyData(self.GetSelection()) | |
148 if not data or not data.startswith("[contact]"): | |
149 return "" | |
150 return JID(data[9:]) | |
151 | |
152 def registerActivatedCB(self, cb): | |
153 """Register a callback with manage contact activation.""" | |
154 self.onActivatedCB=cb | |
155 | |
156 class MainWindow(wx.Frame, QuickApp): | |
157 """main app window""" | |
158 | |
159 def __init__(self): | |
160 wx.Frame.__init__(self,None, title="SAT Wix", size=(400,200)) | |
161 | |
162 | |
163 #Frame elements | |
164 self.contactList = ContactList(self) | |
165 self.contactList.registerActivatedCB(self.onContactActivated) | |
166 self.chat_wins=ChatList(self) | |
167 self.CreateStatusBar() | |
168 self.SetStatusText(msgOFFLINE) | |
169 self.createMenus() | |
170 | |
171 #ToolBar | |
172 self.tools=self.CreateToolBar() | |
173 self.statusBox = wx.ComboBox(self.tools, -1, "Online", choices=const_STATUS.keys(), | |
174 style=wx.CB_DROPDOWN | wx.CB_READONLY) | |
175 self.tools.AddControl(self.statusBox) | |
176 self.tools.AddSeparator() | |
177 self.statusTxt=wx.TextCtrl(self.tools, -1, style = wx.TE_PROCESS_ENTER) | |
178 self.tools.AddControl(self.statusTxt) | |
179 self.Bind(wx.EVT_COMBOBOX, self.onStatusChange, self.statusBox) | |
180 self.Bind(wx.EVT_TEXT_ENTER, self.onStatusChange, self.statusTxt) | |
181 self.tools.Disable() | |
11 | 182 |
183 #tray icon | |
184 ticon = wx.Icon("images/tray_icon.xpm", wx.BITMAP_TYPE_XPM) | |
185 self.tray_icon = wx.TaskBarIcon() | |
186 self.tray_icon.SetIcon(ticon, "Wix jabber client") | |
187 wx.EVT_TASKBAR_LEFT_UP(self.tray_icon, self.onTrayClick) | |
188 | |
0 | 189 |
190 #events | |
191 self.Bind(wx.EVT_CLOSE, self.onClose, self) | |
192 | |
193 QuickApp.__init__(self) | |
194 | |
195 self.Show() | |
196 | |
197 def createMenus(self): | |
198 info("Creating menus") | |
199 connectMenu = wx.Menu() | |
200 connectMenu.Append(idCONNECT, "&Connect CTRL-c"," Connect to the server") | |
1 | 201 connectMenu.Append(idDISCONNECT, "&Disconnect CTRL-d"," Disconnect from the server") |
0 | 202 connectMenu.Append(idPARAM,"&Parameters"," Configure the program") |
203 connectMenu.AppendSeparator() | |
204 connectMenu.Append(idEXIT,"E&xit"," Terminate the program") | |
205 contactMenu = wx.Menu() | |
206 contactMenu.Append(idADD_CONTACT, "&Add contact"," Add a contact to your list") | |
207 contactMenu.Append(idREMOVE_CONTACT, "&Remove contact"," Remove the selected contact from your list") | |
208 menuBar = wx.MenuBar() | |
209 menuBar.Append(connectMenu,"&General") | |
210 menuBar.Append(contactMenu,"&Contacts") | |
211 self.SetMenuBar(menuBar) | |
212 | |
213 #events | |
214 wx.EVT_MENU(self, idCONNECT, self.onConnectRequest) | |
1 | 215 wx.EVT_MENU(self, idDISCONNECT, self.onDisconnectRequest) |
0 | 216 wx.EVT_MENU(self, idPARAM, self.onParam) |
217 wx.EVT_MENU(self, idEXIT, self.onExit) | |
218 wx.EVT_MENU(self, idADD_CONTACT, self.onAddContact) | |
219 wx.EVT_MENU(self, idREMOVE_CONTACT, self.onRemoveContact) | |
220 | |
221 | |
222 def newMessage(self, from_jid, msg, type, to_jid): | |
223 QuickApp.newMessage(self, from_jid, msg, type, to_jid) | |
224 | |
225 def showAlert(self, message): | |
226 # TODO: place this in a separate class | |
227 popup=wx.PopupWindow(self) | |
228 ### following code come from wxpython demo | |
229 popup.SetBackgroundColour("CADET BLUE") | |
230 st = wx.StaticText(popup, -1, message, pos=(10,10)) | |
231 sz = st.GetBestSize() | |
232 popup.SetSize( (sz.width+20, sz.height+20) ) | |
233 x=(wx.DisplaySize()[0]-popup.GetSize()[0])/2 | |
234 popup.SetPosition((x,0)) | |
235 popup.Show() | |
236 wx.CallLater(5000,popup.Destroy) | |
237 | |
238 def showDialog(self, message, title="", type="info"): | |
239 if type == 'info': | |
240 flags = wx.OK | wx.ICON_INFORMATION | |
241 elif type == 'error': | |
242 flags = wx.OK | wx.ICON_ERROR | |
243 elif type == 'question': | |
244 flags = wx.OK | wx.ICON_QUESTION | |
245 else: | |
246 flags = wx.OK | wx.ICON_INFORMATION | |
247 dlg = wx.MessageDialog(self, message, title, flags) | |
248 answer = dlg.ShowModal() | |
249 dlg.Destroy() | |
250 return True if (answer == wx.ID_YES or answer == wx.ID_OK) else False | |
251 | |
252 def setStatusOnline(self, online=True): | |
253 """enable/disable controls, must be called when local user online status change""" | |
254 if online: | |
255 self.SetStatusText(msgONLINE) | |
256 self.tools.Enable() | |
257 else: | |
258 self.SetStatusText(msgOFFLINE) | |
259 self.tools.Disable() | |
260 return | |
261 | |
262 | |
263 def presenceUpdate(self, jabber_id, type, show, status, priority): | |
264 QuickApp.presenceUpdate(self, jabber_id, type, show, status, priority) | |
265 | |
266 def askConfirmation(self, type, id, data): | |
267 #TODO: refactor this in QuickApp | |
268 debug ("Confirmation asked") | |
269 answer_data={} | |
270 if type == "FILE_TRANSFERT": | |
271 debug ("File transfert confirmation asked") | |
272 dlg = wx.MessageDialog(self, "The contact %s wants to send you the file %s\nDo you accept ?" % (data["from"], data["filename"]), | |
273 'File Request', | |
274 wx.YES_NO | wx.ICON_QUESTION | |
275 ) | |
276 answer=dlg.ShowModal() | |
277 if answer==wx.ID_YES: | |
278 filename = wx.FileSelector("Where do you want to save the file ?", flags = wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) | |
279 if filename: | |
280 answer_data["dest_path"] = filename | |
281 self.bridge.confirmationAnswer(id, True, answer_data) | |
282 self.waitProgress(id, "File Transfer", "Copying %s" % os.path.basename(filename)) | |
283 else: | |
284 answer = wx.ID_NO | |
285 if answer==wx.ID_NO: | |
286 self.bridge.confirmationAnswer(id, False, answer_data) | |
287 | |
288 dlg.Destroy() | |
289 | |
290 | |
291 | |
292 | |
293 def progressCB(self, id, title, message): | |
294 data = self.bridge.getProgress(id) | |
295 if data: | |
296 if not data['position']: | |
297 data['position'] = '0' | |
298 if not self.pbar: | |
299 #first answer, we must construct the bar | |
300 self.pbar = wx.ProgressDialog(title, message, int(data['size']), None, | |
301 wx.PD_SMOOTH | wx.PD_ELAPSED_TIME | wx.PD_ESTIMATED_TIME | wx.PD_REMAINING_TIME) | |
302 self.pbar.finish_value = int(data['size']) | |
303 | |
304 self.pbar.Update(int(data['position'])) | |
305 elif self.pbar: | |
306 self.pbar.Update(self.pbar.finish_value) | |
307 return | |
308 | |
309 wx.CallLater(10, self.progressCB, id, title, message) | |
310 | |
311 def waitProgress (self, id, title, message): | |
312 self.pbar = None | |
313 wx.CallLater(10, self.progressCB, id, title, message) | |
314 | |
315 | |
316 | |
317 ### events ### | |
318 | |
319 def onContactActivated(self, jid): | |
320 debug ("onContactActivated: %s", jid) | |
321 if self.chat_wins[jid].IsShown(): | |
322 self.chat_wins[jid].Hide() | |
323 else: | |
324 self.chat_wins[jid].Show() | |
325 | |
326 def onConnectRequest(self, e): | |
327 self.bridge.connect() | |
328 | |
1 | 329 def onDisconnectRequest(self, e): |
330 self.bridge.disconnect() | |
331 | |
0 | 332 def __updateStatus(self): |
333 show = const_STATUS[self.statusBox.GetValue()] | |
334 status = self.statusTxt.GetValue() | |
335 self.bridge.setPresence(show=show, status=status) | |
336 | |
337 def onStatusChange(self, e): | |
338 debug("Status change request") | |
339 self.__updateStatus() | |
340 | |
341 def onParam(self, e): | |
342 debug("Param request") | |
18
6928e3cb73a8
refactoring: using xml params part II
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
343 param=Param(self.bridge) |
0 | 344 |
345 def onExit(self, e): | |
346 self.Close() | |
347 | |
348 def onAddContact(self, e): | |
349 debug("Add contact request") | |
350 dlg = wx.TextEntryDialog( | |
351 self, 'Please enter new contact JID', | |
352 'Adding a contact', 'name@server.ext') | |
353 | |
354 if dlg.ShowModal() == wx.ID_OK: | |
355 jid=JID(dlg.GetValue()) | |
356 if jid.is_valid(): | |
357 self.bridge.addContact(jid.short) | |
358 else: | |
359 error ("'%s' is an invalid JID !", jid) | |
360 #TODO: notice the user | |
361 | |
362 dlg.Destroy() | |
363 | |
364 def onRemoveContact(self, e): | |
365 debug("Remove contact request") | |
366 target = self.contactList.getSelection() | |
367 if not target: | |
368 dlg = wx.MessageDialog(self, "You haven't selected any contact !", | |
369 'Error', | |
370 wx.OK | wx.ICON_ERROR | |
371 ) | |
372 dlg.ShowModal() | |
373 dlg.Destroy() | |
374 return | |
375 | |
376 dlg = wx.MessageDialog(self, "Are you sure you want to delete %s from your roster list ?" % target.short, | |
377 'Contact suppression', | |
378 wx.YES_NO | wx.ICON_QUESTION | |
379 ) | |
380 | |
381 if dlg.ShowModal() == wx.ID_YES: | |
382 info("Unsubsribing %s presence", target.short) | |
383 self.bridge.delContact(target.short) | |
384 | |
385 dlg.Destroy() | |
386 | |
387 def onClose(self, e): | |
388 info("Exiting...") | |
389 e.Skip() | |
11 | 390 |
391 def onTrayClick(self, e): | |
392 debug("Tray Click") | |
393 if self.IsShown(): | |
394 self.Hide() | |
395 else: | |
396 self.Show() | |
397 self.Raise() | |
398 e.Skip() | |
0 | 399 |