]> git.street.me.uk Git - andy/viking.git/blobdiff - src/acquire.c
Improved feedback/feature availability when no direction routing engines are available.
[andy/viking.git] / src / acquire.c
index 96d379e696a077c1f45591422b8c52672d2bd7aa..0e10c1ab3c37b409f2b0571298cba968caae9bce 100644 (file)
@@ -2,7 +2,7 @@
  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
  *
  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
- * Copyright (C) 2013, Rob Norris <rw_norris@hotmail.com>
+ * Copyright (C) 2013-2015, 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
@@ -38,7 +38,9 @@
 
 /*** Input is TRWLayer ***/
 extern VikDataSourceInterface vik_datasource_bfilter_simplify_interface;
+extern VikDataSourceInterface vik_datasource_bfilter_compress_interface;
 extern VikDataSourceInterface vik_datasource_bfilter_dup_interface;
+extern VikDataSourceInterface vik_datasource_bfilter_manual_interface;
 
 /*** Input is a track and a TRWLayer ***/
 extern VikDataSourceInterface vik_datasource_bfilter_polygon_interface;
@@ -48,7 +50,9 @@ extern VikDataSourceInterface vik_datasource_bfilter_exclude_polygon_interface;
 
 const VikDataSourceInterface *filters[] = {
   &vik_datasource_bfilter_simplify_interface,
+  &vik_datasource_bfilter_compress_interface,
   &vik_datasource_bfilter_dup_interface,
+  &vik_datasource_bfilter_manual_interface,
   &vik_datasource_bfilter_polygon_interface,
   &vik_datasource_bfilter_exclude_polygon_interface,
 };
@@ -62,11 +66,10 @@ VikTrack *filter_track = NULL;
 /* passed along to worker thread */
 typedef struct {
   acq_dialog_widgets_t *w;
-  gchar *cmd;
-  gchar *extra;
+  ProcessOptions *po;
   gboolean creating_new_layer;
   VikTrwLayer *vtl;
-  gpointer options;
+  DownloadFileOptions *options;
 } w_and_interface_t;
 
 
@@ -106,15 +109,11 @@ static void on_complete_process (w_and_interface_t *wi)
       // TODO: create function for this operation to hide detail:
       if ( ! vik_trw_layer_is_empty ( wi->vtl ) ) {
         vik_layer_post_read ( VIK_LAYER(wi->vtl), wi->w->vvp, TRUE );
-        vik_aggregate_layer_add_layer( vik_layers_panel_get_top_layer(wi->w->vlp), VIK_LAYER(wi->vtl));
+        vik_aggregate_layer_add_layer ( vik_layers_panel_get_top_layer(wi->w->vlp), VIK_LAYER(wi->vtl), TRUE );
       }
       else
         gtk_label_set_text ( GTK_LABEL(wi->w->status), _("No data.") );
     }
-    /* View this data if available and is desired */
-    if ( wi->vtl && wi->w->source_interface->autoview ) {
-      vik_trw_layer_auto_set_view ( wi->vtl, vik_layers_panel_get_viewport(wi->w->vlp) );
-    }
     if ( wi->w->source_interface->keep_dialog_open ) {
       gtk_dialog_set_response_sensitive ( GTK_DIALOG(wi->w->dialog), GTK_RESPONSE_ACCEPT, TRUE );
       gtk_dialog_set_response_sensitive ( GTK_DIALOG(wi->w->dialog), GTK_RESPONSE_REJECT, FALSE );
@@ -122,8 +121,14 @@ static void on_complete_process (w_and_interface_t *wi)
       gtk_dialog_response ( GTK_DIALOG(wi->w->dialog), GTK_RESPONSE_ACCEPT );
     }
     // Main display update
-    if ( wi->vtl )
+    if ( wi->vtl ) {
+      vik_layer_post_read ( VIK_LAYER(wi->vtl), wi->w->vvp, TRUE );
+      // View this data if desired - must be done after post read (so that the bounds are known)
+      if ( wi->w->source_interface->autoview ) {
+       vik_trw_layer_auto_set_view ( wi->vtl, vik_layers_panel_get_viewport(wi->w->vlp) );
+      }
       vik_layers_panel_emit_update ( wi->w->vlp );
+    }
   } else {
     /* cancelled */
     if ( wi->creating_new_layer )
@@ -131,20 +136,30 @@ static void on_complete_process (w_and_interface_t *wi)
   }
 }
 
