From d91053c85e3e1b7133d3a8a6907b61f279f0b984 Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Thu, 16 Nov 2017 16:16:43 +0100 Subject: [PATCH] Use enum for urgency --- src/notification.c | 37 +++++++++++++++++++++---------------- src/notification.h | 14 +++++++++----- src/rules.c | 8 ++++---- src/rules.h | 2 +- src/settings.c | 2 +- 5 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/notification.c b/src/notification.c index 68c025d..eae26a1 100644 --- a/src/notification.c +++ b/src/notification.c @@ -38,7 +38,7 @@ void notification_print(notification *n) printf("\traw_icon set: %s\n", (n->raw_icon ? "true" : "false")); printf("\tcategory: %s\n", n->category); printf("\ttimeout: %ld\n", n->timeout/1000); - printf("\turgency: %d\n", n->urgency); + printf("\turgency: %s\n", notification_urgency_to_string(n->urgency)); printf("\ttransient: %d\n", n->transient); printf("\tformatted: '%s'\n", n->msg); printf("\tfg: %s\n", n->color_strings[ColFG]); @@ -80,21 +80,7 @@ void notification_run_script(notification *n) char *body = n->body ? n->body : ""; char *icon = n->icon ? n->icon : ""; - char *urgency; - switch (n->urgency) { - case LOW: - urgency = "LOW"; - break; - case NORM: - urgency = "NORMAL"; - break; - case CRIT: - urgency = "CRITICAL"; - break; - default: - urgency = "NORMAL"; - break; - } + const char *urgency = notification_urgency_to_string(n->urgency); int pid1 = fork(); @@ -121,6 +107,25 @@ void notification_run_script(notification *n) } } +/* + * Helper function to convert an urgency to a string + */ +const char *notification_urgency_to_string(enum urgency urgency) +{ + switch (urgency) { + case NONE: + return "NONE"; + case LOW: + return "LOW"; + case NORM: + return "NORMAL"; + case CRIT: + return "CRITICAL"; + default: + return "UNDEF"; + } +} + /* * Helper function to compare to given * notifications. diff --git a/src/notification.h b/src/notification.h index d5b0ae9..5c507b2 100644 --- a/src/notification.h +++ b/src/notification.h @@ -7,12 +7,15 @@ #include "settings.h" -#define LOW 0 -#define NORM 1 -#define CRIT 2 - #define DUNST_NOTIF_MAX_CHARS 5000 +enum urgency { + NONE = -1, + LOW = 0, + NORM = 1, + CRIT = 2, +}; + typedef struct _raw_image { int width; int height; @@ -43,7 +46,7 @@ typedef struct _notification { gint64 start; gint64 timestamp; gint64 timeout; - int urgency; + enum urgency urgency; enum markup_mode markup; bool redisplayed; /* has been displayed before? */ int id; @@ -74,5 +77,6 @@ void notification_replace_single_field(char **haystack, char **needle, const cha void notification_update_text_to_render(notification *n); void notification_do_action(notification *n); +const char *notification_urgency_to_string(enum urgency urgency); #endif /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ diff --git a/src/rules.c b/src/rules.c index a9455b2..d23c7a0 100644 --- a/src/rules.c +++ b/src/rules.c @@ -14,7 +14,7 @@ void rule_apply(rule_t *r, notification *n) { if (r->timeout != -1) n->timeout = r->timeout; - if (r->urgency != -1) + if (r->urgency != NONE) n->urgency = r->urgency; if (r->history_ignore != -1) n->history_ignore = r->history_ignore; @@ -62,9 +62,9 @@ void rule_init(rule_t *r) r->body = NULL; r->icon = NULL; r->category = NULL; - r->msg_urgency = -1; + r->msg_urgency = NONE; r->timeout = -1; - r->urgency = -1; + r->urgency = NONE; r->markup = MARKUP_NULL; r->new_icon = NULL; r->history_ignore = false; @@ -86,6 +86,6 @@ bool rule_matches_notification(rule_t *r, notification *n) && (!r->icon || !fnmatch(r->icon, n->icon, 0)) && (!r->category || !fnmatch(r->category, n->category, 0)) && (r->match_transient == -1 || (r->match_transient == n->transient)) - && (r->msg_urgency == -1 || r->msg_urgency == n->urgency)); + && (r->msg_urgency == NONE || r->msg_urgency == n->urgency)); } /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ diff --git a/src/rules.h b/src/rules.h index a813d98..b8d1d87 100644 --- a/src/rules.h +++ b/src/rules.h @@ -20,7 +20,7 @@ typedef struct _rule_t { /* actions */ gint64 timeout; - int urgency; + enum urgency urgency; enum markup_mode markup; int history_ignore; int match_transient; diff --git a/src/settings.c b/src/settings.c index d9566ad..350a0d1 100644 --- a/src/settings.c +++ b/src/settings.c @@ -47,7 +47,7 @@ static enum markup_mode parse_markup_mode(const char *mode) } } -static int ini_get_urgency(const char *section, const char *key, const int def) +static enum urgency ini_get_urgency(const char *section, const char *key, const int def) { int ret = def; char *urg = ini_get_string(section, key, "");