From 18f8ca4775a44c55212a5f36424a0a4bd118547a Mon Sep 17 00:00:00 2001
From: Nico Alt <nicoalt@posteo.org>
Date: Fri, 25 Sep 2020 20:07:40 +0000
Subject: [PATCH] Show notifications in notification bar

Fixes #28.
---
 briar-gtk/briar_gtk/containers/main_window.py | 24 ++++++++++++++-----
 briar-gtk/briar_gtk/define.py                 |  3 +++
 briar-gtk/briar_gtk/window.py                 |  8 +++++--
 3 files changed, 27 insertions(+), 8 deletions(-)

diff --git a/briar-gtk/briar_gtk/containers/main_window.py b/briar-gtk/briar_gtk/containers/main_window.py
index f2e9a89..394c45d 100644
--- a/briar-gtk/briar_gtk/containers/main_window.py
+++ b/briar-gtk/briar_gtk/containers/main_window.py
@@ -6,13 +6,15 @@
 # https://gitlab.gnome.org/GNOME/fractal/-/tags/4.2.2
 
 from gettext import gettext as _
-from gi.repository import GLib, Gtk
+from gettext import pgettext as _t
+from gi.repository import Gio, GLib, Gtk
 
 from briar_wrapper.models.contacts import Contacts
 
 from briar_gtk.container import Container
 from briar_gtk.containers.private_chat import PrivateChatContainer
-from briar_gtk.define import APP
+from briar_gtk.define import APP, NOTIFICATION_CONTACT_ADDED
+from briar_gtk.define import NOTIFICATION_PRIVATE_MESSAGE
 from briar_gtk.widgets.about_dialog import AboutDialogWidget
 from briar_gtk.widgets.contact_row import ContactRowWidget
 
@@ -259,15 +261,25 @@ class MainWindowContainer(Container):
 
     # pylint: disable=unused-argument
     def _notify_contact_added(self, message):
-        self._notify()
+        self._notify(
+            _t("Notification", "Contact added"),
+            NOTIFICATION_CONTACT_ADDED
+        )
 
     # pylint: disable=unused-argument
     def _notify_message_received(self, message):
-        self._notify()
+        self._notify(
+            _t("Notification", "New private message"),
+            NOTIFICATION_PRIVATE_MESSAGE
+        )
 
     @staticmethod
-    def _notify():
-        APP().window.set_urgency_hint(True)
+    def _notify(title, identifier):
+        if APP().window.is_active():
+            return
+        notification = Gio.Notification.new(title)
+        notification.set_priority(Gio.NotificationPriority.HIGH)
+        APP().send_notification(identifier, notification)
 
     def _refresh_contacts(self):
         self._save_selected_row()
diff --git a/briar-gtk/briar_gtk/define.py b/briar-gtk/briar_gtk/define.py
index 6b323f6..8298a25 100644
--- a/briar-gtk/briar_gtk/define.py
+++ b/briar-gtk/briar_gtk/define.py
@@ -16,6 +16,9 @@ APPLICATION_NAME = "Briar"
 RESOURCES_DIR = os.path.join("/app", "briar", "gtk")
 APPLICATION_STYLING_PATH = "resource:///app/briar/gtk/application.css"
 
+NOTIFICATION_CONTACT_ADDED = "briar-gtk-contact-added"
+NOTIFICATION_PRIVATE_MESSAGE = "briar-gtk-private-message"
+
 APP = Gio.Application.get_default
 
 
diff --git a/briar-gtk/briar_gtk/window.py b/briar-gtk/briar_gtk/window.py
index d52b3d7..5c17bcb 100644
--- a/briar-gtk/briar_gtk/window.py
+++ b/briar-gtk/briar_gtk/window.py
@@ -10,6 +10,8 @@ from briar_gtk.containers.add_contact import AddContactContainer
 from briar_gtk.containers.main_window import MainWindowContainer
 from briar_gtk.containers.startup import StartupContainer
 from briar_gtk.define import APP, APPLICATION_ID, APPLICATION_NAME
+from briar_gtk.define import NOTIFICATION_CONTACT_ADDED
+from briar_gtk.define import NOTIFICATION_PRIVATE_MESSAGE
 
 
 class Window(Gtk.ApplicationWindow):
@@ -72,8 +74,10 @@ class Window(Gtk.ApplicationWindow):
         self.connect("focus-out-event", self._on_focus_change)
 
     # pylint: disable=unused-argument
-    def _on_focus_change(self, widget, event):
-        self.set_urgency_hint(False)
+    @staticmethod
+    def _on_focus_change(widget, event):
+        APP().withdraw_notification(NOTIFICATION_CONTACT_ADDED)
+        APP().withdraw_notification(NOTIFICATION_PRIVATE_MESSAGE)
 
     def _resize_window(self, size):
         if not Window._size_is_valid(size):
-- 
GitLab