[glib: 1/3] GNotification: Allow to set a category




commit 791218a5f52381e1d4c0840ea35c28acd5886e3b
Author: Guido Günther <agx sigxcpu org>
Date:   Wed Jul 28 16:45:26 2021 +0200

    GNotification: Allow to set a category
    
    Some backends like the FDO one allow to set a category. This helps the
    notification daemon to select a proper feedback type.

 docs/reference/gio/gio-sections-common.txt |  1 +
 gio/gnotification-private.h                |  2 ++
 gio/gnotification.c                        | 48 ++++++++++++++++++++++++++++++
 gio/gnotification.h                        |  4 +++
 gio/tests/gnotification.c                  |  4 +++
 5 files changed, 59 insertions(+)
---
diff --git a/docs/reference/gio/gio-sections-common.txt b/docs/reference/gio/gio-sections-common.txt
index a7addedc2..1b1ddc531 100644
--- a/docs/reference/gio/gio-sections-common.txt
+++ b/docs/reference/gio/gio-sections-common.txt
@@ -4677,6 +4677,7 @@ g_notification_set_icon
 GNotificationPriority
 g_notification_set_priority
 g_notification_set_urgent
+g_notification_set_category
 <SUBSECTION>
 g_notification_set_default_action
 g_notification_set_default_action_and_target
diff --git a/gio/gnotification-private.h b/gio/gnotification-private.h
index e3e4a7818..ee38457fb 100644
--- a/gio/gnotification-private.h
+++ b/gio/gnotification-private.h
@@ -28,6 +28,8 @@ const gchar *           g_notification_get_title                        (GNotifi
 
 const gchar *           g_notification_get_body                         (GNotification *notification);
 
+const gchar *           g_notification_get_category                     (GNotification *notification);
+
 GIcon *                 g_notification_get_icon                         (GNotification *notification);
 
 GNotificationPriority   g_notification_get_priority                     (GNotification *notification);
diff --git a/gio/gnotification.c b/gio/gnotification.c
index aa7df7f7b..f77cd4e9e 100644
--- a/gio/gnotification.c
+++ b/gio/gnotification.c
@@ -97,6 +97,7 @@ struct _GNotification
   gchar *body;
   GIcon *icon;
   GNotificationPriority priority;
+  gchar *category;
   GPtrArray *buttons;
   gchar *default_action;
   GVariant *default_action_target;
@@ -141,6 +142,7 @@ g_notification_finalize (GObject *object)
 
   g_free (notification->title);
   g_free (notification->body);
+  g_free (notification->category);
   g_free (notification->default_action);
   if (notification->default_action_target)
     g_variant_unref (notification->default_action_target);
@@ -347,6 +349,52 @@ g_notification_set_urgent (GNotification *notification,
       G_NOTIFICATION_PRIORITY_NORMAL;
 }
 
+/*< private >
+ * g_notification_get_category:
+ * @notification: a #GNotification
+ *
+ * Gets the cateogry of @notification.
+ *
+ * This will be %NULL if no category is set.
+ *
+ * Returns: (nullable): the cateogry of @notification
+ *
+ * Since: 2.70
+ */
+const gchar *
+g_notification_get_category (GNotification *notification)
+{
+  g_return_val_if_fail (G_IS_NOTIFICATION (notification), NULL);
+
+  return notification->category;
+}
+
+/**
+ * g_notification_set_category:
+ * @notification: a #GNotification
+ * @category: (nullable): the category for @notification, or %NULL for no category
+ *
+ * Sets the type of @notification to @category. Categories have a main
+ * type like `email`, `im` or `device` and can have a detail separated
+ * by a `.`, e.g. `im.received` or `email.arrived`. Setting the category
+ * helps the notification server to select proper feedback to the user.
+ *
+ * Standard categories are [listed in the 
specification](https://specifications.freedesktop.org/notification-spec/latest/ar01s06.html).
+ *
+ * Since: 2.70
+ */
+void
+g_notification_set_category (GNotification *notification,
+                             const gchar   *category)
+{
+  g_return_if_fail (G_IS_NOTIFICATION (notification));
+  g_return_if_fail (category == NULL || *category != '\0');
+
+  g_free (notification->category);
+
+  notification->category = g_strdup (category);
+}
+
 /**
  * g_notification_set_priority:
  * @notification: a #GNotification
diff --git a/gio/gnotification.h b/gio/gnotification.h
index 55e683012..0b1035478 100644
--- a/gio/gnotification.h
+++ b/gio/gnotification.h
@@ -59,6 +59,10 @@ GLIB_AVAILABLE_IN_2_42
 void                    g_notification_set_priority                     (GNotification         *notification,
                                                                          GNotificationPriority  priority);
 
+GLIB_AVAILABLE_IN_2_70
+void                    g_notification_set_category                     (GNotification *notification,
+                                                                         const gchar   *category);
+
 GLIB_AVAILABLE_IN_2_40
 void                    g_notification_add_button                       (GNotification *notification,
                                                                          const gchar   *label,
diff --git a/gio/tests/gnotification.c b/gio/tests/gnotification.c
index 80d476d5a..853983c18 100644
--- a/gio/tests/gnotification.c
+++ b/gio/tests/gnotification.c
@@ -180,6 +180,7 @@ struct _GNotification
   gchar *body;
   GIcon *icon;
   GNotificationPriority priority;
+  gchar *category;
   GPtrArray *buttons;
   gchar *default_action;
   GVariant *default_action_target;
@@ -205,10 +206,12 @@ test_properties (void)
 
   g_notification_set_title (n, "title");
   g_notification_set_body (n, "body");
+  g_notification_set_category (n, "cate.gory");
   icon = g_themed_icon_new ("i-c-o-n");
   g_notification_set_icon (n, icon);
   g_object_unref (icon);
   g_notification_set_priority (n, G_NOTIFICATION_PRIORITY_HIGH);
+  g_notification_set_category (n, "cate.gory");
   g_notification_add_button (n, "label1", "app.action1::target1");
   g_notification_set_default_action (n, "app.action2::target2");
 
@@ -222,6 +225,7 @@ test_properties (void)
   g_assert_cmpstr (names[1], ==, "i-c-o-n-symbolic");
   g_assert_null (names[2]);
   g_assert (rn->priority == G_NOTIFICATION_PRIORITY_HIGH);
+  g_assert_cmpstr (rn->category, ==, "cate.gory");
 
   g_assert_cmpint (rn->buttons->len, ==, 1);
   b = (Button*)rn->buttons->pdata[0];


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]