1 /* gcal-schedule-section.c
2  *
3  * Copyright 2020 Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * SPDX-License-Identifier: GPL-3.0-or-later
19  */
20 
21 #define G_LOG_DOMAIN "GcalScheduleSection"
22 
23 #include "gcal-context.h"
24 #include "gcal-date-selector.h"
25 #include "gcal-debug.h"
26 #include "gcal-event.h"
27 #include "gcal-event-editor-section.h"
28 #include "gcal-recurrence.h"
29 #include "gcal-schedule-section.h"
30 #include "gcal-time-selector.h"
31 #include "gcal-utils.h"
32 
33 #include <glib/gi18n.h>
34 
35 struct _GcalScheduleSection
36 {
37   GtkBox              parent;
38 
39   GtkSwitch          *all_day_switch;
40   GtkWidget          *end_date_selector;
41   GtkWidget          *end_time_selector;
42   GtkLabel           *event_end_label;
43   GtkLabel           *event_start_label;
44   GtkWidget          *number_of_occurrences_spin;
45   GtkWidget          *repeat_combo;
46   GtkWidget          *repeat_duration_combo;
47   GtkWidget          *start_date_selector;
48   GtkWidget          *start_time_selector;
49   GtkWidget          *until_date_selector;
50 
51   GcalContext        *context;
52   GcalEvent          *event;
53 
54   GcalEventEditorFlags flags;
55 };
56 
57 static void          gcal_event_editor_section_iface_init        (GcalEventEditorSectionInterface *iface);
58 
59 G_DEFINE_TYPE_WITH_CODE (GcalScheduleSection, gcal_schedule_section, GTK_TYPE_BOX,
60                          G_IMPLEMENT_INTERFACE (GCAL_TYPE_EVENT_EDITOR_SECTION, gcal_event_editor_section_iface_init))
61 
62 enum
63 {
64   PROP_0,
65   PROP_CONTEXT,
66   N_PROPS
67 };
68 
69 
70 /*
71  * Auxiliary methods
72  */
73 
74 static void
find_best_timezones_for_event(GcalScheduleSection * self,GTimeZone ** out_tz_start,GTimeZone ** out_tz_end)75 find_best_timezones_for_event (GcalScheduleSection  *self,
76                                GTimeZone             **out_tz_start,
77                                GTimeZone             **out_tz_end)
78 {
79   gboolean was_all_day;
80   gboolean all_day;
81   gboolean new_event;
82 
83   /* Update all day */
84   all_day = gtk_switch_get_active (self->all_day_switch);
85   was_all_day = gcal_event_get_all_day (self->event);
86   new_event = self->flags & GCAL_EVENT_EDITOR_FLAG_NEW_EVENT;
87 
88   GCAL_TRACE_MSG ("Finding best timezone with all_day=%d, was_all_day=%d, event_is_new=%d",
89                   all_day,
90                   was_all_day,
91                   new_event);
92 
93   if (!new_event && was_all_day && !all_day)
94     {
95       GCAL_TRACE_MSG ("Using original event timezones");
96 
97       gcal_event_get_original_timezones (self->event, out_tz_start, out_tz_end);
98       return;
99     }
100 
101   if (all_day)
102     {
103       GCAL_TRACE_MSG ("Using UTC timezones");
104 
105       if (out_tz_start)
106         *out_tz_start = g_time_zone_new_utc ();
107 
108       if (out_tz_end)
109         *out_tz_end = g_time_zone_new_utc ();
110     }
111   else
112     {
113       g_autoptr (GTimeZone) tz_start = NULL;
114       g_autoptr (GTimeZone) tz_end = NULL;
115 
116       if (new_event)
117         {
118           GCAL_TRACE_MSG ("Using the local timezone");
119 
120           tz_start = g_time_zone_new_local ();
121           tz_end = g_time_zone_new_local ();
122         }
123       else
124         {
125           GCAL_TRACE_MSG ("Using the current timezones");
126 
127           tz_start = g_time_zone_ref (g_date_time_get_timezone (gcal_event_get_date_start (self->event)));
128           tz_end = g_time_zone_ref (g_date_time_get_timezone (gcal_event_get_date_end (self->event)));
129         }
130 
131       if (out_tz_start)
132         *out_tz_start = g_steal_pointer (&tz_start);
133 
134       if (out_tz_end)
135         *out_tz_end = g_steal_pointer (&tz_end);
136     }
137 }
138 
139 static GDateTime*
return_datetime_for_widgets(GcalScheduleSection * self,GTimeZone * timezone,GcalDateSelector * date_selector,GcalTimeSelector * time_selector)140 return_datetime_for_widgets (GcalScheduleSection *self,
141                              GTimeZone           *timezone,
142                              GcalDateSelector    *date_selector,
143                              GcalTimeSelector    *time_selector)
144 {
145   g_autoptr (GDateTime) date_in_local_tz = NULL;
146   g_autoptr (GDateTime) date_in_best_tz = NULL;
147   GDateTime *date;
148   GDateTime *time;
149   GDateTime *retval;
150   gboolean all_day;
151 
152   all_day = gtk_switch_get_active (self->all_day_switch);
153   date = gcal_date_selector_get_date (date_selector);
154   time = gcal_time_selector_get_time (time_selector);
155 
156   date_in_local_tz = g_date_time_new_local (g_date_time_get_year (date),
157                                             g_date_time_get_month (date),
158                                             g_date_time_get_day_of_month (date),
159                                             all_day ? 0 : g_date_time_get_hour (time),
160                                             all_day ? 0 : g_date_time_get_minute (time),
161                                             0);
162 
163   if (all_day)
164     date_in_best_tz = g_date_time_ref (date_in_local_tz);
165   else
166     date_in_best_tz = g_date_time_to_timezone (date_in_local_tz, timezone);
167 
168   retval = g_date_time_new (timezone,
169                             g_date_time_get_year (date_in_best_tz),
170                             g_date_time_get_month (date_in_best_tz),
171                             g_date_time_get_day_of_month (date_in_best_tz),
172                             all_day ? 0 : g_date_time_get_hour (date_in_best_tz),
173                             all_day ? 0 : g_date_time_get_minute (date_in_best_tz),
174                             0);
175 
176   return retval;
177 }
178 
179 static GDateTime*
get_date_start(GcalScheduleSection * self)180 get_date_start (GcalScheduleSection *self)
181 {
182   g_autoptr (GTimeZone) start_tz = NULL;
183 
184   find_best_timezones_for_event (self, &start_tz, NULL);
185 
186   return return_datetime_for_widgets (self,
187                                       start_tz,
188                                       GCAL_DATE_SELECTOR (self->start_date_selector),
189                                       GCAL_TIME_SELECTOR (self->start_time_selector));
190 }
191 
192 static GDateTime*
get_date_end(GcalScheduleSection * self)193 get_date_end (GcalScheduleSection *self)
194 {
195   g_autoptr (GTimeZone) end_tz = NULL;
196 
197   find_best_timezones_for_event (self, NULL, &end_tz);
198 
199   return return_datetime_for_widgets (self,
200                                       end_tz,
201                                       GCAL_DATE_SELECTOR (self->end_date_selector),
202                                       GCAL_TIME_SELECTOR (self->end_time_selector));
203 }
204 
205 static void
remove_recurrence_properties(GcalEvent * event)206 remove_recurrence_properties (GcalEvent *event)
207 {
208   ECalComponent *comp;
209 
210   comp = gcal_event_get_component (event);
211 
212   e_cal_component_set_recurid (comp, NULL);
213   e_cal_component_set_rrules (comp, NULL);
214   e_cal_component_commit_sequence (comp);
215 }
216 
217 static gchar*
format_datetime_for_display(GDateTime * date,GcalTimeFormat format,gboolean all_day)218 format_datetime_for_display (GDateTime      *date,
219                              GcalTimeFormat  format,
220                              gboolean        all_day)
221 {
222   g_autofree gchar *formatted_date = NULL;
223   g_autoptr (GDateTime) local_dt = NULL;
224   g_autoptr (GDateTime) now = NULL;
225   GString *string;
226   gint days_diff;
227 
228   string = g_string_new ("");
229 
230   now = g_date_time_new_now_local ();
231   local_dt = all_day ? g_date_time_ref (date) : g_date_time_to_local (date);
232   days_diff = gcal_date_time_compare_date (local_dt, now);
233 
234   switch (days_diff)
235     {
236     case -7:
237     case -6:
238     case -5:
239     case -4:
240     case -3:
241     case -2:
242       /* Translators: %A is the weekday name (e.g. Sunday, Monday, etc) */
243       formatted_date = g_date_time_format (local_dt, _("Last %A"));
244       break;
245 
246     case -1:
247       formatted_date = g_strdup (_("Yesterday"));
248       break;
249 
250     case 0:
251       formatted_date = g_strdup (_("Today"));
252       break;
253 
254     case 1:
255       formatted_date = g_strdup (_("Tomorrow"));
256       break;
257 
258     case 2:
259     case 3:
260     case 4:
261     case 5:
262     case 6:
263     case 7:
264       /* Translators: %A is the weekday name (e.g. Sunday, Monday, etc) */
265       formatted_date = g_date_time_format (local_dt, _("This %A"));
266       break;
267 
268     default:
269       formatted_date = g_date_time_format (local_dt, "%x");
270       break;
271     }
272 
273   if (!all_day)
274     {
275       g_autofree gchar *formatted_time = NULL;
276 
277       switch (format)
278         {
279         case GCAL_TIME_FORMAT_12H:
280           formatted_time = g_date_time_format (local_dt, "%I:%M %P");
281           break;
282 
283         case GCAL_TIME_FORMAT_24H:
284           formatted_time = g_date_time_format (local_dt, "%R");
285           break;
286 
287         default:
288           g_assert_not_reached ();
289         }
290 
291       /*
292        * Translators: %1$s is the formatted date (e.g. Today, Sunday, or even 2019-10-11) and %2$s is the
293        * formatted time (e.g. 03:14 PM, or 21:29)
294        */
295       g_string_printf (string, _("%1$s, %2$s"), formatted_date, formatted_time);
296     }
297   else
298     {
299       g_string_printf (string, "%s", formatted_date);
300     }
301 
302   return g_string_free (string, FALSE);
303 }
304 
305 static void
update_date_labels(GcalScheduleSection * self)306 update_date_labels (GcalScheduleSection *self)
307 {
308   g_autofree gchar *start_label = NULL;
309   g_autofree gchar *end_label = NULL;
310   g_autoptr (GDateTime) start = NULL;
311   g_autoptr (GDateTime) end = NULL;
312   GcalTimeFormat time_format;
313   gboolean all_day;
314 
315   time_format = gcal_context_get_time_format (self->context);
316 
317   all_day = gtk_switch_get_active (self->all_day_switch);
318   start = get_date_start (self);
319   end = get_date_end (self);
320   start_label = format_datetime_for_display (start, time_format, all_day);
321   end_label = format_datetime_for_display (end, time_format, all_day);
322 
323   gtk_label_set_label (self->event_start_label, start_label);
324   gtk_label_set_label (self->event_end_label, end_label);
325 }
326 
327 static void
sync_datetimes(GcalScheduleSection * self,GParamSpec * pspec,GtkWidget * widget)328 sync_datetimes (GcalScheduleSection *self,
329                 GParamSpec          *pspec,
330                 GtkWidget           *widget)
331 {
332   GDateTime *start, *end, *start_local, *end_local, *new_date;
333   GtkWidget *date_widget, *time_widget;
334   gboolean is_start, is_all_day;
335 
336   GCAL_ENTRY;
337 
338   is_start = (widget == self->start_time_selector || widget == self->start_date_selector);
339   is_all_day = gtk_switch_get_active (self->all_day_switch);
340   start = get_date_start (self);
341   end = get_date_end (self);
342 
343   /* The date is valid, no need to update the fields */
344   if (g_date_time_compare (end, start) >= 0)
345     GCAL_GOTO (out);
346 
347   start_local = g_date_time_to_local (start);
348   end_local = g_date_time_to_local (end);
349 
350   /*
351    * If the user is changing the start date or time, we change the end
352    * date or time (and vice versa).
353    */
354   if (is_start)
355     {
356       new_date = is_all_day ? g_date_time_add_hours (start, 0) : g_date_time_add_hours (start_local, 1);
357 
358       date_widget = self->end_date_selector;
359       time_widget = self->end_time_selector;
360     }
361   else
362     {
363       new_date = is_all_day ? g_date_time_add_hours (end, 0) : g_date_time_add_hours (end_local, -1);
364 
365       date_widget = self->start_date_selector;
366       time_widget = self->start_time_selector;
367     }
368 
369   g_signal_handlers_block_by_func (date_widget, sync_datetimes, self);
370   g_signal_handlers_block_by_func (time_widget, sync_datetimes, self);
371 
372   gcal_date_selector_set_date (GCAL_DATE_SELECTOR (date_widget), new_date);
373   gcal_time_selector_set_time (GCAL_TIME_SELECTOR (time_widget), new_date);
374 
375   g_signal_handlers_unblock_by_func (date_widget, sync_datetimes, self);
376   g_signal_handlers_unblock_by_func (time_widget, sync_datetimes, self);
377 
378   g_clear_pointer (&start_local, g_date_time_unref);
379   g_clear_pointer (&end_local, g_date_time_unref);
380   g_clear_pointer (&new_date, g_date_time_unref);
381 
382 out:
383   g_clear_pointer (&start, g_date_time_unref);
384   g_clear_pointer (&end, g_date_time_unref);
385 
386   update_date_labels (self);
387 
388   GCAL_EXIT;
389 }
390 
391 
392 /*
393  * Callbacks
394  */
395 
396 static void
on_repeat_duration_changed_cb(GtkComboBox * widget,GcalScheduleSection * self)397 on_repeat_duration_changed_cb (GtkComboBox         *widget,
398                                GcalScheduleSection *self)
399 {
400   gint active = gtk_combo_box_get_active (widget);
401 
402   gtk_widget_set_visible (self->number_of_occurrences_spin, active == 1);
403   gtk_widget_set_visible (self->until_date_selector, active == 2);
404 }
405 
406 static void
on_repeat_type_changed_cb(GtkComboBox * combobox,GcalScheduleSection * self)407 on_repeat_type_changed_cb (GtkComboBox         *combobox,
408                            GcalScheduleSection *self)
409 {
410   GcalRecurrenceFrequency frequency;
411 
412   frequency = gtk_combo_box_get_active (combobox);
413 
414   gtk_combo_box_set_active (GTK_COMBO_BOX (self->repeat_duration_combo), GCAL_RECURRENCE_FOREVER);
415   gtk_widget_set_visible (self->repeat_duration_combo, frequency != GCAL_RECURRENCE_NO_REPEAT);
416 }
417 
418 static void
on_all_day_switch_active_changed_cb(GtkSwitch * all_day_switch,GParamSpec * pspec,GcalScheduleSection * self)419 on_all_day_switch_active_changed_cb (GtkSwitch           *all_day_switch,
420                                      GParamSpec          *pspec,
421                                      GcalScheduleSection *self)
422 {
423   gboolean active = gtk_switch_get_active (all_day_switch);
424 
425   gtk_widget_set_sensitive (self->start_time_selector, !active);
426   gtk_widget_set_sensitive (self->end_time_selector, !active);
427 
428   update_date_labels (self);
429 }
430 
431 static void
on_time_format_changed_cb(GcalScheduleSection * self)432 on_time_format_changed_cb (GcalScheduleSection *self)
433 {
434   GcalTimeFormat time_format;
435 
436   time_format = gcal_context_get_time_format (self->context);
437 
438   gcal_time_selector_set_time_format (GCAL_TIME_SELECTOR (self->start_time_selector), time_format);
439   gcal_time_selector_set_time_format (GCAL_TIME_SELECTOR (self->end_time_selector), time_format);
440 }
441 
442 
443 /*
444  * GcalEventEditorSection interface
445  */
446 
447 static void
gcal_schedule_section_set_event(GcalEventEditorSection * section,GcalEvent * event,GcalEventEditorFlags flags)448 gcal_schedule_section_set_event (GcalEventEditorSection *section,
449                                  GcalEvent              *event,
450                                  GcalEventEditorFlags    flags)
451 {
452   g_autoptr (GDateTime) date_start = NULL;
453   g_autoptr (GDateTime) date_end = NULL;
454   GcalRecurrenceLimitType limit_type;
455   GcalRecurrenceFrequency frequency;
456   GcalScheduleSection *self;
457   GcalRecurrence *recur;
458   gboolean all_day;
459 
460   GCAL_ENTRY;
461 
462   self = GCAL_SCHEDULE_SECTION (section);
463 
464   g_set_object (&self->event, event);
465   self->flags = flags;
466 
467   if (!event)
468     GCAL_RETURN ();
469 
470   /* Recurrences */
471   recur = gcal_event_get_recurrence (event);
472   frequency = recur ? recur->frequency : GCAL_RECURRENCE_NO_REPEAT;
473   limit_type = recur ? recur->limit_type : GCAL_RECURRENCE_FOREVER;
474 
475   gtk_combo_box_set_active (GTK_COMBO_BOX (self->repeat_combo), frequency);
476   gtk_combo_box_set_active (GTK_COMBO_BOX (self->repeat_duration_combo), limit_type);
477 
478   if (frequency == GCAL_RECURRENCE_NO_REPEAT)
479     {
480       gtk_widget_hide (self->repeat_duration_combo);
481     }
482   else
483     {
484       gtk_widget_show (self->repeat_duration_combo);
485       gtk_widget_show (self->repeat_duration_combo);
486     }
487 
488   switch (limit_type)
489     {
490     case GCAL_RECURRENCE_COUNT:
491       gtk_spin_button_set_value (GTK_SPIN_BUTTON (self->number_of_occurrences_spin), recur->limit.count);
492       break;
493 
494     case GCAL_RECURRENCE_UNTIL:
495       gcal_date_selector_set_date (GCAL_DATE_SELECTOR (self->until_date_selector), recur->limit.until);
496       break;
497 
498     case GCAL_RECURRENCE_FOREVER:
499       gtk_spin_button_set_value (GTK_SPIN_BUTTON (self->number_of_occurrences_spin), 0);
500       break;
501     }
502 
503   all_day = gcal_event_get_all_day (event);
504 
505   /* retrieve start and end dates */
506   date_start = gcal_event_get_date_start (event);
507   date_start = all_day ? g_date_time_ref (date_start) : g_date_time_to_local (date_start);
508 
509   date_end = gcal_event_get_date_end (event);
510   /*
511    * This is subtracting what has been added in action_button_clicked ().
512    * See bug 769300.
513    */
514   date_end = all_day ? g_date_time_add_days (date_end, -1) : g_date_time_to_local (date_end);
515 
516   /* date */
517   g_signal_handlers_block_by_func (self->end_date_selector, sync_datetimes, self);
518   g_signal_handlers_block_by_func (self->start_date_selector, sync_datetimes, self);
519 
520   gcal_date_selector_set_date (GCAL_DATE_SELECTOR (self->start_date_selector), date_start);
521   gcal_date_selector_set_date (GCAL_DATE_SELECTOR (self->end_date_selector), date_end);
522 
523   g_signal_handlers_unblock_by_func (self->start_date_selector, sync_datetimes, self);
524   g_signal_handlers_unblock_by_func (self->end_date_selector, sync_datetimes, self);
525 
526   /* time */
527   g_signal_handlers_block_by_func (self->end_time_selector, sync_datetimes, self);
528   g_signal_handlers_block_by_func (self->start_time_selector, sync_datetimes, self);
529 
530   gcal_time_selector_set_time (GCAL_TIME_SELECTOR (self->start_time_selector), date_start);
531   gcal_time_selector_set_time (GCAL_TIME_SELECTOR (self->end_time_selector), date_end);
532 
533   g_signal_handlers_unblock_by_func (self->start_time_selector, sync_datetimes, self);
534   g_signal_handlers_unblock_by_func (self->end_time_selector, sync_datetimes, self);
535 
536   /* all_day  */
537   gtk_switch_set_active (self->all_day_switch, all_day);
538 
539   update_date_labels (self);
540 
541   GCAL_EXIT;
542 }
543 
544 static void
gcal_schedule_section_apply(GcalEventEditorSection * section)545 gcal_schedule_section_apply (GcalEventEditorSection *section)
546 {
547   g_autoptr (GDateTime) start_date = NULL;
548   g_autoptr (GDateTime) end_date = NULL;
549   GcalRecurrenceFrequency freq;
550   GcalScheduleSection *self;
551   GcalRecurrence *old_recur;
552   gboolean was_all_day;
553   gboolean all_day;
554 
555   GCAL_ENTRY;
556 
557   self = GCAL_SCHEDULE_SECTION (section);
558   all_day = gtk_switch_get_active (self->all_day_switch);
559   was_all_day = gcal_event_get_all_day (self->event);
560 
561   if (!was_all_day && all_day)
562     gcal_event_save_original_timezones (self->event);
563 
564   /*
565    * Update start & end dates. The dates are already translated to the current
566    * timezone (unless the event used to be all day, but no longer is).
567    */
568   start_date = get_date_start (self);
569   end_date = get_date_end (self);
570 
571 #ifdef GCAL_ENABLE_TRACE
572     {
573       g_autofree gchar *start_dt_string = g_date_time_format (start_date, "%x %X %z");
574       g_autofree gchar *end_dt_string = g_date_time_format (end_date, "%x %X %z");
575 
576       g_debug ("New start date: %s", start_dt_string);
577       g_debug ("New end date: %s", end_dt_string);
578     }
579 #endif
580 
581   gcal_event_set_all_day (self->event, all_day);
582 
583   /*
584    * The end date for multi-day events is exclusive, so we bump it by a day.
585    * This fixes the discrepancy between the end day of the event and how it
586    * is displayed in the month view. See bug 769300.
587    */
588   if (all_day)
589     {
590       GDateTime *fake_end_date = g_date_time_add_days (end_date, 1);
591 
592       g_clear_pointer (&end_date, g_date_time_unref);
593       end_date = fake_end_date;
594     }
595   else if (!all_day && was_all_day)
596     {
597       /* When an all day event is changed to be not an all day event, we
598        * need to correct for the fact that the event's timezone was until
599        * now set to UTC. That means we need to change the timezone to
600        * localtime now, or else it will be saved incorrectly.
601        */
602       GDateTime *localtime_date;
603 
604       localtime_date = g_date_time_to_local (start_date);
605       g_clear_pointer (&start_date, g_date_time_unref);
606       start_date = localtime_date;
607 
608       localtime_date = g_date_time_to_local (end_date);
609       g_clear_pointer (&end_date, g_date_time_unref);
610       end_date = localtime_date;
611     }
612 
613   gcal_event_set_date_start (self->event, start_date);
614   gcal_event_set_date_end (self->event, end_date);
615 
616   /* Check Repeat popover and set recurrence-rules accordingly */
617   old_recur = gcal_event_get_recurrence (self->event);
618   freq = gtk_combo_box_get_active (GTK_COMBO_BOX (self->repeat_combo));
619 
620   if (freq != GCAL_RECURRENCE_NO_REPEAT)
621     {
622       GcalRecurrence *recur;
623 
624       recur = gcal_recurrence_new ();
625       recur->frequency = freq;
626       recur->limit_type = gtk_combo_box_get_active (GTK_COMBO_BOX (self->repeat_duration_combo));
627 
628       if (recur->limit_type == GCAL_RECURRENCE_UNTIL)
629         recur->limit.until = gcal_date_selector_get_date (GCAL_DATE_SELECTOR (self->until_date_selector));
630       else if (recur->limit_type == GCAL_RECURRENCE_COUNT)
631         recur->limit.count = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (self->number_of_occurrences_spin));
632 
633       /* Only apply the new recurrence if it's different from the old one */
634       if (!gcal_recurrence_is_equal (old_recur, recur))
635         {
636           /* Remove the previous recurrence... */
637           remove_recurrence_properties (self->event);
638 
639           /* ... and set the new one */
640           gcal_event_set_recurrence (self->event, recur);
641         }
642 
643       g_clear_pointer (&recur, gcal_recurrence_unref);
644     }
645   else
646     {
647       /* When NO_REPEAT is set, make sure to remove the old recurrent */
648       remove_recurrence_properties (self->event);
649     }
650 
651   GCAL_EXIT;
652 }
653 
654 static void
gcal_event_editor_section_iface_init(GcalEventEditorSectionInterface * iface)655 gcal_event_editor_section_iface_init (GcalEventEditorSectionInterface *iface)
656 {
657   iface->set_event = gcal_schedule_section_set_event;
658   iface->apply = gcal_schedule_section_apply;
659 }
660 
661 
662 /*
663  * GObject overrides
664  */
665 
666 static void
gcal_schedule_section_finalize(GObject * object)667 gcal_schedule_section_finalize (GObject *object)
668 {
669   GcalScheduleSection *self = (GcalScheduleSection *)object;
670 
671   g_clear_object (&self->context);
672   g_clear_object (&self->event);
673 
674   G_OBJECT_CLASS (gcal_schedule_section_parent_class)->finalize (object);
675 }
676 
677 static void
gcal_schedule_section_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)678 gcal_schedule_section_get_property (GObject    *object,
679                                     guint       prop_id,
680                                     GValue     *value,
681                                     GParamSpec *pspec)
682 {
683   GcalScheduleSection *self = GCAL_SCHEDULE_SECTION (object);
684 
685   switch (prop_id)
686     {
687     case PROP_CONTEXT:
688       g_value_set_object (value, self->context);
689       break;
690 
691     default:
692       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
693     }
694 }
695 
696 static void
gcal_schedule_section_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)697 gcal_schedule_section_set_property (GObject      *object,
698                                     guint         prop_id,
699                                     const GValue *value,
700                                     GParamSpec   *pspec)
701 {
702   GcalScheduleSection *self = GCAL_SCHEDULE_SECTION (object);
703 
704   switch (prop_id)
705     {
706     case PROP_CONTEXT:
707       g_assert (self->context == NULL);
708       self->context = g_value_dup_object (value);
709       g_signal_connect_object (self->context,
710                                "notify::time-format",
711                                G_CALLBACK (on_time_format_changed_cb),
712                                self,
713                                G_CONNECT_SWAPPED);
714       on_time_format_changed_cb (self);
715       break;
716 
717     default:
718       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
719     }
720 }
721 
722 static void
gcal_schedule_section_class_init(GcalScheduleSectionClass * klass)723 gcal_schedule_section_class_init (GcalScheduleSectionClass *klass)
724 {
725   GObjectClass *object_class = G_OBJECT_CLASS (klass);
726   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
727 
728   object_class->finalize = gcal_schedule_section_finalize;
729   object_class->get_property = gcal_schedule_section_get_property;
730   object_class->set_property = gcal_schedule_section_set_property;
731 
732   g_object_class_override_property (object_class, PROP_CONTEXT, "context");
733 
734   g_type_ensure (GCAL_TYPE_DATE_SELECTOR);
735   g_type_ensure (GCAL_TYPE_TIME_SELECTOR);
736 
737   gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/calendar/ui/event-editor/gcal-schedule-section.ui");
738 
739   gtk_widget_class_bind_template_child (widget_class, GcalScheduleSection, all_day_switch);
740   gtk_widget_class_bind_template_child (widget_class, GcalScheduleSection, start_time_selector);
741   gtk_widget_class_bind_template_child (widget_class, GcalScheduleSection, start_date_selector);
742   gtk_widget_class_bind_template_child (widget_class, GcalScheduleSection, end_time_selector);
743   gtk_widget_class_bind_template_child (widget_class, GcalScheduleSection, end_date_selector);
744   gtk_widget_class_bind_template_child (widget_class, GcalScheduleSection, event_start_label);
745   gtk_widget_class_bind_template_child (widget_class, GcalScheduleSection, event_end_label);
746   gtk_widget_class_bind_template_child (widget_class, GcalScheduleSection, number_of_occurrences_spin);
747   gtk_widget_class_bind_template_child (widget_class, GcalScheduleSection, repeat_combo);
748   gtk_widget_class_bind_template_child (widget_class, GcalScheduleSection, repeat_duration_combo);
749   gtk_widget_class_bind_template_child (widget_class, GcalScheduleSection, until_date_selector);
750 
751   gtk_widget_class_bind_template_callback (widget_class, on_all_day_switch_active_changed_cb);
752   gtk_widget_class_bind_template_callback (widget_class, on_repeat_duration_changed_cb);
753   gtk_widget_class_bind_template_callback (widget_class, on_repeat_type_changed_cb);
754   gtk_widget_class_bind_template_callback (widget_class, sync_datetimes);
755 }
756 
757 static void
gcal_schedule_section_init(GcalScheduleSection * self)758 gcal_schedule_section_init (GcalScheduleSection *self)
759 {
760   gtk_widget_init_template (GTK_WIDGET (self));
761 }
762