Mercurial > libervia-desktop-kivy
comparison 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 |
comparison
equal
deleted
inserted
replaced
447:f3296a7f35f3 | 448:20a807443c3f |
---|---|
19 | 19 |
20 from functools import partial | 20 from functools import partial |
21 from pathlib import Path | 21 from pathlib import Path |
22 import sys | 22 import sys |
23 import uuid | 23 import uuid |
24 import mimetypes | |
24 from urllib.parse import urlparse | 25 from urllib.parse import urlparse |
25 from kivy.uix.boxlayout import BoxLayout | 26 from kivy.uix.boxlayout import BoxLayout |
26 from kivy.uix.gridlayout import GridLayout | 27 from kivy.uix.gridlayout import GridLayout |
27 from kivy.uix.screenmanager import Screen, NoTransition | 28 from kivy.uix.screenmanager import Screen, NoTransition |
28 from kivy.uix.textinput import TextInput | 29 from kivy.uix.textinput import TextInput |
93 | 94 |
94 | 95 |
95 class AttachmentsToSend(BoxLayout): | 96 class AttachmentsToSend(BoxLayout): |
96 """Layout for attachments to be sent with current message""" | 97 """Layout for attachments to be sent with current message""" |
97 attachments = properties.ObjectProperty() | 98 attachments = properties.ObjectProperty() |
99 reduce_checkbox = properties.ObjectProperty() | |
100 show_resize = properties.BooleanProperty(False) | |
101 | |
102 def on_kv_post(self, __): | |
103 self.attachments.bind(children=self.onAttachment) | |
104 | |
105 def onAttachment(self, __, attachments): | |
106 if len(attachments) == 0: | |
107 self.show_resize = False | |
98 | 108 |
99 | 109 |
100 class BaseAttachmentItem(BoxLayout): | 110 class BaseAttachmentItem(BoxLayout): |
101 data = properties.DictProperty() | 111 data = properties.DictProperty() |
102 progress = properties.NumericProperty(0) | 112 progress = properties.NumericProperty(0) |
972 attachments = extra.setdefault(C.MESS_KEY_ATTACHMENTS, []) | 982 attachments = extra.setdefault(C.MESS_KEY_ATTACHMENTS, []) |
973 attachment = { | 983 attachment = { |
974 "path": str(item.data["path"]), | 984 "path": str(item.data["path"]), |
975 "progress_id": progress_id, | 985 "progress_id": progress_id, |
976 } | 986 } |
987 if 'media_type' in item.data: | |
988 attachment[C.MESS_KEY_ATTACHMENTS_MEDIA_TYPE] = item.data['media_type'] | |
989 | |
990 if ((self.attachments_to_send.reduce_checkbox.active | |
991 and attachment.get('media_type', '').split('/')[0] == 'image')): | |
992 attachment[C.MESS_KEY_ATTACHMENTS_RESIZE] = True | |
993 | |
977 attachments.append(attachment) | 994 attachments.append(attachment) |
978 | 995 |
979 Clock.schedule_once( | 996 Clock.schedule_once( |
980 partial(self._attachmentProgressUpdate, item), | 997 partial(self._attachmentProgressUpdate, item), |
981 PROGRESS_UPDATE) | 998 PROGRESS_UPDATE) |
997 extra=extra, | 1014 extra=extra, |
998 profile_key=self.profile | 1015 profile_key=self.profile |
999 ) | 1016 ) |
1000 input_widget.text = '' | 1017 input_widget.text = '' |
1001 | 1018 |
1002 def addAttachment(self, file_path): | 1019 def _imageCheckCb(self, report_raw): |
1020 report = data_format.deserialise(report_raw) | |
1021 if report['too_large']: | |
1022 self.attachments_to_send.show_resize=True | |
1023 self.attachments_to_send.reduce_checkbox.active=True | |
1024 | |
1025 def addAttachment(self, file_path, media_type=None): | |
1003 file_path = Path(file_path) | 1026 file_path = Path(file_path) |
1027 if media_type is None: | |
1028 media_type = mimetypes.guess_type(file_path, strict=False)[0] | |
1029 if not self.attachments_to_send.show_resize and media_type is not None: | |
1030 # we check if the attachment is an image and if it's too large. | |
1031 # If too large, the reduce size check box will be displayed, and checked by | |
1032 # default. | |
1033 main_type = media_type.split('/')[0] | |
1034 if main_type == "image": | |
1035 G.host.bridge.imageCheck( | |
1036 str(file_path), | |
1037 callback=self._imageCheckCb, | |
1038 errback=partial( | |
1039 G.host.errback, | |
1040 title=_("Can't check image size"), | |
1041 message=_("Can't check image at {path}: {{msg}}").format( | |
1042 path=file_path), | |
1043 ) | |
1044 ) | |
1045 | |
1004 data = { | 1046 data = { |
1005 "path": file_path, | 1047 "path": file_path, |
1006 "name": file_path.name, | 1048 "name": file_path.name, |
1007 } | 1049 } |
1050 | |
1051 if media_type is not None: | |
1052 data['media_type'] = media_type | |
1053 | |
1008 self.attachments_to_send.attachments.add_widget( | 1054 self.attachments_to_send.attachments.add_widget( |
1009 AttachmentToSendItem(data=data) | 1055 AttachmentToSendItem(data=data) |
1010 ) | 1056 ) |
1011 | 1057 |
1012 def transferFile(self, file_path, transfer_type=C.TRANSFER_UPLOAD, cleaning_cb=None): | 1058 def transferFile(self, file_path, transfer_type=C.TRANSFER_UPLOAD, cleaning_cb=None): |