Commit 639bd362 authored by Lars Uebernickel's avatar Lars Uebernickel Committed by Allison Karlitskaya
parent 1b04954c
......@@ -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)
......
......@@ -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: */
......@@ -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__ */
/*
* 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;
}
......@@ -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__
......
......@@ -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 ());
......
......@@ -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:
......
/*
* 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
/*
* 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)