changeset 448:20a807443c3f

chat: resize attachments (images only for now): if attachments to send contain oversized image, a checkbox will be shown (activated by default) to reduce automatically the size. The background color now cover the whole attachments to send widget. If not already specified, media type is guessed from filename when adding an attachment.
author Goffi <goffi@goffi.org>
date Sun, 22 Mar 2020 14:10:59 +0100
parents f3296a7f35f3
children 6c21a5a44b54
files cagou/plugins/plugin_wid_chat.kv cagou/plugins/plugin_wid_chat.py
diffstat 2 files changed, 74 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/cagou/plugins/plugin_wid_chat.kv	Wed Mar 18 20:26:21 2020 +0100
+++ b/cagou/plugins/plugin_wid_chat.kv	Sun Mar 22 14:10:59 2020 +0100
@@ -172,11 +172,18 @@
 
 <AttachmentsToSend>:
     attachments: attachments_layout.attachments
+    reduce_checkbox: reduce_checkbox
     orientation: "vertical"
     size_hint: 1, None
     height: self.minimum_height if self.attachments.children else 0
     opacity: 1 if self.attachments.children else 0
     padding: [app.MARGIN_LEFT, dp(5), app.MARGIN_RIGHT, dp(5)]
+    canvas.before:
+        Color:
+            rgba: app.c_prim
+        Rectangle:
+            pos: self.pos
+            size: self.size
     Label:
         size_hint: 1, None
         size: self.texture_size
@@ -184,13 +191,26 @@
         bold: True
     AttachmentsLayout:
         id: attachments_layout
-        canvas.before:
-            Color:
-                rgba: app.c_prim
-            Rectangle:
-                pos: self.pos
-                size: self.size
-
+    BoxLayout:
+        id: resize_box
+        size_hint: 1, None
+        opacity: 1 if root.show_resize else 0
+        height: dp(25) if root.show_resize else 0
+        Widget:
+        CheckBox:
+            id: reduce_checkbox
+            size_hint: None, 1
+            width: dp(20)
+            active: True
+        Label:
+            size_hint: None, 1
+            text: _("reduce images size")
+            text_size: None, None
+            size: self.texture_size
+            valign: "middle"
+            padding_x: dp(10)
+            font_size: sp(15)
+        Widget:
 
 <Chat>:
     attachments_to_send: attachments_to_send
--- a/cagou/plugins/plugin_wid_chat.py	Wed Mar 18 20:26:21 2020 +0100
+++ b/cagou/plugins/plugin_wid_chat.py	Sun Mar 22 14:10:59 2020 +0100
@@ -21,6 +21,7 @@
 from pathlib import Path
 import sys
 import uuid
+import mimetypes
 from urllib.parse import urlparse
 from kivy.uix.boxlayout import BoxLayout
 from kivy.uix.gridlayout import GridLayout
@@ -95,6 +96,15 @@
 class AttachmentsToSend(BoxLayout):
     """Layout for attachments to be sent with current message"""
     attachments = properties.ObjectProperty()
+    reduce_checkbox = properties.ObjectProperty()
+    show_resize = properties.BooleanProperty(False)
+
+    def on_kv_post(self, __):
+        self.attachments.bind(children=self.onAttachment)
+
+    def onAttachment(self, __, attachments):
+        if len(attachments) == 0:
+            self.show_resize = False
 
 
 class BaseAttachmentItem(BoxLayout):
@@ -974,6 +984,13 @@
                 "path": str(item.data["path"]),
                 "progress_id": progress_id,
             }
+            if 'media_type' in item.data:
+                attachment[C.MESS_KEY_ATTACHMENTS_MEDIA_TYPE] = item.data['media_type']
+
+            if ((self.attachments_to_send.reduce_checkbox.active
+                 and attachment.get('media_type', '').split('/')[0] == 'image')):
+                attachment[C.MESS_KEY_ATTACHMENTS_RESIZE] = True
+
             attachments.append(attachment)
 
             Clock.schedule_once(
@@ -999,12 +1016,41 @@
             )
         input_widget.text = ''
 
-    def addAttachment(self, file_path):
+    def _imageCheckCb(self, report_raw):
+        report = data_format.deserialise(report_raw)
+        if report['too_large']:
+            self.attachments_to_send.show_resize=True
+            self.attachments_to_send.reduce_checkbox.active=True
+
+    def addAttachment(self, file_path, media_type=None):
         file_path = Path(file_path)
+        if media_type is None:
+            media_type = mimetypes.guess_type(file_path, strict=False)[0]
+        if not self.attachments_to_send.show_resize and media_type is not None:
+            # we check if the attachment is an image and if it's too large.
+            # If too large, the reduce size check box will be displayed, and checked by
+            # default.
+            main_type = media_type.split('/')[0]
+            if main_type == "image":
+                G.host.bridge.imageCheck(
+                    str(file_path),
+                    callback=self._imageCheckCb,
+                    errback=partial(
+                        G.host.errback,
+                        title=_("Can't check image size"),
+                        message=_("Can't check image at {path}: {{msg}}").format(
+                            path=file_path),
+                    )
+                )
+
         data = {
             "path": file_path,
             "name": file_path.name,
         }
+
+        if media_type is not None:
+            data['media_type'] = media_type
+
         self.attachments_to_send.attachments.add_widget(
             AttachmentToSendItem(data=data)
         )