Skip to content
Commits on Source (18)
New in 0.7.12
New in 0.8.0
============
* Use Desktop Portal Notification when running confined (snap and flatpak)
Now the library acts like a wrapper in such scenario, with some limited
capabilities, but this will enforce security and user control over the
allowed notifications. [Marco]
* notify-send: Handles SIGINT gracefully, closing waiting notification [Marco]
* Use NotifyClosedReason enum as closed reason return value [Marco]
* Bump dependency on GLib 2.38 [Marco]
* Various introspection docs improvements and fixes [Marco]
Contributors:
Marco Trevisan
New in 0.7.12
=============
* docs/notify-send: Add --transient option to manpage [Marco]
* notify-send: Move server capabilities check to a separate function [Marco]
* notify-send: Add debug message about server not supporting persistence
[Marco]
* notification: Include sender-pid hint by default if not provided [Marco]
* notification: Include sender-pid hint by default if not provided [Marco]
* Delete unused notifynotification.xml [Patrick; !25]
* notification: Bookend calling NotifyActionCallback with temporary ref
[Logan; #25, !26]
......
......@@ -7,6 +7,9 @@ daemon, as defined in the [org.freedesktop.Notifications][fdo-spec] Desktop
Specification. These notifications can be used to inform the user about an event
or display some form of information without getting in the user's way.
It is also a simple wrapper to send cross-desktop Notifications for sandboxed
applications using the [XDG Portal Notification API][portal].
## Notice
For GLib based applications the [GNotification][gnotif] API should be used
......@@ -14,3 +17,4 @@ instead.
[fdo-spec]: https://specifications.freedesktop.org/notification-spec/notification-spec-latest.html
[gnotif]: https://docs.gtk.org/gio/class.Notification.html
[portal]: https://github.com/flatpak/xdg-desktop-portal/blob/main/data/org.freedesktop.portal.Notification.xml
......@@ -4,6 +4,7 @@ NOTIFY_EXPIRES_DEFAULT
NOTIFY_EXPIRES_NEVER
<TITLE>NotifyNotification</TITLE>
NotifyNotification
NotifyClosedReason
NotifyUrgency
NotifyActionCallback
NOTIFY_ACTION_CALLBACK
......
......@@ -26,6 +26,10 @@
#define NOTIFY_DBUS_CORE_INTERFACE "org.freedesktop.Notifications"
#define NOTIFY_DBUS_CORE_OBJECT "/org/freedesktop/Notifications"
#define NOTIFY_PORTAL_DBUS_NAME "org.freedesktop.portal.Desktop"
#define NOTIFY_PORTAL_DBUS_CORE_INTERFACE "org.freedesktop.portal.Notification"
#define NOTIFY_PORTAL_DBUS_CORE_OBJECT "/org/freedesktop/portal/desktop"
G_BEGIN_DECLS
GDBusProxy * _notify_get_proxy (GError **error);
......@@ -36,6 +40,14 @@ gint _notify_notification_get_timeout (const NotifyNotific
gboolean _notify_notification_has_nondefault_actions (const NotifyNotification *n);
gboolean _notify_check_spec_version (int major, int minor);
const char * _notify_get_snap_name (void);
const char * _notify_get_snap_path (void);
const char * _notify_get_snap_app (void);
const char * _notify_get_flatpak_app (void);
gboolean _notify_uses_portal_notifications (void);
G_END_DECLS
#endif /* _LIBNOTIFY_INTERNAL_H_ */
This diff is collapsed.
......@@ -55,6 +55,11 @@ typedef struct _NotifyNotification NotifyNotification;
typedef struct _NotifyNotificationClass NotifyNotificationClass;
typedef struct _NotifyNotificationPrivate NotifyNotificationPrivate;
/**
* NotifyNotification:
*
* A passive pop-up notification.
*/
struct _NotifyNotification
{
/*< private >*/
......@@ -88,11 +93,34 @@ typedef enum
} NotifyUrgency;
/**
* NotifyClosedReason:
* @NOTIFY_CLOSED_REASON_UNSET: Notification not closed.
* @NOTIFY_CLOSED_REASON_EXPIRED: Timeout has expired.
* @NOTIFY_CLOSED_REASON_DISMISSED: It has been dismissed by the user.
* @NOTIFY_CLOSED_REASON_API_REQUEST: It has been closed by a call to
* notify_notification_close().
* @NOTIFY_CLOSED_REASON_UNDEFIEND: Closed by undefined/reserved reasons.
*
* The reason for which the notification has been closed.
*
* Since: 0.8.0
*/
typedef enum
{
NOTIFY_CLOSED_REASON_UNSET = -1,
NOTIFY_CLOSED_REASON_EXPIRED = 1,
NOTIFY_CLOSED_REASON_DISMISSED = 2,
NOTIFY_CLOSED_REASON_API_REQUEST = 3,
NOTIFY_CLOSED_REASON_UNDEFIEND = 4,
} NotifyClosedReason;
/**
* NotifyActionCallback:
* @notification:
* @action:
* @user_data:
* @notification: a #NotifyActionCallback notification
* @action: (transfer none): The activated action name
* @user_data: (nullable) (transfer none): User provided data
*
* An action callback function.
*/
......
......@@ -43,10 +43,14 @@
static gboolean _initted = FALSE;
static char *_app_name = NULL;
static char *_snap_name = NULL;
static char *_snap_app = NULL;
static char *_flatpak_app = NULL;
static GDBusProxy *_proxy = NULL;
static GList *_active_notifications = NULL;
static int _spec_version_major = 0;
static int _spec_version_minor = 0;
static int _portal_version = 0;
gboolean
_notify_check_spec_version (int major,
......@@ -74,6 +78,26 @@ _notify_get_server_info (char **ret_name,
return FALSE;
}
if (_notify_uses_portal_notifications ()) {
if (ret_name) {
*ret_name = g_strdup ("Portal Notification");
}
if (ret_vendor) {
*ret_vendor = g_strdup ("Freedesktop");
}
if (ret_version) {
*ret_version = g_strdup_printf ("%u", _portal_version);
}
if (ret_spec_version) {
*ret_spec_version = g_strdup ("1.2");
}
return TRUE;
}
result = g_dbus_proxy_call_sync (proxy,
"GetServerInformation",
g_variant_new ("()"),
......@@ -119,6 +143,18 @@ _notify_update_spec_version (GError **error)
return TRUE;
}
static gboolean
set_app_name (const char *app_name)
{
g_return_val_if_fail (app_name != NULL, FALSE);
g_return_val_if_fail (*app_name != '\0', FALSE);
g_free (_app_name);
_app_name = g_strdup (app_name);
return TRUE;
}
/**
* notify_set_app_name:
......@@ -130,46 +166,267 @@ _notify_update_spec_version (GError **error)
void
notify_set_app_name (const char *app_name)
{
g_free (_app_name);
_app_name = g_strdup (app_name);
set_app_name (app_name);
}
/**
* notify_init:
* @app_name: The name of the application initializing libnotify.
* @app_name: (nullable): The name of the application initializing libnotify.
*
* Initialized libnotify. This must be called before any other functions.
*
* Starting from 0.8, if the provided @app_name is %NULL, libnotify will
* try to figure it out from the running application.
* Before it was not allowed, and was causing libnotify not to be initialized.
*
* Returns: %TRUE if successful, or %FALSE on error.
*/
gboolean
notify_init (const char *app_name)
{
g_return_val_if_fail (app_name != NULL, FALSE);
g_return_val_if_fail (*app_name != '\0', FALSE);
if (_initted)
return TRUE;
#ifdef GLIB_VERSION_2_32
if (app_name == NULL && g_application_get_default ()) {
GApplication *application = g_application_get_default ();
if (app_name == NULL) {
GApplication *application;
app_name = g_application_get_application_id (application);
}
#endif
app_name = _notify_get_snap_app ();
if (app_name == NULL) {
app_name = _notify_get_flatpak_app ();
}
notify_set_app_name (app_name);
if (app_name == NULL &&
(application = g_application_get_default ())) {
app_name = g_application_get_application_id (application);
}
}
#ifndef GLIB_VERSION_2_36
g_type_init ();
#endif
if (!set_app_name (app_name)) {
return FALSE;
}
_initted = TRUE;
return TRUE;
}
static void
_initialize_snap_names (void)
{
gchar *cgroup_contents = NULL;
gchar *found_snap_name = NULL;
gchar **lines;
gint i;
if (!g_file_get_contents ("/proc/self/cgroup", &cgroup_contents,
NULL, NULL)) {
g_free (cgroup_contents);
return;
}
lines = g_strsplit (cgroup_contents, "\n", -1);
g_free (cgroup_contents);
for (i = 0; lines[i]; ++i) {
gchar **parts = g_strsplit (lines[i], ":", 3);
gchar *basename;
gchar **ns;
guint ns_length;
if (g_strv_length (parts) != 3) {
g_strfreev (parts);
continue;
}
basename = g_path_get_basename (parts[2]);
g_strfreev (parts);
if (!basename) {
continue;
}
ns = g_strsplit (basename, ".", -1);
ns_length = g_strv_length (ns);
g_free (basename);
if (ns_length < 2 || !g_str_equal (ns[0], "snap")) {
g_strfreev (ns);
continue;
}
if (_snap_name == NULL) {
g_free (found_snap_name);
found_snap_name = g_strdup (ns[1]);
}
if (ns_length < 3) {
g_strfreev (ns);
continue;
}
if (_snap_name == NULL) {
_snap_name = found_snap_name;
found_snap_name = NULL;
g_debug ("SNAP name: %s", _snap_name);
}
if (g_str_equal (ns[1], _snap_name)) {
_snap_app = g_strdup (ns[2]);
g_strfreev (ns);
break;
}
g_strfreev (ns);
}
if (_snap_name == NULL && found_snap_name != NULL) {
_snap_name = found_snap_name;
found_snap_name = NULL;
g_debug ("SNAP name: %s", _snap_name);
}
if (_snap_app == NULL) {
_snap_app = g_strdup (_snap_name);
}
g_debug ("SNAP app: %s", _snap_app);
g_strfreev (lines);
g_free (found_snap_name);
}
const char *
_notify_get_snap_path (void)
{
static const char *snap_path = NULL;
static gsize snap_path_set = FALSE;
if (g_once_init_enter (&snap_path_set)) {
snap_path = g_getenv ("SNAP");
if (!snap_path || *snap_path == '\0' ||
!strchr (snap_path, G_DIR_SEPARATOR)) {
snap_path = NULL;
} else {
g_debug ("SNAP path: %s", snap_path);
}
g_once_init_leave (&snap_path_set, TRUE);
}
return snap_path;
}
const char *
_notify_get_snap_name (void)
{
static gsize snap_name_set = FALSE;
if (g_once_init_enter (&snap_name_set)) {
if (!_snap_name) {
const char *snap_name_env = g_getenv ("SNAP_NAME");
if (!snap_name_env || *snap_name_env == '\0')
snap_name_env = NULL;
_snap_name = g_strdup (snap_name_env);
g_debug ("SNAP name: %s", _snap_name);
}
g_once_init_leave (&snap_name_set, TRUE);
}
return _snap_name;
}
const char *
_notify_get_snap_app (void)
{
static gsize snap_app_set = FALSE;
if (g_once_init_enter (&snap_app_set)) {
_initialize_snap_names ();
g_once_init_leave (&snap_app_set, TRUE);
}
return _snap_app;
}
const char *
_notify_get_flatpak_app (void)
{
static gsize flatpak_app_set = FALSE;
if (g_once_init_enter (&flatpak_app_set)) {
GKeyFile *info = g_key_file_new ();
if (g_key_file_load_from_file (info, "/.flatpak-info",
G_KEY_FILE_NONE, NULL)) {
const char *group = "Application";
if (g_key_file_has_group (info, "Runtime")) {
group = "Runtime";
}
_flatpak_app = g_key_file_get_string (info, group,
"name", NULL);
}
g_key_file_free (info);
g_once_init_leave (&flatpak_app_set, TRUE);
}
return _flatpak_app;
}
static gboolean
_notify_is_running_under_flatpak (void)
{
return !!_notify_get_flatpak_app ();
}
static gboolean
_notify_is_running_under_snap (void)
{
return !!_notify_get_snap_app ();
}
static gboolean
_notify_is_running_in_sandbox (void)
{
static gsize use_portal = 0;
enum {
IGNORE_PORTAL = 1,
TRY_USE_PORTAL = 2,
FORCE_PORTAL = 3
};
if (g_once_init_enter (&use_portal)) {
if (G_UNLIKELY (g_getenv ("NOTIFY_IGNORE_PORTAL"))) {
g_once_init_leave (&use_portal, IGNORE_PORTAL);
} else if (G_UNLIKELY (g_getenv ("NOTIFY_FORCE_PORTAL"))) {
g_once_init_leave (&use_portal, FORCE_PORTAL);
} else {
g_once_init_leave (&use_portal, TRY_USE_PORTAL);
}
}
if (use_portal == IGNORE_PORTAL) {
return FALSE;
}
return use_portal == FORCE_PORTAL ||
_notify_is_running_under_flatpak () ||
_notify_is_running_under_snap ();
}
gboolean
_notify_uses_portal_notifications (void)
{
return _portal_version != 0;
}
/**
* notify_get_app_name:
*
......@@ -219,6 +476,15 @@ notify_uninit (void)
_proxy = NULL;
}
g_free (_snap_name);
_snap_name = NULL;
g_free (_snap_app);
_snap_app = NULL;
g_free (_flatpak_app);
_flatpak_app = NULL;
_initted = FALSE;
}
......@@ -235,6 +501,46 @@ notify_is_initted (void)
return _initted;
}
GDBusProxy *
_get_portal_proxy (GError **error)
{
GError *local_error = NULL;
GDBusProxy *proxy;
GVariant *res;
proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
NOTIFY_PORTAL_DBUS_NAME,
NOTIFY_PORTAL_DBUS_CORE_OBJECT,
NOTIFY_PORTAL_DBUS_CORE_INTERFACE,
NULL,
&local_error);
if (proxy == NULL) {
g_debug ("Failed to get portal proxy: %s", local_error->message);
g_clear_error (&local_error);
return NULL;
}
res = g_dbus_proxy_get_cached_property (proxy, "version");
if (!res) {
g_object_unref (proxy);
return NULL;
}
_portal_version = g_variant_get_uint32 (res);
g_assert (_portal_version > 0);
g_warning ("Running in confined mode, using Portal notifications. "
"Some features and hints won't be supported");
g_variant_unref (res);
return proxy;
}
/*
* _notify_get_proxy:
* @error: (allow-none): a location to store a #GError, or %NULL
......@@ -250,6 +556,14 @@ _notify_get_proxy (GError **error)
if (_proxy != NULL)
return _proxy;
if (_notify_is_running_in_sandbox ()) {
_proxy = _get_portal_proxy (error);
if (_proxy != NULL) {
goto out;
}
}
_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
NULL,
......@@ -258,6 +572,8 @@ _notify_get_proxy (GError **error)
NOTIFY_DBUS_CORE_INTERFACE,
NULL,
error);
out:
if (_proxy == NULL) {
return NULL;
}
......@@ -295,6 +611,15 @@ notify_get_server_caps (void)
return NULL;
}
if (_notify_uses_portal_notifications ()) {
list = g_list_prepend (list, g_strdup ("actions"));
list = g_list_prepend (list, g_strdup ("body"));
list = g_list_prepend (list, g_strdup ("body-images"));
list = g_list_prepend (list, g_strdup ("icon-static"));
return list;
}
result = g_dbus_proxy_call_sync (proxy,
"GetCapabilities",
g_variant_new ("()"),
......
project('libnotify',
'c',
version: '0.7.12',
version: '0.8.0',
meson_version: '>= 0.47.0')
gnome = import('gnome')
......@@ -36,7 +36,7 @@ man1dir = join_paths(prefix, get_option('mandir'), 'man1')
libnotify_deps = []
extra_deps = []
glib_req_version = '>= 2.26.0'
glib_req_version = '>= 2.38.0'
gdk_pixbuf_dep = dependency('gdk-pixbuf-2.0')
glib_dep = dependency('glib-2.0', version: glib_req_version)
......
......@@ -29,10 +29,6 @@ main ()
{
NotifyNotification *n;
#ifndef GLIB_VERSION_2_36
g_type_init ();
#endif
notify_init ("Error Handling");
n = notify_notification_new ("Summary", "Content", NULL);
......
......@@ -28,10 +28,6 @@ main ()
GError *error;
error = NULL;
#ifndef GLIB_VERSION_2_36
g_type_init ();
#endif
notify_init ("Replace Test");
n = notify_notification_new ("Summary",
......
......@@ -26,6 +26,7 @@
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib-unix.h>
#include <glib/gprintf.h>
#define N_(x) (x)
......@@ -148,6 +149,19 @@ handle_closed (NotifyNotification *notify,
g_main_loop_quit (loop);
}
static gboolean
on_sigint (gpointer data)
{
NotifyNotification *notification = data;
g_printerr ("Wait cancelled, closing notification\n");
notify_notification_close (notification, NULL);
g_main_loop_quit (loop);
return FALSE;
}
static void
handle_action (NotifyNotification *notify,
char *action,
......@@ -203,6 +217,10 @@ main (int argc, char *argv[])
static char **n_text = NULL;
static char **hints = NULL;
static char **actions = NULL;
static char *server_name = NULL;
static char *server_vendor = NULL;
static char *server_version = NULL;
static char *server_spec_version = NULL;
static gboolean print_id = FALSE;
static gint notification_id = 0;
static gboolean do_version = FALSE;
......@@ -265,10 +283,6 @@ main (int argc, char *argv[])
setlocale (LC_ALL, "");
#ifndef GLIB_VERSION_2_36
g_type_init ();
#endif
g_set_prgname (argv[0]);
g_log_set_always_fatal (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
......@@ -310,6 +324,18 @@ main (int argc, char *argv[])
if (!notify_init ("notify-send"))
exit (1);
notify_get_server_info (&server_name,
&server_vendor,
&server_version,
&server_spec_version);
g_debug ("Using sever %s %s, v%s - Supporting Notification Spec %s",
server_name, server_vendor, server_version, server_spec_version);
g_free (server_name);
g_free (server_vendor);
g_free (server_version);
g_free (server_spec_version);
notify = g_object_new (NOTIFY_TYPE_NOTIFICATION,
"summary", summary,
"body", body,
......@@ -442,6 +468,7 @@ main (int argc, char *argv[])
}
if (wait) {
g_unix_signal_add (SIGINT, on_sigint, notify);
loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (loop);
g_main_loop_unref (loop);
......