Philipp Hörist pushed to branch master at gajim / gajim
Commits: 18cd0156 by lovetox at 2022-01-04T22:18:26+01:00 ged: Improve type annotations
- - - - - 93216c47 by lovetox at 2022-01-04T22:20:10+01:00 Harmonize notification events
- Remove plugin specific code - Port SimpleNotification to Notification - Simplify Code
- - - - - 4d430d23 by lovetox at 2022-01-04T23:44:10+01:00 Notification: Add type annotations
- - - - -
4 changed files:
- gajim/common/client.py - gajim/common/events.py - gajim/common/ged.py - gajim/gtk/notification.py
Changes:
===================================== gajim/common/client.py ===================================== @@ -41,7 +41,7 @@ from gajim.common.events import PlainConnection from gajim.common.events import ShowChanged from gajim.common.events import SignedIn -from gajim.common.events import SimpleNotification +from gajim.common.events import Notification from gajim.common.events import StanzaReceived from gajim.common.events import StanzaSent from gajim.common.const import ClientState @@ -292,10 +292,10 @@ def _on_password(password: str) -> None: on_password=_on_password))
app.ged.raise_event( - SimpleNotification(account=self._account, - notif_type='connection-failed', - title=_('Authentication failed'), - text=text or error)) + Notification(account=self._account, + notif_type='connection-failed', + title=_('Authentication failed'), + text=text or error))
if self._reconnect: self._after_disconnect()
===================================== gajim/common/events.py ===================================== @@ -14,6 +14,7 @@
import typing from typing import Any +from typing import Union from typing import Optional from typing import Callable
@@ -88,24 +89,15 @@ class PasswordRequired(ApplicationEvent): class Notification(ApplicationEvent): name: str = field(init=False, default='notification') account: str - jid: str notif_type: str title: str text: str + jid: Optional[Union[JID, str]] = None notif_detail: Optional[str] = None sound: Optional[str] = None icon_name: Optional[str] = None
-@dataclass -class SimpleNotification(ApplicationEvent): - name: str = field(init=False, default='simple-notification') - account: str - notif_type: str - title: str - text: str - - @dataclass class StanzaSent(ApplicationEvent): name: str = field(init=False, default='stanza-sent')
===================================== gajim/common/ged.py ===================================== @@ -41,8 +41,8 @@ POSTGUI = 110
-HandlerFuncT = Callable[[ApplicationEvent], Any] -EventHelperHandlersT = list[tuple[str, int, Callable[[ApplicationEvent], Any]]] +HandlerFuncT = Callable[[Any], Any] +EventHandlerT = tuple[str, int, HandlerFuncT]
class GlobalEventsDispatcher: @@ -112,18 +112,17 @@ def raise_event(self, event_obj: ApplicationEvent) -> Any:
class EventHelper: def __init__(self): - self.__event_handlers: EventHelperHandlersT = [] + self.__event_handlers: list[EventHandlerT] = []
def register_event(self, event_name: str, priority: int, - handler: Callable[[ApplicationEvent], Any]) -> None: + handler: HandlerFuncT) -> None:
self.__event_handlers.append((event_name, priority, handler)) app.ged.register_event_handler(event_name, priority, handler)
- def register_events(self, events: EventHelperHandlersT) -> None: - + def register_events(self, events: list[EventHandlerT]) -> None: for handler in events: self.__event_handlers.append(handler) app.ged.register_event_handler(*handler) @@ -131,7 +130,7 @@ def register_events(self, events: EventHelperHandlersT) -> None: def unregister_event(self, event_name: str, priority: int, - handler: Callable[[ApplicationEvent], Any]) -> None: + handler: HandlerFuncT) -> None:
self.__event_handlers.remove((event_name, priority, handler)) app.ged.register_event_handler(event_name, priority, handler)
===================================== gajim/gtk/notification.py ===================================== @@ -24,9 +24,9 @@ # 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 Dict +from __future__ import annotations + from typing import Optional -from typing import Tuple
import sys import logging @@ -36,16 +36,14 @@ from gi.repository import Gdk from gi.repository import GdkPixbuf from gi.repository import Gtk -from gi.repository import GObject
from gajim.common import app from gajim.common import ged +from gajim.common import events from gajim.common.const import StyleAttr from gajim.common.i18n import _ from gajim.common.helpers import allow_showing_notification -from gajim.common.helpers import exec_command from gajim.common.helpers import play_sound -from gajim.common.helpers import play_sound_file from gajim.common.ged import EventHelper
from .util import add_css_to_widget @@ -55,7 +53,8 @@
log = logging.getLogger('gajim.gui.notification')
-NOTIFICATION_ICONS: Dict[str, str] = { + +NOTIFICATION_ICONS: dict[str, str] = { 'incoming-message': 'gajim-chat_msg_recv', 'group-chat-invitation': 'gajim-gc_invitation', 'incoming-call': 'call-start-symbolic', @@ -80,9 +79,7 @@ def __init__(self) -> None:
self.register_events([ ('notification', ged.GUI2, self._on_notification), - ('simple-notification', ged.GUI2, self._on_notification), - ('our-show', ged.GUI2, self._on_our_show), - ('connection-lost', ged.GUI2, self._on_connection_lost) + ('our-show', ged.GUI2, self._on_our_show) ])
def _detect_dbus_caps(self) -> None: @@ -93,10 +90,8 @@ def _detect_dbus_caps(self) -> None: self._dbus_available = True return
- def on_proxy_ready(_source: GObject.Object, - res: Gio.AsyncResult, - _data: Optional[object] = None - ) -> None: + def on_proxy_ready(_source: Gio.DBusProxy, + res: Gio.AsyncResult) -> None: try: proxy = Gio.DBusProxy.new_finish(res) self._daemon_capabilities = proxy.GetCapabilities() @@ -116,42 +111,22 @@ def on_proxy_ready(_source: GObject.Object, None, on_proxy_ready)
- def _on_notification(self, event): - if hasattr(event, 'jid'): + def _on_notification(self, event: events.Notification) -> None: + if event.jid is not None: jid = str(event.jid) - else: - jid = None - if hasattr(event, 'command'): - # Used by Triggers plugin - try: - exec_command(event.command, use_shell=True) - except Exception: - pass - - if hasattr(event, 'sound_file'): - # Allow override here, used by Triggers plugin - play_sound_file(event.sound_file) - elif hasattr(event, 'sound'): + + if event.sound is not None: play_sound(event.sound, event.account)
- if hasattr(event, 'show_notification'): - # Allow override here, used by Triggers plugin - if not event.show_notification: - return - else: - if not allow_showing_notification(event.account): - return + if not allow_showing_notification(event.account): + return
- if hasattr(event, 'notif_detail'): - notif_detail = event.notif_detail - else: - notif_detail = event.notif_type + notif_detail = event.notif_detail or event.notif_type
- if hasattr(event, 'icon_name'): + if event.icon_name is not None: icon_name = event.icon_name else: - icon_name = NOTIFICATION_ICONS.get( - notif_detail, 'mail-unread') + icon_name = NOTIFICATION_ICONS.get(notif_detail, 'mail-unread')
self._issue_notification( event.notif_type, @@ -162,33 +137,10 @@ def _on_notification(self, event): text=event.text, icon_name=icon_name)
- def _on_simple_notification(self, event): - if not allow_showing_notification(event.account): - return - - self._issue_notification( - event.notif_type, - event.account, - None, - title=event.title, - text=event.text) - - def _on_our_show(self, event): + def _on_our_show(self, event: events.ShowChanged) -> None: if app.account_is_connected(event.account): self._withdraw('connection-failed', event.account)
- def _on_connection_lost(self, event): - if not allow_showing_notification(event.conn.name): - return - - self._issue_notification( - 'connection-lost', - event.conn.name, - None, - title=event.title, - text=event.msg, - icon_name='gajim-connection_lost') - def _issue_notification(self, notif_type: str, account: str, @@ -375,7 +327,7 @@ def __init__(self, self._timeout_id = GLib.timeout_add_seconds(timeout, self.destroy)
@staticmethod - def _get_window_pos() -> Tuple[int, int]: + def _get_window_pos() -> tuple[int, int]: pos_x = app.settings.get('notification_position_x') screen_w, screen_h = get_total_screen_geometry() if pos_x < 0: @@ -397,6 +349,6 @@ def _on_button_press(self, self._account, self._jid, self._notif_type) self.destroy()
- def _on_destroy(self, *args): + def _on_destroy(self, _widget: Gtk.Window) -> None: if self._timeout_id is not None: GLib.source_remove(self._timeout_id)
View it on GitLab: https://dev.gajim.org/gajim/gajim/-/compare/15f338e4aa9bf116812fd19a887b18b3...