summaryrefslogtreecommitdiff
path: root/src/notification.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/notification.c')
-rw-r--r--src/notification.c240
1 files changed, 152 insertions, 88 deletions
diff --git a/src/notification.c b/src/notification.c
index eb0e79e..a813c1e 100644
--- a/src/notification.c
+++ b/src/notification.c
@@ -1,8 +1,11 @@
+/* vim: colorcolumn=80 ts=4 sw=4
+ */
/*
* notification.c
*
- * Copyright (C) 2002 Sun Microsystems, Inc.
- * Copyright (C) 2006 Christian Persch
+ * Copyright © 2002 Sun Microsystems, Inc.
+ * Copyright © 2006 Christian Persch
+ * Copyright © 2021 Logan Rathbone
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
@@ -19,7 +22,7 @@
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
- * Authors: Glynn Foster <glynn.foster@sun.com>
+ * Original Author: Glynn Foster <glynn.foster@sun.com>
*/
#include <config.h>
@@ -41,12 +44,14 @@ static char *icon_file;
static GHashTable *notification_hints;
static NotifyNotification *
-zenity_notification_new (gchar *message, gchar *icon_file) {
+zenity_notification_new (char *message, char *icon_file)
+{
NotifyNotification *notif;
- gchar **text;
+ char **text;
text = g_strsplit (g_strcompress (message), "\n", 2);
- if (*text == NULL) {
+ if (*text == NULL)
+ {
g_printerr (_ ("Could not parse message\n"));
return NULL;
}
@@ -54,36 +59,41 @@ zenity_notification_new (gchar *message, gchar *icon_file) {
notif = notify_notification_new (text[0], /* title */
text[1], /* summary */
icon_file);
+
g_strfreev (text);
+
return notif;
}
static void
-on_notification_default_action (
- NotifyNotification *n, const char *action, void *user_data) {
- ZenityData *zen_data;
+on_notification_default_action (NotifyNotification *n,
+ const char *action, void *user_data)
+{
+ ZenityData *zen_data = user_data;
- zen_data = (ZenityData *) user_data;
notify_notification_close (n, NULL);
zen_data->exit_code = zenity_util_return_exit_code (ZENITY_OK);
- gtk_main_quit ();
+ zenity_util_gapp_quit (NULL);
}
static GHashTable *
-zenity_notification_parse_hints_array (gchar **hints) {
+zenity_notification_parse_hints_array (char **hints)
+{
GHashTable *result;
- gchar **pair;
+ char **pair;
int i;
result = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
- for (i = 0; i < g_strv_length (hints); i++) {
+ for (i = 0; i < (int)g_strv_length (hints); i++)
+ {
pair = g_strsplit (hints[i], ":", 2);
g_hash_table_replace (result, g_strdup (pair[0]), g_strdup (pair[1]));
g_strfreev (pair);
}
+
if (g_hash_table_size (result) == 0) {
g_hash_table_unref (result);
return NULL;
@@ -93,36 +103,36 @@ zenity_notification_parse_hints_array (gchar **hints) {
}
static GHashTable *
-zenity_notification_parse_hints (gchar *hints) {
+zenity_notification_parse_hints (char *hints)
+{
GHashTable *result;
- gchar **hint_array;
+ char **hint_array;
hint_array = g_strsplit (g_strcompress (hints), "\n", MAX_HINTS);
result = zenity_notification_parse_hints_array (hint_array);
g_strfreev (hint_array);
+
return result;
}
static void
-zenity_notification_set_hint (
- gpointer key, gpointer value, gpointer user_data) {
- NotifyNotification *notification;
- gchar *hint_name;
- GVariant *hint_value;
+zenity_notification_set_hint (gpointer key, gpointer value,
+ gpointer user_data)
+{
+ NotifyNotification *notification = user_data;
+ char *hint_name = key;
+ char *string_value = value;
- gchar *string_value;
+ GVariant *hint_value;
gboolean boolean_value;
gint32 int_value;
guchar byte_value;
- hint_name = (gchar *) key;
- string_value = (gchar *) value;
- notification = (NotifyNotification *) user_data;
-
if ((g_ascii_strcasecmp ("action-icons", hint_name) == 0) ||
(g_ascii_strcasecmp ("resident", hint_name) == 0) ||
(g_ascii_strcasecmp ("suppress-sound", hint_name) == 0) ||
- (g_ascii_strcasecmp ("transient", hint_name) == 0)) {
+ (g_ascii_strcasecmp ("transient", hint_name) == 0))
+ {
/* boolean hints */
if (g_ascii_strcasecmp ("true", string_value) == 0) {
boolean_value = TRUE;
@@ -134,36 +144,46 @@ zenity_notification_set_hint (
return;
}
hint_value = g_variant_new_boolean (boolean_value);
- } else if ((g_ascii_strcasecmp ("category", hint_name) == 0) ||
+ }
+ else if ((g_ascii_strcasecmp ("category", hint_name) == 0) ||
(g_ascii_strcasecmp ("desktop-entry", hint_name) == 0) ||
(g_ascii_strcasecmp ("image-path", hint_name) == 0) ||
(g_ascii_strcasecmp ("image_path", hint_name) == 0) ||
(g_ascii_strcasecmp ("sound-file", hint_name) == 0) ||
- (g_ascii_strcasecmp ("sound-name", hint_name) == 0)) {
+ (g_ascii_strcasecmp ("sound-name", hint_name) == 0))
+ {
/* string hints */
hint_value = g_variant_new_string (string_value);
- } else if ((g_ascii_strcasecmp ("image-data", hint_name) == 0) ||
+ }
+ else if ((g_ascii_strcasecmp ("image-data", hint_name) == 0) ||
(g_ascii_strcasecmp ("image_data", hint_name) == 0) ||
- (g_ascii_strcasecmp ("icon-data", hint_name) == 0)) {
+ (g_ascii_strcasecmp ("icon-data", hint_name) == 0))
+ {
/* (iibiiay) */
g_printerr (_ ("Unsupported hint. Skipping.\n"));
return;
- } else if ((g_ascii_strcasecmp ("x", hint_name) == 0) ||
- (g_ascii_strcasecmp ("y", hint_name) == 0)) {
+ }
+ else if ((g_ascii_strcasecmp ("x", hint_name) == 0) ||
+ (g_ascii_strcasecmp ("y", hint_name) == 0))
+ {
/* int hints */
if (string_value == NULL)
string_value = "";
int_value = (gint32) g_ascii_strtoll (string_value, NULL, 0);
hint_value = g_variant_new_int32 (int_value);
- } else if ((g_ascii_strcasecmp ("urgency", hint_name) == 0)) {
+ }
+ else if ((g_ascii_strcasecmp ("urgency", hint_name) == 0))
+ {
/* byte hints */
if (string_value == NULL)
string_value = "";
byte_value = (guchar) g_ascii_strtoll (string_value, NULL, 0);
hint_value = g_variant_new_byte (byte_value);
- } else {
+ }
+ else
+ {
/* unknown hints */
- g_printerr (_ ("Unknown hint name. Skipping.\n"));
+ g_printerr (_("Unknown hint name. Skipping.\n"));
return;
}
@@ -171,41 +191,49 @@ zenity_notification_set_hint (
}
static void
-zenity_notification_set_hints (
- NotifyNotification *notification, GHashTable *hints) {
- if (hints == NULL) {
+zenity_notification_set_hints (NotifyNotification *notification,
+ GHashTable *hints)
+{
+ if (hints == NULL)
return;
- }
g_hash_table_foreach (hints, zenity_notification_set_hint, notification);
}
static gboolean
-zenity_notification_handle_stdin (
- GIOChannel *channel, GIOCondition condition, gpointer user_data) {
- if ((condition & G_IO_IN) != 0) {
+zenity_notification_handle_stdin (GIOChannel *channel, GIOCondition condition,
+ gpointer user_data)
+{
+ if ((condition & G_IO_IN) != 0)
+ {
GString *string;
GError *error = NULL;
- string = g_string_new (NULL);
while (channel->is_readable == FALSE)
;
+
+ string = g_string_new (NULL);
+
do {
- gint status;
- gchar *command, *value, *colon;
+ int status;
+ char *command, *value, *colon;
do {
- status = g_io_channel_read_line_string (
- channel, string, NULL, &error);
- while (gdk_events_pending ())
- gtk_main_iteration ();
+ status = g_io_channel_read_line_string (channel, string,
+ NULL, &error);
+
+ while (g_main_context_pending (NULL)) {
+ g_main_context_iteration (NULL, FALSE);
+ }
} while (status == G_IO_STATUS_AGAIN);
- if (status != G_IO_STATUS_NORMAL) {
+ if (status != G_IO_STATUS_NORMAL)
+ {
if (error) {
- g_warning ("zenity_notification_handle_stdin () : %s",
- error->message);
+ g_warning ("%s: %s",
+ __func__,
+ error->message);
g_error_free (error);
error = NULL;
}
@@ -214,10 +242,12 @@ zenity_notification_handle_stdin (
zenity_util_strip_newline (string->str);
colon = strchr (string->str, ':');
- if (colon == NULL) {
- g_printerr (_ ("Could not parse command from stdin\n"));
+ if (colon == NULL)
+ {
+ g_printerr (_("Could not parse command from stdin\n"));
continue;
}
+
/* split off the command and value */
command = g_strstrip (g_strndup (string->str, colon - string->str));
@@ -225,19 +255,27 @@ zenity_notification_handle_stdin (
while (*value && g_ascii_isspace (*value))
value++;
- if (!g_ascii_strcasecmp (command, "icon")) {
+ if (! g_ascii_strcasecmp (command, "icon"))
+ {
g_free (icon_file);
icon_file = g_strdup (value);
- } else if (!g_ascii_strcasecmp (command, "hints")) {
+ }
+ else if (!g_ascii_strcasecmp (command, "hints"))
+ {
if (notification_hints != NULL) {
g_hash_table_unref (notification_hints);
}
notification_hints = zenity_notification_parse_hints (value);
- } else if (!g_ascii_strcasecmp (command, "message")) {
+ }
+ else if (!g_ascii_strcasecmp (command, "message"))
+ {
/* display a notification bubble */
- if (!g_utf8_validate (value, -1, NULL)) {
+ if (! g_utf8_validate (value, -1, NULL))
+ {
g_warning ("Invalid UTF-8 in input!");
- } else {
+ }
+ else
+ {
NotifyNotification *notif;
error = NULL;
@@ -257,37 +295,51 @@ zenity_notification_handle_stdin (
g_object_unref (notif);
}
- } else if (!g_ascii_strcasecmp (command, "tooltip")) {
- if (!g_utf8_validate (value, -1, NULL)) {
+ }
+ else if (! g_ascii_strcasecmp (command, "tooltip"))
+ {
+ if (! g_utf8_validate (value, -1, NULL))
+ {
g_warning ("Invalid UTF-8 in input!");
- } else {
- NotifyNotification *notif;
- notif = zenity_notification_new (value, icon_file);
+ }
+ else
+ {
+ NotifyNotification *notif =
+ zenity_notification_new (value, icon_file);
+
if (notif == NULL)
continue;
zenity_notification_set_hints (notif, notification_hints);
notify_notification_show (notif, &error);
- if (error) {
- g_warning (
- "Error showing notification: %s", error->message);
+ if (error)
+ {
+ g_warning ("Error showing notification: %s",
+ error->message);
g_error_free (error);
+
error = NULL;
}
}
- } else if (!g_ascii_strcasecmp (command, "visible")) {
+ }
+ else if (!g_ascii_strcasecmp (command, "visible"))
+ {
- } else {
+ }
+ else
+ {
g_warning ("Unknown command '%s'", command);
}
g_free (command);
} while (g_io_channel_get_buffer_condition (channel) == G_IO_IN);
+
g_string_free (string, TRUE);
}
- if ((condition & G_IO_HUP) != 0) {
+ if ((condition & G_IO_HUP) != 0)
+ {
g_io_channel_shutdown (channel, TRUE, NULL);
return FALSE;
}
@@ -296,19 +348,23 @@ zenity_notification_handle_stdin (
}
static void
-zenity_notification_listen_on_stdin (ZenityData *data) {
+zenity_notification_listen_on_stdin (ZenityData *data)
+{
GIOChannel *channel;
channel = g_io_channel_unix_new (0);
g_io_channel_set_encoding (channel, NULL, NULL);
g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL);
- g_io_add_watch (
- channel, G_IO_IN | G_IO_HUP, zenity_notification_handle_stdin, data);
+ g_io_add_watch (channel,
+ G_IO_IN | G_IO_HUP,
+ zenity_notification_handle_stdin,
+ data);
}
void
-zenity_notification (
- ZenityData *data, ZenityNotificationData *notification_data) {
+zenity_notification (ZenityData *data,
+ ZenityNotificationData *notification_data)
+{
GError *error;
NotifyNotification *notification;
GHashTable *notification_hints;
@@ -318,20 +374,23 @@ zenity_notification (
notify_init (_ ("Zenity notification"));
}
- if (notification_data->listen) {
+ if (notification_data->listen)
+ {
zenity_notification_listen_on_stdin (data);
- gtk_main ();
- } else {
- if (notification_data->notification_text == NULL) {
+
+ zenity_util_gapp_main (NULL);
+ }
+ else
+ {
+ if (notification_data->notification_text == NULL)
exit (1);
- }
- notification = zenity_notification_new (
- notification_data->notification_text, data->window_icon);
+ notification =
+ zenity_notification_new (notification_data->notification_text,
+ notification_data->icon);
- if (notification == NULL) {
+ if (notification == NULL)
exit (1);
- }
/* if we aren't listening for changes, then close on default action */
notify_notification_add_action (notification,
@@ -342,16 +401,20 @@ zenity_notification (
NULL);
/* set the notification hints for the displayed notification */
- if (notification_data->notification_hints != NULL) {
+ if (notification_data->notification_hints != NULL)
+ {
notification_hints = zenity_notification_parse_hints_array (
notification_data->notification_hints);
+
zenity_notification_set_hints (notification, notification_hints);
+
g_hash_table_unref (notification_hints);
}
/* Show icon and wait */
error = NULL;
- if (!notify_notification_show (notification, &error)) {
+ if (! notify_notification_show (notification, &error))
+ {
if (error != NULL) {
g_warning ("Error showing notification: %s", error->message);
g_error_free (error);
@@ -364,7 +427,8 @@ zenity_notification (
g_timeout_add_seconds (data->timeout_delay,
(GSourceFunc) zenity_util_timeout_handle,
NULL);
- gtk_main ();
+
+ zenity_util_gapp_main (NULL);
}
}