Mercurial > libervia-backend
annotate frontends/src/wix/contact_list.py @ 502:debcf5dd404a
QuickFrontend, Primitivus, Wix: special entities management:
- MUC rooms are special entities
- Primitivus: special entities are shown at the top of the contacts list above a divider,
presence updates are temporary ignored for them
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 25 Sep 2012 23:10:22 +0200 |
parents | e9634d2e7b38 |
children | 886754295efe |
rev | line source |
---|---|
227 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 wix: a SAT frontend | |
459 | 6 Copyright (C) 2009, 2010, 2011, 2012 Jérôme Poisson (goffi@goffi.org) |
227 | 7 |
8 This program is free software: you can redistribute it and/or modify | |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
9 it under the terms of the GNU Affero General Public License as published by |
227 | 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 | |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
16 GNU Affero General Public License for more details. |
227 | 17 |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
18 You should have received a copy of the GNU Affero General Public License |
227 | 19 along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 """ | |
21 | |
72 | 22 import wx |
227 | 23 from sat_frontends.quick_frontend.quick_contact_list import QuickContactList |
72 | 24 from logging import debug, info, error |
25 from cgi import escape | |
225
fd9b7834d98a
distutils installation script, draft
Goffi <goffi@goffi.org>
parents:
223
diff
changeset
|
26 from sat.tools.jid import JID |
366
0806a65a5fa9
wix: updated paths to use media_dir
Goffi <goffi@goffi.org>
parents:
316
diff
changeset
|
27 from os.path import join |
72 | 28 |
29 | |
199 | 30 class Group(unicode): |
72 | 31 """Class used to recognize groups""" |
32 | |
199 | 33 class Contact(unicode): |
72 | 34 """Class used to recognize groups""" |
35 | |
36 class ContactList(wx.SimpleHtmlListBox, QuickContactList): | |
37 """Customized control to manage contacts.""" | |
38 | |
39 def __init__(self, parent, host, type="JID"): | |
40 """init the contact list | |
41 @param parent: WxWidgets parent of the widget | |
42 @param host: wix main app class | |
43 @param type: type of contact list: "JID" for the usual big jid contact list | |
44 "CUSTOM" for a customized contact list (self.__presentItem must then be overrided) | |
45 """ | |
46 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
|
47 QuickContactList.__init__(self) |
72 | 48 self.host = host |
49 self.type = type | |
50 self.__typeSwitch() | |
51 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
|
52 self.empty_avatar = join(host.media_dir, 'misc/empty_avatar') |
72 | 53 self.Bind(wx.EVT_LISTBOX, self.onSelected) |
54 self.Bind(wx.EVT_LISTBOX_DCLICK, self.onActivated) | |
124 | 55 |
56 def __contains__(self, jid): | |
57 return bool(self.__find_idx(jid)) | |
72 | 58 |
59 def __typeSwitch(self): | |
60 if self.type == "JID": | |
61 self.__presentItem = self.__presentItemJID | |
62 elif type != "CUSTOM": | |
63 self.__presentItem = self.__presentItemDefault | |
64 | |
65 def __find_idx(self, entity): | |
66 """Find indexes of given contact (or groups) in contact list, manage jid | |
67 @return: list of indexes""" | |
68 result=[] | |
69 for i in range(self.GetCount()): | |
70 if (type(entity) == JID and type(self.GetClientData(i)) == JID and self.GetClientData(i).short == entity.short) or\ | |
71 self.GetClientData(i) == entity: | |
72 result.append(i) | |
73 return result | |
74 | |
501
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
75 def update_jid(self, jid): |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
76 self.replace(jid) |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
77 |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
78 def replace(self, contact, groups=None, attributes=None): |
72 | 79 debug(_("update %s") % contact) |
80 if not self.__find_idx(contact): | |
81 self.add(contact, groups) | |
82 else: | |
83 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
|
84 _present = self.__presentItem(contact) |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
85 if _present != None: |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
86 self.SetString(i, _present) |
72 | 87 |
88 def __eraseGroup(self, group): | |
89 """Erase all contacts in group | |
90 @param group: group to erase | |
91 @return: True if something as been erased""" | |
92 erased = False | |
93 indexes = self.__find_idx(group) | |
94 for idx in indexes: | |
95 while idx<self.GetCount()-1 and type(self.GetClientData(idx+1)) != Group: | |
96 erased = True | |
97 self.Delete(idx+1) | |
98 return erased | |
99 | |
100 | |
101 def __presentGroup(self, group): | |
102 """Make a nice presentation for the contact groups""" | |
199 | 103 html = u"""-- [%s] --""" % group |
72 | 104 |
105 return html | |
106 | |
107 def __presentItemDefault(self, contact): | |
108 """Make a basic presentation of string contacts in the list.""" | |
109 return contact | |
110 | |
111 def __presentItemJID(self, jid): | |
112 """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
|
113 name = self.getCache(jid,'name') |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
114 nick = self.getCache(jid,'nick') |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
115 _show = self.getCache(jid,'show') |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
116 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
|
117 return None |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
118 show = filter(lambda x : x[0] == _show, const_STATUS)[0] |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
119 |
72 | 120 #show[0]==shortcut |
121 #show[1]==human readable | |
122 #show[2]==color (or None) | |
123 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
|
124 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
|
125 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
|
126 #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
|
127 #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
|
128 #The case seems to happen when SimpleHtmlListBox parse the HTML with the <img> tag |
72 | 129 |
130 html = """ | |
131 <table border='0'> | |
132 <td> | |
133 <img height='64' width='64' src='%s' /> | |
134 </td> | |
135 <td> | |
136 <b>%s</b> %s<br /> | |
137 <i>%s</i> | |
138 </td> | |
139 </table> | |
140 """ % (avatar, | |
141 escape(nick or name or jid.node or jid.short), | |
142 show_html, | |
143 escape(status)) | |
144 | |
145 return html | |
146 | |
501
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
147 def clearContacts(self): |
72 | 148 """Clear all the contact list""" |
149 self.Clear() | |
150 | |
151 def add(self, contact, groups = None): | |
152 """add a contact to the list""" | |
153 debug (_("adding %s"),contact) | |
154 if not groups: | |
501
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
155 _present = self.__presentItem(contact) |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
156 if _present: |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
157 idx = self.Insert(_present, 0, contact) |
72 | 158 else: |
159 for group in groups: | |
160 indexes = self.__find_idx(group) | |
161 gp_idx = 0 | |
162 if not indexes: #this is a new group, we have to create it | |
163 gp_idx = self.Append(self.__presentGroup(group), Group(group)) | |
164 else: | |
165 gp_idx = indexes[0] | |
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
|
166 |
501
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
167 _present = self.__presentItem(contact) |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
168 if _present: |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
169 self.Insert(_present, gp_idx+1, contact) |
72 | 170 |
502
debcf5dd404a
QuickFrontend, Primitivus, Wix: special entities management:
Goffi <goffi@goffi.org>
parents:
501
diff
changeset
|
171 def setSpecial(self, special_jid, special_type): |
debcf5dd404a
QuickFrontend, Primitivus, Wix: special entities management:
Goffi <goffi@goffi.org>
parents:
501
diff
changeset
|
172 pass |
72 | 173 |
174 | |
175 def remove(self, contact): | |
176 """remove a contact from the list""" | |
177 debug (_("removing %s"), contact) | |
178 list_idx = self.__find_idx(contact) | |
75 | 179 list_idx.reverse() #as we make some deletions, we have to reverse the order |
72 | 180 for i in list_idx: |
181 self.Delete(i) | |
182 | |
183 def onSelected(self, event): | |
184 """Called when a contact is selected.""" | |
185 data = self.getSelection() | |
186 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
|
187 first_visible = self.GetVisibleBegin() |
72 | 188 group = self.GetClientData(self.GetSelection()) |
189 erased = self.__eraseGroup(group) | |
190 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
|
191 contacts = [JID(contact) for contact in self.host.bridge.getContactsFromGroup(group, self.host.profile)] |
72 | 192 contacts.sort() |
193 id_insert = self.GetSelection()+1 | |
194 for contact in contacts: | |
501
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
195 _present = self.__presentItem(contact) |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
196 if _present: |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
197 self.Insert(_present, id_insert, contact) |
72 | 198 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
|
199 self.ScrollToLine(first_visible) |
72 | 200 event.Skip(False) |
201 else: | |
202 event.Skip() | |
203 | |
204 def onActivated(self, event): | |
205 """Called when a contact is clicked or activated with keyboard.""" | |
206 data = self.getSelection() | |
207 self.onActivatedCB(data) | |
208 event.Skip() | |
209 | |
210 def getSelection(self): | |
211 """Return the selected contact, or an empty string if there is not""" | |
212 if self.GetSelection() == wx.NOT_FOUND: | |
213 return None | |
214 data = self.GetClientData(self.GetSelection()) | |
215 if type(data) == Group: | |
216 return None | |
217 return data | |
218 | |
219 def registerActivatedCB(self, cb): | |
220 """Register a callback with manage contact activation.""" | |
221 self.onActivatedCB=cb | |
222 |