Mercurial > libervia-web
comparison browser/sat_browser/contact_panel.py @ 1124:28e3eb3bb217
files reorganisation and installation rework:
- files have been reorganised to follow other SàT projects and usual Python organisation (no more "/src" directory)
- VERSION file is now used, as for other SàT projects
- replace the overcomplicated setup.py be a more sane one. Pyjamas part is not compiled anymore by setup.py, it must be done separatly
- removed check for data_dir if it's empty
- installation tested working in virtual env
- libervia launching script is now in bin/libervia
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 25 Aug 2018 17:59:48 +0200 |
parents | src/browser/sat_browser/contact_panel.py@f2170536ba23 |
children | 2af117bfe6cc |
comparison
equal
deleted
inserted
replaced
1123:63a4b8fe9782 | 1124:28e3eb3bb217 |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # Libervia: a Salut à Toi frontend | |
5 # Copyright (C) 2011-2018 Jérôme Poisson <goffi@goffi.org> | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 """ Contacts / jids related panels """ | |
21 | |
22 import pyjd # this is dummy in pyjs | |
23 from sat.core.log import getLogger | |
24 log = getLogger(__name__) | |
25 from sat_frontends.tools import jid | |
26 | |
27 from pyjamas.ui.ScrollPanel import ScrollPanel | |
28 from pyjamas.ui.VerticalPanel import VerticalPanel | |
29 | |
30 import contact_widget | |
31 from constants import Const as C | |
32 | |
33 | |
34 class ContactsPanel(ScrollPanel): | |
35 """ContactList graphic representation | |
36 | |
37 Special features like popup menu panel or changing the contact states must be done in a sub-class. | |
38 """ | |
39 | |
40 def __init__(self, host, merge_resources=True, contacts_click=None, | |
41 contacts_style=None, contacts_menus=None, | |
42 contacts_display=C.CONTACT_DEFAULT_DISPLAY): | |
43 """ | |
44 | |
45 @param host (SatWebFrontend): host instance | |
46 @param merge_resources (bool): if True, the entities sharing the same | |
47 bare JID will also share the same contact box. | |
48 @param contacts_click (callable): click callback for the contact boxes | |
49 @param contacts_style (unicode): CSS style name for the contact boxes | |
50 @param contacts_menus (tuple): define the menu types that fit this | |
51 contact panel, with values from the menus type constants. | |
52 @param contacts_display (tuple): prioritize the display methods of the | |
53 contact's label with values in ("jid", "nick", "bare", "resource") | |
54 """ | |
55 self.panel = VerticalPanel() | |
56 ScrollPanel.__init__(self, self.panel) | |
57 self.addStyleName("gwt-ScrollPanel") # XXX: no class is set by Pyjamas | |
58 | |
59 self.host = host | |
60 self.merge_resources = merge_resources | |
61 self._contacts = {} # entity jid to ContactBox map | |
62 self.panel.click_listener = None | |
63 | |
64 if contacts_click is not None: | |
65 self.panel.onClick = contacts_click | |
66 | |
67 self.contacts_style = contacts_style | |
68 self.contacts_menus = contacts_menus | |
69 self.contacts_display = contacts_display | |
70 | |
71 def _key(self, contact_jid): | |
72 """Return internal key for this contact. | |
73 | |
74 @param contact_jid (jid.JID): contact JID | |
75 @return: jid.JID | |
76 """ | |
77 return contact_jid.bare if self.merge_resources else contact_jid | |
78 | |
79 def getJids(self): | |
80 """Return jids present in the panel | |
81 | |
82 @return (list[jid.JID]): full jids or bare jids if self.merge_resources is True | |
83 """ | |
84 return self._contacts.keys() | |
85 | |
86 def getBoxes(self): | |
87 """Return ContactBox present in the panel | |
88 | |
89 @return (list[ContactBox]): boxes of the contacts | |
90 """ | |
91 return self._contacts.itervalues() | |
92 | |
93 def clear(self): | |
94 """Clear all contacts.""" | |
95 self._contacts.clear() | |
96 VerticalPanel.clear(self.panel) | |
97 | |
98 def setList(self, jids): | |
99 """set all contacts in the list in one shot. | |
100 | |
101 @param jids (list[jid.JID]): jids to display (the order is kept) | |
102 @param name (unicode): optional name of the contact | |
103 """ | |
104 current_jids = [box.jid for box in self.panel.children if isinstance(box, contact_widget.ContactBox)] | |
105 if current_jids == jids: | |
106 # the display doesn't change | |
107 return | |
108 for contact_jid in set(current_jids).difference(jids): | |
109 self.removeContactBox(contact_jid) | |
110 count = 0 | |
111 for contact_jid in jids: | |
112 assert isinstance(contact_jid, jid.JID) | |
113 self.updateContactBox(contact_jid, count) | |
114 count += 1 | |
115 | |
116 def getContactBox(self, contact_jid): | |
117 """Get the contact box for the given contact. | |
118 | |
119 @param contact_jid (jid.JID): contact JID | |
120 @return: ContactBox or None | |
121 """ | |
122 try: | |
123 return self._contacts[self._key(contact_jid)] | |
124 except KeyError: | |
125 return None | |
126 | |
127 def updateContactBox(self, contact_jid, index=None): | |
128 """Add a contact or update it if it already exists. | |
129 | |
130 @param contact_jid (jid.JID): contact JID | |
131 @param index (int): insertion index if the contact is added | |
132 @return: ContactBox | |
133 """ | |
134 box = self.getContactBox(contact_jid) | |
135 if box: | |
136 box.update() | |
137 else: | |
138 entity = contact_jid.bare if self.merge_resources else contact_jid | |
139 box = contact_widget.ContactBox(self.host, entity, | |
140 style_name=self.contacts_style, | |
141 display=self.contacts_display, | |
142 plugin_menu_context=self.contacts_menus) | |
143 self._contacts[self._key(contact_jid)] = box | |
144 if index: | |
145 VerticalPanel.insert(self.panel, box, index) | |
146 else: | |
147 VerticalPanel.append(self.panel, box) | |
148 return box | |
149 | |
150 def removeContactBox(self, contact_jid): | |
151 """Remove a contact. | |
152 | |
153 @param contact_jid (jid.JID): contact JID | |
154 """ | |
155 box = self._contacts.pop(self._key(contact_jid), None) | |
156 if box: | |
157 VerticalPanel.remove(self.panel, box) |