]> git.street.me.uk Git - andy/viking.git/blobdiff - src/vikmapniklayer.c
Fix memory leak on re-downloading 'bad' map image tiles.
[andy/viking.git] / src / vikmapniklayer.c
index 449f0b5cbd673f3f4bb70a184b1bdc063c2cc22e..dc8c555897034d0d3046695f925eb19a1114abcd 100644 (file)
@@ -24,7 +24,6 @@
 #endif
 
 #include "viking.h"
-#include "vikutils.h"
 #include <glib.h>
 #include <glib/gstdio.h>
 #include <glib/gi18n.h>
 
 #include "vikmapslayer.h"
 
-#if !GLIB_CHECK_VERSION(2,26,0)
-typedef struct stat GStatBuf;
-#endif
-
 struct _VikMapnikLayerClass
 {
        VikLayerClass object_class;
@@ -123,7 +118,8 @@ static gboolean mapnik_feature_release ( VikMapnikLayer *vml, GdkEventButton *ev
 static VikToolInterface mapnik_tools[] = {
        // Layer Info
        // Zoom All?
-  { { "MapnikFeatures", GTK_STOCK_INFO, N_("_Mapnik Features"), NULL, N_("Mapnik Features"), 0 },
+  { NULL,
+    { "MapnikFeatures", GTK_STOCK_INFO, N_("_Mapnik Features"), NULL, N_("Mapnik Features"), 0 },
     (VikToolConstructorFunc) mapnik_feature_create,
     NULL,
     NULL,
@@ -163,6 +159,8 @@ VikLayerInterface vik_mapnik_layer_interface = {
        (VikLayerFuncDraw)                    mapnik_layer_draw,
        (VikLayerFuncChangeCoordMode)         NULL,
 
+       (VikLayerFuncGetTimestamp)            NULL,
+
        (VikLayerFuncSetMenuItemsSelection)   NULL,
        (VikLayerFuncGetMenuItemsSelection)   NULL,
 
@@ -229,8 +227,10 @@ static VikLayerParamData plugins_default ( void )
 #else
        if ( g_file_test ( "/usr/lib/mapnik/input", G_FILE_TEST_EXISTS ) )
                data.s = g_strdup ( "/usr/lib/mapnik/input" );
+               // Current Debian locations
+       else if ( g_file_test ( "/usr/lib/mapnik/3.0/input", G_FILE_TEST_EXISTS ) )
+               data.s = g_strdup ( "/usr/lib/mapnik/3.0/input" );
        else if ( g_file_test ( "/usr/lib/mapnik/2.2/input", G_FILE_TEST_EXISTS ) )
-               // Current Debian location
                data.s = g_strdup ( "/usr/lib/mapnik/2.2/input" );
        else
                data.s = g_strdup ( "" );
@@ -270,11 +270,11 @@ static GHashTable *requests = NULL;
 /**
  * vik_mapnik_layer_init:
  *
- * Mostly to initialize preferences
+ * Just initialize preferences
  */
 void vik_mapnik_layer_init (void)
 {
-       a_preferences_register_group ( MAPNIK_PREFS_GROUP_KEY, "Mapnik" );
+       a_preferences_register_group ( MAPNIK_PREFS_GROUP_KEY, _("Mapnik") );
 
        guint i = 0;
        VikLayerParamData tmp = plugins_default();
@@ -291,7 +291,15 @@ void vik_mapnik_layer_init (void)
 
        tmp.s = "carto";
        a_preferences_register(&prefs[i++], tmp, MAPNIK_PREFS_GROUP_KEY);
+}
 
+/**
+ * vik_mapnik_layer_post_init:
+ *
+ * Initialize data structures - now that reading preferences is OK to perform
+ */
+void vik_mapnik_layer_post_init (void)
+{
        tp_mutex = vik_mutex_new();
 
        // Just storing keys only
@@ -323,9 +331,14 @@ void vik_mapnik_layer_uninit ()
 // NB Only performed once per program run
 static void mapnik_layer_class_init ( VikMapnikLayerClass *klass )
 {
-       mapnik_interface_initialize ( a_preferences_get (MAPNIK_PREFS_NAMESPACE"plugins_directory")->s,
-                                     a_preferences_get (MAPNIK_PREFS_NAMESPACE"fonts_directory")->s,
-                                     a_preferences_get (MAPNIK_PREFS_NAMESPACE"recurse_fonts_directory")->b );
+       VikLayerParamData *pd = a_preferences_get (MAPNIK_PREFS_NAMESPACE"plugins_directory");
+       VikLayerParamData *fd = a_preferences_get (MAPNIK_PREFS_NAMESPACE"fonts_directory");
+       VikLayerParamData *rfd = a_preferences_get (MAPNIK_PREFS_NAMESPACE"recurse_fonts_directory");
+
+       if ( pd && fd && rfd )
+               mapnik_interface_initialize ( pd->s, fd->s, rfd->b );
+       else
+               g_critical ( "Unable to initialize mapnik interface from preferences" );
 }
 
 GType vik_mapnik_layer_get_type ()
@@ -492,6 +505,7 @@ gboolean carto_load ( VikMapnikLayer *vml, VikViewport *vvp )
                gchar *msg = g_strdup_printf ( "%s: %s", _("Running"), command );
                vik_window_statusbar_update ( vw, msg, VIK_STATUSBAR_INFO );
                vik_window_set_busy_cursor ( vw );
+               g_free ( msg );
        }
 
        gint64 tt1 = 0;
@@ -526,6 +540,7 @@ gboolean carto_load ( VikMapnikLayer *vml, VikViewport *vvp )
                                if ( !g_strcmp0 ( vml->filename_xml, vml->filename_css ) ) {
                                        vml->filename_xml = g_strconcat ( vml->filename_css, ".xml", NULL );
                                }
+                               g_regex_unref ( regex );
                        }
                        if ( !g_file_set_contents (vml->filename_xml, mystdout, -1, &error)  ) {
                                g_warning ("%s: %s", __FUNCTION__, error->message );
@@ -620,7 +635,8 @@ static void possibly_save_pixbuf ( VikMapnikLayer *vml, GdkPixbuf *pixbuf, MapCo
 
                        gchar *dir = g_path_get_dirname ( filename );
                        if ( !g_file_test ( filename, G_FILE_TEST_EXISTS ) )
-                               g_mkdir_with_parents ( dir , 0777 );
+                               if ( g_mkdir_with_parents ( dir , 0777 ) != 0 )
+                                       g_warning ("%s: Failed to mkdir %s", __FUNCTION__, dir );
                        g_free ( dir );
 
                        if ( !gdk_pixbuf_save (pixbuf, filename, "png", &error, NULL ) ) {
@@ -661,8 +677,9 @@ static void render ( VikMapnikLayer *vml, VikCoord *ul, VikCoord *br, MapCoord *
 
        // NB Mapnik can apply alpha, but use our own function for now
        if ( vml->alpha < 255 )
-               pixbuf = ui_pixbuf_set_alpha ( pixbuf, vml->alpha );
+               pixbuf = ui_pixbuf_scale_alpha ( pixbuf, vml->alpha );
        a_mapcache_add ( pixbuf, (mapcache_extra_t){ tt }, ulm->x, ulm->y, ulm->z, MAP_ID_MAPNIK_RENDER, ulm->scale, vml->alpha, 0.0, 0.0, vml->filename_xml );
+       g_object_unref(pixbuf);
 }
 
 static void render_info_free ( RenderInfo *data )
@@ -699,7 +716,7 @@ static void render_cancel_cleanup (RenderInfo *data)
 /**
  * Thread
  */
-void thread_add (VikMapnikLayer *vml, MapCoord *mul, VikCoord *ul, VikCoord *br, gint x, gint y, gint z, gint zoom, const gchar* name )
+static void thread_add (VikMapnikLayer *vml, MapCoord *mul, VikCoord *ul, VikCoord *br, gint x, gint y, gint z, gint zoom, const gchar* name )
 {
        // Create request
        guint nn = name ? g_str_hash ( name ) : 0;
@@ -743,6 +760,9 @@ void thread_add (VikMapnikLayer *vml, MapCoord *mul, VikCoord *ul, VikCoord *br,
 
 /**
  * load_pixbuf:
+ *
+ * If function returns GdkPixbuf properly, reference counter to this
+ * buffer has to be decreased, when buffer is no longer needed.
  */
 static GdkPixbuf *load_pixbuf ( VikMapnikLayer *vml, MapCoord *ulm, MapCoord *brm, gboolean *rerender )
 {
@@ -775,7 +795,8 @@ static GdkPixbuf *load_pixbuf ( VikMapnikLayer *vml, MapCoord *ulm, MapCoord *br
 }
 
 /**
- *
+ * Caller has to decrease reference counter of returned
+ * GdkPixbuf, when buffer is no longer needed.
  */
 static GdkPixbuf *get_pixbuf ( VikMapnikLayer *vml, MapCoord *ulm, MapCoord *brm )
 {
@@ -862,6 +883,7 @@ static void mapnik_layer_draw ( VikMapnikLayer *vml, VikViewport *vvp )
                                        map_utils_iTMS_to_vikcoord ( &ulm, &coord );
                                        vik_viewport_coord_to_screen ( vvp, &coord, &xx, &yy );
                                        vik_viewport_draw_pixbuf ( vvp, pixbuf, 0, 0, xx, yy, vml->tile_size_x, vml->tile_size_x );
+                                       g_object_unref(pixbuf);
                                }
                        }
                }
@@ -1072,7 +1094,7 @@ static void mapnik_layer_tile_info ( VikMapnikLayer *vml )
        if ( g_file_test ( filename, G_FILE_TEST_EXISTS ) ) {
                filemsg = g_strconcat ( "Tile File: ", filename, NULL );
                // Get some timestamp information of the tile
-               struct stat stat_buf;
+               GStatBuf stat_buf;
                if ( g_stat ( filename, &stat_buf ) == 0 ) {
                        gchar time_buf[64];
                        strftime ( time_buf, sizeof(time_buf), "%c", gmtime((const time_t *)&stat_buf.st_mtime) );