changeset 409:2caea63ae2ab

chat: show attachments as clickable icons
author Goffi <goffi@goffi.org>
date Wed, 19 Feb 2020 09:49:21 +0100
parents 355326a3501c
children 2a58f3d756ad
files cagou/kv/simple_xhtml.kv cagou/plugins/plugin_wid_chat.kv cagou/plugins/plugin_wid_chat.py
diffstat 3 files changed, 73 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/cagou/kv/simple_xhtml.kv	Wed Feb 19 09:49:20 2020 +0100
+++ b/cagou/kv/simple_xhtml.kv	Wed Feb 19 09:49:21 2020 +0100
@@ -24,7 +24,7 @@
 <SimpleXHTMLWidgetEscapedText>:
     size_hint: 1, None
     text_size: self.width, None
-    height: self.texture_size[1]
+    height: self.texture_size[1] if self.text else 0
 
 <SimpleXHTMLWidgetText>:
     size_hint: None, None
--- a/cagou/plugins/plugin_wid_chat.kv	Wed Feb 19 09:49:20 2020 +0100
+++ b/cagou/plugins/plugin_wid_chat.kv	Wed Feb 19 09:49:21 2020 +0100
@@ -25,6 +25,27 @@
 
 # Chat
 
+
+<AttachmentItem>:
+    size_hint: None, 1
+    width: dp(100)
+    SymbolLabel:
+        symbol: root.get_symbol(root.data)
+        text: root.data.get('name', '')
+        bold: False
+        on_press: root.on_press()
+
+
+<AttachmentsLayout>:
+    attachments: attachments
+    size_hint: 1, None
+    height: dp(50)
+    BoxLayout:
+        id: attachments
+        size_hint: None, 1
+        width: self.minimum_width
+
+
 <MessAvatar>:
     size_hint: None, None
     size: dp(30), dp(30)
@@ -60,7 +81,7 @@
         size_hint: 1, None
         orientation: 'vertical'
         id: right_part
-        height: header_box.height + mess_xhtml.height
+        height: self.minimum_height
         BoxLayout:
             id: header_box
             size_hint: 1, None
@@ -94,7 +115,7 @@
             id: mess_xhtml
             size_hint: 1, None
             height: self.minimum_height
-            xhtml: root.message_xhtml or self.escape(root.message or u' ')
+            xhtml: root.message_xhtml or self.escape(root.message or u'')
             color: (0.74,0.74,0.24,1) if root.mess_type == "info" else (0, 0, 0, 1)
             padding: root.mess_padding
             bold: True if root.mess_type == "info" else False
--- a/cagou/plugins/plugin_wid_chat.py	Wed Feb 19 09:49:20 2020 +0100
+++ b/cagou/plugins/plugin_wid_chat.py	Wed Feb 19 09:49:21 2020 +0100
@@ -28,6 +28,7 @@
 from kivy.metrics import sp, dp
 from kivy.clock import Clock
 from kivy import properties
+from kivy.uix.scrollview import ScrollView
 from kivy.uix.dropdown import DropDown
 from kivy.core.window import Window
 from sat.core import log as logging
@@ -76,6 +77,33 @@
 INFINITE_SCROLL_LIMIT = dp(600)
 
 
+class AttachmentsLayout(ScrollView):
+    attachments = properties.ObjectProperty()
+
+
+class AttachmentItem(BoxLayout):
+    data = properties.DictProperty()
+
+    def get_symbol(self, data):
+        media_type = data.get('media_type', '')
+        main_type = media_type.split('/', 1)[0]
+        if main_type == 'image':
+            return "file-image"
+        elif main_type == 'video':
+            return "file-video"
+        elif main_type == 'audio':
+            return "file-audio"
+        else:
+            return "doc"
+
+    def on_press(self):
+        url = self.data.get('url')
+        if url:
+            G.local_platform.open_url(url, self)
+        else:
+            log.warning("can't find URL in {self.data}")
+
+
 class MessAvatar(ButtonBehavior, Image):
     pass
 
@@ -90,8 +118,13 @@
     right_part = properties.ObjectProperty()
     header_box = properties.ObjectProperty()
 
-    def on_mess_data(self, wid, mess_data):
-        mess_data.widgets.add(self)
+    def on_kv_post(self, __):
+        if not self.mess_data:
+            raise exceptions.InternalError(
+                "mess_data must always be set in MessageWidget")
+
+        self.mess_data.widgets.add(self)
+        self.add_attachments()
 
     @property
     def chat(self):
@@ -145,6 +178,18 @@
             status = update_dict['status']
             self.delivery.text =  '\u2714' if status == 'delivered' else ''
 
+    def add_attachments(self):
+        """Add attachments layout + attachments item"""
+        attachments = self.mess_data.attachments
+        if not attachments:
+            return
+        root_layout = AttachmentsLayout()
+        self.right_part.add_widget(root_layout)
+        layout = root_layout.attachments
+        for attachment in attachments:
+            item = AttachmentItem(data=attachment)
+            layout.add_widget(item)
+
 
 class SendButton(SymbolButton):
     message_input_box = properties.ObjectProperty()
@@ -855,9 +900,10 @@
         # TODO: factorise with QuickChat._historyGetCb
         scroll_start_height = self.messages_widget.height * self.history_scroll.scroll_y
         for data in reversed(history):
-            uid, timestamp, from_jid, to_jid, message, subject, type_, extra = data
+            uid, timestamp, from_jid, to_jid, message, subject, type_, extra_s = data
             from_jid = jid.JID(from_jid)
             to_jid = jid.JID(to_jid)
+            extra = data_format.deserialise(extra_s)
             extra["history"] = True
             self.messages[uid] = message = quick_chat.Message(
                 self,