Daniel Brötzmann pushed to branch master at gajim / gajim
Commits: bfedea86 by wurstsalat at 2022-01-08T00:48:30+01:00 Preview: Simplify additional data access
- - - - - fb1ec43c by wurstsalat at 2022-01-08T00:48:51+01:00 AccountPage: Typing
- - - - - c6fc0dce by wurstsalat at 2022-01-08T00:49:16+01:00 NotificationManager: Typing
- - - - - 45c5d555 by wurstsalat at 2022-01-08T01:21:23+01:00 Util: Typing
- - - - - 5477bd03 by wurstsalat at 2022-01-08T01:21:54+01:00 Fix false exception type for GLib.Error
- - - - -
6 changed files:
- gajim/common/jingle_rtp.py - gajim/common/preview.py - gajim/gtk/account_page.py - gajim/gtk/filechoosers.py - gajim/gtk/notification_manager.py - gajim/gtk/util.py
Changes:
===================================== gajim/common/jingle_rtp.py ===================================== @@ -124,7 +124,7 @@ def make_bin_from_config(self, config_key, pipeline, text): try: gst_bin = Gst.parse_bin_from_description(pipeline, True) return gst_bin - except GLib.GError as err: + except GLib.Error as err: app.ged.raise_event( InformationEvent( conn=self.session.connection,
===================================== gajim/common/preview.py ===================================== @@ -200,10 +200,7 @@ def _create_session(account: str) -> Tuple[ def _accept_uri(urlparts: ParseResult, uri: str, additional_data: AdditionalDataDict) -> bool: - try: - oob_url = additional_data['gajim']['oob_url'] - except (KeyError, AttributeError): - oob_url = None + oob_url = additional_data.get_value('gajim', 'oob_url')
# geo if urlparts.scheme == 'geo':
===================================== gajim/gtk/account_page.py ===================================== @@ -12,12 +12,19 @@ # 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 Any + +from gi.repository import Gdk from gi.repository import Gio from gi.repository import Gtk
from gajim.common import app from gajim.common import ged from gajim.common.const import AvatarSize +from gajim.common.events import SubscribePresenceReceived +from gajim.common.events import UnsubscribedPresenceReceived +from gajim.common.events import MucInvitation +from gajim.common.events import MucDecline from gajim.common.i18n import _
from .roster import Roster @@ -84,22 +91,22 @@ def __init__(self, account: str) -> None: self.show_all() self.connect('destroy', self._on_destroy)
- def _on_destroy(self, *args): + def _on_destroy(self, *args: Any) -> None: app.check_finalize(self)
- def _on_edit_profile(self, _button): + def _on_edit_profile(self, _button: Gtk.Button) -> None: open_window('ProfileWindow', account=self._account)
- def _on_account_settings(self, _button): + def _on_account_settings(self, _button: Gtk.Button) -> None: window = open_window('AccountsWindow') window.select_account(self._account)
- def _on_search_changed(self, widget): + def _on_search_changed(self, widget: Gtk.SearchEntry) -> None: text = widget.get_text().lower() self._roster.set_search_string(text)
@staticmethod - def _on_button_release(paned, event): + def _on_button_release(paned: Gtk.Paned, event: Gdk.EventButton) -> None: if event.window != paned.get_handle_window(): return position = paned.get_position() @@ -120,22 +127,24 @@ def update(self) -> None:
self._status_selector.update()
- def _subscribe_received(self, event): + def _subscribe_received(self, event: SubscribePresenceReceived) -> None: if event.account != self._account: return self._notification_manager.add_subscription_request(event)
- def _unsubscribed_received(self, event): + def _unsubscribed_received(self, + event: UnsubscribedPresenceReceived + ) -> None: if event.account != self._account: return self._notification_manager.add_unsubscribed(event)
- def _muc_invitation_received(self, event): + def _muc_invitation_received(self, event: MucInvitation) -> None: if event.account != self._account: return self._notification_manager.add_invitation_received(event)
- def _muc_invitation_declined(self, event): + def _muc_invitation_declined(self, event: MucDecline) -> None: if event.account != self._account: return self._notification_manager.add_invitation_declined(event)
===================================== gajim/gtk/filechoosers.py ===================================== @@ -77,7 +77,7 @@ def _update_preview(self, filechooser): try: pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size( path_to_file, *self._preivew_size) - except GLib.GError: + except GLib.Error: preview.clear() return filechooser.get_preview_widget().set_from_pixbuf(pixbuf)
===================================== gajim/gtk/notification_manager.py ===================================== @@ -12,6 +12,11 @@ # 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 Any +from typing import Optional +from typing import Union + from gi.repository import Gio from gi.repository import GLib from gi.repository import Gtk @@ -23,6 +28,10 @@ from gajim.common.const import AvatarSize from gajim.common.i18n import _ from gajim.common.events import Notification +from gajim.common.events import SubscribePresenceReceived +from gajim.common.events import UnsubscribedPresenceReceived +from gajim.common.events import MucInvitation +from gajim.common.events import MucDecline from gajim.common.helpers import get_groupchat_name from gajim.common.helpers import get_muc_context
@@ -31,7 +40,7 @@
class NotificationManager(Gtk.ListBox): - def __init__(self, account): + def __init__(self, account: str) -> None: Gtk.ListBox.__init__(self) self._account = account self._client = app.get_client(account) @@ -51,10 +60,10 @@ def __init__(self, account): self._add_actions() self.connect('destroy', self._on_destroy)
- def _on_destroy(self, *args): + def _on_destroy(self, *args: Any) -> None: self._remove_actions()
- def _add_actions(self): + def _add_actions(self) -> None: actions = [ ('subscription-accept', self._on_subscription_accept), ('subscription-deny', self._on_subscription_deny), @@ -68,7 +77,7 @@ def _add_actions(self): act.connect('activate', func) app.window.add_action(act)
- def update_actions(self): + def update_actions(self) -> None: online = app.account_is_connected(self._account) blocking_support = self._client.get_module('Blocking').supported
@@ -83,7 +92,7 @@ def update_actions(self): f'subscription-report-{self._account}').set_enabled( online and blocking_support)
- def _remove_actions(self): + def _remove_actions(self) -> None: actions = [ 'subscription-accept', 'subscription-deny', @@ -93,7 +102,10 @@ def _remove_actions(self): for action in actions: app.window.remove_action(f'{action}-{self._account}')
- def _on_subscription_accept(self, _action, param): + def _on_subscription_accept(self, + _action: Gio.SimpleAction, + param: GLib.Variant + ) -> None: jid = param.get_string() row = self._get_notification_row(jid) self._client.get_module('Presence').subscribed(jid) @@ -102,38 +114,53 @@ def _on_subscription_accept(self, _action, param): if not contact.is_in_roster: open_window('AddContact', account=self._account, jid=jid, nick=contact.name) - self.remove(row) + if row is not None: + self.remove(row)
- def _on_subscription_block(self, _action, param): + def _on_subscription_block(self, + _action: Gio.SimpleAction, + param: GLib.Variant + ) -> None: jid = param.get_string() self._deny_request(jid) self._client.get_module('Blocking').block([jid]) row = self._get_notification_row(jid) - self.remove(row) + if row is not None: + self.remove(row)
- def _on_subscription_report(self, _action, param): + def _on_subscription_report(self, + _action: Gio.SimpleAction, + param: GLib.Variant + ) -> None: jid = param.get_string() self._deny_request(jid) self._client.get_module('Blocking').block([jid], report='spam') row = self._get_notification_row(jid) - self.remove(row) + if row is not None: + self.remove(row)
- def _on_subscription_deny(self, _action, param): + def _on_subscription_deny(self, + _action: Gio.SimpleAction, + param: GLib.Variant + ) -> None: jid = param.get_string() self._deny_request(jid) row = self._get_notification_row(jid) - self.remove(row) + if row is not None: + self.remove(row)
- def _deny_request(self, jid): + def _deny_request(self, jid: str) -> None: self._client.get_module('Presence').unsubscribed(jid)
- def _get_notification_row(self, jid): - for child in self.get_children(): - if child.jid == jid: - return child + def _get_notification_row(self, jid: str) -> Optional[NotificationRow]: + for row in self.get_children(): + if row.jid == jid: + return row return None
- def add_subscription_request(self, event): + def add_subscription_request(self, + event: SubscribePresenceReceived + ) -> None: row = self._get_notification_row(event.jid) if row is None: self.add(SubscriptionRequestRow( @@ -152,7 +179,9 @@ def add_subscription_request(self, event): elif row.type == 'unsubscribed': self.remove(row)
- def add_unsubscribed(self, event): + def add_unsubscribed(self, + event: UnsubscribedPresenceReceived + ) -> None: row = self._get_notification_row(event.jid) if row is None: self.add(UnsubscribedRow(self._account, event.jid)) @@ -170,8 +199,8 @@ def add_unsubscribed(self, event): elif row.type == 'subscribe': self.remove(row)
- def add_invitation_received(self, event): - row = self._get_notification_row(event.muc) + def add_invitation_received(self, event: MucInvitation) -> None: + row = self._get_notification_row(str(event.muc)) if row is None: self.add(InvitationReceivedRow(self._account, event))
@@ -191,24 +220,25 @@ def add_invitation_received(self, event): title=_('Group Chat Invitation'), text=text))
- def add_invitation_declined(self, event): - row = self._get_notification_row(event.muc) + def add_invitation_declined(self, event: MucDecline) -> None: + row = self._get_notification_row(str(event.muc)) if row is None: self.add(InvitationDeclinedRow(self._account, event))
class NotificationRow(Gtk.ListBoxRow): - def __init__(self, account, jid): + def __init__(self, account: str, jid: str) -> None: Gtk.ListBoxRow.__init__(self) self._account = account self._client = app.get_client(account) self.jid = jid + self.type = ''
self.grid = Gtk.Grid(column_spacing=12) self.add(self.grid)
@staticmethod - def _generate_label(): + def _generate_label() -> Gtk.Label: label = Gtk.Label() label.set_halign(Gtk.Align.START) label.set_hexpand(True) @@ -217,7 +247,7 @@ def _generate_label(): label.set_max_width_chars(30) return label
- def _generate_avatar_image(self, jid): + def _generate_avatar_image(self, jid: Union[str, JID]) -> Gtk.Image: contact = self._client.get_module('Contacts').get_contact(jid) surface = contact.get_avatar( AvatarSize.ROSTER, self.get_scale_factor(), add_show=False) @@ -227,7 +257,12 @@ def _generate_avatar_image(self, jid):
class SubscriptionRequestRow(NotificationRow): - def __init__(self, account, jid, text, user_nick=None): + def __init__(self, + account: str, + jid: str, + text: str, + user_nick: Optional[str] = None + ) -> None: NotificationRow.__init__(self, account, jid) self.type = 'subscribe'
@@ -264,7 +299,7 @@ def __init__(self, account, jid, text, user_nick=None): more_button = Gtk.MenuButton() more_button.set_valign(Gtk.Align.CENTER) more_button.add(more_image) - subscription_menu = get_subscription_menu(self._account, self.jid) + subscription_menu = get_subscription_menu(self._account, str(self.jid)) more_button.set_menu_model(subscription_menu) self.grid.attach(more_button, 4, 1, 1, 2)
@@ -272,7 +307,7 @@ def __init__(self, account, jid, text, user_nick=None):
class UnsubscribedRow(NotificationRow): - def __init__(self, account, jid): + def __init__(self, account: str, jid: str) -> None: NotificationRow.__init__(self, account, jid) self.type = 'unsubscribed'
@@ -310,13 +345,13 @@ def __init__(self, account, jid):
self.show_all()
- def _on_dismiss(self, _button): + def _on_dismiss(self, _button: Gtk.Button) -> None: self.destroy()
class InvitationReceivedRow(NotificationRow): - def __init__(self, account, event): - NotificationRow.__init__(self, account, event.muc) + def __init__(self, account: str, event: MucInvitation) -> None: + NotificationRow.__init__(self, account, str(event.muc)) self.type = 'invitation-received'
self._event = event @@ -358,21 +393,21 @@ def __init__(self, account, event):
self.show_all()
- def _on_show_invitation(self, _button): + def _on_show_invitation(self, _button: Gtk.Button) -> None: open_window('GroupChatInvitation', account=self._account, event=self._event) self.destroy()
- def _on_decline_invitation(self, _button): + def _on_decline_invitation(self, _button: Gtk.Button) -> None: self._client.get_module('MUC').decline( self.jid, self._event.from_) self.destroy()
class InvitationDeclinedRow(NotificationRow): - def __init__(self, account, event): - NotificationRow.__init__(self, account, event.muc) + def __init__(self, account: str, event: MucDecline) -> None: + NotificationRow.__init__(self, account, str(event.muc)) self.type = 'invitation-declined'
jid = event.from_.bare @@ -407,5 +442,5 @@ def __init__(self, account, event):
self.show_all()
- def _on_dismiss(self, _button): + def _on_dismiss(self, _button: Gtk.Button) -> None: self.destroy()
===================================== gajim/gtk/util.py ===================================== @@ -145,7 +145,7 @@ def nick_matching(nick: str) -> bool: return matches + other_nicks
-def set_urgency_hint(window: Any, setting: bool) -> None: +def set_urgency_hint(window: Gtk.Window, setting: bool) -> None: if app.settings.get('use_urgency_hint'): window.set_urgency_hint(setting)
@@ -205,9 +205,9 @@ def load_icon_pixbuf(icon_name: str, return icon_info.load_icon()
-def get_app_icon_list(scale_widget: Any) -> List[GdkPixbuf.Pixbuf]: +def get_app_icon_list(scale_widget: Gtk.Widget) -> list[GdkPixbuf.Pixbuf]: scale = scale_widget.get_scale_factor() - pixbufs = [] + pixbufs: list[GdkPixbuf.Pixbuf] = [] for size in (16, 32, 48, 64, 128): pixbuf = load_icon_pixbuf('org.gajim.Gajim', size=size, scale=scale) if pixbuf is not None: @@ -247,8 +247,8 @@ def load_user_iconsets() -> None: icon_theme.append_search_path(str(path))
-def get_available_iconsets() -> List[str]: - iconsets = [] +def get_available_iconsets() -> list[str]: + iconsets: list[str] = [] for iconset in GajimIconSet: iconsets.append(iconset.value)
@@ -263,7 +263,7 @@ def get_available_iconsets() -> List[str]: return iconsets
-def get_total_screen_geometry() -> Tuple[int, int]: +def get_total_screen_geometry() -> tuple[int, int]: total_width = 0 total_height = 0 display = Gdk.Display.get_default() @@ -630,7 +630,7 @@ def get_pixbuf_from_data(file_data: bytes) -> Optional[GdkPixbuf.Pixbuf]: pixbufloader.write(file_data) pixbufloader.close() pixbuf = pixbufloader.get_pixbuf() - except GLib.GError: + except GLib.Error: pixbufloader.close()
log.warning('loading avatar using pixbufloader failed, trying to ' @@ -686,7 +686,7 @@ def load_pixbuf(path: Union[str, Path], return GdkPixbuf.Pixbuf.new_from_file_at_scale( str(path), size, size, True)
- except GLib.GError: + except GLib.Error: try: with open(path, 'rb') as im_handle: img = Image.open(im_handle)
View it on GitLab: https://dev.gajim.org/gajim/gajim/-/compare/203e63d4a2756126cd49922a49cbd981...