From 00b2be3dc019854fe3b4b83a14c8a32f92acd938 Mon Sep 17 00:00:00 2001 From: progandy Date: Wed, 27 Feb 2013 16:51:28 +0100 Subject: [PATCH] cleaner handling of actions in menu --- menu.c | 36 ++++++++++++++++++++++++------------ notification.c | 6 ++++-- utils.c | 7 +++++++ utils.h | 4 ++++ 4 files changed, 39 insertions(+), 14 deletions(-) diff --git a/menu.c b/menu.c index 338f848..9390f0c 100644 --- a/menu.c +++ b/menu.c @@ -97,24 +97,26 @@ void invoke_action(const char *action) notification *invoked = NULL; char *action_identifier = NULL; - char *name_begin = strstr(action, "("); - if (!name_begin) { + char *appname_begin = strchr(action, '['); + if (!appname_begin) { printf("invalid action: %s\n", action); return; } - name_begin++; + appname_begin++; + int appname_len = strlen(appname_begin) - 1; // remove ] + int action_len = strlen(action) - appname_len - 3; // remove space, [, ] for (GList * iter = g_queue_peek_head_link(displayed); iter; iter = iter->next) { notification *n = iter->data; - if (g_str_has_prefix(action, n->appname)) { + if (g_str_has_prefix(appname_begin, n->appname) && strlen(n->appname) == appname_len) { if (!n->actions) continue; for (int i = 0; i < n->actions->count; i += 2) { char *a_identifier = n->actions->actions[i]; char *name = n->actions->actions[i + 1]; - if (g_str_has_prefix(name_begin, name)) { + if (g_str_has_prefix(action, name) && strlen(name) == action_len) { invoked = n; action_identifier = a_identifier; break; @@ -134,14 +136,24 @@ void invoke_action(const char *action) */ void dispatch_menu_result(const char *input) { - char *maybe_url = extract_urls(input); - if (maybe_url) { - open_browser(maybe_url); - free(maybe_url); - return; + switch (input[0]) { + case '#': + invoke_action(input + 1); + break; + case '[': // named url. skip name and continue + input = strchr(input, ']'); + if (input == NULL) + break; + default: + { // test and open url + char *maybe_url = extract_urls(input); + if (maybe_url) { + open_browser(maybe_url); + free(maybe_url); + break; + } + } } - - invoke_action(input); } /* diff --git a/notification.c b/notification.c index 16c2e92..7edd857 100644 --- a/notification.c +++ b/notification.c @@ -332,6 +332,8 @@ int notification_init(notification * n, int id) n->actions->dmenu_str = NULL; for (int i = 0; i < n->actions->count; i += 2) { char *human_readable = n->actions->actions[i + 1]; + string_replace_char('[', '(', human_readable); // kill square brackets + string_replace_char(']', ')', human_readable); printf("debug: %s\n", n->appname); printf("debug: %s\n", human_readable); char *tmp = @@ -341,8 +343,8 @@ int notification_init(notification * n, int id) n->actions->dmenu_str = string_append(n->actions->dmenu_str, - g_strdup_printf("%s(%s)", n->appname, - human_readable), + g_strdup_printf("#%s [%s]", human_readable, + n->appname), "\n"); } } diff --git a/utils.c b/utils.c index 1923a62..2df96eb 100644 --- a/utils.c +++ b/utils.c @@ -10,6 +10,13 @@ #include "utils.h" #include "dunst.h" +char *string_replace_char(char needle, char replacement, char *haystack) { + char *current = haystack; + while ((current = strchr (current, needle)) != NULL) + *current++ = replacement; + return haystack; +} + char *string_replace_all(const char *needle, const char *replacement, char *haystack) { diff --git a/utils.h b/utils.h index 47bf82a..7a87b19 100644 --- a/utils.h +++ b/utils.h @@ -1,6 +1,10 @@ /* copyright 2013 Sascha Kruse and contributors (see LICENSE for licensing information) */ #ifndef UTIL_H #define UTIL_H + +/* replace all occurences of the character needle with the character replacement in haystack */ +char *string_replace_char(char needle, char replacement, char *haystack); + /* replace all occurrences of needle with replacement in haystack */ char *string_replace_all(const char *needle, const char *replacement, char *haystack);