From f2f3c519595467d34c4f2c5c199cee62439c7e77 Mon Sep 17 00:00:00 2001 From: David Shea Date: Thu, 14 Nov 2013 10:50:49 -0500 Subject: [PATCH 04/10] Moved CcTimezoneLocation into its own file. Mixing GObject and non-GObject code in tz.c confuses g-ir-scanner and adds a lot non-introspectable identifiers to the .gir file. Added get and set functions for the CcTimezoneLocation properties. --- src/Makefile.am | 15 +- src/cc-timezone-location.c | 393 +++++++++++++++++++++++++++++++++++++++++++++ src/cc-timezone-location.h | 95 +++++++++++ src/cc-timezone-map.c | 53 ++---- src/cc-timezone-map.h | 1 + src/tz.c | 319 +++--------------------------------- src/tz.h | 44 +---- 7 files changed, 533 insertions(+), 387 deletions(-) create mode 100644 src/cc-timezone-location.c create mode 100644 src/cc-timezone-location.h diff --git a/src/Makefile.am b/src/Makefile.am index 05dfb1a..b4461cd 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -63,7 +63,7 @@ AM_CPPFLAGS = \ noinst_PROGRAMS = test-timezone -test_timezone_SOURCES = test-timezone.c tz.c tz.h +test_timezone_SOURCES = test-timezone.c cc-timezone-location.c tz.c tz.h test_timezone_LDADD = $(LIBTIMEZONEMAP_LIBS) -lm test_timezone_CFLAGS = $(LIBTIMEZONEMAP_CFLAGS) @@ -76,12 +76,11 @@ check-local: test-timezone lib_LTLIBRARIES = libtimezonemap.la -libtimezonemap_la_SOURCES = \ - cc-timezone-map.c \ - cc-timezone-map.h \ - timezone-completion.c \ - timezone-completion.h \ - tz.c tz.h +libtimezonemap_GISOURCES = cc-timezone-map.c cc-timezone-map.h \ + cc-timezone-location.c cc-timezone-location.h \ + timezone-completion.c timezone-completion.h +libtimezonemap_NONGISOURCES = tz.c tz.h +libtimezonemap_la_SOURCES = $(libtimezonemap_GISOURCES) $(libtimezonemap_NONGISOURCES) # Specify 'timezonemap' twice: once for package (so we could eventually add # a timezonemap-gtk4 for example), and once for namespacing inside code so @@ -105,7 +104,7 @@ INTROSPECTION_COMPILER_ARGS = --includedir=$(srcdir) INTROSPECTION_GIRS = TimezoneMap-1.0.gir -introspection_sources = $(libtimezonemap_la_SOURCES) +introspection_sources = $(libtimezonemap_GISOURCES) TimezoneMap-1.0.gir: libtimezonemap.la TimezoneMap_1_0_gir_INCLUDES = GObject-2.0 Gtk-3.0 Json-1.0 diff --git a/src/cc-timezone-location.c b/src/cc-timezone-location.c new file mode 100644 index 0000000..3bf4cad --- /dev/null +++ b/src/cc-timezone-location.c @@ -0,0 +1,393 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* Generic timezone utilities. + * + * Copyright (C) 2000-2001 Ximian, Inc. + * + * Authors: Hans Petter Jansson + * + * Largely based on Michael Fulbright's work on Anaconda. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "cc-timezone-location.h" + +G_DEFINE_TYPE (CcTimezoneLocation, cc_timezone_location, G_TYPE_OBJECT) + +#define TIMEZONE_LOCATION_PRIVATE(o) \ + (G_TYPE_INSTANCE_GET_PRIVATE ((o), CC_TYPE_TIMEZONE_LOCATION, CcTimezoneLocationPrivate)) + +struct _CcTimezoneLocationPrivate +{ + gchar *country; + gchar *full_country; + gchar *en_name; + gchar *state; + gdouble latitude; + gdouble longitude; + gchar *zone; + gchar *comment; + + gdouble dist; /* distance to clicked point for comparison */ +}; + +enum { + PROP_0, + PROP_COUNTRY, + PROP_FULL_COUNTRY, + PROP_EN_NAME, + PROP_STATE, + PROP_LATITUDE, + PROP_LONGITUDE, + PROP_ZONE, + PROP_COMMENT, + PROP_DIST, +}; + +static void +cc_timezone_location_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec) +{ + CcTimezoneLocationPrivate *priv = CC_TIMEZONE_LOCATION (object)->priv; + switch (property_id) { + case PROP_COUNTRY: + g_value_set_string (value, priv->country); + break; + case PROP_FULL_COUNTRY: + g_value_set_string (value, priv->full_country); + break; + case PROP_EN_NAME: + g_value_set_string (value, priv->en_name); + break; + case PROP_STATE: + g_value_set_string (value, priv->state); + break; + case PROP_LATITUDE: + g_value_set_double (value, priv->latitude); + break; + case PROP_LONGITUDE: + g_value_set_double (value, priv->longitude); + break; + case PROP_ZONE: + g_value_set_string (value, priv->zone); + break; + case PROP_COMMENT: + g_value_set_string (value, priv->comment); + break; + case PROP_DIST: + g_value_set_double (value, priv->dist); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + } +} + +static void +cc_timezone_location_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) +{ + CcTimezoneLocation *loc = CC_TIMEZONE_LOCATION(object); + switch (property_id) { + case PROP_COUNTRY: + cc_timezone_location_set_country(loc, g_value_get_string(value)); + break; + case PROP_FULL_COUNTRY: + cc_timezone_location_set_full_country(loc, g_value_get_string(value)); + break; + case PROP_EN_NAME: + cc_timezone_location_set_en_name(loc, g_value_get_string(value)); + break; + case PROP_STATE: + cc_timezone_location_set_state(loc, g_value_get_string(value)); + break; + case PROP_LATITUDE: + cc_timezone_location_set_latitude(loc, g_value_get_double(value)); + break; + case PROP_LONGITUDE: + cc_timezone_location_set_longitude(loc, g_value_get_double(value)); + break; + case PROP_ZONE: + cc_timezone_location_set_zone(loc, g_value_get_string(value)); + break; + case PROP_COMMENT: + cc_timezone_location_set_comment(loc, g_value_get_string(value)); + break; + case PROP_DIST: + cc_timezone_location_set_dist(loc, g_value_get_double(value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + } +} + +static void +cc_timezone_location_dispose (GObject *object) +{ + CcTimezoneLocationPrivate *priv = CC_TIMEZONE_LOCATION (object)->priv; + + if (priv->country) + { + g_free (priv->country); + priv->country = NULL; + } + + if (priv->full_country) + { + g_free (priv->full_country); + priv->full_country = NULL; + } + + if (priv->en_name) + { + g_free (priv->en_name); + priv->en_name = NULL; + } + + if (priv->state) + { + g_free (priv->state); + priv->state = NULL; + } + + if (priv->zone) + { + g_free (priv->zone); + priv->zone = NULL; + } + + if (priv->comment) + { + g_free (priv->comment); + priv->comment = NULL; + } + + G_OBJECT_CLASS (cc_timezone_location_parent_class)->dispose (object); +} + +static void +cc_timezone_location_finalize (GObject *object) +{ + CcTimezoneLocationPrivate *priv = CC_TIMEZONE_LOCATION (object)->priv; + G_OBJECT_CLASS (cc_timezone_location_parent_class)->finalize (object); +} + +static void +cc_timezone_location_class_init (CcTimezoneLocationClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + g_type_class_add_private (klass, sizeof (CcTimezoneLocationPrivate)); + + object_class->get_property = cc_timezone_location_get_property; + object_class->set_property = cc_timezone_location_set_property; + object_class->dispose = cc_timezone_location_dispose; + object_class->finalize = cc_timezone_location_finalize; + + g_object_class_install_property(object_class, + PROP_COUNTRY, + g_param_spec_string ("country", + "Country", + "The country for the location", + "", + G_PARAM_READWRITE)); + g_object_class_install_property(object_class, + PROP_FULL_COUNTRY, + g_param_spec_string ("full_country", + "Country (full name)", + "The full country name", + "", + G_PARAM_READWRITE)); + g_object_class_install_property(object_class, + PROP_EN_NAME, + g_param_spec_string ("en_name", + "English Name", + "The name of the location", + "", + G_PARAM_READWRITE)); + g_object_class_install_property(object_class, + PROP_STATE, + g_param_spec_string ("state", + "State", + "The state for the location", + "", + G_PARAM_READWRITE)); + g_object_class_install_property(object_class, + PROP_LATITUDE, + g_param_spec_double ("latitude", + "Latitude", + "The latitude for the location", + -90.0, + 90.0, + 0.0, + G_PARAM_READWRITE)); + g_object_class_install_property(object_class, + PROP_LONGITUDE, + g_param_spec_double ("longitude", + "Longitude", + "The longitude for the location", + -180.0, + 180.0, + 0.0, + G_PARAM_READWRITE)); + g_object_class_install_property(object_class, + PROP_ZONE, + g_param_spec_string ("zone", + "Zone", + "The time zone for the location", + "", + G_PARAM_READWRITE)); + g_object_class_install_property(object_class, + PROP_COMMENT, + g_param_spec_string ("Comment", + "Comment", + "A comment for the location", + "", + G_PARAM_READWRITE)); + g_object_class_install_property(object_class, + PROP_DIST, + g_param_spec_double ("dist", + "Distance", + "The distance for the location", + 0.0, + DBL_MAX, + 0.0, + G_PARAM_READWRITE)); +} + +static void +cc_timezone_location_init (CcTimezoneLocation *self) { + CcTimezoneLocationPrivate *priv; + priv = self->priv = TIMEZONE_LOCATION_PRIVATE (self); +} + +CcTimezoneLocation * +cc_timezone_location_new (void) +{ + return g_object_new (CC_TYPE_TIMEZONE_LOCATION, NULL); +} + +const gchar *cc_timezone_location_get_country(CcTimezoneLocation *loc) +{ + return loc->priv->country; +} + +void cc_timezone_location_set_country(CcTimezoneLocation *loc, const gchar *country) +{ + g_free(loc->priv->country); + loc->priv->country = g_strdup(country); + + g_object_notify(G_OBJECT(loc), "country"); +} + +const gchar *cc_timezone_location_get_full_country(CcTimezoneLocation *loc) +{ + return loc->priv->full_country; +} + +void cc_timezone_location_set_full_country(CcTimezoneLocation *loc, const gchar *full_country) +{ + g_free(loc->priv->full_country); + loc->priv->full_country = g_strdup(full_country); + + g_object_notify(G_OBJECT(loc), "full_country"); +} + +const gchar *cc_timezone_location_get_en_name(CcTimezoneLocation *loc) +{ + return loc->priv->en_name; +} + +void cc_timezone_location_set_en_name(CcTimezoneLocation *loc, const gchar *en_name) +{ + g_free(loc->priv->en_name); + loc->priv->full_country = g_strdup(en_name); + + g_object_notify(G_OBJECT(loc), "en_name"); +} + +const gchar *cc_timezone_location_get_state(CcTimezoneLocation *loc) +{ + return loc->priv->state; +} + +void cc_timezone_location_set_state(CcTimezoneLocation *loc, const gchar *state) +{ + g_free(loc->priv->state); + loc->priv->state = g_strdup(state); + + g_object_notify(G_OBJECT(loc), "state"); +} + +gdouble cc_timezone_location_get_latitude(CcTimezoneLocation *loc) +{ + return loc->priv->latitude; +} + +void cc_timezone_location_set_latitude(CcTimezoneLocation *loc, gdouble lat) +{ + loc->priv->latitude = lat; + g_object_notify(G_OBJECT(loc), "latitude"); +} + +gdouble cc_timezone_location_get_longitude(CcTimezoneLocation *loc) +{ + return loc->priv->longitude; +} + +void cc_timezone_location_set_longitude(CcTimezoneLocation *loc, gdouble lng) +{ + loc->priv->longitude = lng; + g_object_notify(G_OBJECT(loc), "longitude"); +} + +const gchar *cc_timezone_location_get_zone(CcTimezoneLocation *loc) +{ + return loc->priv->zone; +} + +void cc_timezone_location_set_zone(CcTimezoneLocation *loc, const gchar *zone) +{ + g_free(loc->priv->zone); + loc->priv->zone = g_strdup(zone); + + g_object_notify(G_OBJECT(loc), "zone"); +} + +const gchar *cc_timezone_location_get_comment(CcTimezoneLocation *loc) +{ + return loc->priv->comment; +} + +void cc_timezone_location_set_comment(CcTimezoneLocation *loc, const gchar *comment) +{ + g_free(loc->priv->comment); + loc->priv->comment = g_strdup(comment); + + g_object_notify(G_OBJECT(loc), "Comment"); +} + +gdouble cc_timezone_location_get_dist(CcTimezoneLocation *loc) +{ + return loc->priv->dist; +} + +void cc_timezone_location_set_dist(CcTimezoneLocation *loc, gdouble dist) +{ + loc->priv->dist = dist; + g_object_notify(G_OBJECT(loc), "dist"); +} diff --git a/src/cc-timezone-location.h b/src/cc-timezone-location.h new file mode 100644 index 0000000..d322794 --- /dev/null +++ b/src/cc-timezone-location.h @@ -0,0 +1,95 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* Timezone location information + * + * Copyright (C) 2000-2001 Ximian, Inc. + * + * Authors: Hans Petter Jansson + * + * Largely based on Michael Fulbright's work on Anaconda. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef _CC_TIMEZONE_LOCATION_H +#define _CC_TIMEZONE_LOCATION_H + +#include +#include + +G_BEGIN_DECLS + +#define CC_TYPE_TIMEZONE_LOCATION cc_timezone_location_get_type() + +#define CC_TIMEZONE_LOCATION(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST ((obj), \ + CC_TYPE_TIMEZONE_LOCATION, CcTimezoneLocation)) + +#define CC_TIMEZONE_LOCATION_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST ((klass), \ + CC_TYPE_TIMEZONE_LOCATION, CcTimezoneLocationClass)) + +#define CC_IS_TIMEZONE_LOCATION(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \ + CC_TYPE_TIMEZONE_LOCATION)) + +#define CC_IS_TIMEZONE_LOCATION_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE ((klass), \ + CC_TYPE_TIMEZONE_LOCATION)) + +#define CC_TIMEZONE_LOCATION_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS ((obj), \ + CC_TYPE_TIMEZONE_LOCATION, CcTimezoneLocationClass)) + +typedef struct _CcTimezoneLocation CcTimezoneLocation; +typedef struct _CcTimezoneLocationClass CcTimezoneLocationClass; +typedef struct _CcTimezoneLocationPrivate CcTimezoneLocationPrivate; + +struct _CcTimezoneLocation +{ + GObject parent; + CcTimezoneLocationPrivate *priv; +}; + +struct _CcTimezoneLocationClass +{ + GObjectClass parent_class; +}; + +GType cc_timezone_location_get_type (void) G_GNUC_CONST; + +CcTimezoneLocation *cc_timezone_location_new (void); + +const gchar *cc_timezone_location_get_country(CcTimezoneLocation *loc); +void cc_timezone_location_set_country(CcTimezoneLocation *loc, const gchar *country); +const gchar *cc_timezone_location_get_full_country(CcTimezoneLocation *loc); +void cc_timezone_location_set_full_country(CcTimezoneLocation *loc, const gchar *full_country); +const gchar *cc_timezone_location_get_en_name(CcTimezoneLocation *loc); +void cc_timezone_location_set_en_name(CcTimezoneLocation *loc, const gchar *en_name); +const gchar *cc_timezone_location_get_state(CcTimezoneLocation *loc); +void cc_timezone_location_set_state(CcTimezoneLocation *loc, const gchar *state); +gdouble cc_timezone_location_get_latitude(CcTimezoneLocation *loc); +void cc_timezone_location_set_latitude(CcTimezoneLocation *loc, gdouble lat); +gdouble cc_timezone_location_get_longitude(CcTimezoneLocation *loc); +void cc_timezone_location_set_longitude(CcTimezoneLocation *loc, gdouble lng); +const gchar *cc_timezone_location_get_zone(CcTimezoneLocation *loc); +void cc_timezone_location_set_zone(CcTimezoneLocation *loc, const gchar *zone); +const gchar *cc_timezone_location_get_comment(CcTimezoneLocation *loc); +void cc_timezone_location_set_comment(CcTimezoneLocation *loc, const gchar *comment); +gdouble cc_timezone_location_get_dist(CcTimezoneLocation *loc); +void cc_timezone_location_set_dist(CcTimezoneLocation *loc, gdouble dist); + +G_END_DECLS + +#endif /* _CC_TIMEZONE_LOCATION_H */ diff --git a/src/cc-timezone-map.c b/src/cc-timezone-map.c index 60297e4..a782089 100644 --- a/src/cc-timezone-map.c +++ b/src/cc-timezone-map.c @@ -24,6 +24,7 @@ */ #include "cc-timezone-map.h" +#include "cc-timezone-location.h" #include #include "tz.h" @@ -822,16 +823,10 @@ cc_timezone_map_draw (GtkWidget *widget, g_clear_error (&err); } - GValue lat = {0}; - GValue lon = {0}; - g_value_init (&lat, G_TYPE_DOUBLE); - g_value_init (&lon, G_TYPE_DOUBLE); - g_object_get_property(G_OBJECT (priv->location), "latitude", &lat); - g_object_get_property(G_OBJECT (priv->location), "longitude", &lon); - pointx = convert_longtitude_to_x (g_value_get_double(&lon), alloc.width); - pointy = convert_latitude_to_y (g_value_get_double(&lat), alloc.height); - g_value_unset (&lon); - g_value_unset (&lat); + pointx = convert_longtitude_to_x ( + cc_timezone_location_get_longitude(priv->location), alloc.width); + pointy = convert_latitude_to_y ( + cc_timezone_location_get_latitude(priv->location), alloc.height); if (pointy > alloc.height) pointy = alloc.height; @@ -881,17 +876,9 @@ static gint sort_locations (CcTimezoneLocation *a, CcTimezoneLocation *b) { - GValue val_a = {0}; - GValue val_b = {0}; gdouble dist_a, dist_b; - g_value_init (&val_a, G_TYPE_DOUBLE); - g_value_init (&val_b, G_TYPE_DOUBLE); - g_object_get_property(G_OBJECT (a), "dist", &val_a); - g_object_get_property(G_OBJECT (b), "dist", &val_b); - dist_a = g_value_get_double(&val_a); - dist_b = g_value_get_double(&val_b); - g_value_unset (&val_a); - g_value_unset (&val_b); + dist_a = cc_timezone_location_get_dist(a); + dist_b = cc_timezone_location_get_dist(b); if (dist_a > dist_b) return 1; @@ -934,13 +921,6 @@ get_loc_for_xy (GtkWidget * widget, gint x, gint y) GtkAllocation alloc; CcTimezoneLocation* location; - GValue glon = {0}; - GValue glat = {0}; - GValue gdist = {0}; - g_value_init (&glon, G_TYPE_DOUBLE); - g_value_init (&glat, G_TYPE_DOUBLE); - g_value_init (&gdist, G_TYPE_DOUBLE); - rowstride = priv->visible_map_rowstride; pixels = priv->visible_map_pixels; @@ -986,16 +966,13 @@ get_loc_for_xy (GtkWidget * widget, gint x, gint y) gdouble pointx, pointy, dx, dy; CcTimezoneLocation *loc = array->pdata[i]; - g_object_get_property(G_OBJECT (loc), "longitude", &glon); - g_object_get_property(G_OBJECT (loc), "latitude", &glat); - pointx = convert_longtitude_to_x (g_value_get_double(&glon), width); - pointy = convert_latitude_to_y (g_value_get_double(&glat), height); + pointx = convert_longtitude_to_x (cc_timezone_location_get_longitude(loc), width); + pointy = convert_latitude_to_y (cc_timezone_location_get_latitude(loc), height); dx = pointx - x; dy = pointy - y; - g_value_set_double(&gdist, (gdouble) dx * dx + dy * dy); - g_object_set_property(G_OBJECT (loc), "dist", &gdist); + cc_timezone_location_set_dist(loc, (gdouble) dx * dx + dy * dy); priv->distances = g_list_prepend (priv->distances, loc); } priv->distances = g_list_sort (priv->distances, (GCompareFunc) sort_locations); @@ -1004,10 +981,6 @@ get_loc_for_xy (GtkWidget * widget, gint x, gint y) priv->previous_y = y; } - g_value_unset (&glon); - g_value_unset (&glat); - g_value_unset (&gdist); - return location; } @@ -1143,8 +1116,6 @@ cc_timezone_map_set_timezone (CcTimezoneMap *map, GPtrArray *locations; guint i; char *real_tz; - GValue zone = {0}; - g_value_init (&zone, G_TYPE_STRING); real_tz = g_hash_table_lookup (map->priv->alias_db, timezone); @@ -1153,9 +1124,8 @@ cc_timezone_map_set_timezone (CcTimezoneMap *map, for (i = 0; i < locations->len; i++) { CcTimezoneLocation *loc = locations->pdata[i]; - g_object_get_property(G_OBJECT (loc), "zone", &zone); - if (!g_strcmp0 (g_value_get_string(&zone), real_tz ? real_tz : timezone)) + if (!g_strcmp0 (cc_timezone_location_get_zone(loc), real_tz ? real_tz : timezone)) { set_location (map, loc); break; @@ -1163,7 +1133,6 @@ cc_timezone_map_set_timezone (CcTimezoneMap *map, } gtk_widget_queue_draw (GTK_WIDGET (map)); - g_value_unset (&zone); } void diff --git a/src/cc-timezone-map.h b/src/cc-timezone-map.h index ddb6961..15f2e0b 100644 --- a/src/cc-timezone-map.h +++ b/src/cc-timezone-map.h @@ -25,6 +25,7 @@ #include #include "tz.h" +#include "cc-timezone-location.h" G_BEGIN_DECLS diff --git a/src/tz.c b/src/tz.c index 8d83d0a..9393ba2 100644 --- a/src/tz.c +++ b/src/tz.c @@ -43,269 +43,6 @@ static int compare_country_names (const void *a, const void *b); static void sort_locations_by_country (GPtrArray *locations); static gchar * tz_data_file_get (gchar *env, gchar *defaultfile); -G_DEFINE_TYPE (CcTimezoneLocation, cc_timezone_location, G_TYPE_OBJECT) - -#define TIMEZONE_LOCATION_PRIVATE(o) \ - (G_TYPE_INSTANCE_GET_PRIVATE ((o), CC_TYPE_TIMEZONE_LOCATION, CcTimezoneLocationPrivate)) - -struct _CcTimezoneLocationPrivate -{ - gchar *country; - gchar *full_country; - gchar *en_name; - gchar *state; - gdouble latitude; - gdouble longitude; - gchar *zone; - gchar *comment; - - gdouble dist; /* distance to clicked point for comparison */ -}; - -enum { - PROP_0, - PROP_COUNTRY, - PROP_FULL_COUNTRY, - PROP_EN_NAME, - PROP_STATE, - PROP_LATITUDE, - PROP_LONGITUDE, - PROP_ZONE, - PROP_COMMENT, - PROP_DIST, -}; - -static void -cc_timezone_location_get_property (GObject *object, - guint property_id, - GValue *value, - GParamSpec *pspec) -{ - CcTimezoneLocationPrivate *priv = CC_TIMEZONE_LOCATION (object)->priv; - switch (property_id) { - case PROP_COUNTRY: - g_value_set_string (value, priv->country); - break; - case PROP_FULL_COUNTRY: - g_value_set_string (value, priv->full_country); - break; - case PROP_EN_NAME: - g_value_set_string (value, priv->en_name); - break; - case PROP_STATE: - g_value_set_string (value, priv->state); - break; - case PROP_LATITUDE: - g_value_set_double (value, priv->latitude); - break; - case PROP_LONGITUDE: - g_value_set_double (value, priv->longitude); - break; - case PROP_ZONE: - g_value_set_string (value, priv->zone); - break; - case PROP_COMMENT: - g_value_set_string (value, priv->comment); - break; - case PROP_DIST: - g_value_set_double (value, priv->dist); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); - } -} - -static void -cc_timezone_location_set_property (GObject *object, - guint property_id, - const GValue *value, - GParamSpec *pspec) -{ - CcTimezoneLocationPrivate *priv = CC_TIMEZONE_LOCATION (object)->priv; - switch (property_id) { - case PROP_COUNTRY: - g_free(priv->country); - priv->country = g_strdup(g_value_get_string(value)); - break; - case PROP_FULL_COUNTRY: - g_free(priv->full_country); - priv->full_country = g_strdup(g_value_get_string(value)); - break; - case PROP_EN_NAME: - g_free(priv->en_name); - priv->en_name = g_strdup(g_value_get_string(value)); - break; - case PROP_STATE: - g_free(priv->state); - priv->state = g_strdup(g_value_get_string(value)); - break; - case PROP_LATITUDE: - priv->latitude = g_value_get_double(value); - break; - case PROP_LONGITUDE: - priv->longitude = g_value_get_double(value); - break; - case PROP_ZONE: - g_strdup(priv->zone); - priv->zone = g_strdup(g_value_get_string(value)); - break; - case PROP_COMMENT: - g_free(priv->comment); - priv->comment = g_strdup(g_value_get_string(value)); - break; - case PROP_DIST: - priv->dist = g_value_get_double(value); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); - } -} - -static void -cc_timezone_location_dispose (GObject *object) -{ - CcTimezoneLocationPrivate *priv = CC_TIMEZONE_LOCATION (object)->priv; - - if (priv->country) - { - g_free (priv->country); - priv->country = NULL; - } - - if (priv->full_country) - { - g_free (priv->full_country); - priv->full_country = NULL; - } - - if (priv->en_name) - { - g_free (priv->en_name); - priv->en_name = NULL; - } - - if (priv->state) - { - g_free (priv->state); - priv->state = NULL; - } - - if (priv->zone) - { - g_free (priv->zone); - priv->zone = NULL; - } - - if (priv->comment) - { - g_free (priv->comment); - priv->comment = NULL; - } - - G_OBJECT_CLASS (cc_timezone_location_parent_class)->dispose (object); -} - -static void -cc_timezone_location_finalize (GObject *object) -{ - CcTimezoneLocationPrivate *priv = CC_TIMEZONE_LOCATION (object)->priv; - G_OBJECT_CLASS (cc_timezone_location_parent_class)->finalize (object); -} - -static void -cc_timezone_location_class_init (CcTimezoneLocationClass *klass) -{ - GObjectClass *object_class = G_OBJECT_CLASS (klass); - g_type_class_add_private (klass, sizeof (CcTimezoneLocationPrivate)); - - object_class->get_property = cc_timezone_location_get_property; - object_class->set_property = cc_timezone_location_set_property; - object_class->dispose = cc_timezone_location_dispose; - object_class->finalize = cc_timezone_location_finalize; - - g_object_class_install_property(object_class, - PROP_COUNTRY, - g_param_spec_string ("country", - "Country", - "The country for the location", - "", - G_PARAM_READWRITE)); - g_object_class_install_property(object_class, - PROP_FULL_COUNTRY, - g_param_spec_string ("full_country", - "Country (full name)", - "The full country name", - "", - G_PARAM_READWRITE)); - g_object_class_install_property(object_class, - PROP_EN_NAME, - g_param_spec_string ("en_name", - "English Name", - "The name of the location", - "", - G_PARAM_READWRITE)); - g_object_class_install_property(object_class, - PROP_STATE, - g_param_spec_string ("state", - "State", - "The state for the location", - "", - G_PARAM_READWRITE)); - g_object_class_install_property(object_class, - PROP_LATITUDE, - g_param_spec_double ("latitude", - "Latitude", - "The latitude for the location", - -90.0, - 90.0, - 0.0, - G_PARAM_READWRITE)); - g_object_class_install_property(object_class, - PROP_LONGITUDE, - g_param_spec_double ("longitude", - "Longitude", - "The longitude for the location", - -180.0, - 180.0, - 0.0, - G_PARAM_READWRITE)); - g_object_class_install_property(object_class, - PROP_ZONE, - g_param_spec_string ("zone", - "Zone", - "The time zone for the location", - "", - G_PARAM_READWRITE)); - g_object_class_install_property(object_class, - PROP_COMMENT, - g_param_spec_string ("Comment", - "Comment", - "A comment for the location", - "", - G_PARAM_READWRITE)); - g_object_class_install_property(object_class, - PROP_DIST, - g_param_spec_double ("dist", - "Distance", - "The distance for the location", - 0.0, - DBL_MAX, - 0.0, - G_PARAM_READWRITE)); -} - -static void -cc_timezone_location_init (CcTimezoneLocation *self) { - CcTimezoneLocationPrivate *priv; - priv = self->priv = TIMEZONE_LOCATION_PRIVATE (self); -} - -CcTimezoneLocation * -cc_timezone_location_new (void) -{ - return g_object_new (CC_TYPE_TIMEZONE_LOCATION, NULL); -} - void parse_file (const char * filename, const guint ncolumns, GFunc func, @@ -376,25 +113,24 @@ void parse_cities15000 (gpointer parsed_data, CcTimezoneLocation *loc = cc_timezone_location_new (); - loc->priv->country = g_strdup (parsed_data_v[8]); - loc->priv->en_name = g_strdup (parsed_data_v[2]); + cc_timezone_location_set_country(loc, parsed_data_v[8]); + cc_timezone_location_set_en_name(loc, parsed_data_v[2]); - gchar * tmpState = g_strdup_printf ("%s.%s", loc->priv->country, + gchar * tmpState = g_strdup_printf ("%s.%s", + cc_timezone_location_get_country(loc), parsed_data_v[10]); - loc->priv->state = g_strdup ( - (gchar *) g_hash_table_lookup ( + cc_timezone_location_set_state(loc, g_hash_table_lookup( stateHash, tmpState)); g_free (tmpState); - loc->priv->full_country = g_strdup ( - (gchar *) g_hash_table_lookup ( + cc_timezone_location_set_full_country(loc, g_hash_table_lookup( countryHash, - loc->priv->country)); + cc_timezone_location_get_country(loc))); - loc->priv->zone = g_strdup (parsed_data_v[17]); - loc->priv->latitude = g_ascii_strtod(parsed_data_v[4], NULL); - loc->priv->longitude = g_ascii_strtod(parsed_data_v[5], NULL); + cc_timezone_location_set_zone(loc, parsed_data_v[17]); + cc_timezone_location_set_latitude(loc, g_ascii_strtod(parsed_data_v[4], NULL)); + cc_timezone_location_set_longitude(loc, g_ascii_strtod(parsed_data_v[5], NULL)); #ifdef __sun gchar *latstr, *lngstr, *p; @@ -423,7 +159,7 @@ void parse_cities15000 (gpointer parsed_data, g_ptr_array_add (ptr_array, (gpointer) locgrp); } #else - loc->priv->comment = NULL; + cc_timezone_location_set_comment(loc, NULL); #endif g_ptr_array_add (ptr_array, (gpointer) loc); @@ -510,19 +246,6 @@ tz_db_free (TzDB *db) g_free (db); } -static gint -sort_locations (CcTimezoneLocation *a, - CcTimezoneLocation *b) -{ - if (a->priv->dist > b->priv->dist) - return 1; - - if (a->priv->dist < b->priv->dist) - return -1; - - return 0; -} - static gdouble convert_longtitude_to_x (gdouble longitude, gint map_width) { @@ -584,15 +307,17 @@ tz_location_set_locally (CcTimezoneLocation *loc) struct tm *curzone; gboolean is_dst = FALSE; gint correction = 0; + const gchar *zone; g_return_val_if_fail (loc != NULL, 0); - g_return_val_if_fail (loc->priv->zone != NULL, 0); + zone = cc_timezone_location_get_zone(loc); + g_return_val_if_fail (zone != NULL, 0); curtime = time (NULL); curzone = localtime (&curtime); is_dst = curzone->tm_isdst; - setenv ("TZ", loc->priv->zone, 1); + setenv ("TZ", zone, 1); #if 0 curtime = time (NULL); curzone = localtime (&curtime); @@ -614,11 +339,13 @@ tz_info_from_location (CcTimezoneLocation *loc) TzInfo *tzinfo; time_t curtime; struct tm *curzone; + const gchar *zone; g_return_val_if_fail (loc != NULL, NULL); - g_return_val_if_fail (loc->priv->zone != NULL, NULL); + zone = cc_timezone_location_get_zone(loc); + g_return_val_if_fail (zone != NULL, NULL); - setenv ("TZ", loc->priv->zone, 1); + setenv ("TZ", zone, 1); #if 0 tzset (); @@ -701,10 +428,12 @@ convert_pos (gchar *pos, int digits) static int compare_country_names (const void *a, const void *b) { - const CcTimezoneLocation *tza = * (CcTimezoneLocation **) a; - const CcTimezoneLocation *tzb = * (CcTimezoneLocation **) b; + CcTimezoneLocation *tza = * (CcTimezoneLocation **) a; + CcTimezoneLocation *tzb = * (CcTimezoneLocation **) b; + const gchar *zone_a = cc_timezone_location_get_zone(tza); + const gchar *zone_b = cc_timezone_location_get_zone(tzb); - return strcmp (tza->priv->zone, tzb->priv->zone); + return strcmp (zone_a, zone_b); } diff --git a/src/tz.h b/src/tz.h index 005d72f..8eb55d3 100644 --- a/src/tz.h +++ b/src/tz.h @@ -27,7 +27,8 @@ #define _E_TZ_H #include -#include + +#include "cc-timezone-location.h" #ifndef __sun # define TZ_DATA_FILE "/usr/share/libtimezonemap/ui/cities15000.txt" @@ -40,49 +41,8 @@ G_BEGIN_DECLS -#define CC_TYPE_TIMEZONE_LOCATION cc_timezone_location_get_type() - -#define CC_TIMEZONE_LOCATION(obj) \ - (G_TYPE_CHECK_INSTANCE_CAST ((obj), \ - CC_TYPE_TIMEZONE_LOCATION, CcTimezoneLocation)) - -#define CC_TIMEZONE_LOCATION_CLASS(klass) \ - (G_TYPE_CHECK_CLASS_CAST ((klass), \ - CC_TYPE_TIMEZONE_LOCATION, CcTimezoneLocationClass)) - -#define CC_IS_TIMEZONE_LOCATION(obj) \ - (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \ - CC_TYPE_TIMEZONE_LOCATION)) - -#define CC_IS_TIMEZONE_LOCATION_CLASS(klass) \ - (G_TYPE_CHECK_CLASS_TYPE ((klass), \ - CC_TYPE_TIMEZONE_LOCATION)) - -#define CC_TIMEZONE_LOCATION_GET_CLASS(obj) \ - (G_TYPE_INSTANCE_GET_CLASS ((obj), \ - CC_TYPE_TIMEZONE_LOCATION, CcTimezoneLocationClass)) - typedef struct _TzDB TzDB; typedef struct _TzInfo TzInfo; -typedef struct _CcTimezoneLocation CcTimezoneLocation; -typedef struct _CcTimezoneLocationClass CcTimezoneLocationClass; -typedef struct _CcTimezoneLocationPrivate CcTimezoneLocationPrivate; - -struct _CcTimezoneLocation -{ - GObject parent; - CcTimezoneLocationPrivate *priv; -}; - -struct _CcTimezoneLocationClass -{ - GObjectClass parent_class; -}; - -GType cc_timezone_location_get_type (void) G_GNUC_CONST; - -CcTimezoneLocation *cc_timezone_location_new (void); - struct _TzDB { -- 1.8.4.2