Mercurial > libervia-backend
annotate frontends/src/wix/contact_list.py @ 683:75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
author | souliane <souliane@mailoo.org> |
---|---|
date | Wed, 23 Oct 2013 12:45:13 +0200 |
parents | 84a6e83157c2 |
children | 0b9bd47dffcd |
rev | line source |
---|---|
227 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
4 # wix: a SAT frontend |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org) |
227 | 6 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
10 # (at your option) any later version. |
227 | 11 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
15 # GNU Affero General Public License for more details. |
227 | 16 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
227 | 19 |
72 | 20 import wx |
227 | 21 from sat_frontends.quick_frontend.quick_contact_list import QuickContactList |
72 | 22 from logging import debug, info, error |
23 from cgi import escape | |
225
fd9b7834d98a
distutils installation script, draft
Goffi <goffi@goffi.org>
parents:
223
diff
changeset
|
24 from sat.tools.jid import JID |
366
0806a65a5fa9
wix: updated paths to use media_dir
Goffi <goffi@goffi.org>
parents:
316
diff
changeset
|
25 from os.path import join |
72 | 26 |
27 | |
199 | 28 class Group(unicode): |
72 | 29 """Class used to recognize groups""" |
30 | |
199 | 31 class Contact(unicode): |
72 | 32 """Class used to recognize groups""" |
33 | |
34 class ContactList(wx.SimpleHtmlListBox, QuickContactList): | |
35 """Customized control to manage contacts.""" | |
36 | |
37 def __init__(self, parent, host, type="JID"): | |
38 """init the contact list | |
39 @param parent: WxWidgets parent of the widget | |
40 @param host: wix main app class | |
41 @param type: type of contact list: "JID" for the usual big jid contact list | |
42 "CUSTOM" for a customized contact list (self.__presentItem must then be overrided) | |
43 """ | |
44 wx.SimpleHtmlListBox.__init__(self, parent, -1) | |
501
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
45 QuickContactList.__init__(self) |
72 | 46 self.host = host |
47 self.type = type | |
48 self.__typeSwitch() | |
49 self.groups = {} #list contacts in each groups, key = group | |
366
0806a65a5fa9
wix: updated paths to use media_dir
Goffi <goffi@goffi.org>
parents:
316
diff
changeset
|
50 self.empty_avatar = join(host.media_dir, 'misc/empty_avatar') |
72 | 51 self.Bind(wx.EVT_LISTBOX, self.onSelected) |
52 self.Bind(wx.EVT_LISTBOX_DCLICK, self.onActivated) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
53 |
124 | 54 def __contains__(self, jid): |
55 return bool(self.__find_idx(jid)) | |
72 | 56 |
57 def __typeSwitch(self): | |
58 if self.type == "JID": | |
59 self.__presentItem = self.__presentItemJID | |
60 elif type != "CUSTOM": | |
61 self.__presentItem = self.__presentItemDefault | |
62 | |
63 def __find_idx(self, entity): | |
64 """Find indexes of given contact (or groups) in contact list, manage jid | |
65 @return: list of indexes""" | |
66 result=[] | |
67 for i in range(self.GetCount()): | |
68 if (type(entity) == JID and type(self.GetClientData(i)) == JID and self.GetClientData(i).short == entity.short) or\ | |
69 self.GetClientData(i) == entity: | |
70 result.append(i) | |
71 return result | |
72 | |
501
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
73 def update_jid(self, jid): |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
74 self.replace(jid) |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
75 |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
76 def replace(self, contact, groups=None, attributes=None): |
72 | 77 debug(_("update %s") % contact) |
78 if not self.__find_idx(contact): | |
79 self.add(contact, groups) | |
80 else: | |
81 for i in self.__find_idx(contact): | |
501
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
82 _present = self.__presentItem(contact) |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
83 if _present != None: |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
84 self.SetString(i, _present) |
72 | 85 |
86 def __eraseGroup(self, group): | |
87 """Erase all contacts in group | |
88 @param group: group to erase | |
89 @return: True if something as been erased""" | |
90 erased = False | |
91 indexes = self.__find_idx(group) | |
92 for idx in indexes: | |
93 while idx<self.GetCount()-1 and type(self.GetClientData(idx+1)) != Group: | |
94 erased = True | |
95 self.Delete(idx+1) | |
96 return erased | |
97 | |
98 | |
99 def __presentGroup(self, group): | |
100 """Make a nice presentation for the contact groups""" | |
199 | 101 html = u"""-- [%s] --""" % group |
72 | 102 |
103 return html | |
104 | |
105 def __presentItemDefault(self, contact): | |
106 """Make a basic presentation of string contacts in the list.""" | |
107 return contact | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
108 |
72 | 109 def __presentItemJID(self, jid): |
110 """Make a nice presentation of the contact in the list for JID contacts.""" | |
501
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
111 name = self.getCache(jid,'name') |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
112 nick = self.getCache(jid,'nick') |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
113 _show = self.getCache(jid,'show') |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
114 if _show == None or _show == 'unavailable': |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
115 return None |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
116 show = filter(lambda x : x[0] == _show, const_STATUS)[0] |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
117 |
72 | 118 #show[0]==shortcut |
119 #show[1]==human readable | |
120 #show[2]==color (or None) | |
121 show_html = "<font color='%s'>[%s]</font>" % (show[2], show[1]) if show[2] else "" | |
501
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
122 status = self.getCache(jid,'status') or '' |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
123 avatar = self.getCache(jid,'avatar') or self.empty_avatar #XXX: there is a weird bug here: if the image has an extension (i.e. empty_avatar.png), |
316
3a21d586dae4
wix: workaround a weird bug which crash wix while using empty_avatar, see comments for more information
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
124 #WxPython segfault, and it doesn't without nothing. I couldn't reproduce the case with a basic test script, so it need further investigation before reporting it |
3a21d586dae4
wix: workaround a weird bug which crash wix while using empty_avatar, see comments for more information
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
125 #to WxPython dev. Anyway, the program crash with a segfault, not a python exception, so there is definitely something wrong with WxPython. |
3a21d586dae4
wix: workaround a weird bug which crash wix while using empty_avatar, see comments for more information
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
126 #The case seems to happen when SimpleHtmlListBox parse the HTML with the <img> tag |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
127 |
72 | 128 html = """ |
129 <table border='0'> | |
130 <td> | |
131 <img height='64' width='64' src='%s' /> | |
132 </td> | |
133 <td> | |
134 <b>%s</b> %s<br /> | |
135 <i>%s</i> | |
136 </td> | |
137 </table> | |
138 """ % (avatar, | |
139 escape(nick or name or jid.node or jid.short), | |
140 show_html, | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
141 escape(status)) |
72 | 142 |
143 return html | |
144 | |
501
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
145 def clearContacts(self): |
72 | 146 """Clear all the contact list""" |
147 self.Clear() | |
148 | |
149 def add(self, contact, groups = None): | |
150 """add a contact to the list""" | |
151 debug (_("adding %s"),contact) | |
152 if not groups: | |
501
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
153 _present = self.__presentItem(contact) |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
154 if _present: |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
155 idx = self.Insert(_present, 0, contact) |
72 | 156 else: |
157 for group in groups: | |
158 indexes = self.__find_idx(group) | |
159 gp_idx = 0 | |
160 if not indexes: #this is a new group, we have to create it | |
161 gp_idx = self.Append(self.__presentGroup(group), Group(group)) | |
162 else: | |
163 gp_idx = indexes[0] | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
164 |
501
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
165 _present = self.__presentItem(contact) |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
166 if _present: |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
167 self.Insert(_present, gp_idx+1, contact) |
72 | 168 |
502
debcf5dd404a
QuickFrontend, Primitivus, Wix: special entities management:
Goffi <goffi@goffi.org>
parents:
501
diff
changeset
|
169 def setSpecial(self, special_jid, special_type): |
510
886754295efe
quick frontend, primitivus, wix: MUC private messages management
Goffi <goffi@goffi.org>
parents:
502
diff
changeset
|
170 QuickContactList.setSpecial(self, special_jid, special_type) |
72 | 171 |
172 def remove(self, contact): | |
173 """remove a contact from the list""" | |
174 debug (_("removing %s"), contact) | |
175 list_idx = self.__find_idx(contact) | |
75 | 176 list_idx.reverse() #as we make some deletions, we have to reverse the order |
72 | 177 for i in list_idx: |
178 self.Delete(i) | |
179 | |
180 def onSelected(self, event): | |
181 """Called when a contact is selected.""" | |
182 data = self.getSelection() | |
183 if data == None: #we have a group | |
138
2f8c86488b05
wix: scrolling is not reseted anymore when clicking on a group on contact list
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
184 first_visible = self.GetVisibleBegin() |
72 | 185 group = self.GetClientData(self.GetSelection()) |
186 erased = self.__eraseGroup(group) | |
187 if not erased: #the group was already erased, we can add again the contacts | |
501
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
188 contacts = [JID(contact) for contact in self.host.bridge.getContactsFromGroup(group, self.host.profile)] |
72 | 189 contacts.sort() |
190 id_insert = self.GetSelection()+1 | |
191 for contact in contacts: | |
501
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
192 _present = self.__presentItem(contact) |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
193 if _present: |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
194 self.Insert(_present, id_insert, contact) |
72 | 195 self.SetSelection(wx.NOT_FOUND) |
138
2f8c86488b05
wix: scrolling is not reseted anymore when clicking on a group on contact list
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
196 self.ScrollToLine(first_visible) |
72 | 197 event.Skip(False) |
198 else: | |
199 event.Skip() | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
200 |
72 | 201 def onActivated(self, event): |
202 """Called when a contact is clicked or activated with keyboard.""" | |
203 data = self.getSelection() | |
204 self.onActivatedCB(data) | |
205 event.Skip() | |
206 | |
207 def getSelection(self): | |
208 """Return the selected contact, or an empty string if there is not""" | |
209 if self.GetSelection() == wx.NOT_FOUND: | |
210 return None | |
211 data = self.GetClientData(self.GetSelection()) | |
212 if type(data) == Group: | |
213 return None | |
214 return data | |
215 | |
216 def registerActivatedCB(self, cb): | |
217 """Register a callback with manage contact activation.""" | |
218 self.onActivatedCB=cb | |
219 |