When downloading waypoints from places like geocaching.com, the waypoints all
have the same file name. I wanted a way to have them automatically be imported
into a chosen track/waypoint layer so I could just quickly download files
without have to rename them and manually append them later. I wrote a small
addition to viking to do that. I'm posting it here in case anyone is
interested. Attached is a diff against the git repo in sf.
Eric
If a patch is not ok for this single commit, I can figure out the public git
repo method.
diff --git a/src/dialog.c b/src/dialog.c
index 6413555..82a5a1e 100644
--- a/src/dialog.c
+++ b/src/dialog.c
@@ -913,3 +913,64 @@ void a_dialog_license ( GtkWindow *parent, const gchar *map, const gchar *licens
} while (response != GTK_RESPONSE_DELETE_EVENT && response != GTK_RESPONSE_OK);
gtk_widget_destroy (dialog);
}
+
+
+gint a_dialog_auto_import ( GtkWindow *parent, gboolean enable_stop, gchar **dir, gchar **patterns)
+{
+ gint resp;
+ gint ret = 0;
+ GtkWidget *dialog = gtk_dialog_new_with_buttons (_("Auto Import"),
+ parent,
+ GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
+ GTK_STOCK_CANCEL,
+ GTK_RESPONSE_CANCEL,
+ GTK_STOCK_STOP,
+ GTK_RESPONSE_NO,
+ GTK_STOCK_OK,
+ GTK_RESPONSE_ACCEPT,
+ NULL);
+ GtkWidget *dirlabel, *direntry, *patlabel, *patentry, *warninglabel;
+
+ gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), GTK_RESPONSE_NO, enable_stop);
+
+ if (*patterns == NULL)
+ *patterns = g_strdup("*.gpx;*.loc");
+
+ dirlabel = gtk_label_new (_("Directory:"));
+ direntry = gtk_entry_new ();
+ patlabel = gtk_label_new (_("Patterns:"));
+ patentry = gtk_entry_new ();
+ warninglabel = gtk_label_new(_("\nWarning:\nthis will delete files after importing!\n"));
+
+ g_signal_connect_swapped ( direntry, "activate", G_CALLBACK(a_dialog_response_accept), GTK_DIALOG(dialog) );
+ direntry = gtk_entry_new ();
+
+ gtk_entry_set_text ( GTK_ENTRY(direntry), *dir);
+ gtk_entry_set_text ( GTK_ENTRY(patentry), *patterns);
+
+ gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), dirlabel, FALSE, FALSE, 0);
+ gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), direntry, FALSE, FALSE, 0);
+ gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), patlabel, FALSE, FALSE, 0);
+ gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), patentry, FALSE, FALSE, 0);
+ gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), warninglabel, FALSE, FALSE, 0);
+
+ gtk_widget_show_all ( GTK_DIALOG(dialog)->vbox );
+
+ resp = gtk_dialog_run ( GTK_DIALOG(dialog) );
+ if ( resp == GTK_RESPONSE_ACCEPT ) {
+ /* FIXME check that directory exits, and good stuff like that */
+ if (*dir)
+ g_free(*dir);
+ *dir = g_strdup(gtk_entry_get_text(GTK_ENTRY(direntry)));
+ if (*patterns)
+ g_free(*patterns);
+ *patterns = g_strdup(gtk_entry_get_text(GTK_ENTRY(patentry)));
+ ret = 1;
+ }
+ else if (resp == GTK_RESPONSE_NO) {
+ ret = -1;
+ }
+
+ gtk_widget_destroy ( dialog );
+ return ret;
+}
diff --git a/src/dialog.h b/src/dialog.h
index 74b2c87..1f23986 100644
--- a/src/dialog.h
+++ b/src/dialog.h
@@ -76,4 +76,6 @@ gboolean a_dialog_map_n_zoom(GtkWindow *parent, gchar *mapnames[], gint default_
GList *a_dialog_select_from_list ( GtkWindow *parent, GList *names, gboolean multiple_selection_allowed, const gchar *title, const gchar *msg );
void a_dialog_license ( GtkWindow *parent, const gchar *map, const gchar *license, const gchar *url);
+
+gint a_dialog_auto_import ( GtkWindow *parent, gboolean enable_stop, gchar **dir, gchar **patterns);
#endif
diff --git a/src/file.c b/src/file.c
index 8d7ebd4..4cbf701 100644
--- a/src/file.c
+++ b/src/file.c
@@ -744,3 +744,44 @@ const gchar *a_get_viking_dir()
return viking_dir;
}
+
+
+gchar *a_get_import_dir()
+{
+ gchar *home = g_getenv("HOME");
+ if (!home || g_access(home, W_OK))
+ home = g_get_home_dir ();
+
+ return g_build_filename(home, "gpx", NULL);
+}
+
+
+VikLoadType_t a_file_import_to_layer ( VikTrwLayer *vtl, VikViewport *vp, const gchar *filename )
+{
+ gboolean is_gpx_file = check_file_ext ( filename, ".gpx" );
+ FILE *f = xfopen ( filename, "r" );
+
+ g_assert ( vp );
+
+ // In fact both kml & gpx files start the same as they are in xml
+ if ( check_file_ext ( filename, ".kml" ) && check_magic ( f, GPX_MAGIC ) ) {
+ // Implicit Conversion
+ if ( ! a_babel_convert_from ( VIK_TRW_LAYER(vtl), "-i kml", NULL, filename, NULL ) ) {
+ // Probably want to remove the vtl, but I'm not sure how yet...
+ xfclose(f);
+ return LOAD_TYPE_GPSBABEL_FAILURE;
+ }
+ }
+ else if ( is_gpx_file || check_magic ( f, GPX_MAGIC ) ) {
+ a_gpx_read_file ( VIK_TRW_LAYER(vtl), f );
+ }
+ else
+ a_gpspoint_read_file ( VIK_TRW_LAYER(vtl), f );
+
+ vik_layer_post_read ( vtl, vp, TRUE );
+
+ vik_trw_layer_auto_set_view ( VIK_TRW_LAYER(vtl), vp );
+
+ xfclose(f);
+ return LOAD_TYPE_OTHER_SUCCESS;
+}
diff --git a/src/file.h b/src/file.h
index e081558..321e01f 100644
--- a/src/file.h
+++ b/src/file.h
@@ -58,5 +58,7 @@ const gchar *a_get_viking_dir();
void file_write_layer_param ( FILE *f, const gchar *name, guint8 type, VikLayerParamData data );
+gchar *a_get_import_dir();
+VikLoadType_t a_file_import_to_layer ( VikTrwLayer *vtl, VikViewport *vp, const gchar *filename );
#endif
diff --git a/src/viktrwlayer.c b/src/viktrwlayer.c
index 3ff8108..1fdd430 100644
--- a/src/viktrwlayer.c
+++ b/src/viktrwlayer.c
@@ -48,6 +48,7 @@
#endif
#include "acquire.h"
#include "util.h"
+#include "file.h"
#include "icons/icons.h"
@@ -188,6 +189,10 @@ struct _VikTrwLayer {
VikStdLayerMenuItem menu_selection;
gint highest_wp_number;
+
+ gchar* auto_import_dir;
+ gchar* auto_import_patterns;
+ gboolean auto_import_on;
};
/* A caached waypoint image. */
@@ -291,6 +296,7 @@ static void trw_layer_cancel_last_tp ( VikTrwLayer *vtl );
static void trw_layer_cancel_current_tp ( VikTrwLayer *vtl, gboolean destroy );
static void trw_layer_tpwin_response ( VikTrwLayer *vtl, gint response );
static void trw_layer_tpwin_init ( VikTrwLayer *vtl );
+static void trw_layer_auto_import ( gpointer lav[2] );
static gpointer tool_edit_trackpoint_create ( VikWindow *vw, VikViewport *vvp);
static gboolean tool_edit_trackpoint_click ( VikTrwLayer *vtl, GdkEventButton *event, gpointer data );
@@ -2343,6 +2349,11 @@ void vik_trw_layer_add_menu_items ( VikTrwLayer *vtl, GtkMenu *menu, gpointer vl
item = gtk_menu_item_new_with_mnemonic ( _("Export as _KML...") );
g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(trw_layer_export_kml), pass_along );
gtk_menu_shell_append (GTK_MENU_SHELL (export_submenu), item);
+
+ gtk_widget_show ( item );
+ item = gtk_menu_item_new_with_mnemonic ( _("Auto Import") );
+ g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(trw_layer_auto_import), pass_along );
+ gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
gtk_widget_show ( item );
item = gtk_menu_item_new_with_mnemonic ( _("_New Waypoint...") );
@@ -5518,3 +5529,75 @@ static gchar *highest_wp_number_get(VikTrwLayer *vtl)
g_snprintf(buf,4,"%03d", vtl->highest_wp_number+1 );
return g_strdup(buf);
}
+
+
+static gboolean trw_layer_auto_import_timer(VikTrwLayer *vtl)
+{
+ GDir* dir;
+ VikWindow *vw = (VikWindow *)(VIK_GTK_WINDOW_FROM_LAYER(vtl));
+ VikViewport *vvp = vik_window_viewport(vw);
+ gchar** glob_list;
+
+ if (vtl->auto_import_on == FALSE)
+ return FALSE;
+ dir = g_dir_open(vtl->auto_import_dir, 0, NULL);
+ if (dir == NULL) {
+ /* FIXME signal user */
+ vtl->auto_import_on = FALSE;
+ return FALSE;
+ }
+
+ glob_list = g_strsplit(vtl->auto_import_patterns, ";", 0);
+ while (1) {
+ const gchar* filename = g_dir_read_name(dir);
+ gchar* full_path;
+
+ if (filename == NULL)
+ break;
+ else {
+ gchar** glob_list2 = glob_list;
+ gboolean match = FALSE;
+ while (*glob_list2 != NULL && !match) {
+ match = g_pattern_match_simple(*glob_list2, filename);
+ glob_list2++;
+ }
+ if (!match)
+ continue;
+ }
+
+ full_path = g_strconcat(vtl->auto_import_dir, "/", filename, NULL);
+ if (a_file_import_to_layer(vtl, vvp, full_path) == LOAD_TYPE_OTHER_SUCCESS)
+ g_unlink(full_path);
+ else {
+ vtl->auto_import_on = FALSE;
+ /* FIXME signal user */
+ }
+ g_free(full_path);
+ }
+ g_strfreev(glob_list);
+
+ g_dir_close(dir);
+ return TRUE;
+}
+
+
+static void trw_layer_auto_import ( gpointer lav[2] )
+{
+ VikTrwLayer *vtl = VIK_TRW_LAYER(lav[0]);
+ //VikLayersPanel *vlp = VIK_LAYERS_PANEL(lav[1]);
+ if (vtl->auto_import_dir == NULL)
+ vtl->auto_import_dir = a_get_import_dir();
+ gint ret = a_dialog_auto_import(VIK_GTK_WINDOW_FROM_LAYER(vtl),
+ vtl->auto_import_on,
+ &vtl->auto_import_dir,
+ &vtl->auto_import_patterns);
+ if (ret > 0) {
+ if (vtl->auto_import_on == FALSE) {
+ g_timeout_add_seconds(1, (GSourceFunc) trw_layer_auto_import_timer, vtl);
+ }
+ vtl->auto_import_on = TRUE;
+ }
+ else if (ret < 0) {
+ vtl->auto_import_on = FALSE;
+ }
+}
------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
Viking-devel mailing list
Viking-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/viking-devel
Viking home page: http://viking.sf.net/