Daniel Brötzmann pushed to branch master at gajim / gajim

Commits:

2 changed files:

Changes:

  • gajim/gtk/chat_stack.py
    ... ... @@ -14,11 +14,11 @@
    14 14
     
    
    15 15
     from __future__ import annotations
    
    16 16
     
    
    17
    -from typing import Any
    
    18 17
     from typing import Optional
    
    19 18
     
    
    20 19
     import sys
    
    21 20
     import logging
    
    21
    +import time
    
    22 22
     
    
    23 23
     from gi.repository import Gdk
    
    24 24
     from gi.repository import GLib
    
    ... ... @@ -31,8 +31,9 @@
    31 31
     from nbxmpp.structs import PresenceProperties
    
    32 32
     
    
    33 33
     from gajim.common import app
    
    34
    +from gajim.common import events
    
    34 35
     from gajim.common import helpers
    
    35
    -from gajim.common.events import MucDiscoUpdate
    
    36
    +from gajim.common import preview_helpers
    
    36 37
     from gajim.common.i18n import _
    
    37 38
     from gajim.common.const import CallType
    
    38 39
     from gajim.common.modules.contacts import BareContact
    
    ... ... @@ -47,7 +48,9 @@
    47 48
     from .control import ChatControl
    
    48 49
     from .message_actions_box import MessageActionsBox
    
    49 50
     from .message_input import MessageInputTextView
    
    50
    -from .util import EventHelper, open_window
    
    51
    +from .util import EventHelper
    
    52
    +from .util import open_window
    
    53
    +from .util import set_urgency_hint
    
    51 54
     
    
    52 55
     log = logging.getLogger('gajim.gui.chatstack')
    
    53 56
     
    
    ... ... @@ -300,7 +303,7 @@ def _on_user_affiliation_changed(self,
    300 303
                                          ) -> None:
    
    301 304
             self._update_group_chat_actions(contact)
    
    302 305
     
    
    303
    -    def _on_muc_disco_update(self, event: MucDiscoUpdate) -> None:
    
    306
    +    def _on_muc_disco_update(self, event: events.MucDiscoUpdate) -> None:
    
    304 307
             if self._current_contact is None:
    
    305 308
                 return
    
    306 309
     
    
    ... ... @@ -310,6 +313,84 @@ def _on_muc_disco_update(self, event: MucDiscoUpdate) -> None:
    310 313
             if isinstance(self._current_contact, GroupchatContact):
    
    311 314
                 self._update_group_chat_actions(self._current_contact)
    
    312 315
     
    
    316
    +    def _on_message_received(self, event: events.MessageReceived) -> None:
    
    317
    +        if not event.msgtxt or event.properties.is_sent_carbon:
    
    318
    +            return
    
    319
    +
    
    320
    +        if app.window.is_chat_active(event.account, event.jid):
    
    321
    +            return
    
    322
    +
    
    323
    +        self._issue_notification(event)
    
    324
    +
    
    325
    +    def _issue_notification(self, event: events.MessageReceived) -> None:
    
    326
    +        text = event.msgtxt
    
    327
    +        tim = event.properties.timestamp
    
    328
    +        additional_data = event.additional_data
    
    329
    +        client = app.get_client(event.account)
    
    330
    +        contact = client.get_module('Contacts').get_contact(event.jid)
    
    331
    +
    
    332
    +        title = _('New message from')
    
    333
    +
    
    334
    +        is_previewable = app.preview_manager.is_previewable(
    
    335
    +            text, additional_data)
    
    336
    +        if is_previewable:
    
    337
    +            if text.startswith('geo:'):
    
    338
    +                text = _('Location')
    
    339
    +            else:
    
    340
    +                file_name = preview_helpers.filename_from_uri(text)
    
    341
    +                _icon, file_type = preview_helpers.guess_simple_file_type(text)
    
    342
    +                text = f'{file_type} ({file_name})'
    
    343
    +
    
    344
    +        sound: Optional[str] = None
    
    345
    +        msg_type = 'chat-message'
    
    346
    +        if isinstance(contact, BareContact):
    
    347
    +            msg_type = 'chat-message'
    
    348
    +            title += f' {contact.name}'
    
    349
    +            sound = 'first_message_received'
    
    350
    +            set_urgency_hint(app.window, True)
    
    351
    +
    
    352
    +        if isinstance(contact, GroupchatContact):
    
    353
    +            msg_type = 'group-chat-message'
    
    354
    +            title += f' {contact.nickname} ({contact.name})'
    
    355
    +            assert contact.nickname is not None
    
    356
    +            needs_highlight = helpers.message_needs_highlight(
    
    357
    +                text, contact.nickname, client.get_own_jid().bare)
    
    358
    +            if needs_highlight:
    
    359
    +                sound = 'muc_message_highlight'
    
    360
    +            else:
    
    361
    +                sound = 'muc_message_received'
    
    362
    +
    
    363
    +            if not contact.can_notify() and not needs_highlight:
    
    364
    +                return
    
    365
    +
    
    366
    +            if contact.can_notify() or needs_highlight:
    
    367
    +                set_urgency_hint(app.window, True)
    
    368
    +
    
    369
    +        if isinstance(contact, GroupchatParticipant):
    
    370
    +            msg_type = 'private-chat-message'
    
    371
    +            title += f' {contact.name} (private in {contact.room.name})'
    
    372
    +            sound = 'first_message_received'
    
    373
    +
    
    374
    +        # Is it a history message? Don't want sound-floods when we join.
    
    375
    +        if tim is not None and time.mktime(time.localtime()) - tim > 1:
    
    376
    +            sound = None
    
    377
    +
    
    378
    +        if app.settings.get('notification_preview_message'):
    
    379
    +            if text.startswith('/me') or text.startswith('/me\n'):
    
    380
    +                name = contact.name
    
    381
    +                if isinstance(contact, GroupchatContact):
    
    382
    +                    name = contact.nickname
    
    383
    +                text = f'* {name} {text[3:]}'
    
    384
    +
    
    385
    +        app.ged.raise_event(
    
    386
    +            events.Notification(account=contact.account,
    
    387
    +                                jid=contact.jid,
    
    388
    +                                type='incoming-message',
    
    389
    +                                sub_type=msg_type,
    
    390
    +                                title=title,
    
    391
    +                                text=text,
    
    392
    +                                sound=sound))
    
    393
    +
    
    313 394
         def _connect_actions(self) -> None:
    
    314 395
             actions = [
    
    315 396
                 'add-to-roster',
    
    ... ... @@ -643,9 +724,12 @@ def clear(self) -> None:
    643 724
             self._message_action_box.clear()
    
    644 725
             self._chat_control.clear()
    
    645 726
     
    
    646
    -    def process_event(self, event: Any) -> None:
    
    647
    -        if isinstance(event, MucDiscoUpdate):
    
    727
    +    def process_event(self, event: events.MainEventT) -> None:
    
    728
    +        if isinstance(event, events.MucDiscoUpdate):
    
    648 729
                 self._on_muc_disco_update(event)
    
    730
    +        if isinstance(event, events.MessageReceived):
    
    731
    +            self._on_message_received(event)
    
    732
    +
    
    649 733
             self._chat_control.process_event(event)
    
    650 734
             self._message_action_box.process_event(event)
    
    651 735
     
    

  • gajim/gtk/control.py
    ... ... @@ -37,7 +37,6 @@
    37 37
     from gajim.common import events
    
    38 38
     from gajim.common import helpers
    
    39 39
     from gajim.common import types
    
    40
    -from gajim.common.helpers import message_needs_highlight
    
    41 40
     from gajim.common.helpers import get_file_path_from_dnd_dropped_uri
    
    42 41
     from gajim.common.helpers import to_user_string
    
    43 42
     from gajim.common.i18n import _
    
    ... ... @@ -49,14 +48,11 @@
    49 48
     from gajim.common.modules.contacts import GroupchatParticipant
    
    50 49
     from gajim.common.modules.contacts import GroupchatContact
    
    51 50
     from gajim.common.modules.httpupload import HTTPFileTransfer
    
    52
    -from gajim.common.preview_helpers import filename_from_uri
    
    53
    -from gajim.common.preview_helpers import guess_simple_file_type
    
    54 51
     from gajim.common.storage.archive import ConversationRow
    
    55 52
     
    
    56 53
     from gajim.gui.conversation.scrolled import ScrolledView
    
    57 54
     from gajim.gui.conversation.jump_to_end_button import JumpToEndButton
    
    58 55
     from gajim.gui.builder import get_builder
    
    59
    -from gajim.gui.util import set_urgency_hint
    
    60 56
     from gajim.gui.dialogs import DialogButton
    
    61 57
     from gajim.gui.dialogs import ConfirmationDialog
    
    62 58
     from gajim.gui.groupchat_roster import GroupchatRoster
    
    ... ... @@ -367,7 +363,6 @@ def _add_message(self,
    367 363
                          kind: str,
    
    368 364
                          name: str,
    
    369 365
                          tim: float,
    
    370
    -                     notify: bool,
    
    371 366
                          displaymarking: Optional[Displaymarking] = None,
    
    372 367
                          msg_log_id: Optional[int] = None,
    
    373 368
                          message_id: Optional[str] = None,
    
    ... ... @@ -378,9 +373,6 @@ def _add_message(self,
    378 373
             if additional_data is None:
    
    379 374
                 additional_data = AdditionalDataDict()
    
    380 375
     
    
    381
    -        chat_active = app.window.is_chat_active(
    
    382
    -            self.contact.account, self.contact.jid)
    
    383
    -
    
    384 376
             if self._allow_add_message():
    
    385 377
                 self.conversation_view.add_message(
    
    386 378
                     text,
    
    ... ... @@ -407,89 +399,6 @@ def _add_message(self,
    407 399
                 else:
    
    408 400
                     self.last_msg_id = message_id
    
    409 401
     
    
    410
    -        if kind == 'incoming':
    
    411
    -            if notify:
    
    412
    -                # Issue notification
    
    413
    -                self._notify(name, text, tim, additional_data)
    
    414
    -
    
    415
    -            if not chat_active and notify:
    
    416
    -                if isinstance(self._contact, GroupchatContact):
    
    417
    -                    assert self._contact.nickname is not None
    
    418
    -                    needs_highlight = message_needs_highlight(
    
    419
    -                        text,
    
    420
    -                        self._contact.nickname,
    
    421
    -                        self.client.get_own_jid().bare)
    
    422
    -                    if needs_highlight or self._contact.can_notify():
    
    423
    -                        set_urgency_hint(app.window, True)
    
    424
    -                else:
    
    425
    -                    set_urgency_hint(app.window, True)
    
    426
    -
    
    427
    -    def _notify(self,
    
    428
    -                name: str,
    
    429
    -                text: str,
    
    430
    -                tim: Optional[float],
    
    431
    -                additional_data: AdditionalDataDict
    
    432
    -                ) -> None:
    
    433
    -        if app.window.is_chat_active(self.contact.account, self.contact.jid):
    
    434
    -            if self._scrolled_view.get_autoscroll():
    
    435
    -                return
    
    436
    -
    
    437
    -        title = _('New message from %s') % name
    
    438
    -
    
    439
    -        is_previewable = app.preview_manager.is_previewable(
    
    440
    -            text, additional_data)
    
    441
    -        if is_previewable:
    
    442
    -            if text.startswith('geo:'):
    
    443
    -                text = _('Location')
    
    444
    -            else:
    
    445
    -                file_name = filename_from_uri(text)
    
    446
    -                _icon, file_type = guess_simple_file_type(text)
    
    447
    -                text = f'{file_type} ({file_name})'
    
    448
    -
    
    449
    -        sound: Optional[str] = None
    
    450
    -        msg_type = 'chat-message'
    
    451
    -        if self.is_chat:
    
    452
    -            msg_type = 'chat-message'
    
    453
    -            sound = 'first_message_received'
    
    454
    -
    
    455
    -        if isinstance(self._contact, GroupchatContact):
    
    456
    -            msg_type = 'group-chat-message'
    
    457
    -            title += f' ({self._contact.name})'
    
    458
    -            assert self._contact.nickname is not None
    
    459
    -            needs_highlight = message_needs_highlight(
    
    460
    -                text, self._contact.nickname, self.client.get_own_jid().bare)
    
    461
    -            if needs_highlight:
    
    462
    -                sound = 'muc_message_highlight'
    
    463
    -            else:
    
    464
    -                sound = 'muc_message_received'
    
    465
    -
    
    466
    -            if not self._contact.can_notify() and not needs_highlight:
    
    467
    -                return
    
    468
    -
    
    469
    -        if self.is_privatechat:
    
    470
    -            room_contact = self.client.get_module('Contacts').get_contact(
    
    471
    -                self.contact.jid.bare)
    
    472
    -            msg_type = 'private-chat-message'
    
    473
    -            title += f' (private in {room_contact.name})'
    
    474
    -            sound = 'first_message_received'
    
    475
    -
    
    476
    -        # Is it a history message? Don't want sound-floods when we join.
    
    477
    -        if tim is not None and time.mktime(time.localtime()) - tim > 1:
    
    478
    -            sound = None
    
    479
    -
    
    480
    -        if app.settings.get('notification_preview_message'):
    
    481
    -            if text.startswith('/me') or text.startswith('/me\n'):
    
    482
    -                text = f'* {name} {text[3:]}'
    
    483
    -
    
    484
    -        app.ged.raise_event(
    
    485
    -            events.Notification(account=self.contact.account,
    
    486
    -                                jid=self.contact.jid,
    
    487
    -                                type='incoming-message',
    
    488
    -                                sub_type=msg_type,
    
    489
    -                                title=title,
    
    490
    -                                text=text,
    
    491
    -                                sound=sound))
    
    492
    -
    
    493 402
         def reset_view(self) -> None:
    
    494 403
             self._scrolled_view.reset()
    
    495 404
     
    
    ... ... @@ -622,8 +531,7 @@ def _on_mam_message_received(self,
    622 531
                                      contact=event.properties.muc_nickname,
    
    623 532
                                      message_id=event.properties.id,
    
    624 533
                                      stanza_id=event.stanza_id,
    
    625
    -                                 additional_data=event.additional_data,
    
    626
    -                                 notify=False)
    
    534
    +                                 additional_data=event.additional_data)
    
    627 535
     
    
    628 536
             else:
    
    629 537
     
    
    ... ... @@ -643,8 +551,7 @@ def _on_mam_message_received(self,
    643 551
                                  tim=event.properties.mam.timestamp,
    
    644 552
                                  message_id=event.properties.id,
    
    645 553
                                  stanza_id=event.stanza_id,
    
    646
    -                             additional_data=event.additional_data,
    
    647
    -                             notify=False)
    
    554
    +                             additional_data=event.additional_data)
    
    648 555
     
    
    649 556
         def _on_message_received(self, event: events.MessageReceived) -> None:
    
    650 557
             if not event.msgtxt:
    
    ... ... @@ -703,8 +610,7 @@ def add_message(self,
    703 610
                         msg_log_id: Optional[int] = None,
    
    704 611
                         stanza_id: Optional[str] = None,
    
    705 612
                         message_id: Optional[str] = None,
    
    706
    -                    additional_data: Optional[AdditionalDataDict] = None,
    
    707
    -                    notify: bool = True
    
    613
    +                    additional_data: Optional[AdditionalDataDict] = None
    
    708 614
                         ) -> None:
    
    709 615
     
    
    710 616
             if kind == 'incoming':
    
    ... ... @@ -716,7 +622,6 @@ def add_message(self,
    716 622
                               kind,
    
    717 623
                               name,
    
    718 624
                               tim,
    
    719
    -                          notify,
    
    720 625
                               displaymarking=displaymarking,
    
    721 626
                               msg_log_id=msg_log_id,
    
    722 627
                               message_id=message_id,
    
    ... ... @@ -856,7 +761,6 @@ def add_muc_message(self,
    856 761
                             message_id: Optional[str] = None,
    
    857 762
                             stanza_id: Optional[str] = None,
    
    858 763
                             additional_data: Optional[AdditionalDataDict] = None,
    
    859
    -                        notify: bool = True
    
    860 764
                             ) -> None:
    
    861 765
     
    
    862 766
             assert isinstance(self._contact, GroupchatContact)
    
    ... ... @@ -871,7 +775,6 @@ def add_muc_message(self,
    871 775
                               kind,
    
    872 776
                               contact,
    
    873 777
                               tim,
    
    874
    -                          notify,
    
    875 778
                               displaymarking=displaymarking,
    
    876 779
                               message_id=message_id,
    
    877 780
                               stanza_id=stanza_id,