[libchamplain] Simplify use of cache for ordinary applications



commit daced1cc38370186579f8a628e1e2a377d014919
Author: JiÅ?í Techet <techet gmail com>
Date:   Thu Jan 21 00:13:24 2010 +0100

    Simplify use of cache for ordinary applications
    
    * Create default constructor for caches
    
    * Create a factory function for creation of
    cache->tile source->error tile source chain
    
    * Rename champlain_map_source_chain_push_map_source to
    champlain_map_source_chain_push
    
    Signed-off-by: JiÅ?í Techet <techet gmail com>

 champlain/champlain-file-cache.c         |   13 ++++++++++
 champlain/champlain-file-cache.h         |    1 +
 champlain/champlain-map-source-chain.c   |    2 +-
 champlain/champlain-map-source-chain.h   |    2 +-
 champlain/champlain-map-source-factory.c |   38 +++++++++++++++--------------
 champlain/champlain-map-source-factory.h |    2 +
 champlain/champlain-view.c               |   22 +---------------
 demos/launcher-gtk.c                     |   21 +---------------
 demos/local-rendering.c                  |   11 +++-----
 9 files changed, 45 insertions(+), 67 deletions(-)
---
diff --git a/champlain/champlain-file-cache.c b/champlain/champlain-file-cache.c
index e6d5cb7..209c631 100644
--- a/champlain/champlain-file-cache.c
+++ b/champlain/champlain-file-cache.c
@@ -357,6 +357,19 @@ champlain_file_cache_init (ChamplainFileCache *file_cache)
   priv->stmt_update = NULL;
 }
 
