]> err.no Git - mapper/blob - src/map-repo.c
Move some variables around
[mapper] / src / map-repo.c
1 /*
2  * This file is part of mapper
3  *
4  * Copyright (C) 2007 Kaj-Michael Lang
5  * Copyright (C) 2006-2007 John Costigan.
6  *
7  * POI and GPS-Info code originally written by Cezary Jackiewicz.
8  *
9  * Default map data provided by http://www.openstreetmap.org/
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include <config.h>
27
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <strings.h>
32 #include <stddef.h>
33 #include <libintl.h>
34 #include <locale.h>
35 #include <math.h>
36 #include <gtk/gtk.h>
37 #include <libgnomevfs/gnome-vfs.h>
38
39 #include "hildon-mapper.h"
40
41 #include "utils.h"
42 #include "gps.h"
43 #include "map.h"
44 #include "latlon.h"
45 #include "route.h"
46 #include "settings.h"
47 #include "mapper-types.h"
48 #include "map-download.h"
49 #include "ui-common.h"
50 #include "dialogs.h"
51 #include "help.h"
52
53 #define MAP_REPO_LIST_URL "http://www.gnuite.com/nokia770/maemo-mapper/repos.txt"
54
55 typedef struct _RepoManInfo RepoManInfo;
56 struct _RepoManInfo {
57         GtkWidget *dialog;
58         GtkWidget *notebook;
59         GtkWidget *cmb_repos;
60         GList *repo_edits;
61 };
62
63 typedef struct _RepoEditInfo RepoEditInfo;
64 struct _RepoEditInfo {
65         gchar *name;
66         GtkWidget *txt_url;
67         GtkWidget *txt_cache_dir;
68         GtkWidget *num_dl_zoom_steps;
69         GtkWidget *num_view_zoom_steps;
70         GtkWidget *chk_double_size;
71         GtkWidget *chk_nextable;
72         GtkWidget *btn_browse;
73         BrowseInfo browse_info;
74 };
75
76 typedef struct _MapmanInfo MapmanInfo;
77 struct _MapmanInfo {
78         GtkWidget *dialog;
79         GtkWidget *notebook;
80         GtkWidget *tbl_area;
81
82         /* The "Setup" tab. */
83         GtkWidget *rad_download;
84         GtkWidget *rad_delete;
85         GtkWidget *chk_overwrite;
86         GtkWidget *rad_by_area;
87         GtkWidget *rad_by_route;
88         GtkWidget *num_route_radius;
89
90         /* The "Area" tab. */
91         GtkWidget *txt_topleft_lat;
92         GtkWidget *txt_topleft_lon;
93         GtkWidget *txt_botright_lat;
94         GtkWidget *txt_botright_lon;
95
96         /* The "Zoom" tab. */
97         GtkWidget *chk_zoom_levels[MAX_ZOOM];
98 };
99
100 void set_repo_type(RepoData * repo)
101 {
102 if (repo->url && *repo->url) {
103         gchar *url = g_utf8_strdown(repo->url, -1);
104
105         /* Determine type of repository. */
106         if (strstr(url, "service=wms"))
107                 repo->type = REPOTYPE_WMS;
108         else if (strstr(url, "%s"))
109                 repo->type = REPOTYPE_QUAD_QRST;
110         else if (strstr(url, "%0d"))
111                 repo->type = REPOTYPE_XYZ_INV;
112         else if (strstr(url, "%0s"))
113                 repo->type = REPOTYPE_QUAD_ZERO;
114         else
115                 repo->type = REPOTYPE_XYZ;
116
117         g_free(url);
118 } else
119         repo->type = REPOTYPE_NONE;
120 }
121
122 gboolean
123 repo_make_cache_dir(gchar * name, const gchar * cache_dir, GtkWidget * parent)
124 {
125 if (g_mkdir_with_parents(cache_dir, 0755)) {
126         /* Failed to create Map Cache directory. */
127         gchar buffer[BUFFER_SIZE];
128         g_snprintf(buffer, sizeof(buffer), "%s: %s",
129                  _("Unable to create cache directory for repository"), name);
130         popup_error(parent, buffer);
131         return FALSE;
132 }
133 return g_file_test(cache_dir, G_FILE_TEST_EXISTS);
134 }
135
136 gboolean 
137 repo_set_curr(RepoData * rd)
138 {
139 _curr_repo = rd;
140 return repo_make_cache_dir(rd->name, rd->cache_dir, _window);
141 }
142
143 static gboolean 
144 repoman_dialog_select(GtkWidget * widget, RepoManInfo * rmi)
145 {
146 gint curr_index = gtk_combo_box_get_active(GTK_COMBO_BOX(rmi->cmb_repos));
147 gtk_notebook_set_current_page(GTK_NOTEBOOK(rmi->notebook), curr_index);
148 return TRUE;
149 }
150
151 static gboolean 
152 repoman_dialog_browse(GtkWidget * widget, BrowseInfo * browse_info)
153 {
154 GtkWidget *dialog;
155
156 dialog = GTK_WIDGET(hildon_file_chooser_dialog_new
157        (GTK_WINDOW(browse_info->dialog),
158                 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER));
159
160 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(dialog), TRUE);
161 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), gtk_entry_get_text(GTK_ENTRY(browse_info->txt)));
162
163 if (GTK_RESPONSE_OK == gtk_dialog_run(GTK_DIALOG(dialog))) {
164         gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
165         gtk_entry_set_text(GTK_ENTRY(browse_info->txt), filename);
166         g_free(filename);
167 }
168
169 gtk_widget_destroy(dialog);
170
171 return TRUE;
172 }
173
174 static gboolean 
175 repoman_dialog_rename(GtkWidget * widget, RepoManInfo * rmi)
176 {
177 GtkWidget *hbox;
178 GtkWidget *label;
179 GtkWidget *txt_name;
180 GtkWidget *dialog;
181
182 dialog = gtk_dialog_new_with_buttons(_("New Name"),
183                              GTK_WINDOW(rmi->dialog),
184                              GTK_DIALOG_MODAL, GTK_STOCK_OK,
185                              GTK_RESPONSE_ACCEPT,
186                              GTK_STOCK_CANCEL,
187                              GTK_RESPONSE_REJECT, NULL);
188
189 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox = gtk_hbox_new(FALSE, 4), FALSE, FALSE, 4);
190
191 gtk_box_pack_start(GTK_BOX(hbox), label = gtk_label_new(_("Name")), FALSE, FALSE, 0);
192 gtk_box_pack_start(GTK_BOX(hbox), txt_name = gtk_entry_new(), TRUE, TRUE, 0);
193
194 gtk_widget_show_all(dialog);
195
196 while (GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) {
197         gint active = gtk_combo_box_get_active(GTK_COMBO_BOX(rmi->cmb_repos));
198         RepoEditInfo *rei = g_list_nth_data(rmi->repo_edits, active);
199         g_free(rei->name);
200         rei->name = g_strdup(gtk_entry_get_text(GTK_ENTRY(txt_name)));
201         gtk_combo_box_insert_text(GTK_COMBO_BOX(rmi->cmb_repos), active, g_strdup(rei->name));
202         gtk_combo_box_set_active(GTK_COMBO_BOX(rmi->cmb_repos), active);
203         gtk_combo_box_remove_text(GTK_COMBO_BOX(rmi->cmb_repos), active + 1);
204         break;
205 }
206
207 gtk_widget_destroy(dialog);
208 return TRUE;
209 }
210
211 static gboolean 
212 repoman_dialog_delete(GtkWidget * widget, RepoManInfo * rmi)
213 {
214 gchar buffer[100];
215 GtkWidget *confirm;
216 gint active = gtk_combo_box_get_active(GTK_COMBO_BOX(rmi->cmb_repos));
217
218 if (gtk_tree_model_iter_n_children(GTK_TREE_MODEL(gtk_combo_box_get_model(GTK_COMBO_BOX(rmi->cmb_repos))), NULL) <= 1) {
219         popup_error(rmi->dialog, _("Cannot delete the last repository - there must be at lease one repository."));
220         return TRUE;
221 }
222
223 g_snprintf(buffer, sizeof(buffer), "%s:\n%s\n",
224          _("Confirm delete of repository"),
225          gtk_combo_box_get_active_text(GTK_COMBO_BOX(rmi->cmb_repos)));
226 confirm = hildon_note_new_confirmation(GTK_WINDOW(_window), buffer);
227
228 if (GTK_RESPONSE_OK == gtk_dialog_run(GTK_DIALOG(confirm))) {
229         gtk_combo_box_remove_text(GTK_COMBO_BOX(rmi->cmb_repos), active);
230         gtk_notebook_remove_page(GTK_NOTEBOOK(rmi->notebook), active);
231         rmi->repo_edits = g_list_remove_link(rmi->repo_edits, g_list_nth(rmi->repo_edits, active));
232         gtk_combo_box_set_active(GTK_COMBO_BOX(rmi->cmb_repos), MAX(0, active - 1));
233 }
234
235 gtk_widget_destroy(confirm);
236
237 return TRUE;
238 }
239
240 static RepoEditInfo *
241 repoman_dialog_add_repo(RepoManInfo * rmi, gchar * name)
242 {
243 GtkWidget *vbox;
244 GtkWidget *table;
245 GtkWidget *label;
246 GtkWidget *hbox;
247 RepoEditInfo *rei = g_new(RepoEditInfo, 1);
248
249 rei->name = name;
250
251 /* Maps page. */
252 gtk_notebook_append_page(GTK_NOTEBOOK(rmi->notebook), vbox = gtk_vbox_new(FALSE, 4), gtk_label_new(name));
253
254 gtk_box_pack_start(GTK_BOX(vbox), table = gtk_table_new(2, 2, FALSE), FALSE, FALSE, 0);
255 /* Map download URI. */
256 gtk_table_attach(GTK_TABLE(table), label = gtk_label_new(_("URL Format")), 0, 1, 0, 1, GTK_FILL, 0, 2, 4);
257 gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
258 gtk_table_attach(GTK_TABLE(table), rei->txt_url = gtk_entry_new(), 1, 2, 0, 1, GTK_EXPAND | GTK_FILL, 0, 2, 4);
259
260 /* Map Directory. */
261 gtk_table_attach(GTK_TABLE(table), label = gtk_label_new(_("Cache Dir.")), 0, 1, 1, 2, GTK_FILL, 0, 2, 4);
262 gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
263 gtk_table_attach(GTK_TABLE(table), hbox = gtk_hbox_new(FALSE, 4), 1, 2, 1, 2, GTK_EXPAND | GTK_FILL, 0, 2, 4);
264 gtk_box_pack_start(GTK_BOX(hbox), rei->txt_cache_dir = gtk_entry_new(), TRUE, TRUE, 0);
265 gtk_box_pack_start(GTK_BOX(hbox), rei->btn_browse = gtk_button_new_with_label(_("Browse...")), FALSE, FALSE, 0);
266
267 /* Initialize cache dir */
268 {
269         gchar *cache_base = gnome_vfs_expand_initial_tilde(REPO_DEFAULT_CACHE_BASE);
270         gchar *cache_dir = gnome_vfs_uri_make_full_from_relative(cache_base, name);
271         gtk_entry_set_text(GTK_ENTRY(rei->txt_cache_dir), cache_dir);
272         g_free(cache_dir);
273         g_free(cache_base);
274 }
275
276 gtk_box_pack_start(GTK_BOX(vbox), table = gtk_table_new(3, 2, FALSE), FALSE, FALSE, 0);
277
278 /* Download Zoom Steps. */
279 gtk_table_attach(GTK_TABLE(table), label = gtk_label_new(_("Download Zoom Steps")), 0, 1, 0, 1, GTK_FILL, 0, 2, 4);
280 gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
281 gtk_table_attach(GTK_TABLE(table), rei->num_dl_zoom_steps = hildon_controlbar_new(), 1, 2, 0, 1, GTK_FILL | GTK_EXPAND, 0, 2, 4);
282 hildon_controlbar_set_range(HILDON_CONTROLBAR(rei->num_dl_zoom_steps), 1, 4);
283 hildon_controlbar_set_value(HILDON_CONTROLBAR(rei->num_dl_zoom_steps), REPO_DEFAULT_DL_ZOOM_STEPS);
284 force_min_visible_bars(HILDON_CONTROLBAR(rei->num_dl_zoom_steps), 1);
285
286 /* Download Zoom Steps. */
287 gtk_table_attach(GTK_TABLE(table), label = gtk_label_new(_("View Zoom Steps")), 0, 1, 1, 2, GTK_FILL, 0, 2, 4);
288 gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
289 gtk_table_attach(GTK_TABLE(table), rei->num_view_zoom_steps = hildon_controlbar_new(), 1, 2, 1, 2, GTK_FILL | GTK_EXPAND, 0, 2, 4);
290 hildon_controlbar_set_range(HILDON_CONTROLBAR(rei->num_view_zoom_steps), 1, 4);
291 hildon_controlbar_set_value(HILDON_CONTROLBAR(rei->num_view_zoom_steps), REPO_DEFAULT_VIEW_ZOOM_STEPS);
292 force_min_visible_bars(HILDON_CONTROLBAR(rei->num_view_zoom_steps), 1);
293
294 gtk_table_attach(GTK_TABLE(table), label = gtk_vseparator_new(), 2, 3, 0, 2, GTK_FILL, GTK_FILL, 4, 4);
295
296 /* Double-size. */
297 gtk_table_attach(GTK_TABLE(table), rei->chk_double_size = gtk_check_button_new_with_label(_("Double Pixels")), 3, 4, 0, 1, GTK_FILL, GTK_FILL, 0, 4);
298 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(rei->chk_double_size), FALSE);
299
300 /* Next-able */
301 gtk_table_attach(GTK_TABLE(table), rei->chk_nextable = gtk_check_button_new_with_label(_("Next-able")), 3, 4, 1, 2, GTK_FILL, GTK_FILL, 0, 4);
302 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(rei->chk_nextable), TRUE);
303
304 rmi->repo_edits = g_list_append(rmi->repo_edits, rei);
305
306 /* Connect signals. */
307 rei->browse_info.dialog = rmi->dialog;
308 rei->browse_info.txt = rei->txt_cache_dir;
309 g_signal_connect(G_OBJECT(rei->btn_browse), "clicked", G_CALLBACK(repoman_dialog_browse), &rei->browse_info);
310
311 gtk_widget_show_all(vbox);
312
313 gtk_combo_box_append_text(GTK_COMBO_BOX(rmi->cmb_repos), name);
314 gtk_combo_box_set_active(GTK_COMBO_BOX(rmi->cmb_repos),
315         gtk_tree_model_iter_n_children(GTK_TREE_MODEL(gtk_combo_box_get_model(GTK_COMBO_BOX (rmi->cmb_repos))), NULL) - 1);
316
317 return rei;
318 }
319
320 static gboolean 
321 repoman_dialog_new(GtkWidget * widget, RepoManInfo * rmi)
322 {
323 GtkWidget *hbox;
324 GtkWidget *label;
325 GtkWidget *txt_name;
326 GtkWidget *dialog;
327
328 dialog = gtk_dialog_new_with_buttons(_("New Repository"),
329                                      GTK_WINDOW(rmi->dialog),
330                                      GTK_DIALOG_MODAL, GTK_STOCK_OK,
331                                      GTK_RESPONSE_ACCEPT,
332                                      GTK_STOCK_CANCEL,
333                                      GTK_RESPONSE_REJECT, NULL);
334
335 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox = gtk_hbox_new(FALSE, 4), FALSE, FALSE, 4);
336
337 gtk_box_pack_start(GTK_BOX(hbox), label = gtk_label_new(_("Name")), FALSE, FALSE, 0);
338 gtk_box_pack_start(GTK_BOX(hbox), txt_name = gtk_entry_new(), TRUE, TRUE, 0);
339
340 gtk_widget_show_all(dialog);
341
342 while (GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) {
343                 repoman_dialog_add_repo(rmi, g_strdup(gtk_entry_get_text(GTK_ENTRY(txt_name))));
344         break;
345 }
346
347 gtk_widget_destroy(dialog);
348
349 return TRUE;
350 }
351
352 static gboolean
353 repoman_reset(GtkWidget * widget, RepoManInfo * rmi)
354 {
355 GtkWidget *confirm;
356
357 confirm = hildon_note_new_confirmation(GTK_WINDOW(_window),
358                 _("Replace all repositories with the default repository?"));
359
360 if (GTK_RESPONSE_OK == gtk_dialog_run(GTK_DIALOG(confirm))) {
361         /* First, delete all existing repositories. */
362         while (rmi->repo_edits) {
363                 gtk_combo_box_remove_text(GTK_COMBO_BOX(rmi->cmb_repos), 0);
364                         gtk_notebook_remove_page(GTK_NOTEBOOK(rmi->notebook), 0);
365                         rmi->repo_edits = g_list_remove_link(rmi->repo_edits, g_list_first(rmi->repo_edits));
366                 }
367
368         /* Now, add the default repository. */
369         repoman_dialog_add_repo(rmi, REPO_DEFAULT_NAME);
370         gtk_entry_set_text(GTK_ENTRY(((RepoEditInfo *) rmi->repo_edits->data)->txt_url), REPO_DEFAULT_MAP_URI);
371
372         gtk_combo_box_set_active(GTK_COMBO_BOX(rmi->cmb_repos), 0);
373 }
374 gtk_widget_destroy(confirm);
375
376 return TRUE;
377 }
378
379 static gboolean 
380 repoman_download(GtkWidget * widget, RepoManInfo * rmi)
381 {
382 GtkWidget *confirm;
383
384 confirm = hildon_note_new_confirmation(GTK_WINDOW(rmi->dialog),
385                                        _("Mapper will now download and add a list of "
386                                         "possibly-duplicate repositories from the internet.  "
387                                         "Continue?"));
388
389 if (GTK_RESPONSE_OK == gtk_dialog_run(GTK_DIALOG(confirm))) {
390         gchar *bytes;
391         gchar *head;
392         gchar *tail;
393         gint size;
394         GnomeVFSResult vfs_result;
395
396         /* Get repo config file from www.gnuite.com. */
397         if (GNOME_VFS_OK != (vfs_result = gnome_vfs_read_entire_file(MAP_REPO_LIST_URL, &size, &bytes))) {
398                 popup_error(rmi->dialog,
399                             _("An error occurred while retrieving the repositories.  "
400                              "The web service may be temporarily down."));
401                 g_printerr("Error while download repositories: %s\n", gnome_vfs_result_to_string(vfs_result));
402         } else {
403                 /* Parse each line as a reposotory. */
404                 for (head = bytes; head && *head; head = tail) {
405                         RepoData *rd;
406                         RepoEditInfo *rei;
407                         tail = strchr(head, '\n');
408                         *tail++ = '\0';
409                         rd = config_parse_repo(head);
410                         rei = repoman_dialog_add_repo(rmi, g_strdup(rd->name));
411                         /* Initialize fields with data from the RepoData object. */
412                         gtk_entry_set_text(GTK_ENTRY(rei->txt_url), rd->url);
413                         gtk_entry_set_text(GTK_ENTRY(rei->txt_cache_dir), rd->cache_dir);
414                         hildon_controlbar_set_value(HILDON_CONTROLBAR(rei->num_dl_zoom_steps), rd->dl_zoom_steps);
415                         hildon_controlbar_set_value(HILDON_CONTROLBAR(rei->num_view_zoom_steps), rd->view_zoom_steps);
416                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(rei->chk_double_size), rd->double_size);
417                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(rei->chk_nextable), rd->nextable);
418                 }
419                 g_free(bytes);
420         }
421 }
422 gtk_widget_destroy(confirm);
423
424 return TRUE;
425 }
426
427 gboolean 
428 repoman_dialog()
429 {
430 RepoManInfo rmi;
431 GtkWidget *hbox;
432 GtkWidget *btn_rename;
433 GtkWidget *btn_delete;
434 GtkWidget *btn_new;
435 GtkWidget *btn_reset;
436 GtkWidget *btn_download;
437 guint i, curr_repo_index = 0;
438 GList *curr;
439
440 rmi.dialog = gtk_dialog_new_with_buttons(_("Manage Repositories"),
441                                          GTK_WINDOW(_window),
442                                          GTK_DIALOG_MODAL, GTK_STOCK_OK,
443                                          GTK_RESPONSE_ACCEPT, NULL);
444
445 help_dialog_help_enable(GTK_DIALOG(rmi.dialog), HELP_ID_REPOMAN);
446
447 /* Reset button. */
448 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(rmi.dialog)->action_area),
449                   btn_reset = gtk_button_new_with_label(_("Reset...")));
450 g_signal_connect(G_OBJECT(btn_reset), "clicked", G_CALLBACK(repoman_reset), &rmi);
451
452 /* Download button. */
453 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(rmi.dialog)->action_area),
454                   btn_download = gtk_button_new_with_label(_("Download...")));
455 g_signal_connect(G_OBJECT(btn_download), "clicked", G_CALLBACK(repoman_download), &rmi);
456
457 /* Cancel button. */
458 gtk_dialog_add_button(GTK_DIALOG(rmi.dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT);
459
460 hbox = gtk_hbox_new(FALSE, 4);
461
462 gtk_box_pack_start(GTK_BOX(hbox), rmi.cmb_repos = gtk_combo_box_new_text(), TRUE, TRUE, 4);
463
464 gtk_box_pack_start(GTK_BOX(hbox), gtk_vseparator_new(), FALSE, FALSE, 4);
465 gtk_box_pack_start(GTK_BOX(hbox), btn_rename = gtk_button_new_with_label(_("Rename...")), FALSE, FALSE, 4);
466 gtk_box_pack_start(GTK_BOX(hbox), btn_delete = gtk_button_new_with_label(_("Delete...")), FALSE, FALSE, 4);
467 gtk_box_pack_start(GTK_BOX(hbox), btn_new = gtk_button_new_with_label(_("New...")), FALSE, FALSE, 4);
468
469 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(rmi.dialog)->vbox), hbox, FALSE, FALSE, 4);
470 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(rmi.dialog)->vbox), gtk_hseparator_new(), TRUE, TRUE, 4);
471 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(rmi.dialog)->vbox), rmi.notebook = gtk_notebook_new(), TRUE, TRUE, 4);
472
473 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(rmi.notebook), FALSE);
474 gtk_notebook_set_show_border(GTK_NOTEBOOK(rmi.notebook), FALSE);
475
476 rmi.repo_edits = NULL;
477
478 /* Populate combo box and pages in notebook. */
479 for (i = 0, curr = _repo_list; curr; curr = curr->next, i++) {
480         RepoData *rd = (RepoData *) curr->data;
481         RepoEditInfo *rei = repoman_dialog_add_repo(&rmi, g_strdup(rd->name));
482
483         /* Initialize fields with data from the RepoData object. */
484         gtk_entry_set_text(GTK_ENTRY(rei->txt_url), rd->url);
485         gtk_entry_set_text(GTK_ENTRY(rei->txt_cache_dir), rd->cache_dir);
486         hildon_controlbar_set_value(HILDON_CONTROLBAR(rei->num_dl_zoom_steps), rd->dl_zoom_steps);
487         hildon_controlbar_set_value(HILDON_CONTROLBAR(rei->num_view_zoom_steps), rd->view_zoom_steps);
488         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(rei->chk_double_size), rd->double_size);
489         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(rei->chk_nextable), rd->nextable);
490         if (rd == _curr_repo)
491                 curr_repo_index = i;
492 }
493
494 /* Connect signals. */
495 g_signal_connect(G_OBJECT(btn_rename), "clicked", G_CALLBACK(repoman_dialog_rename), &rmi);
496 g_signal_connect(G_OBJECT(btn_delete), "clicked", G_CALLBACK(repoman_dialog_delete), &rmi);
497 g_signal_connect(G_OBJECT(btn_new), "clicked", G_CALLBACK(repoman_dialog_new), &rmi);
498 g_signal_connect(G_OBJECT(rmi.cmb_repos), "changed", G_CALLBACK(repoman_dialog_select), &rmi);
499 gtk_combo_box_set_active(GTK_COMBO_BOX(rmi.cmb_repos), curr_repo_index);
500 gtk_notebook_set_current_page(GTK_NOTEBOOK(rmi.notebook), curr_repo_index);
501
502 gtk_widget_show_all(rmi.dialog);
503
504 while (GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(rmi.dialog))) {
505         /* Iterate through repos and verify each. */
506         gboolean verified = TRUE;
507         gint i;
508         GList *curr;
509         gchar *old_curr_repo_name = _curr_repo->name;
510
511         for (i = 0, curr = rmi.repo_edits; verified && curr; curr = curr->next, i++) {
512                 RepoEditInfo *rei = curr->data;
513                 gchar *expanded = gnome_vfs_expand_initial_tilde(gtk_entry_get_text(GTK_ENTRY(rei->txt_cache_dir)));
514                 verified = repo_make_cache_dir(rei->name, expanded,     rmi.dialog);
515                 g_free(expanded);
516         }
517         if (!verified) {
518                 gtk_combo_box_set_active(GTK_COMBO_BOX(rmi.cmb_repos), i - 1);
519                 continue;
520         }
521
522         /* But keep the repo list in memory, in case downloads are using it. */
523         _repo_list = NULL;
524
525         /* Write new _repo_list. */
526         curr_repo_index = gtk_combo_box_get_active(GTK_COMBO_BOX(rmi.cmb_repos));
527         _curr_repo = NULL;
528         for (i = 0, curr = rmi.repo_edits; curr; curr = curr->next, i++) {
529                 RepoEditInfo *rei = curr->data;
530                 RepoData *rd = g_new(RepoData, 1);
531                 rd->name = g_strdup(rei->name);
532                 rd->url = g_strdup(gtk_entry_get_text(GTK_ENTRY(rei->txt_url)));
533                 rd->cache_dir = gnome_vfs_expand_initial_tilde(gtk_entry_get_text(GTK_ENTRY(rei->txt_cache_dir)));
534                 rd->dl_zoom_steps = hildon_controlbar_get_value(HILDON_CONTROLBAR(rei->num_dl_zoom_steps));
535                 rd->view_zoom_steps = hildon_controlbar_get_value(HILDON_CONTROLBAR(rei->num_view_zoom_steps));
536                 rd->double_size = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(rei->chk_double_size));
537                 rd->nextable = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(rei->chk_nextable));
538                 set_repo_type(rd);
539
540                 _repo_list = g_list_append(_repo_list, rd);
541
542                 if (!_curr_repo && !strcmp(old_curr_repo_name, rd->name))
543                         repo_set_curr(rd);
544                 else if (i == curr_repo_index)
545                         repo_set_curr(rd);
546         }
547         if (!_curr_repo)
548                         repo_set_curr((RepoData *) g_list_first(_repo_list)->data);
549
550         menu_maps_add_repos();
551         config_save_repo();
552         break;
553 }
554
555 gtk_widget_hide(rmi.dialog);    /* Destroying causes a crash (!?!?!??!) */
556 map_set_zoom(_zoom);    /* make sure we're at an appropriate zoom level. */
557 return TRUE;
558 }
559
560 static gboolean
561 mapman_by_area(gfloat start_lat, gfloat start_lon,
562                gfloat end_lat, gfloat end_lon, MapmanInfo * mapman_info,
563                gboolean is_deleting, gboolean is_overwriting)
564 {
565 guint start_unitx, start_unity, end_unitx, end_unity;
566 guint num_maps = 0;
567 guint i;
568 gchar buffer[80];
569 GtkWidget *confirm;
570
571 latlon2unit(start_lat, start_lon, start_unitx, start_unity);
572 latlon2unit(end_lat, end_lon, end_unitx, end_unity);
573
574 /* Swap if they specified flipped lats or lons. */
575 if (start_unitx > end_unitx) {
576         guint swap = start_unitx;
577         start_unitx = end_unitx;
578         end_unitx = swap;
579 }
580 if (start_unity > end_unity) {
581         guint swap = start_unity;
582         start_unity = end_unity;
583         end_unity = swap;
584 }
585
586 /* First, get the number of maps to download. */
587 for (i = 0; i < MAX_ZOOM; i++) {
588         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(mapman_info->chk_zoom_levels[i]))) {
589                 guint start_tilex, start_tiley, end_tilex, end_tiley;
590                 start_tilex = unit2ztile(start_unitx, i);
591                 start_tiley = unit2ztile(start_unity, i);
592                 end_tilex = unit2ztile(end_unitx, i);
593                 end_tiley = unit2ztile(end_unity, i);
594                 num_maps += (end_tilex - start_tilex + 1) * (end_tiley - start_tiley + 1);
595                 }
596 }
597
598 if (is_deleting) {
599         g_snprintf(buffer, sizeof(buffer), "%s %d %s", _("Confirm DELETION of"), num_maps, _("maps "));
600 } else {
601         g_snprintf(buffer, sizeof(buffer), "%s %d %s\n(%s %.2f MB)\n", _("Confirm download of"),
602                 num_maps, _("maps"), _("up to about"), num_maps * (strstr(_curr_repo->url, "%s") ? 18e-3 : 6e-3));
603 }
604 confirm = hildon_note_new_confirmation(GTK_WINDOW(mapman_info->dialog), buffer);
605
606 if (GTK_RESPONSE_OK != gtk_dialog_run(GTK_DIALOG(confirm))) {
607         gtk_widget_destroy(confirm);
608         return FALSE;
609 }
610
611 for (i = 0; i < MAX_ZOOM; i++) {
612         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(mapman_info->chk_zoom_levels[i]))) {
613                 guint start_tilex, start_tiley, end_tilex, end_tiley;
614                 guint tilex, tiley;
615                 start_tilex = unit2ztile(start_unitx, i);
616                 start_tiley = unit2ztile(start_unity, i);
617                 end_tilex = unit2ztile(end_unitx, i);
618                 end_tiley = unit2ztile(end_unity, i);
619
620                 for (tiley = start_tiley; tiley <= end_tiley; tiley++)
621                         for (tilex = start_tilex; tilex <= end_tilex; tilex++)
622                                 map_initiate_download(tilex, tiley, i, is_deleting ? 0 : (is_overwriting ?
623                                                          -INITIAL_DOWNLOAD_RETRIES : INITIAL_DOWNLOAD_RETRIES));
624         }
625 }
626 gtk_widget_destroy(confirm);
627 return TRUE;
628 }
629
630 static gboolean
631 mapman_by_route(MapmanInfo * mapman_info, gboolean is_deleting, gboolean is_overwriting)
632 {
633 GtkWidget *confirm;
634 guint prev_tilex, prev_tiley, num_maps = 0, i;
635 Point *curr;
636 gchar buffer[80];
637 guint radius = hildon_number_editor_get_value(HILDON_NUMBER_EDITOR(mapman_info->num_route_radius));
638
639 /* First, get the number of maps to download. */
640 for (i = 0; i < MAX_ZOOM; i++) {
641         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(mapman_info->chk_zoom_levels[i]))) {
642                 prev_tilex = 0;
643                 prev_tiley = 0;
644
645                 for (curr = _route.head - 1; curr++ != _route.tail;) {
646                         if (curr->unity) {
647                                 guint tilex = unit2ztile(curr->unitx, i);
648                                 guint tiley = unit2ztile(curr->unity, i);
649                                 if (tilex != prev_tilex || tiley != prev_tiley) {
650                                         if (prev_tiley)
651                                                 num_maps += (abs((gint) tilex - prev_tilex) + 1) * (abs((gint) tiley - prev_tiley) + 1) - 1;
652                                         prev_tilex = tilex;
653                                         prev_tiley = tiley;
654                                 }
655                         }
656                 }
657         }
658 }
659 num_maps *= 0.625 * pow(radius + 1, 1.85);
660
661 if (is_deleting) {
662         g_snprintf(buffer, sizeof(buffer), "%s %s %d %s",
663                  _("Confirm DELETION of"), _("about"),
664                  num_maps, _("maps "));
665 } else {
666         g_snprintf(buffer, sizeof(buffer),
667                  "%s %s %d %s\n(%s %.2f MB)\n",
668                  _("Confirm download of"), _("about"), num_maps,
669                  _("maps"), _("up to about"),
670                  num_maps * (strstr(_curr_repo->url, "%s") ? 18e-3 : 6e-3));
671 }
672 confirm = hildon_note_new_confirmation(GTK_WINDOW(mapman_info->dialog), buffer);
673
674 if (GTK_RESPONSE_OK != gtk_dialog_run(GTK_DIALOG(confirm))) {
675         gtk_widget_destroy(confirm);
676         return FALSE;
677 }
678
679 /* Now, do the actual download. */
680 for (i = 0; i < MAX_ZOOM; i++) {
681         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(mapman_info->chk_zoom_levels[i]))) {
682                 prev_tilex = 0;
683                 prev_tiley = 0;
684
685                 for (curr = _route.head - 1; curr++ != _route.tail;) {
686                         if (curr->unity) {
687                                 guint tilex = unit2ztile(curr->unitx, i);
688                                 guint tiley = unit2ztile(curr->unity, i);
689                                 if (tilex != prev_tilex || tiley != prev_tiley) {
690                                         guint minx, miny, maxx, maxy, x, y;
691                                         if (prev_tiley != 0) {
692                                                 minx = MIN(tilex, prev_tilex) - radius;
693                                                 miny = MIN(tiley, prev_tiley) - radius;
694                                                 maxx = MAX(tilex, prev_tilex) + radius;
695                                                 maxy = MAX(tiley, prev_tiley) + radius;
696                                         } else {
697                                                 minx = tilex - radius;
698                                                 miny = tiley - radius;
699                                                 maxx = tilex + radius;
700                                                 maxy = tiley + radius;
701                                         }
702                                         for (x = minx; x <= maxx; x++)
703                                                 for (y = miny; y <= maxy; y++)
704                                                         map_initiate_download(x, y, i, is_deleting ? 0
705                                                              : (is_overwriting ? -INITIAL_DOWNLOAD_RETRIES : INITIAL_DOWNLOAD_RETRIES));
706                                         prev_tilex = tilex;
707                                         prev_tiley = tiley;
708                                 }
709                         }
710                 }
711         }
712 }
713 _route_dl_radius = radius;
714 gtk_widget_destroy(confirm);
715 return TRUE;
716 }
717
718 static void 
719 mapman_clear(GtkWidget * widget, MapmanInfo * mapman_info)
720 {
721 guint i;
722
723 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(mapman_info->notebook))) {
724         /* This is the second page (the "Zoom" page) - clear the checks. */
725         for (i = 0; i < MAX_ZOOM; i++)
726                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mapman_info->chk_zoom_levels[i]), FALSE);
727 } else {
728         /* This is the first page (the "Area" page) - clear the text fields. */
729         gtk_entry_set_text(GTK_ENTRY(mapman_info->txt_topleft_lat), "");
730         gtk_entry_set_text(GTK_ENTRY(mapman_info->txt_topleft_lon), "");
731         gtk_entry_set_text(GTK_ENTRY(mapman_info->txt_botright_lat), "");
732         gtk_entry_set_text(GTK_ENTRY(mapman_info->txt_botright_lon), "");
733 }
734 }
735
736 static void 
737 mapman_update_state(GtkWidget * widget, MapmanInfo * mapman_info)
738 {
739 gtk_widget_set_sensitive(mapman_info->chk_overwrite,
740          gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(mapman_info->rad_download)));
741
742         if (gtk_toggle_button_get_active
743             (GTK_TOGGLE_BUTTON(mapman_info->rad_by_area)))
744                 gtk_widget_show(mapman_info->tbl_area);
745         else if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(mapman_info->notebook))
746                  == 3)
747                 gtk_widget_hide(mapman_info->tbl_area);
748
749         gtk_widget_set_sensitive(mapman_info->num_route_radius,
750                                  gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON
751                                                               (mapman_info->rad_by_route)));
752 }
753
754 gboolean menu_cb_mapman(GtkAction * action)
755 {
756         GtkWidget *dialog;
757         GtkWidget *vbox;
758         GtkWidget *hbox;
759         GtkWidget *table;
760         GtkWidget *label;
761         GtkWidget *button;
762         GtkWidget *lbl_gps_lat;
763         GtkWidget *lbl_gps_lon;
764         GtkWidget *lbl_center_lat;
765         GtkWidget *lbl_center_lon;
766         MapmanInfo mapman_info;
767         gchar buffer[80];
768         gfloat lat, lon;
769         guint i;
770
771         mapman_info.dialog = dialog = gtk_dialog_new_with_buttons(_("Manage Maps"), GTK_WINDOW(_window),
772                                         GTK_DIALOG_MODAL, GTK_STOCK_OK,
773                                         GTK_RESPONSE_ACCEPT, NULL);
774
775         help_dialog_help_enable(GTK_DIALOG(mapman_info.dialog), HELP_ID_MAPMAN);
776
777         /* Clear button. */
778         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
779                           button = gtk_button_new_with_label(_("Clear")));
780         g_signal_connect(G_OBJECT(button), "clicked",
781                          G_CALLBACK(mapman_clear), &mapman_info);
782
783         /* Cancel button. */
784         gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT);
785
786         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
787                            mapman_info.notebook = gtk_notebook_new(), TRUE, TRUE, 0);
788
789         /* Setup page. */
790         gtk_notebook_append_page(GTK_NOTEBOOK(mapman_info.notebook),
791                                  vbox = gtk_vbox_new(FALSE, 2),
792                                  label = gtk_label_new(_("Setup")));
793         gtk_notebook_set_tab_label_packing(GTK_NOTEBOOK(mapman_info.notebook),
794                                            vbox, FALSE, FALSE, GTK_PACK_START);
795
796         gtk_box_pack_start(GTK_BOX(vbox),
797                            hbox = gtk_hbox_new(FALSE, 4), FALSE, FALSE, 0);
798         gtk_box_pack_start(GTK_BOX(hbox),
799                            mapman_info.rad_download = gtk_radio_button_new_with_label(NULL, _("Download Maps")), FALSE, FALSE, 0);
800         gtk_box_pack_start(GTK_BOX(hbox), label =
801                            gtk_alignment_new(0.f, 0.5f, 0.f, 0.f), FALSE, FALSE,
802                            0);
803         gtk_container_add(GTK_CONTAINER(label), mapman_info.chk_overwrite =
804                           gtk_check_button_new_with_label(_("Overwrite"))),
805             gtk_box_pack_start(GTK_BOX(vbox), mapman_info.rad_delete =
806                                gtk_radio_button_new_with_label_from_widget
807                                (GTK_RADIO_BUTTON(mapman_info.rad_download),
808                                 _("Delete Maps")), FALSE, FALSE, 0);
809
810         gtk_box_pack_start(GTK_BOX(vbox),
811                            gtk_hseparator_new(), FALSE, FALSE, 0);
812
813         gtk_box_pack_start(GTK_BOX(vbox),
814                            mapman_info.rad_by_area
815                            = gtk_radio_button_new_with_label(NULL, _("By Area (see tab)")), FALSE, FALSE, 0);
816         gtk_box_pack_start(GTK_BOX(vbox), hbox =
817                            gtk_hbox_new(FALSE, 4), FALSE, FALSE, 0);
818         gtk_box_pack_start(GTK_BOX(hbox), mapman_info.rad_by_route =
819                            gtk_radio_button_new_with_label_from_widget
820                            (GTK_RADIO_BUTTON(mapman_info.rad_by_area),
821                             _("Along Route - Radius (tiles):")), FALSE, FALSE,
822                            0);
823         gtk_widget_set_sensitive(mapman_info.rad_by_route,
824                                  _route.head != _route.tail);
825         gtk_box_pack_start(GTK_BOX(hbox), mapman_info.num_route_radius =
826                            hildon_number_editor_new(0, 100), FALSE, FALSE, 0);
827         hildon_number_editor_set_value(HILDON_NUMBER_EDITOR
828                                        (mapman_info.num_route_radius),
829                                        _route_dl_radius);
830
831         /* Zoom page. */
832         gtk_notebook_append_page(GTK_NOTEBOOK(mapman_info.notebook),
833                                  table = gtk_table_new(5, 5, FALSE),
834                                  label = gtk_label_new(_("Zoom")));
835         gtk_notebook_set_tab_label_packing(GTK_NOTEBOOK(mapman_info.notebook),
836                                            table, FALSE, FALSE, GTK_PACK_START);
837         gtk_table_attach(GTK_TABLE(table), label =
838                          gtk_label_new(_
839                                        ("Zoom Levels to Download: (0 = most detail)")),
840                          0, 4, 0, 1, GTK_FILL, 0, 4, 0);
841         gtk_misc_set_alignment(GTK_MISC(label), 0.f, 0.5f);
842         for (i = 0; i < MAX_ZOOM; i++) {
843                 g_snprintf(buffer, sizeof(buffer), "%d", i);
844                 gtk_table_attach(GTK_TABLE(table),
845                                  mapman_info.chk_zoom_levels[i]
846                                  = gtk_check_button_new_with_label(buffer),
847                                  i % 4, i % 4 + 1, i / 4 + 1, i / 4 + 2,
848                                  GTK_EXPAND | GTK_FILL, 0, 4, 0);
849         }
850
851         /* Area page. */
852         gtk_notebook_append_page(GTK_NOTEBOOK(mapman_info.notebook),
853                                  mapman_info.tbl_area =
854                                  gtk_table_new(3, 4, FALSE), label =
855                                  gtk_label_new(_("Area")));
856
857         /* Label Columns. */
858         gtk_table_attach(GTK_TABLE(mapman_info.tbl_area),
859                          label = gtk_label_new(_("Latitude")),
860                          1, 2, 0, 1, GTK_FILL, 0, 4, 0);
861         gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
862         gtk_table_attach(GTK_TABLE(mapman_info.tbl_area),
863                          label = gtk_label_new(_("Longitude")),
864                          2, 3, 0, 1, GTK_FILL, 0, 4, 0);
865         gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
866
867         /* GPS. */
868         gtk_table_attach(GTK_TABLE(mapman_info.tbl_area),
869                          label = gtk_label_new(_("GPS Location")),
870                          0, 1, 1, 2, GTK_FILL, 0, 4, 0);
871         gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
872         gtk_table_attach(GTK_TABLE(mapman_info.tbl_area),
873                          lbl_gps_lat = gtk_label_new(""),
874                          1, 2, 1, 2, GTK_FILL, 0, 4, 0);
875         gtk_label_set_selectable(GTK_LABEL(lbl_gps_lat), TRUE);
876         gtk_misc_set_alignment(GTK_MISC(lbl_gps_lat), 1.f, 0.5f);
877         gtk_table_attach(GTK_TABLE(mapman_info.tbl_area),
878                          lbl_gps_lon = gtk_label_new(""),
879                          2, 3, 1, 2, GTK_FILL, 0, 4, 0);
880         gtk_label_set_selectable(GTK_LABEL(lbl_gps_lon), TRUE);
881         gtk_misc_set_alignment(GTK_MISC(lbl_gps_lon), 1.f, 0.5f);
882
883         /* Center. */
884         gtk_table_attach(GTK_TABLE(mapman_info.tbl_area),
885                          label = gtk_label_new(_("View Center")),
886                          0, 1, 2, 3, GTK_FILL, 0, 4, 0);
887         gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
888         gtk_table_attach(GTK_TABLE(mapman_info.tbl_area),
889                          lbl_center_lat = gtk_label_new(""),
890                          1, 2, 2, 3, GTK_FILL, 0, 4, 0);
891         gtk_label_set_selectable(GTK_LABEL(lbl_center_lat), TRUE);
892         gtk_misc_set_alignment(GTK_MISC(lbl_center_lat), 1.f, 0.5f);
893         gtk_table_attach(GTK_TABLE(mapman_info.tbl_area),
894                          lbl_center_lon = gtk_label_new(""),
895                          2, 3, 2, 3, GTK_FILL, 0, 4, 0);
896         gtk_label_set_selectable(GTK_LABEL(lbl_center_lon), TRUE);
897         gtk_misc_set_alignment(GTK_MISC(lbl_center_lon), 1.f, 0.5f);
898
899         /* default values for Top Left and Bottom Right are defined by the
900          * rectangle of the current and the previous Center */
901
902         /* Top Left. */
903         gtk_table_attach(GTK_TABLE(mapman_info.tbl_area),
904                          label = gtk_label_new(_("Top-Left")),
905                          0, 1, 3, 4, GTK_FILL, 0, 4, 0);
906         gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
907         gtk_table_attach(GTK_TABLE(mapman_info.tbl_area),
908                          mapman_info.txt_topleft_lat = gtk_entry_new(),
909                          1, 2, 3, 4, GTK_EXPAND | GTK_FILL, 0, 4, 0);
910         gtk_entry_set_alignment(GTK_ENTRY(mapman_info.txt_topleft_lat), 1.f);
911         gtk_table_attach(GTK_TABLE(mapman_info.tbl_area),
912                          mapman_info.txt_topleft_lon = gtk_entry_new(),
913                          2, 3, 3, 4, GTK_EXPAND | GTK_FILL, 0, 4, 0);
914         gtk_entry_set_alignment(GTK_ENTRY(mapman_info.txt_topleft_lon), 1.f);
915
916         /* Bottom Right. */
917         gtk_table_attach(GTK_TABLE(mapman_info.tbl_area),
918                          label = gtk_label_new(_("Bottom-Right")),
919                          0, 1, 4, 5, GTK_FILL, 0, 4, 0);
920         gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
921         gtk_table_attach(GTK_TABLE(mapman_info.tbl_area),
922                          mapman_info.txt_botright_lat = gtk_entry_new(),
923                          1, 2, 4, 5, GTK_EXPAND | GTK_FILL, 0, 4, 0);
924         gtk_entry_set_alignment(GTK_ENTRY(mapman_info.txt_botright_lat), 1.f);
925         gtk_table_attach(GTK_TABLE(mapman_info.tbl_area),
926                          mapman_info.txt_botright_lon = gtk_entry_new(),
927                          2, 3, 4, 5, GTK_EXPAND | GTK_FILL, 0, 4, 0);
928         gtk_entry_set_alignment(GTK_ENTRY(mapman_info.txt_botright_lon), 1.f);
929
930 #if defined(WITH_DEVICE_770) && !defined(WITH_HILDON_NEW)
931         /* Set hildon input hints */
932         g_object_set(G_OBJECT(mapman_info.txt_topleft_lon),
933                      HILDON_INPUT_MODE_HINT,
934                      HILDON_INPUT_MODE_HINT_NUMERICSPECIAL, NULL);
935         g_object_set(G_OBJECT(mapman_info.txt_topleft_lat),
936                      HILDON_INPUT_MODE_HINT,
937                      HILDON_INPUT_MODE_HINT_NUMERICSPECIAL, NULL);
938         g_object_set(G_OBJECT(mapman_info.txt_botright_lon),
939                      HILDON_INPUT_MODE_HINT,
940                      HILDON_INPUT_MODE_HINT_NUMERICSPECIAL, NULL);
941         g_object_set(G_OBJECT(mapman_info.txt_botright_lat),
942                      HILDON_INPUT_MODE_HINT,
943                      HILDON_INPUT_MODE_HINT_NUMERICSPECIAL, NULL);
944 #endif
945
946         /* Default action is to download by area. */
947         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mapman_info.rad_by_area), TRUE);
948
949         /* Initialize fields.  Do no use g_ascii_formatd; these strings will be
950          * output (and parsed) as locale-dependent. */
951
952         g_snprintf(buffer, sizeof(buffer), "%.06f", _gps->data.lat);
953         gtk_label_set_text(GTK_LABEL(lbl_gps_lat), buffer);
954         g_snprintf(buffer, sizeof(buffer), "%.06f", _gps->data.lon);
955         gtk_label_set_text(GTK_LABEL(lbl_gps_lon), buffer);
956
957         unit2latlon(_center.unitx, _center.unity, lat, lon);
958         g_snprintf(buffer, sizeof(buffer), "%.06f", lat);
959         gtk_label_set_text(GTK_LABEL(lbl_center_lat), buffer);
960         g_snprintf(buffer, sizeof(buffer), "%.06f", lon);
961         gtk_label_set_text(GTK_LABEL(lbl_center_lon), buffer);
962
963         /* Initialize to the bounds of the screen. */
964         unit2latlon(x2unit(0), y2unit(0), lat, lon);
965         g_snprintf(buffer, sizeof(buffer), "%.06f", lat);
966         gtk_entry_set_text(GTK_ENTRY(mapman_info.txt_topleft_lat), buffer);
967         g_snprintf(buffer, sizeof(buffer), "%.06f", lon);
968         gtk_entry_set_text(GTK_ENTRY(mapman_info.txt_topleft_lon), buffer);
969
970         unit2latlon(x2unit(_screen_width_pixels), y2unit(_screen_height_pixels), lat, lon);
971         g_snprintf(buffer, sizeof(buffer), "%.06f", lat);
972         gtk_entry_set_text(GTK_ENTRY(mapman_info.txt_botright_lat), buffer);
973         g_snprintf(buffer, sizeof(buffer), "%.06f", lon);
974         gtk_entry_set_text(GTK_ENTRY(mapman_info.txt_botright_lon), buffer);
975
976         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mapman_info.chk_zoom_levels[_zoom]), TRUE);
977
978         gtk_widget_show_all(dialog);
979
980         mapman_update_state(NULL, &mapman_info);
981
982         /* Connect signals. */
983         if (_curr_repo->type != REPOTYPE_NONE) {
984                 g_signal_connect(G_OBJECT(mapman_info.rad_download), "clicked", G_CALLBACK(mapman_update_state), &mapman_info);
985                 gtk_widget_set_sensitive(mapman_info.rad_download, TRUE);
986         } else {
987                 gtk_widget_set_sensitive(mapman_info.rad_download, FALSE);
988                 popup_error(dialog, _("NOTE: You must set a Map URI in the current repository in order to download maps."));
989         }
990
991         g_signal_connect(G_OBJECT(mapman_info.rad_delete), "clicked", G_CALLBACK(mapman_update_state), &mapman_info);
992         g_signal_connect(G_OBJECT(mapman_info.rad_by_area), "clicked", G_CALLBACK(mapman_update_state), &mapman_info);
993         g_signal_connect(G_OBJECT(mapman_info.rad_by_route), "clicked", G_CALLBACK(mapman_update_state), &mapman_info);
994
995         while (GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) {
996                 gboolean is_deleting = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(mapman_info.rad_delete));
997                 gboolean is_overwriting = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(mapman_info.chk_overwrite));
998                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(mapman_info.rad_by_route))) {
999                         if (mapman_by_route(&mapman_info, is_deleting, is_overwriting))
1000                                 break;
1001                 } else {
1002                         const gchar *text;
1003                         gchar *error_check;
1004                         gfloat start_lat, start_lon, end_lat, end_lon;
1005
1006                         text = gtk_entry_get_text(GTK_ENTRY(mapman_info.txt_topleft_lat));
1007                         start_lat = strtof(text, &error_check);
1008                         if (text == error_check || start_lat < -90.f || start_lat > 90.f) {
1009                                 popup_error(dialog, _("Invalid Top-Left Latitude"));
1010                                 continue;
1011                         }
1012
1013                         text = gtk_entry_get_text(GTK_ENTRY(mapman_info.txt_topleft_lon));
1014                         start_lon = strtof(text, &error_check);
1015                         if (text == error_check || start_lon < -180.f || start_lon > 180.f) {
1016                                 popup_error(dialog, _("Invalid Top-Left Longitude"));
1017                                 continue;
1018                         }
1019
1020                         text = gtk_entry_get_text(GTK_ENTRY(mapman_info.txt_botright_lat));
1021                         end_lat = strtof(text, &error_check);
1022                         if (text == error_check || end_lat < -90.f || end_lat > 90.f) {
1023                                 popup_error(dialog, _("Invalid Bottom-Right Latitude"));
1024                                 continue;
1025                         }
1026
1027                         text = gtk_entry_get_text(GTK_ENTRY(mapman_info.txt_botright_lon));
1028                         end_lon = strtof(text, &error_check);
1029                         if (text == error_check || end_lon < -180.f || end_lon > 180.f) {
1030                                 popup_error(dialog, _("Invalid Bottom-Right Longitude"));
1031                                 continue;
1032                         }
1033
1034                         if (mapman_by_area(start_lat, start_lon, end_lat, end_lon, &mapman_info, is_deleting, is_overwriting))
1035                                 break;
1036                 }
1037         }
1038
1039         gtk_widget_hide(dialog);        /* Destroying causes a crash (!?!?!??!) */
1040
1041         return TRUE;
1042 }