Add GNotification

https://bugzilla.gnome.org/show_bug.cgi?id=688492
This commit is contained in:
Lars Uebernickel 2013-10-15 11:21:10 -04:00 committed by Ryan Lortie
parent 1b04954cd0
commit 639bd3626b
12 changed files with 1512 additions and 0 deletions

View File

@ -152,6 +152,9 @@ application_headers = \
gmenu.h \
gmenuexporter.h \
gdbusmenumodel.h \
gnotification.h \
gnotification-private.h \
gnotificationbackend.h \
$(NULL)
application_sources = \
@ -175,6 +178,8 @@ application_sources = \
gmenu.c \
gmenuexporter.c \
gdbusmenumodel.c \
gnotification.c \
gnotificationbackend.c \
$(NULL)
local_sources = \
@ -261,6 +266,7 @@ unix_sources = \
gunixoutputstream.c \
gcontenttype.c \
gcontenttypeprivate.h \
gfdonotificationbackend.c \
$(NULL)

View File

@ -32,6 +32,9 @@
#include "gactionmap.h"
#include "gmenumodel.h"
#include "gsettings.h"
#include "gnotification-private.h"
#include "gnotificationbackend.h"
#include "gdbusutils.h"
#include "gioenumtypes.h"
#include "gioenums.h"
@ -249,6 +252,8 @@ struct _GApplicationPrivate
GRemoteActionGroup *remote_actions;
GApplicationImpl *impl;
GNotificationBackend *notifications;
};
enum
@ -686,6 +691,9 @@ g_application_finalize (GObject *object)
if (application->priv->actions)
g_object_unref (application->priv->actions);
if (application->priv->notifications)
g_object_unref (application->priv->notifications);
G_OBJECT_CLASS (g_application_parent_class)
->finalize (object);
}
@ -1919,5 +1927,96 @@ g_application_unmark_busy (GApplication *application)
g_application_impl_set_busy_state (application->priv->impl, FALSE);
}
/* Notifications {{{1 */
/**
* g_application_send_notification:
* @application: a #GApplication
* @id: (allow-none): id of the notification, or %NULL
* @notification: the #GNotification to send
*
* Sends a notification on behalf of @application to the desktop shell.
* There is no guarantee that the notification is displayed immediately,
* or even at all.
*
* Notifications may persist after the application exits. It will be
* D-Bus-activated when the notification or one of its actions is
* activated.
*
* Modifying @notification after this call has no effect. However, the
* object can be reused for a later call to this function.
*
* @id may be any string that uniquely identifies the event for the
* application. It does not need to be in any special format. For
* example, "new-message" might be appropriate for a notification about
* new messages.
*
* If a previous notification was sent with the same @id, it will be
* replaced with @notification and shown again as if it was a new
* notification. This works even for notifications sent from a previous
* execution of the application, as long as @id is the same string.
*
* @id may be %NULL, but it is impossible to replace or withdraw
* notifications without an id.
*
* If @notification is no longer relevant, it can be withdrawn with
* g_application_withdraw_notification().
*
* Since: 2.40
*/
void
g_application_send_notification (GApplication *application,
const gchar *id,
GNotification *notification)
{
gchar *generated_id = NULL;
g_return_if_fail (G_IS_APPLICATION (application));
g_return_if_fail (G_IS_NOTIFICATION (notification));
g_return_if_fail (g_application_get_is_registered (application));
g_return_if_fail (!g_application_get_is_remote (application));
if (application->priv->notifications == NULL)
application->priv->notifications = g_notification_backend_new_default (application);
if (id == NULL)
{
generated_id = g_dbus_generate_guid ();
id = generated_id;
}
g_notification_backend_send_notification (application->priv->notifications, id, notification);
g_free (generated_id);
}
/**
* g_application_withdraw_notification:
* @application: a #GApplication
* @id: id of a previously sent notification
*
* Withdraws a notification that was sent with
* g_application_send_notification().
*
* This call does nothing if a notification with @id doesn't exist or
* the notification was never sent.
*
* This function works even for notifications sent in previous
* executions of this application, as long @id is the same as it was for
* the sent notification.
*
* Since: 2.40
*/
void
g_application_withdraw_notification (GApplication *application,
const gchar *id)
{
g_return_if_fail (G_IS_APPLICATION (application));
g_return_if_fail (id != NULL);
if (application->priv->notifications)
g_notification_backend_withdraw_notification (application->priv->notifications, id);
}
/* Epilogue {{{1 */
/* vim:set foldmethod=marker: */

View File

