]> git.street.me.uk Git - andy/viking.git/blobdiff - src/file.c
Fix excluding own self track when creating lists of other tracks.
[andy/viking.git] / src / file.c
index f3f9b4c0ce146a48199b9859a4c775d382f5511d..ae22df2c1f726bd89b7f7d4b44ba8ddd018abb82 100644 (file)
@@ -3,6 +3,7 @@
  *
  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
  * Copyright (C) 2012, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
+ * Copyright (C) 2012-2013, Rob Norris <rw_norris@hotmail.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -264,7 +265,7 @@ static void string_list_set_param (gint i, GList *list, gpointer *layer_and_vp)
  * TODO flow up line number(s) / error messages of problems encountered...
  *
  */
-static gboolean file_read ( VikAggregateLayer *top, FILE *f, VikViewport *vp )
+static gboolean file_read ( VikAggregateLayer *top, FILE *f, const gchar *dirpath, VikViewport *vp )
 {
   Stack *stack;
   struct LatLon ll = { 0.0, 0.0 };
@@ -383,7 +384,7 @@ static gboolean file_read ( VikAggregateLayer *top, FILE *f, VikViewport *vp )
         if ( stack->data && vik_layer_get_interface(VIK_LAYER(stack->data)->type)->read_file_data )
         {
           /* must read until hits ~EndLayerData */
-          if ( ! vik_layer_get_interface(VIK_LAYER(stack->data)->type)->read_file_data ( VIK_LAYER(stack->data), f ) )
+          if ( ! vik_layer_get_interface(VIK_LAYER(stack->data)->type)->read_file_data ( VIK_LAYER(stack->data), f, dirpath ) )
             successful_read = FALSE;
         }
         else
@@ -545,7 +546,7 @@ name=this
   }
 
   if ( ll.lat != 0.0 || ll.lon != 0.0 )
-    vik_viewport_set_center_latlon ( VIK_VIEWPORT(vp), &ll );
+    vik_viewport_set_center_latlon ( VIK_VIEWPORT(vp), &ll, TRUE );
 
   if ( ( ! VIK_LAYER(top)->visible ) && VIK_LAYER(top)->realized )
     vik_treeview_item_set_visible ( VIK_LAYER(top)->vt, &(VIK_LAYER(top)->iter), FALSE ); 
@@ -603,6 +604,44 @@ gboolean check_file_magic_vik ( const gchar *filename )
   return result;
 }
 
