comparison libervia/desktop_kivy/core/share_widget.py @ 493:b3cedbee561d

refactoring: rename `cagou` to `libervia.desktop_kivy` + update imports and names following backend changes
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 18:26:16 +0200
parents cagou/core/share_widget.py@203755bbe0fe
children
comparison
equal deleted inserted replaced
492:5114bbb5daa3 493:b3cedbee561d
1 #!/usr/bin/env python3
2
3 #Libervia Desktop-Kivy
4 # Copyright (C) 2016-2021 Jérôme Poisson (goffi@goffi.org)
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Affero General Public License for more details.
15
16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19
20 from pathlib import Path
21 from functools import partial
22 from libervia.backend.core import log as logging
23 from libervia.backend.core.i18n import _
24 from libervia.backend.tools.common import data_format
25 from libervia.frontends.tools import jid
26 from kivy.uix.boxlayout import BoxLayout
27 from kivy.properties import StringProperty, DictProperty, ObjectProperty
28 from kivy.metrics import dp
29 from .constants import Const as C
30 from libervia.desktop_kivy import G
31
32
33 log = logging.getLogger(__name__)
34
35
36 PLUGIN_INFO = {
37 "name": _("share"),
38 "main": "Share",
39 "description": _("share a file"),
40 "icon_symbol": "share",
41 }
42
43
44 class TextPreview(BoxLayout):
45 """Widget previewing shared text"""
46 text = StringProperty()
47
48
49 class ImagePreview(BoxLayout):
50 """Widget previewing shared image"""
51 path = StringProperty()
52 reduce_layout = ObjectProperty()
53 reduce_checkbox = ObjectProperty()
54
55 def _check_image_cb(self, report_raw):
56 self.report = data_format.deserialise(report_raw)
57 if self.report['too_large']:
58 self.reduce_layout.opacity = 1
59 self.reduce_layout.height = self.reduce_layout.minimum_height + dp(10)
60 self.reduce_layout.padding = [0, dp(5)]
61
62 def _check_image_eb(self, failure_):
63 log.error(f"Can't check image: {failure_}")
64
65 def on_path(self, wid, path):
66 G.host.bridge.image_check(
67 path, callback=self._check_image_cb, errback=self._check_image_eb)
68
69 def resize_image(self, data, callback, errback):
70
71 def image_resize_cb(new_path):
72 new_path = Path(new_path)
73 log.debug(f"image {data['path']} resized at {new_path}")
74 data['path'] = new_path
75 data['cleaning_cb'] = lambda: new_path.unlink()
76 callback(data)
77
78 path = data['path']
79 width, height = self.report['recommended_size']
80 G.host.bridge.image_resize(
81 path, width, height,
82 callback=image_resize_cb,
83 errback=errback
84 )
85
86 def get_filter(self):
87 if self.report['too_large'] and self.reduce_checkbox.active:
88 return self.resize_image
89 else:
90 return lambda data, callback, errback: callback(data)
91
92
93 class GenericPreview(BoxLayout):
94 """Widget previewing shared image"""
95 path = StringProperty()
96
97
98 class ShareWidget(BoxLayout):
99 media_type = StringProperty()
100 data = DictProperty()
101 preview_box = ObjectProperty()
102
103 def on_kv_post(self, wid):
104 self.type, self.subtype = self.media_type.split('/')
105 if self.type == 'text' and 'text' in self.data:
106 self.preview_box.add_widget(TextPreview(text=self.data['text']))
107 elif self.type == 'image':
108 self.preview_box.add_widget(ImagePreview(path=self.data['path']))
109 else:
110 self.preview_box.add_widget(GenericPreview(path=self.data['path']))
111
112 def close(self):
113 G.host.close_ui()
114
115 def get_filtered_data(self, callback, errback):
116 """Apply filter if suitable, and call callback with with modified data"""
117 try:
118 get_filter = self.preview_box.children[0].get_filter
119 except AttributeError:
120 callback(self.data)
121 else:
122 filter_ = get_filter()
123 filter_(self.data, callback=callback, errback=errback)
124
125 def filter_data_cb(self, data, contact_jid, profile):
126 chat_wid = G.host.do_action('chat', contact_jid, [profile])
127
128 if self.type == 'text' and 'text' in self.data:
129 text = self.data['text']
130 chat_wid.message_input.text += text
131 else:
132 path = self.data['path']
133 chat_wid.transfer_file(path, cleaning_cb=data.get('cleaning_cb'))
134 self.close()
135
136 def filter_data_eb(self, failure_):
137 G.host.add_note(
138 _("file filter error"),
139 _("Can't apply filter to file: {msg}").format(msg=failure_),
140 level=C.XMLUI_DATA_LVL_ERROR)
141
142 def on_select(self, contact_button):
143 contact_jid = jid.JID(contact_button.jid)
144 self.get_filtered_data(
145 partial(
146 self.filter_data_cb,
147 contact_jid=contact_jid,
148 profile=contact_button.profile),
149 self.filter_data_eb
150 )
151
152 def key_input(self, window, key, scancode, codepoint, modifier):
153 if key == 27:
154 return G.local_platform.on_key_back_share(self)