diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/browser/sat_browser/contact_widget.py	Thu Feb 26 18:10:54 2015 +0100
@@ -0,0 +1,136 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+# Libervia: a Salut à Toi frontend
+# Copyright (C) 2011, 2012, 2013, 2014 Jérôme Poisson <goffi@goffi.org>
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import pyjd  # this is dummy in pyjs
+from sat.core.log import getLogger
+log = getLogger(__name__)
+
+from pyjamas.ui.VerticalPanel import VerticalPanel
+from pyjamas.ui.HTML import HTML
+from pyjamas.ui.Image import Image
+from pyjamas.ui.ClickListener import ClickHandler
+from constants import Const as C
+import html_tools
+import base_widget
+import libervia_widget
+
+unicode = str # XXX: pyjama doesn't manage unicode
+
+
+class ContactLabel(HTML):
+    """Display a contact in HTML, selecting best display (jid/nick/etc)"""
+
+    def __init__(self, host, jid_):
+        # TODO: add a listener for nick changes
+        HTML.__init__(self)
+        self.host = host
+        self.jid = jid_.bare
+        self.nick = self.host.contact_lists[C.PROF_KEY_NONE].getCache(self.jid, "nick")
+        self.alert = False
+        self.refresh()
+        self.setStyleName('contactLabel')
+
+    def refresh(self):
+        alert_html = "<strong>(*)</strong>&nbsp;" if self.alert else ""
+        contact_html = html_tools.html_sanitize(self.nick or unicode(self.jid))
+        html = "%(alert)s%(contact)s" % {'alert': alert_html,
+                                         'contact': contact_html}
+        self.setHTML(html)
+
+    def updateNick(self, new_nick):
+        """Change the current nick
+
+        @param new_nick(unicode): new nick to use
+        """
+        self.nick = new_nick
+        self.refresh()
+
+    def setAlert(self, alert):
+        """Show a visual indicator
+
+        @param alert: True if alert must be shown
+        """
+        self.alert = alert
+        self.refresh()
+
+
+class ContactMenuBar(base_widget.WidgetMenuBar):
+
+    def onBrowserEvent(self, event):
+        base_widget.WidgetMenuBar.onBrowserEvent(self, event)
+        event.stopPropagation()  # prevent opening the chat dialog
+
+    @classmethod
+    def getCategoryHTML(cls, menu_name_i18n, type_):
+        return '<img src="%s"/>' % C.DEFAULT_AVATAR_URL
+
+    def setUrl(self, url):
+        """Set the URL of the contact avatar."""
+        self.items[0].setHTML('<img src="%s" />' % url)
+
+
+class ContactBox(VerticalPanel, ClickHandler, libervia_widget.DragLabel):
+
+    def __init__(self, parent, jid_):
+        """
+        @param parent (ContactPanel): ContactPanel hosting this box
+        @param jid_ (jid.JID): contact JID
+        """
+        VerticalPanel.__init__(self, StyleName='contactBox', VerticalAlignment='middle')
+        ClickHandler.__init__(self)
+        libervia_widget.DragLabel.__init__(self, jid_, "CONTACT", parent.host)
+        self.jid = jid_.bare
+        self.label = ContactLabel(parent.host, self.jid)
+        self.avatar = ContactMenuBar(self, parent.host) if parent.handle_menu else Image()
+        self.updateAvatar(parent.host.getAvatarURL(self.jid))
+        self.add(self.avatar)
+        self.add(self.label)
+        self.addClickListener(self)
+
+    def addMenus(self, menu_bar):
+        menu_bar.addCachedMenus(C.MENU_ROSTER_JID_CONTEXT, {'jid': unicode(self.jid)})
+        menu_bar.addCachedMenus(C.MENU_JID_CONTEXT, {'jid': unicode(self.jid)})
+
+    def setAlert(self, alert):
+        """Show a visual indicator
+
+        @param alert: True if alert indicator show be shown"""
+        self.label.setAlert(alert)
+
+    def updateAvatar(self, url):
+        """Update the avatar.
+
+        @param url (unicode): image url
+        """
+        self.avatar.setUrl(url)
+
+    def updateNick(self, new_nick):
+        """Update the nickname.
+
+        @param new_nick (unicode): new nickname to use
+        """
+        self.label.updateNick(new_nick)
+
+    def onClick(self, sender):
+        try:
+            self.parent.onClick(self.jid)
+        except AttributeError:
+            pass
+        else:
+            self.setAlert(False)