diff cagou/plugins/plugin_wid_chat.py @ 184:c63922860f80

chat: show desktop notification and/or note when suitable
author Goffi <goffi@goffi.org>
date Thu, 10 May 2018 08:32:46 +0200
parents 0ddd2b20cc6b
children ab3f5173ef5c
line wrap: on
line diff
--- a/cagou/plugins/plugin_wid_chat.py	Tue May 01 21:53:33 2018 +0200
+++ b/cagou/plugins/plugin_wid_chat.py	Thu May 10 08:32:46 2018 +0200
@@ -34,6 +34,7 @@
 from cagou.core.image import Image
 from cagou.core.common import SymbolButton, JidItem
 from kivy.uix.dropdown import DropDown
+from kivy.core.window import Window
 from cagou import G
 import mimetypes
 
@@ -351,6 +352,47 @@
 
     def appendMessage(self, mess_data):
         self.messages_widget.add_widget(MessageWidget(mess_data=mess_data))
+        self.notify(mess_data)
+
+    def _get_notif_msg(self, mess_data):
+        return _(u"{nick}: {message}").format(
+            nick=mess_data.nick,
+            message=mess_data.main_message)
+
+    def notify(self, mess_data):
+        """Notify user when suitable
+
+        For one2one chat, notification will happen when window has not focus
+        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.
+        """
+        if self.type == C.CHAT_ONE2ONE:
+            is_visible = self.target in [w.target for w in G.host.getVisibleList(self.__class__)]
+            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=_(u"private message"))
+                if not is_visible:
+                    G.host.addNote(
+                        _(u"private message"),
+                        notif_msg
+                        )
+        else:
+            if mess_data.mention and not mess_data.history:
+                notif_msg = self._get_notif_msg(mess_data)
+                G.host.addNote(
+                    _(u"mention"),
+                    notif_msg
+                    )
+                if not Window.focus:
+                    G.host.desktop_notif(
+                        notif_msg,
+                        title=_(u"mention ({room_jid})").format(
+                            room_jid=self.target)
+                        )
 
     def onSend(self, input_widget):
         G.host.messageSend(