comparison cagou/plugins/plugin_wid_chat.py @ 435:53bb3886b408

chat: generate preview if there is not one already for non encrypted attachments
author Goffi <goffi@goffi.org>
date Sun, 01 Mar 2020 22:11:25 +0100
parents aa204c813f07
children b5e6d36fbf9c
comparison
equal deleted inserted replaced
434:977158b56ce6 435:53bb3886b408
140 140
141 def _setPreview(self, attachment, wid, preview_path): 141 def _setPreview(self, attachment, wid, preview_path):
142 attachment['preview'] = preview_path 142 attachment['preview'] = preview_path
143 wid.source = preview_path 143 wid.source = preview_path
144 144
145 def _setDecryptedPath(self, attachment, wid, path): 145 def _setPath(self, attachment, wid, path):
146 attachment['path'] = path 146 attachment['path'] = path
147 if wid is not None: 147 if wid is not None:
148 # we also need a preview for the widget 148 # we also need a preview for the widget
149 if 'preview' in attachment: 149 if 'preview' in attachment:
150 wid.source = attachment['preview'] 150 wid.source = attachment['preview']
158 def on_kv_post(self, __): 158 def on_kv_post(self, __):
159 attachments = self.attachments 159 attachments = self.attachments
160 self.clear_widgets() 160 self.clear_widgets()
161 for idx, attachment in enumerate(attachments): 161 for idx, attachment in enumerate(attachments):
162 url = attachment['url'] 162 url = attachment['url']
163 to_decrypt = url.startswith("aesgcm:") 163 if url.startswith("aesgcm:"):
164 del attachment['url']
165 # if the file is encrypted, we need to download it for decryption
166 to_download = True
167 else:
168 to_download = False
164 169
165 if idx < 3 or len(attachments) <= 4: 170 if idx < 3 or len(attachments) <= 4:
166 if ((self.mess_data.own_mess 171 if ((self.mess_data.own_mess
167 or self.chat.contact_list.isInRoster(self.mess_data.from_jid))): 172 or self.chat.contact_list.isInRoster(self.mess_data.from_jid))):
168 wid_kwargs = { 173 wid = AsyncImage(size_hint=(1, 1))
169 "size_hint": (1, 1), 174 if 'preview' in attachment:
170 } 175 wid.source = attachment["preview"]
171 if not to_decrypt: 176 elif 'path' in attachment:
172 wid_kwargs["source"] = url 177 G.host.bridge.imageGeneratePreview(
173 wid = AsyncImage(**wid_kwargs) 178 attachment['path'],
179 self.chat.profile,
180 callback=partial(self._setPreview, attachment, wid),
181 )
182 else:
183 # we'll download the file, the preview will then be generated
184 to_download = True
174 else: 185 else:
175 # we don't download automatically the image if the contact is not 186 # we don't download automatically the image if the contact is not
176 # in roster, to avoid leaking the ip 187 # in roster, to avoid leaking the ip
177 wid = Symbol(symbol="file-image") 188 wid = Symbol(symbol="file-image")
178 self.add_widget(wid) 189 self.add_widget(wid)
179 else: 190 else:
180 wid = None 191 wid = None
181 192
182 if to_decrypt: 193 if to_download:
183 # the file needs to be decrypted, it will be done by downloadURL 194 # the file needs to be downloaded, the widget source,
184 # and the widget source and attachment path will then be completed 195 # attachment path, and preview will then be completed
185 del attachment['url']
186 G.host.downloadURL( 196 G.host.downloadURL(
187 url, 197 url,
188 callback=partial(self._setDecryptedPath, attachment, wid), 198 callback=partial(self._setPath, attachment, wid),
189 dest=C.FILE_DEST_CACHE, 199 dest=C.FILE_DEST_CACHE,
190 profile=self.chat.profile, 200 profile=self.chat.profile,
191 ) 201 )
192 202
193 if len(attachments) > 4: 203 if len(attachments) > 4:
198 self.add_widget(counter) 208 self.add_widget(counter)
199 209
200 def on_press(self): 210 def on_press(self):
201 sources = [] 211 sources = []
202 for attachment in self.attachments: 212 for attachment in self.attachments:
203 source = attachment.get('path', attachment.get('url')) 213 source = attachment.get('path') or attachment.get('url')
204 if not source: 214 if not source:
205 log.warning(f"no source for {attachment}") 215 log.warning(f"no source for {attachment}")
206 else: 216 else:
207 sources.append(source) 217 sources.append(source)
208 gallery = ImagesGallery(sources=sources) 218 gallery = ImagesGallery(sources=sources)