@ -201,6 +201,14 @@ void g_application_mark_busy (GApplic
GLIB_AVAILABLE_IN_2_38
void g_application_unmark_busy (GApplication *application);
GLIB_AVAILABLE_IN_2_40
void g_application_send_notification (GApplication *application,
const gchar *id,
GNotification *notification);
GLIB_AVAILABLE_IN_2_40
void g_application_withdraw_notification (GApplication *application,
const gchar *id);
G_END_DECLS
#endif /* __G_APPLICATION_H__ */

View File

@ -0,0 +1,405 @@
/*
* Copyright © 2013 Lars Uebernickel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authors: Lars Uebernickel <lars@uebernic.de>
*/
#include "gnotificationbackend.h"
#include "gapplication.h"
#include "giomodule-priv.h"
#include "gnotification-private.h"
#include "gdbusconnection.h"
#include "gaction.h"
#include "gfileicon.h"
#include "gfile.h"
#include "gdbusutils.h"
#define G_TYPE_FDO_NOTIFICATION_BACKEND (g_fdo_notification_backend_get_type ())
#define G_FDO_NOTIFICATION_BACKEND(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_FDO_NOTIFICATION_BACKEND, GFdoNotificationBackend))
typedef struct _GFdoNotificationBackend GFdoNotificationBackend;
typedef GNotificationBackendClass GFdoNotificationBackendClass;
struct _GFdoNotificationBackend
{
GNotificationBackend parent;
guint notify_subscription;
GSList *notifications;
};
GType g_fdo_notification_backend_get_type (void);
G_DEFINE_TYPE_WITH_CODE (GFdoNotificationBackend, g_fdo_notification_backend, G_TYPE_NOTIFICATION_BACKEND,
_g_io_modules_ensure_extension_points_registered ();
g_io_extension_point_implement (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME,
g_define_type_id, "freedesktop", 0))
typedef struct
{
GFdoNotificationBackend *backend;
gchar *id;
guint32 notify_id;
gchar *default_action;
GVariant *default_action_target;
} FreedesktopNotification;
static void
freedesktop_notification_free (gpointer data)
{
FreedesktopNotification *n = data;
g_free (n->id);
g_free (n->default_action);
if (n->default_action_target)
g_variant_unref (n->default_action_target);
g_slice_free (FreedesktopNotification, n);
}
static FreedesktopNotification *
g_fdo_notification_backend_find_notification (GFdoNotificationBackend *backend,
const gchar *id)
{
GSList *it;
for (it = backend->notifications; it != NULL; it = it->next)
{
FreedesktopNotification *n = it->data;
if (g_str_equal (n->id, id))
return n;
}
return NULL;
}
static FreedesktopNotification *
g_fdo_notification_backend_find_notification_by_notify_id (GFdoNotificationBackend *backend,
guint32 id)
{
GSList *it;
for (it = backend->notifications; it != NULL; it = it->next)
{
FreedesktopNotification *n = it->data;
if (n->notify_id == id)
return n;
}
return NULL;
}
static void
notify_signal (GDBusConnection *connection,
const gchar *sender_name,
const gchar *object_path,
const gchar *interface_name,
const gchar *signal_name,
GVariant *parameters,
gpointer user_data)
{
GFdoNotificationBackend *backend = user_data;
guint32 id = 0;
const gchar *action = NULL;
FreedesktopNotification *n;
if (g_str_equal (signal_name, "NotificationClosed") &&
g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(uu)")))
{
g_variant_get (parameters, "(uu)", &id, NULL);
}
else if (g_str_equal (signal_name, "ActionInvoked") &&
g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(us)")))
{
g_variant_get (parameters, "(u&s)", &id, &action);
}
else
return;
n = g_fdo_notification_backend_find_notification_by_notify_id (backend, id);
if (n == NULL)
return;
if (action)
{
if (g_str_equal (action, "default"))
{
g_notification_backend_activate_action (G_NOTIFICATION_BACKEND (backend),
n->default_action,
n->default_action_target);
}
else
{
gchar *name;
GVariant *target;
if (g_action_parse_detailed_name (action, &name, &target, NULL))
{
g_notification_backend_activate_action (G_NOTIFICATION_BACKEND (backend), name, target);
g_free (name);
if (target)
g_variant_unref (target);
}
}
}
backend->notifications = g_slist_remove (backend->notifications, n);
freedesktop_notification_free (n);
}
static void
call_notify (GDBusConnection *con,
GApplication *app,
guint32 replace_id,
GNotification *notification,
GAsyncReadyCallback callback,
gpointer user_data)
{
GVariantBuilder action_builder;
guint n_buttons;
guint i;
GVariantBuilder hints_builder;
GIcon *image;
GVariant *parameters;
const gchar *body;
g_variant_builder_init (&action_builder, G_VARIANT_TYPE_STRING_ARRAY);
if (g_notification_get_default_action (notification, NULL, NULL))
{
g_variant_builder_add (&action_builder, "s", "default");
g_variant_builder_add (&action_builder, "s", "");
}
n_buttons = g_notification_get_n_buttons (notification);
for (i = 0; i < n_buttons; i++)
{
gchar *label;
gchar *action;
GVariant *target;
gchar *detailed_name;
g_notification_get_button (notification, i, &label, &action, &target);
detailed_name = g_action_print_detailed_name (action, target);
/* Actions named 'default' collide with libnotify's naming of the
* default action. Rewriting them to something unique is enough,
* because those actions can never be activated (they aren't
* prefixed with 'app.').
*/
if (g_str_equal (detailed_name, "default"))
{
g_free (detailed_name);
detailed_name = g_dbus_generate_guid ();
}
g_variant_builder_add_value (&action_builder, g_variant_new_take_string (detailed_name));
g_variant_builder_add_value (&action_builder, g_variant_new_take_string (label));
g_free (action);
if (target)
g_variant_unref (target);
}
g_variant_builder_init (&hints_builder, G_VARIANT_TYPE ("a{sv}"));
g_variant_builder_add (&hints_builder, "{sv}", "desktop-entry",
g_variant_new_string (g_application_get_application_id (app)));
if (g_notification_get_urgent (notification))
g_variant_builder_add (&hints_builder, "{sv}", "urgency", g_variant_new_byte (2));
image = g_notification_get_image (notification);
if (image != NULL && G_IS_FILE_ICON (image))
{
GFile *file;
file = g_file_icon_get_file (G_FILE_ICON (image));
g_variant_builder_add (&hints_builder, "{sv}", "image-path",
g_variant_new_take_string (g_file_get_uri (file)));
}
body = g_notification_get_body (notification);
parameters = g_variant_new ("(susssasa{sv}i)",
"", /* app name */
replace_id,
"", /* app icon */
g_notification_get_title (notification),
body ? body : "",
&action_builder,
&hints_builder,
-1); /* expire_timeout */
g_dbus_connection_call (con, "org.freedesktop.Notifications", "/org/freedesktop/Notifications",
"org.freedesktop.Notifications", "Notify",
parameters, G_VARIANT_TYPE ("(u)"),
G_DBUS_CALL_FLAGS_NONE, -1, NULL,
callback, user_data);
}
static void
notification_sent (GObject *source_object,
GAsyncResult *result,
gpointer user_data)
{
FreedesktopNotification *n = user_data;
GVariant *val;
GError *error = NULL;
static gboolean warning_printed = FALSE;
val = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), result, &error);
if (val)
{
g_variant_get (val, "(u)", &n->notify_id);
g_variant_unref (val);
}
else
{
if (!warning_printed)
{
g_warning ("unable to send notifications through org.freedesktop.Notifications: %s",
error->message);
warning_printed = TRUE;
}
n->backend->notifications = g_slist_remove (n->backend->notifications, n);
freedesktop_notification_free (n);
g_error_free (error);
}
}
static void
g_fdo_notification_backend_dispose (GObject *object)
{
GFdoNotificationBackend *backend = G_FDO_NOTIFICATION_BACKEND (object);
if (backend->notify_subscription)
{
GDBusConnection *session_bus;
session_bus = g_notification_backend_get_dbus_connection (G_NOTIFICATION_BACKEND (backend));
g_dbus_connection_signal_unsubscribe (session_bus, backend->notify_subscription);
backend->notify_subscription = 0;
}
if (backend->notifications)
{
g_slist_free_full (backend->notifications, freedesktop_notification_free);
backend->notifications = NULL;
}
G_OBJECT_CLASS (g_fdo_notification_backend_parent_class)->dispose (object);
}
static gboolean
g_fdo_notification_backend_is_supported (void)
{
/* This is the fallback backend with the lowest priority. To avoid an
* unnecessary synchronous dbus call to check for
* org.freedesktop.Notifications, this function always succeeds. A
* warning will be printed when sending the first notification fails.
*/
return TRUE;
}
static void
g_fdo_notification_backend_send_notification (GNotificationBackend *backend,
const gchar *id,
GNotification *notification)
{
GFdoNotificationBackend *self = G_FDO_NOTIFICATION_BACKEND (backend);
FreedesktopNotification *n;
if (self->notify_subscription == 0)
{
self->notify_subscription =
g_dbus_connection_signal_subscribe (g_notification_backend_get_dbus_connection (backend),
"org.freedesktop.Notifications",
"org.freedesktop.Notifications", NULL,
"/org/freedesktop/Notifications", NULL,
G_DBUS_SIGNAL_FLAGS_NONE,
notify_signal, backend, NULL);
}
n = g_fdo_notification_backend_find_notification (self, id);
if (n == NULL)
{
n = g_slice_new (FreedesktopNotification);
n->backend = self;
n->id = g_strdup (id);
n->notify_id = 0;
n->backend->notifications = g_slist_prepend (n->backend->notifications, n);
}
else
{
/* Only clear default action. All other fields are still valid */
g_free (n->default_action);
if (n->default_action_target)
g_variant_unref (n->default_action_target);
}
g_notification_get_default_action (notification, &n->default_action, &n->default_action_target);
call_notify (g_notification_backend_get_dbus_connection (backend),
g_notification_backend_get_application (backend),
n->notify_id, notification, notification_sent, n);
}
static void
g_fdo_notification_backend_withdraw_notification (GNotificationBackend *backend,
const gchar *id)
{
GFdoNotificationBackend *self = G_FDO_NOTIFICATION_BACKEND (backend);
FreedesktopNotification *n;
n = g_fdo_notification_backend_find_notification (self, id);
if (n)
{
if (n->notify_id > 0)
{
g_dbus_connection_call (g_notification_backend_get_dbus_connection (backend),
"org.freedesktop.Notifications",
"/org/freedesktop/Notifications",
"org.freedesktop.Notifications", "CloseNotification",
g_variant_new ("(u)", n->id), NULL,
G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
}
self->notifications = g_slist_remove (self->notifications, n);
freedesktop_notification_free (n);
}
}
static void
g_fdo_notification_backend_init (GFdoNotificationBackend *backend)
{
}
static void
g_fdo_notification_backend_class_init (GFdoNotificationBackendClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
GNotificationBackendClass *backend_class = G_NOTIFICATION_BACKEND_CLASS (class);
object_class->dispose = g_fdo_notification_backend_dispose;
backend_class->is_supported = g_fdo_notification_backend_is_supported;
backend_class->send_notification = g_fdo_notification_backend_send_notification;
backend_class->withdraw_notification = g_fdo_notification_backend_withdraw_notification;
}

View File

@ -161,6 +161,7 @@
#include <gio/gmenu.h>
#include <gio/gmenuexporter.h>
#include <gio/gdbusmenumodel.h>
#include <gio/gnotification.h>
#undef __GIO_GIO_H_INSIDE__

View File

@ -37,6 +37,7 @@
#include "gsocks5proxy.h"
#include "gtlsbackend.h"
#include "gvfs.h"
#include "gnotificationbackend.h"
#ifdef G_OS_WIN32
#include "gregistrysettingsbackend.h"
#endif
@ -894,6 +895,10 @@ extern GType g_network_monitor_base_get_type (void);
extern GType _g_network_monitor_netlink_get_type (void);
#endif
#ifdef G_OS_UNIX
extern GType g_fdo_notification_backend_get_type (void);
#endif
#ifdef G_PLATFORM_WIN32
#include <windows.h>
@ -997,6 +1002,9 @@ _g_io_modules_ensure_extension_points_registered (void)
ep = g_io_extension_point_register (G_NETWORK_MONITOR_EXTENSION_POINT_NAME);
g_io_extension_point_set_required_type (ep, G_TYPE_NETWORK_MONITOR);
ep = g_io_extension_point_register (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME);
g_io_extension_point_set_required_type (ep, G_TYPE_NOTIFICATION_BACKEND);
}
G_UNLOCK (registered_extensions);
@ -1065,6 +1073,7 @@ _g_io_modules_ensure_loaded (void)
#endif
#ifdef G_OS_UNIX
g_type_ensure (_g_unix_volume_monitor_get_type ());
g_type_ensure (g_fdo_notification_backend_get_type ());
#endif
#ifdef G_OS_WIN32
g_type_ensure (_g_winhttp_vfs_get_type ());

View File

@ -62,6 +62,8 @@ typedef struct _GSettings GSettings;
typedef struct _GPermission GPermission;
typedef struct _GMenuModel GMenuModel;
typedef struct _GNotificationBackend GNotificationBackend;
typedef struct _GNotification GNotification;
/**
* GDrive:

View File

@ -0,0 +1,54 @@
/*
* Copyright © 2013 Lars Uebernickel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authors: Lars Uebernickel <lars@uebernic.de>
*/
#ifndef __G_NOTIFICATION_PRIVATE_H__
#define __G_NOTIFICATION_PRIVATE_H__
#include "gnotification.h"
const gchar * g_notification_get_id (GNotification *notification);
const gchar * g_notification_get_title (GNotification *notification);
const gchar * g_notification_get_body (GNotification *notification);
GIcon * g_notification_get_image (GNotification *notification);
gboolean g_notification_get_urgent (GNotification *notification);
guint g_notification_get_n_buttons (GNotification *notification);
void g_notification_get_button (GNotification *notification,
gint index,
gchar **label,
gchar **action,
GVariant **target);
gint g_notification_get_button_with_action (GNotification *notification,
const gchar *action);
gboolean g_notification_get_default_action (GNotification *notification,
gchar **action,
GVariant **target);
GVariant * g_notification_serialize (GNotification *notification);
#endif

639
gio/gnotification.c Normal file
View File

@ -0,0 +1,639 @@
/*
* Copyright © 2013 Lars Uebernickel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authors: Lars Uebernickel <lars@uebernic.de>
*/
#include "config.h"
#include "gnotification-private.h"
#include "gdbusutils.h"
#include "gicon.h"
#include "gaction.h"
typedef GObjectClass GNotificationClass;
struct _GNotification
{
GObject parent;
gchar *title;
gchar *body;
GIcon *image;
gboolean urgent;
GPtrArray *buttons;
gchar *default_action;
GVariant *default_action_target;
};
typedef struct
{
gchar *label;
gchar *action_name;
GVariant *target;
} Button;
G_DEFINE_TYPE (GNotification, g_notification, G_TYPE_OBJECT);
static void
button_free (gpointer data)
{
Button *button = data;
g_free (button->label);
g_free (button->action_name);
if (button->target)
g_variant_unref (button->target);
g_slice_free (Button, button);
}
static void
g_notification_dispose (GObject *object)
{
GNotification *notification = G_NOTIFICATION (object);
g_clear_object (&notification->image);
G_OBJECT_CLASS (g_notification_parent_class)->dispose (object);
}
static void
g_notification_finalize (GObject *object)
{
GNotification *notification = G_NOTIFICATION (object);
g_free (notification->title);
g_free (notification->body);
g_free (notification->default_action);
if (notification->default_action_target)
g_variant_unref (notification->default_action_target);
g_ptr_array_free (notification->buttons, TRUE);
G_OBJECT_CLASS (g_notification_parent_class)->finalize (object);
}
static void
g_notification_class_init (GNotificationClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->dispose = g_notification_dispose;
object_class->finalize = g_notification_finalize;
}
static void
g_notification_init (GNotification *notification)
{
notification->buttons = g_ptr_array_new_full (2, button_free);
}
/**
* g_notification_new:
* @title: the title of the notification
*
* Creates a new #GNotification with @title as its title.
*
* After populating @notification with more details, it can be sent to
* the desktop shell with g_application_send_notification(). Changing
* any properties after this call will not have any effect until
* resending @notification.
*
* Returns: a new #GNotification instance
*
* Since: 2.40
*/
GNotification *
g_notification_new (const gchar *title)
{
GNotification *notification;
g_return_val_if_fail (title != NULL, NULL);
notification = g_object_new (G_TYPE_NOTIFICATION, NULL);
notification->title = g_strdup (title);
return notification;
}
/**
* g_notification_get_title:
* @notification: a #GNotification
*
* Gets the title of @notification.
*
* Returns: the title of @notification
*
* Since: 2.40
*/
const gchar *
g_notification_get_title (GNotification *notification)
{
g_return_val_if_fail (G_IS_NOTIFICATION (notification), NULL);
return notification->title;
}
/**
* g_notification_set_title:
* @notification: a #GNotification
* title: the new title for @notification
*
* Sets the title of @notification to @title.
*
* Since: 2.40
*/
void
g_notification_set_title (GNotification *notification,
const gchar *title)
{
g_return_if_fail (G_IS_NOTIFICATION (notification));
g_return_if_fail (title != NULL);
g_free (notification->title);
notification->title = g_strdup (title);
}
/**
* g_notification_get_body:
* @notification: a #GNotification
*
* Gets the current body of @notification.
*
* Returns: (allow-none): the body of @notification
*
* Since: 2.40
*/
const gchar *
g_notification_get_body (GNotification *notification)
{
g_return_val_if_fail (G_IS_NOTIFICATION (notification), NULL);
return notification->body;
}
/**
* g_notification_set_body:
* @notification: a #GNotification
* @body: (allow-none): the new body for @notification, or %NULL
*
* Sets the body of @notification to @body.
*
* Since: 2.40
*/
void
g_notification_set_body (GNotification *notification,
const gchar *body)
{
g_return_if_fail (G_IS_NOTIFICATION (notification));
g_return_if_fail (body != NULL);
g_free (notification->body);
notification->body = g_strdup (body);
}
/**
* g_notification_get_image:
* @notification: a #GNotification
*
* Gets the image currently set on @notification.
*
* Returns: (transfer none): the image associated with @notification
*
* Since: 2.40
*/
GIcon *
g_notification_get_image (GNotification *notification)
{
g_return_val_if_fail (G_IS_NOTIFICATION (notification), NULL);
return notification->image;
}
/**
* g_notification_set_image:
* @notification: a #GNotification
* @image: the image to be shown in @notification, as a #GIcon
*
* Sets the image of @notification to @image.
*
* Since: 2.40
*/
void
g_notification_set_image (GNotification *notification,
GIcon *image)
{
g_return_if_fail (G_IS_NOTIFICATION (notification));
if (notification->image)
g_object_unref (notification->image);
notification->image = g_object_ref (image);
}
/**
* g_notification_get_urgent:
* @notification: a #GNotification
*
* Returns %TRUE if @notification is marked as urgent.
*
* Since: 2.40
*/
gboolean
g_notification_get_urgent (GNotification *notification)
{
g_return_val_if_fail (G_IS_NOTIFICATION (notification), FALSE);
return notification->urgent;
}
/**
* g_notification_set_urgent:
* @notification: a #GNotification
* @urgent: %TRUE if @notification is urgent
*
* Sets or unsets whether @notification is marked as urgent.
*
* Since: 2.40
*/
void
g_notification_set_urgent (GNotification *notification,
gboolean urgent)
{
g_return_if_fail (G_IS_NOTIFICATION (notification));
notification->urgent = urgent;
}
/**
* g_notification_add_button:
* @notification: a #GNotification
* @label: label of the button
* @detailed_action: a detailed action name
*
* Adds a button to @notification that activates the action in
* @detailed_action when clicked. That action must be an
* application-wide action (starting with "app."). If @detailed_action
* contains a target, the action will be activated with that target as
* its parameter.
*
* See g_action_parse_detailed_name() for a description of the format
* for @detailed_action.
*
* Since: 2.40
*/
void
g_notification_add_button (GNotification *notification,
const gchar *label,
const gchar *detailed_action)
{
gchar *action;
GVariant *target;
GError *error = NULL;
g_return_if_fail (detailed_action != NULL);
if (!g_action_parse_detailed_name (detailed_action, &action, &target, &error))
{
g_warning ("%s: %s", G_STRFUNC, error->message);
g_error_free (error);
return;
}
g_notification_add_button_with_target_value (notification, label, action, target);
g_free (action);
if (target)
g_variant_unref (target);
}
/**
* g_notification_add_button_with_target: (skip)
* @notification: a #GNotification
* @label: label of the button
* @action: an action name
* @target_format: (allow-none): a GVariant format string, or %NULL
* @...: positional parameters, as determined by @format_string
*
* Adds a button to @notification that activates @action when clicked.
* @action must be an application-wide action (it must start with "app.").
*
* If @target_format is given, it is used to collect remaining
* positional parameters into a GVariant instance, similar to
* g_variant_new(). @action will be activated with that GVariant as its
* parameter.
*
* Since: 2.40
*/
void
g_notification_add_button_with_target (GNotification *notification,
const gchar *label,
const gchar *action,
const gchar *target_format,
...)
{
va_list args;
GVariant *target = NULL;
if (target_format)
{
va_start (args, target_format);
target = g_variant_new_va (target_format, NULL, &args);
va_end (args);
}
g_notification_add_button_with_target_value (notification, label, action, target);
}
/**
* g_notification_add_button_with_target_value: (rename-to g_notification_add_button_with_target)
* @notification: a #GNotification
* @label: label of the button
* @action: an action name
* @target: (allow-none): a GVariant to use as @action's parameter, or %NULL
*
* Adds a button to @notification that activates @action when clicked.
* @action must be an application-wide action (it must start with "app.").
*
* If @target is non-%NULL, @action will be activated with @target as
* its parameter.
*
* Since: 2.40
*/
void
g_notification_add_button_with_target_value (GNotification *notification,
const gchar *label,
const gchar *action,
GVariant *target)
{
Button *button;
g_return_if_fail (G_IS_NOTIFICATION (notification));
g_return_if_fail (label != NULL);
g_return_if_fail (action != NULL && g_action_name_is_valid (action));
if (!g_str_has_prefix (action, "app."))
{
g_warning ("%s: action '%s' does not start with 'app.'."
"This is unlikely to work properly.", G_STRFUNC, action);
}
button = g_slice_new0 (Button);
button->label = g_strdup (label);
button->action_name = g_strdup (action);
if (target)
button->target = g_variant_ref_sink (target);
g_ptr_array_add (notification->buttons, button);
}
/*< private >
* g_notification_get_n_buttons:
* @notification: a #GNotification
*
* Returns: the amount of buttons added to @notification.
*/
guint
g_notification_get_n_buttons (GNotification *notification)
{
return notification->buttons->len;
}
/*< private >
* g_notification_get_button:
* @notification: a #GNotification
* @index: index of the button
* @label: (): return location for the button's label
* @action: (): return location for the button's associated action
* @target: (): return location for the target @action should be
* activated with
*
* Returns a description of a button that was added to @notification
* with g_notification_add_button().
*
* @index must be smaller than the value returned by
* g_notification_get_n_buttons().
*/
void
g_notification_get_button (GNotification *notification,
gint index,
gchar **label,
gchar **action,
GVariant **target)
{
Button *button;
button = g_ptr_array_index (notification->buttons, index);
if (label)
*label = g_strdup (button->label);
if (action)
*action = g_strdup (button->action_name);
if (target)
*target = button->target ? g_variant_ref (button->target) : NULL;
}
/*< private >
* g_notification_get_button_with_action:
* @notification: a #GNotification
* @action: an action name
*
* Returns the index of the button in @notification that is associated
* with @action, or -1 if no such button exists.
*/
gint
g_notification_get_button_with_action (GNotification *notification,
const gchar *action)
{
guint i;
for (i = 0; i < notification->buttons->len; i++)
{
Button *button;
button = g_ptr_array_index (notification->buttons, i);
if (g_str_equal (action, button->action_name))
return i;
}
return -1;
}
/*< private >
* g_notification_get_default_action:
* @notification: a #GNotification
* @action: (allow-none): return location for the default action
* @target: (allow-none): return location for the target of the default action
*
* Gets the action and target for the default action of @notification.
*
* Returns: %TRUE if @notification has a default action
*/
gboolean
g_notification_get_default_action (GNotification *notification,
gchar **action,
GVariant **target)
{
if (notification->default_action == NULL)
return FALSE;
if (action)
*action = g_strdup (notification->default_action);
if (target)
{
if (notification->default_action_target)
*target = g_variant_ref (notification->default_action_target);
else
*target = NULL;
}
return TRUE;
}
/**
* g_notification_set_default_action:
* @notification: a #GNotification
* @detailed_action: a detailed action name
*
* Sets the default action of @notification to @detailed_action. This
* action is activated when the notification is clicked on.
*
* The action in @detailed_action must be an application-wide action (it
* must start with "app."). If @detailed_action contains a target, the
* given action will be activated with that target as its parameter.
* See g_action_parse_detailed_name() for a description of the format
* for @detailed_action.
*
* When no default action is set, the application that the notification
* was sent on is activated.
*
* Since: 2.40
*/
void
g_notification_set_default_action (GNotification *notification,
const gchar *detailed_action)
{
gchar *action;
GVariant *target;
GError *error = NULL;
if (!g_action_parse_detailed_name (detailed_action, &action, &target, &error))
{
g_warning ("%s: %s", G_STRFUNC, error->message);
g_error_free (error);
return;
}
g_notification_set_default_action_and_target_value (notification, action, target);
g_free (action);
if (target)
g_variant_unref (target);
}
/**
* g_notification_set_default_action_and_target: (skip)
* @notification: a #GNotification
* @action: an action name
* @target_format: (allow-none): a GVariant format string, or %NULL
* @...: positional parameters, as determined by @format_string
*
* Sets the default action of @notification to @action. This action is
* activated when the notification is clicked on. It must be an
* application-wide action (it must start with "app.").
*
* If @target_format is given, it is used to collect remaining
* positional parameters into a GVariant instance, similar to
* g_variant_new(). @action will be activated with that GVariant as its
* parameter.
*
* When no default action is set, the application that the notification
* was sent on is activated.
*
* Since: 2.40
*/
void
g_notification_set_default_action_and_target (GNotification *notification,
const gchar *action,
const gchar *target_format,
...)
{
va_list args;
GVariant *target = NULL;
if (target_format)
{
va_start (args, target_format);
target = g_variant_new_va (target_format, NULL, &args);
va_end (args);
}
g_notification_set_default_action_and_target_value (notification, action, target);
}
/**
* g_notification_set_default_action_and_target_value: (rename-to g_notification_set_default_action_and_target)
* @notification: a #GNotification
* @action: an action name
* @target: (allow-none): a GVariant to use as @action's parameter, or %NULL
*
* Sets the default action of @notification to @action. This action is
* activated when the notification is clicked on. It must be an
* application-wide action (start with "app.").
*
* If @target_format is given, it is used to collect remaining
* positional parameters into a GVariant instance, similar to
* g_variant_new().
*
* If @target is non-%NULL, @action will be activated with @target as
* its parameter.
*
* When no default action is set, the application that the notification
* was sent on is activated.
*
* Since: 2.40
*/
void
g_notification_set_default_action_and_target_value (GNotification *notification,
const gchar *action,
GVariant *target)
{
g_return_if_fail (G_IS_NOTIFICATION (notification));
g_return_if_fail (action != NULL && g_action_name_is_valid (action));
if (!g_str_has_prefix (action, "app."))
{
g_warning ("%s: action '%s' does not start with 'app.'."
"This is unlikely to work properly.", G_STRFUNC, action);
}
g_free (notification->default_action);
g_clear_pointer (&notification->default_action_target, g_variant_unref);
notification->default_action = g_strdup (action);
if (target)
notification->default_action_target = g_variant_ref_sink (target);
}

94
gio/gnotification.h Normal file
View File

@ -0,0 +1,94 @@
/*
* Copyright © 2013 Lars Uebernickel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authors: Lars Uebernickel <lars@uebernic.de>
*/
#ifndef __G_NOTIFICATION_H__
#define __G_NOTIFICATION_H__
#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
#error "Only <gio/gio.h> can be included directly."
#endif
#include <gio/giotypes.h>
G_BEGIN_DECLS
#define G_TYPE_NOTIFICATION (g_notification_get_type ())
#define G_NOTIFICATION(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_NOTIFICATION, GNotification))
#define G_IS_NOTIFICATION(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_NOTIFICATION))
GLIB_AVAILABLE_IN_2_40
GType g_notification_get_type (void) G_GNUC_CONST;
GLIB_AVAILABLE_IN_2_40
GNotification * g_notification_new (const gchar *title);
GLIB_AVAILABLE_IN_2_40
void g_notification_set_title (GNotification *notification,
const gchar *title);
GLIB_AVAILABLE_IN_2_40
void g_notification_set_body (GNotification *notification,
const gchar *body);
GLIB_AVAILABLE_IN_2_40
void g_notification_set_image (GNotification *notification,
GIcon *image);
GLIB_AVAILABLE_IN_2_40
void g_notification_set_urgent (GNotification *notification,
gboolean urgent);
GLIB_AVAILABLE_IN_2_40
void g_notification_add_button (GNotification *notification,
const gchar *label,
const gchar *detailed_action);
GLIB_AVAILABLE_IN_2_40
void g_notification_add_button_with_target (GNotification *notification,
const gchar *label,
const gchar *action,
const gchar *target_format,
...);
GLIB_AVAILABLE_IN_2_40
void g_notification_add_button_with_target_value (GNotification *notification,
const gchar *label,
const gchar *action,
GVariant *target);
GLIB_AVAILABLE_IN_2_40
void g_notification_set_default_action (GNotification *notification,
const gchar *detailed_action);
GLIB_AVAILABLE_IN_2_40
void g_notification_set_default_action_and_target (GNotification *notification,
const gchar *action,
const gchar *target_format,
...);
GLIB_AVAILABLE_IN_2_40
void g_notification_set_default_action_and_target_value (GNotification *notification,
const gchar *action,
GVariant *target);
G_END_DECLS
#endif

112
gio/gnotificationbackend.c Normal file
View File

@ -0,0 +1,112 @@
/*
* Copyright © 2013 Lars Uebernickel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2 of the licence or (at
* your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authors: Lars Uebernickel <lars@uebernic.de>
*/
#include "gnotificationbackend.h"
#include "gnotification.h"
#include "gapplication.h"
#include "gactiongroup.h"
#include "giomodule-priv.h"
G_DEFINE_TYPE (GNotificationBackend, g_notification_backend, G_TYPE_OBJECT);
static void
g_notification_backend_class_init (GNotificationBackendClass *class)
{
}
static void
g_notification_backend_init (GNotificationBackend *backend)
{
}
GNotificationBackend *
g_notification_backend_new_default (GApplication *application)
{
GType backend_type;
GNotificationBackend *backend;
g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
backend_type = _g_io_module_get_default_type (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME,
"GNOTIFICATION_BACKEND",
G_STRUCT_OFFSET (GNotificationBackendClass, is_supported));
backend = g_object_new (backend_type, NULL);
/* Avoid ref cycle by not taking a ref to the application at all. The
* backend only lives as long as the application does.
*/
backend->application = application;
backend->dbus_connection = g_object_ref (g_application_get_dbus_connection (application));
return backend;
}
void
g_notification_backend_send_notification (GNotificationBackend *backend,
const gchar *id,
GNotification *notification)
{
g_return_if_fail (G_IS_NOTIFICATION_BACKEND (backend));
g_return_if_fail (G_IS_NOTIFICATION (notification));
G_NOTIFICATION_BACKEND_GET_CLASS (backend)->send_notification (backend, id, notification);
}
void
g_notification_backend_withdraw_notification (GNotificationBackend *backend,
const gchar *id)
{
g_return_if_fail (G_IS_NOTIFICATION_BACKEND (backend));
g_return_if_fail (id != NULL);
G_NOTIFICATION_BACKEND_GET_CLASS (backend)->withdraw_notification (backend, id);
}
GApplication *
g_notification_backend_get_application (GNotificationBackend *backend)
{
g_return_val_if_fail (G_IS_NOTIFICATION_BACKEND (backend), NULL);
return backend->application;
}
void
g_notification_backend_activate_action (GNotificationBackend *backend,
const gchar *name,
GVariant *parameter)
{
g_return_if_fail (G_IS_NOTIFICATION_BACKEND (backend));
g_return_if_fail (name != NULL);
if (g_str_has_prefix (name, "app."))
g_action_group_activate_action (G_ACTION_GROUP (backend->application), name + 4, parameter);
}
GDBusConnection *
g_notification_backend_get_dbus_connection (GNotificationBackend *backend)
{
g_return_val_if_fail (G_IS_NOTIFICATION_BACKEND (backend), NULL);
return backend->dbus_connection;
}

View File

@ -0,0 +1,83 @@
/*
* Copyright © 2013 Lars Uebernickel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2 of the licence or (at
* your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authors: Lars Uebernickel <lars@uebernic.de>
*/
#ifndef __G_NOTIFICATION_BACKEND_H__
#define __G_NOTIFICATION_BACKEND_H__
#include <gio/giotypes.h>
G_BEGIN_DECLS
#define G_TYPE_NOTIFICATION_BACKEND (g_notification_backend_get_type ())
#define G_NOTIFICATION_BACKEND(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_NOTIFICATION_BACKEND, GNotificationBackend))
#define G_IS_NOTIFICATION_BACKEND(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_NOTIFICATION_BACKEND))
#define G_NOTIFICATION_BACKEND_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), G_TYPE_NOTIFICATION_BACKEND, GNotificationBackendClass))
#define G_NOTIFICATION_BACKEND_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), G_TYPE_NOTIFICATION_BACKEND, GNotificationBackendClass))
#define G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME "gnotification-backend"
typedef struct _GNotificationBackend GNotificationBackend;
typedef struct _GNotificationBackendClass GNotificationBackendClass;
struct _GNotificationBackend
{
GObject parent_instance;
GApplication *application;
GDBusConnection *dbus_connection;
};
struct _GNotificationBackendClass
{
GObjectClass parent_class;
gboolean (*is_supported) (void);
void (*send_notification) (GNotificationBackend *backend,
const gchar *id,
GNotification *notification);
void (*withdraw_notification) (GNotificationBackend *backend,
const gchar *id);
};
GType g_notification_backend_get_type (void);
GNotificationBackend * g_notification_backend_new_default (GApplication *application);
void g_notification_backend_send_notification (GNotificationBackend *backend,
const gchar *id,
GNotification *notification);
void g_notification_backend_withdraw_notification (GNotificationBackend *backend,
const gchar *id);
GApplication * g_notification_backend_get_application (GNotificationBackend *backend);
void g_notification_backend_activate_action (GNotificationBackend *backend,
const gchar *name,
GVariant *parameter);
GDBusConnection * g_notification_backend_get_dbus_connection (GNotificationBackend *backend);
G_END_DECLS
#endif