Daniel Brötzmann pushed to branch master at gajim / gajim
Commits: 33cbaadc by wurstsalat at 2021-12-18T22:56:55+01:00 Multimedia helpers: Typing
- - - - - 6b58d0d0 by wurstsalat at 2021-12-19T00:22:06+01:00 Preview: Add download progress
- - - - -
4 changed files:
- gajim/common/multimedia_helpers.py - gajim/common/preview.py - gajim/data/gui/preview.ui - gajim/gtk/preview.py
Changes:
===================================== gajim/common/multimedia_helpers.py ===================================== @@ -14,6 +14,10 @@ # You should have received a copy of the GNU General Public License # along with Gajim. If not, see http://www.gnu.org/licenses/.
+from __future__ import annotations +from typing import Dict +from typing import Optional + import logging
try: @@ -27,23 +31,25 @@
class DeviceManager: - def __init__(self): - self.devices = {} + def __init__(self) -> None: + self.devices: Dict[str, str] = {}
- def detect(self): + def detect(self) -> None: self.devices = {}
- def get_devices(self): + def get_devices(self) -> Dict[str, str]: if not self.devices: self.detect() return self.devices
- def detect_element(self, name, text, pipe='%s'): + def detect_element(self, name: str, text: str, pipe: str = '%s') -> None: if Gst.ElementFactory.find(name): - element = Gst.ElementFactory.make(name, '%spresencetest' % name) + element: Optional[Gst.Element] = Gst.ElementFactory.make( + name, f'{name}presencetest') if element is None: log.warning('could not create %spresencetest', name) return + if hasattr(element.props, 'device'): element.set_state(Gst.State.READY) devices = element.get_properties('device') @@ -57,7 +63,7 @@ def detect_element(self, name, text, pipe='%s'): element.set_state(Gst.State.READY) device_name = element.get_property('device-name') self.devices[text % device_name] = pipe % \ - '%s device=%s' % (name, device) + f'{name} device={device}' element.set_state(Gst.State.NULL) else: self.devices[text] = pipe % name @@ -66,8 +72,8 @@ def detect_element(self, name, text, pipe='%s'):
class AudioInputManager(DeviceManager): - def detect(self): - self.devices = {} + def detect(self) -> None: + self.devices: Dict[str, str] = {} # Test src self.detect_element('audiotestsrc', _('Audio test'), '%s is-live=true name=gajim_vol') @@ -83,8 +89,8 @@ def detect(self):
class AudioOutputManager(DeviceManager): - def detect(self): - self.devices = {} + def detect(self) -> None: + self.devices: Dict[str, str] = {} # Fake sink self.detect_element('fakesink', _('Fake audio output')) # Auto sink @@ -96,8 +102,8 @@ def detect(self):
class VideoInputManager(DeviceManager): - def detect(self): - self.devices = {} + def detect(self) -> None: + self.devices: Dict[str, str] = {} # Test src self.detect_element('videotestsrc', _('Video test'), '%s is-live=true ! video/x-raw,framerate=10/1 ! videoconvert')
===================================== gajim/common/preview.py ===================================== @@ -78,6 +78,7 @@ def __init__(self, self.thumbnail: Optional[bytes] = None self.mime_type: str = '' self.file_size: int = 0 + self._received_size: int = 0
self.key: Optional[bytes] = None self.iv: Optional[bytes] = None @@ -149,6 +150,14 @@ def create_thumbnail(self, data: bytes) -> bool: def update_widget(self, data: Optional[GdkPixbufType] = None) -> None: self._widget.update(self, data)
+ def update_progress(self, size: int) -> None: + self._received_size += size + if self.file_size == 0 or self._received_size == 0: + return + + progress = self._received_size / self.file_size + self._widget.update_progress(self, progress) +
class PreviewManager: def __init__(self) -> None: @@ -361,6 +370,7 @@ def download_content(self, message.connect('starting', self._check_certificate, preview) message.connect( 'content-sniffed', self._on_content_sniffed, preview, force) + message.connect('got-chunk', self._on_got_chunk, preview)
session = self._get_session(preview.account) session.queue_message(message, self._on_finished, preview) @@ -407,6 +417,13 @@ def _on_content_sniffed(self,
preview.update_widget()
+ def _on_got_chunk(self, + _message: Soup.Message, + chunk: Soup.Buffer, + preview: Preview + ) -> None: + preview.update_progress(len(chunk.get_data())) + def _on_finished(self, _session: Soup.Session, message: Soup.Message,
===================================== gajim/data/gui/preview.ui ===================================== @@ -42,6 +42,47 @@ <property name="can-focus">False</property> <property name="orientation">vertical</property> <property name="spacing">3</property> + <child> + <object class="GtkEventBox" id="image_event_box"> + <property name="visible">True</property> + <property name="can-focus">False</property> + <property name="halign">center</property> + <signal name="realize" handler="_on_realize" swapped="no"/> + <child> + <object class="GtkButton" id="image_button"> + <property name="visible">True</property> + <property name="can-focus">True</property> + <property name="receives-default">False</property> + <property name="relief">none</property> + <signal name="button-press-event" handler="_on_button_press_event" swapped="no"/> + <signal name="clicked" handler="_on_content_button_clicked" swapped="no"/> + <child> + <placeholder/> + </child> + <style> + <class name="preview-image-button"/> + </style> + </object> + </child> + </object> + <packing> + <property name="expand">False</property> + <property name="fill">True</property> + <property name="position">0</property> + </packing> + </child> + <child> + <object class="GtkProgressBar" id="progressbar"> + <property name="can-focus">False</property> + <property name="no-show-all">True</property> + <property name="show-text">True</property> + </object> + <packing> + <property name="expand">False</property> + <property name="fill">True</property> + <property name="position">1</property> + </packing> + </child> <child> <object class="GtkBox" id="button_box"> <property name="visible">True</property> @@ -207,36 +248,7 @@ <property name="expand">False</property> <property name="fill">True</property> <property name="pack-type">end</property> - <property name="position">0</property> - </packing> - </child> - <child> - <object class="GtkEventBox" id="image_event_box"> - <property name="visible">True</property> - <property name="can-focus">False</property> - <property name="halign">center</property> - <signal name="realize" handler="_on_realize" swapped="no"/> - <child> - <object class="GtkButton" id="image_button"> - <property name="visible">True</property> - <property name="can-focus">True</property> - <property name="receives-default">False</property> - <property name="relief">none</property> - <signal name="button-press-event" handler="_on_button_press_event" swapped="no"/> - <signal name="clicked" handler="_on_content_button_clicked" swapped="no"/> - <child> - <placeholder/> - </child> - <style> - <class name="preview-image-button"/> - </style> - </object> - </child> - </object> - <packing> - <property name="expand">False</property> - <property name="fill">True</property> - <property name="position">1</property> + <property name="position">2</property> </packing> </child> </object>
===================================== gajim/gtk/preview.py ===================================== @@ -66,8 +66,13 @@ def get_text(self) -> str: return '' return self._preview.uri
+ def update_progress(self, _preview: Preview, progress: float) -> None: + self._ui.progressbar.show() + self._ui.progressbar.set_fraction(progress) + def update(self, preview: Preview, data: Optional[GdkPixbufType]) -> None: self._preview = preview + self._ui.progressbar.hide()
if preview.is_geo_uri: data = load_icon('map',
View it on GitLab: https://dev.gajim.org/gajim/gajim/-/compare/6b22517505571cf8a4a0ae1e92c8eca5...