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

28
menu.c
View File

@ -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)
{
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);
return;
break;
}
}
}
invoke_action(input);
}
/*

View File

@ -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");
}
}

View File

@ -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)
{

View File

@ -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);