+/**
+ * append_file_ext:
+ *
+ * Append a file extension, if not already present.
+ *
+ * Returns: a newly allocated string
+ */
+gchar *append_file_ext ( const gchar *filename, VikLoadType_t type )
+{
+  gchar *new_name = NULL;
+  const gchar *ext = NULL;
+
+  /* Select an extension */
+  switch (type)
+  {
+  case FILE_TYPE_GPX:
+    ext = ".gpx";
+    break;
+  case FILE_TYPE_KML:
+    ext = ".kml";
+    break;
+  case FILE_TYPE_GPSMAPPER:
+  case FILE_TYPE_GPSPOINT:
+  default:
+    /* Do nothing, ext already set to NULL */
+    break;
+  }
+
+  /* Do */
+  if ( ext != NULL && ! check_file_ext ( filename, ext ) )
+    new_name = g_strconcat ( filename, ext, NULL );
+  else
+    /* Simply duplicate */
+    new_name = g_strdup ( filename );
+
+  return new_name;
+}
+
 VikLoadType_t a_file_load ( VikAggregateLayer *top, VikViewport *vp, const gchar *filename_or_uri )
 {
   g_return_val_if_fail ( vp != NULL, LOAD_TYPE_READ_FAILURE );
@@ -620,22 +659,13 @@ VikLoadType_t a_file_load ( VikAggregateLayer *top, VikViewport *vp, const gchar
   if ( ! f )
     return LOAD_TYPE_READ_FAILURE;
 
-  // Enables relative paths in a .vik file to work
-  // Also allows us to remember what directory we where using
-  gchar *dir = g_path_get_dirname ( filename );
-  if ( dir ) {
-    if ( g_chdir ( dir ) ) {
-      g_warning ( "Could not change directory to %s", dir );
-    }
-    g_free (dir);
-  }
-
   VikLoadType_t load_answer = LOAD_TYPE_OTHER_SUCCESS;
 
+  gchar *dirpath = g_path_get_dirname ( filename );
   // Attempt loading the primary file type first - our internal .vik file:
   if ( check_magic ( f, VIK_MAGIC ) )
   {
-    if ( file_read ( top, f, vp ) )
+    if ( file_read ( top, f, dirpath, vp ) )
       load_answer = LOAD_TYPE_VIK_SUCCESS;
     else
       load_answer = LOAD_TYPE_VIK_FAILURE_NON_FATAL;
@@ -665,11 +695,12 @@ VikLoadType_t a_file_load ( VikAggregateLayer *top, VikViewport *vp, const gchar
     }
     else {
       // Try final supported file type
-      if ( ! ( success = a_gpspoint_read_file ( VIK_TRW_LAYER(vtl), f ) ) ) {
-               // Failure here means we don't know how to handle the file
+      if ( ! ( success = a_gpspoint_read_file ( VIK_TRW_LAYER(vtl), f, dirpath ) ) ) {
+        // Failure here means we don't know how to handle the file
         load_answer = LOAD_TYPE_UNSUPPORTED_FAILURE;
-         }
+      }
     }
+    g_free ( dirpath );
 
     // Clean up when we can't handle the file
     if ( ! success ) {
@@ -700,7 +731,7 @@ gboolean a_file_save ( VikAggregateLayer *top, gpointer vp, const gchar *filenam
     return FALSE;
 
   // Enable relative paths in .vik files to work
-  // Also allows us to remember what directory we where using
+  gchar *cwd = g_get_current_dir();
   gchar *dir = g_path_get_dirname ( filename );
   if ( dir ) {
     if ( g_chdir ( dir ) ) {
@@ -711,6 +742,14 @@ gboolean a_file_save ( VikAggregateLayer *top, gpointer vp, const gchar *filenam
 
   file_write ( top, f, vp );
 
+  // Restore previous working directory
+  if ( cwd ) {
+    if ( g_chdir ( cwd ) ) {
+      g_warning ( "Could not return to directory %s", cwd );
+    }
+    g_free (cwd);
+  }
+
   fclose(f);
   f = NULL;
 
@@ -812,6 +851,22 @@ gboolean a_file_export ( VikTrwLayer *vtl, const gchar *filename, VikFileType_t
   return FALSE;
 }
 
+/**
+ * a_file_export_babel:
+ */
+gboolean a_file_export_babel ( VikTrwLayer *vtl, const gchar *filename, const gchar *format,
+                               gboolean tracks, gboolean routes, gboolean waypoints )
+{
+  gchar *args = g_strdup_printf("%s %s %s -o %s",
+                                tracks ? "-t" : "",
+                                routes ? "-r" : "",
+                                waypoints ? "-w" : "",
+                                format);
+  gboolean result = a_babel_convert_to ( vtl, NULL, args, filename, NULL, NULL );
+  g_free(args);
+  return result;
+}
+
 /**
  * Just a wrapper around realpath, which itself is platform dependent
  */
@@ -820,6 +875,24 @@ char *file_realpath ( const char *path, char *real )
   return realpath ( path, real );
 }
 
+#ifndef MAXPATHLEN
+#define MAXPATHLEN 1024
+#endif
+/**
+ * Always return the canonical filename in a newly allocated string
+ */
+char *file_realpath_dup ( const char *path )
+{
+       char real[MAXPATHLEN];
+
+       g_return_val_if_fail(path != NULL, NULL);
+
+       if (file_realpath(path, real))
+               return g_strdup(real);
+
+       return g_strdup(path);
+}
+
 /**
  * Permission granted to use this code after personal correspondance
  * Slightly reworked for better cross platform use, glibisms, function rename and a compacter format
@@ -831,10 +904,6 @@ char *file_realpath ( const char *path, char *real )
 // rfisher@iee.org
 // http://come.to/robfisher
 
-// defines
-#ifndef MAXPATHLEN
-#define MAXPATHLEN 1024
-#endif
 // The number of characters at the start of an absolute filename.  e.g. in DOS,
 // absolute filenames start with "X:\" so this value should be 3, in UNIX they start
 // with "\" so this value should be 1.