diff src/browser/sat_browser/chat.py @ 914:0c0551967bdf

server, browser: partial Libervia fix Libervia was broken following the refactorings. This commit partially fixes it : Libervia is starting, avatar, blog and message are working again, but not everything is restablished yet. following things have been fixed/changed: - new dependency: shortuuid - D-Bus bridge is working again - fixed naming in several bridge methods - register method changed to register_signal - fixed Chat widget, which was not working anymore since the refactoring - avatar now use avatarGet. Cache dir is accessible using a session specific uuid, to avoid cache leak (i.e. accessing cache of other profiles) - server: new uuid attribute in session data Browser code is not fully working yet, notably OTR and contact list are not fully fixed.
author Goffi <goffi@goffi.org>
date Sun, 26 Feb 2017 18:32:47 +0100
parents e8b133b77aa4
children 5d9f6d25c586
line wrap: on
line diff
--- a/src/browser/sat_browser/chat.py	Sun Aug 28 19:25:52 2016 +0200
+++ b/src/browser/sat_browser/chat.py	Sun Feb 26 18:32:47 2017 +0100
@@ -29,15 +29,12 @@
 from pyjamas.ui.AbsolutePanel import AbsolutePanel
 from pyjamas.ui.VerticalPanel import VerticalPanel
 from pyjamas.ui.HorizontalPanel import HorizontalPanel
-from pyjamas.ui.Label import Label
-from pyjamas.ui.HTML import HTML
 from pyjamas.ui.KeyboardListener import KEY_ENTER, KeyboardHandler
 from pyjamas.ui.HTMLPanel import HTMLPanel
 from pyjamas import DOM
 from pyjamas import Window
 
 from datetime import datetime
-from time import time
 
 import html_tools
 import libervia_widget
@@ -53,21 +50,46 @@
 unicode = str  # FIXME: pyjamas workaround
 
 
-class ChatText(HTMLPanel):
+class MessageWidget(HTMLPanel):
+
+    def __init__(self, mess_data):
+        """
+        @param mess_data(quick_chat.Message, None): message data
+            None: used only for non text widgets (e.g.: focus separator)
+        """
+        self.mess_data = mess_data
+        mess_data.widgets.add(self)
+        _msg_class = []
+        if mess_data.type == C.MESS_TYPE_INFO:
+            markup = "<span class='{msg_class}'>{msg}</span>"
 
-    def __init__(self, timestamp, nick, mymess, msg, extra):
-        xhtml = extra.get('xhtml')
-        _date = datetime.fromtimestamp(float(timestamp or time()))
-        _msg_class = ["chat_text_msg"]
-        if mymess:
-            _msg_class.append("chat_text_mymess")
-        HTMLPanel.__init__(self, "<span class='chat_text_timestamp'>%(timestamp)s</span> <span class='chat_text_nick'>%(nick)s</span> <span class='%(msg_class)s'>%(msg)s</span>" %
-                           {"timestamp": _date.strftime("%H:%M"),
-                            "nick": "[%s]" % html_tools.html_sanitize(nick),
-                            "msg_class": ' '.join(_msg_class),
-                            "msg": strings.addURLToText(html_tools.html_sanitize(msg)) if not xhtml else html_tools.inlineRoot(xhtml)}  # FIXME: images and external links must be removed according to preferences
-                           )
-        self.setStyleName('chatText')
+            if mess_data.extra.get('info_type') == 'me':
+                _msg_class.append('chatTextMe')
+            else:
+                _msg_class.append('chatTextInfo')
+            # FIXME: following code was in printInfo before refactoring
+            #        seems to be used only in radiocol
+            # elif type_ == 'link':
+            #     _wid = HTML(msg)
+            #     _wid.setStyleName('chatTextInfo-link')
+            #     if link_cb:
+            #         _wid.addClickListener(link_cb)
+        else:
+            markup = "<span class='chat_text_timestamp'>{timestamp}</span> <span class='chat_text_nick'>{nick}</span> <span class='{msg_class}'>{msg}</span>"
+            _msg_class.append("chat_text_msg")
+            if mess_data.own_mess:
+                _msg_class.append("chat_text_mymess")
+
+        xhtml = mess_data.main_message_xhtml
+        _date = datetime.fromtimestamp(float(mess_data.timestamp))
+        HTMLPanel.__init__(self, markup.format(
+                               timestamp = _date.strftime("%H:%M"),
+                               nick =  "[{}]".format(html_tools.html_sanitize(mess_data.nick)),
+                               msg_class = ' '.join(_msg_class),
+                               msg = strings.addURLToText(html_tools.html_sanitize(mess_data.main_message)) if not xhtml else html_tools.inlineRoot(xhtml)  # FIXME: images and external links must be removed according to preferences
+                           ))
+        if mess_data.type != C.MESS_TYPE_INFO:
+            self.setStyleName('chatText')
 
 
 class Chat(QuickChat, libervia_widget.LiberviaWidget, KeyboardHandler):
