cleaner handling of actions in menu

This commit is contained in:
progandy 2013-02-27 16:51:28 +01:00 committed by Sascha Kruse
parent fd1ae14fa3
commit 00b2be3dc0
4 changed files with 39 additions and 14 deletions

36
menu.c
View File

@ -97,24 +97,26 @@ void invoke_action(const char *action)
notification *invoked = NULL; notification *invoked = NULL;
char *action_identifier = NULL; char *action_identifier = NULL;
char *name_begin = strstr(action, "("); char *appname_begin = strchr(action, '[');
if (!name_begin) { if (!appname_begin) {
printf("invalid action: %s\n", action); printf("invalid action: %s\n", action);
return; 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; for (GList * iter = g_queue_peek_head_link(displayed); iter;
iter = iter->next) { iter = iter->next) {
notification *n = iter->data; 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) if (!n->actions)
continue; continue;
for (int i = 0; i < n->actions->count; i += 2) { for (int i = 0; i < n->actions->count; i += 2) {
char *a_identifier = n->actions->actions[i]; char *a_identifier = n->actions->actions[i];
char *name = n->actions->actions[i + 1]; 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; invoked = n;
action_identifier = a_identifier; action_identifier = a_identifier;
break; break;
@ -134,14 +136,24 @@ void invoke_action(const char *action)
*/ */
void dispatch_menu_result(const char *input) void dispatch_menu_result(const char *input)
{ {
char *maybe_url = extract_urls(input); switch (input[0]) {
if (maybe_url) { case '#':
open_browser(maybe_url); invoke_action(input + 1);
free(maybe_url); break;
return; 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);
} }
/* /*

View File

@ -332,6 +332,8 @@ int notification_init(notification * n, int id)
n->actions->dmenu_str = NULL; n->actions->dmenu_str = NULL;
for (int i = 0; i < n->actions->count; i += 2) { for (int i = 0; i < n->actions->count; i += 2) {
char *human_readable = n->actions->actions[i + 1]; 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", n->appname);
printf("debug: %s\n", human_readable); printf("debug: %s\n", human_readable);
char *tmp = char *tmp =
@ -341,8 +343,8 @@ int notification_init(notification * n, int id)
n->actions->dmenu_str = n->actions->dmenu_str =
string_append(n->actions->dmenu_str, string_append(n->actions->dmenu_str,
g_strdup_printf("%s(%s)", n->appname, g_strdup_printf("#%s [%s]", human_readable,
human_readable), n->appname),
"\n"); "\n");
} }
} }

View File

@ -10,6 +10,13 @@
#include "utils.h" #include "utils.h"
#include "dunst.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 *string_replace_all(const char *needle, const char *replacement,
char *haystack) char *haystack)
{ {

View File

@ -1,6 +1,10 @@
/* copyright 2013 Sascha Kruse and contributors (see LICENSE for licensing information) */ /* copyright 2013 Sascha Kruse and contributors (see LICENSE for licensing information) */
#ifndef UTIL_H #ifndef UTIL_H
#define 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 */ /* replace all occurrences of needle with replacement in haystack */
char *string_replace_all(const char *needle, const char *replacement, char *string_replace_all(const char *needle, const char *replacement,
char *haystack); char *haystack);