Mercurial > libervia-backend
annotate frontends/src/wix/contact_list.py @ 737:378af36155c2
frontends: set and retrieve your own presence and status
author | souliane <souliane@mailoo.org> |
---|---|
date | Mon, 25 Nov 2013 01:56:07 +0100 |
parents | 6246eb6d64a0 |
children | bfabeedbf32e |
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 |
736
6246eb6d64a0
frontends: define the constants with classes and inheritance instance of using __builtin__
souliane <souliane@mailoo.org>
parents:
688
diff
changeset
|
22 from sat_frontends.wix.constants import Const |
72 | 23 from logging import debug, info, error |
24 from cgi import escape | |
225
fd9b7834d98a
distutils installation script, draft
Goffi <goffi@goffi.org>
parents:
223
diff
changeset
|
25 from sat.tools.jid import JID |
366
0806a65a5fa9
wix: updated paths to use media_dir
Goffi <goffi@goffi.org>
parents:
316
diff
changeset
|
26 from os.path import join |
72 | 27 |
28 | |
199 | 29 class Group(unicode): |
72 | 30 """Class used to recognize groups""" |
31 | |
199 | 32 class Contact(unicode): |
72 | 33 """Class used to recognize groups""" |
34 | |
35 class ContactList(wx.SimpleHtmlListBox, QuickContactList): | |
36 """Customized control to manage contacts.""" | |
37 | |
38 def __init__(self, parent, host, type="JID"): | |
39 """init the contact list | |
40 @param parent: WxWidgets parent of the widget | |
41 @param host: wix main app class | |
42 @param type: type of contact list: "JID" for the usual big jid contact list | |
43 "CUSTOM" for a customized contact list (self.__presentItem must then be overrided) | |
44 """ | |
45 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
|
46 QuickContactList.__init__(self) |
72 | 47 self.host = host |
48 self.type = type | |
49 self.__typeSwitch() | |
50 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
|
51 self.empty_avatar = join(host.media_dir, 'misc/empty_avatar') |
72 | 52 self.Bind(wx.EVT_LISTBOX, self.onSelected) |
53 self.Bind(wx.EVT_LISTBOX_DCLICK, self.onActivated) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
54 |
124 | 55 def __contains__(self, jid): |
56 return bool(self.__find_idx(jid)) | |
72 | 57 |
58 def __typeSwitch(self): | |
59 if self.type == "JID": | |
60 self.__presentItem = self.__presentItemJID | |
61 elif type != "CUSTOM": | |
62 self.__presentItem = self.__presentItemDefault | |
63 | |
64 def __find_idx(self, entity): | |
65 """Find indexes of given contact (or groups) in contact list, manage jid | |
66 @return: list of indexes""" | |
67 result=[] | |
68 for i in range(self.GetCount()): | |
688
f7878ad3c846
tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents:
685
diff
changeset
|
69 if (type(entity) == JID and type(self.GetClientData(i)) == JID and self.GetClientData(i).bare == entity.bare) or\ |
72 | 70 self.GetClientData(i) == entity: |
71 result.append(i) | |
72 return result | |
73 | |
501
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
74 def update_jid(self, jid): |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
75 self.replace(jid) |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
76 |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
77 def replace(self, contact, groups=None, attributes=None): |
72 | 78 debug(_("update %s") % contact) |
79 if not self.__find_idx(contact): | |
80 self.add(contact, groups) | |
81 else: | |
82 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
|
83 _present = self.__presentItem(contact) |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
84 if _present != None: |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
85 self.SetString(i, _present) |
72 | 86 |
87 def __eraseGroup(self, group): | |
88 """Erase all contacts in group | |
89 @param group: group to erase | |
90 @return: True if something as been erased""" | |
91 erased = False | |
92 indexes = self.__find_idx(group) | |
93 for idx in indexes: | |
94 while idx<self.GetCount()-1 and type(self.GetClientData(idx+1)) != Group: | |
95 erased = True | |
96 self.Delete(idx+1) | |
97 return erased | |
98 | |
99 | |
100 def __presentGroup(self, group): | |
101 """Make a nice presentation for the contact groups""" | |
199 | 102 html = u"""-- [%s] --""" % group |
72 | 103 |
104 return html | |
105 | |
106 def __presentItemDefault(self, contact): | |
107 """Make a basic presentation of string contacts in the list.""" | |
108 return contact | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
109 |
72 | 110 def __presentItemJID(self, jid): |
111 """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
|
112 name = self.getCache(jid,'name') |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
113 nick = self.getCache(jid,'nick') |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
114 _show = self.getCache(jid,'show') |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
115 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
|
116 return None |
737
378af36155c2
frontends: set and retrieve your own presence and status
souliane <souliane@mailoo.org>
parents:
736
diff
changeset
|
117 show = [x for x in Const.PRESENCE if x[0] == _show][0] |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
118 |
72 | 119 #show[0]==shortcut |
120 #show[1]==human readable | |
121 #show[2]==color (or None) | |
122 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
|
123 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
|
124 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
|
125 #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
|
126 #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
|
127 #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
|
128 |
72 | 129 html = """ |
130 <table border='0'> | |
131 <td> | |
132 <img height='64' width='64' src='%s' /> | |
133 </td> | |
134 <td> | |
135 <b>%s</b> %s<br /> | |
136 <i>%s</i> | |
137 </td> | |
138 </table> | |
139 """ % (avatar, | |
688
f7878ad3c846
tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents:
685
diff
changeset
|
140 escape(nick or name or jid.node or jid.bare), |
72 | 141 show_html, |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
142 escape(status)) |
72 | 143 |
144 return html | |
145 | |
501
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
146 def clearContacts(self): |
72 | 147 """Clear all the contact list""" |
148 self.Clear() | |
149 | |
150 def add(self, contact, groups = None): | |
151 """add a contact to the list""" | |
152 debug (_("adding %s"),contact) | |
153 if not groups: | |
501
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
154 _present = self.__presentItem(contact) |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
155 if _present: |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
156 idx = self.Insert(_present, 0, contact) |
72 | 157 else: |
158 for group in groups: | |
159 indexes = self.__find_idx(group) | |
160 gp_idx = 0 | |
161 if not indexes: #this is a new group, we have to create it | |
162 gp_idx = self.Append(self.__presentGroup(group), Group(group)) | |
163 else: | |
164 gp_idx = indexes[0] | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
165 |
501
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
166 _present = self.__presentItem(contact) |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
167 if _present: |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
168 self.Insert(_present, gp_idx+1, contact) |
72 | 169 |
685
0b9bd47dffcd
primitivus, wix: auto-display MUC dialog after it has been joined:
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
170 def setSpecial(self, special_jid, special_type, show=False): |
0b9bd47dffcd
primitivus, wix: auto-display MUC dialog after it has been joined:
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
171 """Set entity as a special |
0b9bd47dffcd
primitivus, wix: auto-display MUC dialog after it has been joined:
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
172 @param jid: jid of the entity |
0b9bd47dffcd
primitivus, wix: auto-display MUC dialog after it has been joined:
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
173 @param _type: special type (e.g.: "MUC") |
0b9bd47dffcd
primitivus, wix: auto-display MUC dialog after it has been joined:
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
174 @param show: True to display the dialog to chat with this entity |
0b9bd47dffcd
primitivus, wix: auto-display MUC dialog after it has been joined:
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
175 """ |
0b9bd47dffcd
primitivus, wix: auto-display MUC dialog after it has been joined:
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
176 QuickContactList.setSpecial(self, special_jid, special_type, show) |
0b9bd47dffcd
primitivus, wix: auto-display MUC dialog after it has been joined:
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
177 if show: |
0b9bd47dffcd
primitivus, wix: auto-display MUC dialog after it has been joined:
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
178 self._showDialog(special_jid) |
0b9bd47dffcd
primitivus, wix: auto-display MUC dialog after it has been joined:
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
179 |
0b9bd47dffcd
primitivus, wix: auto-display MUC dialog after it has been joined:
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
180 def _showDialog(self, jid): |
0b9bd47dffcd
primitivus, wix: auto-display MUC dialog after it has been joined:
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
181 """Show the dialog associated to the given jid.""" |
0b9bd47dffcd
primitivus, wix: auto-display MUC dialog after it has been joined:
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
182 indexes = self.__find_idx(jid) |
0b9bd47dffcd
primitivus, wix: auto-display MUC dialog after it has been joined:
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
183 if not indexes: |
0b9bd47dffcd
primitivus, wix: auto-display MUC dialog after it has been joined:
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
184 return |
0b9bd47dffcd
primitivus, wix: auto-display MUC dialog after it has been joined:
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
185 self.DeselectAll() |
0b9bd47dffcd
primitivus, wix: auto-display MUC dialog after it has been joined:
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
186 self.SetSelection(indexes[0]) |
0b9bd47dffcd
primitivus, wix: auto-display MUC dialog after it has been joined:
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
187 self.onActivated(wx.MouseEvent()) |
72 | 188 |
189 def remove(self, contact): | |
190 """remove a contact from the list""" | |
191 debug (_("removing %s"), contact) | |
192 list_idx = self.__find_idx(contact) | |
75 | 193 list_idx.reverse() #as we make some deletions, we have to reverse the order |
72 | 194 for i in list_idx: |
195 self.Delete(i) | |
196 | |
197 def onSelected(self, event): | |
198 """Called when a contact is selected.""" | |
199 data = self.getSelection() | |
200 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
|
201 first_visible = self.GetVisibleBegin() |
72 | 202 group = self.GetClientData(self.GetSelection()) |
203 erased = self.__eraseGroup(group) | |
204 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
|
205 contacts = [JID(contact) for contact in self.host.bridge.getContactsFromGroup(group, self.host.profile)] |
72 | 206 contacts.sort() |
207 id_insert = self.GetSelection()+1 | |
208 for contact in contacts: | |
501
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
209 _present = self.__presentItem(contact) |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
210 if _present: |
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
211 self.Insert(_present, id_insert, contact) |
72 | 212 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
|
213 self.ScrollToLine(first_visible) |
72 | 214 event.Skip(False) |
215 else: | |
216 event.Skip() | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
217 |
72 | 218 def onActivated(self, event): |
219 """Called when a contact is clicked or activated with keyboard.""" | |
220 data = self.getSelection() | |
221 self.onActivatedCB(data) | |
222 event.Skip() | |
223 | |
224 def getSelection(self): | |
225 """Return the selected contact, or an empty string if there is not""" | |
226 if self.GetSelection() == wx.NOT_FOUND: | |
227 return None | |
228 data = self.GetClientData(self.GetSelection()) | |
229 if type(data) == Group: | |
230 return None | |
231 return data | |
232 | |
233 def registerActivatedCB(self, cb): | |
234 """Register a callback with manage contact activation.""" | |
235 self.onActivatedCB=cb | |
236 |