+static void free_process_options ( ProcessOptions *po )
+{
+  if ( po ) {
+    g_free ( po->babelargs );
+    g_free ( po->filename );
+    g_free ( po->input_file_type );
+    g_free ( po->babel_filters );
+    g_free ( po->url );
+    g_free ( po->shell_command );
+    g_free ( po );
+  }
+}
+
 /* this routine is the worker thread.  there is only one simultaneous download allowed */
 static void get_from_anything ( w_and_interface_t *wi )
 {
-  gchar *cmd = wi->cmd;
-  gchar *extra = wi->extra;
   gboolean result = TRUE;
 
   VikDataSourceInterface *source_interface = wi->w->source_interface;
 
-  if ( source_interface->process_func )
-    result = source_interface->process_func ( wi->vtl, cmd, extra, (BabelStatusFunc) progress_func, wi->w, wi->options );
-
-  g_free ( cmd );
-  g_free ( extra );
+  if ( source_interface->process_func ) {
+    result = source_interface->process_func ( wi->vtl, wi->po, (BabelStatusFunc)progress_func, wi->w, wi->options );
+  }
+  free_process_options ( wi->po );
   g_free ( wi->options );
 
   if (wi->w->running && !result) {
@@ -175,54 +190,33 @@ static void get_from_anything ( w_and_interface_t *wi )
   g_thread_exit ( NULL );
 }
 
-
-static gchar *write_tmp_trwlayer ( VikTrwLayer *vtl )
-{
-  int fd_src;
-  gchar *name_src;
-  FILE *f;
-  g_assert ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0);
-  g_debug ("%s: temporary file: %s", __FUNCTION__, name_src);
-  f = fdopen(fd_src, "w");
-  a_gpx_write_file(vtl, f, NULL);
-  fclose(f);
-  f = NULL;
-  return name_src;
-}
-
-/* TODO: write with name of old track */
-static gchar *write_tmp_track ( VikTrack *track )
-{
-  int fd_src;
-  gchar *name_src;
-  FILE *f;
-  g_assert ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0);
-  g_debug ("%s: temporary file: %s", __FUNCTION__, name_src);
-  f = fdopen(fd_src, "w");
-  a_gpx_write_track_file(track, f, NULL); /* Thank you Guilhem! Just when I needed this function... -- Evan */
-  fclose(f);
-  f = NULL;
-  return name_src;
-}
-
-/* TODO: cleanup, getr rid of redundancy */
-
 /* depending on type of filter, often only vtl or track will be given.
  * the other can be NULL.
  */
