comparison src/browser/sat_browser/contact_widget.py @ 648:6d3142b782c3 frontends_multi_profiles

browser_side: classes reorganisation: - moved widgets in dedicated modules (base, contact, editor, libervia) and a widget module for single classes - same thing for panels (base, main, contact) - libervia_widget mix main panels and widget and drag n drop for technical reasons (see comments) - renamed WebPanel to WebWidget
author Goffi <goffi@goffi.org>
date Thu, 26 Feb 2015 18:10:54 +0100
parents src/browser/sat_browser/base_widget.py@e0021d571eef
children 267761bf7f08
comparison
equal deleted inserted replaced
647:e0021d571eef 648:6d3142b782c3
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 pyjamas.ui.VerticalPanel import VerticalPanel
25 from pyjamas.ui.HTML import HTML
26 from pyjamas.ui.Image import Image
27 from pyjamas.ui.ClickListener import ClickHandler
28 from constants import Const as C
29 import html_tools
30 import base_widget
31 import libervia_widget
32
33 unicode = str # XXX: pyjama doesn't manage unicode
34
35
36 class ContactLabel(HTML):
37 """Display a contact in HTML, selecting best display (jid/nick/etc)"""
38
39 def __init__(self, host, jid_):
40 # TODO: add a listener for nick changes
41 HTML.__init__(self)
42 self.host = host
43 self.jid = jid_.bare
44 self.nick = self.host.contact_lists[C.PROF_KEY_NONE].getCache(self.jid, "nick")
45 self.alert = False
46 self.refresh()
47 self.setStyleName('contactLabel')
48
49 def refresh(self):
50 alert_html = "<strong>(*)</strong>&nbsp;" if self.alert else ""
51 contact_html = html_tools.html_sanitize(self.nick or unicode(self.jid))
52 html = "%(alert)s%(contact)s" % {'alert': alert_html,
53 'contact': contact_html}
54 self.setHTML(html)
55
56 def updateNick(self, new_nick):
57 """Change the current nick
58
59 @param new_nick(unicode): new nick to use
60 """
61 self.nick = new_nick
62 self.refresh()
63
64 def setAlert(self, alert):
65 """Show a visual indicator
66
67 @param alert: True if alert must be shown
68 """
69 self.alert = alert
70 self.refresh()
71
72
73 class ContactMenuBar(base_widget.WidgetMenuBar):
74
75 def onBrowserEvent(self, event):
76 base_widget.WidgetMenuBar.onBrowserEvent(self, event)
77 event.stopPropagation() # prevent opening the chat dialog
78
79 @classmethod
80 def getCategoryHTML(cls, menu_name_i18n, type_):
81 return '<img src="%s"/>' % C.DEFAULT_AVATAR_URL
82
83 def setUrl(self, url):
84 """Set the URL of the contact avatar."""
85 self.items[0].setHTML('<img src="%s" />' % url)
86
87
88 class ContactBox(VerticalPanel, ClickHandler, libervia_widget.DragLabel):
89
90 def __init__(self, parent, jid_):
91 """
92 @param parent (ContactPanel): ContactPanel hosting this box
93 @param jid_ (jid.JID): contact JID
94 """
95 VerticalPanel.__init__(self, StyleName='contactBox', VerticalAlignment='middle')
96 ClickHandler.__init__(self)
97 libervia_widget.DragLabel.__init__(self, jid_, "CONTACT", parent.host)
98 self.jid = jid_.bare
99 self.label = ContactLabel(parent.host, self.jid)
100 self.avatar = ContactMenuBar(self, parent.host) if parent.handle_menu else Image()
101 self.updateAvatar(parent.host.getAvatarURL(self.jid))
102 self.add(self.avatar)
103 self.add(self.label)
104 self.addClickListener(self)
105
106 def addMenus(self, menu_bar):
107 menu_bar.addCachedMenus(C.MENU_ROSTER_JID_CONTEXT, {'jid': unicode(self.jid)})
108 menu_bar.addCachedMenus(C.MENU_JID_CONTEXT, {'jid': unicode(self.jid)})
109
110 def setAlert(self, alert):
111 """Show a visual indicator
112
113 @param alert: True if alert indicator show be shown"""
114 self.label.setAlert(alert)
115
116 def updateAvatar(self, url):
117 """Update the avatar.
118
119 @param url (unicode): image url
120 """
121 self.avatar.setUrl(url)
122
123 def updateNick(self, new_nick):
124 """Update the nickname.
125
126 @param new_nick (unicode): new nickname to use
127 """
128 self.label.updateNick(new_nick)
129
130 def onClick(self, sender):
131 try:
132 self.parent.onClick(self.jid)
133 except AttributeError:
134 pass
135 else:
136 self.setAlert(False)