]> git.street.me.uk Git - andy/viking.git/blob - src/vikgoto.c
Use settings to save and restore the Goto Provider selected.
[andy/viking.git] / src / vikgoto.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5  * Copyright (C) 2009, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <glib.h>
29 #include <glib/gstdio.h>
30 #include <glib/gprintf.h>
31 #include <glib/gi18n.h>
32
33 #include "viking.h"
34 #include "util.h"
35 #include "vikgototool.h"
36 #include "vikgoto.h"
37
38 static gchar *last_goto_str = NULL;
39 static VikCoord *last_coord = NULL;
40 static gchar *last_successful_goto_str = NULL;
41
42 static GList *goto_tools_list = NULL;
43
44 #define VIK_SETTINGS_GOTO_PROVIDER "goto_provider"
45 int last_goto_tool = -1;
46
47 void vik_goto_register ( VikGotoTool *tool )
48 {
49   if ( IS_VIK_GOTO_TOOL( tool ) )
50     goto_tools_list = g_list_append ( goto_tools_list, g_object_ref ( tool ) );
51 }
52
53 void vik_goto_unregister_all ()
54 {
55   g_list_foreach ( goto_tools_list, (GFunc) g_object_unref, NULL );
56 }
57
58 gchar * a_vik_goto_get_search_string_for_this_place(VikWindow *vw)
59 {
60   if (!last_coord)
61     return NULL;
62
63   VikViewport *vvp = vik_window_viewport(vw);
64   const VikCoord *cur_center = vik_viewport_get_center(vvp);
65   if (vik_coord_equals(cur_center, last_coord)) {
66     return(last_successful_goto_str);
67   }
68   else
69     return NULL;
70 }
71
72 static void display_no_tool(VikWindow *vw)
73 {
74   GtkWidget *dialog = NULL;
75
76   dialog = gtk_message_dialog_new ( GTK_WINDOW(vw), GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("No goto tool available.") );
77
78   gtk_dialog_run ( GTK_DIALOG(dialog) );
79
80   gtk_widget_destroy(dialog);
81 }
82
83 static gboolean prompt_try_again(VikWindow *vw)
84 {
85   GtkWidget *dialog = NULL;
86   gboolean ret = TRUE;
87
88   dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
89   gtk_window_set_title(GTK_WINDOW(dialog), _("goto"));
90
91   GtkWidget *goto_label = gtk_label_new(_("I don't know that place. Do you want another goto?"));
92   gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), goto_label, FALSE, FALSE, 5 );
93   gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
94   gtk_widget_show_all(dialog);
95
96   if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT )
97     ret = FALSE;
98
99   gtk_widget_destroy(dialog);
100   return ret;
101 }
102
103 static gint find_entry = -1;
104 static gint wanted_entry = -1;
105
106 static void find_provider (gpointer elem, gpointer user_data)
107 {
108   const gchar *name = vik_goto_tool_get_label (elem);
109   const gchar *provider = user_data;
110   find_entry++;
111   if (!strcmp(name, provider)) {
112     wanted_entry = find_entry;
113   }
114 }
115
116 static gchar *a_prompt_for_goto_string(VikWindow *vw)
117 {
118   GtkWidget *dialog = NULL;
119
120   dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
121   gtk_window_set_title(GTK_WINDOW(dialog), _("goto"));
122
123   GtkWidget *tool_label = gtk_label_new(_("goto provider:"));
124   GtkWidget *tool_list = vik_combo_box_text_new ();
125   GList *current = g_list_first (goto_tools_list);
126   while (current != NULL)
127   {
128     char *label = NULL;
129     VikGotoTool *tool = current->data;
130     label = vik_goto_tool_get_label (tool);
131     vik_combo_box_text_append ( tool_list, label );
132     current = g_list_next (current);
133   }
134
135   // Use setting for the provider if available
136   if ( last_goto_tool < 0 ) {
137     find_entry = -1;
138     wanted_entry = -1;
139     gchar *provider = NULL;
140     if ( a_settings_get_string ( VIK_SETTINGS_GOTO_PROVIDER, &provider ) ) {
141       // Use setting
142       if ( provider )
143         g_list_foreach (goto_tools_list, find_provider, provider);
144       // If not found set it to the first entry, otherwise use the entry
145       last_goto_tool = ( wanted_entry < 0 ) ? 0 : wanted_entry;
146     }
147     else
148       last_goto_tool = 0;
149   }
150   gtk_combo_box_set_active ( GTK_COMBO_BOX( tool_list ), last_goto_tool );
151
152   GtkWidget *goto_label = gtk_label_new(_("Enter address or place name:"));
153   GtkWidget *goto_entry = gtk_entry_new();
154   if (last_goto_str)
155     gtk_entry_set_text(GTK_ENTRY(goto_entry), last_goto_str);
156
157   // 'ok' when press return in the entry
158   g_signal_connect_swapped (goto_entry, "activate", G_CALLBACK(a_dialog_response_accept), dialog);
159
160   gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), tool_label, FALSE, FALSE, 5 );
161   gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), tool_list, FALSE, FALSE, 5 );
162   gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), goto_label, FALSE, FALSE, 5 );
163   gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), goto_entry, FALSE, FALSE, 5 );
164   gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
165   gtk_widget_show_all(dialog);
166
167   // Ensure the text field has focus so we can start typing straight away
168   gtk_widget_grab_focus ( goto_entry );
169
170   if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT ) {
171     gtk_widget_destroy(dialog);
172     return NULL;
173   }
174   
175   last_goto_tool = gtk_combo_box_get_active ( GTK_COMBO_BOX(tool_list) );
176   gchar *provider = vik_goto_tool_get_label ( g_list_nth_data (goto_tools_list, last_goto_tool) );
177   a_settings_set_string ( VIK_SETTINGS_GOTO_PROVIDER, provider );
178
179   gchar *goto_str = g_strdup ( gtk_entry_get_text ( GTK_ENTRY(goto_entry) ) );
180
181   gtk_widget_destroy(dialog);
182
183   if (goto_str[0] != '\0') {
184     if (last_goto_str)
185       g_free(last_goto_str);
186     last_goto_str = g_strdup(goto_str);
187   }
188
189   return(goto_str);   /* goto_str needs to be freed by caller */
190 }
191
192 void a_vik_goto(VikWindow *vw, VikViewport *vvp)
193 {
194   VikCoord new_center;
195   gchar *s_str;
196   gboolean more = TRUE;
197
198   if (goto_tools_list == NULL)
199   {
200     /* Empty list */
201     display_no_tool(vw);
202     return;
203   }
204
205   do {
206     s_str = a_prompt_for_goto_string(vw);
207     if ((!s_str) || (s_str[0] == 0)) {
208       more = FALSE;
209     }
210
211     else if (!vik_goto_tool_get_coord(g_list_nth_data (goto_tools_list, last_goto_tool), vw, vvp, s_str, &new_center)) {
212       if (last_coord)
213         g_free(last_coord);
214       last_coord = g_malloc(sizeof(VikCoord));
215       *last_coord = new_center;
216       if (last_successful_goto_str)
217         g_free(last_successful_goto_str);
218       last_successful_goto_str = g_strdup(last_goto_str);
219       vik_viewport_set_center_coord(vvp, &new_center);
220       more = FALSE;
221     }
222     else if (!prompt_try_again(vw))
223         more = FALSE;
224     g_free(s_str);
225   } while (more);
226 }