From de9c52f98f2c9bc9bbb962b6e4c8d60b28954565 Mon Sep 17 00:00:00 2001 From: Nikos Tsipinakis Date: Thu, 22 Dec 2016 19:38:49 +0200 Subject: [PATCH] Implement notification_create function notification_create currently simply allocates memory and zeroes it to properly initialise a notification. Since we currently have at least 2 places in the code that create notifications(startup notification in dunst.c and notifications from dbus in dbus.c) the creation should be handled from a central location in case we ever need to change the creation procedure later on. --- src/dbus.c | 2 +- src/dunst.c | 2 +- src/notification.c | 14 ++++++++++++++ src/notification.h | 1 + 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/dbus.c b/src/dbus.c index f05f56e..cf0b33a 100644 --- a/src/dbus.c +++ b/src/dbus.c @@ -295,7 +295,7 @@ static void onNotify(GDBusConnection * connection, } } - notification *n = malloc(sizeof(notification)); + notification *n = notification_create(); if(n == NULL) { die("Unable to allocate memory", EXIT_FAILURE); } diff --git a/src/dunst.c b/src/dunst.c index 11d347b..7109064 100644 --- a/src/dunst.c +++ b/src/dunst.c @@ -343,7 +343,7 @@ int dunst_main(int argc, char *argv[]) x_setup(); if (settings.startup_notification) { - notification *n = malloc(sizeof(notification)); + notification *n = notification_create(); if(n == NULL) { die("Unable to allocate memory", EXIT_FAILURE); } diff --git a/src/notification.c b/src/notification.c index 6a67381..7771682 100644 --- a/src/notification.c +++ b/src/notification.c @@ -311,6 +311,20 @@ char *notification_extract_markup_urls(char **str_ptr) { return urls; } +/* + * This is a helper function that allocates, initialises a notification and + * returns a pointer to it. All notifications should be created using this + * function. After setting all the necessary fields(i.e. appname, summary, + * body, icon etc) notification_init should be called to do the actual + * initialisation. + */ +notification *notification_create(void) +{ + notification *n = malloc(sizeof(notification)); + memset(n, 0, sizeof(notification)); + return n; +} + /* * Initialize the given notification and add it to * the queue. Replace notification with id if id > 0. diff --git a/src/notification.h b/src/notification.h index 5a06d24..e6d42dc 100644 --- a/src/notification.h +++ b/src/notification.h @@ -54,6 +54,7 @@ typedef struct _notification { Actions *actions; } notification; +notification *notification_create(void); int notification_init(notification * n, int id); void notification_free(notification * n); int notification_close_by_id(int id, int reason);