From d019bb4cf7ee416b4e8f57484b7da3dd5a916401 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Fri, 27 Jul 2018 21:43:29 +0200 Subject: [PATCH] Add notifications Notifications are sent hourly and provide a button the user can use to instantly add water --- src/gwr-window.c | 12 ++++--- src/gwr-window.h | 2 ++ src/main.c | 89 +++++++++++++++++++++++++++--------------------- src/meson.build | 1 + 4 files changed, 62 insertions(+), 42 deletions(-) diff --git a/src/gwr-window.c b/src/gwr-window.c index a68987d..03b4410 100644 --- a/src/gwr-window.c +++ b/src/gwr-window.c @@ -89,13 +89,17 @@ gwr_window_init (GwrWindow *self) level_changed(self->remaining, NULL, self); } +void +gwr_window_add_water(GwrWindow *self) +{ + gwr_water_level_set_level (self->remaining, gwr_water_level_get_level (self->remaining) - 0.1); + gwr_water_level_set_level (self->level, gwr_water_level_get_level (self->level) + 0.1); +} + static void gwr_window_add_action(GSimpleAction *action, GVariant *parameter, gpointer user_data) { - GwrWindow *self = GWR_WINDOW(user_data); - - gwr_water_level_set_level (self->remaining, gwr_water_level_get_level (self->remaining) - 0.1); - gwr_water_level_set_level (self->level, gwr_water_level_get_level (self->level) + 0.1); + gwr_window_add_water(GWR_WINDOW(user_data)); } diff --git a/src/gwr-window.h b/src/gwr-window.h index 5f263dd..dfffdaf 100644 --- a/src/gwr-window.h +++ b/src/gwr-window.h @@ -26,4 +26,6 @@ G_BEGIN_DECLS G_DECLARE_FINAL_TYPE (GwrWindow, gwr_window, GWR, WINDOW, GtkApplicationWindow) +void gwr_window_add_water(GwrWindow *window); + G_END_DECLS diff --git a/src/main.c b/src/main.c index 2364464..872c155 100644 --- a/src/main.c +++ b/src/main.c @@ -17,75 +17,88 @@ */ #include +#include #include "gwr-config.h" #include "gwr-window.h" +static void +add_some(NotifyNotification *notification, + char *action, + gpointer user_data) +{ + GwrWindow *window = GWR_WINDOW(user_data); + GError *error = NULL; + + if (!notify_notification_close(notification, &error)) { + g_warning("Could not close notification: %s", error->message); + + g_clear_error(&error); + } + + gwr_window_add_water(window); +} + +static gboolean +show_notification(gpointer user_data) +{ + NotifyNotification *notification; + GwrWindow *window = GWR_WINDOW(user_data); + GError *error = NULL; + + notification = notify_notification_new(_("Remember to drink water!"), NULL, NULL); + notify_notification_set_timeout(notification, 5000); + notify_notification_add_action(notification, "add", _("I just drank"), add_some, g_object_ref(window), g_object_unref); + + if (!notify_notification_show(notification, &error)) { + g_warning("Could not present notification: %s", error->message); + g_clear_error(&error); + } +} + static void on_activate (GtkApplication *app) { GtkWindow *window; - /* It's good practice to check your parameters at the beginning of the - * function. It helps catch errors early and in development instead of - * by your users. - */ g_assert (GTK_IS_APPLICATION (app)); /* Get the current window or create one if necessary. */ window = gtk_application_get_active_window (app); - if (window == NULL) - window = g_object_new (GWR_TYPE_WINDOW, - "application", app, - "default-width", 600, - "default-height", 300, - NULL); + + if (window == NULL) { + window = g_object_new (GWR_TYPE_WINDOW, + "application", app, + NULL); + } /* Ask the window manager/compositor to present the window. */ gtk_window_present (window); + + g_timeout_add_seconds(3600, show_notification, window); } int main (int argc, char *argv[]) { + gint ret; + g_autoptr(GtkApplication) app = NULL; - int ret; /* Set up gettext translations */ bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); textdomain (GETTEXT_PACKAGE); - /* - * Create a new GtkApplication. The application manages our main loop, - * application windows, integration with the window manager/compositor, and - * desktop features such as file opening and single-instance applications. - */ app = gtk_application_new ("eu.polonkai.gergely.GnomeWaterReminder", G_APPLICATION_FLAGS_NONE); - - /* - * We connect to the activate signal to create a window when the application - * has been lauched. Additionally, this signal notifies us when the user - * tries to launch a "second instance" of the application. When they try - * to do that, we'll just present any existing window. - * - * Because we can't pass a pointer to any function type, we have to cast - * our "on_activate" function to a GCallback. - */ g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL); - /* - * Run the application. This function will block until the applicaiton - * exits. Upon return, we have our exit code to return to the shell. (This - * is the code you see when you do `echo $?` after running a command in a - * terminal. - * - * Since GtkApplication inherits from GApplication, we use the parent class - * method "run". But we need to cast, which is what the "G_APPLICATION()" - * macro does. - */ - ret = g_application_run (G_APPLICATION (app), argc, argv); + notify_init(_("Water Reminder")); - return ret; + ret = g_application_run (G_APPLICATION (app), argc, argv); + + notify_uninit(); + + return ret; } diff --git a/src/meson.build b/src/meson.build index 5f70d8e..cb08c78 100644 --- a/src/meson.build +++ b/src/meson.build @@ -10,6 +10,7 @@ gwr_enums = gnome.mkenums_simple('gwr-enums', sources: 'gwr-water-level.h') gwr_deps = [ dependency('gio-2.0', version: '>= 2.50'), dependency('gtk+-3.0', version: '>= 3.22'), + dependency('libnotify', version: '>= 0.7'), ] executable('gnome-water-reminder', gwr_sources, gwr_enums,