Mercurial > libervia-web
comparison src/browser/sat_browser/contact_widget.py @ 679:a90cc8fc9605
merged branch frontends_multi_profiles
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 18 Mar 2015 16:15:18 +0100 |
parents | 849ffb24d5bf |
children | e876f493dccc |
comparison
equal
deleted
inserted
replaced
590:1bffc4c244c3 | 679:a90cc8fc9605 |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # Libervia: a Salut à Toi frontend | |
5 # Copyright (C) 2011, 2012, 2013, 2014 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 import pyjd # this is dummy in pyjs | |
21 from sat.core.log import getLogger | |
22 log = getLogger(__name__) | |
23 | |
24 from sat.core import exceptions | |
25 from sat_frontends.quick_frontend import quick_menus | |
26 from pyjamas.ui.VerticalPanel import VerticalPanel | |
27 from pyjamas.ui.HTML import HTML | |
28 from pyjamas.ui.Image import Image | |
29 from pyjamas.ui.ClickListener import ClickHandler | |
30 from constants import Const as C | |
31 import html_tools | |
32 import base_widget | |
33 import libervia_widget | |
34 | |
35 unicode = str # XXX: pyjama doesn't manage unicode | |
36 | |
37 | |
38 class ContactLabel(HTML): | |
39 """Display a contact in HTML, selecting best display (jid/nick/etc)""" | |
40 | |
41 def __init__(self, host, jid_, display=C.CONTACT_DEFAULT_DISPLAY): | |
42 """ | |
43 | |
44 @param host (SatWebFrontend): host instance | |
45 @param jid_ (jid.JID): contact JID | |
46 @param display (tuple): prioritize the display methods of the contact's | |
47 label with values in ("jid", "nick", "bare", "resource"). | |
48 """ | |
49 # TODO: add a listener for nick changes | |
50 HTML.__init__(self) | |
51 self.host = host | |
52 self.jid = jid_ | |
53 if "nick" in display: | |
54 self.nick = self.host.contact_lists[C.PROF_KEY_NONE].getCache(self.jid, "nick") | |
55 self.display = display | |
56 self.alert = False | |
57 self.refresh() | |
58 self.setStyleName('contactLabel') | |
59 | |
60 def refresh(self): | |
61 alert_html = "<strong>(*)</strong> " if self.alert else "" | |
62 contact_raw = None | |
63 for disp in self.display: | |
64 if disp == "jid": | |
65 contact_raw = unicode(self.jid) | |
66 elif disp == "nick": | |
67 contact_raw = self.nick | |
68 elif disp == "bare": | |
69 contact_raw = unicode(self.jid.bare) | |
70 elif disp == "resource": | |
71 contact_raw = self.jid.resource | |
72 else: | |
73 raise exceptions.InternalError(u"Unknown display argument [{}]".format(disp)) | |
74 if contact_raw: | |
75 break | |
76 if not contact_raw: | |
77 log.error(u"Counld not find a contact display for jid {jid} (display: {display})".format(jid=self.jid, display=self.display)) | |
78 contact_raw = "UNNAMED" | |
79 contact_html = html_tools.html_sanitize(contact_raw) | |
80 html = "%(alert)s%(contact)s" % {'alert': alert_html, | |
81 'contact': contact_html} | |
82 self.setHTML(html) | |
83 | |
84 def updateNick(self, new_nick): | |
85 """Change the current nick | |
86 | |
87 @param new_nick(unicode): new nick to use | |
88 """ | |
89 self.nick = new_nick | |
90 self.refresh() | |
91 | |
92 def setAlert(self, alert): | |
93 """Show a visual indicator | |
94 | |
95 @param alert: True if alert must be shown | |
96 """ | |
97 self.alert = alert | |
98 self.refresh() | |
99 | |
100 | |
101 class ContactMenuBar(base_widget.WidgetMenuBar): | |
102 | |
103 def onBrowserEvent(self, event): | |
104 base_widget.WidgetMenuBar.onBrowserEvent(self, event) | |
105 event.stopPropagation() # prevent opening the chat dialog | |
106 | |
107 @classmethod | |
108 def getCategoryHTML(cls, menu_name_i18n, type_): | |
109 return '<img src="%s"/>' % C.DEFAULT_AVATAR_URL | |
110 | |
111 def setUrl(self, url): | |
112 """Set the URL of the contact avatar.""" | |
113 self.items[0].setHTML('<img src="%s" />' % url) | |
114 | |
115 | |
116 class ContactBox(VerticalPanel, ClickHandler, libervia_widget.DragLabel): | |
117 | |
118 def __init__(self, host, jid_, style_name=None, display=C.CONTACT_DEFAULT_DISPLAY, plugin_menu_context=None): | |
119 """ | |
120 @param host (SatWebFrontend): host instance | |
121 @param jid_ (jid.JID): contact JID | |
122 @param style_name (unicode): CSS style name | |
123 @param contacts_display (tuple): prioritize the display methods of the | |
124 contact's label with values in ("jid", "nick", "bare", "resource"). | |
125 @param plugin_menu_context (iterable): contexts of menus to have (list of C.MENU_* constant) | |
126 | |
127 """ | |
128 self.plugin_menu_context = [] if plugin_menu_context is None else plugin_menu_context | |
129 VerticalPanel.__init__(self, StyleName=style_name or 'contactBox', VerticalAlignment='middle') | |
130 ClickHandler.__init__(self) | |
131 libervia_widget.DragLabel.__init__(self, jid_, "CONTACT", host) | |
132 self.jid = jid_ | |
133 self.label = ContactLabel(host, self.jid, display=display) | |
134 self.avatar = ContactMenuBar(self, host) if plugin_menu_context else Image() | |
135 try: # FIXME: dirty hack to force using an Image when the menu is actually empty | |
136 self.avatar.items[0] | |
137 except IndexError: | |
138 self.avatar = Image() | |
139 self.updateAvatar(host.getAvatarURL(self.jid.bare)) | |
140 self.add(self.avatar) | |
141 self.add(self.label) | |
142 self.addClickListener(self) | |
143 | |
144 def setAlert(self, alert): | |
145 """Show a visual indicator | |
146 | |
147 @param alert: True if alert indicator show be shown""" | |
148 self.label.setAlert(alert) | |
149 | |
150 def updateAvatar(self, url): | |
151 """Update the avatar. | |
152 | |
153 @param url (unicode): image url | |
154 """ | |
155 self.avatar.setUrl(url) | |
156 | |
157 def updateNick(self, new_nick): | |
158 """Update the nickname. | |
159 | |
160 @param new_nick (unicode): new nickname to use | |
161 """ | |
162 self.label.updateNick(new_nick) | |
163 | |
164 def onClick(self, sender): | |
165 try: | |
166 self.parent.onClick(self.jid.bare) | |
167 except (AttributeError, TypeError): | |
168 pass | |
169 else: | |
170 self.setAlert(False) | |
171 | |
172 quick_menus.QuickMenusManager.addDataCollector(C.MENU_JID_CONTEXT, lambda caller, dummy: {'jid': unicode(caller.jid.bare)}) |