[gnome-calendar] views: get rid of current date
- From: Georges Basile Stavracas Neto <gbsneto src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-calendar] views: get rid of current date
- Date: Fri, 17 Mar 2017 18:52:41 +0000 (UTC)
commit 002271ebb1365f39b29c32face4813830c0a0a00
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date: Fri Mar 17 15:49:12 2017 -0300
views: get rid of current date
We can easily track the current date with GDateTime API,
so we don't actually need the current date field.
src/gcal-window.c | 66 ++++++++++++++++++++++-------------------
src/views/gcal-month-view.c | 20 ++++--------
src/views/gcal-month-view.h | 4 --
src/views/gcal-week-grid.c | 14 ---------
src/views/gcal-week-grid.h | 3 --
src/views/gcal-week-header.c | 16 ----------
src/views/gcal-week-header.h | 3 --
src/views/gcal-week-view.c | 14 ---------
src/views/gcal-week-view.h | 3 --
src/views/gcal-year-view.c | 55 +++++++++++++++++++---------------
10 files changed, 73 insertions(+), 125 deletions(-)
---
diff --git a/src/gcal-window.c b/src/gcal-window.c
index b2a03c4..7856abe 100644
--- a/src/gcal-window.c
+++ b/src/gcal-window.c
@@ -99,7 +99,6 @@ struct _GcalWindow
GcalWindowViewType active_view;
icaltimetype *active_date;
- icaltimetype *current_date;
gboolean rtl;
/* states */
@@ -169,28 +168,31 @@ static const GActionEntry actions[] = {
static void
update_today_button_sensitive (GcalWindow *window)
{
+ g_autoptr (GDateTime) now;
gboolean sensitive;
+ now = g_date_time_new_now_local ();
+
switch (window->active_view)
{
case GCAL_WINDOW_VIEW_DAY:
- sensitive = window->active_date->year != window->current_date->year ||
- window->active_date->month != window->current_date->month ||
- window->active_date->day != window->current_date->day;
+ sensitive = window->active_date->year != g_date_time_get_year (now) ||
+ window->active_date->month != g_date_time_get_month (now) ||
+ window->active_date->day != g_date_time_get_day_of_month (now);
break;
case GCAL_WINDOW_VIEW_WEEK:
- sensitive = window->active_date->year != window->current_date->year ||
- icaltime_week_number (*window->active_date) != icaltime_week_number
(*window->current_date);
+ sensitive = window->active_date->year != g_date_time_get_year (now) ||
+ icaltime_week_number (*window->active_date) != g_date_time_get_week_of_year (now);
break;
case GCAL_WINDOW_VIEW_MONTH:
- sensitive = window->active_date->year != window->current_date->year ||
- window->active_date->month != window->current_date->month;
+ sensitive = window->active_date->year != g_date_time_get_year (now) ||
+ window->active_date->month != g_date_time_get_month (now);
break;
case GCAL_WINDOW_VIEW_YEAR:
- sensitive = window->active_date->year != window->current_date->year;
+ sensitive = window->active_date->year != g_date_time_get_year (now);
break;
case GCAL_WINDOW_VIEW_LIST:
@@ -301,7 +303,10 @@ date_updated (GtkButton *button,
if (move_today)
{
- *new_date = *(window->current_date);
+ g_autoptr (GDateTime) now;
+
+ now = g_date_time_new_now_local ();
+ new_date = datetime_to_icaltime (now);
}
else
{
@@ -410,24 +415,20 @@ key_pressed (GtkWidget *widget,
static gboolean
update_current_date (GcalWindow *window)
{
- guint seconds;
-
GCAL_ENTRY;
- if (window->current_date == NULL)
- window->current_date = g_new0 (icaltimetype, 1);
-
- *(window->current_date) = icaltime_current_time_with_zone (gcal_manager_get_system_timezone
(window->manager));
- *(window->current_date) = icaltime_set_timezone (window->current_date, gcal_manager_get_system_timezone
(window->manager));
+ /* FIXME: we end up here before constructed(), this shouldn't happen */
+ if (!window->views[GCAL_WINDOW_VIEW_WEEK])
+ GCAL_GOTO (out);
- gcal_week_view_set_current_date (GCAL_WEEK_VIEW (window->week_view), window->current_date);
- gcal_month_view_set_current_date (GCAL_MONTH_VIEW (window->month_view), window->current_date);
- gcal_year_view_set_current_date (GCAL_YEAR_VIEW (window->year_view), window->current_date);
+ gtk_widget_queue_draw (window->views[GCAL_WINDOW_VIEW_WEEK]);
+ gtk_widget_queue_draw (window->views[GCAL_WINDOW_VIEW_MONTH]);
+ gtk_widget_queue_draw (window->views[GCAL_WINDOW_VIEW_YEAR]);
- seconds = 24 * 60 * 60 - (icaltime_as_timet (*(window->current_date)) % (24 * 60 * 60));
- g_timeout_add_seconds (seconds, (GSourceFunc) update_current_date, window);
+out:
+ g_timeout_add_seconds (60, (GSourceFunc) update_current_date, window);
- GCAL_RETURN (FALSE);
+ GCAL_RETURN (G_SOURCE_CONTINUE);
}
static void
@@ -1195,6 +1196,8 @@ gcal_window_constructed (GObject *object)
gchar *clock_format;
gboolean use_24h_format;
+ GCAL_ENTRY;
+
if (G_OBJECT_CLASS (gcal_window_parent_class)->constructed != NULL)
G_OBJECT_CLASS (gcal_window_parent_class)->constructed (object);
@@ -1251,6 +1254,8 @@ gcal_window_constructed (GObject *object)
g_list_free (sources);
}
+
+ GCAL_EXIT;
}
static void
@@ -1290,7 +1295,6 @@ gcal_window_finalize (GObject *object)
g_clear_object (&window->views_switcher);
g_free (window->active_date);
- g_free (window->current_date);
G_OBJECT_CLASS (gcal_window_parent_class)->finalize (object);
@@ -1305,6 +1309,8 @@ gcal_window_set_property (GObject *object,
{
GcalWindow *self = GCAL_WINDOW (object);
+ GCAL_ENTRY;
+
switch (property_id)
{
case PROP_ACTIVE_VIEW:
@@ -1342,6 +1348,8 @@ gcal_window_set_property (GObject *object,
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
+
+ GCAL_EXIT;
}
static void
@@ -1594,17 +1602,12 @@ gcal_window_new_with_view_and_date (GcalApplication *app,
void
gcal_window_new_event (GcalWindow *self)
{
- GDateTime *start_date, *end_date;
- icaltimetype date;
+ g_autoptr (GDateTime) start_date, end_date;
/* 1st and 2nd steps */
set_new_event_mode (self, TRUE);
- date = *self->current_date;
- date.is_date = 1;
-
- start_date = icaltime_to_datetime (&date);
- end_date = icaltime_to_datetime (&date);
+ start_date = g_date_time_new_now_local ();
/* adjusting dates according to the actual view */
switch (self->active_view)
@@ -1620,6 +1623,7 @@ gcal_window_new_event (GcalWindow *self)
case GCAL_WINDOW_VIEW_LIST:
case GCAL_WINDOW_VIEW_SEARCH:
default:
+ end_date = g_date_time_add_days (start_date, 1);
break;
}
diff --git a/src/views/gcal-month-view.c b/src/views/gcal-month-view.c
index 7e02947..c5d069b 100644
--- a/src/views/gcal-month-view.c
+++ b/src/views/gcal-month-view.c
@@ -82,8 +82,6 @@ struct _GcalMonthView
/* text direction factors */
gboolean k;
- icaltimetype *current_date;
-
/* The cell hovered during Drag and Drop */
gint dnd_cell;
@@ -1496,6 +1494,8 @@ gcal_month_view_draw (GtkWidget *widget,
PangoLayout *layout;
PangoFontDescription *font_desc, *sfont_desc;
+ g_autoptr (GDateTime) today;
+
gint font_width, font_height, pos_x, pos_y, shown_rows;
gint i, j, sw, lower_mark = 43, upper_mark = -2;
gint cell_width, cell_height;
@@ -1505,6 +1505,8 @@ gcal_month_view_draw (GtkWidget *widget,
self = GCAL_MONTH_VIEW (widget);
ppriv = GCAL_SUBSCRIBER_VIEW (widget)->priv;
+ today = g_date_time_new_now_local ();
+
/* fonts and colors selection */
context = gtk_widget_get_style_context (widget);
state = gtk_style_context_get_state (context);
@@ -1705,8 +1707,9 @@ gcal_month_view_draw (GtkWidget *widget,
g_object_unref (overflow_layout);
}
- if (self->date->year == self->current_date->year && self->date->month == self->current_date->month &&
- j == self->current_date->day)
+ if (self->date->year == g_date_time_get_year (today) &&
+ self->date->month == g_date_time_get_month (today) &&
+ j == g_date_time_get_day_of_month (today))
{
PangoFontDescription *cfont_desc;
PangoLayout *clayout;
@@ -2286,15 +2289,6 @@ gcal_month_view_init (GcalMonthView *self)
}
/* Public API */
-void
-gcal_month_view_set_current_date (GcalMonthView *self,
- icaltimetype *current_date)
-{
- g_return_if_fail (GCAL_IS_MONTH_VIEW (self));
-
- self->current_date = current_date;
- gtk_widget_queue_draw (GTK_WIDGET (self));
-}
/**
* gcal_month_view_set_first_weekday:
diff --git a/src/views/gcal-month-view.h b/src/views/gcal-month-view.h
index 2f5964a..174fffc 100644
--- a/src/views/gcal-month-view.h
+++ b/src/views/gcal-month-view.h
@@ -29,10 +29,6 @@ G_BEGIN_DECLS
G_DECLARE_FINAL_TYPE (GcalMonthView, gcal_month_view, GCAL, MONTH_VIEW, GcalSubscriberView)
-
-void gcal_month_view_set_current_date (GcalMonthView *self,
- icaltimetype *current_date);
-
void gcal_month_view_set_first_weekday (GcalMonthView *self,
gint day_nr);
diff --git a/src/views/gcal-week-grid.c b/src/views/gcal-week-grid.c
index c87c6a2..fb1e838 100644
--- a/src/views/gcal-week-grid.c
+++ b/src/views/gcal-week-grid.c
@@ -55,7 +55,6 @@ struct _GcalWeekGrid
gboolean use_24h_format : 1;
icaltimetype *active_date;
- icaltimetype *current_date;
GcalRangeTree *events;
@@ -257,7 +256,6 @@ gcal_week_grid_finalize (GObject *object)
g_clear_pointer (&self->events, gcal_range_tree_unref);
g_clear_pointer (&self->active_date, g_free);
- g_clear_pointer (&self->current_date, g_free);
if (self->redraw_timeout_id > 0)
{
@@ -1180,18 +1178,6 @@ gcal_week_grid_set_use_24h_format (GcalWeekGrid *self,
}
void
-gcal_week_grid_set_current_date (GcalWeekGrid *self,
- icaltimetype *current_date)
-{
- g_return_if_fail (GCAL_IS_WEEK_GRID (self));
-
- g_clear_pointer (&self->current_date, g_free);
- self->current_date = gcal_dup_icaltime (current_date);
-
- gtk_widget_queue_draw (GTK_WIDGET (self));
-}
-
-void
gcal_week_grid_add_event (GcalWeekGrid *self,
GcalEvent *event)
{
diff --git a/src/views/gcal-week-grid.h b/src/views/gcal-week-grid.h
index 34d4881..9992508 100644
--- a/src/views/gcal-week-grid.h
+++ b/src/views/gcal-week-grid.h
@@ -40,9 +40,6 @@ void gcal_week_grid_set_first_weekday (GcalWeekGrid
void gcal_week_grid_set_use_24h_format (GcalWeekGrid *week_grid,
gboolean use_24h_format);
-void gcal_week_grid_set_current_date (GcalWeekGrid *week_grid,
- icaltimetype *current_date);
-
void gcal_week_grid_add_event (GcalWeekGrid *self,
GcalEvent *event);
diff --git a/src/views/gcal-week-header.c b/src/views/gcal-week-header.c
index b0d7745..15db96a 100644
--- a/src/views/gcal-week-header.c
+++ b/src/views/gcal-week-header.c
@@ -69,7 +69,6 @@ struct _GcalWeekHeader
gint redraw_timeout_id;
icaltimetype *active_date;
- icaltimetype *current_date;
gint selection_start;
gint selection_end;
@@ -1025,7 +1024,6 @@ gcal_week_header_finalize (GObject *object)
gint i;
g_clear_pointer (&self->active_date, g_free);
- g_clear_pointer (&self->current_date, g_free);
if (self->redraw_timeout_id > 0)
{
@@ -1744,20 +1742,6 @@ gcal_week_header_get_children_by_uuid (GcalWeekHeader *self,
return result;
}
-void
-gcal_week_header_set_current_date (GcalWeekHeader *self,
- icaltimetype *current_date)
-{
- g_return_if_fail (GCAL_IS_WEEK_HEADER (self));
-
- g_clear_pointer (&self->current_date, g_free);
- self->current_date = gcal_dup_icaltime (current_date);
-
- update_title (self);
-
- gtk_widget_queue_draw (GTK_WIDGET (self));
-}
-
GtkSizeGroup*
gcal_week_header_get_sidebar_size_group (GcalWeekHeader *self)
{
diff --git a/src/views/gcal-week-header.h b/src/views/gcal-week-header.h
index f916c4e..fa866dc 100644
--- a/src/views/gcal-week-header.h
+++ b/src/views/gcal-week-header.h
@@ -40,9 +40,6 @@ void gcal_week_header_set_first_weekday (GcalWeekHeader
void gcal_week_header_set_use_24h_format (GcalWeekHeader *self,
gboolean use_24h_format);
-void gcal_week_header_set_current_date (GcalWeekHeader *self,
- icaltimetype *current_date);
-
void gcal_week_header_add_event (GcalWeekHeader *self,
GcalEvent *event);
diff --git a/src/views/gcal-week-view.c b/src/views/gcal-week-view.c
index bd761eb..42fc2a2 100644
--- a/src/views/gcal-week-view.c
+++ b/src/views/gcal-week-view.c
@@ -62,7 +62,6 @@ struct _GcalWeekView
/* property */
icaltimetype *date;
- icaltimetype *current_date;
GcalManager *manager; /* weak referenced */
guint scroll_grid_timeout_id;
@@ -655,16 +654,3 @@ gcal_week_view_set_use_24h_format (GcalWeekView *self,
self->use_24h_format = use_24h;
}
-
-void
-gcal_week_view_set_current_date (GcalWeekView *self,
- icaltimetype *current_date)
-{
- g_return_if_fail (GCAL_IS_WEEK_VIEW (self));
-
- g_clear_pointer (&self->current_date, g_free);
- self->current_date = current_date;
-
- gcal_week_header_set_current_date (GCAL_WEEK_HEADER (self->header), current_date);
- gcal_week_grid_set_current_date (GCAL_WEEK_GRID (self->week_grid), current_date);
-}
diff --git a/src/views/gcal-week-view.h b/src/views/gcal-week-view.h
index 49446b8..55d869f 100644
--- a/src/views/gcal-week-view.h
+++ b/src/views/gcal-week-view.h
@@ -40,9 +40,6 @@ void gcal_week_view_set_first_weekday (GcalWeekView
void gcal_week_view_set_use_24h_format (GcalWeekView *self,
gboolean use_24h);
-void gcal_week_view_set_current_date (GcalWeekView *self,
- icaltimetype *current_date);
-
G_END_DECLS
#endif /* __GCAL_WEEK_VIEW_H__ */
diff --git a/src/views/gcal-year-view.c b/src/views/gcal-year-view.c
index d37fbd5..9bfa30f 100644
--- a/src/views/gcal-year-view.c
+++ b/src/views/gcal-year-view.c
@@ -105,9 +105,6 @@ struct _GcalYearView
/* text direction factors */
gint k;
- /* weak ref to current date */
- icaltimetype *current_date;
-
/* date property */
icaltimetype *date;
};
@@ -218,7 +215,17 @@ update_selected_dates_from_button_data (GcalYearView *year_view)
}
else
{
- *(year_view->start_selected_date) = year_view->date ? *(year_view->date) : *(year_view->current_date);
+ g_autofree icaltimetype *current_date;
+ g_autoptr (GDateTime) now;
+
+ now = g_date_time_new_now_local ();
+ current_date = datetime_to_icaltime (now);
+
+ if (year_view->date)
+ *(year_view->start_selected_date) = *year_view->date;
+ else
+ *(year_view->start_selected_date) = *current_date;
+
year_view->start_selected_date->hour = 0;
year_view->start_selected_date->minute = 0;
year_view->start_selected_date->second = 0;
@@ -245,13 +252,17 @@ update_selected_dates_from_button_data (GcalYearView *year_view)
static void
update_no_events_page (GcalYearView *year_view)
{
- gchar *title;
+ g_autoptr (GDateTime) start_selected_date, now;
gboolean has_range;
+ gchar *title;
+
+ now = g_date_time_new_now_local ();
+ start_selected_date = icaltime_to_datetime (year_view->start_selected_date);
has_range = (year_view->start_selected_date->day != year_view->end_selected_date->day ||
year_view->start_selected_date->month != year_view->end_selected_date->month);
- if (icaltime_compare_date (year_view->current_date, year_view->start_selected_date) == 0)
+ if (datetime_compare_date (now, start_selected_date) == 0)
{
title = g_strdup_printf ("%s%s", _("Today"), has_range ? "…" : "");
}
@@ -465,14 +476,19 @@ update_sidebar_headers (GtkListBoxRow *row,
if (before_shift == -1 || before_shift != row_shift)
{
+ g_autoptr (GDateTime) now, dt;
GtkWidget *label;
gchar *label_str;
row_header = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
+ now = g_date_time_new_now_local ();
+
date = *(year_view->start_selected_date);
icaltime_adjust (&date, row_shift, 0, 0, 0);
- if (icaltime_compare_date (&date, year_view->current_date) == 0)
+ dt = icaltime_to_datetime (&date);
+
+ if (datetime_compare_date (dt, now) == 0)
label_str = g_strdup (_("Today"));
else
label_str = g_strdup_printf ("%s %d", gcal_get_month_name (date.month - 1), date.day);
@@ -674,6 +690,8 @@ draw_month_grid (GcalYearView *year_view,
PangoLayout *layout, *slayout;
PangoFontDescription *font_desc, *sfont_desc;
+ g_autoptr (GDateTime) now;
+
GdkRGBA color;
gint layout_width, layout_height, i, j, sw;
gint column, row;
@@ -684,6 +702,8 @@ draw_month_grid (GcalYearView *year_view,
GList *events;
icaltimetype start_date, end_date;
+ now = g_date_time_new_now_local ();
+
cairo_save (cr);
context = gtk_widget_get_style_context (widget);
state_flags = gtk_style_context_get_state (context);
@@ -845,8 +865,9 @@ draw_month_grid (GcalYearView *year_view,
}
}
- if (year_view->date->year == year_view->current_date->year && month_nr + 1 ==
year_view->current_date->month &&
- j == year_view->current_date->day)
+ if (year_view->date->year == g_date_time_get_year (now) &&
+ month_nr + 1 == g_date_time_get_month (now) &&
+ j == g_date_time_get_day_of_month (now))
{
PangoLayout *clayout;
PangoFontDescription *cfont_desc;
@@ -1237,10 +1258,7 @@ add_event_clicked_cb (GcalYearView *year_view,
if (year_view->start_selected_date->day == 0)
{
- start_date = g_date_time_new_local (year_view->current_date->year,
- year_view->current_date->month,
- year_view->current_date->day,
- 0, 0, 0);
+ start_date = g_date_time_new_now_local ();
}
else
{
@@ -1978,14 +1996,3 @@ gcal_year_view_set_use_24h_format (GcalYearView *year_view,
{
year_view->use_24h_format = use_24h_format;
}
-
-void
-gcal_year_view_set_current_date (GcalYearView *year_view,
- icaltimetype *current_date)
-{
- year_view->current_date = current_date;
-
- /* Launches update */
- gtk_widget_queue_draw (GTK_WIDGET (year_view));
- update_sidebar (year_view);
-}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]