diff options
author | wurstsalat <mailtrash@posteo.de> | 2022-08-12 19:03:44 +0300 |
---|---|---|
committer | wurstsalat <mailtrash@posteo.de> | 2022-08-12 19:03:52 +0300 |
commit | 5ed1791e52cc058b849ce4b7baf4c3be989f9604 (patch) | |
tree | b70c01c1b6d88879b724bb9910e8b54ed71b1dca | |
parent | 8aed5d2dd79004df46c9567f802c43cec780ce1a (diff) |
refactor: Move notification logic from Control to ChatStack
-rw-r--r-- | gajim/gtk/chat_stack.py | 96 | ||||
-rw-r--r-- | gajim/gtk/control.py | 103 |
2 files changed, 93 insertions, 106 deletions
diff --git a/gajim/gtk/chat_stack.py b/gajim/gtk/chat_stack.py index 708c674e0..d7a579c9f 100644 --- a/gajim/gtk/chat_stack.py +++ b/gajim/gtk/chat_stack.py @@ -14,11 +14,11 @@ from __future__ import annotations -from typing import Any from typing import Optional import sys import logging +import time from gi.repository import Gdk from gi.repository import GLib @@ -31,8 +31,9 @@ from nbxmpp.structs import MessageProperties from nbxmpp.structs import PresenceProperties from gajim.common import app +from gajim.common import events from gajim.common import helpers -from gajim.common.events import MucDiscoUpdate +from gajim.common import preview_helpers from gajim.common.i18n import _ from gajim.common.const import CallType from gajim.common.modules.contacts import BareContact @@ -47,7 +48,9 @@ from .const import TARGET_TYPE_URI_LIST from .control import ChatControl from .message_actions_box import MessageActionsBox from .message_input import MessageInputTextView -from .util import EventHelper, open_window +from .util import EventHelper +from .util import open_window +from .util import set_urgency_hint log = logging.getLogger('gajim.gui.chatstack') @@ -300,7 +303,7 @@ class ChatStack(Gtk.Stack, EventHelper): ) -> None: self._update_group_chat_actions(contact) - def _on_muc_disco_update(self, event: MucDiscoUpdate) -> None: + def _on_muc_disco_update(self, event: events.MucDiscoUpdate) -> None: if self._current_contact is None: return @@ -310,6 +313,84 @@ class ChatStack(Gtk.Stack, EventHelper): if isinstance(self._current_contact, GroupchatContact): self._update_group_chat_actions(self._current_contact) + def _on_message_received(self, event: events.MessageReceived) -> None: + if not event.msgtxt or event.properties.is_sent_carbon: + return + + if app.window.is_chat_active(event.account, event.jid): + return + + self._issue_notification(event) + + def _issue_notification(self, event: events.MessageReceived) -> None: + text = event.msgtxt + tim = event.properties.timestamp + additional_data = event.additional_data + client = app.get_client(event.account) + contact = client.get_module('Contacts').get_contact(event.jid) + + title = _('New message from') + + is_previewable = app.preview_manager.is_previewable( + text, additional_data) + if is_previewable: + if text.startswith('geo:'): + text = _('Location') + else: + file_name = preview_helpers.filename_from_uri(text) + _icon, file_type = preview_helpers.guess_simple_file_type(text) + text = f'{file_type} ({file_name})' + + sound: Optional[str] = None + msg_type = 'chat-message' + if isinstance(contact, BareContact): + msg_type = 'chat-message' + title += f' {contact.name}' + sound = 'first_message_received' + set_urgency_hint(app.window, True) + + if isinstance(contact, GroupchatContact): + msg_type = 'group-chat-message' + title += f' {contact.nickname} ({contact.name})' + assert contact.nickname is not None + needs_highlight = helpers.message_needs_highlight( + text, contact.nickname, client.get_own_jid().bare) + if needs_highlight: + sound = 'muc_message_highlight' + else: + sound = 'muc_message_received' + + if not contact.can_notify() and not needs_highlight: + return + + if contact.can_notify() or needs_highlight: + set_urgency_hint(app.window, True) + + if isinstance(contact, GroupchatParticipant): + msg_type = 'private-chat-message' + title += f' {contact.name} (private in {contact.room.name})' + sound = 'first_message_received' + + # Is it a history message? Don't want sound-floods when we join. + if tim is not None and time.mktime(time.localtime()) - tim > 1: + sound = None + + if app.settings.get('notification_preview_message'): + if text.startswith('/me') or text.startswith('/me\n'): + name = contact.name + if isinstance(contact, GroupchatContact): + name = contact.nickname + text = f'* {name} {text[3:]}' + + app.ged.raise_event( + events.Notification(account=contact.account, + jid=contact.jid, + type='incoming-message', + sub_type=msg_type, + title=title, + text=text, + sound=sound)) + def _connect_actions(self) -> None: actions = [ 'add-to-roster', @@ -643,9 +724,12 @@ class ChatStack(Gtk.Stack, EventHelper): self._message_action_box.clear() self._chat_control.clear() - def process_event(self, event: Any) -> None: - if isinstance(event, MucDiscoUpdate): + def process_event(self, event: events.MainEventT) -> None: + if isinstance(event, events.MucDiscoUpdate): self._on_muc_disco_update(event) + if isinstance(event, events.MessageReceived): + self._on_message_received(event) + self._chat_control.process_event(event) self._message_action_box.process_event(event) diff --git a/gajim/gtk/control.py b/gajim/gtk/control.py index 9cd0d54ae..f77853870 100644 --- a/gajim/gtk/control.py +++ b/gajim/gtk/control.py @@ -37,7 +37,6 @@ from gajim.common import app from gajim.common import events from gajim.common import helpers from gajim.common import types -from gajim.common.helpers import message_needs_highlight from gajim.common.helpers import get_file_path_from_dnd_dropped_uri from gajim.common.helpers import to_user_string from gajim.common.i18n import _ @@ -49,14 +48,11 @@ from gajim.common.modules.contacts import BareContact from gajim.common.modules.contacts import GroupchatParticipant from gajim.common.modules.contacts import GroupchatContact from gajim.common.modules.httpupload import HTTPFileTransfer -from gajim.common.preview_helpers import filename_from_uri -from gajim.common.preview_helpers import guess_simple_file_type from gajim.common.storage.archive import ConversationRow from gajim.gui.conversation.scrolled import ScrolledView from gajim.gui.conversation.jump_to_end_button import JumpToEndButton from gajim.gui.builder import get_builder -from gajim.gui.util import set_urgency_hint from gajim.gui.dialogs import DialogButton from gajim.gui.dialogs import ConfirmationDialog from gajim.gui.groupchat_roster import GroupchatRoster @@ -367,7 +363,6 @@ class ChatControl(EventHelper): kind: str, name: str, tim: float, - notify: bool, displaymarking: Optional[Displaymarking] = None, msg_log_id: Optional[int] = None, message_id: Optional[str] = None, @@ -378,9 +373,6 @@ class ChatControl(EventHelper): if additional_data is None: additional_data = AdditionalDataDict() - chat_active = app.window.is_chat_active( - self.contact.account, self.contact.jid) - if self._allow_add_message(): self.conversation_view.add_message( text, @@ -407,89 +399,6 @@ class ChatControl(EventHelper): else: self.last_msg_id = message_id - if kind == 'incoming': - if notify: - # Issue notification - self._notify(name, text, tim, additional_data) - - if not chat_active and notify: - if isinstance(self._contact, GroupchatContact): - assert self._contact.nickname is not None - needs_highlight = message_needs_highlight( - text, - self._contact.nickname, - self.client.get_own_jid().bare) - if needs_highlight or self._contact.can_notify(): - set_urgency_hint(app.window, True) - else: - set_urgency_hint(app.window, True) - - def _notify(self, - name: str, - text: str, - tim: Optional[float], - additional_data: AdditionalDataDict - ) -> None: - if app.window.is_chat_active(self.contact.account, self.contact.jid): - if self._scrolled_view.get_autoscroll(): - return - - title = _('New message from %s') % name - - is_previewable = app.preview_manager.is_previewable( - text, additional_data) - if is_previewable: - if text.startswith('geo:'): - text = _('Location') - else: - file_name = filename_from_uri(text) - _icon, file_type = guess_simple_file_type(text) - text = f'{file_type} ({file_name})' - - sound: Optional[str] = None - msg_type = 'chat-message' - if self.is_chat: - msg_type = 'chat-message' - sound = 'first_message_received' - - if isinstance(self._contact, GroupchatContact): - msg_type = 'group-chat-message' - title += f' ({self._contact.name})' - assert self._contact.nickname is not None - needs_highlight = message_needs_highlight( - text, self._contact.nickname, self.client.get_own_jid().bare) - if needs_highlight: - sound = 'muc_message_highlight' - else: - sound = 'muc_message_received' - - if not self._contact.can_notify() and not needs_highlight: - return - - if self.is_privatechat: - room_contact = self.client.get_module('Contacts').get_contact( - self.contact.jid.bare) - msg_type = 'private-chat-message' - title += f' (private in {room_contact.name})' - sound = 'first_message_received' - - # Is it a history message? Don't want sound-floods when we join. - if tim is not None and time.mktime(time.localtime()) - tim > 1: - sound = None - - if app.settings.get('notification_preview_message'): - if text.startswith('/me') or text.startswith('/me\n'): - text = f'* {name} {text[3:]}' - - app.ged.raise_event( - events.Notification(account=self.contact.account, - jid=self.contact.jid, - type='incoming-message', - sub_type=msg_type, - title=title, - text=text, - sound=sound)) - def reset_view(self) -> None: self._scrolled_view.reset() @@ -622,8 +531,7 @@ class ChatControl(EventHelper): contact=event.properties.muc_nickname, message_id=event.properties.id, stanza_id=event.stanza_id, - additional_data=event.additional_data, - notify=False) + additional_data=event.additional_data) else: @@ -643,8 +551,7 @@ class ChatControl(EventHelper): tim=event.properties.mam.timestamp, message_id=event.properties.id, stanza_id=event.stanza_id, - additional_data=event.additional_data, - notify=False) + additional_data=event.additional_data) def _on_message_received(self, event: events.MessageReceived) -> None: if not event.msgtxt: @@ -703,8 +610,7 @@ class ChatControl(EventHelper): msg_log_id: Optional[int] = None, stanza_id: Optional[str] = None, message_id: Optional[str] = None, - additional_data: Optional[AdditionalDataDict] = None, - notify: bool = True + additional_data: Optional[AdditionalDataDict] = None ) -> None: if kind == 'incoming': @@ -716,7 +622,6 @@ class ChatControl(EventHelper): kind, name, tim, - notify, displaymarking=displaymarking, msg_log_id=msg_log_id, message_id=message_id, @@ -856,7 +761,6 @@ class ChatControl(EventHelper): message_id: Optional[str] = None, stanza_id: Optional[str] = None, additional_data: Optional[AdditionalDataDict] = None, - notify: bool = True ) -> None: assert isinstance(self._contact, GroupchatContact) @@ -871,7 +775,6 @@ class ChatControl(EventHelper): kind, contact, tim, - notify, displaymarking=displaymarking, message_id=message_id, stanza_id=stanza_id, |