From 64a7dd9743dad8a19a35002ca1b310edd080f3e5 Mon Sep 17 00:00:00 2001 From: Sascha Kruse Date: Fri, 22 Feb 2013 10:47:11 +0000 Subject: [PATCH] move text generation to notification code --- notification.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ notification.h | 4 +++- x.c | 56 ++++---------------------------------------------- 3 files changed, 63 insertions(+), 53 deletions(-) diff --git a/notification.c b/notification.c index 6274369..4146ef8 100644 --- a/notification.c +++ b/notification.c @@ -230,6 +230,7 @@ int notification_init(notification * n, int id) } n->script = NULL; + n->text_to_render = NULL; n->format = settings.format; @@ -411,4 +412,59 @@ int notification_close(notification * n, int reason) return -1; return notification_close_by_id(n->id, reason); } + +void notification_update_text_to_render(notification *n) +{ + if (n->text_to_render) { + free(n->text_to_render); + n->text_to_render = NULL; + } + + char *buf = NULL; + + char *msg = g_strstrip(n->msg); + + /* print dup_count and msg */ + if (n->dup_count > 0 && (n->actions || n->urls)) { + buf = g_strdup_printf("(%d%s%s) %s", + n->dup_count, + n->actions ? "A" : "", + n->urls ? "U" : "", msg); + } else if (n->actions || n->urls) { + buf = g_strdup_printf("(%s%s) %s", + n->actions ? "A" : "", + n->urls ? "U" : "", msg); + } else { + buf = g_strdup(msg); + } + + /* print age */ + int hours, minutes, seconds; + time_t t_delta = time(NULL) - n->timestamp; + + if (settings.show_age_threshold >= 0 + && t_delta >= settings.show_age_threshold) { + hours = t_delta / 3600; + minutes = t_delta / 60 % 60; + seconds = t_delta % 60; + + char *new_buf; + if (hours > 0) { + new_buf = + g_strdup_printf("%s (%dh %dm %ds old)", buf, hours, + minutes, seconds); + } else if (minutes > 0) { + new_buf = + g_strdup_printf("%s (%dm %ds old)", buf, minutes, + seconds); + } else { + new_buf = g_strdup_printf("%s (%ds old)", buf, seconds); + } + + free(buf); + buf = new_buf; + } + + n->text_to_render = buf; +} /* vim: set ts=8 sw=8 tw=0: */ diff --git a/notification.h b/notification.h index 7fd7062..d621da2 100644 --- a/notification.h +++ b/notification.h @@ -18,7 +18,8 @@ typedef struct _notification { char *summary; char *body; char *icon; - char *msg; + char *msg; /* formatted message */ + char *text_to_render; const char *format; char *dbus_client; time_t start; @@ -46,3 +47,4 @@ void notification_run_script(notification * n); int notification_close(notification * n, int reason); void notification_print(notification * n); char *notification_fix_markup(char *str); +void notification_update_text_to_render(notification *n); diff --git a/x.c b/x.c index c7c88b8..39ee588 100644 --- a/x.c +++ b/x.c @@ -660,55 +660,6 @@ GSList *do_word_wrap(char *text, int max_width) return result; } -char *generate_final_text(notification * n) -{ - char *msg = g_strstrip(n->msg); - char *buf; - - /* print dup_count and msg */ - if (n->dup_count > 0 && (n->actions || n->urls)) { - buf = g_strdup_printf("(%d%s%s) %s", - n->dup_count, - n->actions ? "A" : "", - n->urls ? "U" : "", msg); - } else if (n->actions || n->urls) { - buf = g_strdup_printf("(%s%s) %s", - n->actions ? "A" : "", - n->urls ? "U" : "", msg); - } else { - buf = g_strdup(msg); - } - - /* print age */ - int hours, minutes, seconds; - time_t t_delta = time(NULL) - n->timestamp; - - if (settings.show_age_threshold >= 0 - && t_delta >= settings.show_age_threshold) { - hours = t_delta / 3600; - minutes = t_delta / 60 % 60; - seconds = t_delta % 60; - - char *new_buf; - if (hours > 0) { - new_buf = - g_strdup_printf("%s (%dh %dm %ds old)", buf, hours, - minutes, seconds); - } else if (minutes > 0) { - new_buf = - g_strdup_printf("%s (%dm %ds old)", buf, minutes, - seconds); - } else { - new_buf = g_strdup_printf("%s (%ds old)", buf, seconds); - } - - free(buf); - buf = new_buf; - } - - return buf; -} - int calculate_x_offset(int line_width, int text_width) { int leftover = line_width - text_width; @@ -847,11 +798,12 @@ GSList *generate_render_texts(int width) for (GList * iter = g_queue_peek_head_link(displayed); iter; iter = iter->next) { render_text *rt = g_malloc(sizeof(render_text)); + notification *n = iter->data; - rt->colors = ((notification *) iter->data)->colors; - char *text = generate_final_text(iter->data); + notification_update_text_to_render(n); + rt->colors = n->colors; + char *text = n->text_to_render; rt->lines = do_word_wrap(text, width); - free(text); render_texts = g_slist_append(render_texts, rt); }