From c5b50235085c81a7c6d94e9b1bb689464f08d64f Mon Sep 17 00:00:00 2001 From: Georges Basile Stavracas Neto Date: Wed, 24 Apr 2019 18:13:54 -0300 Subject: [PATCH] application: Move time format to GcalContext It finally found its place under the sun. --- src/gcal-application.c | 47 --------------------------- src/gcal-context.c | 65 ++++++++++++++++++++++++++++++++++++++ src/gcal-context.h | 3 ++ src/gcal-edit-dialog.c | 60 ++++++++++------------------------- src/gcal-edit-dialog.h | 4 --- src/gcal-search-popover.c | 25 +++------------ src/gcal-window.c | 27 ---------------- src/views/gcal-week-view.c | 32 ++++++------------- 8 files changed, 100 insertions(+), 163 deletions(-) diff --git a/src/gcal-application.c b/src/gcal-application.c index 73e1a53a..94a5765d 100644 --- a/src/gcal-application.c +++ b/src/gcal-application.c @@ -24,7 +24,6 @@ #include "css-code.h" #include "gcal-application.h" #include "gcal-debug.h" -#include "gcal-enums.h" #include "gcal-log.h" #include "gcal-shell-search-provider.h" #include "gcal-window.h" @@ -46,9 +45,6 @@ struct _GcalApplication gchar *uuid; icaltimetype *initial_date; - GSettings *desktop_settings; - GcalTimeFormat time_format; - GcalShellSearchProvider *search_provider; GcalContext *context; @@ -115,7 +111,6 @@ enum PROP_0, PROP_CONTEXT, PROP_MANAGER, - PROP_TIME_FORMAT, N_PROPS }; @@ -188,25 +183,6 @@ load_css_provider (GcalApplication *self) gtk_css_provider_load_from_resource (self->provider, "/org/gnome/calendar/theme/Adwaita.css"); } -static void -load_time_format (GcalApplication *self) -{ - g_autofree gchar *clock_format = NULL; - g_autofree gchar *enum_format = NULL; - - clock_format = g_settings_get_string (self->desktop_settings, "clock-format"); - - if (g_strcmp0 (clock_format, "12h") == 0) - self->time_format = GCAL_TIME_FORMAT_12H; - else - self->time_format = GCAL_TIME_FORMAT_24H; - - enum_format = g_enum_to_string (GCAL_TYPE_TIME_FORMAT, self->time_format); - g_debug ("Setting time format to %s", enum_format); - - g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TIME_FORMAT]); -} - /* * GObject overrides @@ -223,7 +199,6 @@ gcal_application_finalize (GObject *object) g_clear_pointer (&self->uuid, g_free); g_clear_object (&self->context); g_clear_object (&self->colors_provider); - g_clear_object (&self->desktop_settings); g_clear_object (&self->provider); g_clear_object (&self->search_provider); @@ -250,10 +225,6 @@ gcal_application_get_property (GObject *object, g_value_set_object (value, gcal_context_get_manager (self->context)); break; - case PROP_TIME_FORMAT: - g_value_set_enum (value, self->time_format); - break; - default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); } @@ -301,11 +272,8 @@ gcal_application_activate (GApplication *application) "application", self, "context", self->context, "active-date", self->initial_date, - "time-format", self->time_format, NULL); - g_object_bind_property (self, "time-format", self->window, "time-format", G_BINDING_DEFAULT); - g_signal_connect (self->window, "destroy", G_CALLBACK (gtk_widget_destroyed), &self->window); gtk_widget_show (self->window); } @@ -347,14 +315,6 @@ gcal_application_startup (GApplication *app) self->colors_provider = gtk_css_provider_new (); - /* Time format */ - self->desktop_settings = g_settings_new ("org.gnome.desktop.interface"); - g_signal_connect_swapped (self->desktop_settings, - "changed::clock-format", - G_CALLBACK (load_time_format), - self); - load_time_format (self); - /* Startup the manager */ gcal_manager_startup (gcal_context_get_manager (self->context)); @@ -513,13 +473,6 @@ gcal_application_class_init (GcalApplicationClass *klass) GCAL_TYPE_MANAGER, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); - properties[PROP_TIME_FORMAT] = g_param_spec_enum ("time-format", - "The time format of the computer", - "The time format of the computer", - GCAL_TYPE_TIME_FORMAT, - GCAL_TIME_FORMAT_24H, - G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); - g_object_class_install_properties (object_class, N_PROPS, properties); } diff --git a/src/gcal-context.c b/src/gcal-context.c index 2129d2f2..4486c17e 100644 --- a/src/gcal-context.c +++ b/src/gcal-context.c @@ -27,10 +27,13 @@ struct _GcalContext { GObject parent; + GSettings *desktop_settings; + GcalClock *clock; GoaClient *goa_client; GcalManager *manager; GSettings *settings; + GcalTimeFormat time_format; GcalWeatherService *weather_service; GcalNightLightMonitor *night_light_monitor; @@ -45,6 +48,7 @@ enum PROP_GOA_CLIENT, PROP_MANAGER, PROP_SETTINGS, + PROP_TIME_FORMAT, PROP_WEATHER_SERVICE, N_PROPS }; @@ -52,6 +56,30 @@ enum static GParamSpec *properties [N_PROPS]; +/* + * Auxiliary methods + */ + +static void +load_time_format (GcalContext *self) +{ + g_autofree gchar *clock_format = NULL; + g_autofree gchar *enum_format = NULL; + + clock_format = g_settings_get_string (self->desktop_settings, "clock-format"); + + if (g_strcmp0 (clock_format, "12h") == 0) + self->time_format = GCAL_TIME_FORMAT_12H; + else + self->time_format = GCAL_TIME_FORMAT_24H; + + enum_format = g_enum_to_string (GCAL_TYPE_TIME_FORMAT, self->time_format); + g_debug ("Setting time format to %s", enum_format); + + g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TIME_FORMAT]); +} + + /* * GObject overrides */ @@ -64,6 +92,7 @@ gcal_context_finalize (GObject *object) gcal_weather_service_stop (self->weather_service); g_clear_object (&self->clock); + g_clear_object (&self->desktop_settings); g_clear_object (&self->goa_client); g_clear_object (&self->manager); g_clear_object (&self->night_light_monitor); @@ -98,6 +127,10 @@ gcal_context_get_property (GObject *object, g_value_set_object (value, self->settings); break; + case PROP_TIME_FORMAT: + g_value_set_enum (value, self->time_format); + break; + case PROP_WEATHER_SERVICE: g_value_set_object (value, self->weather_service); break; @@ -119,6 +152,7 @@ gcal_context_set_property (GObject *object, case PROP_GOA_CLIENT: case PROP_MANAGER: case PROP_SETTINGS: + case PROP_TIME_FORMAT: case PROP_WEATHER_SERVICE: default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -158,6 +192,13 @@ gcal_context_class_init (GcalContextClass *klass) G_TYPE_SETTINGS, G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS); + properties[PROP_TIME_FORMAT] = g_param_spec_enum ("time-format", + "Time format", + "System time format", + GCAL_TYPE_TIME_FORMAT, + GCAL_TIME_FORMAT_24H, + G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS); + properties[PROP_WEATHER_SERVICE] = g_param_spec_object ("weather-service", "Weather service", "Weather service", @@ -177,6 +218,15 @@ gcal_context_init (GcalContext *self) self->weather_service = gcal_weather_service_new (); self->night_light_monitor = gcal_night_light_monitor_new (self); + + /* Time format */ + self->desktop_settings = g_settings_new ("org.gnome.desktop.interface"); + g_signal_connect_object (self->desktop_settings, + "changed::clock-format", + G_CALLBACK (load_time_format), + self, + G_CONNECT_SWAPPED); + load_time_format (self); } /** @@ -253,6 +303,21 @@ gcal_context_get_settings (GcalContext *self) return self->settings; } +/** + * gcal_context_get_time_format: + * + * Retrieves the #GcalTimeFormat from @self. + * + * Returns: a #GcalTimeFormat + */ +GcalTimeFormat +gcal_context_get_time_format (GcalContext *self) +{ + g_return_val_if_fail (GCAL_IS_CONTEXT (self), 0); + + return self->time_format; +} + /** * gcal_context_get_weather_service: * diff --git a/src/gcal-context.h b/src/gcal-context.h index d8f884fe..1925d947 100644 --- a/src/gcal-context.h +++ b/src/gcal-context.h @@ -21,6 +21,7 @@ #pragma once #include "gcal-clock.h" +#include "gcal-enums.h" #include "gcal-manager.h" #include "weather/gcal-weather-service.h" @@ -42,6 +43,8 @@ GcalManager* gcal_context_get_manager (GcalContext GSettings* gcal_context_get_settings (GcalContext *self); +GcalTimeFormat gcal_context_get_time_format (GcalContext *self); + GcalWeatherService* gcal_context_get_weather_service (GcalContext *self); G_END_DECLS diff --git a/src/gcal-edit-dialog.c b/src/gcal-edit-dialog.c index b15432ce..e80612b3 100644 --- a/src/gcal-edit-dialog.c +++ b/src/gcal-edit-dialog.c @@ -108,7 +108,6 @@ struct _GcalEditDialog ESource *selected_source; /* flags */ - GcalTimeFormat time_format; gboolean event_is_new; gboolean recurrence_changed; gboolean setting_event; @@ -142,7 +141,6 @@ enum PROP_0, PROP_CONTEXT, PROP_EVENT, - PROP_TIME_FORMAT, PROP_WRITABLE, N_PROPS }; @@ -1020,6 +1018,17 @@ on_sound_toggle_changed_cb (GtkToggleButton *button, } +static void +on_time_format_changed_cb (GcalEditDialog *self) +{ + GcalTimeFormat time_format; + + time_format = gcal_context_get_time_format (self->context); + + gcal_time_selector_set_time_format (GCAL_TIME_SELECTOR (self->start_time_selector), time_format); + gcal_time_selector_set_time_format (GCAL_TIME_SELECTOR (self->end_time_selector), time_format); +} + static void setup_alarms (GcalEditDialog *self) { @@ -1179,10 +1188,6 @@ gcal_edit_dialog_get_property (GObject *object, g_value_set_object (value, self->context); break; - case PROP_TIME_FORMAT: - g_value_set_enum (value, self->time_format); - break; - case PROP_WRITABLE: g_value_set_boolean (value, self->writable); break; @@ -1208,10 +1213,12 @@ gcal_edit_dialog_set_property (GObject *object, case PROP_CONTEXT: self->context = g_value_dup_object (value); - break; - - case PROP_TIME_FORMAT: - gcal_edit_dialog_set_time_format (self, g_value_get_enum (value)); + g_signal_connect_object (self->context, + "notify::time-format", + G_CALLBACK (on_time_format_changed_cb), + self, + G_CONNECT_SWAPPED); + on_time_format_changed_cb (self); break; case PROP_WRITABLE: @@ -1259,18 +1266,6 @@ gcal_edit_dialog_class_init (GcalEditDialogClass *klass) GCAL_TYPE_MANAGER, G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS); - /** - * GcalEditDialog::time-format: - * - * The time format. - */ - properties[PROP_TIME_FORMAT] = g_param_spec_enum ("time-format", - "Manager of the dialog", - "The manager of the dialog", - GCAL_TYPE_TIME_FORMAT, - GCAL_TIME_FORMAT_24H, - G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS); - /** * GcalEditDialog::writable: * @@ -1364,27 +1359,6 @@ gcal_edit_dialog_new (void) return g_object_new (GCAL_TYPE_EDIT_DIALOG, NULL); } -/** - * gcal_edit_dialog_set_time_format: - * @dialog: a #GcalDialog - * @use_24h_format: %TRUE to use 24h format, %FALSE otherwise - * - * Sets the time format to be used by @dialog. - */ -void -gcal_edit_dialog_set_time_format (GcalEditDialog *self, - GcalTimeFormat time_format) -{ - g_return_if_fail (GCAL_IS_EDIT_DIALOG (self)); - - self->time_format = time_format; - - gcal_time_selector_set_time_format (GCAL_TIME_SELECTOR (self->start_time_selector), self->time_format); - gcal_time_selector_set_time_format (GCAL_TIME_SELECTOR (self->end_time_selector), self->time_format); - - g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TIME_FORMAT]); -} - /** * gcal_edit_dialog_set_event_is_new: * @dialog: a #GcalDialog diff --git a/src/gcal-edit-dialog.h b/src/gcal-edit-dialog.h index 4cfd6347..d4bea553 100644 --- a/src/gcal-edit-dialog.h +++ b/src/gcal-edit-dialog.h @@ -19,7 +19,6 @@ #ifndef __GCAL_EDIT_DIALOG_H__ #define __GCAL_EDIT_DIALOG_H__ -#include "gcal-enums.h" #include "gcal-event.h" #include "gcal-manager.h" @@ -45,9 +44,6 @@ GcalEvent* gcal_edit_dialog_get_event (GcalEditDialog *d void gcal_edit_dialog_set_event (GcalEditDialog *dialog, GcalEvent *event); -void gcal_edit_dialog_set_time_format (GcalEditDialog *dialog, - GcalTimeFormat time_format); - gboolean gcal_edit_dialog_get_recurrence_changed (GcalEditDialog *self); GcalRecurrenceModType gcal_edit_dialog_get_recurrence_mod_type (GcalEditDialog *self); diff --git a/src/gcal-search-popover.c b/src/gcal-search-popover.c index f8d80354..c023a249 100644 --- a/src/gcal-search-popover.c +++ b/src/gcal-search-popover.c @@ -61,7 +61,6 @@ struct _GcalSearchPopover GcalManager *manager; /* weak reference */ /* flags */ - GcalTimeFormat time_format; gboolean subscribed; GcalContext *context; @@ -72,7 +71,7 @@ enum PROP_0, PROP_CONTEXT, PROP_DATE, - PROP_TIME_FORMAT, + N_PROPS, }; enum @@ -296,8 +295,11 @@ make_row_for_event (GcalSearchPopover *self, /* show 'all day' instead of 00:00 */ if (!gcal_event_get_all_day (event)) { + GcalTimeFormat time_format; + + time_format = gcal_context_get_time_format (self->context); text = g_date_time_format (local_datetime, - self->time_format == GCAL_TIME_FORMAT_24H ? "%R" : "%r"); + time_format == GCAL_TIME_FORMAT_24H ? "%R" : "%r"); time_label = gtk_label_new (text); g_free (text); } @@ -593,10 +595,6 @@ gcal_search_popover_set_property (GObject *object, self->date = g_value_dup_boxed (value); break; - case PROP_TIME_FORMAT: - self->time_format = g_value_get_enum (value); - break; - default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; @@ -621,10 +619,6 @@ gcal_search_popover_get_property (GObject *object, g_value_set_boxed (value, self->date); break; - case PROP_TIME_FORMAT: - g_value_set_enum (value, self->time_format); - break; - default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; @@ -700,15 +694,6 @@ gcal_search_popover_class_init (GcalSearchPopoverClass *klass) ICAL_TIME_TYPE, G_PARAM_READWRITE)); - g_object_class_install_property (object_class, - PROP_TIME_FORMAT, - g_param_spec_enum ("time-format", - "The time format", - "The time format", - GCAL_TYPE_TIME_FORMAT, - GCAL_TIME_FORMAT_24H, - G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); - /* bind things for/from the template class */ gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass), "/org/gnome/calendar/search-popover.ui"); diff --git a/src/gcal-window.c b/src/gcal-window.c index 4bcaba19..67e329b1 100644 --- a/src/gcal-window.c +++ b/src/gcal-window.c @@ -157,8 +157,6 @@ struct _GcalWindow gint open_edit_dialog_timeout_id; - GcalTimeFormat time_format; - /* weather management */ GcalWeatherSettings *weather_settings; @@ -183,7 +181,6 @@ enum PROP_ACTIVE_VIEW, PROP_CONTEXT, PROP_NEW_EVENT_MODE, - PROP_TIME_FORMAT, N_PROPS }; @@ -1259,9 +1256,6 @@ gcal_window_constructed (GObject *object) g_object_bind_property (self, "context", self->edit_dialog, "context", G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE); g_object_bind_property (self, "context", self->search_popover, "context", G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE); g_object_bind_property (self->context, "manager", self->quick_add_popover, "manager", G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE); - g_object_bind_property (self, "time-format", self->edit_dialog, "time-format", G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE); - g_object_bind_property (self, "time-format", self->search_popover, "time-format", G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE); - g_object_bind_property (self, "time-format", self->week_view, "time-format", G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE); GCAL_EXIT; } @@ -1326,14 +1320,6 @@ gcal_window_set_property (GObject *object, } break; - case PROP_TIME_FORMAT: - if (self->time_format != g_value_get_enum (value)) - { - self->time_format = g_value_get_enum (value); - g_object_notify_by_pspec (object, properties[PROP_TIME_FORMAT]); - } - break; - default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); } @@ -1367,10 +1353,6 @@ gcal_window_get_property (GObject *object, g_value_set_object (value, self->context); break; - case PROP_TIME_FORMAT: - g_value_set_enum (value, self->time_format); - break; - default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); } @@ -1449,13 +1431,6 @@ gcal_window_class_init (GcalWindowClass *klass) FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); - properties[PROP_TIME_FORMAT] = g_param_spec_enum ("time-format", - "The time format of the computer", - "The time format of the computer", - GCAL_TYPE_TIME_FORMAT, - GCAL_TIME_FORMAT_24H, - G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); - g_object_class_install_properties (object_class, N_PROPS, properties); gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/calendar/window.ui"); @@ -1531,8 +1506,6 @@ gcal_window_init (GcalWindow *self) G_N_ELEMENTS (actions), self); - self->time_format = GCAL_TIME_FORMAT_24H; - gtk_widget_init_template (GTK_WIDGET (self)); self->views[GCAL_WINDOW_VIEW_WEEK] = self->week_view; diff --git a/src/views/gcal-week-view.c b/src/views/gcal-week-view.c index b21ebcf2..3f168c0e 100644 --- a/src/views/gcal-week-view.c +++ b/src/views/gcal-week-view.c @@ -48,8 +48,6 @@ struct _GcalWeekView GtkWidget *scrolled_window; GtkWidget *week_grid; - GcalTimeFormat time_format; - /* property */ icaltimetype *date; GcalContext *context; @@ -70,7 +68,6 @@ enum PROP_0, PROP_DATE, PROP_CONTEXT, - PROP_TIME_FORMAT, NUM_PROPS }; @@ -383,6 +380,7 @@ gcal_week_view_draw_hours (GcalWeekView *self, GtkWidget *widget) { GtkStyleContext *context; + GcalTimeFormat time_format; GtkStateFlags state; GtkBorder padding; GdkRGBA color; @@ -394,6 +392,7 @@ gcal_week_view_draw_hours (GcalWeekView *self, PangoLayout *layout; PangoFontDescription *font_desc; + time_format = gcal_context_get_time_format (self->context); context = gtk_widget_get_style_context (widget); state = gtk_widget_get_state_flags (widget); ltr = gtk_widget_get_direction (widget) != GTK_TEXT_DIR_RTL; @@ -418,7 +417,7 @@ gcal_week_view_draw_hours (GcalWeekView *self, { gchar *hours; - if (self->time_format == GCAL_TIME_FORMAT_24H) + if (time_format == GCAL_TIME_FORMAT_24H) { hours = g_strdup_printf ("%02d:00", i); } @@ -528,12 +527,14 @@ gcal_week_view_set_property (GObject *object, gcal_week_grid_set_context (GCAL_WEEK_GRID (self->week_grid), self->context); gcal_week_header_set_context (GCAL_WEEK_HEADER (self->header), self->context); - g_object_notify (object, "context"); - break; - case PROP_TIME_FORMAT: - self->time_format = g_value_get_enum (value); - gtk_widget_queue_draw (self->hours_bar); + g_signal_connect_object (self->context, + "notify::time-format", + G_CALLBACK (gtk_widget_queue_draw), + self->hours_bar, + G_CONNECT_SWAPPED); + + g_object_notify (object, "context"); break; default: @@ -563,10 +564,6 @@ gcal_week_view_get_property (GObject *object, g_value_set_object (value, self->context); break; - case PROP_TIME_FORMAT: - g_value_set_enum (value, self->time_format); - break; - default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; @@ -589,15 +586,6 @@ gcal_week_view_class_init (GcalWeekViewClass *klass) g_object_class_override_property (object_class, PROP_DATE, "active-date"); g_object_class_override_property (object_class, PROP_CONTEXT, "context"); - g_object_class_install_property (object_class, - PROP_TIME_FORMAT, - g_param_spec_enum ("time-format", - "Time format", - "Time format", - GCAL_TYPE_TIME_FORMAT, - GCAL_TIME_FORMAT_24H, - G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); - gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/calendar/week-view.ui"); gtk_widget_class_bind_template_child (widget_class, GcalWeekView, header);