[libgweather] GWeatherLocation: Add API to create world from specific path



commit ad604b791b3a4ff6c87696e75b8b86441fe02af2
Author: Bastien Nocera <hadess hadess net>
Date:   Wed Dec 6 01:39:54 2017 +0100

    GWeatherLocation: Add API to create world from specific path
    
    This will be used to run tests from the uninstalled Locations.xml file.
    
    Note that this also removes a guard from gweather_location_unref() so
    that the created world can be destroyed (muhahaha).
    
    https://bugzilla.gnome.org/show_bug.cgi?id=791289

 libgweather/gweather-location.c |   27 +++++++++++++++++++++++++--
 libgweather/gweather-location.h |    2 ++
 libgweather/gweather-parser.c   |   33 ++++++++++++++++++++++-----------
 libgweather/gweather-parser.h   |    1 +
 4 files changed, 50 insertions(+), 13 deletions(-)
---
diff --git a/libgweather/gweather-location.c b/libgweather/gweather-location.c
index d273ff4..e80bed3 100644
--- a/libgweather/gweather-location.c
+++ b/libgweather/gweather-location.c
@@ -352,6 +352,31 @@ gweather_location_get_world (void)
 }
 
 /**
+ * gweather_location_new_world_for_path:
+ *
+ * The same as gweather_location_get_world() but for a specific Locations.xml
+ * file. This is usually only needed for debugging and testing purposes.
+ *
+ * Return value: (allow-none) (transfer full): a %GWEATHER_LOCATION_WORLD
+ * location, or %NULL if Locations.xml could not be found or could not be parsed.
+ **/
+GWeatherLocation *
+gweather_location_new_world_for_path (const char *path)
+{
+    GWeatherParser *parser;
+    GWeatherLocation *world;
+
+    parser = _gweather_parser_new_for_path (path);
+    if (!parser)
+        return NULL;
+
+    world = location_new_from_xml (parser, GWEATHER_LOCATION_WORLD, NULL);
+    _gweather_parser_free (parser);
+
+    return world;
+}
+
+/**
  * gweather_location_new_world:
  * @use_regions: whether or not to divide the world into regions
  *
@@ -404,8 +429,6 @@ gweather_location_unref (GWeatherLocation *loc)
     if (--loc->ref_count)
        return;
 
-    g_return_if_fail (loc->level != GWEATHER_LOCATION_WORLD);
-
     g_free (loc->english_name);
     g_free (loc->local_name);
     g_free (loc->msgctxt);
diff --git a/libgweather/gweather-location.h b/libgweather/gweather-location.h
index 946df7d..218afac 100644
--- a/libgweather/gweather-location.h
+++ b/libgweather/gweather-location.h
@@ -57,6 +57,8 @@ GWeatherLocation      *gweather_location_get_world      (void);
 
 GWEATHER_EXTERN G_DEPRECATED_FOR(gweather_location_get_world)
 GWeatherLocation      *gweather_location_new_world      (gboolean use_regions);
+GWEATHER_EXTERN
+GWeatherLocation      *gweather_location_new_world_for_path (const char *path);
 
 GWEATHER_EXTERN
 GWeatherLocation      *gweather_location_ref            (GWeatherLocation  *loc);
diff --git a/libgweather/gweather-parser.c b/libgweather/gweather-parser.c
index fc2907e..54cc79c 100644
--- a/libgweather/gweather-parser.c
+++ b/libgweather/gweather-parser.c
@@ -122,17 +122,9 @@ gweather_location_list_free (gpointer list)
 GWeatherParser *
 _gweather_parser_new (void)
 {
-    GWeatherParser *parser;
     int zlib_support;
-    int keep_going;
     char *filename;
-    char *tagname, *format;
-    time_t now;
-    struct tm tm;
-
-    _gweather_gettext_init ();
-
-    parser = g_slice_new0 (GWeatherParser);
+    GWeatherParser *parser;
 
     zlib_support = xmlHasFeature (XML_WITH_ZLIB);
 
@@ -143,10 +135,29 @@ _gweather_parser_new (void)
        filename = g_build_filename (GWEATHER_XML_LOCATION_DIR, "Locations.xml.gz", NULL);
     }
 
-    /* Open the xml file containing the different locations */
-    parser->xml = xmlNewTextReaderFilename (filename);
+    parser = _gweather_parser_new_for_path (filename);
+
     g_free (filename);
 
+    return parser;
+}
+
+GWeatherParser *
+_gweather_parser_new_for_path (const char *path)
+{
+    GWeatherParser *parser;
+    int keep_going;
+    char *tagname, *format;
+    time_t now;
+    struct tm tm;
+
+    _gweather_gettext_init ();
+
+    parser = g_slice_new0 (GWeatherParser);
+
+    /* Open the xml file containing the different locations */
+    parser->xml = xmlNewTextReaderFilename (path);
+
     if (parser->xml == NULL)
        goto error_out;
 
diff --git a/libgweather/gweather-parser.h b/libgweather/gweather-parser.h
index 07c42e4..4e62064 100644
--- a/libgweather/gweather-parser.h
+++ b/libgweather/gweather-parser.h
@@ -33,6 +33,7 @@ typedef struct {
 } GWeatherParser;
 
 GWeatherParser *_gweather_parser_new                 (void);
+GWeatherParser *_gweather_parser_new_for_path        (const char *path);
 void            _gweather_parser_free                (GWeatherParser *parser);
 
 char           *_gweather_parser_get_value           (GWeatherParser *parser);


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