[libgweather] Add gweather_location_new_detached()



commit bac22d11eebc33fab539c0ccfa3baf502198ecdb
Author: Giovanni Campagna <scampa giovanni gmail com>
Date:   Sun Feb 15 20:59:01 2015 -0800

    Add gweather_location_new_detached()
    
    Constructs a new detached location, given its parameters (it
    actually tries to find if the database already contains the
    exact location, and it will fallback to a detached location
    otherwise). Useful for integration with geoclue and similar.

 doc/libgweather-sections.txt    |    1 +
 libgweather/gweather-location.c |   65 ++++++++++++++++++++++++++++++++++-----
 libgweather/gweather-location.h |    5 +++
 3 files changed, 63 insertions(+), 8 deletions(-)
---
diff --git a/doc/libgweather-sections.txt b/doc/libgweather-sections.txt
index 713c056..f5da5c1 100644
--- a/doc/libgweather-sections.txt
+++ b/doc/libgweather-sections.txt
@@ -47,6 +47,7 @@ GWeatherLocationLevel
 gweather_location_find_nearest_city
 gweather_location_get_world
 gweather_location_new_world
+gweather_location_new_detached
 gweather_location_ref
 gweather_location_unref
 
diff --git a/libgweather/gweather-location.c b/libgweather/gweather-location.c
index 9553883..43387fb 100644
--- a/libgweather/gweather-location.c
+++ b/libgweather/gweather-location.c
@@ -1254,13 +1254,20 @@ _gweather_location_new_detached (GWeatherLocation *nearest_station,
     self = g_slice_new0 (GWeatherLocation);
     self->ref_count = 1;
     self->level = GWEATHER_LOCATION_DETACHED;
-    self->english_name = g_strdup (name);
-    self->local_name = g_strdup (name);
-
-    normalized = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
-    self->english_sort_name = g_utf8_casefold (normalized, -1);
-    self->local_sort_name = g_strdup (self->english_sort_name);
-    g_free (normalized);
+    if (name != NULL) {
+       self->english_name = g_strdup (name);
+       self->local_name = g_strdup (name);
+
+       normalized = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
+       self->english_sort_name = g_utf8_casefold (normalized, -1);
+       self->local_sort_name = g_strdup (self->english_sort_name);
+       g_free (normalized);
+    } else if (nearest_station) {
+       self->english_name = g_strdup (nearest_station->english_name);
+       self->local_name = g_strdup (nearest_station->local_name);
+       self->english_sort_name = g_strdup (nearest_station->english_sort_name);
+       self->local_sort_name = g_strdup (nearest_station->local_sort_name);
+    }
 
     self->parent = nearest_station;
     self->children = NULL;
@@ -1339,7 +1346,9 @@ gweather_location_common_deserialize (GWeatherLocation *world,
                continue;
            }
 
-           if (g_strcmp0 (name, city->english_name) == 0)
+           if (name == NULL ||
+               g_strcmp0 (name, city->english_name) == 0 ||
+               g_strcmp0 (name, city->local_name) == 0)
                found = gweather_location_ref (city);
            else
                found = _gweather_location_new_detached (ws, name, TRUE, latitude, longitude);
@@ -1492,3 +1501,43 @@ gweather_location_deserialize (GWeatherLocation *world,
     g_variant_unref (v);
     return loc;
 }
+
+/**
+ * gweather_location_new_detached:
+ * @name: the user visible location name
+ * @icao: (nullable): the ICAO code of the location
+ * @latitude: the latitude of the location
+ * @longitude: the longitude of the location
+ *
+ * Construct a new location from the given data, supplementing
+ * any missing information from the static database.
+ */
+GWeatherLocation *
+gweather_location_new_detached (const char *name,
+                               const char *icao,
+                               gdouble     latitude,
+                               gdouble     longitude)
+{
+    GWeatherLocation *world, *city;
+
+    g_return_val_if_fail (name != NULL, NULL);
+
+    if (*name == 0)
+       name = NULL;
+
+    world = gweather_location_get_world ();
+
+    if (icao != NULL) {
+       return gweather_location_common_deserialize (world, name,
+                                                    icao, FALSE,
+                                                    TRUE, latitude, longitude,
+                                                    FALSE, 0, 0);
+    } else {
+       city = gweather_location_find_nearest_city (world, latitude, longitude);
+
+       latitude = DEGREES_TO_RADIANS (latitude);
+       longitude = DEGREES_TO_RADIANS (longitude);
+       return _gweather_location_new_detached (city, name,
+                                               TRUE, latitude, longitude);
+    }
+}
diff --git a/libgweather/gweather-location.h b/libgweather/gweather-location.h
index 5d02e5a..a0f268a 100644
--- a/libgweather/gweather-location.h
+++ b/libgweather/gweather-location.h
@@ -111,6 +111,11 @@ GVariant              *gweather_location_serialize      (GWeatherLocation  *loc)
 GWeatherLocation      *gweather_location_deserialize    (GWeatherLocation  *world,
                                                         GVariant          *serialized);
 
+GWeatherLocation      *gweather_location_new_detached   (const char        *name,
+                                                        const char        *icao,
+                                                        gdouble            latitude,
+                                                        gdouble            longitude);
+
 G_END_DECLS
 
 #endif /* __GWEATHER_LOCATIONS_H__ */


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