diff options
author | Ben Blain <mail@servc.eu> | 2021-03-11 10:32:57 +0000 |
---|---|---|
committer | Marco Trevisan (TreviƱo) <mail@3v1n0.net> | 2022-04-25 22:23:37 +0200 |
commit | 6c707d2dfd01150f12282160f3a0a25b2759b080 (patch) | |
tree | 31f135cd9e68b2d57877c3f639a760423846cd70 | |
parent | a396dd9af9c023315ce97605198b95c649176055 (diff) | |
download | libnotify-6c707d2dfd01150f12282160f3a0a25b2759b080.tar.gz |
notify-send: Add support for notification actions and responses
The activated action name is written to stdout and the notification is closed
when a valid action has been selected.
-rw-r--r-- | docs/notify-send.xml | 6 | ||||
-rw-r--r-- | tools/notify-send.c | 53 |
2 files changed, 59 insertions, 0 deletions
diff --git a/docs/notify-send.xml b/docs/notify-send.xml index 185dbd6..fefbf1a 100644 --- a/docs/notify-send.xml +++ b/docs/notify-send.xml @@ -59,6 +59,12 @@ </listitem> </varlistentry> <varlistentry> + <term><option>-A</option>, <option>--action</option>=[<replaceable>NAME</replaceable>=]<replaceable>Text...</replaceable></term> + <listitem> + <para>Specifies the actions to display to the user. Implies <option>--wait</option> to wait for user input. May be set multiple times. The <replaceable>NAME</replaceable> of the action is output to <literal>stdout</literal>. If <replaceable>NAME</replaceable> is not specified, the numerical index of the option is used (starting with <literal>1</literal>).</para> + </listitem> + </varlistentry> + <varlistentry> <term><option>-u</option>, <option>--urgency</option>=<replaceable>LEVEL</replaceable></term> <listitem> <para>Specifies the urgency level (<literal>low</literal>, <literal>normal</literal>, <literal>critical</literal>).</para> diff --git a/tools/notify-send.c b/tools/notify-send.c index e8dc272..bf31894 100644 --- a/tools/notify-send.c +++ b/tools/notify-send.c @@ -128,6 +128,17 @@ handle_closed (NotifyNotification *notify, g_main_loop_quit (loop); } +static void +handle_action (NotifyNotification *notify, + char *action, + gpointer user_data) +{ + const char *action_name = user_data; + + g_printf ("%s\n", action_name); + notify_notification_close (notify, NULL); +} + static gboolean on_wait_timeout (gpointer data) { @@ -147,6 +158,7 @@ main (int argc, char *argv[]) static char *icon_str = NULL; static char **n_text = NULL; static char **hints = NULL; + static char **actions = NULL; static gboolean print_id = FALSE; static gint notification_id = 0; static gboolean do_version = FALSE; @@ -186,6 +198,12 @@ main (int argc, char *argv[]) {"wait", 'w', 0, G_OPTION_ARG_NONE, &wait, N_("Wait for the notification to be closed before exiting."), NULL}, + {"action", 'A', 0, G_OPTION_ARG_FILENAME_ARRAY, &actions, + N_ + ("Specifies the actions to display to the user. Implies --wait to wait for user input." + " May be set multiple times. The name of the action is output to stdout. If NAME is " + "not specified, the numerical index of the option is used (starting with 0)."), + N_("[NAME=]Text...")}, {"version", 'v', 0, G_OPTION_ARG_NONE, &do_version, N_("Version of the package."), NULL}, @@ -294,6 +312,41 @@ main (int argc, char *argv[]) } } + if (actions != NULL) { + gint i = 0; + char *action = NULL; + gchar **spl = NULL; + + while ((action = actions[i++])) { + gchar *name; + const gchar *label; + + spl = g_strsplit (action, "=", 2); + + if (g_strv_length (spl) == 1) { + name = g_strdup_printf ("%d", i - 1); + label = g_strstrip (spl[0]); + } else { + name = g_strdup (g_strstrip (spl[0])); + label = g_strstrip (spl[1]); + } + + if (*label != '\0' && *name != '\0') { + notify_notification_add_action (notify, + name, + label, + handle_action, + name, + g_free); + wait = TRUE; + } + + g_strfreev (spl); + } + + g_strfreev (actions); + } + if (wait) { g_signal_connect (G_OBJECT (notify), "closed", |