-static void acquire ( VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikDataSourceInterface *source_interface,
-                     VikTrwLayer *vtl, VikTrack *track )
+static void acquire ( VikWindow *vw,
+                      VikLayersPanel *vlp,
+                      VikViewport *vvp,
+                      vik_datasource_mode_t mode,
+                      VikDataSourceInterface *source_interface,
+                      VikTrwLayer *vtl,
+                      VikTrack *track,
+                      gpointer userdata,
+                      VikDataSourceCleanupFunc cleanup_function )
 {
   /* for manual dialogs */
   GtkWidget *dialog = NULL;
   GtkWidget *status;
-  gchar *cmd = NULL;
-  gchar *extra = NULL;
-  gchar *cmd_off = NULL;
-  gchar *extra_off = NULL;
+  gchar *args_off = NULL;
+  gchar *fd_off = NULL;
   acq_dialog_widgets_t *w;
   gpointer user_data;
-  gpointer options = NULL;
+  DownloadFileOptions *options = g_malloc0 ( sizeof(DownloadFileOptions) );
+
+  acq_vik_t avt;
+  avt.vlp = vlp;
+  avt.vvp = vvp;
+  avt.vw = vw;
+  avt.userdata = userdata;
 
   /* for UI builder */
   gpointer pass_along_data;
@@ -232,7 +226,7 @@ static void acquire ( VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikD
 
   /*** INIT AND CHECK EXISTENCE ***/
   if ( source_interface->init_func )
-    user_data = source_interface->init_func();
+    user_data = source_interface->init_func(&avt);
   else
     user_data = NULL;
   pass_along_data = user_data;
@@ -283,38 +277,40 @@ static void acquire ( VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikD
       return; /* TODO: do we have to free anything here? */
   }
 
-  /* CREATE INPUT DATA & GET COMMAND STRING */
+  /* CREATE INPUT DATA & GET OPTIONS */
+  ProcessOptions *po = g_malloc0 ( sizeof(ProcessOptions) );
 
   if ( source_interface->inputtype == VIK_DATASOURCE_INPUTTYPE_TRWLAYER ) {
-    gchar *name_src = write_tmp_trwlayer ( vtl );
+    gchar *name_src = a_gpx_write_tmp_file ( vtl, NULL );
 
-    ((VikDataSourceGetCmdStringFuncWithInput) source_interface->get_cmd_string_func)
-       ( pass_along_data, &cmd, &extra, name_src );
+    source_interface->get_process_options_func ( pass_along_data, po, NULL, name_src, NULL );
+
+    util_add_to_deletion_list ( name_src );
 
     g_free ( name_src );
-    /* TODO: delete the tmp file? or delete it only after we're done with it? */
   } else if ( source_interface->inputtype == VIK_DATASOURCE_INPUTTYPE_TRWLAYER_TRACK ) {
-    gchar *name_src = write_tmp_trwlayer ( vtl );
-    gchar *name_src_track = write_tmp_track ( track );
+    gchar *name_src = a_gpx_write_tmp_file ( vtl, NULL );
+    gchar *name_src_track = a_gpx_write_track_tmp_file ( track, NULL );
+
+    source_interface->get_process_options_func ( pass_along_data, po, NULL, name_src, name_src_track );
 
-    ((VikDataSourceGetCmdStringFuncWithInputInput) source_interface->get_cmd_string_func)
-       ( pass_along_data, &cmd, &extra, name_src, name_src_track );
+    util_add_to_deletion_list ( name_src );
+    util_add_to_deletion_list ( name_src_track );
 
     g_free ( name_src );
     g_free ( name_src_track );
   } else if ( source_interface->inputtype == VIK_DATASOURCE_INPUTTYPE_TRACK ) {
-    gchar *name_src_track = write_tmp_track ( track );
+    gchar *name_src_track = a_gpx_write_track_tmp_file ( track, NULL );
 
-    ((VikDataSourceGetCmdStringFuncWithInput) source_interface->get_cmd_string_func)
-       ( pass_along_data, &cmd, &extra, name_src_track );
+    source_interface->get_process_options_func ( pass_along_data, po, NULL, NULL, name_src_track );
 
     g_free ( name_src_track );
-  } else if ( source_interface->get_cmd_string_func )
-    source_interface->get_cmd_string_func ( pass_along_data, &cmd, &extra, &options );
+  } else if ( source_interface->get_process_options_func )
+    source_interface->get_process_options_func ( pass_along_data, po, options, NULL, NULL );
 
   /* Get data for Off command */
   if ( source_interface->off_func ) {
-    source_interface->off_func ( pass_along_data, &cmd_off, &extra_off );
+    source_interface->off_func ( pass_along_data, &args_off, &fd_off );
   }
 
   /* cleanup for option dialogs */
@@ -329,11 +325,10 @@ static void acquire ( VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikD
   wi = g_malloc(sizeof(*wi));
   wi->w = w;
   wi->w->source_interface = source_interface;
-  wi->cmd = cmd;
-  wi->extra = extra; /* usually input data type (?) */
+  wi->po = po;
   wi->options = options;
   wi->vtl = vtl;
-  wi->creating_new_layer = (!vtl);
+  wi->creating_new_layer = (!vtl); // Default if Auto Layer Management is passed in
 
   dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
   gtk_dialog_set_response_sensitive ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT, FALSE );
@@ -342,7 +337,7 @@ static void acquire ( VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikD
   w->dialog = dialog;
   w->running = TRUE;
   status = gtk_label_new (_("Working..."));
-  gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), status, FALSE, FALSE, 5 );
+  gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), status, FALSE, FALSE, 5 );
   gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
   // May not want to see the dialog at all
   if ( source_interface->is_thread || source_interface->keep_dialog_open )
@@ -357,14 +352,17 @@ static void acquire ( VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikD
   }
   w->user_data = user_data;
 
