changeset 396:ae6f7fd1cb0e

chat: use QuickApp.notify and clear notifications when selected.
author Goffi <goffi@goffi.org>
date Sun, 09 Feb 2020 23:47:29 +0100
parents c04c3b167cb0
children 54f6a47cc60a
files cagou/core/cagou_main.py cagou/plugins/plugin_wid_chat.py
diffstat 2 files changed, 36 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/cagou/core/cagou_main.py	Thu Feb 06 21:16:21 2020 +0100
+++ b/cagou/core/cagou_main.py	Sun Feb 09 23:47:29 2020 +0100
@@ -1036,15 +1036,27 @@
             log.error(e)
             self.closeUI()
 
+    def notify(self, type_, entity=None, message=None, subject=None, callback=None,
+               cb_args=None, widget=None, profile=C.PROF_KEY_NONE):
+        super().notify(
+            type_=type_, entity=entity, message=message, subject=subject,
+            callback=callback, cb_args=cb_args, widget=widget, profile=profile)
+        self.desktop_notif(message, title=subject)
+
     def desktop_notif(self, message, title='', duration=5000):
         global notification
         if notification is not None:
             try:
+                log.debug(
+                    f"sending desktop notification (duration: {duration}):\n"
+                    f"{title}\n"
+                    f"{message}"
+                )
                 notification.notify(title=title,
                                     message=message,
                                     app_name=C.APP_NAME,
                                     app_icon=self.app.icon,
-                                    timeout = duration)
+                                    timeout=duration)
             except Exception as e:
                 log.warning(_("Can't use notifications, disabling: {msg}").format(
                     msg = e))
--- a/cagou/plugins/plugin_wid_chat.py	Thu Feb 06 21:16:21 2020 +0100
+++ b/cagou/plugins/plugin_wid_chat.py	Sun Feb 09 23:47:29 2020 +0100
@@ -621,7 +621,7 @@
         or when one2one chat is not visible. A note is also there when widget
         is not visible.
         For group chat, note will be added on mention, with a desktop notification if
-        window has not focus.
+        window has not focus or is not visible.
         """
         visible_clones = [w for w in G.host.getVisibleList(self.__class__)
                           if w.target == self.target]
@@ -633,9 +633,14 @@
         if self.type == C.CHAT_ONE2ONE:
             if (not Window.focus or not is_visible) and not mess_data.history:
                 notif_msg = self._get_notif_msg(mess_data)
-                G.host.desktop_notif(
-                    notif_msg,
-                    title=_("private message"))
+                G.host.notify(
+                    type_=C.NOTIFY_MESSAGE,
+                    entity=mess_data.from_jid,
+                    message=notif_msg,
+                    subject=_("private message"),
+                    widget=self,
+                    profile=self.profile
+                )
                 if not is_visible:
                     G.host.addNote(
                         _("private message"),
@@ -647,7 +652,7 @@
                             "profiles": self.profiles}
                         )
         else:
-            if mess_data.mention and not mess_data.history:
+            if mess_data.mention:
                 notif_msg = self._get_notif_msg(mess_data)
                 G.host.addNote(
                     _("mention"),
@@ -658,12 +663,16 @@
                         "target": self.target,
                         "profiles": self.profiles}
                     )
-                if not Window.focus:
-                    G.host.desktop_notif(
-                        notif_msg,
-                        title=_("mention ({room_jid})").format(
-                            room_jid=self.target)
-                        )
+                if not is_visible or not Window.focus:
+                    subject=_("mention ({room_jid})").format(room_jid=self.target)
+                    G.host.notify(
+                        type_=C.NOTIFY_MENTION,
+                        entity=self.target,
+                        message=notif_msg,
+                        subject=subject,
+                        widget=self,
+                        profile=self.profile
+                    )
 
     # message input
 
@@ -789,6 +798,9 @@
         if not self.sync:
             self.resync()
 
+    def onSelected(self):
+        G.host.clearNotifs(self.target, profile=self.profile)
+
     def onDelete(self, **kwargs):
         if kwargs.get('explicit_close', False):
             wrapper = self.whwrapper