[libshumate/tintou/map-source-registry: 1/2] map-source-registry: Replaces map-source-factory and cleanup unused types




commit d629fa9f5b4d584707fb4166a5130a32d1153bf8
Author: Corentin Noël <corentin noel collabora com>
Date:   Thu Jun 10 13:04:52 2021 +0200

    map-source-registry: Replaces map-source-factory and cleanup unused types
    
    ShumateMapSourceDesc is now gone as it wasn't providing any value.
    
    The ShumateMapSourceRegistry is now a GListModel implementation.

 demos/shumate-demo-window.c                        |  37 +-
 demos/shumate-demo-window.ui                       |   7 -
 demos/shumate-test-tile-source.c                   |   5 +-
 demos/shumate-test-tile-source.h                   |   2 +
 docs/reference/libshumate-docs.sgml                |   6 +-
 shumate/meson.build                                |   6 +-
 shumate/shumate-map-source-desc.c                  | 570 ---------------------
 shumate/shumate-map-source-desc.h                  |  76 ---
 shumate/shumate-map-source-factory.c               | 325 ------------
 shumate/shumate-map-source-registry.c              | 399 +++++++++++++++
 ...rce-factory.h => shumate-map-source-registry.h} |  46 +-
 shumate/shumate-view.c                             |   8 +-
 shumate/shumate.h                                  |   2 +-
 tests/coordinate.c                                 |  11 +-
 14 files changed, 460 insertions(+), 1040 deletions(-)
---
diff --git a/demos/shumate-demo-window.c b/demos/shumate-demo-window.c
index 6011bfc..102d2fd 100644
--- a/demos/shumate-demo-window.c
+++ b/demos/shumate-demo-window.c
@@ -29,6 +29,7 @@ struct _ShumateDemoWindow
   ShumateScale *scale;
   ShumateLicense *license;
   GtkDropDown *layers_dropdown;
+  ShumateMapSourceRegistry *registry;
 
   ShumateMapLayer *tile_layer;
   ShumateMarkerLayer *marker_layer;
@@ -73,8 +74,7 @@ set_map_source (ShumateDemoWindow *self, ShumateMapSource *new_source)
     shumate_license_remove_map_source (self->license, self->current_source);
   }
 
-  g_clear_object (&self->current_source);
-  self->current_source = new_source;
+  g_set_object (&self->current_source, new_source);
 
   shumate_viewport_set_reference_map_source (viewport, new_source);
   shumate_view_set_map_source (self->view, new_source);
@@ -92,28 +92,19 @@ set_map_source (ShumateDemoWindow *self, ShumateMapSource *new_source)
 static void
 on_layers_dropdown_notify_selected (ShumateDemoWindow *self, GParamSpec *pspec, GtkDropDown *dropdown)
 {
-  g_autoptr(ShumateMapSourceFactory) factory = NULL;
-
-  switch (gtk_drop_down_get_selected (dropdown)) {
-  case 0:
-    factory = shumate_map_source_factory_dup_default ();
-    set_map_source (self, shumate_map_source_factory_create (factory, SHUMATE_MAP_SOURCE_OSM_MAPNIK));
-    break;
-  case 1:
-    set_map_source (self, SHUMATE_MAP_SOURCE (shumate_test_tile_source_new ()));
-    break;
-  }
+  set_map_source (self, gtk_drop_down_get_selected_item (dropdown));
 }
 
 
 static void
-shumate_demo_window_finalize (GObject *object)
+shumate_demo_window_dispose (GObject *object)
 {
   ShumateDemoWindow *self = SHUMATE_DEMO_WINDOW (object);
 
   g_clear_object (&self->current_source);
+  g_clear_object (&self->registry);
 
-  G_OBJECT_CLASS (shumate_demo_window_parent_class)->finalize (object);
+  G_OBJECT_CLASS (shumate_demo_window_parent_class)->dispose (object);
 }
 
 
@@ -123,7 +114,7 @@ shumate_demo_window_class_init (ShumateDemoWindowClass *klass)
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
 