-  if (source_interface->mode == VIK_DATASOURCE_ADDTOLAYER) {
+  if ( mode == VIK_DATASOURCE_ADDTOLAYER ) {
     VikLayer *current_selected = vik_layers_panel_get_selected ( w->vlp );
     if ( IS_VIK_TRW_LAYER(current_selected) ) {
       wi->vtl = VIK_TRW_LAYER(current_selected);
       wi->creating_new_layer = FALSE;
     }
   }
-  else if ( source_interface->mode == VIK_DATASOURCE_MANUAL_LAYER_MANAGEMENT ) {
+  else if ( mode == VIK_DATASOURCE_CREATENEWLAYER ) {
+    wi->creating_new_layer = TRUE;
+  }
+  else if ( mode == VIK_DATASOURCE_MANUAL_LAYER_MANAGEMENT ) {
     // Don't create in acquire - as datasource will perform the necessary actions
     wi->creating_new_layer = FALSE;
     VikLayer *current_selected = vik_layers_panel_get_selected ( w->vlp );
@@ -372,26 +370,31 @@ static void acquire ( VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikD
       wi->vtl = VIK_TRW_LAYER(current_selected);
   }
   if ( wi->creating_new_layer ) {
-    wi->vtl = VIK_TRW_LAYER ( vik_layer_create ( VIK_LAYER_TRW, w->vvp, NULL, FALSE ) );
+    wi->vtl = VIK_TRW_LAYER ( vik_layer_create ( VIK_LAYER_TRW, w->vvp, FALSE ) );
     vik_layer_rename ( VIK_LAYER ( wi->vtl ), _(source_interface->layer_title) );
   }
 
   if ( source_interface->is_thread ) {
-    if ( cmd ) {
-      g_thread_create((GThreadFunc)get_from_anything, wi, FALSE, NULL );
+    if ( po->babelargs || po->url || po->shell_command ) {
+#if GLIB_CHECK_VERSION (2, 32, 0)
+      g_thread_try_new ( "get_from_anything", (GThreadFunc)get_from_anything, wi, NULL );
+#else
+      g_thread_create ( (GThreadFunc)get_from_anything, wi, FALSE, NULL );
+#endif
       gtk_dialog_run ( GTK_DIALOG(dialog) );
       if (w->running) {
         // Cancel and mark for thread to finish
         w->running = FALSE;
         // NB Thread will free memory
       } else {
-        if ( cmd_off ) {
+        if ( args_off ) {
           /* Turn off */
-          a_babel_convert_from (NULL, cmd_off, extra_off, NULL, NULL, NULL);
-          g_free ( cmd_off );
+          ProcessOptions off_po = { args_off, fd_off, NULL, NULL, NULL };
+          a_babel_convert_from (NULL, &off_po, NULL, NULL, NULL);
+          g_free ( args_off );
         }
-        if ( extra_off )
-          g_free ( extra_off );
+        if ( fd_off )
+          g_free ( fd_off );
 
         // Thread finished by normal completion - free memory
         g_free ( w );
@@ -407,12 +410,11 @@ static void acquire ( VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikD
   else {
     // bypass thread method malarkly - you'll just have to wait...
     if ( source_interface->process_func ) {
-      gboolean result = source_interface->process_func ( wi->vtl, cmd, extra, (BabelStatusFunc) progress_func, w, options );
+      gboolean result = source_interface->process_func ( wi->vtl, po, (BabelStatusFunc) progress_func, w, options );
       if ( !result )
         a_dialog_msg ( GTK_WINDOW(vw), GTK_MESSAGE_ERROR, _("Error: acquisition failed."), NULL );
     }
-    g_free ( cmd );
-    g_free ( extra );
+    free_process_options ( po );
     g_free ( options );
 
     on_complete_process ( wi );
@@ -425,15 +427,32 @@ static void acquire ( VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikD
   }
 
   gtk_widget_destroy ( dialog );
+
+  if ( cleanup_function )
+    cleanup_function ( source_interface );
 }
 
 /**
  * a_acquire:
+ * @vw: The #VikWindow to work with
+ * @vlp: The #VikLayersPanel in which a #VikTrwLayer layer may be created/appended
+ * @vvp: The #VikViewport defining the current view
+ * @mode: How layers should be managed
+ * @source_interface: The #VikDataSourceInterface determining how and what actions to take
+ * @userdata: External data to be passed into the #VikDataSourceInterface
+ * @cleanup_function: The function to dispose the #VikDataSourceInterface if necessary
  *
  * Process the given VikDataSourceInterface for sources with no input data.
  */
-void a_acquire ( VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikDataSourceInterface *source_interface ) {
-  acquire ( vw, vlp, vvp, source_interface, NULL, NULL );
+void a_acquire ( VikWindow *vw,
+                 VikLayersPanel *vlp,
+                 VikViewport *vvp,
+                 vik_datasource_mode_t mode,
+                 VikDataSourceInterface *source_interface,
+                 gpointer userdata,
+                 VikDataSourceCleanupFunc cleanup_function )
+{
+  acquire ( vw, vlp, vvp, mode, source_interface, NULL, NULL, userdata, cleanup_function );
 }
 
 static void acquire_trwlayer_callback ( GObject *menuitem, gpointer *pass_along )
@@ -445,7 +464,7 @@ static void acquire_trwlayer_callback ( GObject *menuitem, gpointer *pass_along
   VikTrwLayer *vtl =   pass_along[3];
   VikTrack *tr =       pass_along[4];
 
-  acquire ( vw, vlp, vvp, iface, vtl, tr );
+  acquire ( vw, vlp, vvp, iface->mode, iface, vtl, tr, NULL, NULL );
 }
 
 static GtkWidget *acquire_build_menu ( VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp,