From 0baeaa160a595fd1ce0cb2d1d84097f7b315a08e Mon Sep 17 00:00:00 2001 From: Georges Basile Stavracas Neto Date: Thu, 9 May 2019 17:09:41 -0300 Subject: [PATCH] window: Delegate changing dates to GcalView The view knows its time range, and knows what the next or previous dates should be. So instead of calculating the next date manually, ask the views for that. --- data/ui/window.ui | 6 +- src/gui/gcal-window.c | 120 ++++++++++++++++-------------------- src/views/gcal-month-view.c | 21 +++++++ src/views/gcal-view.c | 63 +++++++++++++++++++ src/views/gcal-view.h | 7 +++ src/views/gcal-week-view.c | 21 +++++++ src/views/gcal-year-view.c | 21 +++++++ 7 files changed, 190 insertions(+), 69 deletions(-) diff --git a/data/ui/window.ui b/data/ui/window.ui index d4dcb8d0..622e9ed8 100644 --- a/data/ui/window.ui +++ b/data/ui/window.ui @@ -168,9 +168,9 @@ True True True + win.today - 1 @@ -184,9 +184,9 @@ True True True + win.previous-date - True @@ -200,9 +200,9 @@ True True True + win.next-date - True diff --git a/src/gui/gcal-window.c b/src/gui/gcal-window.c index c853585c..ae9775c9 100644 --- a/src/gui/gcal-window.c +++ b/src/gui/gcal-window.c @@ -176,22 +176,8 @@ enum gtk_application_set_accels_for_action (GTK_APPLICATION (app), action, tmp);\ } - -static void on_show_calendars_action_activated (GSimpleAction *action, - GVariant *param, - gpointer user_data); - -static void on_view_action_activated (GSimpleAction *action, - GVariant *param, - gpointer user_data); - G_DEFINE_TYPE (GcalWindow, gcal_window, GTK_TYPE_APPLICATION_WINDOW) -static const GActionEntry actions[] = { - {"change-view", on_view_action_activated, "i" }, - {"show-calendars", on_show_calendars_action_activated }, -}; - static GParamSpec* properties[N_PROPS] = { NULL, }; @@ -274,57 +260,6 @@ update_active_date (GcalWindow *window, GCAL_EXIT; } -static void -date_updated (GcalWindow *window, - GtkButton *button) -{ - g_autoptr (GDateTime) new_date = NULL; - gboolean move_today; - gboolean move_back; - gint factor; - - GCAL_ENTRY; - - factor = window->rtl ? - 1 : 1; - - move_today = window->today_button == (GtkWidget*) button; - move_back = window->back_button == (GtkWidget*) button; - - if (move_today) - { - new_date = g_date_time_new_now_local (); - } - else - { - gint diff = factor * (move_back ? -1 : 1); - - switch (window->active_view) - { - case GCAL_WINDOW_VIEW_DAY: - new_date = g_date_time_add_days (window->active_date, 1 * diff); - break; - case GCAL_WINDOW_VIEW_WEEK: - new_date = g_date_time_add_weeks (window->active_date, 1 * diff); - break; - case GCAL_WINDOW_VIEW_MONTH: - new_date = g_date_time_add_months (window->active_date, 1 * diff); - break; - case GCAL_WINDOW_VIEW_YEAR: - new_date = g_date_time_add_years (window->active_date, 1 * diff); - break; - case GCAL_WINDOW_VIEW_LIST: - case GCAL_WINDOW_VIEW_SEARCH: - default: - break; - } - } - - update_active_date (window, new_date); - - GCAL_EXIT; -} - - static void on_show_calendars_action_activated (GSimpleAction *action, GVariant *param, @@ -361,6 +296,52 @@ on_view_action_activated (GSimpleAction *action, g_object_notify_by_pspec (G_OBJECT (user_data), properties[PROP_ACTIVE_VIEW]); } +static void +on_window_next_date_activated_cb (GSimpleAction *action, + GVariant *param, + gpointer user_data) +{ + g_autoptr (GDateTime) next_date = NULL; + GcalWindow *self; + GcalView *current_view; + + self = GCAL_WINDOW (user_data); + current_view = GCAL_VIEW (self->views[self->active_view]); + next_date = gcal_view_get_next_date (current_view); + + update_active_date (self, next_date); +} + +static void +on_window_previous_date_activated_cb (GSimpleAction *action, + GVariant *param, + gpointer user_data) +{ + g_autoptr (GDateTime) previous_date = NULL; + GcalWindow *self; + GcalView *current_view; + + self = GCAL_WINDOW (user_data); + current_view = GCAL_VIEW (self->views[self->active_view]); + previous_date = gcal_view_get_previous_date (current_view); + + update_active_date (self, previous_date); +} + +static void +on_window_today_activated_cb (GSimpleAction *action, + GVariant *param, + gpointer user_data) +{ + g_autoptr (GDateTime) today = NULL; + GcalWindow *self; + + self = GCAL_WINDOW (user_data); + today = g_date_time_new_now_local (); + + update_active_date (self, today); +} + static void load_geometry (GcalWindow *self) { @@ -1051,7 +1032,6 @@ gcal_window_class_init (GcalWindowClass *klass) gtk_widget_class_bind_template_child (widget_class, GcalWindow, notification_close_button); gtk_widget_class_bind_template_callback (widget_class, view_changed); - gtk_widget_class_bind_template_callback (widget_class, date_updated); /* Event removal related */ gtk_widget_class_bind_template_callback (widget_class, hide_notification); @@ -1075,6 +1055,14 @@ gcal_window_class_init (GcalWindowClass *klass) static void gcal_window_init (GcalWindow *self) { + static const GActionEntry actions[] = { + {"change-view", on_view_action_activated, "i" }, + {"next-date", on_window_next_date_activated_cb }, + {"previous-date", on_window_previous_date_activated_cb }, + {"show-calendars", on_show_calendars_action_activated }, + {"today", on_window_today_activated_cb } + }; + GApplication *app; /* Setup actions */ diff --git a/src/views/gcal-month-view.c b/src/views/gcal-month-view.c index 24893f82..64b4c982 100644 --- a/src/views/gcal-month-view.c +++ b/src/views/gcal-month-view.c @@ -1220,6 +1220,25 @@ gcal_month_view_update_subscription (GcalView *view) range_end); } +static GDateTime* +gcal_month_view_get_next_date (GcalView *view) +{ + GcalMonthView *self = GCAL_MONTH_VIEW (view); + + g_assert (self->date != NULL); + return g_date_time_add_months (self->date, 1); +} + + +static GDateTime* +gcal_month_view_get_previous_date (GcalView *view) +{ + GcalMonthView *self = GCAL_MONTH_VIEW (view); + + g_assert (self->date != NULL); + return g_date_time_add_months (self->date, -1); +} + static void gcal_view_interface_init (GcalViewInterface *iface) { @@ -1228,6 +1247,8 @@ gcal_view_interface_init (GcalViewInterface *iface) iface->clear_marks = gcal_month_view_clear_marks; iface->get_children_by_uuid = gcal_month_view_get_children_by_uuid; iface->update_subscription = gcal_month_view_update_subscription; + iface->get_next_date = gcal_month_view_get_next_date; + iface->get_previous_date = gcal_month_view_get_previous_date; } diff --git a/src/views/gcal-view.c b/src/views/gcal-view.c index baa38848..c5b0bf50 100644 --- a/src/views/gcal-view.c +++ b/src/views/gcal-view.c @@ -210,3 +210,66 @@ gcal_view_update_subscription (GcalView *self) GCAL_VIEW_GET_IFACE (self)->update_subscription (self); } + +/** + * gcal_view_get_next_date: + * @self: a #GcalView + * + * Retrieves the next date from @self. Different views have + * different time ranges (e.g. day view ranges 1 day, week + * view ranges 1 week, etc) and pressing the next button + * of the main window may advance the time in different steps. + * + * Returns: (transfer full): a #GDateTime + */ +GDateTime* +gcal_view_get_next_date (GcalView *self) +{ + g_autoptr (GDateTime) next_date = NULL; + + g_return_val_if_fail (GCAL_IS_VIEW (self), NULL); + g_return_val_if_fail (GCAL_VIEW_GET_IFACE (self)->get_next_date, NULL); + + next_date = GCAL_VIEW_GET_IFACE (self)->get_next_date (self); + +#ifdef GCAL_ENABLE_TRACE + { + g_autofree gchar *str = NULL; + + str = g_date_time_format (next_date, "%x %X %z"); + g_debug ("%s's next date: %s", G_OBJECT_TYPE_NAME (self), str); + } +#endif + + return g_steal_pointer (&next_date); +} + +/** + * gcal_view_get_previous_date: + * @self: a #GcalView + * + * Retrieves the previous date from @self. + * + * Returns: (transfer full): a #GDateTime + */ +GDateTime* +gcal_view_get_previous_date (GcalView *self) +{ + g_autoptr (GDateTime) previous_date = NULL; + + g_return_val_if_fail (GCAL_IS_VIEW (self), NULL); + g_return_val_if_fail (GCAL_VIEW_GET_IFACE (self)->get_previous_date, NULL); + + previous_date = GCAL_VIEW_GET_IFACE (self)->get_previous_date (self); + +#ifdef GCAL_ENABLE_TRACE + { + g_autofree gchar *str = NULL; + + str = g_date_time_format (previous_date, "%x %X %z"); + g_debug ("%s's previous date: %s", G_OBJECT_TYPE_NAME (self), str); + } +#endif + + return g_steal_pointer (&previous_date); +} diff --git a/src/views/gcal-view.h b/src/views/gcal-view.h index b2eea60d..f7aedc34 100644 --- a/src/views/gcal-view.h +++ b/src/views/gcal-view.h @@ -50,6 +50,10 @@ struct _GcalViewInterface void (*update_subscription) (GcalView *self); + GDateTime* (*get_next_date) (GcalView *self); + + GDateTime* (*get_previous_date) (GcalView *self); + /* Marks related API */ void (*clear_marks) (GcalView *view); @@ -73,6 +77,9 @@ GList* gcal_view_get_children_by_uuid (GcalView void gcal_view_update_subscription (GcalView *self); +GDateTime* gcal_view_get_next_date (GcalView *self); + +GDateTime* gcal_view_get_previous_date (GcalView *self); G_END_DECLS diff --git a/src/views/gcal-week-view.c b/src/views/gcal-week-view.c index 0d4dc49a..1622ecf7 100644 --- a/src/views/gcal-week-view.c +++ b/src/views/gcal-week-view.c @@ -248,6 +248,25 @@ gcal_week_view_update_subscription (GcalView *view) range_end); } +static GDateTime* +gcal_week_view_get_next_date (GcalView *view) +{ + GcalWeekView *self = GCAL_WEEK_VIEW (view); + + g_assert (self->date != NULL); + return g_date_time_add_weeks (self->date, 1); +} + + +static GDateTime* +gcal_week_view_get_previous_date (GcalView *view) +{ + GcalWeekView *self = GCAL_WEEK_VIEW (view); + + g_assert (self->date != NULL); + return g_date_time_add_weeks (self->date, -1); +} + static void gcal_view_interface_init (GcalViewInterface *iface) { @@ -256,6 +275,8 @@ gcal_view_interface_init (GcalViewInterface *iface) iface->get_children_by_uuid = gcal_week_view_get_children_by_uuid; iface->clear_marks = gcal_week_view_clear_marks; iface->update_subscription = gcal_week_view_update_subscription; + iface->get_next_date = gcal_week_view_get_next_date; + iface->get_previous_date = gcal_week_view_get_previous_date; } static void diff --git a/src/views/gcal-year-view.c b/src/views/gcal-year-view.c index 87befea0..c02bb7ba 100644 --- a/src/views/gcal-year-view.c +++ b/src/views/gcal-year-view.c @@ -1609,6 +1609,25 @@ gcal_year_view_update_subscription (GcalView *view) range_end); } +static GDateTime* +gcal_year_view_get_next_date (GcalView *view) +{ + GcalYearView *self = GCAL_YEAR_VIEW (view); + + g_assert (self->date != NULL); + return g_date_time_add_years (self->date, 1); +} + + +static GDateTime* +gcal_year_view_get_previous_date (GcalView *view) +{ + GcalYearView *self = GCAL_YEAR_VIEW (view); + + g_assert (self->date != NULL); + return g_date_time_add_years (self->date, -1); +} + static void gcal_view_interface_init (GcalViewInterface *iface) { @@ -1617,6 +1636,8 @@ gcal_view_interface_init (GcalViewInterface *iface) iface->set_date = gcal_year_view_set_date; iface->get_children_by_uuid = gcal_year_view_get_children_by_uuid; iface->update_subscription = gcal_year_view_update_subscription; + iface->get_next_date = gcal_year_view_get_next_date; + iface->get_previous_date = gcal_year_view_get_previous_date; } static void