+ChamplainFileCache* champlain_file_cache_new (void)
+{
+  gchar *cache_path;
+  ChamplainFileCache *cache;
+
+  cache_path = g_build_path (G_DIR_SEPARATOR_S, g_get_user_cache_dir (), "champlain", NULL);
+  cache = g_object_new (CHAMPLAIN_TYPE_FILE_CACHE, "size-limit", 100000000,
+                        "cache-dir", cache_path, "persistent-cache", TRUE, NULL);
+  g_free(cache_path);
+
+  return cache;
+}
+
 ChamplainFileCache* champlain_file_cache_new_full (guint size_limit,
     const gchar *cache_dir, gboolean persistent)
 {
diff --git a/champlain/champlain-file-cache.h b/champlain/champlain-file-cache.h
index a6a3d69..d15d681 100644
--- a/champlain/champlain-file-cache.h
+++ b/champlain/champlain-file-cache.h
@@ -51,6 +51,7 @@ struct _ChamplainFileCacheClass
 
 GType champlain_file_cache_get_type (void);
 
+ChamplainFileCache* champlain_file_cache_new (void);
 ChamplainFileCache* champlain_file_cache_new_full (guint size_limit,
     const gchar *cache_dir, gboolean persistent);
 
diff --git a/champlain/champlain-map-source-chain.c b/champlain/champlain-map-source-chain.c
index e9df378..25e381c 100644
--- a/champlain/champlain-map-source-chain.c
+++ b/champlain/champlain-map-source-chain.c
@@ -219,7 +219,7 @@ void reload_tiles_cb(ChamplainMapSource *map_source, ChamplainMapSourceChain *so
   g_signal_emit_by_name (source_chain, "reload-tiles", NULL);
 }
 
-void champlain_map_source_chain_push_map_source(ChamplainMapSourceChain *source_chain, ChamplainMapSource *map_source)
+void champlain_map_source_chain_push(ChamplainMapSourceChain *source_chain, ChamplainMapSource *map_source)
 {
   ChamplainMapSourceChainPrivate *priv = GET_PRIVATE(source_chain);
   gboolean is_cache = FALSE;
diff --git a/champlain/champlain-map-source-chain.h b/champlain/champlain-map-source-chain.h
index e633070..f6d3379 100644
--- a/champlain/champlain-map-source-chain.h
+++ b/champlain/champlain-map-source-chain.h
@@ -53,7 +53,7 @@ GType champlain_map_source_chain_get_type (void);
 
 ChamplainMapSourceChain* champlain_map_source_chain_new (void);
 
-void champlain_map_source_chain_push_map_source(ChamplainMapSourceChain *source_chain, ChamplainMapSource *map_source);
+void champlain_map_source_chain_push(ChamplainMapSourceChain *source_chain, ChamplainMapSource *map_source);
 void champlain_map_source_chain_pop_map_source(ChamplainMapSourceChain *source_chain);
 
 G_END_DECLS
diff --git a/champlain/champlain-map-source-factory.c b/champlain/champlain-map-source-factory.c
index d78153b..4c84555 100644
--- a/champlain/champlain-map-source-factory.c
+++ b/champlain/champlain-map-source-factory.c
@@ -357,12 +357,7 @@ ChamplainMapSource *
 champlain_map_source_factory_create (ChamplainMapSourceFactory *factory,
     const gchar *id)
 {
-//  ChamplainMapSource *map_source = NULL;
-//  ChamplainMapSourceChain *source_chain;
-//  ChamplainMapSource *source;
   GSList *item;
-//  guint tile_size;
-//  gchar *cache_path;
 
   item = factory->priv->registered_sources;
 
@@ -370,28 +365,35 @@ champlain_map_source_factory_create (ChamplainMapSourceFactory *factory,
     {
       ChamplainMapSourceDesc *desc = CHAMPLAIN_MAP_SOURCE_DESC (item->data);
       if (strcmp (desc->id, id) == 0)
-        return desc->constructor (desc, desc->data); //map_source = desc->constructor (desc, desc->data);
+        return desc->constructor (desc, desc->data);
       item = g_slist_next (item);
     }
 
-//  if (!map_source)
-     return NULL;
+  return NULL;
+}
 
-/*  source_chain = champlain_map_source_chain_new ();
+ChamplainMapSource * champlain_map_source_factory_create_cached_source (ChamplainMapSourceFactory *factory,
+    const gchar *id)
+{
+  ChamplainMapSourceChain *source_chain;
+  ChamplainMapSource *tile_source;
+  ChamplainMapSource *error_source;
+  ChamplainMapSource *file_cache;
+  guint tile_size;
 
-  tile_size = champlain_map_source_get_tile_size(map_source);
-  source = CHAMPLAIN_MAP_SOURCE(champlain_error_tile_source_new_full (tile_size));
+  tile_source = champlain_map_source_factory_create (factory, id);
 
-  champlain_map_source_chain_push_map_source(source_chain, source);
-  champlain_map_source_chain_push_map_source(source_chain, map_source);
+  tile_size = champlain_map_source_get_tile_size(tile_source);
+  error_source = CHAMPLAIN_MAP_SOURCE(champlain_error_tile_source_new_full (tile_size));
 
-  cache_path = g_build_path (G_DIR_SEPARATOR_S, g_get_user_cache_dir (), "champlain", NULL);
-  source = CHAMPLAIN_MAP_SOURCE(champlain_file_cache_new_full (100000000, cache_path, TRUE));
-  g_free(cache_path);
+  file_cache = CHAMPLAIN_MAP_SOURCE(champlain_file_cache_new ());
 
-  champlain_map_source_chain_push_map_source(source_chain, source);
+  source_chain = champlain_map_source_chain_new ();
+  champlain_map_source_chain_push(source_chain, error_source);
+  champlain_map_source_chain_push(source_chain, tile_source);
+  champlain_map_source_chain_push(source_chain, file_cache);
 
-  return CHAMPLAIN_MAP_SOURCE(source_chain);*/
+  return CHAMPLAIN_MAP_SOURCE(source_chain);
 }
 
 /**
diff --git a/champlain/champlain-map-source-factory.h b/champlain/champlain-map-source-factory.h
index 972178f..badfaff 100644
--- a/champlain/champlain-map-source-factory.h
+++ b/champlain/champlain-map-source-factory.h
@@ -61,6 +61,8 @@ GSList * champlain_map_source_factory_dup_list (ChamplainMapSourceFactory *facto
 
 ChamplainMapSource * champlain_map_source_factory_create (ChamplainMapSourceFactory *factory,
     const gchar *id);
+ChamplainMapSource * champlain_map_source_factory_create_cached_source (ChamplainMapSourceFactory *factory,
+    const gchar *id);
 
 gboolean
 champlain_map_source_factory_register (ChamplainMapSourceFactory *factory,
diff --git a/champlain/champlain-view.c b/champlain/champlain-view.c
index 29f7b10..bae4953 100644
--- a/champlain/champlain-view.c
+++ b/champlain/champlain-view.c
@@ -1286,34 +1286,16 @@ static void
 champlain_view_init (ChamplainView *view)
 {
   ChamplainViewPrivate *priv = GET_PRIVATE (view);
-  ChamplainMapSourceChain *source_chain;
   ChamplainMapSource *source;
-  ChamplainMapSource *src;
-  guint tile_size;
-  gchar *cache_path;
 
   champlain_debug_set_flags (g_getenv ("CHAMPLAIN_DEBUG"));
 
   view->priv = priv;
 
   priv->factory = champlain_map_source_factory_dup_default ();
-  source = champlain_map_source_factory_create (priv->factory, CHAMPLAIN_MAP_SOURCE_OSM_MAPNIK);
+  source = champlain_map_source_factory_create_cached_source (priv->factory, CHAMPLAIN_MAP_SOURCE_OSM_MAPNIK);
 
-  source_chain = champlain_map_source_chain_new ();
-
-  tile_size = champlain_map_source_get_tile_size(source);
-  src = CHAMPLAIN_MAP_SOURCE(champlain_error_tile_source_new_full (tile_size));
-
-  champlain_map_source_chain_push_map_source(source_chain, src);
-  champlain_map_source_chain_push_map_source(source_chain, source);
-
-  cache_path = g_build_path (G_DIR_SEPARATOR_S, g_get_user_cache_dir (), "champlain", NULL);
-  src = CHAMPLAIN_MAP_SOURCE(champlain_file_cache_new_full (100000000, cache_path, TRUE));
-  g_free(cache_path);
-
-  champlain_map_source_chain_push_map_source(source_chain, src);
-
-  priv->map_source = CHAMPLAIN_MAP_SOURCE(source_chain);
+  priv->map_source = CHAMPLAIN_MAP_SOURCE(source);
 
   priv->zoom_level = 0;
   priv->min_zoom_level = champlain_map_source_get_min_zoom_level (priv->map_source);
diff --git a/demos/launcher-gtk.c b/demos/launcher-gtk.c
index 20b561e..db66f0b 100644
--- a/demos/launcher-gtk.c
+++ b/demos/launcher-gtk.c
@@ -75,11 +75,6 @@ map_source_changed (GtkWidget *widget,
   ChamplainMapSource *source;
   GtkTreeIter iter;
   GtkTreeModel *model;
-  ChamplainMapSourceChain *source_chain;
-  ChamplainMapSource *src;
-  guint tile_size;
-  gchar *cache_path;
-
 
   if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (widget), &iter))
     return;
@@ -91,21 +86,7 @@ map_source_changed (GtkWidget *widget,
   ChamplainMapSourceFactory *factory = champlain_map_source_factory_dup_default ();
   source = champlain_map_source_factory_create (factory, id);
 
-  source_chain = champlain_map_source_chain_new ();
-
-  tile_size = champlain_map_source_get_tile_size(source);
-  src = CHAMPLAIN_MAP_SOURCE(champlain_error_tile_source_new_full (tile_size));
-
-  champlain_map_source_chain_push_map_source(source_chain, src);
-  champlain_map_source_chain_push_map_source(source_chain, source);
-
-  cache_path = g_build_path (G_DIR_SEPARATOR_S, g_get_user_cache_dir (), "champlain", NULL);
-  src = CHAMPLAIN_MAP_SOURCE(champlain_file_cache_new_full (100000000, cache_path, TRUE));
-  g_free(cache_path);
-
-  champlain_map_source_chain_push_map_source(source_chain, src);
-
-  g_object_set (G_OBJECT (view), "map-source", source_chain, NULL);
+  g_object_set (G_OBJECT (view), "map-source", source, NULL);
   g_object_unref (factory);
 }
 
diff --git a/demos/local-rendering.c b/demos/local-rendering.c
index 971fb79..dfeeb52 100644
--- a/demos/local-rendering.c
+++ b/demos/local-rendering.c
@@ -428,7 +428,6 @@ map_source_changed (GtkWidget *widget, ChamplainView *view)
       ChamplainMapSourceChain *source_chain;
       ChamplainMapSource *src;
       guint tile_size;
-      gchar *cache_path;
 
       if (g_strcmp0 (id, "memphis-local") == 0)
         {
@@ -465,14 +464,12 @@ map_source_changed (GtkWidget *widget, ChamplainView *view)
       tile_size = champlain_map_source_get_tile_size(tile_source);
       src = CHAMPLAIN_MAP_SOURCE(champlain_error_tile_source_new_full (tile_size));
 
-      champlain_map_source_chain_push_map_source(source_chain, src);
-      champlain_map_source_chain_push_map_source(source_chain, tile_source);
+      champlain_map_source_chain_push(source_chain, src);
+      champlain_map_source_chain_push(source_chain, tile_source);
 
-      cache_path = g_build_path (G_DIR_SEPARATOR_S, g_get_user_cache_dir (), "champlain", NULL);
-      src = CHAMPLAIN_MAP_SOURCE(champlain_file_cache_new_full (100000000, cache_path, FALSE));
-      g_free(cache_path);
+      src = CHAMPLAIN_MAP_SOURCE(champlain_file_cache_new_full (100000000, NULL, FALSE));
 
-      champlain_map_source_chain_push_map_source(source_chain, src);
+      champlain_map_source_chain_push(source_chain, src);
 
       g_object_set (G_OBJECT (view), "map-source", source_chain, NULL);
       if (strncmp (id, "memphis", 7) == 0)



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