Mercurial > libervia-desktop-kivy
diff cagou/plugins/plugin_wid_chat.py @ 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 | ffdf2390ea56 |
children | 642cd2435e26 |
line wrap: on
line diff
--- 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) )