2 * This file is part of mapper
4 * Copyright (C) 2007 Kaj-Michael Lang
5 * Copyright (C) 2006-2007 John Costigan.
7 * POI and GPS-Info code originally written by Cezary Jackiewicz.
9 * Default map data provided by http://www.openstreetmap.org/
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.
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.
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.
37 #include <libgnomevfs/gnome-vfs.h>
39 #include "hildon-mapper.h"
47 #include "mapper-types.h"
48 #include "map-download.h"
49 #include "ui-common.h"
53 #define MAP_REPO_LIST_URL "http://www.gnuite.com/nokia770/maemo-mapper/repos.txt"
55 typedef struct _RepoManInfo RepoManInfo;
63 typedef struct _RepoEditInfo RepoEditInfo;
64 struct _RepoEditInfo {
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;
76 typedef struct _MapmanInfo MapmanInfo;
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;
91 GtkWidget *txt_topleft_lat;
92 GtkWidget *txt_topleft_lon;
93 GtkWidget *txt_botright_lat;
94 GtkWidget *txt_botright_lon;
97 GtkWidget *chk_zoom_levels[MAX_ZOOM];
100 void set_repo_type(RepoData * repo)
102 if (repo->url && *repo->url) {
103 gchar *url = g_utf8_strdown(repo->url, -1);
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;
115 repo->type = REPOTYPE_XYZ;
119 repo->type = REPOTYPE_NONE;
123 repo_make_cache_dir(gchar * name, const gchar * cache_dir, GtkWidget * parent)
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);
133 return g_file_test(cache_dir, G_FILE_TEST_EXISTS);
137 repo_set_curr(RepoData * rd)
140 return repo_make_cache_dir(rd->name, rd->cache_dir, _window);
144 repoman_dialog_select(GtkWidget * widget, RepoManInfo * rmi)
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);
152 repoman_dialog_browse(GtkWidget * widget, BrowseInfo * browse_info)
156 dialog = GTK_WIDGET(hildon_file_chooser_dialog_new
157 (GTK_WINDOW(browse_info->dialog),
158 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER));
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)));
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);
169 gtk_widget_destroy(dialog);
175 repoman_dialog_rename(GtkWidget * widget, RepoManInfo * rmi)
182 dialog = gtk_dialog_new_with_buttons(_("New Name"),
183 GTK_WINDOW(rmi->dialog),
184 GTK_DIALOG_MODAL, GTK_STOCK_OK,
187 GTK_RESPONSE_REJECT, NULL);
189 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox = gtk_hbox_new(FALSE, 4), FALSE, FALSE, 4);
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);
194 gtk_widget_show_all(dialog);
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);
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);
207 gtk_widget_destroy(dialog);
212 repoman_dialog_delete(GtkWidget * widget, RepoManInfo * rmi)
216 gint active = gtk_combo_box_get_active(GTK_COMBO_BOX(rmi->cmb_repos));
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."));
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);
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));
235 gtk_widget_destroy(confirm);
240 static RepoEditInfo *
241 repoman_dialog_add_repo(RepoManInfo * rmi, gchar * name)
247 RepoEditInfo *rei = g_new(RepoEditInfo, 1);
252 gtk_notebook_append_page(GTK_NOTEBOOK(rmi->notebook), vbox = gtk_vbox_new(FALSE, 4), gtk_label_new(name));
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);
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);
267 /* Initialize cache dir */
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);
276 gtk_box_pack_start(GTK_BOX(vbox), table = gtk_table_new(3, 2, FALSE), FALSE, FALSE, 0);
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);
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);
294 gtk_table_attach(GTK_TABLE(table), label = gtk_vseparator_new(), 2, 3, 0, 2, GTK_FILL, GTK_FILL, 4, 4);
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);
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);
304 rmi->repo_edits = g_list_append(rmi->repo_edits, rei);
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);
311 gtk_widget_show_all(vbox);
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);
321 repoman_dialog_new(GtkWidget * widget, RepoManInfo * rmi)
328 dialog = gtk_dialog_new_with_buttons(_("New Repository"),
329 GTK_WINDOW(rmi->dialog),
330 GTK_DIALOG_MODAL, GTK_STOCK_OK,
333 GTK_RESPONSE_REJECT, NULL);
335 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox = gtk_hbox_new(FALSE, 4), FALSE, FALSE, 4);
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);
340 gtk_widget_show_all(dialog);
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))));
347 gtk_widget_destroy(dialog);
353 repoman_reset(GtkWidget * widget, RepoManInfo * rmi)
357 confirm = hildon_note_new_confirmation(GTK_WINDOW(_window),
358 _("Replace all repositories with the default repository?"));
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));
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);
372 gtk_combo_box_set_active(GTK_COMBO_BOX(rmi->cmb_repos), 0);
374 gtk_widget_destroy(confirm);
380 repoman_download(GtkWidget * widget, RepoManInfo * rmi)
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. "
389 if (GTK_RESPONSE_OK == gtk_dialog_run(GTK_DIALOG(confirm))) {
394 GnomeVFSResult vfs_result;
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));
403 /* Parse each line as a reposotory. */
404 for (head = bytes; head && *head; head = tail) {
407 tail = strchr(head, '\n');
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);
422 gtk_widget_destroy(confirm);
432 GtkWidget *btn_rename;
433 GtkWidget *btn_delete;
435 GtkWidget *btn_reset;
436 GtkWidget *btn_download;
437 guint i, curr_repo_index = 0;
440 rmi.dialog = gtk_dialog_new_with_buttons(_("Manage Repositories"),
442 GTK_DIALOG_MODAL, GTK_STOCK_OK,
443 GTK_RESPONSE_ACCEPT, NULL);
445 help_dialog_help_enable(GTK_DIALOG(rmi.dialog), HELP_ID_REPOMAN);
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);
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);
458 gtk_dialog_add_button(GTK_DIALOG(rmi.dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT);
460 hbox = gtk_hbox_new(FALSE, 4);
462 gtk_box_pack_start(GTK_BOX(hbox), rmi.cmb_repos = gtk_combo_box_new_text(), TRUE, TRUE, 4);
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);
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);
473 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(rmi.notebook), FALSE);
474 gtk_notebook_set_show_border(GTK_NOTEBOOK(rmi.notebook), FALSE);
476 rmi.repo_edits = NULL;
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));
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)
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);
502 gtk_widget_show_all(rmi.dialog);
504 while (GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(rmi.dialog))) {
505 /* Iterate through repos and verify each. */
506 gboolean verified = TRUE;
509 gchar *old_curr_repo_name = _curr_repo->name;
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);
518 gtk_combo_box_set_active(GTK_COMBO_BOX(rmi.cmb_repos), i - 1);
522 /* But keep the repo list in memory, in case downloads are using it. */
525 /* Write new _repo_list. */
526 curr_repo_index = gtk_combo_box_get_active(GTK_COMBO_BOX(rmi.cmb_repos));
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));
540 _repo_list = g_list_append(_repo_list, rd);
542 if (!_curr_repo && !strcmp(old_curr_repo_name, rd->name))
544 else if (i == curr_repo_index)
548 repo_set_curr((RepoData *) g_list_first(_repo_list)->data);
550 menu_maps_add_repos();
555 gtk_widget_hide(rmi.dialog); /* Destroying causes a crash (!?!?!??!) */
556 map_set_zoom(_zoom); /* make sure we're at an appropriate zoom level. */
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)
565 guint start_unitx, start_unity, end_unitx, end_unity;
571 latlon2unit(start_lat, start_lon, start_unitx, start_unity);
572 latlon2unit(end_lat, end_lon, end_unitx, end_unity);
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;
580 if (start_unity > end_unity) {
581 guint swap = start_unity;
582 start_unity = end_unity;
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);
599 g_snprintf(buffer, sizeof(buffer), "%s %d %s", _("Confirm DELETION of"), num_maps, _("maps "));
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));
604 confirm = hildon_note_new_confirmation(GTK_WINDOW(mapman_info->dialog), buffer);
606 if (GTK_RESPONSE_OK != gtk_dialog_run(GTK_DIALOG(confirm))) {
607 gtk_widget_destroy(confirm);
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;
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);
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));
626 gtk_widget_destroy(confirm);
631 mapman_by_route(MapmanInfo * mapman_info, gboolean is_deleting, gboolean is_overwriting)
634 guint prev_tilex, prev_tiley, num_maps = 0, i;
637 guint radius = hildon_number_editor_get_value(HILDON_NUMBER_EDITOR(mapman_info->num_route_radius));
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]))) {
645 for (curr = _route.head - 1; curr++ != _route.tail;) {
647 guint tilex = unit2ztile(curr->unitx, i);
648 guint tiley = unit2ztile(curr->unity, i);
649 if (tilex != prev_tilex || tiley != prev_tiley) {
651 num_maps += (abs((gint) tilex - prev_tilex) + 1) * (abs((gint) tiley - prev_tiley) + 1) - 1;
659 num_maps *= 0.625 * pow(radius + 1, 1.85);
662 g_snprintf(buffer, sizeof(buffer), "%s %s %d %s",
663 _("Confirm DELETION of"), _("about"),
664 num_maps, _("maps "));
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));
672 confirm = hildon_note_new_confirmation(GTK_WINDOW(mapman_info->dialog), buffer);
674 if (GTK_RESPONSE_OK != gtk_dialog_run(GTK_DIALOG(confirm))) {
675 gtk_widget_destroy(confirm);
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]))) {
685 for (curr = _route.head - 1; curr++ != _route.tail;) {
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;
697 minx = tilex - radius;
698 miny = tiley - radius;
699 maxx = tilex + radius;
700 maxy = tiley + radius;
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));
713 _route_dl_radius = radius;
714 gtk_widget_destroy(confirm);
719 mapman_clear(GtkWidget * widget, MapmanInfo * mapman_info)
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);
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), "");
737 mapman_update_state(GtkWidget * widget, MapmanInfo * mapman_info)
739 gtk_widget_set_sensitive(mapman_info->chk_overwrite,
740 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(mapman_info->rad_download)));
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))
747 gtk_widget_hide(mapman_info->tbl_area);
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)));
754 gboolean menu_cb_mapman(GtkAction * action)
762 GtkWidget *lbl_gps_lat;
763 GtkWidget *lbl_gps_lon;
764 GtkWidget *lbl_center_lat;
765 GtkWidget *lbl_center_lon;
766 MapmanInfo mapman_info;
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);
775 help_dialog_help_enable(GTK_DIALOG(mapman_info.dialog), HELP_ID_MAPMAN);
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);
784 gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT);
786 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
787 mapman_info.notebook = gtk_notebook_new(), TRUE, TRUE, 0);
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);
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,
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);
810 gtk_box_pack_start(GTK_BOX(vbox),
811 gtk_hseparator_new(), FALSE, FALSE, 0);
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,
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),
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 =
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);
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")));
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);
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);
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);
899 /* default values for Top Left and Bottom Right are defined by the
900 * rectangle of the current and the previous Center */
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);
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);
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);
946 /* Default action is to download by area. */
947 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mapman_info.rad_by_area), TRUE);
949 /* Initialize fields. Do no use g_ascii_formatd; these strings will be
950 * output (and parsed) as locale-dependent. */
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);
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);
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);
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);
976 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mapman_info.chk_zoom_levels[_zoom]), TRUE);
978 gtk_widget_show_all(dialog);
980 mapman_update_state(NULL, &mapman_info);
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);
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."));
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);
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))
1004 gfloat start_lat, start_lon, end_lat, end_lon;
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"));
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"));
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"));
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"));
1034 if (mapman_by_area(start_lat, start_lon, end_lat, end_lon, &mapman_info, is_deleting, is_overwriting))
1039 gtk_widget_hide(dialog); /* Destroying causes a crash (!?!?!??!) */