changeset 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 6f09bc00a9e7
children ab3f5173ef5c
files cagou/core/cagou_main.py cagou/core/menu.py cagou/plugins/plugin_wid_chat.py setup.py
diffstat 4 files changed, 59 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/cagou/core/cagou_main.py	Tue May 01 21:53:33 2018 +0200
+++ b/cagou/core/cagou_main.py	Thu May 10 08:32:46 2018 +0200
@@ -66,6 +66,11 @@
 import cagou.kv
 from kivy import utils as kivy_utils
 import sys
+try:
+    from plyer import notification
+except ImportError:
+    notification = None
+    log.warning(_(u"Can't import plyer, some features disabled"))
 
 # we want white background by default
 Window.clearcolor = (1, 1, 1, 1)
@@ -727,3 +732,12 @@
         # TODO
         log.info(u"FIXME: showDialog not implemented")
         log.info(u"message: {} -- {}".format(title, message))
+
+
+    def desktop_notif(self, message, title=u'', duration=5000):
+        if notification is not None:
+            notification.notify(title=title,
+                                message=message,
+                                app_name=C.APP_NAME,
+                                app_icon=self.app.icon,
+                                timeout = duration)
--- a/cagou/core/menu.py	Tue May 01 21:53:33 2018 +0200
+++ b/cagou/core/menu.py	Thu May 10 08:32:46 2018 +0200
@@ -34,13 +34,13 @@
 import webbrowser
 
 ABOUT_TITLE = _(u"About {}".format(C.APP_NAME))
-ABOUT_CONTENT = _(u"""Cagou (Salut à Toi) v{}
+ABOUT_CONTENT = _(u"""Cagou (Salut à Toi) v{version}
 
 Cagou is a libre communication tool based on libre standard XMPP.
 
 Cagou is part of the "Salut à Toi" project
 more informations at [color=5500ff][ref=website]salut-a-toi.org[/ref][/color]
-""").format(C.APP_VERSION)
+""").format(version=C.APP_VERSION)
 
 
 class AboutContent(Label):
--- 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(
--- a/setup.py	Tue May 01 21:53:33 2018 +0200
+++ b/setup.py	Thu May 10 08:32:46 2018 +0200
@@ -27,6 +27,7 @@
 install_requires = [
     'kivy',
     'pillow',
+    'plyer',
     'sat',
 ]