annotate cagou/core/share_widget.py @ 364:a1f3af7c0b67

android (p4a): added `hyperlink`, `requests` and `treq` requirements
author Goffi <goffi@goffi.org>
date Mon, 27 Jan 2020 21:17:08 +0100
parents f20ec8462493
children 4d660b252487
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
348
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
1 #!/usr/bin/env python3
336
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
2
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2016-2019 Jérôme Poisson (goffi@goffi.org)
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
5
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
10
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
15
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
18
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
19
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from pathlib import Path
348
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
21 from functools import partial
336
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core import log as logging
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from sat.core.i18n import _
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat.tools.common import data_format
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat_frontends.tools import jid
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from kivy.uix.boxlayout import BoxLayout
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from kivy.properties import StringProperty, DictProperty, ObjectProperty
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from kivy.metrics import dp
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from .constants import Const as C
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from cagou import G
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
31
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
32
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
33 log = logging.getLogger(__name__)
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
34
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
35
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
36 PLUGIN_INFO = {
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
37 "name": _("share"),
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
38 "main": "Share",
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
39 "description": _("share a file"),
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
40 "icon_symbol": "share",
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
41 }
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
42
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
43
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
44 class TextPreview(BoxLayout):
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
45 """Widget previewing shared text"""
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
46 text = StringProperty()
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
47
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
48
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
49 class ImagePreview(BoxLayout):
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
50 """Widget previewing shared image"""
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
51 path = StringProperty()
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
52 reduce_layout = ObjectProperty()
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
53 reduce_checkbox = ObjectProperty()
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
54
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
55 def _checkImageCb(self, report_raw):
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
56 self.report = data_format.deserialise(report_raw)
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
57 if self.report['too_large']:
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
58 self.reduce_layout.opacity = 1
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
59 self.reduce_layout.height = self.reduce_layout.minimum_height + dp(10)
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
60 self.reduce_layout.padding = [0, dp(5)]
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
61
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
62 def _checkImageEb(self, failure_):
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
63 log.error(f"Can't check image: {failure_}")
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
64
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
65 def on_path(self, wid, path):
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
66 G.host.bridge.imageCheck(
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
67 path, callback=self._checkImageCb, errback=self._checkImageEb)
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
68
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
69 def resizeImage(self, data, callback, errback):
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
70
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
71 def imageResizeCb(new_path):
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
72 new_path = Path(new_path)
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
73 log.debug(f"image {data['path']} resized at {new_path}")
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
74 data['path'] = new_path
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
75 data['cleaning_cb'] = lambda: new_path.unlink()
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
76 callback(data)
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
77
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
78 path = data['path']
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
79 width, height = self.report['recommended_size']
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
80 G.host.bridge.imageResize(
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
81 path, width, height,
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
82 callback=imageResizeCb,
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
83 errback=errback
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
84 )
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
85
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
86 def getFilter(self):
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
87 if self.report['too_large'] and self.reduce_checkbox.active:
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
88 return self.resizeImage
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
89 else:
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
90 return lambda data, callback, errback: callback(data)
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
91
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
92
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
93 class GenericPreview(BoxLayout):
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
94 """Widget previewing shared image"""
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
95 path = StringProperty()
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
96
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
97
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
98 class ShareWidget(BoxLayout):
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
99 media_type = StringProperty()
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
100 data = DictProperty()
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
101 preview_box = ObjectProperty()
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
102
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
103 def on_kv_post(self, wid):
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
104 self.type, self.subtype = self.media_type.split('/')
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
105 if self.type == 'text':
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
106 self.preview_box.add_widget(TextPreview(text=self.data['text']))
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
107 elif self.type == 'image':
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
108 self.preview_box.add_widget(ImagePreview(path=self.data['path']))
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
109 else:
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
110 self.preview_box.add_widget(GenericPreview(path=self.data['path']))
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
111
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
112 def close(self):
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
113 G.host.closeUI()
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
114
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
115 def getFilteredData(self, callback, errback):
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
116 """Apply filter if suitable, and call callback with with modified data"""
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
117 try:
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
118 getFilter = self.preview_box.children[0].getFilter
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
119 except AttributeError:
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
120 callback(self.data)
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
121 else:
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
122 filter_ = getFilter()
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
123 filter_(self.data, callback=callback, errback=errback)
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
124
348
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
125 def filterDataCb(self, data, contact_jid, profile):
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
126 G.host.doAction('chat', contact_jid, [profile])
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
127 chat_wid = G.host.selected_widget
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
128
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
129 if self.type == 'text':
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
130 text = self.data['text']
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
131 chat_wid.message_input.text += text
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
132 else:
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
133 path = self.data['path']
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
134 chat_wid.transferFile(path, cleaning_cb=data.get('cleaning_cb'))
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
135 self.close()
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
136
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
137 def filterDataEb(self, failure_):
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
138 G.host.addNote(
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
139 _("file filter error"),
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
140 _("Can't apply filter to file: {msg}").format(msg=failure_),
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
141 level=C.XMLUI_DATA_LVL_ERROR)
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
142
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
143 def on_select(self, contact_button):
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
144 contact_jid = jid.JID(contact_button.jid)
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
145 self.getFilteredData(
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
146 partial(
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
147 self.filterDataCb,
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
148 contact_jid=contact_jid,
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
149 profile=contact_button.profile),
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
150 self.filterDataEb
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
151 )
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 336
diff changeset
152
336
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
153 def key_input(self, window, key, scancode, codepoint, modifier):
b0c9017a1db7 core: added forgotten share_widget module
Goffi <goffi@goffi.org>
parents:
diff changeset
154 if key == 27:
357
4d3a0c4f2430 core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents: 348
diff changeset
155 return G.local_platform.on_key_back_share(self)