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() |
|
182 |
|
183 #events |
|
184 self.Bind(wx.EVT_CLOSE, self.onClose, self) |
|
185 |
|
186 QuickApp.__init__(self) |
|
187 |
|
188 self.Show() |
|
189 |
|
190 def createMenus(self): |
|
191 info("Creating menus") |
|
192 connectMenu = wx.Menu() |
|
193 connectMenu.Append(idCONNECT, "&Connect CTRL-c"," Connect to the server") |
1
|
194 connectMenu.Append(idDISCONNECT, "&Disconnect CTRL-d"," Disconnect from the server") |
0
|
195 connectMenu.Append(idPARAM,"&Parameters"," Configure the program") |
|
196 connectMenu.AppendSeparator() |
|
197 connectMenu.Append(idEXIT,"E&xit"," Terminate the program") |
|
198 contactMenu = wx.Menu() |
|
199 contactMenu.Append(idADD_CONTACT, "&Add contact"," Add a contact to your list") |
|
200 contactMenu.Append(idREMOVE_CONTACT, "&Remove contact"," Remove the selected contact from your list") |
|
201 menuBar = wx.MenuBar() |
|
202 menuBar.Append(connectMenu,"&General") |
|
203 menuBar.Append(contactMenu,"&Contacts") |
|
204 self.SetMenuBar(menuBar) |
|
205 |
|
206 #events |
|
207 wx.EVT_MENU(self, idCONNECT, self.onConnectRequest) |
1
|
208 wx.EVT_MENU(self, idDISCONNECT, self.onDisconnectRequest) |
0
|
209 wx.EVT_MENU(self, idPARAM, self.onParam) |
|
210 wx.EVT_MENU(self, idEXIT, self.onExit) |
|
211 wx.EVT_MENU(self, idADD_CONTACT, self.onAddContact) |
|
212 wx.EVT_MENU(self, idREMOVE_CONTACT, self.onRemoveContact) |
|
213 |
|
214 |
|
215 def newMessage(self, from_jid, msg, type, to_jid): |
|
216 QuickApp.newMessage(self, from_jid, msg, type, to_jid) |
|
217 |
|
218 def showAlert(self, message): |
|
219 # TODO: place this in a separate class |
|
220 popup=wx.PopupWindow(self) |
|
221 ### following code come from wxpython demo |
|
222 popup.SetBackgroundColour("CADET BLUE") |
|
223 st = wx.StaticText(popup, -1, message, pos=(10,10)) |
|
224 sz = st.GetBestSize() |
|
225 popup.SetSize( (sz.width+20, sz.height+20) ) |
|
226 x=(wx.DisplaySize()[0]-popup.GetSize()[0])/2 |
|
227 popup.SetPosition((x,0)) |
|
228 popup.Show() |
|
229 wx.CallLater(5000,popup.Destroy) |
|
230 |
|
231 def showDialog(self, message, title="", type="info"): |
|
232 if type == 'info': |
|
233 flags = wx.OK | wx.ICON_INFORMATION |
|
234 elif type == 'error': |
|
235 flags = wx.OK | wx.ICON_ERROR |
|
236 elif type == 'question': |
|
237 flags = wx.OK | wx.ICON_QUESTION |
|
238 else: |
|
239 flags = wx.OK | wx.ICON_INFORMATION |
|
240 dlg = wx.MessageDialog(self, message, title, flags) |
|
241 answer = dlg.ShowModal() |
|
242 dlg.Destroy() |
|
243 return True if (answer == wx.ID_YES or answer == wx.ID_OK) else False |
|
244 |
|
245 def setStatusOnline(self, online=True): |
|
246 """enable/disable controls, must be called when local user online status change""" |
|
247 if online: |
|
248 self.SetStatusText(msgONLINE) |
|
249 self.tools.Enable() |
|
250 else: |
|
251 self.SetStatusText(msgOFFLINE) |
|
252 self.tools.Disable() |
|
253 return |
|
254 |
|
255 |
|
256 def presenceUpdate(self, jabber_id, type, show, status, priority): |
|
257 QuickApp.presenceUpdate(self, jabber_id, type, show, status, priority) |
|
258 |
|
259 def askConfirmation(self, type, id, data): |
|
260 #TODO: refactor this in QuickApp |
|
261 debug ("Confirmation asked") |
|
262 answer_data={} |
|
263 if type == "FILE_TRANSFERT": |
|
264 debug ("File transfert confirmation asked") |
|
265 dlg = wx.MessageDialog(self, "The contact %s wants to send you the file %s\nDo you accept ?" % (data["from"], data["filename"]), |
|
266 'File Request', |
|
267 wx.YES_NO | wx.ICON_QUESTION |
|
268 ) |
|
269 answer=dlg.ShowModal() |
|
270 if answer==wx.ID_YES: |
|
271 filename = wx.FileSelector("Where do you want to save the file ?", flags = wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) |
|
272 if filename: |
|
273 answer_data["dest_path"] = filename |
|
274 self.bridge.confirmationAnswer(id, True, answer_data) |
|
275 self.waitProgress(id, "File Transfer", "Copying %s" % os.path.basename(filename)) |
|
276 else: |
|
277 answer = wx.ID_NO |
|
278 if answer==wx.ID_NO: |
|
279 self.bridge.confirmationAnswer(id, False, answer_data) |
|
280 |
|
281 dlg.Destroy() |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 def progressCB(self, id, title, message): |
|
287 data = self.bridge.getProgress(id) |
|
288 if data: |
|
289 if not data['position']: |
|
290 data['position'] = '0' |
|
291 if not self.pbar: |
|
292 #first answer, we must construct the bar |
|
293 self.pbar = wx.ProgressDialog(title, message, int(data['size']), None, |
|
294 wx.PD_SMOOTH | wx.PD_ELAPSED_TIME | wx.PD_ESTIMATED_TIME | wx.PD_REMAINING_TIME) |
|
295 self.pbar.finish_value = int(data['size']) |
|
296 |
|
297 self.pbar.Update(int(data['position'])) |
|
298 elif self.pbar: |
|
299 self.pbar.Update(self.pbar.finish_value) |
|
300 return |
|
301 |
|
302 wx.CallLater(10, self.progressCB, id, title, message) |
|
303 |
|
304 def waitProgress (self, id, title, message): |
|
305 self.pbar = None |
|
306 wx.CallLater(10, self.progressCB, id, title, message) |
|
307 |
|
308 |
|
309 |
|
310 ### events ### |
|
311 |
|
312 def onContactActivated(self, jid): |
|
313 debug ("onContactActivated: %s", jid) |
|
314 if self.chat_wins[jid].IsShown(): |
|
315 self.chat_wins[jid].Hide() |
|
316 else: |
|
317 self.chat_wins[jid].Show() |
|
318 |
|
319 def onConnectRequest(self, e): |
|
320 self.bridge.connect() |
|
321 |
1
|
322 def onDisconnectRequest(self, e): |
|
323 self.bridge.disconnect() |
|
324 |
0
|
325 def __updateStatus(self): |
|
326 show = const_STATUS[self.statusBox.GetValue()] |
|
327 status = self.statusTxt.GetValue() |
|
328 self.bridge.setPresence(show=show, status=status) |
|
329 |
|
330 def onStatusChange(self, e): |
|
331 debug("Status change request") |
|
332 self.__updateStatus() |
|
333 |
|
334 def onParam(self, e): |
|
335 debug("Param request") |
|
336 param=Param(self.bridge.setParam, self.bridge.getParam, self.bridge.getParams, self.bridge.getParamsCategories) |
|
337 |
|
338 def onExit(self, e): |
|
339 self.Close() |
|
340 |
|
341 def onAddContact(self, e): |
|
342 debug("Add contact request") |
|
343 dlg = wx.TextEntryDialog( |
|
344 self, 'Please enter new contact JID', |
|
345 'Adding a contact', 'name@server.ext') |
|
346 |
|
347 if dlg.ShowModal() == wx.ID_OK: |
|
348 jid=JID(dlg.GetValue()) |
|
349 if jid.is_valid(): |
|
350 self.bridge.addContact(jid.short) |
|
351 else: |
|
352 error ("'%s' is an invalid JID !", jid) |
|
353 #TODO: notice the user |
|
354 |
|
355 dlg.Destroy() |
|
356 |
|
357 def onRemoveContact(self, e): |
|
358 debug("Remove contact request") |
|
359 target = self.contactList.getSelection() |
|
360 if not target: |
|
361 dlg = wx.MessageDialog(self, "You haven't selected any contact !", |
|
362 'Error', |
|
363 wx.OK | wx.ICON_ERROR |
|
364 ) |
|
365 dlg.ShowModal() |
|
366 dlg.Destroy() |
|
367 return |
|
368 |
|
369 dlg = wx.MessageDialog(self, "Are you sure you want to delete %s from your roster list ?" % target.short, |
|
370 'Contact suppression', |
|
371 wx.YES_NO | wx.ICON_QUESTION |
|
372 ) |
|
373 |
|
374 if dlg.ShowModal() == wx.ID_YES: |
|
375 info("Unsubsribing %s presence", target.short) |
|
376 self.bridge.delContact(target.short) |
|
377 |
|
378 dlg.Destroy() |
|
379 |
|
380 def onClose(self, e): |
|
381 info("Exiting...") |
|
382 e.Skip() |
|
383 |