[gnome-panel/wip/applets/clock: 12/18] clock: move clock-format to ClockLocation



commit 4a94af5b698a4da32c4698e5b7fbfffd019726a6
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Mon Nov 10 05:53:35 2014 +0200

    clock: move clock-format to ClockLocation

 applets/clock/clock-location-tile.c |   71 ++++++++++---------------
 applets/clock/clock-location.c      |   98 +++++++++++++++++++++++++++++++++++
 applets/clock/clock-location.h      |    2 +
 applets/clock/clock.c               |   11 +++--
 4 files changed, 136 insertions(+), 46 deletions(-)
---
diff --git a/applets/clock/clock-location-tile.c b/applets/clock/clock-location-tile.c
index 2df8d38..24ec1dc 100644
--- a/applets/clock/clock-location-tile.c
+++ b/applets/clock/clock-location-tile.c
@@ -41,9 +41,9 @@ struct _ClockLocationTilePrivate
 {
        ClockLocation       *location;
        ClockTime           *time;
-       GDesktopClockFormat  clock_format;
 
        gulong               weather_updated_id;
+       gulong               clock_format_id;
 
        GtkWidget           *time_label;
 
@@ -67,7 +67,6 @@ enum
 {
        PROP_0,
        PROP_LOCATION,
-       PROP_CLOCK_FORMAT,
        N_PROPERTIES
 };
 
@@ -174,28 +173,6 @@ clock_location_tile_enter_or_leave (GtkWidget        *widget,
        return TRUE;
 }
 
-/*
- * Should match enum values in gdesktop-enums.h:
- * https://git.gnome.org/browse/gsettings-desktop-schemas/tree/headers/gdesktop-enums.h
- */
-static GType
-g_desktop_clock_format_get_type (void)
-{
-       static GType etype = 0;
-
-       if (etype == 0) {
-               static const GEnumValue values[] = {
-                       { G_DESKTOP_CLOCK_FORMAT_24H, "G_DESKTOP_CLOCK_FORMAT_24H", "24h" },
-                       { G_DESKTOP_CLOCK_FORMAT_12H, "G_DESKTOP_CLOCK_FORMAT_12H", "12h" },
-                       { 0, NULL, NULL }
-               };
-
-               etype = g_enum_register_static ("GDesktopClockFormat", values);
-       }
-
-       return etype;
-}
-
 static gboolean
 show_weather_tooltip (GtkWidget  *widget,
                       gint        x,
@@ -210,7 +187,7 @@ show_weather_tooltip (GtkWidget  *widget,
 
        return clock_location_setup_weather_tooltip (tile->priv->location,
                                                     tooltip,
-                                                    tile->priv->clock_format);
+                                                    clock_location_get_clock_format (tile->priv->location));
 }
 
 static void
@@ -290,6 +267,7 @@ format_time (ClockLocationTile *tile)
        glong        offset;
        gint         day_of_week;
        gint         day_of_week_local;
+       GDesktopClockFormat  clock_format;
        const gchar *format;
        gchar       *buf;
        glong        hours;
@@ -304,8 +282,10 @@ format_time (ClockLocationTile *tile)
        day_of_week = g_date_time_get_day_of_week (time);
        day_of_week_local = g_date_time_get_day_of_week (time_local);
 
+       clock_format = clock_location_get_clock_format (tile->priv->location);
+
        if (day_of_week != day_of_week_local) {
-               if (tile->priv->clock_format == G_DESKTOP_CLOCK_FORMAT_12H) {
+               if (clock_format == G_DESKTOP_CLOCK_FORMAT_12H) {
                        /* Translators: This is a strftime format string.
                         * It is used to display the time in 12-hours format
                         * (eg, like in the US: 8:10 am), when the local
@@ -323,7 +303,7 @@ format_time (ClockLocationTile *tile)
                        format = _("%H:%M <small>(%A)</small>");
                }
        } else {
-               if (tile->priv->clock_format == G_DESKTOP_CLOCK_FORMAT_12H) {
+               if (clock_format == G_DESKTOP_CLOCK_FORMAT_12H) {
                        /* Translators: This is a strftime format string.
                         * It is used to display the time in 12-hours format
                         * (eg, like in the US: 8:10 am). The %p expands to
@@ -542,6 +522,18 @@ update_time_label (ClockLocationTile *tile)
 }
 
 static void
+clock_location_tile_clock_format_changed (ClockLocation       *location,
+                                          GDesktopClockFormat  clock_format,
+                                          gpointer             user_data)
+{
+       ClockLocationTile *tile;
+
+       tile = CLOCK_LOCATION_TILE (user_data);
+
+       update_time_label (tile);
+}
+
+static void
 clock_location_tile_minute_changed (ClockTime *time,
                                     gint       hour,
                                     gint       minute,
@@ -567,6 +559,10 @@ clock_location_tile_set_location (ClockLocationTile *tile,
        tile->priv->location = g_object_ref (location);
        tile->priv->time = clock_time_new (location);
 
+       tile->priv->clock_format_id =
+               g_signal_connect (tile->priv->location, "notify::clock-format",
+                                 G_CALLBACK (clock_location_tile_clock_format_changed), tile);
+
        g_signal_connect (tile->priv->time, "minute-changed",
                          G_CALLBACK (clock_location_tile_minute_changed), tile);
 }
@@ -614,6 +610,12 @@ clock_location_tile_finalize (GObject *object)
                tile->priv->weather_updated_id = 0;
        }
 
+       if (tile->priv->clock_format_id > 0) {
+               g_signal_handler_disconnect (tile->priv->location,
+                                            tile->priv->clock_format_id);
+               tile->priv->clock_format_id = 0;
+       }
+
        g_clear_object (&tile->priv->time);
        g_clear_object (&tile->priv->location);
 
@@ -636,10 +638,6 @@ clock_location_tile_set_property (GObject      *object,
                        clock_location_tile_set_location (tile,
                                                          g_value_get_object (value));
                        break;
-               case PROP_CLOCK_FORMAT:
-                       tile->priv->clock_format = g_value_get_enum (value);
-                       update_time_label (tile);
-                       break;
                default:
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
                                                           property_id,
@@ -663,9 +661,6 @@ clock_location_tile_get_property (GObject    *object,
                case PROP_LOCATION:
                        g_value_set_object (value, tile->priv->location);
                        break;
-               case PROP_CLOCK_FORMAT:
-                       g_value_set_enum (value, tile->priv->clock_format);
-                       break;
                default:
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
                                                           property_id,
@@ -706,14 +701,6 @@ clock_location_tile_class_init (ClockLocationTileClass *class)
                                     G_PARAM_CONSTRUCT_ONLY |
                                     G_PARAM_READWRITE);
 
-       object_properties[PROP_CLOCK_FORMAT] =
-               g_param_spec_enum ("clock-format",
-                                  "clock-format",
-                                  "clock-format",
-                                  g_desktop_clock_format_get_type (),
-                                  G_DESKTOP_CLOCK_FORMAT_24H,
-                                  G_PARAM_READWRITE);
-
        g_object_class_install_properties (object_class,
                                           N_PROPERTIES,
                                           object_properties);
diff --git a/applets/clock/clock-location.c b/applets/clock/clock-location.c
index 38e75d7..2c964e5 100644
--- a/applets/clock/clock-location.c
+++ b/applets/clock/clock-location.c
@@ -63,6 +63,8 @@ struct _ClockLocationPrivate
 
         SystemTimezone *systz;
 
+       GDesktopClockFormat  clock_format;
+
         gdouble latitude;
         gdouble longitude;
 
@@ -80,12 +82,42 @@ enum
        LAST_SIGNAL
 };
 
+enum
+{
+       PROP_0,
+       PROP_CLOCK_FORMAT,
+       N_PROPERTIES
+};
+
 static guint location_signals[LAST_SIGNAL] = { 0 };
+static GParamSpec *object_properties[N_PROPERTIES] = { NULL, };
 
 static void clock_location_finalize (GObject *);
 static gboolean update_weather_info (gpointer user_data);
 static void setup_weather_updates (ClockLocation *loc);
 
+/*
+ * Should match enum values in gdesktop-enums.h:
+ * https://git.gnome.org/browse/gsettings-desktop-schemas/tree/headers/gdesktop-enums.h
+ */
+static GType
+g_desktop_clock_format_get_type (void)
+{
+       static GType etype = 0;
+
+       if (etype == 0) {
+               static const GEnumValue values[] = {
+                       { G_DESKTOP_CLOCK_FORMAT_24H, "G_DESKTOP_CLOCK_FORMAT_24H", "24h" },
+                       { G_DESKTOP_CLOCK_FORMAT_12H, "G_DESKTOP_CLOCK_FORMAT_12H", "12h" },
+                       { 0, NULL, NULL }
+               };
+
+               etype = g_enum_register_static ("GDesktopClockFormat", values);
+       }
+
+       return etype;
+}
+
 ClockLocation *
 clock_location_new (GWeatherLocation *world,
                    const char       *name,
@@ -125,6 +157,52 @@ clock_location_new (GWeatherLocation *world,
 static ClockLocation *current_location = NULL;
 
 static void
+clock_location_set_property (GObject      *object,
+                             guint         property_id,
+                             const GValue *value,
+                             GParamSpec   *pspec)
+{
+       ClockLocation *location;
+
+       location = CLOCK_LOCATION (object);
+
+       switch (property_id)
+       {
+               case PROP_CLOCK_FORMAT:
+                       location->priv->clock_format = g_value_get_enum (value);
+                       break;
+               default:
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
+                                                          property_id,
+                                                          pspec);
+                       break;
+       }
+}
+
+static void
+clock_location_get_property (GObject    *object,
+                             guint       property_id,
+                             GValue     *value,
+                             GParamSpec *pspec)
+{
+       ClockLocation *location;
+
+       location = CLOCK_LOCATION (object);
+
+       switch (property_id)
+       {
+               case PROP_CLOCK_FORMAT:
+                       g_value_set_enum (value, location->priv->clock_format);
+                       break;
+               default:
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
+                                                          property_id,
+                                                          pspec);
+                       break;
+       }
+}
+
+static void
 clock_location_class_init (ClockLocationClass *class)
 {
        GObjectClass *object_class;
@@ -132,6 +210,8 @@ clock_location_class_init (ClockLocationClass *class)
        object_class = G_OBJECT_CLASS (class);
 
        object_class->finalize = clock_location_finalize;
+       object_class->set_property = clock_location_set_property;
+       object_class->get_property = clock_location_get_property;
 
        location_signals[WEATHER_UPDATED] =
                g_signal_new ("weather-updated",
@@ -157,6 +237,18 @@ clock_location_class_init (ClockLocationClass *class)
                              g_cclosure_marshal_VOID__VOID,
                              G_TYPE_NONE,
                              0);
+
+       object_properties[PROP_CLOCK_FORMAT] =
+               g_param_spec_enum ("clock-format",
+                                  "clock-format",
+                                  "clock-format",
+                                  g_desktop_clock_format_get_type (),
+                                  G_DESKTOP_CLOCK_FORMAT_24H,
+                                  G_PARAM_READWRITE);
+
+       g_object_class_install_properties (object_class,
+                                          N_PROPERTIES,
+                                          object_properties);
 }
 
 static void
@@ -567,6 +659,12 @@ convert_time_to_str (time_t now, GDesktopClockFormat clock_format, const char *t
        return ret;
 }
 
+GDesktopClockFormat
+clock_location_get_clock_format (ClockLocation *location)
+{
+       return location->priv->clock_format;
+}
+
 gboolean
 clock_location_setup_weather_tooltip (ClockLocation       *location,
                                       GtkTooltip          *tooltip,
diff --git a/applets/clock/clock-location.h b/applets/clock/clock-location.h
index 3588a98..9f4e438 100644
--- a/applets/clock/clock-location.h
+++ b/applets/clock/clock-location.h
@@ -102,6 +102,8 @@ GWeatherInfo *clock_location_get_weather_info (ClockLocation *loc);
 
 glong clock_location_get_offset (ClockLocation *loc);
 
+GDesktopClockFormat  clock_location_get_clock_format (ClockLocation *location);
+
 gboolean clock_location_setup_weather_tooltip (ClockLocation       *location,
                                                GtkTooltip          *tip,
                                                GDesktopClockFormat  clock_format);
diff --git a/applets/clock/clock.c b/applets/clock/clock.c
index e7de4b9..8848296 100644
--- a/applets/clock/clock.c
+++ b/applets/clock/clock.c
@@ -674,10 +674,6 @@ create_cities_section (ClockData *cd)
                 g_signal_connect (city, "tile-pressed",
                                   G_CALLBACK (location_tile_pressed_cb), cd);
 
-                g_settings_bind (cd->clock_settings, "clock-format",
-                                 city, "clock-format",
-                                 G_SETTINGS_BIND_GET);
-
                 gtk_box_pack_start (GTK_BOX (cd->cities_section),
                                     GTK_WIDGET (city),
                                     FALSE, FALSE, 0);
@@ -1270,6 +1266,10 @@ load_cities (ClockData *cd)
                                           name, code,
                                           latlon_override, latitude, longitude);
 
+                g_settings_bind (cd->clock_settings, "clock-format",
+                                 loc, "clock-format",
+                                 G_SETTINGS_BIND_GET);
+
                 cd->locations = g_list_prepend (cd->locations, loc);
         }
 
@@ -1505,6 +1505,9 @@ run_prefs_edit_save (GtkButton *button, ClockData *cd)
         }
 
         loc = clock_location_new (cd->world, name, weather_code, TRUE, lat, lon);
+        g_settings_bind (cd->clock_settings, "clock-format",
+                         loc, "clock-format",
+                         G_SETTINGS_BIND_GET);
         /* has the side-effect of setting the current location if
          * there's none and this one can be considered as a current one
          */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]