move text generation to notification code

This commit is contained in:
Sascha Kruse 2013-02-22 10:47:11 +00:00
parent cff9c74e0e
commit 64a7dd9743
3 changed files with 63 additions and 53 deletions

View File

@ -230,6 +230,7 @@ int notification_init(notification * n, int id)
} }
n->script = NULL; n->script = NULL;
n->text_to_render = NULL;
n->format = settings.format; n->format = settings.format;
@ -411,4 +412,59 @@ int notification_close(notification * n, int reason)
return -1; return -1;
return notification_close_by_id(n->id, reason); 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: */ /* vim: set ts=8 sw=8 tw=0: */

View File

@ -18,7 +18,8 @@ typedef struct _notification {
char *summary; char *summary;
char *body; char *body;
char *icon; char *icon;
char *msg; char *msg; /* formatted message */
char *text_to_render;
const char *format; const char *format;
char *dbus_client; char *dbus_client;
time_t start; time_t start;
@ -46,3 +47,4 @@ void notification_run_script(notification * n);
int notification_close(notification * n, int reason); int notification_close(notification * n, int reason);
void notification_print(notification * n); void notification_print(notification * n);
char *notification_fix_markup(char *str); char *notification_fix_markup(char *str);
void notification_update_text_to_render(notification *n);

56
x.c
View File

@ -660,55 +660,6 @@ GSList *do_word_wrap(char *text, int max_width)
return result; 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 calculate_x_offset(int line_width, int text_width)
{ {
int leftover = line_width - 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; for (GList * iter = g_queue_peek_head_link(displayed); iter;
iter = iter->next) { iter = iter->next) {
render_text *rt = g_malloc(sizeof(render_text)); render_text *rt = g_malloc(sizeof(render_text));
notification *n = iter->data;
rt->colors = ((notification *) iter->data)->colors; notification_update_text_to_render(n);
char *text = generate_final_text(iter->data); rt->colors = n->colors;
char *text = n->text_to_render;
rt->lines = do_word_wrap(text, width); rt->lines = do_word_wrap(text, width);
free(text);
render_texts = g_slist_append(render_texts, rt); render_texts = g_slist_append(render_texts, rt);
} }