diff options
author | Victor Kareh <[email protected]> | 2025-08-27 16:09:55 -0400 |
---|---|---|
committer | Luke from DC <[email protected]> | 2025-10-10 02:11:17 +0000 |
commit | fcde65c52c3ceb4c97af33679ead210b1b1d58e2 (patch) | |
tree | d5204783fa592016f16be85bda850f482a9f56c4 | |
parent | 8e5725e39e66965c2ded9aa1327e8852a37fa663 (diff) | |
download | mate-notification-daemon-fcde65c52c3ceb4c97af33679ead210b1b1d58e2.tar.bz2 mate-notification-daemon-fcde65c52c3ceb4c97af33679ead210b1b1d58e2.tar.xz |
capplet: Add D-Bus context for notification daemon communication
Add D-Bus context module to connect future components of applet with
notification history.
-rw-r--r-- | src/capplet/Makefile.am | 5 | ||||
-rw-r--r-- | src/capplet/mate-notification-applet-dbus.c | 214 | ||||
-rw-r--r-- | src/capplet/mate-notification-applet-dbus.h | 52 | ||||
-rw-r--r-- | src/capplet/mate-notification-applet.c | 22 |
4 files changed, 293 insertions, 0 deletions
diff --git a/src/capplet/Makefile.am b/src/capplet/Makefile.am index af7569a..4126d3f 100644 --- a/src/capplet/Makefile.am +++ b/src/capplet/Makefile.am @@ -45,6 +45,8 @@ libmate_notification_applet_la_SOURCES = \ $(mate_notification_applet_resources_files) \ ../common/constants.h \ mate-notification-applet.c \ + mate-notification-applet-dbus.h \ + mate-notification-applet-dbus.c \ $(NULL) libmate_notification_applet_la_CFLAGS = \ @@ -64,6 +66,8 @@ mate_notification_applet_SOURCES = \ $(mate_notification_applet_resources_files) \ ../common/constants.h \ mate-notification-applet.c \ + mate-notification-applet-dbus.h \ + mate-notification-applet-dbus.c \ $(NULL) mate_notification_applet_CFLAGS = \ @@ -104,6 +108,7 @@ EXTRA_DIST = \ $(mate_notification_properties_resources_xml) \ mate-notification-applet-menu.xml \ mate-notification-properties.ui \ + mate-notification-applet-dbus.h \ $(NULL) -include $(top_srcdir)/git.mk diff --git a/src/capplet/mate-notification-applet-dbus.c b/src/capplet/mate-notification-applet-dbus.c new file mode 100644 index 0000000..68ed6f3 --- /dev/null +++ b/src/capplet/mate-notification-applet-dbus.c @@ -0,0 +1,214 @@ +/* mate-notification-applet-dbus.c - MATE Notification Applet - D-Bus Context + * + * Copyright (C) 2025 MATE Developers + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#include "config.h" + +#include <glib.h> +#include <gio/gio.h> + +#include "mate-notification-applet-dbus.h" +#include "../common/constants.h" + +MateNotificationDBusContext * +dbus_context_new (void) +{ + MateNotificationDBusContext *context = g_new0 (MateNotificationDBusContext, 1); + context->daemon_proxy = NULL; + context->daemon_available = FALSE; + context->watch_id = 0; + return context; +} + +void +dbus_context_free (MateNotificationDBusContext *context) +{ + if (context) { + dbus_context_disconnect (context); + g_free (context); + } +} + +gboolean +dbus_context_connect (MateNotificationDBusContext *context) +{ + GError *error = NULL; + + if (!context) + return FALSE; + + /* Clean up existing connection */ + dbus_context_disconnect (context); + + /* Create D-Bus proxy for notification daemon */ + context->daemon_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION, + G_DBUS_PROXY_FLAGS_NONE, + NULL, /* GDBusInterfaceInfo */ + NOTIFICATION_BUS_NAME, + NOTIFICATION_BUS_PATH, + "org.freedesktop.Notifications", + NULL, /* GCancellable */ + &error); + + if (error) { + g_warning ("Failed to connect to notification daemon: %s", error->message); + g_error_free (error); + context->daemon_available = FALSE; + return FALSE; + } + + context->daemon_available = TRUE; + return TRUE; +} + +void +dbus_context_disconnect (MateNotificationDBusContext *context) +{ + if (!context) + return; + + if (context->daemon_proxy) { + g_object_unref (context->daemon_proxy); + context->daemon_proxy = NULL; + } + + context->daemon_available = FALSE; +} + +gboolean +dbus_context_is_available (MateNotificationDBusContext *context) +{ + return context && context->daemon_available && context->daemon_proxy; +} + +guint +dbus_context_get_notification_count (MateNotificationDBusContext *context) +{ + GVariant *result; + GError *error = NULL; + guint count = 0; + + if (!dbus_context_is_available (context)) + return 0; + + result = g_dbus_proxy_call_sync (context->daemon_proxy, + "GetNotificationCount", + NULL, + G_DBUS_CALL_FLAGS_NONE, + -1, /* timeout */ + NULL, /* GCancellable */ + &error); + + if (error) { + g_warning ("Failed to get notification count: %s", error->message); + g_error_free (error); + context->daemon_available = FALSE; + } else if (result) { + g_variant_get (result, "(u)", &count); + g_variant_unref (result); + } + + return count; +} + +gboolean +dbus_context_clear_notification_history (MateNotificationDBusContext *context) +{ + GVariant *result; + GError *error = NULL; + + if (!dbus_context_is_available (context)) + return FALSE; + + result = g_dbus_proxy_call_sync (context->daemon_proxy, + "ClearNotificationHistory", + NULL, + G_DBUS_CALL_FLAGS_NONE, + -1, /* timeout */ + NULL, /* GCancellable */ + &error); + + if (error) { + g_warning ("Failed to clear notification history: %s", error->message); + g_error_free (error); + return FALSE; + } + + if (result) { + g_variant_unref (result); + } + + return TRUE; +} + +gboolean +dbus_context_mark_all_notifications_as_read (MateNotificationDBusContext *context) +{ + GVariant *result; + GError *error = NULL; + + if (!dbus_context_is_available (context)) + return FALSE; + + result = g_dbus_proxy_call_sync (context->daemon_proxy, + "MarkAllNotificationsAsRead", + NULL, + G_DBUS_CALL_FLAGS_NONE, + -1, /* timeout */ + NULL, /* GCancellable */ + &error); + + if (error) { + g_warning ("Failed to mark all notifications as read: %s", error->message); + g_error_free (error); + return FALSE; + } + + if (result) { + g_variant_unref (result); + } + + return TRUE; +} + +GVariant * +dbus_context_get_notification_history (MateNotificationDBusContext *context) +{ + GVariant *result; + GError *error = NULL; + + if (!dbus_context_is_available (context)) + return NULL; + + result = g_dbus_proxy_call_sync (context->daemon_proxy, + "GetNotificationHistory", + NULL, + G_DBUS_CALL_FLAGS_NONE, + -1, /* timeout */ + NULL, /* GCancellable */ + &error); + + if (error) { + g_warning ("Failed to get notification history: %s", error->message); + g_error_free (error); + return NULL; + } + + return result; /* Caller owns this reference */ +} diff --git a/src/capplet/mate-notification-applet-dbus.h b/src/capplet/mate-notification-applet-dbus.h new file mode 100644 index 0000000..7f95bc3 --- /dev/null +++ b/src/capplet/mate-notification-applet-dbus.h @@ -0,0 +1,52 @@ +/* mate-notification-applet-dbus.h - MATE Notification Applet - D-Bus Context + * + * Copyright (C) 2025 MATE Developers + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#ifndef __MATE_NOTIFICATION_APPLET_DBUS_H__ +#define __MATE_NOTIFICATION_APPLET_DBUS_H__ + +#include <gio/gio.h> + +G_BEGIN_DECLS + +/* D-Bus context structure for notification daemon communication */ +typedef struct { + GDBusProxy *daemon_proxy; + gboolean daemon_available; + guint watch_id; +} MateNotificationDBusContext; + +/* Context creation/cleanup */ +MateNotificationDBusContext *dbus_context_new (void); +void dbus_context_free (MateNotificationDBusContext *context); + +/* Connection management */ +gboolean dbus_context_connect (MateNotificationDBusContext *context); +void dbus_context_disconnect (MateNotificationDBusContext *context); +gboolean dbus_context_is_available (MateNotificationDBusContext *context); + +/* Notification daemon method calls */ +guint dbus_context_get_notification_count (MateNotificationDBusContext *context); +gboolean dbus_context_clear_notification_history (MateNotificationDBusContext *context); +gboolean dbus_context_mark_all_notifications_as_read (MateNotificationDBusContext *context); +GVariant *dbus_context_get_notification_history (MateNotificationDBusContext *context); + +G_END_DECLS + +#endif /* __MATE_NOTIFICATION_APPLET_DBUS_H__ */ diff --git a/src/capplet/mate-notification-applet.c b/src/capplet/mate-notification-applet.c index 7402707..f7d02e0 100644 --- a/src/capplet/mate-notification-applet.c +++ b/src/capplet/mate-notification-applet.c @@ -25,11 +25,13 @@ #include <glib/gi18n-lib.h> #include <gtk/gtk.h> #include <mate-panel-applet.h> +#include <gio/gio.h> #define MATE_DESKTOP_USE_UNSTABLE_API #include <libmate-desktop/mate-desktop-utils.h> #include "constants.h" +#include "mate-notification-applet-dbus.h" typedef struct { @@ -39,6 +41,8 @@ typedef struct GtkWidget *image_off; GtkActionGroup *action_group; GSettings *settings; + + MateNotificationDBusContext *dbus_context; } MateNotificationApplet; static void @@ -48,6 +52,9 @@ static void call_properties (GtkAction *action, MateNotificationApplet *applet); +static void +setup_daemon_connection (MateNotificationApplet *applet); + static const GtkActionEntry applet_menu_actions [] = { { "Preferences", "document-properties", N_("_Preferences"), NULL, NULL, G_CALLBACK (call_properties) }, @@ -65,6 +72,10 @@ applet_destroy (MatePanelApplet *applet_widget, { g_assert (applet); + if (applet->dbus_context) { + dbus_context_free (applet->dbus_context); + } + g_object_unref (applet->settings); g_object_unref (applet->action_group); g_free (applet); @@ -161,6 +172,12 @@ applet_draw_icon (MatePanelApplet *applet_widget, cairo_surface_destroy (image_off); } +static void +setup_daemon_connection (MateNotificationApplet *applet) +{ + dbus_context_connect (applet->dbus_context); +} + static MateNotificationApplet* applet_main (MatePanelApplet *applet_widget) { @@ -176,6 +193,9 @@ applet_main (MatePanelApplet *applet_widget) applet->applet = applet_widget; applet->settings = g_settings_new (GSETTINGS_SCHEMA); + /* Initialize D-Bus context */ + applet->dbus_context = dbus_context_new (); + #ifndef ENABLE_IN_PROCESS /* needed to clamp ourselves to the panel size */ mate_panel_applet_set_flags (MATE_PANEL_APPLET (applet), MATE_PANEL_APPLET_EXPAND_MINOR); @@ -230,6 +250,8 @@ applet_main (MatePanelApplet *applet_widget) g_signal_connect (G_OBJECT (applet->settings), "changed::" GSETTINGS_KEY_DO_NOT_DISTURB, G_CALLBACK (settings_changed), applet); + setup_daemon_connection (applet); + return applet; } |