Add notifications

Notifications are sent hourly and provide a button the user can use to instantly add water
This commit is contained in:
Gergely Polonkai 2018-07-27 21:43:29 +02:00
parent 2d6a91435c
commit d019bb4cf7
4 changed files with 62 additions and 42 deletions

View File

@ -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));
}

View File

@ -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

View File

@ -17,75 +17,88 @@
*/
#include <glib/gi18n.h>
#include <libnotify/notify.h>
#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;
}

View File

@ -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,