-  object_class->finalize = shumate_demo_window_finalize;
+  object_class->dispose = shumate_demo_window_dispose;
 
   gtk_widget_class_set_template_from_resource (widget_class, 
"/org/gnome/Shumate/Demo/ui/shumate-demo-window.ui");
   gtk_widget_class_bind_template_child (widget_class, ShumateDemoWindow, view);
@@ -134,14 +125,28 @@ shumate_demo_window_class_init (ShumateDemoWindowClass *klass)
   gtk_widget_class_bind_template_callback (widget_class, on_layers_dropdown_notify_selected);
 }
 
+static gchar *
+get_map_source_name (ShumateMapSource *map_source)
+{
+  return g_strdup (shumate_map_source_get_name (map_source));
+}
 
 static void
 shumate_demo_window_init (ShumateDemoWindow *self)
 {
   ShumateViewport *viewport;
+  GtkExpression *expression;
 
   gtk_widget_init_template (GTK_WIDGET (self));
 
+  self->registry = shumate_map_source_registry_new_with_defaults ();
+  shumate_map_source_registry_add (self->registry, SHUMATE_MAP_SOURCE (shumate_test_tile_source_new ()));
+  expression = gtk_cclosure_expression_new (G_TYPE_STRING, NULL, 0, NULL,
+                                            (GCallback)get_map_source_name, NULL, NULL);
+  gtk_drop_down_set_expression (self->layers_dropdown, expression);
+  gtk_drop_down_set_model (self->layers_dropdown, G_LIST_MODEL (self->registry));
+
+
   viewport = shumate_view_get_viewport (self->view);
 
   /* Set the map source */
diff --git a/demos/shumate-demo-window.ui b/demos/shumate-demo-window.ui
index 2fabff7..9191aca 100644
--- a/demos/shumate-demo-window.ui
+++ b/demos/shumate-demo-window.ui
@@ -10,7 +10,6 @@
       <object class="GtkHeaderBar">
         <child>
           <object class="GtkDropDown" id="layers_dropdown">
-            <property name="model">layers</property>
             <signal name="notify::selected" handler="on_layers_dropdown_notify_selected" swapped="true" />
           </object>
         </child>
@@ -38,10 +37,4 @@
       </object>
     </child>
   </template>
-  <object class="GtkStringList" id="layers">
-    <items>
-      <item translatable="yes">Mapnik (OSM)</item>
-      <item translatable="yes">Test Pattern</item>
-    </items>
-  </object>
 </interface>
diff --git a/demos/shumate-test-tile-source.c b/demos/shumate-test-tile-source.c
index c0a458a..3c970a9 100644
--- a/demos/shumate-test-tile-source.c
+++ b/demos/shumate-test-tile-source.c
@@ -29,7 +29,10 @@ G_DEFINE_TYPE (ShumateTestTileSource, shumate_test_tile_source, SHUMATE_TYPE_TIL
 ShumateTestTileSource *
 shumate_test_tile_source_new (void)
 {
-  return g_object_new (SHUMATE_TYPE_TEST_TILE_SOURCE, NULL);
+  return g_object_new (SHUMATE_TYPE_TEST_TILE_SOURCE,
+                       "id", SHUMATE_MAP_SOURCE_TEST,
+                       "name", "Test Pattern",
+                       NULL);
 }
 
 
diff --git a/demos/shumate-test-tile-source.h b/demos/shumate-test-tile-source.h
index 8a17d44..83956b5 100644
--- a/demos/shumate-test-tile-source.h
+++ b/demos/shumate-test-tile-source.h
@@ -29,4 +29,6 @@ G_DECLARE_FINAL_TYPE (ShumateTestTileSource, shumate_test_tile_source, SHUMATE,
 
 ShumateTestTileSource *shumate_test_tile_source_new (void);
 
+#define SHUMATE_MAP_SOURCE_TEST "test-source"
+
 G_END_DECLS
diff --git a/docs/reference/libshumate-docs.sgml b/docs/reference/libshumate-docs.sgml
index d99d7c2..493a36e 100644
--- a/docs/reference/libshumate-docs.sgml
+++ b/docs/reference/libshumate-docs.sgml
@@ -73,6 +73,7 @@
   <part>
     <title>Map Source API</title>
     <xi:include href="xml/shumate-map-source.xml"/>
+    <xi:include href="xml/shumate-map-source-registry.xml"/>
     <chapter>
       <title>Tile Sources</title>
       <xi:include href="xml/shumate-tile-source.xml"/>
@@ -83,11 +84,6 @@
       <xi:include href="xml/shumate-file-cache.xml"/>
       <xi:include href="xml/shumate-memory-cache.xml"/>
     </chapter>
-    <chapter>
-      <title>Map Source Utilities</title>
-      <xi:include href="xml/shumate-map-source-factory.xml"/>
-      <xi:include href="xml/shumate-map-source-desc.xml"/>
-    </chapter>
   </part>
   <part>
     <title>Others</title>
diff --git a/shumate/meson.build b/shumate/meson.build
index 61cd055..80c19f4 100644
--- a/shumate/meson.build
+++ b/shumate/meson.build
@@ -5,8 +5,7 @@ libshumate_public_h = [
   'shumate-license.h',
   'shumate-location.h',
   'shumate-map-layer.h',
-  'shumate-map-source-desc.h',
-  'shumate-map-source-factory.h',
+  'shumate-map-source-registry.h',
   'shumate-map-source.h',
   'shumate-marker-layer.h',
   'shumate-marker.h',
@@ -35,8 +34,7 @@ libshumate_sources = [
   'shumate-license.c',
   'shumate-location.c',
   'shumate-map-layer.c',
-  'shumate-map-source-desc.c',
-  'shumate-map-source-factory.c',
+  'shumate-map-source-registry.c',
   'shumate-map-source.c',
   'shumate-marker-layer.c',
   'shumate-marker.c',
diff --git a/shumate/shumate-map-source-registry.c b/shumate/shumate-map-source-registry.c
new file mode 100644
index 0000000..f690713
--- /dev/null
+++ b/shumate/shumate-map-source-registry.c
@@ -0,0 +1,399 @@
+/*
+ * Copyright 2021 Collabora Ltd. (https://collabora.com)
+ * Copyright 2021 Corentin Noël <corentin noel collabora com>
+ * Copyright 2009 Pierre-Luc Beaudoin <pierre-luc pierlux com>
+ * Copyright 2010-2013 Jiri Techet <techet gmail com>
+ * Copyright 2019 Marcus Lundblad <ml update uu se>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+/**
+ * SECTION:shumate-map-source-registry
+ * @short_description: An object holding #ShumateMapSource
+ *
+ * This object allows you to hold #ShumateMapSource instances, you can access a
+ * default set of sources with shumate_map_source_registry_populate_defaults().
+ *
+ * It conveniently implements #GListModel to easily integrate with it.
+ */
+
+#include "shumate-map-source-registry.h"
+
+#include <gio/gio.h>
+
+#include "shumate-network-tile-source.h"
+
+struct _ShumateMapSourceRegistry
+{
+  GObject parent_instance;
+
+  GPtrArray *map_sources;
+};
+
+static void shumate_map_source_registry_list_model_init (GListModelInterface *iface);
+
+G_DEFINE_TYPE_WITH_CODE (ShumateMapSourceRegistry, shumate_map_source_registry, G_TYPE_OBJECT,
+                         G_IMPLEMENT_INTERFACE(G_TYPE_LIST_MODEL, 
shumate_map_source_registry_list_model_init))
+
+static gboolean
+shumate_map_source_registry_find_by_id (ShumateMapSource *map_source,
+                                        const gchar      *id)
+{
+  return g_strcmp0 (shumate_map_source_get_id (map_source), id) == 0;
+}
+
+static void
+shumate_map_source_registry_dispose (GObject *object)
+{
+  ShumateMapSourceRegistry *self = (ShumateMapSourceRegistry *)object;
+
+  g_clear_pointer (&self->map_sources, g_ptr_array_unref);
+
+  G_OBJECT_CLASS (shumate_map_source_registry_parent_class)->dispose (object);
+}
+
+static void
+shumate_map_source_registry_class_init (ShumateMapSourceRegistryClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->dispose = shumate_map_source_registry_dispose;
+}
+
+static void
+shumate_map_source_registry_init (ShumateMapSourceRegistry *self)
+{
+  self->map_sources = g_ptr_array_new_with_free_func (g_object_unref);
+}
+
+static GType
+shumate_map_source_registry_get_item_type (GListModel *list)
+{
+  return SHUMATE_TYPE_MAP_SOURCE;
+}
+
+static guint
+shumate_map_source_registry_get_n_items (GListModel *list)
+{
+  ShumateMapSourceRegistry *self = SHUMATE_MAP_SOURCE_REGISTRY (list);
+
+  return self->map_sources->len;
+}
+
+static gpointer
+shumate_map_source_registry_get_item (GListModel *list,
+                                      guint       position)
+{
+  ShumateMapSourceRegistry *self = SHUMATE_MAP_SOURCE_REGISTRY (list);
+
+  if (position >= self->map_sources->len)
+    return NULL;
+  else
+    return g_object_ref (g_ptr_array_index (self->map_sources, position));
+}
+
+static void
+shumate_map_source_registry_list_model_init (GListModelInterface *iface)
+{
+  iface->get_item_type = shumate_map_source_registry_get_item_type;
+  iface->get_n_items = shumate_map_source_registry_get_n_items;
+  iface->get_item = shumate_map_source_registry_get_item;
+}
+
+/**
+ * shumate_map_source_registry_new:
+ *
+ * Create a new #ShumateMapSourceRegistry.
+ *
+ * Returns: (transfer full): a newly created #ShumateMapSourceRegistry
+ */
+ShumateMapSourceRegistry *
+shumate_map_source_registry_new (void)
+{
+  return g_object_new (SHUMATE_TYPE_MAP_SOURCE_REGISTRY, NULL);
+}
+
+/**
+ * shumate_map_source_registry_new_with_defaults:
+ *
+ * Create a new #ShumateMapSourceRegistry with defaults map sources.
+ * This is identical to calling shumate_map_source_registry_populate_defaults()
+ * after shumate_map_source_registry_new().
+ *
+ * Returns: (transfer full): a newly created #ShumateMapSourceRegistry
+ */
+ShumateMapSourceRegistry *
+shumate_map_source_registry_new_with_defaults (void)
+{
+  ShumateMapSourceRegistry *registry = shumate_map_source_registry_new ();
+
+  shumate_map_source_registry_populate_defaults (registry);
+
+  return registry;
+}
+
+/**
+ * shumate_map_source_registry_populate_defaults:
+ * @self: a #ShumateMapSourceRegistry
+ *
+ * Populates the #ShumateMapSourceRegistry with a default set of sources.
+ */
+void
+shumate_map_source_registry_populate_defaults (ShumateMapSourceRegistry *self)
+{
+  uint n_items;
+
+  g_return_if_fail (SHUMATE_IS_MAP_SOURCE_REGISTRY (self));
+
+  n_items = self->map_sources->len;
+
+  if (!shumate_map_source_registry_get_by_id (self, SHUMATE_MAP_SOURCE_OSM_MAPNIK))
+    {
+      g_ptr_array_add (self->map_sources,
+        shumate_network_tile_source_new_full (
+          SHUMATE_MAP_SOURCE_OSM_MAPNIK,
+          "OpenStreetMap Mapnik",
+          "Map Data ODBL OpenStreetMap Contributors, Map Imagery CC-BY-SA 2.0 OpenStreetMap",
+          "http://creativecommons.org/licenses/by-sa/2.0/";,
+          0,
+          18,
+          256,
+          SHUMATE_MAP_PROJECTION_MERCATOR,
+          "https://tile.openstreetmap.org/#Z#/#X#/#Y#.png";
+        )
+      );
+    }
+
+  if (!shumate_map_source_registry_get_by_id (self, SHUMATE_MAP_SOURCE_OSM_CYCLE_MAP))
+    {
+      g_ptr_array_add (self->map_sources,
+        shumate_network_tile_source_new_full (
+          SHUMATE_MAP_SOURCE_OSM_CYCLE_MAP,
+          "OpenStreetMap Cycle Map",
+          "Map data is CC-BY-SA 2.0 OpenStreetMap contributors",
+          "http://creativecommons.org/licenses/by-sa/2.0/";,
+          0,
+          18,
+          256,
+          SHUMATE_MAP_PROJECTION_MERCATOR,
+          "http://tile.opencyclemap.org/cycle/#Z#/#X#/#Y#.png";
+        )
+      );
+    }
+
+  if (!shumate_map_source_registry_get_by_id (self, SHUMATE_MAP_SOURCE_OSM_TRANSPORT_MAP))
+    {
+      g_ptr_array_add (self->map_sources,
+        shumate_network_tile_source_new_full (
+          SHUMATE_MAP_SOURCE_OSM_TRANSPORT_MAP,
+          "OpenStreetMap Transport Map",
+          "Map data is CC-BY-SA 2.0 OpenStreetMap contributors",
+          "http://creativecommons.org/licenses/by-sa/2.0/";,
+          0,
+          18,
+          256,
+          SHUMATE_MAP_PROJECTION_MERCATOR,
+          "http://tile.xn--pnvkarte-m4a.de/tilegen/#Z#/#X#/#Y#.png";
+        )
+      );
+    }
+
+  if (!shumate_map_source_registry_get_by_id (self, SHUMATE_MAP_SOURCE_MFF_RELIEF))
+    {
+      g_ptr_array_add (self->map_sources,
+        shumate_network_tile_source_new_full (
+          SHUMATE_MAP_SOURCE_MFF_RELIEF,
+          "Maps for Free Relief",
+          "Map data available under GNU Free Documentation license, Version 1.2 or later",
+          "http://www.gnu.org/copyleft/fdl.html";,
+          0,
+          11,
+          256,
+          SHUMATE_MAP_PROJECTION_MERCATOR,
+          "http://maps-for-free.com/layer/relief/z#Z#/row#Y#/#Z#_#X#-#Y#.jpg";
+        )
+      );
+    }
+
+  if (!shumate_map_source_registry_get_by_id (self, SHUMATE_MAP_SOURCE_OWM_CLOUDS))
+    {
+      g_ptr_array_add (self->map_sources,
+        shumate_network_tile_source_new_full (
+          SHUMATE_MAP_SOURCE_OWM_CLOUDS,
+          "OpenWeatherMap cloud layer",
+          "Map data is CC-BY-SA 2.0 OpenWeatherMap contributors",
+          "http://creativecommons.org/licenses/by-sa/2.0/";,
+          0,
+          18,
+          256,
+          SHUMATE_MAP_PROJECTION_MERCATOR,
+          "http://tile.openweathermap.org/map/clouds/#Z#/#X#/#Y#.png";
+        )
+      );
+    }
+
+  if (!shumate_map_source_registry_get_by_id (self, SHUMATE_MAP_SOURCE_OWM_WIND))
+    {
+      g_ptr_array_add (self->map_sources,
+        shumate_network_tile_source_new_full (
+          SHUMATE_MAP_SOURCE_OWM_WIND,
+          "OpenWeatherMap wind layer",
+          "Map data is CC-BY-SA 2.0 OpenWeatherMap contributors",
+          "http://creativecommons.org/licenses/by-sa/2.0/";,
+          0,
+          18,
+          256,
+          SHUMATE_MAP_PROJECTION_MERCATOR,
+          "http://tile.openweathermap.org/map/wind/#Z#/#X#/#Y#.png";
+        )
+      );
+    }
+
+  if (!shumate_map_source_registry_get_by_id (self, SHUMATE_MAP_SOURCE_OWM_TEMPERATURE))
+    {
+      g_ptr_array_add (self->map_sources,
+        shumate_network_tile_source_new_full (
+          SHUMATE_MAP_SOURCE_OWM_TEMPERATURE,
+          "OpenWeatherMap temperature layer",
+          "Map data is CC-BY-SA 2.0 OpenWeatherMap contributors",
+          "http://creativecommons.org/licenses/by-sa/2.0/";,
+          0,
+          18,
+          256,
+          SHUMATE_MAP_PROJECTION_MERCATOR,
+          "http://tile.openweathermap.org/map/temp/#Z#/#X#/#Y#.png";
+        )
+      );
+    }
+
+  if (!shumate_map_source_registry_get_by_id (self, SHUMATE_MAP_SOURCE_OWM_PRECIPITATION))
+    {
+      g_ptr_array_add (self->map_sources,
+        shumate_network_tile_source_new_full (
+          SHUMATE_MAP_SOURCE_OWM_PRECIPITATION,
+          "OpenWeatherMap precipitation layer",
+          "Map data is CC-BY-SA 2.0 OpenWeatherMap contributors",
+          "http://creativecommons.org/licenses/by-sa/2.0/";,
+          0,
+          18,
+          256,
+          SHUMATE_MAP_PROJECTION_MERCATOR,
+          "http://tile.openweathermap.org/map/precipitation/#Z#/#X#/#Y#.png";
+        )
+      );
+    }
+
+  if (!shumate_map_source_registry_get_by_id (self, SHUMATE_MAP_SOURCE_OWM_PRESSURE))
+    {
+      g_ptr_array_add (self->map_sources,
+        shumate_network_tile_source_new_full (
+          SHUMATE_MAP_SOURCE_OWM_PRESSURE,
+          "OpenWeatherMap sea level pressure layer",
+          "Map data is CC-BY-SA 2.0 OpenWeatherMap contributors",
+          "http://creativecommons.org/licenses/by-sa/2.0/";,
+          0,
+          18,
+          256,
+          SHUMATE_MAP_PROJECTION_MERCATOR,
+          "http://tile.openweathermap.org/map/pressure/#Z#/#X#/#Y#.png";
+        )
+      );
+    }
+
+  if (self->map_sources->len - n_items > 0) {
+    g_list_model_items_changed (G_LIST_MODEL (self), n_items, self->map_sources->len - n_items, 0);
+  }
+}
+
+/**
+ * shumate_map_source_registry_get_by_id:
+ * @self: a #ShumateMapSourceRegistry
+ * @id: the id of the #ShumateMapSource
+ *
+ * Find the #ShumateMapSource with the corresponding id
+ *
+ * Returns: (transfer none) (nullable): the #ShumateMapSource or %NULL if no
+ * map source has been found
+ */
+ShumateMapSource *
+shumate_map_source_registry_get_by_id (ShumateMapSourceRegistry *self,
+                                       const gchar              *id)
+{
+  guint index;
+
+  g_return_val_if_fail (SHUMATE_IS_MAP_SOURCE_REGISTRY (self), NULL);
+  g_return_val_if_fail (id != NULL, NULL);
+
+  if (g_ptr_array_find_with_equal_func (self->map_sources, id,
+                                        (GEqualFunc) shumate_map_source_registry_find_by_id,
+                                        &index))
+    {
+      return g_ptr_array_index (self->map_sources, index);
+    }
+
+  return NULL;
+}
+
+/**
+ * shumate_map_source_registry_add:
+ * @self: a #ShumateMapSourceRegistry
+ * @map_source: (transfer full): a #ShumateMapSource
+ *
+ * Adds the #ShumateMapSource to the #ShumateMapSourceRegistry
+ */
+void shumate_map_source_registry_add (ShumateMapSourceRegistry *self,
+                                      ShumateMapSource         *map_source)
+{
+  guint n_items;
+
+  g_return_if_fail (SHUMATE_IS_MAP_SOURCE_REGISTRY (self));
+  g_return_if_fail (SHUMATE_IS_MAP_SOURCE (map_source));
+
+  if (!g_ptr_array_find_with_equal_func (self->map_sources, shumate_map_source_get_id (map_source),
+                                         (GEqualFunc) shumate_map_source_registry_find_by_id,
+                                         NULL))
+    {
+      n_items = self->map_sources->len;
+      g_ptr_array_add (self->map_sources, map_source);
+
+      g_list_model_items_changed (G_LIST_MODEL (self), n_items, 0, 1);
+    }
+}
+
+/**
+ * shumate_map_source_registry_remove:
+ * @self: a #ShumateMapSourceRegistry
+ * @id: a #ShumateMapSource id
+ *
+ * Removes the corresponding #ShumateMapSource from the registry.
+ * If the source doesn't exist in the registry, this function does nothing.
+ */
+void shumate_map_source_registry_remove (ShumateMapSourceRegistry *self,
+                                         const gchar              *id)
+{
+  guint index;
+
+  g_return_if_fail (SHUMATE_IS_MAP_SOURCE_REGISTRY (self));
+  g_return_if_fail (id != NULL);
+
+  if (g_ptr_array_find_with_equal_func (self->map_sources, id,
+                                        (GEqualFunc) shumate_map_source_registry_find_by_id,
+                                        &index))
+    {
+      g_ptr_array_remove_index (self->map_sources, index);
+      g_list_model_items_changed (G_LIST_MODEL (self), index, 1, 0);
+    }
+}
diff --git a/shumate/shumate-map-source-factory.h b/shumate/shumate-map-source-registry.h
similarity index 62%
rename from shumate/shumate-map-source-factory.h
rename to shumate/shumate-map-source-registry.h
index 7541c35..83b2c55 100644
--- a/shumate/shumate-map-source-factory.h
+++ b/shumate/shumate-map-source-registry.h
@@ -1,7 +1,9 @@
 /*
- * Copyright (C) 2009 Pierre-Luc Beaudoin <pierre-luc pierlux com>
- * Copyright (C) 2010-2013 Jiri Techet <techet gmail com>
- * Copyright (C) 2019 Marcus Lundblad <ml update uu se>
+ * Copyright 2021 Collabora Ltd. (https://collabora.com)
+ * Copyright 2021 Corentin Noël <corentin noel collabora com>
+ * Copyright 2009 Pierre-Luc Beaudoin <pierre-luc pierlux com>
+ * Copyright 2010-2013 Jiri Techet <techet gmail com>
+ * Copyright 2019 Marcus Lundblad <ml update uu se>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -22,34 +24,28 @@
 #error "Only <shumate/shumate.h> can be included directly."
 #endif
 
-#ifndef SHUMATE_MAP_SOURCE_FACTORY_H
-#define SHUMATE_MAP_SOURCE_FACTORY_H
-
-#include <shumate/shumate-map-source.h>
-#include <shumate/shumate-map-source-desc.h>
+#ifndef __SHUMATE_MAP_SOURCE_REGISTRY_H__
+#define __SHUMATE_MAP_SOURCE_REGISTRY_H__
 
 #include <glib-object.h>
 
-G_BEGIN_DECLS
-
-#define SHUMATE_TYPE_MAP_SOURCE_FACTORY shumate_map_source_factory_get_type ()
-G_DECLARE_FINAL_TYPE (ShumateMapSourceFactory, shumate_map_source_factory, SHUMATE, MAP_SOURCE_FACTORY, 
GObject)
+#include <shumate/shumate-map-source.h>
 
-/**
- * ShumateMapSourceFactory:
- *
- * The #ShumateMapSourceFactory structure contains only private data
- * and should be accessed using the provided API
- */
+G_BEGIN_DECLS
 
-ShumateMapSourceFactory *shumate_map_source_factory_dup_default (void);
+#define SHUMATE_TYPE_MAP_SOURCE_REGISTRY (shumate_map_source_registry_get_type())
 
-ShumateMapSource *shumate_map_source_factory_create (ShumateMapSourceFactory *factory,
-    const char *id);
+G_DECLARE_FINAL_TYPE (ShumateMapSourceRegistry, shumate_map_source_registry, SHUMATE, MAP_SOURCE_REGISTRY, 
GObject)
 
-gboolean shumate_map_source_factory_register (ShumateMapSourceFactory *factory,
-    ShumateMapSourceDesc *desc);
-GSList *shumate_map_source_factory_get_registered (ShumateMapSourceFactory *factory);
+ShumateMapSourceRegistry *shumate_map_source_registry_new (void);
+ShumateMapSourceRegistry *shumate_map_source_registry_new_with_defaults (void);
+void shumate_map_source_registry_populate_defaults (ShumateMapSourceRegistry *self);
+ShumateMapSource *shumate_map_source_registry_get_by_id (ShumateMapSourceRegistry *self,
+                                                         const gchar              *id);
+void shumate_map_source_registry_add (ShumateMapSourceRegistry *self,
+                                      ShumateMapSource         *map_source);
+void shumate_map_source_registry_remove (ShumateMapSourceRegistry *self,
+                                         const gchar              *id);
 
 /**
  * SHUMATE_MAP_SOURCE_OSM_MAPNIK:
@@ -108,4 +104,4 @@ GSList *shumate_map_source_factory_get_registered (ShumateMapSourceFactory *fact
 
 G_END_DECLS
 
-#endif
+#endif /* __SHUMATE_MAP_SOURCE_REGISTRY_H__ */
diff --git a/shumate/shumate-view.c b/shumate/shumate-view.c
index c01b59d..9090c92 100644
--- a/shumate/shumate-view.c
+++ b/shumate/shumate-view.c
@@ -56,7 +56,7 @@
 #include "shumate-marshal.h"
 #include "shumate-map-layer.h"
 #include "shumate-map-source.h"
-#include "shumate-map-source-factory.h"
+#include "shumate-map-source-registry.h"
 #include "shumate-tile.h"
 #include "shumate-license.h"
 #include "shumate-location.h"
@@ -967,14 +967,14 @@ ShumateView *
 shumate_view_new_simple (void)
 {
   ShumateView *view = g_object_new (SHUMATE_TYPE_VIEW, NULL);
-  ShumateMapSourceFactory *factory;
+  g_autoptr(ShumateMapSourceRegistry) registry = NULL;
   ShumateMapSource *source;
   ShumateMapLayer *map_layer;
   ShumateViewport *viewport;
   
   viewport = shumate_view_get_viewport (view);
-  factory = shumate_map_source_factory_dup_default ();
-  source = shumate_map_source_factory_create (factory, SHUMATE_MAP_SOURCE_OSM_MAPNIK);
+  registry = shumate_map_source_registry_new_with_defaults ();
+  source = shumate_map_source_registry_get_by_id (registry, SHUMATE_MAP_SOURCE_OSM_MAPNIK);
   shumate_viewport_set_reference_map_source (viewport, source);
   map_layer = shumate_map_layer_new (source, viewport);
   shumate_view_add_layer (view, SHUMATE_LAYER (map_layer));
diff --git a/shumate/shumate.h b/shumate/shumate.h
index 71f1650..591d40c 100644
--- a/shumate/shumate.h
+++ b/shumate/shumate.h
@@ -43,7 +43,7 @@
 #include "shumate/shumate-map-source.h"
 #include "shumate/shumate-tile-source.h"
 
-#include "shumate/shumate-map-source-factory.h"
+#include "shumate/shumate-map-source-registry.h"
 
 #include "shumate/shumate-network-tile-source.h"
 
diff --git a/tests/coordinate.c b/tests/coordinate.c
index 9fa1960..1ab7b04 100644
--- a/tests/coordinate.c
+++ b/tests/coordinate.c
@@ -6,17 +6,17 @@
 static void
 test_coordinate_convert (void)
 {
-  ShumateMapSourceFactory *factory;
+  ShumateMapSourceRegistry *registry;
   ShumateMapSource *source;
   double latitude = -73.75f;
   double longitude = 45.466f;
   double zoom_level;
 
-  factory = shumate_map_source_factory_dup_default ();
+  registry = shumate_map_source_registry_new_with_defaults ();
 
-  g_assert_nonnull (factory);
+  g_assert_nonnull (registry);
 
-  source = shumate_map_source_factory_create (factory, SHUMATE_MAP_SOURCE_OSM_MAPNIK);
+  source = shumate_map_source_registry_get_by_id (registry, SHUMATE_MAP_SOURCE_OSM_MAPNIK);
 
   g_assert_nonnull (source);
   
@@ -34,8 +34,7 @@ test_coordinate_convert (void)
       g_assert_cmpfloat_with_epsilon (shumate_map_source_get_latitude (source, zoom_level, y), latitude, 
ACCEPTABLE_EPSILON);
     }
 
-  g_object_unref (source);
-  g_object_unref (factory);
+  g_object_unref (registry);
 }
 
 int


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