Daniel Brötzmann pushed to branch mainwindow at gajim / gajim
Commits: 4dc79ca4 by wurstsalat at 2021-11-21T12:30:12+01:00 Preview: Typing
- - - - -
5 changed files:
- gajim/common/preview.py - gajim/common/preview_helpers.py - gajim/common/types.py - gajim/gtk/preview.py - gajim/gtk/preview_audio.py
Changes:
===================================== gajim/common/preview.py ===================================== @@ -23,7 +23,6 @@ from urllib.parse import urlparse from urllib.parse import ParseResult
-from gi.repository import GdkPixbuf from gi.repository import Gio from gi.repository import GLib from gi.repository import Soup @@ -45,6 +44,7 @@ from gajim.common.preview_helpers import get_image_paths from gajim.common.preview_helpers import guess_mime_type from gajim.common.preview_helpers import pixbuf_from_data +from gajim.common.types import GdkPixbufType
log = logging.getLogger('gajim.c.preview')
@@ -55,7 +55,7 @@
class Preview: - def __init__(self, + def __init__(self, uri: str, urlparts: Optional[ParseResult], orig_path: Optional[Path], @@ -139,7 +139,7 @@ def create_thumbnail(self, data: bytes) -> bool: return False return True
- def update_widget(self, data: Optional[GdkPixbuf.Pixbuf] = None) -> None: + def update_widget(self, data: Optional[GdkPixbufType] = None) -> None: self._widget.update(self, data)
===================================== gajim/common/preview_helpers.py ===================================== @@ -285,12 +285,9 @@ def get_image_paths(uri: str, urlparts: ParseResult, size: int, orig_dir: Path,
name_hash = hashlib.sha1(str(uri).encode()).hexdigest()
- orig_filename = '%s_%s%s' % (web_stem, name_hash, extension) + orig_filename = f'{web_stem}_{name_hash}{extension}'
- thumb_filename = '%s_%s_thumb_%s%s' % (web_stem, - name_hash, - size, - extension) + thumb_filename = f'{web_stem}_{name_hash}_thumb_{size}{extension}'
orig_path = orig_dir / orig_filename thumb_path = thumb_dir / thumb_filename
===================================== gajim/common/types.py ===================================== @@ -23,6 +23,9 @@ from typing import TYPE_CHECKING
from pathlib import Path + +from gi.repository import GdkPixbuf + import nbxmpp from nbxmpp.protocol import JID from nbxmpp.structs import BookmarkData @@ -61,9 +64,12 @@ # Plugins PluginExtensionPoints = Dict[str, Tuple[Optional[Callable[..., None]], Optional[Callable[..., None]]]] -EventHandlersDict = Dict[str, Tuple[int, Callable[['NetworkEvent'], Optional[bool]]]] +EventHandlersDict = Dict[ + str, Tuple[int, Callable[['NetworkEvent'], Optional[bool]]]] PluginEvents = List['NetworkEvent']
SettingsT = Union['Settings']
BookmarksDict = Dict[JID, BookmarkData] + +GdkPixbufType = Union[GdkPixbuf.Pixbuf, GdkPixbuf.PixbufAnimation]
===================================== gajim/gtk/preview.py ===================================== @@ -12,6 +12,8 @@ # You should have received a copy of the GNU General Public License # along with Gajim. If not, see http://www.gnu.org/licenses/.
+from typing import Optional + import logging import shutil import os @@ -26,9 +28,11 @@ from gajim.common.helpers import open_file from gajim.common.helpers import open_uri from gajim.common.i18n import _ +from gajim.common.preview import Preview from gajim.common.preview_helpers import split_geo_uri from gajim.common.preview_helpers import contains_audio_streams from gajim.common.preview_helpers import get_icon_for_mime_type +from gajim.common.types import GdkPixbufType
from .dialogs import ErrorDialog from .filechoosers import FileSaveDialog @@ -42,10 +46,10 @@
class PreviewWidget(Gtk.Box): - def __init__(self, account): + def __init__(self, account: str) -> None: Gtk.Box.__init__(self) self.account = account - self._preview = None + self._preview: Optional[Preview] = None
if app.settings.get('use_kib_mib'): self._units = GLib.FormatSizeFlags.IEC_UNITS @@ -57,12 +61,12 @@ def __init__(self, account): self.add(self._ui.preview_box) self.show_all()
- def get_text(self): + def get_text(self) -> str: if self._preview is None: return '' return self._preview.uri
- def update(self, preview, data): + def update(self, preview: Preview, data: Optional[GdkPixbufType]) -> None: self._preview = preview
if preview.is_geo_uri: @@ -114,7 +118,8 @@ def update(self, preview, data): self._ui.download_button.hide() self._ui.open_folder_button.show() self._ui.save_as_button.show() - if (preview.is_audio and app.is_installed('GST') and + if (preview.orig_path is not None and preview.is_audio and + app.is_installed('GST') and contains_audio_streams(preview.orig_path)): self._ui.image_button.hide() audio_widget = AudioWidget(preview.orig_path) @@ -223,7 +228,7 @@ def _on_open_link_in_browser(self, _menu):
def _on_content_button_clicked(self, _button): action = app.settings.get('preview_leftclick_action') - method = getattr(self, '_on_%s' % action) + method = getattr(self, f'_on_{action}') method(None)
def _on_button_press_event(self, _button, event):
===================================== gajim/gtk/preview_audio.py ===================================== @@ -13,6 +13,7 @@ # along with Gajim. If not, see http://www.gnu.org/licenses/.
import logging +from pathlib import Path
from gi.repository import GLib from gi.repository import Gtk @@ -26,12 +27,18 @@
class AudioWidget(Gtk.Box): - def __init__(self, file_path): + def __init__(self, file_path: Path) -> None: Gtk.Box.__init__(self, orientation=Gtk.Orientation.HORIZONTAL, spacing=6) - self._playbin = None - self._query = None - self._has_timeout = False + self._playbin = Gst.ElementFactory.make('playbin', 'bin') + if self._playbin is None: + label = Gtk.Label(label=_('Audio preview is not available')) + self.add(label) + log.debug('Could not create GST playbin') + return + + self._query = Gst.Query.new_position(Gst.Format.TIME) + self._has_timeout: bool = False
self._build_audio_widget() self._setup_audio_player(file_path) @@ -67,18 +74,13 @@ def _build_audio_widget(self): self.connect('destroy', self._on_destroy) self.show_all()
- def _setup_audio_player(self, file_path): - self._playbin = Gst.ElementFactory.make('playbin', 'bin') - if self._playbin is None: - log.debug('Could not create GST playbin') - return + def _setup_audio_player(self, file_path: Path) -> None: self._playbin.set_property('uri', f'file://{file_path}') state_return = self._playbin.set_state(Gst.State.PAUSED) if state_return == Gst.StateChangeReturn.FAILURE: log.debug('Could not setup GST playbin') return
- self._query = Gst.Query.new_position(Gst.Format.TIME) bus = self._playbin.get_bus() bus.add_signal_watch() bus.connect('message', self._on_bus_message) @@ -111,11 +113,11 @@ def _on_play_clicked(self, _button): def _on_destroy(self, _widget): self._playbin.set_state(Gst.State.NULL)
- def _get_paused(self): + def _get_paused(self) -> bool: _, state, _ = self._playbin.get_state(20) return state == Gst.State.PAUSED
- def _set_pause(self, paused): + def _set_pause(self, paused: bool) -> None: if paused: self._playbin.set_state(Gst.State.PAUSED) self._play_icon.set_from_icon_name( @@ -127,7 +129,7 @@ def _set_pause(self, paused): 'media-playback-pause-symbolic', Gtk.IconSize.BUTTON)
- def _update_seek_bar(self): + def _update_seek_bar(self) -> bool: if self._get_paused(): self._has_timeout = False return False @@ -138,7 +140,7 @@ def _update_seek_bar(self): return True
@staticmethod - def _format_audio_timestamp(_widget, ns): + def _format_audio_timestamp(_widget: Gtk.Widget, ns: int) -> str: seconds = ns / 1000000000 minutes = seconds / 60 hours = minutes / 60
View it on GitLab: https://dev.gajim.org/gajim/gajim/-/commit/4dc79ca45426e17f247a11e0aeb404d1d...