@@ -121,6 +143,7 @@
         self.message_box.onSelectedChange(self)
         self.message_box.addKeyboardListener(self)
         self.vpanel.add(self.message_box)
+        self.postInit()
 
     def onWindowResized(self, width=None, height=None):
         if self.type == C.CHAT_GROUP:
@@ -210,40 +233,27 @@
             self.setHeaderInfo(header_info)
         QuickChat.newMessage(self, from_jid, target, msg, type_, extra, profile)
 
-    def printInfo(self, msg, type_='normal', extra=None, link_cb=None):
-        """Print general info
-        @param msg: message to print
-        @param type_: one of:
-            "normal": general info like "toto has joined the room" (will be sanitized)
-            "link": general info that is clickable like "click here to join the main room" (no sanitize done)
-            "me": "/me" information like "/me clenches his fist" ==> "toto clenches his fist" (will stay on one line)
-        @param extra (dict): message data
-        @param link_cb: method to call when the info is clicked, ignored if type_ is not 'link'
+    def _onHistoryPrinted(self):
+        """Refresh or scroll down the focus after the history is printed"""
+        self.printMessages(clear=False)
+        super(Chat, self)._onHistoryPrinted()
+
+    def printMessages(self, clear=True):
+        """generate message widgets
+
+        @param clear(bool): clear message before printing if true
         """
-        QuickChat.printInfo(self, msg, type_, extra)
-        if extra is None:
-            extra = {}
-        if type_ == 'normal':
-            _wid = HTML(strings.addURLToText(html_tools.XHTML2Text(msg)))
-            _wid.setStyleName('chatTextInfo')
-        elif type_ == 'link':
-            _wid = HTML(msg)
-            _wid.setStyleName('chatTextInfo-link')
-            if link_cb:
-                _wid.addClickListener(link_cb)
-        elif type_ == 'me':
-            _wid = Label(msg)
-            _wid.setStyleName('chatTextMe')
-        else:
-            raise ValueError("Unknown printInfo type %s" % type_)
-        self.content.add(_wid)
-        self.content_scroll.scrollToBottom()
+        if clear:
+            # FIXME: clear is not handler
+            pass
+        for message in self.messages.itervalues():
+            self.appendMessage(message)
 
-    def printMessage(self, nick, my_message, message, timestamp, extra=None, profile=C.PROF_KEY_NONE):
-        QuickChat.printMessage(self, nick, my_message, message, timestamp, extra, profile)
-        if extra is None:
-            extra = {}
-        self.content.add(ChatText(timestamp, nick, my_message, message, extra))
+    def createMessage(self, message):
+        self.appendMessage(message)
+
+    def appendMessage(self, message):
+        self.content.add(MessageWidget(message))
         self.content_scroll.scrollToBottom()
 
     def notify(self, contact="somebody", msg=""):
@@ -254,12 +264,12 @@
         """
         self.host.notification.notify(contact, msg)
 
-    def printDayChange(self, day):
-        """Display the day on a new line.
+    # def printDayChange(self, day):
+    #     """Display the day on a new line.
 
-        @param day(unicode): day to display (or not if this method is not overwritten)
-        """
-        self.printInfo("* " + day)
+    #     @param day(unicode): day to display (or not if this method is not overwritten)
+    #     """
+    #     self.printInfo("* " + day)
 
     def setTitle(self, title=None, extra=None):
         """Refresh the title of this Chat dialog