comparison browser/sat_browser/contact_widget.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_widget.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 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 self.display = display
54 self.alert = False
55 self.setStyleName('contactLabel')
56
57 def update(self):
58 clist = self.host.contact_list
59 notifs = list(self.host.getNotifs(self.jid, exact_jid=False, profile=C.PROF_KEY_NONE))
60 alerts_count = len(notifs)
61 alert_html = ("<strong>(%i)</strong>&nbsp;" % alerts_count) if alerts_count else ""
62
63 contact_raw = None
64 for disp in self.display:
65 if disp == "jid":
66 contact_raw = unicode(self.jid)
67 elif disp == "nick":
68 clist = self.host.contact_list
69 contact_raw = html_tools.html_sanitize(clist.getCache(self.jid, "nick"))
70 elif disp == "bare":
71 contact_raw = unicode(self.jid.bare)
72 elif disp == "resource":
73 contact_raw = self.jid.resource
74 else:
75 raise exceptions.InternalError(u"Unknown display argument [{}]".format(disp))
76 if contact_raw:
77 break
78 if not contact_raw:
79 log.error(u"Could not find a contact display for jid {jid} (display: {display})".format(jid=self.jid, display=self.display))
80 contact_raw = "UNNAMED"
81 contact_html = html_tools.html_sanitize(contact_raw)
82
83 html = "%(alert)s%(contact)s" % {'alert': alert_html,
84 'contact': contact_html}
85 self.setHTML(html)
86
87
88 class ContactMenuBar(base_widget.WidgetMenuBar):
89 """WidgetMenuBar displaying the contact's avatar as category."""
90
91 def onBrowserEvent(self, event):
92 base_widget.WidgetMenuBar.onBrowserEvent(self, event)
93 event.stopPropagation() # prevent opening the chat dialog
94
95 @classmethod
96 def getCategoryHTML(cls, category):
97 """Return the HTML code for displaying contact's avatar.
98
99 @param category (quick_menus.MenuCategory): ignored
100 @return(unicode): HTML to display
101 """
102 return '<img src="%s"/>' % C.DEFAULT_AVATAR_URL
103
104 def setUrl(self, url):
105 """Set the URL of the contact avatar.
106
107 @param url (unicode): avatar URL
108 """
109 if not self.items: # the menu is empty but we've been asked to set an avatar
110 self.addCategory("dummy")
111 self.items[0].setHTML('<img src="%s" />' % url)
112
113
114 class ContactBox(VerticalPanel, ClickHandler, libervia_widget.DragLabel):
115
116 def __init__(self, host, jid_, style_name=None, display=C.CONTACT_DEFAULT_DISPLAY, plugin_menu_context=None):
117 """
118 @param host (SatWebFrontend): host instance
119 @param jid_ (jid.JID): contact JID
120 @param style_name (unicode): CSS style name
121 @param contacts_display (tuple): prioritize the display methods of the
122 contact's label with values in ("jid", "nick", "bare", "resource").
123 @param plugin_menu_context (iterable): contexts of menus to have (list of C.MENU_* constant)
124
125 """
126 self.plugin_menu_context = [] if plugin_menu_context is None else plugin_menu_context
127 VerticalPanel.__init__(self, StyleName=style_name or 'contactBox', VerticalAlignment='middle')
128 ClickHandler.__init__(self)
129 libervia_widget.DragLabel.__init__(self, jid_, "CONTACT", host)
130 self.jid = jid_
131 self.label = ContactLabel(host, self.jid, display=display)
132 self.avatar = ContactMenuBar(self, host) if plugin_menu_context else Image()
133 self.states = HTML()
134 self.add(self.avatar)
135 self.add(self.label)
136 self.add(self.states)
137 self.update()
138 self.addClickListener(self)
139
140 def update(self):
141 """Update the display.
142
143 @param with_bare (bool): if True, ignore the resource and update with bare information.
144 """
145 self.avatar.setUrl(self.host.getAvatarURL(self.jid))
146
147 self.label.update()
148 clist = self.host.contact_list
149 show = clist.getCache(self.jid, C.PRESENCE_SHOW)
150 if show is None:
151 show = C.PRESENCE_UNAVAILABLE
152 html_tools.setPresenceStyle(self.label, show)
153
154 def onClick(self, sender):
155 try:
156 self.parent.onClick(self.jid)
157 except (AttributeError, TypeError):
158 pass
159
160 quick_menus.QuickMenusManager.addDataCollector(C.MENU_JID_CONTEXT, lambda caller, dummy: {'jid': unicode(caller.jid.bare)})