]> git.itanic.dy.fi Git - maemo-mapper/blob - src/poi.c
Added basic APRS support - Can be disabled by removing definition of INCLUDE_APRS
[maemo-mapper] / src / poi.c
1 /*
2  * Copyright (C) 2006, 2007 John Costigan.
3  *
4  * POI and GPS-Info code originally written by Cezary Jackiewicz.
5  *
6  * Default map data provided by http://www.openstreetmap.org/
7  *
8  * This file is part of Maemo Mapper.
9  *
10  * Maemo Mapper is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * Maemo Mapper is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Maemo Mapper.  If not, see <http://www.gnu.org/licenses/>.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #    include "config.h"
26 #endif
27
28 #define _GNU_SOURCE
29
30 #include <string.h>
31 #include <math.h>
32
33 #ifndef LEGACY
34 #    include <hildon/hildon-help.h>
35 #    include <hildon/hildon-note.h>
36 #    include <hildon/hildon-file-chooser-dialog.h>
37 #    include <hildon/hildon-number-editor.h>
38 #    include <hildon/hildon-banner.h>
39 #else
40 #    include <osso-helplib.h>
41 #    include <hildon-widgets/hildon-note.h>
42 #    include <hildon-widgets/hildon-file-chooser-dialog.h>
43 #    include <hildon-widgets/hildon-number-editor.h>
44 #    include <hildon-widgets/hildon-banner.h>
45 #    include <hildon-widgets/hildon-input-mode-hint.h>
46 #endif
47
48 #include <sqlite3.h>
49
50 #include "types.h"
51 #include "data.h"
52 #include "defines.h"
53
54 #include "display.h"
55 #include "gdk-pixbuf-rotate.h"
56 #include "gpx.h"
57 #include "main.h"
58 #include "poi.h"
59 #include "util.h"
60
61 static sqlite3 *_poi_db = NULL;
62 static sqlite3_stmt *_stmt_browse_poi = NULL;
63 static sqlite3_stmt *_stmt_browsecat_poi = NULL;
64 static sqlite3_stmt *_stmt_select_poi = NULL;
65 static sqlite3_stmt *_stmt_select_nearest_poi = NULL;
66 static sqlite3_stmt *_stmt_insert_poi = NULL;
67 static sqlite3_stmt *_stmt_update_poi = NULL;
68 static sqlite3_stmt *_stmt_delete_poi = NULL;
69 static sqlite3_stmt *_stmt_delete_poi_by_catid = NULL;
70 static sqlite3_stmt *_stmt_nextlabel_poi = NULL;
71 static sqlite3_stmt *_stmt_select_cat = NULL;
72 static sqlite3_stmt *_stmt_insert_cat = NULL;
73 static sqlite3_stmt *_stmt_update_cat = NULL;
74 static sqlite3_stmt *_stmt_delete_cat = NULL;
75 static sqlite3_stmt *_stmt_toggle_cat = NULL;
76 static sqlite3_stmt *_stmt_selall_cat = NULL;
77
78 typedef struct _PoiListInfo PoiListInfo;
79 struct _PoiListInfo
80 {
81     GtkWidget *dialog;
82     GtkWidget *dialog2;
83     GtkTreeViewColumn *select_column;
84     GtkWidget *tree_view;
85     gboolean select_all;
86 };
87
88 typedef struct _OriginToggleInfo OriginToggleInfo;
89 struct _OriginToggleInfo {
90     GtkWidget *rad_use_gps;
91     GtkWidget *rad_use_route;
92     GtkWidget *rad_use_text;
93     GtkWidget *txt_origin;
94     GtkWidget *txt_query;
95 };
96
97 typedef struct _PoiCategoryEditInfo PoiCategoryEditInfo;
98 struct _PoiCategoryEditInfo
99 {
100     GtkWidget *dialog;
101     GtkWidget *cmb_category;
102     gint cat_id;
103     GtkWidget *tree_view;
104 };
105
106 /** Data used during action: add or edit category/poi **/
107 typedef struct _DeletePOI DeletePOI;
108 struct _DeletePOI {
109     GtkWidget *dialog;
110     gchar *txt_label;
111     gint id;
112     gboolean deleted;
113 };
114
115 void
116 poi_db_connect()
117 {
118     gchar buffer[100];
119     gchar **pszResult;
120     gint nRow, nColumn;
121     gchar *db_dirname = NULL;
122     printf("%s()\n", __PRETTY_FUNCTION__);
123
124     if(_poi_db)
125     {
126         sqlite3_close(_poi_db);
127         _poi_db = NULL;
128     }
129
130     if(!_poi_db_filename)
131     {
132         /* Do nothing. */
133     }
134     else if(NULL == (db_dirname = g_path_get_dirname(_poi_db_filename))
135             || (g_mkdir_with_parents(db_dirname, 0755), /* comma operator */
136                 (SQLITE_OK != (sqlite3_open(_poi_db_filename, &_poi_db)))))
137     {
138         gchar buffer2[BUFFER_SIZE];
139         snprintf(buffer2, sizeof(buffer2),
140                 "%s: %s", _("Error with POI database"),
141                 sqlite3_errmsg(_poi_db));
142         sqlite3_close(_poi_db);
143         _poi_db = NULL;
144         popup_error(_window, buffer2);
145     }
146     else if(SQLITE_OK != sqlite3_get_table(_poi_db,
147                 "select label from poi limit 1",
148                 &pszResult, &nRow, &nColumn, NULL))
149     {
150         gchar *create_sql = sqlite3_mprintf(
151                 "create table poi (poi_id integer PRIMARY KEY, lat real, "
152                 "lon real, label text, desc text, cat_id integer);"
153                 "create table category (cat_id integer PRIMARY KEY,"
154                 "label text, desc text, enabled integer);"
155                 /* Add some default categories... */
156                 "insert into category (label, desc, enabled) "
157                     "values ('%q', '%q', 1); "
158                 "insert into category (label, desc, enabled) "
159                     "values ('%q', '%q', 1); "
160                 "insert into category (label, desc, enabled) "
161                     "values ('%q', '%q', 1); "
162                 "insert into category (label, desc, enabled) "
163                     "values ('%q', '%q', 1); "
164                 "insert into category (label, desc, enabled) "
165                     "values ('%q', '%q', 1); "
166                 "insert into category (label, desc, enabled) "
167                     "values ('%q', '%q', 1); "
168                 "insert into category (label, desc, enabled) "
169                     "values ('%q', '%q', 1); "
170                 "insert into category (label, desc, enabled) "
171                     "values ('%q', '%q', 1); "
172                 "insert into category (label, desc, enabled) "
173                     "values ('%q', '%q', 1); "
174                 "insert into category (label, desc, enabled) "
175                     "values ('%q', '%q', 1); "
176                 "insert into category (label, desc, enabled) "
177                     "values ('%q', '%q', 1); ",
178                 _("Service Station"),
179                 _("Stations for purchasing fuel for vehicles."),
180                 _("Residence"),
181                 _("Houses, apartments, or other residences of import."),
182                 _("Restaurant"),
183                 _("Places to eat or drink."),
184                 _("Shopping/Services"),
185                 _("Places to shop or acquire services."),
186                 _("Recreation"),
187                 _("Indoor or Outdoor places to have fun."),
188                 _("Transportation"),
189                 _("Bus stops, airports, train stations, etc."),
190                 _("Lodging"),
191                 _("Places to stay temporarily or for the night."),
192                 _("School"),
193                 _("Elementary schools, college campuses, etc."),
194                 _("Business"),
195                 _("General places of business."),
196                 _("Landmark"),
197                 _("General landmarks."),
198                 _("Other"),
199                 _("Miscellaneous category for everything else."));
200
201         if(SQLITE_OK != sqlite3_exec(_poi_db, create_sql, NULL, NULL, NULL)
202                 && (SQLITE_OK != sqlite3_get_table(_poi_db,
203                         "select label from poi limit 1",
204                         &pszResult, &nRow, &nColumn, NULL)))
205         {
206             snprintf(buffer, sizeof(buffer), "%s:\n%s",
207                     _("Failed to open or create database"),
208                     sqlite3_errmsg(_poi_db));
209             sqlite3_close(_poi_db);
210             _poi_db = NULL;
211             popup_error(_window, buffer);
212         }
213     }
214     else
215         sqlite3_free_table(pszResult);
216
217     g_free(db_dirname);
218
219     if(_poi_db)
220     {
221         /* Prepare our SQL statements. */
222         /* browse poi */
223         sqlite3_prepare(_poi_db,
224                         "select p.poi_id, p.cat_id, p.lat, p.lon,"
225                         " p.label, p.desc, c.label"
226                         " from poi p inner join category c"
227                         "   on p.cat_id = c.cat_id"
228                         " where c.enabled = 1"
229                         " and p.label like $QUERY or p.desc like $QUERY"
230                         " order by (($LAT - p.lat) * ($LAT - p.lat) "
231                                  "+ ($LON - p.lon) * ($LON - p.lon)) DESC "
232                         " limit 100",
233                         -1, &_stmt_browse_poi, NULL);
234
235         /* browse poi by category */
236         sqlite3_prepare(_poi_db,
237                         "select p.poi_id, p.cat_id, p.lat, p.lon,"
238                         " p.label, p.desc, c.label"
239                         " from poi p inner join category c"
240                         "   on p.cat_id = c.cat_id"
241                         " where c.enabled = 1"
242                         " and p.cat_id = $CATID"
243                         " and ( p.label like $QUERY or p.desc like $QUERY )"
244                         " order by (($LAT - p.lat) * ($LAT - p.lat) "
245                                  "+ ($LON - p.lon) * ($LON - p.lon)) DESC"
246                         " limit 100",
247                         -1, &_stmt_browsecat_poi, NULL);
248
249         /* Prepare our SQL statements. */
250         /* select from poi */
251         sqlite3_prepare(_poi_db,
252                         "select p.lat, p.lon, p.poi_id, p.label, p.desc,"
253                         " p.cat_id, c.label, c.desc"
254                         " from poi p inner join category c"
255                         "    on p.cat_id = c.cat_id"
256                         " where c.enabled = 1"
257                         " and p.lat between ? and ? "
258                         " and p.lon between ? and ? ",
259                         -1, &_stmt_select_poi, NULL);
260
261         /* select nearest pois */
262         sqlite3_prepare(_poi_db,
263                         "select p.poi_id, p.cat_id, p.lat, p.lon,"
264                         " p.label, p.desc, c.label"
265                         " from poi p inner join category c"
266                         "   on p.cat_id = c.cat_id"
267                         " where c.enabled = 1"
268                         " order by (($LAT - p.lat) * ($LAT - p.lat) "
269                                  "+ ($LON - p.lon) * ($LON - p.lon)) limit 1",
270                         -1, &_stmt_select_nearest_poi, NULL);
271
272         /* insert poi */
273         sqlite3_prepare(_poi_db,
274                             "insert into poi (lat, lon, label, desc, cat_id)"
275                             " values (?, ?, ?, ?, ?)",
276                         -1, &_stmt_insert_poi, NULL);
277         /* update poi */
278         sqlite3_prepare(_poi_db,
279                             "update poi set lat = ?, lon = ?, "
280                             "label = ?, desc = ?, cat_id = ? where poi_id = ?",
281                         -1, &_stmt_update_poi, NULL);
282         /* delete from poi */
283         sqlite3_prepare(_poi_db,
284                         " delete from poi where poi_id = ?",
285                         -1, &_stmt_delete_poi, NULL);
286         /* delete from poi by cat_id */
287         sqlite3_prepare(_poi_db,
288                         "delete from poi where cat_id = ?",
289                         -1, &_stmt_delete_poi_by_catid, NULL);
290         /* get next poilabel */
291         sqlite3_prepare(_poi_db,
292                         "select ifnull(max(poi_id) + 1,1) from poi",
293                         -1, &_stmt_nextlabel_poi, NULL);
294
295         /* select from category */
296         sqlite3_prepare(_poi_db,
297                         "select c.label, c.desc, c.enabled"
298                         " from category c where c.cat_id = ?",
299                         -1, &_stmt_select_cat, NULL);
300         /* insert into category */
301         sqlite3_prepare(_poi_db,
302                         "insert into category (label, desc, enabled)"
303                         " values (?, ?, ?)",
304                         -1, &_stmt_insert_cat, NULL);
305         /* update category */
306         sqlite3_prepare(_poi_db,
307                         "update category set label = ?, desc = ?,"
308                         " enabled = ? where cat_id = ?",
309                         -1, &_stmt_update_cat, NULL);
310         /* delete from category */
311         sqlite3_prepare(_poi_db,
312                         "delete from category where cat_id = ?",
313                         -1, &_stmt_delete_cat, NULL);
314         /* enable category */
315         sqlite3_prepare(_poi_db,
316                         "update category set enabled = ?"
317                         " where cat_id = ?",
318                         -1, &_stmt_toggle_cat, NULL);
319         /* select all category */
320         sqlite3_prepare(_poi_db,
321                         "select c.cat_id, c.label, c.desc, c.enabled,"
322                         " count(p.poi_id)"
323                         " from category c"
324                         " left outer join poi p on c.cat_id = p.cat_id"
325                         " group by c.cat_id, c.label, c.desc, c.enabled "
326                         " order by c.label",
327                         -1, &_stmt_selall_cat, NULL);
328     }
329
330     _poi_enabled = _poi_db != NULL;
331
332     gtk_widget_set_sensitive(_menu_poi_item, _poi_enabled);
333     gtk_widget_set_sensitive(_cmenu_loc_add_poi_item, _poi_enabled);
334     gtk_widget_set_sensitive(_cmenu_loc_download_poi_item, _poi_enabled);
335     gtk_widget_set_sensitive(_cmenu_loc_browse_poi_item, _poi_enabled);
336     gtk_widget_set_sensitive(_cmenu_way_add_poi_item, _poi_enabled);
337     gtk_widget_set_sensitive(_cmenu_poi_submenu, _poi_enabled);
338
339     vprintf("%s(): return\n", __PRETTY_FUNCTION__);
340 }
341
342 gboolean
343 get_nearest_poi(gint unitx, gint unity, PoiInfo *poi)
344 {
345     printf("%s(%d, %d)\n", __PRETTY_FUNCTION__, unitx, unity);
346     gboolean result;
347     gdouble lat, lon;
348     unit2latlon(unitx, unity, lat, lon);
349
350     if(SQLITE_OK == sqlite3_bind_double(_stmt_select_nearest_poi, 1, lat)
351     && SQLITE_OK == sqlite3_bind_double(_stmt_select_nearest_poi, 2, lon)
352         && SQLITE_ROW == sqlite3_step(_stmt_select_nearest_poi))
353     {
354         poi->poi_id = sqlite3_column_int(_stmt_select_nearest_poi, 0);
355         poi->cat_id = sqlite3_column_int(_stmt_select_nearest_poi, 1);
356         poi->lat = sqlite3_column_double(_stmt_select_nearest_poi, 2);
357         poi->lon = sqlite3_column_double(_stmt_select_nearest_poi, 3);
358         poi->label =g_strdup(sqlite3_column_text(_stmt_select_nearest_poi, 4));
359         poi->desc = g_strdup(sqlite3_column_text(_stmt_select_nearest_poi, 5));
360         poi->clabel=g_strdup(sqlite3_column_text(_stmt_select_nearest_poi, 6));
361         result = TRUE;
362     }
363     else
364         result = FALSE;
365     sqlite3_reset(_stmt_select_nearest_poi);
366     vprintf("%s(): return %d\n", __PRETTY_FUNCTION__, result);
367     return result;
368 }
369
370 gboolean
371 select_poi(gint unitx, gint unity, PoiInfo *poi, gboolean quick)
372 {
373     gint x, y;
374     gdouble lat1, lon1, lat2, lon2;
375     static GtkWidget *dialog = NULL;
376     static GtkWidget *list = NULL;
377     static GtkWidget *sw = NULL;
378     static GtkTreeViewColumn *column = NULL;
379     static GtkCellRenderer *renderer = NULL;
380     GtkListStore *store = NULL;
381     GtkTreeIter iter;
382     gboolean selected = FALSE;
383     gchar tmp1[LL_FMT_LEN], tmp2[LL_FMT_LEN];
384     gint num_cats = 0;
385     printf("%s()\n", __PRETTY_FUNCTION__);
386
387     x = unitx - pixel2unit(3 * _draw_width);
388     y = unity + pixel2unit(3 * _draw_width);
389     unit2latlon(x, y, lat1, lon1);
390
391     x = unitx + pixel2unit(3 * _draw_width);
392     y = unity - pixel2unit(3 * _draw_width);
393     unit2latlon(x, y, lat2, lon2);
394
395     if(SQLITE_OK != sqlite3_bind_double(_stmt_select_poi, 1, lat1) ||
396           SQLITE_OK != sqlite3_bind_double(_stmt_select_poi, 2, lat2) ||
397           SQLITE_OK != sqlite3_bind_double(_stmt_select_poi, 3, lon1) ||
398           SQLITE_OK != sqlite3_bind_double(_stmt_select_poi, 4, lon2))
399     {
400         g_printerr("Failed to bind values for _stmt_select_poi\n");
401         return FALSE;
402     }
403
404     /* Initialize store. */
405     store = gtk_list_store_new(POI_NUM_COLUMNS,
406                                G_TYPE_BOOLEAN,/* Selected */
407                                G_TYPE_INT,    /* POI ID */
408                                G_TYPE_INT,    /* Category ID */
409                                G_TYPE_DOUBLE,  /* Latitude */
410                                G_TYPE_DOUBLE,  /* Longitude */
411                                G_TYPE_STRING, /* Lat/Lon */
412                                G_TYPE_FLOAT,  /* Bearing */
413                                G_TYPE_FLOAT,  /* Distance */
414                                G_TYPE_STRING, /* POI Label */
415                                G_TYPE_STRING, /* POI Desc. */
416                                G_TYPE_STRING);/* Category Label */
417
418     while(SQLITE_ROW == sqlite3_step(_stmt_select_poi))
419     {
420         gdouble lat, lon;
421         lat = sqlite3_column_double(_stmt_select_poi, 0);
422         lon = sqlite3_column_double(_stmt_select_poi, 1);
423         
424         format_lat_lon(lat, lon, tmp1, tmp2);
425         //lat_format(lat, tmp1);
426         //lon_format(lon, tmp2);
427         gtk_list_store_append(store, &iter);
428         gtk_list_store_set(store, &iter,
429                 POI_POIID, sqlite3_column_int(_stmt_select_poi, 2),
430                 POI_CATID, sqlite3_column_int(_stmt_select_poi, 5),
431                 POI_LAT, lat,
432                 POI_LON, lon,
433                 POI_LATLON, g_strdup_printf("%s, %s", tmp1, tmp2),
434                 POI_LABEL, sqlite3_column_text(_stmt_select_poi, 3),
435                 POI_DESC, sqlite3_column_text(_stmt_select_poi, 4),
436                 POI_CLABEL, sqlite3_column_text(_stmt_select_poi, 6),
437                 -1);
438         num_cats++;
439     }
440     sqlite3_reset(_stmt_select_poi);
441
442     switch(num_cats)
443     {
444         case 0:
445             g_object_unref(G_OBJECT(store));
446             if(!quick)
447             {
448                 MACRO_BANNER_SHOW_INFO(_window, _("No POIs found."));
449             }
450             return FALSE;
451             break;
452         case 1:
453             /* iter is still set to the most-recently added POI. */
454             gtk_tree_model_get(GTK_TREE_MODEL(store),
455                 &iter,
456                 POI_POIID, &(poi->poi_id),
457                 POI_CATID, &(poi->cat_id),
458                 POI_LAT, &(poi->lat),
459                 POI_LON, &(poi->lon),
460                 POI_LABEL, &(poi->label),
461                 POI_DESC, &(poi->desc),
462                 POI_CLABEL, &(poi->clabel),
463                 -1);
464             g_object_unref(G_OBJECT(store));
465             return TRUE;
466             break;
467         default:
468             if(quick)
469             {
470                 g_object_unref(G_OBJECT(store));
471                 return get_nearest_poi(unitx, unity, poi);
472             }
473     }
474
475     /* There are at least 2 matching POI's - let the user select one. */
476     if(dialog == NULL)
477     {
478         dialog = gtk_dialog_new_with_buttons(_("Select POI"),
479                 GTK_WINDOW(_window), GTK_DIALOG_MODAL,
480                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
481                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
482                 NULL);
483
484         gtk_window_set_default_size(GTK_WINDOW(dialog), 500, 300);
485
486         sw = gtk_scrolled_window_new (NULL, NULL);
487         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
488                 GTK_SHADOW_ETCHED_IN);
489         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
490                 GTK_POLICY_NEVER,
491                 GTK_POLICY_AUTOMATIC);
492         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
493                 sw, TRUE, TRUE, 0);
494
495         list = gtk_tree_view_new();
496         gtk_container_add(GTK_CONTAINER(sw), list);
497
498         gtk_tree_selection_set_mode(
499                 gtk_tree_view_get_selection(GTK_TREE_VIEW(list)),
500                 GTK_SELECTION_SINGLE);
501         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(list), TRUE);
502
503         renderer = gtk_cell_renderer_text_new();
504         column = gtk_tree_view_column_new_with_attributes(
505                 _("Location"), renderer, "text", POI_LATLON, NULL);
506         gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
507
508         renderer = gtk_cell_renderer_text_new();
509         column = gtk_tree_view_column_new_with_attributes(
510                 _("Label"), renderer, "text", POI_LABEL, NULL);
511         gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
512
513         renderer = gtk_cell_renderer_text_new();
514         column = gtk_tree_view_column_new_with_attributes(
515                 _("Category"), renderer, "text", POI_CLABEL, NULL);
516         gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
517     }
518
519     gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));
520     g_object_unref(G_OBJECT(store));
521
522     gtk_widget_show_all(dialog);
523
524     while(GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog)))
525     {
526         if(gtk_tree_selection_get_selected(
527                     gtk_tree_view_get_selection(GTK_TREE_VIEW(list)),
528                     NULL, &iter))
529         {
530             gtk_tree_model_get(GTK_TREE_MODEL(store), &iter,
531                 POI_POIID, &(poi->poi_id),
532                 POI_CATID, &(poi->cat_id),
533                 POI_LAT, &(poi->lat),
534                 POI_LON, &(poi->lon),
535                 POI_LABEL, &(poi->label),
536                 POI_DESC, &(poi->desc),
537                 POI_CLABEL, &(poi->clabel),
538                 -1);
539             selected = TRUE;
540             break;
541         }
542         else
543             popup_error(dialog, _("Select one POI from the list."));
544     }
545
546     map_force_redraw();
547
548     gtk_widget_hide(dialog);
549
550     vprintf("%s(): return %d\n", __PRETTY_FUNCTION__, selected);
551     return selected;
552 }
553
554 static gboolean
555 category_delete(GtkWidget *widget, DeletePOI *dpoi)
556 {
557     GtkWidget *confirm;
558     gint i;
559     gchar *buffer;
560     printf("%s()\n", __PRETTY_FUNCTION__);
561
562     buffer = g_strdup_printf("%s\n\t%s\n%s",
563             _("Delete category?"),
564             dpoi->txt_label,
565             _("WARNING: All POIs in that category will also be deleted!"));
566     confirm = hildon_note_new_confirmation(GTK_WINDOW(dpoi->dialog), buffer);
567     g_free(buffer);
568     i = gtk_dialog_run(GTK_DIALOG(confirm));
569     gtk_widget_destroy(GTK_WIDGET(confirm));
570
571     if(i == GTK_RESPONSE_OK)
572     {
573         /* delete dpoi->poi_id */
574         if(SQLITE_OK != sqlite3_bind_int(_stmt_delete_poi_by_catid, 1,
575                     dpoi->id) ||
576            SQLITE_DONE != sqlite3_step(_stmt_delete_poi_by_catid))
577         {
578             MACRO_BANNER_SHOW_INFO(dpoi->dialog, _("Error deleting POI"));
579             sqlite3_reset(_stmt_delete_poi_by_catid);
580             return FALSE;
581         }
582         sqlite3_reset(_stmt_delete_poi_by_catid);
583
584         if(SQLITE_OK != sqlite3_bind_int(_stmt_delete_cat, 1, dpoi->id) ||
585            SQLITE_DONE != sqlite3_step(_stmt_delete_cat))
586         {
587             MACRO_BANNER_SHOW_INFO(dpoi->dialog, _("Error deleting category"));
588             sqlite3_reset(_stmt_delete_cat);
589             return FALSE;
590         }
591         sqlite3_reset(_stmt_delete_cat);
592
593         map_force_redraw();
594     }
595     gtk_widget_destroy(confirm);
596
597     vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
598     return TRUE;
599 }
600
601 gboolean
602 category_edit_dialog(GtkWidget *parent, gint cat_id)
603 {
604     gchar *cat_label = NULL, *cat_desc = NULL;
605     gint cat_enabled;
606     GtkWidget *dialog;
607     GtkWidget *table;
608     GtkWidget *label;
609     GtkWidget *txt_label;
610     GtkWidget *txt_desc;
611     GtkWidget *btn_delete = NULL;
612     GtkWidget *txt_scroll;
613     GtkWidget *chk_enabled;
614     GtkTextBuffer *desc_txt;
615     GtkTextIter begin, end;
616     gboolean results = TRUE;
617     DeletePOI dpoi = {NULL, NULL, 0};
618     printf("%s()\n", __PRETTY_FUNCTION__);
619
620     if(cat_id > 0)
621     {
622         if(SQLITE_OK != sqlite3_bind_double(_stmt_select_cat, 1, cat_id) ||
623            SQLITE_ROW != sqlite3_step(_stmt_select_cat))
624         {
625             vprintf("%s(): return FALSE\n", __PRETTY_FUNCTION__);
626             sqlite3_reset(_stmt_select_cat);
627             return FALSE;
628         }
629
630         cat_label = g_strdup(sqlite3_column_text(_stmt_select_cat, 0));
631         cat_desc = g_strdup(sqlite3_column_text(_stmt_select_cat, 1));
632         cat_enabled = sqlite3_column_int(_stmt_select_cat, 2);
633
634         sqlite3_reset(_stmt_select_cat);
635
636         dialog = gtk_dialog_new_with_buttons(_("Edit Category"),
637             GTK_WINDOW(parent), GTK_DIALOG_MODAL,
638             GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
639             NULL);
640
641         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
642                 btn_delete = gtk_button_new_with_label(_("Delete...")));
643
644         dpoi.dialog = dialog;
645         dpoi.txt_label = g_strdup(cat_label);
646         dpoi.id = cat_id;
647         dpoi.deleted = FALSE;
648
649         g_signal_connect(G_OBJECT(btn_delete), "clicked",
650                           G_CALLBACK(category_delete), &dpoi);
651
652         gtk_dialog_add_button(GTK_DIALOG(dialog),
653                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT);
654     }
655     else
656     {
657         cat_enabled = 1;
658         cat_label = g_strdup("");
659         cat_id = -1;
660         cat_desc = g_strdup("");
661
662         dialog = gtk_dialog_new_with_buttons(_("Add Category"),
663             GTK_WINDOW(parent), GTK_DIALOG_MODAL,
664             GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
665             GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
666             NULL);
667     }
668
669     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
670             table = gtk_table_new(6, 4, FALSE), TRUE, TRUE, 0);
671
672     gtk_table_attach(GTK_TABLE(table),
673             label = gtk_label_new(_("Label")),
674             0, 1, 0, 1, GTK_FILL, 0, 2, 4);
675     gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
676     gtk_table_attach(GTK_TABLE(table),
677             txt_label = gtk_entry_new(),
678             1, 2, 0, 1, GTK_EXPAND | GTK_FILL, 0, 2, 4);
679
680     gtk_table_attach(GTK_TABLE(table),
681             label = gtk_label_new(_("Description")),
682             0, 1, 1, 2, GTK_FILL, 0, 2, 4);
683     gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
684
685     txt_scroll = gtk_scrolled_window_new(NULL, NULL);
686     gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(txt_scroll),
687                                    GTK_SHADOW_IN);
688     gtk_table_attach(GTK_TABLE(table),
689             txt_scroll,
690             1, 2, 1, 2, GTK_EXPAND | GTK_FILL, 0, 2, 4);
691
692     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(txt_scroll),
693                                  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
694
695     txt_desc = gtk_text_view_new();
696     gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(txt_desc), GTK_WRAP_WORD);
697
698     gtk_container_add(GTK_CONTAINER(txt_scroll), txt_desc);
699     gtk_widget_set_size_request(GTK_WIDGET(txt_scroll), 400, 60);
700
701     desc_txt = gtk_text_view_get_buffer(GTK_TEXT_VIEW(txt_desc));
702
703     gtk_table_attach(GTK_TABLE(table),
704             chk_enabled = gtk_check_button_new_with_label(
705                 _("Enabled")),
706             0, 2, 2, 3, GTK_EXPAND | GTK_FILL, 0, 2, 4);
707
708     /* label */
709     gtk_entry_set_text(GTK_ENTRY(txt_label), cat_label);
710
711     /* desc */
712     gtk_text_buffer_set_text(desc_txt, cat_desc, -1);
713
714     /* enabled */
715     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(chk_enabled),
716             (cat_enabled == 1 ? TRUE : FALSE));
717
718     g_free(cat_label);
719     cat_label = NULL;
720     g_free(cat_desc);
721     cat_desc = NULL;
722
723     gtk_widget_show_all(dialog);
724
725     while(GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog)))
726     {
727         if(strlen(gtk_entry_get_text(GTK_ENTRY(txt_label))))
728             cat_label = g_strdup(gtk_entry_get_text(GTK_ENTRY(txt_label)));
729         else
730         {
731             popup_error(dialog, _("Please specify a name for the category."));
732             continue;
733         }
734
735         gtk_text_buffer_get_iter_at_offset(desc_txt, &begin,0 );
736         gtk_text_buffer_get_end_iter (desc_txt, &end);
737         cat_desc = gtk_text_buffer_get_text(desc_txt, &begin, &end, TRUE);
738
739         cat_enabled = (gtk_toggle_button_get_active(
740                 GTK_TOGGLE_BUTTON(chk_enabled)) ? 1 : 0);
741
742         if(cat_id > 0)
743         {
744             /* edit category */
745             if(SQLITE_OK != sqlite3_bind_text(_stmt_update_cat, 1, cat_label,
746                         -1, g_free) ||
747                SQLITE_OK != sqlite3_bind_text(_stmt_update_cat, 2, cat_desc,
748                         -1, g_free) ||
749                SQLITE_OK != sqlite3_bind_int(_stmt_update_cat, 3,cat_enabled)||
750                SQLITE_OK != sqlite3_bind_int(_stmt_update_cat, 4, cat_id) ||
751                SQLITE_DONE != sqlite3_step(_stmt_update_cat))
752             {
753                 MACRO_BANNER_SHOW_INFO(parent,_("Error updating category"));
754                 results = FALSE;
755             }
756             sqlite3_reset(_stmt_update_cat);
757         }
758         else
759         {
760             /* add category */
761             if(SQLITE_OK != sqlite3_bind_text(_stmt_insert_cat, 1, cat_label,
762                         -1, g_free) ||
763                SQLITE_OK != sqlite3_bind_text(_stmt_insert_cat, 2, cat_desc,
764                         -1, g_free) ||
765                SQLITE_OK != sqlite3_bind_int(_stmt_insert_cat, 3,cat_enabled)||
766                SQLITE_DONE != sqlite3_step(_stmt_insert_cat))
767             {
768                 MACRO_BANNER_SHOW_INFO(parent, _("Error adding category"));
769                 results = FALSE;
770             }
771             sqlite3_reset(_stmt_insert_cat);
772         }
773         break;
774     }
775
776     g_free(dpoi.txt_label);
777
778     g_object_unref (desc_txt);
779
780     if(results)
781         map_force_redraw();
782
783     gtk_widget_hide(dialog);
784
785     vprintf("%s(): return\n", __PRETTY_FUNCTION__);
786     return results;
787 }
788
789 static void
790 category_toggled(GtkCellRendererToggle *cell, gchar *path, GtkListStore *data)
791 {
792     GtkTreeIter iter;
793     gboolean cat_enabled;
794     gint cat_id;
795     printf("%s()\n", __PRETTY_FUNCTION__);
796
797     GtkTreeModel *model = GTK_TREE_MODEL(data);
798     if( !gtk_tree_model_get_iter_from_string(model, &iter, path) )
799         return;
800
801     gtk_tree_model_get(model, &iter,
802             CAT_ENABLED, &cat_enabled,
803             CAT_ID, &cat_id,
804             -1);
805
806     cat_enabled ^= 1;
807
808     if(SQLITE_OK != sqlite3_bind_int(_stmt_toggle_cat, 1, cat_enabled) ||
809        SQLITE_OK != sqlite3_bind_int(_stmt_toggle_cat, 2, cat_id) ||
810        SQLITE_DONE != sqlite3_step(_stmt_toggle_cat))
811     {
812         MACRO_BANNER_SHOW_INFO(_window, _("Error updating Category"));
813     }
814     else
815     {
816         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
817                    CAT_ENABLED, cat_enabled, -1);
818         map_force_redraw();
819     }
820
821     sqlite3_reset(_stmt_toggle_cat);
822
823     vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
824 }
825
826 static GtkListStore*
827 generate_store()
828 {
829     GtkTreeIter iter;
830     GtkListStore *store;
831     printf("%s()\n", __PRETTY_FUNCTION__);
832
833     store = gtk_list_store_new(CAT_NUM_COLUMNS,
834                                G_TYPE_UINT,
835                                G_TYPE_BOOLEAN,
836                                G_TYPE_STRING,
837                                G_TYPE_STRING,
838                                G_TYPE_UINT);
839
840     while(SQLITE_ROW == sqlite3_step(_stmt_selall_cat))
841     {
842         gtk_list_store_append(store, &iter);
843         gtk_list_store_set(store, &iter,
844                 CAT_ID, sqlite3_column_int(_stmt_selall_cat, 0),
845                 CAT_ENABLED, sqlite3_column_int(_stmt_selall_cat, 3),
846                 CAT_LABEL, sqlite3_column_text(_stmt_selall_cat, 1),
847                 CAT_DESC, sqlite3_column_text(_stmt_selall_cat, 2),
848                 CAT_POI_CNT, sqlite3_column_int(_stmt_selall_cat, 4),
849                 -1);
850     }
851     sqlite3_reset(_stmt_selall_cat);
852
853     vprintf("%s(): return %p\n", __PRETTY_FUNCTION__, store);
854     return store;
855 }
856
857 static gboolean
858 category_add(GtkWidget *widget, PoiCategoryEditInfo *pcedit)
859 {
860     GtkListStore *store;
861     printf("%s()\n", __PRETTY_FUNCTION__);
862
863     if(category_edit_dialog(pcedit->dialog, 0))
864     {
865         store = generate_store();
866         gtk_tree_view_set_model(
867                 GTK_TREE_VIEW(pcedit->tree_view),
868                 GTK_TREE_MODEL(store));
869         g_object_unref(G_OBJECT(store));
870     }
871     vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
872     return TRUE;
873 }
874
875 static gboolean
876 category_edit(GtkWidget *widget, PoiCategoryEditInfo *pcedit)
877 {
878     GtkTreeIter iter;
879     GtkTreeModel *store;
880     GtkTreeSelection *selection;
881     printf("%s()\n", __PRETTY_FUNCTION__);
882
883     store = gtk_tree_view_get_model(GTK_TREE_VIEW(pcedit->tree_view));
884     selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(pcedit->tree_view));
885     if(gtk_tree_selection_get_selected(selection, &store, &iter))
886     {
887         GValue val;
888         memset(&val, 0, sizeof(val));
889         gtk_tree_model_get_value(store, &iter, 0, &val);
890         if(category_edit_dialog(pcedit->dialog, g_value_get_uint(&val)))
891         {
892             GtkListStore *new_store = generate_store();
893             gtk_tree_view_set_model(
894                     GTK_TREE_VIEW(pcedit->tree_view),
895                     GTK_TREE_MODEL(new_store));
896             g_object_unref(G_OBJECT(new_store));
897         }
898     }
899     vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
900     return TRUE;
901 }
902
903 gboolean
904 category_list_dialog(GtkWidget *parent)
905 {
906     static GtkWidget *dialog = NULL;
907     static GtkWidget *tree_view = NULL;
908     static GtkWidget *sw = NULL;
909     static GtkWidget *btn_edit = NULL;
910     static GtkWidget *btn_add = NULL;
911     static GtkTreeViewColumn *column = NULL;
912     static GtkCellRenderer *renderer = NULL;
913     static GtkListStore *store;
914     static PoiCategoryEditInfo pcedit;
915     printf("%s()\n", __PRETTY_FUNCTION__);
916
917     store = generate_store();
918
919     if(!store)
920         return TRUE;
921
922     dialog = gtk_dialog_new_with_buttons(_("POI Categories"),
923             GTK_WINDOW(parent), GTK_DIALOG_MODAL,
924             GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
925             NULL);
926
927     /* Enable the help button. */
928 #ifndef LEGACY
929     hildon_help_dialog_help_enable(
930 #else
931     ossohelp_dialog_help_enable(
932 #endif
933             GTK_DIALOG(dialog), HELP_ID_POICAT, _osso);
934
935     gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
936             btn_edit = gtk_button_new_with_label(_("Edit...")));
937
938     gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
939             btn_add = gtk_button_new_with_label(_("Add...")));
940
941     sw = gtk_scrolled_window_new(NULL, NULL);
942     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (sw),
943                   GTK_POLICY_NEVER,
944                   GTK_POLICY_AUTOMATIC);
945     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
946             sw, TRUE, TRUE, 0);
947
948     tree_view = gtk_tree_view_new();
949     /* Maemo-related? */
950     g_object_set(tree_view, "allow-checkbox-mode", FALSE, NULL);
951     gtk_container_add (GTK_CONTAINER (sw), tree_view);
952
953     gtk_tree_selection_set_mode(
954             gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view)),
955             GTK_SELECTION_SINGLE);
956     gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree_view), TRUE);
957
958     renderer = gtk_cell_renderer_text_new();
959     column = gtk_tree_view_column_new_with_attributes(
960             _("ID"), renderer, "text", CAT_ID, NULL);
961     gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), column);
962     gtk_tree_view_column_set_max_width (column, 1);
963
964     renderer = gtk_cell_renderer_toggle_new();
965     g_signal_connect (renderer, "toggled",
966             G_CALLBACK (category_toggled), store);
967     column = gtk_tree_view_column_new_with_attributes(
968             _("Enabled"), renderer, "active", CAT_ENABLED, NULL);
969     gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), column);
970
971     renderer = gtk_cell_renderer_text_new();
972     column = gtk_tree_view_column_new_with_attributes(
973             _("Label"), renderer, "text", CAT_LABEL, NULL);
974     gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), column);
975
976     renderer = gtk_cell_renderer_text_new();
977     column = gtk_tree_view_column_new_with_attributes(
978             _("Description"), renderer, "text", CAT_DESC, NULL);
979     gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), column);
980
981     renderer = gtk_cell_renderer_text_new();
982     column = gtk_tree_view_column_new_with_attributes(
983             _("# POIs"), renderer, "text", CAT_POI_CNT, NULL);
984     gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), column);
985
986     gtk_window_set_default_size(GTK_WINDOW(dialog), -1, 400);
987
988     pcedit.dialog = dialog;
989     pcedit.tree_view = tree_view;
990
991     g_signal_connect(G_OBJECT(btn_edit), "clicked",
992             G_CALLBACK(category_edit), &pcedit);
993
994     g_signal_connect(G_OBJECT(btn_add), "clicked",
995             G_CALLBACK(category_add), &pcedit);
996
997     gtk_tree_view_set_model(GTK_TREE_VIEW(tree_view), GTK_TREE_MODEL(store));
998     g_object_unref(G_OBJECT(store));
999
1000     gtk_widget_show_all(dialog);
1001
1002     while(GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog)))
1003     {
1004         break;
1005     }
1006
1007     gtk_widget_destroy(dialog);
1008
1009     vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
1010     return TRUE;
1011 }
1012
1013 static gboolean
1014 poi_delete(GtkWidget *widget, DeletePOI *dpoi)
1015 {
1016     GtkWidget *confirm;
1017     gint i;
1018     gchar *buffer;
1019     printf("%s()\n", __PRETTY_FUNCTION__);
1020
1021     buffer = g_strdup_printf("%s\n%s", _("Delete POI?"), dpoi->txt_label);
1022     confirm = hildon_note_new_confirmation(GTK_WINDOW(dpoi->dialog), buffer);
1023     g_free(buffer);
1024     i = gtk_dialog_run(GTK_DIALOG(confirm));
1025     gtk_widget_destroy(GTK_WIDGET(confirm));
1026
1027     if(i == GTK_RESPONSE_OK)
1028     {
1029         if(SQLITE_OK != sqlite3_bind_int(_stmt_delete_poi, 1, dpoi->id) ||
1030            SQLITE_DONE != sqlite3_step(_stmt_delete_poi))
1031         {
1032             MACRO_BANNER_SHOW_INFO(dpoi->dialog, _("Error deleting POI"));
1033         }
1034         else
1035         {
1036             dpoi->deleted = TRUE;
1037             gtk_widget_hide(dpoi->dialog);
1038             map_force_redraw();
1039         }
1040         sqlite3_reset(_stmt_delete_poi);
1041     }
1042
1043     vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
1044     return TRUE;
1045 }
1046
1047 static gboolean
1048 poi_populate_categories(GtkListStore *store, gint cat_id,
1049         GtkTreeIter *out_active)
1050 {
1051     gboolean has_active = FALSE;
1052     printf("%s()\n", __PRETTY_FUNCTION__);
1053
1054     gtk_list_store_clear(store);
1055
1056     while(SQLITE_ROW == sqlite3_step(_stmt_selall_cat))
1057     {
1058         GtkTreeIter iter;
1059         gint cid = sqlite3_column_int(_stmt_selall_cat, 0);
1060         const gchar *clab = sqlite3_column_text(_stmt_selall_cat, 1);
1061
1062         gtk_list_store_append(store, &iter);
1063         gtk_list_store_set(store, &iter, 0, cid, 1, clab, -1);
1064
1065         if(cid == cat_id || !has_active)
1066         {
1067             if(out_active)
1068                 *out_active = iter;
1069             has_active = TRUE;
1070         }
1071     }
1072     sqlite3_reset(_stmt_selall_cat);
1073
1074     vprintf("%s(): return\n", __PRETTY_FUNCTION__);
1075     return has_active;
1076 }
1077
1078 static gboolean
1079 poi_edit_cat(GtkWidget *widget, PoiCategoryEditInfo *data)
1080 {
1081     printf("%s()\n", __PRETTY_FUNCTION__);
1082     if(category_list_dialog(data->dialog))
1083     {
1084         GtkTreeIter active;
1085         if(poi_populate_categories(GTK_LIST_STORE(gtk_combo_box_get_model(
1086                         GTK_COMBO_BOX(data->cmb_category))),
1087                 data->cat_id, &active))
1088         {
1089             gtk_combo_box_set_active_iter(
1090                     GTK_COMBO_BOX(data->cmb_category), &active);
1091         }
1092     }
1093     vprintf("%s(): return\n", __PRETTY_FUNCTION__);
1094     return TRUE;
1095 }
1096
1097 static GtkWidget*
1098 poi_create_cat_combo()
1099 {
1100     GtkWidget *cmb_category;
1101     GtkTreeModel *model;
1102     printf("%s()\n", __PRETTY_FUNCTION__);
1103
1104     model = GTK_TREE_MODEL(gtk_list_store_new(2,
1105                 G_TYPE_INT,      /* Category ID */
1106                 G_TYPE_STRING)); /* Category Label */
1107     cmb_category = gtk_combo_box_new_with_model(model);
1108     g_object_unref(model);
1109
1110     /* Set up the view for the combo box. */
1111     {
1112         GtkCellRenderer *renderer;
1113         GtkTreeIter active;
1114         renderer = gtk_cell_renderer_text_new();
1115         gtk_cell_layout_pack_start(
1116                 GTK_CELL_LAYOUT(cmb_category), renderer, TRUE);
1117         gtk_cell_layout_set_attributes(
1118                 GTK_CELL_LAYOUT(cmb_category), renderer, "text", 1, NULL);
1119
1120         poi_populate_categories(GTK_LIST_STORE(gtk_combo_box_get_model(
1121                         GTK_COMBO_BOX(cmb_category))), -1, &active);
1122     }
1123     vprintf("%s(): return\n", __PRETTY_FUNCTION__);
1124     return cmb_category;
1125 }
1126
1127 gboolean
1128 poi_add_dialog(GtkWidget *parent, gint unitx, gint unity)
1129 {
1130     static PoiInfo poi;
1131     static GtkWidget *dialog;
1132     static GtkWidget *table;
1133     static GtkWidget *label;
1134     static GtkWidget *txt_label;
1135     static GtkWidget *txt_lat;
1136     static GtkWidget *txt_lon;
1137     static GtkWidget *cmb_category;
1138     static GtkWidget *txt_desc;
1139     static GtkWidget *btn_catedit;
1140     static GtkWidget *hbox;
1141     static GtkWidget *txt_scroll;
1142     static GtkTextBuffer *desc_txt;
1143     static GtkTextIter begin, end;
1144     static DeletePOI dpoi = {NULL, NULL, 0};
1145     static PoiCategoryEditInfo pcedit;
1146     printf("%s()\n", __PRETTY_FUNCTION__);
1147
1148     if(dialog == NULL)
1149     {
1150         dialog = gtk_dialog_new_with_buttons(_("Add POI"),
1151             GTK_WINDOW(parent), GTK_DIALOG_MODAL,
1152             GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1153             GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
1154             NULL);
1155
1156         /* Set the lat/lon strings. */
1157         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1158                 table = gtk_table_new(6, 4, FALSE), TRUE, TRUE, 0);
1159
1160         gtk_table_attach(GTK_TABLE(table),
1161                 label = gtk_label_new(_("Lat")),
1162                 0, 1, 0, 1, GTK_FILL, 0, 2, 0);
1163         gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
1164         gtk_table_attach(GTK_TABLE(table),
1165                 txt_lat = gtk_entry_new(),
1166                 1, 2, 0, 1, GTK_FILL, 0, 2, 0);
1167
1168         gtk_table_attach(GTK_TABLE(table),
1169                 label = gtk_label_new(_("Lon")),
1170                 2, 3, 0, 1, GTK_FILL, 0, 2, 0);
1171         gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
1172         gtk_table_attach(GTK_TABLE(table),
1173                 txt_lon = gtk_entry_new(),
1174                 3, 4, 0, 1, GTK_FILL, 0, 2, 0);
1175
1176         gtk_table_attach(GTK_TABLE(table),
1177                 label = gtk_label_new(_("Label")),
1178                 0, 1, 1, 2, GTK_FILL, 0, 2, 0);
1179         gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
1180         gtk_table_attach(GTK_TABLE(table),
1181                 txt_label = gtk_entry_new(),
1182                 1, 4, 1, 2, GTK_EXPAND | GTK_FILL, 0, 2, 0);
1183
1184         gtk_table_attach(GTK_TABLE(table),
1185                 label = gtk_label_new(_("Category")),
1186                 0, 1, 3, 4, GTK_FILL, 0, 2, 0);
1187         gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
1188         gtk_table_attach(GTK_TABLE(table),
1189                 hbox = gtk_hbox_new(FALSE, 4),
1190                 1, 4, 3, 4, GTK_EXPAND | GTK_FILL, 0, 2, 0);
1191         gtk_box_pack_start(GTK_BOX(hbox),
1192                 cmb_category = poi_create_cat_combo(),
1193                 FALSE, FALSE, 0);
1194
1195         gtk_box_pack_start(GTK_BOX(hbox),
1196                 btn_catedit = gtk_button_new_with_label(
1197                     _("Edit Categories...")),
1198                 FALSE, FALSE, 0);
1199
1200         gtk_table_attach(GTK_TABLE(table),
1201                 label = gtk_label_new(_("Description")),
1202                 0, 1, 5, 6, GTK_FILL, GTK_FILL, 2, 0);
1203         gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.0f);
1204
1205         txt_scroll = gtk_scrolled_window_new(NULL, NULL);
1206         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(txt_scroll),
1207                 GTK_SHADOW_IN);
1208         gtk_table_attach(GTK_TABLE(table),
1209                 txt_scroll,
1210                 1, 4, 5, 6, GTK_EXPAND | GTK_FILL, 0, 2, 0);
1211
1212         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(txt_scroll),
1213                 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1214
1215         txt_desc = gtk_text_view_new ();
1216         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(txt_desc), GTK_WRAP_WORD);
1217
1218         gtk_container_add(GTK_CONTAINER(txt_scroll), txt_desc);
1219         gtk_widget_set_size_request(GTK_WIDGET(txt_scroll), 550, 120);
1220
1221         desc_txt = gtk_text_view_get_buffer(GTK_TEXT_VIEW (txt_desc));
1222
1223         g_signal_connect(G_OBJECT(btn_catedit), "clicked",
1224                 G_CALLBACK(poi_edit_cat), &pcedit);
1225     }
1226
1227     poi.poi_id = -1;
1228     poi.cat_id = -1;
1229     poi.clabel = NULL;
1230     poi.desc = g_strdup("");
1231     unit2latlon(unitx, unity, poi.lat, poi.lon);
1232
1233     /* Lat/Lon */
1234     {
1235         gchar tmp1[LL_FMT_LEN], tmp2[LL_FMT_LEN];
1236
1237         format_lat_lon(poi.lat, poi.lon, tmp1, tmp2);
1238         //lat_format(poi.lat, tmp1);
1239         //lon_format(poi.lon, tmp2);
1240
1241         gtk_entry_set_text(GTK_ENTRY(txt_lat), tmp1);
1242         gtk_entry_set_text(GTK_ENTRY(txt_lon), tmp2);
1243     }
1244
1245     /* Label */
1246     if(SQLITE_ROW == sqlite3_step(_stmt_nextlabel_poi))
1247         poi.label = g_strdup_printf("Point%06d",
1248                 sqlite3_column_int(_stmt_nextlabel_poi, 0));
1249     else
1250         poi.label = g_strdup("");
1251     sqlite3_reset(_stmt_nextlabel_poi);
1252     gtk_entry_set_text(GTK_ENTRY(txt_label), poi.label);
1253
1254     /* POI Desc. */
1255     gtk_text_buffer_set_text(desc_txt, "", -1);
1256
1257     /* Category. */
1258     {
1259         GtkTreeIter iter;
1260         gint cat_id = -1;
1261         gboolean had_cat_id = FALSE;
1262
1263         if(gtk_combo_box_get_active_iter(
1264                 GTK_COMBO_BOX(cmb_category), &iter))
1265         {
1266             gtk_tree_model_get(
1267                     gtk_combo_box_get_model(GTK_COMBO_BOX(cmb_category)),&iter,
1268                     0, &cat_id,
1269                     -1);
1270             had_cat_id = TRUE;
1271         }
1272         gtk_list_store_clear(GTK_LIST_STORE(gtk_combo_box_get_model(
1273                         GTK_COMBO_BOX(cmb_category))));
1274         if(poi_populate_categories(GTK_LIST_STORE(gtk_combo_box_get_model(
1275                     GTK_COMBO_BOX(cmb_category))), cat_id, &iter)
1276                 && had_cat_id)
1277         {
1278             gtk_combo_box_set_active_iter(GTK_COMBO_BOX(cmb_category), &iter);
1279         }
1280     }
1281
1282     pcedit.dialog = dialog;
1283     pcedit.cmb_category = cmb_category;
1284     pcedit.cat_id = poi.cat_id;
1285
1286     gtk_widget_show_all(dialog);
1287
1288     while(GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog)))
1289     {
1290         GtkTreeIter iter;
1291         const gchar *text;
1292         gchar *error_check;
1293
1294         text = gtk_entry_get_text(GTK_ENTRY(txt_lat));
1295         poi.lat = strdmstod(text, &error_check);
1296         if(text == error_check || poi.lat < -90. || poi.lat > 90.) {
1297             popup_error(dialog, _("Invalid Latitude"));
1298             continue;
1299         }
1300
1301         text = gtk_entry_get_text(GTK_ENTRY(txt_lon));
1302         poi.lon = strdmstod(text, &error_check);
1303         if(text == error_check || poi.lon < -180. || poi.lon > 180.) {
1304             popup_error(dialog, _("Invalid Longitude"));
1305             continue;
1306         }
1307
1308         if(strlen(gtk_entry_get_text(GTK_ENTRY(txt_label))))
1309         {
1310             if(poi.label)
1311                 g_free(poi.label);
1312             poi.label = g_strdup(gtk_entry_get_text(GTK_ENTRY(txt_label)));
1313         }
1314         else
1315         {
1316             popup_error(dialog, _("Please specify a name."));
1317             continue;
1318         }
1319
1320         if(!gtk_combo_box_get_active_iter(
1321                 GTK_COMBO_BOX(cmb_category), &iter))
1322         {
1323             popup_error(dialog, _("Please specify a category."));
1324             continue;
1325         }
1326
1327         gtk_text_buffer_get_iter_at_offset(desc_txt, &begin,0 );
1328         gtk_text_buffer_get_end_iter (desc_txt, &end);
1329         if(poi.desc)
1330             g_free(poi.desc);
1331         poi.desc = gtk_text_buffer_get_text(desc_txt, &begin, &end, TRUE);
1332
1333         if(poi.clabel)
1334             g_free(poi.clabel);
1335         gtk_tree_model_get(
1336                 gtk_combo_box_get_model(GTK_COMBO_BOX(cmb_category)), &iter,
1337                 0, &poi.cat_id,
1338                 1, &poi.clabel,
1339                 -1);
1340
1341         /* add poi */
1342         if(SQLITE_OK != sqlite3_bind_double(_stmt_insert_poi, 1, poi.lat)
1343         || SQLITE_OK != sqlite3_bind_double(_stmt_insert_poi, 2, poi.lon)
1344         || SQLITE_OK != sqlite3_bind_text(_stmt_insert_poi, 3, poi.label,
1345                -1, SQLITE_STATIC)
1346         || SQLITE_OK != sqlite3_bind_text(_stmt_insert_poi, 4, poi.desc,
1347                -1, SQLITE_STATIC)
1348         || SQLITE_OK != sqlite3_bind_int(_stmt_insert_poi, 5, poi.cat_id)
1349         || SQLITE_DONE != sqlite3_step(_stmt_insert_poi))
1350         {
1351             MACRO_BANNER_SHOW_INFO(parent, _("Error adding POI"));
1352         }
1353
1354         sqlite3_reset(_stmt_insert_poi);
1355
1356         /* We're done. */
1357         break;
1358     }
1359
1360     g_free(poi.label);
1361     g_free(poi.desc);
1362     g_free(dpoi.txt_label);
1363
1364     map_force_redraw();
1365
1366     gtk_widget_hide(dialog);
1367
1368     vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
1369     return !dpoi.deleted;
1370 }
1371
1372 gboolean
1373 poi_view_dialog(GtkWidget *parent, PoiInfo *poi)
1374 {
1375     GtkTreeIter iter;
1376     static GtkWidget *dialog;
1377     static GtkWidget *table;
1378     static GtkWidget *label;
1379     static GtkWidget *txt_label;
1380     static GtkWidget *txt_lat;
1381     static GtkWidget *txt_lon;
1382     static GtkWidget *cmb_category;
1383     static GtkWidget *txt_desc;
1384     static GtkWidget *btn_delete = NULL;
1385     static GtkWidget *btn_catedit;
1386     static GtkWidget *hbox;
1387     static GtkWidget *txt_scroll;
1388     static GtkTextBuffer *desc_txt;
1389     static GtkTextIter begin, end;
1390     static DeletePOI dpoi = {NULL, NULL, 0};
1391     static PoiCategoryEditInfo pcedit;
1392     static int last_deg_format = 0;
1393     
1394     printf("%s()\n", __PRETTY_FUNCTION__);
1395     
1396
1397     if(_degformat != last_deg_format)
1398     {
1399         last_deg_format = _degformat;
1400         
1401                 if(dialog != NULL) gtk_widget_destroy(dialog);
1402         dialog = NULL;
1403     }
1404
1405     if(dialog == NULL)
1406     {
1407         dialog = gtk_dialog_new_with_buttons(_("Edit POI"),
1408             GTK_WINDOW(parent), GTK_DIALOG_MODAL,
1409             GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1410             NULL);
1411
1412         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
1413                 btn_delete = gtk_button_new_with_label(_("Delete...")));
1414
1415         gtk_dialog_add_button(GTK_DIALOG(dialog),
1416                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT);
1417
1418         /* Set the lat/lon strings. */
1419         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1420                 table = gtk_table_new(6, 4, FALSE), TRUE, TRUE, 0);
1421
1422         gtk_table_attach(GTK_TABLE(table),
1423                 label = gtk_label_new(DEG_FORMAT_ENUM_TEXT[_degformat].short_field_1),
1424                 0, 1, 0, 1, GTK_FILL, 0, 2, 0);
1425         gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
1426         gtk_table_attach(GTK_TABLE(table),
1427                 txt_lat = gtk_entry_new(),
1428                 1, 2, 0, 1, GTK_FILL, 0, 2, 0);
1429
1430         if(DEG_FORMAT_ENUM_TEXT[_degformat].field_2_in_use)
1431         {
1432                 gtk_table_attach(GTK_TABLE(table),
1433                         label = gtk_label_new(DEG_FORMAT_ENUM_TEXT[_degformat].short_field_2),
1434                         2, 3, 0, 1, GTK_FILL, 0, 2, 0);
1435                 gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
1436                 gtk_table_attach(GTK_TABLE(table),
1437                         txt_lon = gtk_entry_new(),
1438                         3, 4, 0, 1, GTK_FILL, 0, 2, 0);
1439         }
1440         
1441         gtk_table_attach(GTK_TABLE(table),
1442                 label = gtk_label_new(_("Label")),
1443                 0, 1, 1, 2, GTK_FILL, 0, 2, 0);
1444         gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
1445         gtk_table_attach(GTK_TABLE(table),
1446                 txt_label = gtk_entry_new(),
1447                 1, 4, 1, 2, GTK_EXPAND | GTK_FILL, 0, 2, 0);
1448
1449         gtk_table_attach(GTK_TABLE(table),
1450                 label = gtk_label_new(_("Category")),
1451                 0, 1, 3, 4, GTK_FILL, 0, 2, 0);
1452         gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
1453         gtk_table_attach(GTK_TABLE(table),
1454                 hbox = gtk_hbox_new(FALSE, 4),
1455                 1, 4, 3, 4, GTK_EXPAND | GTK_FILL, 0, 2, 0);
1456         gtk_box_pack_start(GTK_BOX(hbox),
1457                 cmb_category = poi_create_cat_combo(),
1458                 FALSE, FALSE, 0);
1459
1460         gtk_box_pack_start(GTK_BOX(hbox),
1461                 btn_catedit = gtk_button_new_with_label(
1462                     _("Edit Categories...")),
1463                 FALSE, FALSE, 0);
1464
1465         gtk_table_attach(GTK_TABLE(table),
1466                 label = gtk_label_new(_("Description")),
1467                 0, 1, 5, 6, GTK_FILL, GTK_FILL, 2, 0);
1468         gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.0f);
1469
1470         txt_scroll = gtk_scrolled_window_new(NULL, NULL);
1471         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(txt_scroll),
1472                 GTK_SHADOW_IN);
1473         gtk_table_attach(GTK_TABLE(table),
1474                 txt_scroll,
1475                 1, 4, 5, 6, GTK_EXPAND | GTK_FILL, 0, 2, 0);
1476
1477         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(txt_scroll),
1478                 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1479
1480         txt_desc = gtk_text_view_new ();
1481         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(txt_desc), GTK_WRAP_WORD);
1482
1483         gtk_container_add(GTK_CONTAINER(txt_scroll), txt_desc);
1484         gtk_widget_set_size_request(GTK_WIDGET(txt_scroll), 550, 120);
1485
1486         desc_txt = gtk_text_view_get_buffer (GTK_TEXT_VIEW (txt_desc));
1487
1488         g_signal_connect(G_OBJECT(btn_delete), "clicked",
1489                           G_CALLBACK(poi_delete), &dpoi);
1490
1491         g_signal_connect(G_OBJECT(btn_catedit), "clicked",
1492                 G_CALLBACK(poi_edit_cat), &pcedit);
1493     }
1494
1495     dpoi.dialog = dialog;
1496     dpoi.txt_label = g_strdup(poi->label);
1497     dpoi.id = poi->poi_id;
1498     dpoi.deleted = FALSE;
1499
1500     /* Lat/Lon */
1501     {
1502         gchar tmp1[LL_FMT_LEN], tmp2[LL_FMT_LEN];
1503
1504         format_lat_lon(poi->lat, poi->lon, tmp1, tmp2);
1505         //lat_format(poi->lat, tmp1);
1506         //lon_format(poi->lon, tmp2);
1507
1508         gtk_entry_set_text(GTK_ENTRY(txt_lat), tmp1);
1509         
1510         if(DEG_FORMAT_ENUM_TEXT[_degformat].field_2_in_use)
1511                 gtk_entry_set_text(GTK_ENTRY(txt_lon), tmp2);
1512         else
1513                 gtk_entry_set_text(GTK_ENTRY(txt_lon), g_strdup(""));
1514     }
1515
1516     /* label */
1517     gtk_entry_set_text(GTK_ENTRY(txt_label), poi->label);
1518
1519     /* poi_desc */
1520     gtk_text_buffer_set_text(desc_txt, poi->desc, -1);
1521
1522     /* Category. */
1523     gtk_list_store_clear(GTK_LIST_STORE(gtk_combo_box_get_model(
1524                     GTK_COMBO_BOX(cmb_category))));
1525     if(poi_populate_categories(GTK_LIST_STORE(gtk_combo_box_get_model(
1526                 GTK_COMBO_BOX(cmb_category))), poi->cat_id, &iter))
1527         gtk_combo_box_set_active_iter(GTK_COMBO_BOX(cmb_category), &iter);
1528
1529     /* Connect Signals */
1530     pcedit.dialog = dialog;
1531     pcedit.cmb_category = cmb_category;
1532     pcedit.cat_id = poi->cat_id;
1533
1534     gtk_widget_show_all(dialog);
1535
1536     while(GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog)))
1537     {
1538         const gchar *text, *text_lat, *text_lon;
1539         gchar *error_check;
1540
1541         text_lat = gtk_entry_get_text(GTK_ENTRY(txt_lat));
1542         text_lon = gtk_entry_get_text(GTK_ENTRY(txt_lon));
1543         
1544         if(!parse_coords(text_lat, text_lon, &poi->lat, &poi->lon))
1545         {
1546                 popup_error(dialog, _("Invalid coordinate specified"));
1547                 continue;
1548         }
1549
1550         
1551         if(strlen(gtk_entry_get_text(GTK_ENTRY(txt_label))))
1552         {
1553             if(poi->label)
1554                 g_free(poi->label);
1555             poi->label = g_strdup(gtk_entry_get_text(GTK_ENTRY(txt_label)));
1556         }
1557         else
1558         {
1559             popup_error(dialog, _("Please specify a name."));
1560             continue;
1561         }
1562
1563         if(!gtk_combo_box_get_active_iter(
1564                 GTK_COMBO_BOX(cmb_category), &iter))
1565         {
1566             popup_error(dialog, _("Please specify a category."));
1567             continue;
1568         }
1569
1570         gtk_text_buffer_get_iter_at_offset(desc_txt, &begin,0 );
1571         gtk_text_buffer_get_end_iter (desc_txt, &end);
1572         if(poi->desc)
1573             g_free(poi->desc);
1574         poi->desc = gtk_text_buffer_get_text(desc_txt, &begin, &end, TRUE);
1575
1576         if(poi->clabel)
1577             g_free(poi->clabel);
1578         gtk_tree_model_get(
1579                 gtk_combo_box_get_model(GTK_COMBO_BOX(cmb_category)), &iter,
1580                 0, &poi->cat_id,
1581                 1, &poi->clabel,
1582                 -1);
1583
1584         /* edit poi */
1585         if(SQLITE_OK != sqlite3_bind_double(
1586                     _stmt_update_poi, 1, poi->lat) ||
1587            SQLITE_OK != sqlite3_bind_double(
1588                _stmt_update_poi, 2, poi->lon) ||
1589            SQLITE_OK != sqlite3_bind_text(_stmt_update_poi, 3, poi->label,
1590                     -1, SQLITE_STATIC) ||
1591            SQLITE_OK != sqlite3_bind_text(_stmt_update_poi, 4, poi->desc,
1592                -1, SQLITE_STATIC) ||
1593            SQLITE_OK != sqlite3_bind_int(
1594                _stmt_update_poi, 5, poi->cat_id) ||
1595            SQLITE_OK != sqlite3_bind_int(
1596                _stmt_update_poi, 6, poi->poi_id) ||
1597            SQLITE_DONE != sqlite3_step(_stmt_update_poi))
1598         {
1599             MACRO_BANNER_SHOW_INFO(parent, _("Error updating POI"));
1600         }
1601
1602         sqlite3_reset(_stmt_update_poi);
1603
1604         /* We're done. */
1605         break;
1606     }
1607
1608     g_free(dpoi.txt_label);
1609
1610     map_force_redraw();
1611
1612     gtk_widget_hide(dialog); /* Destroying causes a crash.... ??? */
1613
1614     vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
1615     return !dpoi.deleted;
1616 }
1617
1618 static gint
1619 poi_list_insert(GtkWidget *parent, GList *poi_list, GtkComboBox *cmb_category)
1620 {
1621     gint default_cat_id;
1622     gchar *default_cat_label;
1623     gint num_inserts = 0;
1624     GList *curr;
1625     GtkTreeIter iter;
1626     printf("%s()\n", __PRETTY_FUNCTION__);
1627
1628     /* Get defaults from the given GtkComboBox */
1629     if(!gtk_combo_box_get_active_iter(
1630             GTK_COMBO_BOX(cmb_category), &iter))
1631     {
1632         vprintf("%s(): return 0\n", __PRETTY_FUNCTION__);
1633         return 0;
1634     }
1635     gtk_tree_model_get(
1636             gtk_combo_box_get_model(GTK_COMBO_BOX(cmb_category)),
1637             &iter,
1638             0, &default_cat_id,
1639             1, &default_cat_label,
1640             -1);
1641
1642     /* Iterate through the data model and import as desired. */
1643     for(curr = poi_list; curr; )
1644     {
1645         PoiInfo *poi = curr->data;
1646         if(
1647         (    SQLITE_OK != sqlite3_bind_double(_stmt_insert_poi, 1, poi->lat)
1648           || SQLITE_OK != sqlite3_bind_double(_stmt_insert_poi, 2, poi->lon)
1649           || SQLITE_OK != sqlite3_bind_text(_stmt_insert_poi, 3, poi->label,
1650              -1, SQLITE_STATIC)
1651           || SQLITE_OK != sqlite3_bind_text(_stmt_insert_poi, 4, poi->desc,
1652              -1, SQLITE_STATIC)
1653           || SQLITE_OK != sqlite3_bind_int(_stmt_insert_poi, 5,
1654               poi->cat_id = default_cat_id)
1655           || SQLITE_DONE != sqlite3_step(_stmt_insert_poi)
1656         ))
1657         {
1658             /* Failure. */
1659             GList *tmp = curr->next;
1660             if(poi->label)
1661                 g_free(poi->label);
1662             if(poi->desc)
1663                 g_free(poi->desc);
1664             g_slice_free(PoiInfo, poi);
1665             poi_list = g_list_delete_link(poi_list, curr);
1666             curr = tmp;
1667         }
1668         else
1669         {
1670             /* Success. */
1671             ++num_inserts;
1672             if(default_cat_label)
1673                 poi->clabel = g_strdup(default_cat_label);
1674             poi->poi_id = sqlite3_last_insert_rowid(_poi_db);
1675             curr = curr->next;
1676         }
1677         sqlite3_reset(_stmt_insert_poi);
1678     }
1679
1680     if(num_inserts)
1681     {
1682         gchar buffer[BUFFER_SIZE];
1683         map_force_redraw();
1684         snprintf(buffer, sizeof(buffer), "%d %s", num_inserts,
1685            _("POIs were added to the POI database.  The following screen will "
1686                "allow you to modify or delete any of the new POIs."));
1687         popup_error(parent, buffer);
1688     }
1689     else
1690     {
1691         popup_error(parent, _("No POIs were found."));
1692     }
1693
1694     if(default_cat_label)
1695         g_free(default_cat_label);
1696
1697     vprintf("%s(): return %d\n", __PRETTY_FUNCTION__, num_inserts);
1698     return num_inserts;
1699 }
1700
1701 static void
1702 poi_list_free(GList *poi_list)
1703 {
1704     GList *curr;
1705     printf("%s()\n", __PRETTY_FUNCTION__);
1706
1707     for(curr = poi_list; curr; curr = curr->next)
1708     {
1709         PoiInfo *poi_info = curr->data;
1710         if(poi_info)
1711         {
1712             if(poi_info->label)
1713                 g_free(poi_info->label);
1714             if(poi_info->desc)
1715                 g_free(poi_info->desc);
1716             if(poi_info->clabel)
1717                 g_free(poi_info->clabel);
1718             g_slice_free(PoiInfo, poi_info);
1719         }
1720     }
1721
1722     g_list_free(poi_list);
1723
1724     vprintf("%s(): return\n", __PRETTY_FUNCTION__);
1725 }
1726
1727 static void
1728 poi_list_bearing_cell_data_func(
1729         GtkTreeViewColumn *tree_column,
1730         GtkCellRenderer *cell,
1731         GtkTreeModel *tree_model,
1732         GtkTreeIter *iter)
1733 {
1734     gchar buffer[80];
1735     gfloat f;
1736     vprintf("%s()\n", __PRETTY_FUNCTION__);
1737
1738     gtk_tree_model_get(tree_model, iter, POI_BEARING, &f, -1);
1739     snprintf(buffer, sizeof(buffer), "%.1f", f);
1740     g_object_set(cell, "text", buffer, NULL);
1741
1742     vprintf("%s(): return\n", __PRETTY_FUNCTION__);
1743 }
1744
1745 static void
1746 poi_list_distance_cell_data_func(
1747         GtkTreeViewColumn *tree_column,
1748         GtkCellRenderer *cell,
1749         GtkTreeModel *tree_model,
1750         GtkTreeIter *iter)
1751 {
1752     gchar buffer[80];
1753     gfloat f;
1754     vprintf("%s()\n", __PRETTY_FUNCTION__);
1755
1756     gtk_tree_model_get(tree_model, iter, POI_DISTANCE, &f, -1);
1757     snprintf(buffer, sizeof(buffer), "%.2f", f);
1758     g_object_set(cell, "text", buffer, NULL);
1759
1760     vprintf("%s(): return\n", __PRETTY_FUNCTION__);
1761 }
1762
1763 static gboolean
1764 poi_list_row_selected(GtkCellRendererToggle *renderer,
1765         gchar *path_string, GtkTreeModel *tree_model)
1766 {
1767     GtkTreeIter iter;
1768     vprintf("%s()\n", __PRETTY_FUNCTION__);
1769
1770     if(gtk_tree_model_get_iter_from_string(tree_model, &iter, path_string))
1771     {
1772         gboolean old_value;
1773         gtk_tree_model_get(tree_model, &iter, POI_SELECTED, &old_value, -1);
1774         gtk_list_store_set(GTK_LIST_STORE(tree_model), &iter,
1775                 POI_SELECTED, !old_value,
1776                 -1);
1777     }
1778
1779     vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
1780     return TRUE;
1781 }
1782
1783 static gboolean
1784 poi_list_set_category(GtkWidget *widget, PoiListInfo *pli)
1785 {
1786     static GtkWidget *dialog = NULL;
1787     static GtkWidget *cmb_category = NULL;
1788     static GtkWidget *btn_catedit = NULL;
1789     static PoiCategoryEditInfo pcedit;
1790     printf("%s()\n", __PRETTY_FUNCTION__);
1791
1792     if(dialog == NULL)
1793     {
1794         GtkWidget *hbox;
1795         GtkWidget *label;
1796
1797         dialog = gtk_dialog_new_with_buttons(_("Set Category..."),
1798                 GTK_WINDOW(pli->dialog2), GTK_DIALOG_MODAL,
1799                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1800                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
1801                 NULL);
1802
1803         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1804                 hbox = gtk_hbox_new(FALSE, 4), FALSE, FALSE, 4);
1805
1806         gtk_box_pack_start(GTK_BOX(hbox),
1807                 label = gtk_label_new(_("Category")),
1808                 FALSE, FALSE, 0);
1809
1810         gtk_box_pack_start(GTK_BOX(hbox),
1811                 cmb_category = poi_create_cat_combo(),
1812                 FALSE, FALSE, 4);
1813
1814         gtk_box_pack_start(GTK_BOX(hbox),
1815                 btn_catedit = gtk_button_new_with_label(
1816                     _("Edit Categories...")),
1817                 FALSE, FALSE, 0);
1818
1819         /* Connect Signals */
1820         pcedit.dialog = dialog;
1821         pcedit.cmb_category = cmb_category;
1822         pcedit.cat_id = -1;
1823         g_signal_connect(G_OBJECT(btn_catedit), "clicked",
1824                 G_CALLBACK(poi_edit_cat), &pcedit);
1825     }
1826
1827     gtk_widget_show_all(dialog);
1828
1829     while(GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog)))
1830     {
1831         GtkTreeIter iter;
1832         GtkListStore *store;
1833         gint cat_id;
1834         const gchar *cat_label;
1835
1836         /* Get the text of the chosen category. */
1837         if(!gtk_combo_box_get_active_iter(
1838                 GTK_COMBO_BOX(cmb_category), &iter))
1839         {
1840             popup_error(dialog, _("Please specify a category."));
1841             continue;
1842         }
1843
1844         gtk_tree_model_get(
1845                 gtk_combo_box_get_model(GTK_COMBO_BOX(cmb_category)),
1846                 &iter,
1847                 0, &cat_id,
1848                 1, &cat_label,
1849                 -1);
1850
1851         /* Iterate through the data store and categorize as desired. */
1852         store = GTK_LIST_STORE(gtk_tree_view_get_model(
1853                     GTK_TREE_VIEW(pli->tree_view)));
1854         if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter)) do
1855         {
1856             PoiInfo poi;
1857             gboolean selected;
1858
1859             memset(&poi, 0, sizeof(poi));
1860
1861             gtk_tree_model_get(GTK_TREE_MODEL(store), &iter,
1862                     POI_SELECTED, &selected,
1863                     POI_POIID, &(poi.poi_id),
1864                     POI_LAT, &(poi.lat),
1865                     POI_LON, &(poi.lon),
1866                     POI_LABEL, &(poi.label),
1867                     POI_DESC, &(poi.desc),
1868                     -1);
1869
1870             if(selected)
1871             {
1872                 gtk_list_store_set(store, &iter,
1873                     POI_CATID, cat_id,
1874                     POI_CLABEL, cat_label,
1875                     -1);
1876                 /* edit poi */
1877                 if(SQLITE_OK != sqlite3_bind_double(
1878                             _stmt_update_poi, 1, poi.lat) ||
1879                    SQLITE_OK != sqlite3_bind_double(
1880                        _stmt_update_poi, 2, poi.lon) ||
1881                    SQLITE_OK != sqlite3_bind_text(_stmt_update_poi,
1882                        3, poi.label, -1, SQLITE_STATIC) ||
1883                    SQLITE_OK != sqlite3_bind_text(_stmt_update_poi,
1884                        4, poi.desc, -1, SQLITE_STATIC) ||
1885                    SQLITE_OK != sqlite3_bind_int(
1886                        _stmt_update_poi, 5, cat_id) ||
1887                    SQLITE_OK != sqlite3_bind_int(
1888                        _stmt_update_poi, 6, poi.poi_id) ||
1889                    SQLITE_DONE != sqlite3_step(_stmt_update_poi))
1890                 {
1891                     MACRO_BANNER_SHOW_INFO(pli->dialog2,
1892                             _("Error updating POI"));
1893                 }
1894                 sqlite3_reset(_stmt_update_poi);
1895             }
1896         } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter));
1897
1898         break;
1899     }
1900
1901     map_force_redraw();
1902     gtk_widget_hide(dialog);
1903
1904     vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
1905     return TRUE;
1906 }
1907
1908 static gboolean
1909 poi_list_select_all(GtkTreeViewColumn *column, PoiListInfo *pli)
1910 {
1911     GtkTreeIter iter;
1912     GtkListStore *store;
1913     printf("%s()\n", __PRETTY_FUNCTION__);
1914
1915     /* Iterate through the data store and select as desired. */
1916     store = GTK_LIST_STORE(gtk_tree_view_get_model(
1917                 GTK_TREE_VIEW(pli->tree_view)));
1918     if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter)) do
1919     {
1920         gtk_list_store_set(store, &iter,
1921             POI_SELECTED, pli->select_all,
1922             -1);
1923     } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter));
1924
1925     pli->select_all = !pli->select_all;
1926
1927     vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
1928     return TRUE;
1929 }
1930
1931 static gboolean
1932 poi_list_view(GtkWidget *widget, PoiListInfo *pli)
1933 {
1934     GtkTreeIter iter;
1935     GtkTreeSelection *selection;
1936     GtkListStore *store;
1937     printf("%s()\n", __PRETTY_FUNCTION__);
1938
1939     selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(pli->tree_view));
1940     store = GTK_LIST_STORE(gtk_tree_view_get_model(
1941                 GTK_TREE_VIEW(pli->tree_view)));
1942
1943     /* Iterate through the data store and import as desired. */
1944     if(gtk_tree_selection_get_selected(selection, NULL, &iter))
1945     {
1946         PoiInfo poi;
1947         memset(&poi, 0, sizeof(poi));
1948
1949         gtk_tree_model_get(GTK_TREE_MODEL(store), &iter,
1950                 POI_POIID, &(poi.poi_id),
1951                 POI_CATID, &(poi.cat_id),
1952                 POI_LAT, &(poi.lat),
1953                 POI_LON, &(poi.lon),
1954                 POI_LABEL, &(poi.label),
1955                 POI_DESC, &(poi.desc),
1956                 POI_CLABEL, &(poi.clabel),
1957                 -1);
1958
1959         if(poi_view_dialog(pli->dialog, &poi))
1960         {
1961             gtk_list_store_set(store, &iter,
1962                     POI_POIID, poi.poi_id,
1963                     POI_CATID, poi.cat_id,
1964                     POI_LAT, poi.lat,
1965                     POI_LON, poi.lon,
1966                     POI_LABEL, poi.label,
1967                     POI_DESC, poi.desc,
1968                     POI_CLABEL, poi.clabel,
1969                     -1);
1970         }
1971         else
1972         {
1973             /* POI was deleted. */
1974             gtk_list_store_remove(store, &iter);
1975         }
1976     }
1977
1978     vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
1979     return TRUE;
1980 }
1981
1982 static void
1983 poi_list_row_activated(GtkTreeView *tree_view, GtkTreePath *path,
1984         GtkTreeViewColumn *column, PoiListInfo *pli)
1985 {
1986     printf("%s()\n", __PRETTY_FUNCTION__);
1987
1988     if(column != pli->select_column)
1989         poi_list_view(GTK_WIDGET(tree_view), pli);
1990
1991     vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
1992 }
1993
1994 static gboolean
1995 poi_list_goto(GtkWidget *widget, PoiListInfo *pli)
1996 {
1997     GtkTreeIter iter;
1998     GtkTreeSelection *selection;
1999     GtkListStore *store;
2000     printf("%s()\n", __PRETTY_FUNCTION__);
2001
2002     selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(pli->tree_view));
2003     store = GTK_LIST_STORE(gtk_tree_view_get_model(
2004                 GTK_TREE_VIEW(pli->tree_view)));
2005
2006     /* Iterate through the data store and import as desired. */
2007     if(gtk_tree_selection_get_selected(selection, NULL, &iter))
2008     {
2009         gdouble lat, lon;
2010         Point unit;
2011
2012         gtk_tree_model_get(GTK_TREE_MODEL(store), &iter,
2013                 POI_LAT, &lat,
2014                 POI_LON, &lon,
2015                 -1);
2016
2017         latlon2unit(lat, lon, unit.unitx, unit.unity);
2018
2019         if(_center_mode > 0)
2020             gtk_check_menu_item_set_active(
2021                     GTK_CHECK_MENU_ITEM(_menu_view_ac_none_item), TRUE);
2022
2023         map_center_unit(unit);
2024     }
2025
2026     vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
2027     return TRUE;
2028 }
2029
2030 static gboolean
2031 poi_list_delete(GtkWidget *widget, PoiListInfo *pli)
2032 {
2033     GtkWidget *confirm;
2034     printf("%s()\n", __PRETTY_FUNCTION__);
2035
2036     confirm = hildon_note_new_confirmation(
2037             GTK_WINDOW(pli->dialog2), _("Delete selected POI?"));
2038
2039     if(GTK_RESPONSE_OK == gtk_dialog_run(GTK_DIALOG(confirm)))
2040     {
2041         GtkTreeIter iter;
2042         GtkListStore *store;
2043         gboolean already_next;
2044         gboolean must_iterate;;
2045
2046         /* Iterate through the data store and import as desired. */
2047         store = GTK_LIST_STORE(gtk_tree_view_get_model(
2048                     GTK_TREE_VIEW(pli->tree_view)));
2049         if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter)) do
2050         {
2051             gboolean selected;
2052             must_iterate = TRUE;
2053             already_next = FALSE;
2054             gint poi_id;
2055             gtk_tree_model_get(GTK_TREE_MODEL(store), &iter,
2056                 POI_SELECTED, &selected,
2057                 POI_POIID, &poi_id,
2058                 -1);
2059             if(selected)
2060             {
2061                 /* Delete POI. */
2062                 if(SQLITE_OK != sqlite3_bind_int(_stmt_delete_poi, 1, poi_id)
2063                 || SQLITE_DONE != sqlite3_step(_stmt_delete_poi))
2064                 {
2065                     MACRO_BANNER_SHOW_INFO(pli->dialog2,
2066                             _("Error deleting POI"));
2067                 }
2068                 else
2069                 {
2070                     already_next = gtk_list_store_remove(store, &iter);
2071                     must_iterate = FALSE;
2072                 }
2073                 sqlite3_reset(_stmt_delete_poi);
2074             }
2075         } while(already_next || (must_iterate
2076                 && gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter)));
2077     }
2078
2079     map_force_redraw();
2080
2081     gtk_widget_destroy(confirm);
2082
2083     vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
2084     return TRUE;
2085 }
2086
2087 static gboolean
2088 poi_list_export_gpx(GtkWidget *widget, PoiListInfo *pli)
2089 {
2090     GnomeVFSHandle *handle;
2091     printf("%s()\n", __PRETTY_FUNCTION__);
2092
2093     if(display_open_file(GTK_WINDOW(pli->dialog2), NULL, &handle, NULL,
2094                 NULL, NULL, GTK_FILE_CHOOSER_ACTION_SAVE))
2095     {
2096         gint num_exported = gpx_poi_write(
2097                gtk_tree_view_get_model(GTK_TREE_VIEW(pli->tree_view)), handle);
2098         if(num_exported >= 0)
2099         {
2100             gchar buffer[80];
2101             snprintf(buffer, sizeof(buffer), "%d %s\n", num_exported,
2102                     _("POIs Exported"));
2103             MACRO_BANNER_SHOW_INFO(pli->dialog2, buffer);
2104         }
2105         else
2106             popup_error(pli->dialog2, _("Error writing GPX file."));
2107         gnome_vfs_close(handle);
2108     }
2109
2110     vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
2111     return TRUE;
2112 }
2113
2114 static gboolean
2115 poi_list_manage_checks(GtkWidget *widget, PoiListInfo *pli)
2116 {
2117     GtkWidget *btn_category;
2118     GtkWidget *btn_delete;
2119     GtkWidget *btn_export_gpx;
2120
2121     printf("%s()\n", __PRETTY_FUNCTION__);
2122
2123     pli->dialog2 = gtk_dialog_new_with_buttons(_("Checked POI Actions..."),
2124             GTK_WINDOW(pli->dialog), GTK_DIALOG_MODAL,
2125             NULL);
2126
2127     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(pli->dialog2)->vbox),
2128             gtk_label_new(_("Select an operation to perform\n"
2129                             "on the POIs that you checked\n"
2130                             "in the POI list.")),
2131                 FALSE, FALSE, 4);
2132
2133     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(pli->dialog2)->vbox),
2134             btn_category = gtk_button_new_with_label(_("Set Category...")),
2135                 FALSE, FALSE, 4);
2136
2137     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(pli->dialog2)->vbox),
2138             btn_delete = gtk_button_new_with_label(_("Delete...")),
2139                 FALSE, FALSE, 4);
2140
2141     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(pli->dialog2)->vbox),
2142             btn_export_gpx = gtk_button_new_with_label(
2143                 _("Export to GPX...")),
2144                 FALSE, FALSE, 4);
2145
2146     gtk_dialog_add_button(GTK_DIALOG(pli->dialog2),
2147             GTK_STOCK_CLOSE, GTK_RESPONSE_ACCEPT);
2148
2149     g_signal_connect(G_OBJECT(btn_category), "clicked",
2150             G_CALLBACK(poi_list_set_category), pli);
2151
2152     g_signal_connect(G_OBJECT(btn_delete), "clicked",
2153             G_CALLBACK(poi_list_delete), pli);
2154
2155     g_signal_connect(G_OBJECT(btn_export_gpx), "clicked",
2156             G_CALLBACK(poi_list_export_gpx), pli);
2157
2158     gtk_widget_show_all(pli->dialog2);
2159
2160     gtk_dialog_run(GTK_DIALOG(pli->dialog2));
2161
2162     gtk_widget_destroy(pli->dialog2);
2163     pli->dialog2 = NULL;
2164
2165     vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
2166     return TRUE;
2167 }
2168
2169 static gboolean
2170 poi_list_dialog(GtkWidget *parent, gint unitx, gint unity, GList *poi_list)
2171 {
2172     static PoiListInfo pli = { NULL, NULL };
2173     static GtkWidget *scroller;
2174     static GtkWidget *btn_goto;
2175     static GtkWidget *btn_edit;
2176     static GtkWidget *btn_manage_checks;
2177     static GtkListStore *store;
2178     GtkTreeIter iter;
2179     GList *curr;
2180     gdouble src_lat, src_lon;
2181     printf("%s()\n", __PRETTY_FUNCTION__);
2182
2183     if(pli.dialog == NULL)
2184     {
2185         GtkCellRenderer *renderer;
2186         GtkTreeViewColumn *column;
2187
2188         pli.dialog = gtk_dialog_new_with_buttons(_("POI List"),
2189             GTK_WINDOW(parent), GTK_DIALOG_MODAL,
2190                 NULL);
2191
2192         store = gtk_list_store_new(POI_NUM_COLUMNS,
2193                                    G_TYPE_BOOLEAN,/* Selected */
2194                                    G_TYPE_INT,    /* POI ID */
2195                                    G_TYPE_INT,    /* Category ID */
2196                                    G_TYPE_DOUBLE, /* Latitude */
2197                                    G_TYPE_DOUBLE, /* Longitude */
2198                                    G_TYPE_STRING, /* Lat/Lon */
2199                                    G_TYPE_FLOAT,  /* Bearing */
2200                                    G_TYPE_FLOAT,  /* Distance */
2201                                    G_TYPE_STRING, /* POI Label */
2202                                    G_TYPE_STRING, /* POI Desc. */
2203                                    G_TYPE_STRING);/* Category Label */
2204
2205         /* Set up the tree view. */
2206         pli.tree_view = gtk_tree_view_new();
2207         g_object_set(G_OBJECT(pli.tree_view),
2208                 "allow-checkbox-mode", FALSE, NULL);
2209
2210         gtk_tree_selection_set_mode(
2211                 gtk_tree_view_get_selection(GTK_TREE_VIEW(pli.tree_view)),
2212                 GTK_SELECTION_SINGLE);
2213         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(pli.tree_view), TRUE);
2214
2215         renderer = gtk_cell_renderer_toggle_new();
2216         gtk_cell_renderer_toggle_set_active(GTK_CELL_RENDERER_TOGGLE(renderer),
2217                 TRUE);
2218         g_signal_connect(G_OBJECT(renderer), "toggled",
2219                 G_CALLBACK(poi_list_row_selected), store);
2220         pli.select_column = gtk_tree_view_column_new_with_attributes(
2221                 "*", renderer, "active", POI_SELECTED, NULL);
2222         gtk_tree_view_append_column(GTK_TREE_VIEW(pli.tree_view),
2223                 pli.select_column);
2224         gtk_tree_view_column_set_clickable(pli.select_column, TRUE);
2225         g_signal_connect(G_OBJECT(pli.select_column), "clicked",
2226                 G_CALLBACK(poi_list_select_all), &pli);
2227
2228         renderer = gtk_cell_renderer_combo_new();
2229         column = gtk_tree_view_column_new_with_attributes(
2230                 _("Category"), renderer, "text", POI_CLABEL, NULL);
2231         gtk_tree_view_column_set_sizing(column,GTK_TREE_VIEW_COLUMN_GROW_ONLY);
2232         gtk_tree_view_column_set_sort_column_id(column, POI_CLABEL);
2233         gtk_tree_view_append_column(GTK_TREE_VIEW(pli.tree_view), column);
2234
2235         renderer = gtk_cell_renderer_text_new();
2236         g_object_set(renderer, "xalign", 1.f, NULL);
2237         column = gtk_tree_view_column_new_with_attributes(
2238                 _("Dist."), renderer, "text", POI_DISTANCE, NULL);
2239         gtk_tree_view_column_set_cell_data_func(column, renderer,
2240                 (GtkTreeCellDataFunc)poi_list_distance_cell_data_func,
2241                 NULL, NULL);
2242         gtk_tree_view_column_set_sort_column_id(column, POI_DISTANCE);
2243         gtk_tree_view_append_column(GTK_TREE_VIEW(pli.tree_view), column);
2244
2245         renderer = gtk_cell_renderer_text_new();
2246         g_object_set(renderer, "xalign", 1.f, NULL);
2247         column = gtk_tree_view_column_new_with_attributes(
2248                 _("Bear."), renderer, "text", POI_BEARING, NULL);
2249         gtk_tree_view_column_set_cell_data_func(column, renderer,
2250                 (GtkTreeCellDataFunc)poi_list_bearing_cell_data_func,
2251                 NULL, NULL);
2252         gtk_tree_view_column_set_sort_column_id(column, POI_BEARING);
2253         gtk_tree_view_append_column(GTK_TREE_VIEW(pli.tree_view), column);
2254
2255         renderer = gtk_cell_renderer_text_new();
2256         column = gtk_tree_view_column_new_with_attributes(
2257                 _("Label"), renderer, "text", POI_LABEL, NULL);
2258         gtk_tree_view_column_set_sort_column_id(column, POI_LABEL);
2259         gtk_tree_view_append_column(GTK_TREE_VIEW(pli.tree_view), column);
2260
2261         g_signal_connect(G_OBJECT(pli.tree_view), "row-activated",
2262                 G_CALLBACK(poi_list_row_activated), &pli);
2263
2264         gtk_tree_view_set_model(GTK_TREE_VIEW(pli.tree_view),
2265                 GTK_TREE_MODEL(store));
2266         g_object_unref(G_OBJECT(store));
2267
2268         /* Enable the help button. */
2269 #ifndef LEGACY
2270         hildon_help_dialog_help_enable(
2271 #else
2272         ossohelp_dialog_help_enable(
2273 #endif
2274                 GTK_DIALOG(pli.dialog), HELP_ID_POILIST, _osso);
2275
2276         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(pli.dialog)->action_area),
2277                 btn_goto = gtk_button_new_with_label(_("Go to")));
2278
2279         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(pli.dialog)->action_area),
2280                 btn_edit = gtk_button_new_with_label(_("Edit...")));
2281
2282         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(pli.dialog)->action_area),
2283                 btn_manage_checks = gtk_button_new_with_label(
2284                     _("Checked POI Actions...")));
2285
2286         gtk_dialog_add_button(GTK_DIALOG(pli.dialog),
2287                 GTK_STOCK_CLOSE, GTK_RESPONSE_ACCEPT);
2288
2289         gtk_window_set_default_size(GTK_WINDOW(pli.dialog), 500, 400);
2290
2291         scroller = gtk_scrolled_window_new (NULL, NULL);
2292         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroller),
2293                 GTK_SHADOW_ETCHED_IN);
2294         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroller),
2295                 GTK_POLICY_NEVER,
2296                 GTK_POLICY_AUTOMATIC);
2297         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(pli.dialog)->vbox),
2298                 scroller, TRUE, TRUE, 0);
2299
2300         gtk_container_add(GTK_CONTAINER(scroller), pli.tree_view);
2301
2302         g_signal_connect(G_OBJECT(btn_goto), "clicked",
2303                 G_CALLBACK(poi_list_goto), &pli);
2304
2305         g_signal_connect(G_OBJECT(btn_edit), "clicked",
2306                 G_CALLBACK(poi_list_view), &pli);
2307
2308         g_signal_connect(G_OBJECT(btn_manage_checks), "clicked",
2309                 G_CALLBACK(poi_list_manage_checks), &pli);
2310     }
2311
2312     /* Initialize the tree store. */
2313
2314     gtk_list_store_clear(store);
2315     pli.select_all = FALSE;
2316
2317     unit2latlon(unitx, unity, src_lat, src_lon);
2318
2319     for(curr = poi_list; curr; curr = curr->next)
2320     {
2321         PoiInfo *poi_info = curr->data;
2322         gchar tmp1[LL_FMT_LEN], tmp2[LL_FMT_LEN];
2323
2324         printf("poi: (%f, %f, %s, %s)\n",
2325                 poi_info->lat, poi_info->lon,
2326                 poi_info->label, poi_info->desc);
2327
2328         format_lat_lon(poi_info->lat, poi_info->lon, tmp1, tmp2);
2329         //lat_format(poi_info->lat, tmp1);
2330         //lon_format(poi_info->lon, tmp2);
2331
2332         gtk_list_store_append(store, &iter);
2333         gtk_list_store_set(store, &iter,
2334                 POI_SELECTED, TRUE,
2335                 POI_POIID, poi_info->poi_id,
2336                 POI_LAT, poi_info->lat,
2337                 POI_LON, poi_info->lon,
2338                 POI_BEARING, calculate_bearing(src_lat, src_lon,
2339                     poi_info->lat, poi_info->lon),
2340                 POI_DISTANCE, calculate_distance(src_lat,src_lon,
2341                     poi_info->lat, poi_info->lon) * UNITS_CONVERT[_units],
2342                 POI_LABEL, poi_info->label,
2343                 POI_DESC, poi_info->desc,
2344                 POI_CATID, poi_info->cat_id,
2345                 POI_CLABEL, poi_info->clabel,
2346                 -1);
2347     }
2348
2349     gtk_widget_show_all(pli.dialog);
2350
2351     GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(pli.dialog));
2352
2353     map_force_redraw();
2354
2355     gtk_widget_hide(pli.dialog);
2356
2357     vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
2358     return TRUE;
2359 }
2360
2361 gboolean
2362 poi_import_dialog(gint unitx, gint unity)
2363 {
2364     GtkWidget *dialog = NULL;
2365     gboolean success = FALSE;
2366     printf("%s()\n", __PRETTY_FUNCTION__);
2367
2368     dialog = hildon_file_chooser_dialog_new(GTK_WINDOW(_window),
2369             GTK_FILE_CHOOSER_ACTION_OPEN);
2370
2371     gtk_widget_show_all(dialog);
2372
2373     while(!success && gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
2374     {
2375         gchar *file_uri_str = NULL;
2376         gchar *bytes = NULL;
2377         gint size;
2378         GnomeVFSResult vfs_result;
2379         GList *poi_list = NULL;
2380
2381         file_uri_str = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog));
2382
2383         /* Parse the given file as GPX. */
2384         if(GNOME_VFS_OK != (vfs_result = gnome_vfs_read_entire_file(
2385                         file_uri_str, &size, &bytes)))
2386         {
2387             popup_error(dialog, gnome_vfs_result_to_string(vfs_result));
2388         }
2389         else if(gpx_poi_parse(bytes, size, &poi_list))
2390         {
2391             static GtkWidget *cat_dialog = NULL;
2392             static GtkWidget *cmb_category = NULL;
2393             static GtkWidget *btn_catedit = NULL;
2394             static PoiCategoryEditInfo pcedit;
2395
2396             if(!cat_dialog)
2397             {
2398                 GtkWidget *hbox;
2399                 GtkWidget *label;
2400                 cat_dialog = gtk_dialog_new_with_buttons(_("Default Category"),
2401                         GTK_WINDOW(dialog), GTK_DIALOG_MODAL,
2402                         GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
2403                         GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
2404                         NULL);
2405
2406                 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(cat_dialog)->vbox),
2407                         hbox = gtk_hbox_new(FALSE, 4), FALSE, FALSE, 4);
2408
2409                 gtk_box_pack_start(GTK_BOX(hbox),
2410                         label = gtk_label_new(_("Category")),
2411                         FALSE, FALSE, 0);
2412
2413                 gtk_box_pack_start(GTK_BOX(hbox),
2414                         cmb_category = poi_create_cat_combo(),
2415                         FALSE, FALSE, 4);
2416
2417                 gtk_box_pack_start(GTK_BOX(hbox),
2418                         btn_catedit = gtk_button_new_with_label(
2419                             _("Edit Categories...")),
2420                         FALSE, FALSE, 0);
2421
2422                 /* Connect Signals */
2423                 pcedit.dialog = dialog;
2424                 pcedit.cmb_category = cmb_category;
2425                 pcedit.cat_id = -1;
2426                 g_signal_connect(G_OBJECT(btn_catedit), "clicked",
2427                         G_CALLBACK(poi_edit_cat), &pcedit);
2428             }
2429
2430             gtk_widget_show_all(cat_dialog);
2431
2432             while(GTK_RESPONSE_ACCEPT ==gtk_dialog_run(GTK_DIALOG(cat_dialog)))
2433             {
2434                 if(gtk_combo_box_get_active(GTK_COMBO_BOX(cmb_category)) == -1)
2435                 {
2436                     popup_error(dialog,
2437                             _("Please specify a default category."));
2438                     continue;
2439                 }
2440
2441                 /* Insert the POIs into the database. */
2442                 gint num_inserts = poi_list_insert(dialog,
2443                         poi_list, GTK_COMBO_BOX(cmb_category));
2444
2445                 if(num_inserts)
2446                 {
2447                     /* Hide the dialogs. */
2448                     gtk_widget_hide(cat_dialog);
2449
2450                     /* Create a new dialog with the results. */
2451                     poi_list_dialog(dialog, unitx, unity, poi_list);
2452                     success = TRUE;
2453                 }
2454                 break;
2455             }
2456
2457             gtk_widget_hide(cat_dialog);
2458
2459             poi_list_free(poi_list);
2460         }
2461         else
2462             popup_error(dialog, _("Error parsing GPX file."));
2463
2464         g_free(file_uri_str);
2465         g_free(bytes);
2466     }
2467
2468     /* Hide the dialog. */
2469     gtk_widget_destroy(dialog);
2470
2471     vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
2472     return success;
2473 }
2474
2475 static gboolean
2476 poi_download_cat_selected(GtkComboBox *cmb_category, GtkEntry *txt_query)
2477 {
2478     GtkTreeIter iter;
2479     printf("%s()\n", __PRETTY_FUNCTION__);
2480
2481     if(gtk_combo_box_get_active_iter(GTK_COMBO_BOX(cmb_category), &iter))
2482     {
2483         gchar buffer[BUFFER_SIZE];
2484         GtkWidget *confirm = NULL;
2485         gchar *category = NULL;
2486
2487         gtk_tree_model_get(
2488                 gtk_combo_box_get_model(GTK_COMBO_BOX(cmb_category)), &iter,
2489                 1, &category,
2490                 -1);
2491
2492         if(*gtk_entry_get_text(txt_query))
2493         {
2494             snprintf(buffer, sizeof(buffer), "%s\n  %s",
2495                     _("Overwrite query with the following text?"), category);
2496             confirm = hildon_note_new_confirmation(GTK_WINDOW(_window),buffer);
2497
2498         }
2499
2500         if(confirm == NULL
2501                 || GTK_RESPONSE_OK == gtk_dialog_run(GTK_DIALOG(confirm)))
2502             gtk_entry_set_text(txt_query, category);
2503
2504         if(confirm)
2505             gtk_widget_destroy(confirm);
2506     }
2507
2508     vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
2509     return TRUE;
2510 }
2511
2512
2513 static gboolean
2514 origin_type_selected(GtkWidget *toggle, OriginToggleInfo *oti)
2515 {
2516     printf("%s()\n", __PRETTY_FUNCTION__);
2517
2518     if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(toggle)))
2519         gtk_widget_set_sensitive(oti->txt_origin, toggle == oti->rad_use_text);
2520
2521     vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
2522     return TRUE;
2523 }
2524
2525 gboolean
2526 poi_download_dialog(gint unitx, gint unity)
2527 {
2528     static GtkWidget *dialog = NULL;
2529     static GtkWidget *hbox = NULL;
2530     static GtkWidget *table = NULL;
2531     static GtkWidget *table2 = NULL;
2532     static GtkWidget *label = NULL;
2533     static GtkWidget *num_page = NULL;
2534     static GtkWidget *txt_source_url = NULL;
2535     static OriginToggleInfo oti;
2536     static GtkWidget *cmb_category;
2537     printf("%s()\n", __PRETTY_FUNCTION__);
2538
2539     conic_recommend_connected();
2540
2541     if(!dialog)
2542     {
2543         GtkEntryCompletion *origin_comp;
2544
2545         dialog = gtk_dialog_new_with_buttons(_("Download POIs"),
2546                 GTK_WINDOW(_window), GTK_DIALOG_MODAL,
2547                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
2548                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
2549                 NULL);
2550
2551         /* Enable the help button. */
2552 #ifndef LEGACY
2553         hildon_help_dialog_help_enable(
2554 #else
2555         ossohelp_dialog_help_enable(
2556 #endif
2557                 GTK_DIALOG(dialog), HELP_ID_DOWNPOI, _osso);
2558
2559         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
2560                 table = gtk_table_new(4, 4, FALSE), TRUE, TRUE, 0);
2561
2562         /* Source URL. */
2563         gtk_table_attach(GTK_TABLE(table),
2564                 hbox = gtk_hbox_new(FALSE, 4),
2565                 0, 4, 0, 1, GTK_EXPAND | GTK_FILL, 0, 2, 4);
2566         gtk_box_pack_start(GTK_BOX(hbox),
2567                 label = gtk_label_new(_("Source URL")), FALSE, TRUE, 4);
2568         gtk_box_pack_start(GTK_BOX(hbox),
2569                 txt_source_url = gtk_entry_new(), TRUE, TRUE, 4);
2570
2571         /* Auto. */
2572         gtk_table_attach(GTK_TABLE(table),
2573                 oti.rad_use_gps = gtk_radio_button_new_with_label(NULL,
2574                     _("Use GPS Location")),
2575                 0, 1, 1, 2, GTK_FILL, 0, 2, 4);
2576
2577         /* Use End of Route. */
2578         gtk_table_attach(GTK_TABLE(table),
2579                oti.rad_use_route = gtk_radio_button_new_with_label_from_widget(
2580                    GTK_RADIO_BUTTON(oti.rad_use_gps), _("Use End of Route")),
2581                0, 1, 2, 3, GTK_FILL, 0, 2, 4);
2582
2583
2584         gtk_table_attach(GTK_TABLE(table),
2585                 gtk_vseparator_new(),
2586                 1, 2, 1, 3, GTK_FILL, GTK_FILL, 2,4);
2587
2588         /* Category. */
2589         gtk_table_attach(GTK_TABLE(table),
2590                 label = gtk_label_new(_("Category")),
2591                 2, 3, 1, 2, GTK_FILL, 0, 2, 4);
2592         gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
2593         gtk_table_attach(GTK_TABLE(table),
2594                 cmb_category = poi_create_cat_combo(),
2595                 3, 4, 1, 2, GTK_FILL, 0, 2, 4);
2596
2597         /* Page. */
2598         gtk_table_attach(GTK_TABLE(table),
2599                 label = gtk_label_new(_("Page")),
2600                 2, 3, 2, 3, GTK_FILL, 0, 2, 4);
2601         gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
2602         gtk_table_attach(GTK_TABLE(table),
2603                 num_page = hildon_number_editor_new(1, 999),
2604                 3, 4, 2, 3, GTK_FILL, 0, 2, 4);
2605
2606
2607         /* Another table for the Origin and Query. */
2608         gtk_table_attach(GTK_TABLE(table),
2609                 table2 = gtk_table_new(2, 2, FALSE),
2610                 0, 4, 3, 4, GTK_EXPAND | GTK_FILL, 0, 2, 4);
2611
2612         /* Origin. */
2613         gtk_table_attach(GTK_TABLE(table2),
2614                 oti.rad_use_text = gtk_radio_button_new_with_label_from_widget(
2615                     GTK_RADIO_BUTTON(oti.rad_use_gps), _("Origin")),
2616                 0, 1, 0, 1, GTK_FILL, 0, 2, 4);
2617         gtk_table_attach(GTK_TABLE(table2),
2618                 oti.txt_origin = gtk_entry_new(),
2619                 1, 2, 0, 1, GTK_EXPAND | GTK_FILL, 0, 2, 4);
2620         gtk_entry_set_width_chars(GTK_ENTRY(oti.txt_origin), 25);
2621 #ifdef MAEMO_CHANGES
2622 #ifndef LEGACY
2623         g_object_set(G_OBJECT(oti.txt_origin), "hildon-input-mode",
2624                 HILDON_GTK_INPUT_MODE_FULL, NULL);
2625 #else
2626         g_object_set(G_OBJECT(oti.txt_origin), HILDON_AUTOCAP, FALSE, NULL);
2627 #endif
2628 #endif
2629
2630         /* Query. */
2631         gtk_table_attach(GTK_TABLE(table2),
2632                 label = gtk_label_new(_("Query")),
2633                 0, 1, 1, 2, GTK_FILL, 0, 2, 4);
2634         gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
2635         gtk_table_attach(GTK_TABLE(table2),
2636                 oti.txt_query = gtk_entry_new(),
2637                 1, 2, 1, 2, GTK_EXPAND | GTK_FILL, 0, 2, 4);
2638         gtk_entry_set_width_chars(GTK_ENTRY(oti.txt_query), 25);
2639 #ifdef MAEMO_CHANGES
2640 #ifndef LEGACY
2641         g_object_set(G_OBJECT(oti.txt_query), "hildon-input-mode",
2642                 HILDON_GTK_INPUT_MODE_FULL, NULL);
2643 #else
2644         g_object_set(G_OBJECT(oti.txt_query), HILDON_AUTOCAP, FALSE, NULL);
2645 #endif
2646 #endif
2647
2648         /* Set up auto-completion. */
2649         origin_comp = gtk_entry_completion_new();
2650         gtk_entry_completion_set_model(origin_comp,GTK_TREE_MODEL(_loc_model));
2651         gtk_entry_completion_set_text_column(origin_comp, 0);
2652         gtk_entry_set_completion(GTK_ENTRY(oti.txt_origin), origin_comp);
2653
2654         g_signal_connect(G_OBJECT(oti.rad_use_gps), "toggled",
2655                           G_CALLBACK(origin_type_selected), &oti);
2656         g_signal_connect(G_OBJECT(oti.rad_use_route), "toggled",
2657                           G_CALLBACK(origin_type_selected), &oti);
2658         g_signal_connect(G_OBJECT(oti.rad_use_text), "toggled",
2659                           G_CALLBACK(origin_type_selected), &oti);
2660
2661         g_signal_connect(G_OBJECT(cmb_category), "changed",
2662                 G_CALLBACK(poi_download_cat_selected), oti.txt_query);
2663     }
2664
2665     /* Initialize fields. */
2666
2667     hildon_number_editor_set_value(HILDON_NUMBER_EDITOR(num_page), 1);
2668
2669     gtk_entry_set_text(GTK_ENTRY(txt_source_url), _poi_dl_url);
2670     if(unity != 0)
2671     {
2672         gchar buffer[80];
2673         gchar strlat[32];
2674         gchar strlon[32];
2675         gdouble lat, lon;
2676
2677         unit2latlon(unitx, unity, lat, lon);
2678
2679         g_ascii_formatd(strlat, 32, "%.06f", lat);
2680         g_ascii_formatd(strlon, 32, "%.06f", lon);
2681         snprintf(buffer, sizeof(buffer), "%s, %s", strlat, strlon);
2682
2683         gtk_entry_set_text(GTK_ENTRY(oti.txt_origin), buffer);
2684         gtk_toggle_button_set_active(
2685                 GTK_TOGGLE_BUTTON(oti.rad_use_text), TRUE);
2686     }
2687     /* Else use "End of Route" by default if they have a route. */
2688     else if(_route.head != _route.tail)
2689     {
2690         /* There is no route, so make it the default. */
2691         gtk_widget_set_sensitive(oti.rad_use_route, TRUE);
2692         gtk_toggle_button_set_active(
2693                 GTK_TOGGLE_BUTTON(oti.rad_use_route), TRUE);
2694         gtk_widget_grab_focus(oti.rad_use_route);
2695     }
2696     /* Else use "GPS Location" if they have GPS enabled. */
2697     else
2698     {
2699         /* There is no route, so desensitize "Use End of Route." */
2700         gtk_widget_set_sensitive(oti.rad_use_route, FALSE);
2701         if(_enable_gps)
2702         {
2703             gtk_toggle_button_set_active(
2704                     GTK_TOGGLE_BUTTON(oti.rad_use_gps), TRUE);
2705             gtk_widget_grab_focus(oti.rad_use_gps);
2706         }
2707         /* Else use text. */
2708         else
2709         {
2710             gtk_toggle_button_set_active(
2711                     GTK_TOGGLE_BUTTON(oti.rad_use_text), TRUE);
2712             gtk_widget_grab_focus(oti.txt_origin);
2713         }
2714     }
2715
2716     gtk_widget_show_all(dialog);
2717
2718     while(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
2719     {
2720         gchar origin_buffer[BUFFER_SIZE];
2721         const gchar *source_url, *origin, *query;
2722         gchar *file_uri_str = NULL;
2723         gchar *bytes = NULL;
2724         gint size;
2725         GnomeVFSResult vfs_result;
2726         GList *poi_list = NULL;
2727
2728         source_url = gtk_entry_get_text(GTK_ENTRY(txt_source_url));
2729         if(!strlen(source_url))
2730         {
2731             popup_error(dialog, _("Please specify a source URL."));
2732             continue;
2733         }
2734         else
2735         {
2736             g_free(_poi_dl_url);
2737             _poi_dl_url = g_strdup(source_url);
2738         }
2739
2740         if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(oti.rad_use_gps)))
2741         {
2742             gchar strlat[32];
2743             gchar strlon[32];
2744             latlon2unit(_gps.lat, _gps.lon, unitx, unity);
2745             g_ascii_formatd(strlat, 32, "%.06f", _gps.lat);
2746             g_ascii_formatd(strlon, 32, "%.06f", _gps.lon);
2747             snprintf(origin_buffer, sizeof(origin_buffer),
2748                     "%s, %s", strlat, strlon);
2749             origin = origin_buffer;
2750         }
2751         else if(gtk_toggle_button_get_active(
2752                     GTK_TOGGLE_BUTTON(oti.rad_use_route)))
2753         {
2754             gchar strlat[32];
2755             gchar strlon[32];
2756             Point *p;
2757             gdouble lat, lon;
2758
2759             /* Use last non-zero route point. */
2760             for(p = _route.tail; !p->unity; p--) { }
2761
2762             unitx = p->unitx;
2763             unity = p->unity;
2764             unit2latlon(p->unitx, p->unity, lat, lon);
2765             g_ascii_formatd(strlat, 32, "%.06f", lat);
2766             g_ascii_formatd(strlon, 32, "%.06f", lon);
2767             snprintf(origin_buffer, sizeof(origin_buffer),
2768                     "%s, %s", strlat, strlon);
2769             origin = origin_buffer;
2770         }
2771         else
2772         {
2773             Point porig;
2774             origin = gtk_entry_get_text(GTK_ENTRY(oti.txt_origin));
2775             if(*origin)
2776             {
2777                 porig = locate_address(dialog, origin);
2778                 if(!porig.unity)
2779                     continue;
2780             }
2781         }
2782
2783         if(!*origin)
2784         {
2785             popup_error(dialog, _("Please specify an origin."));
2786             continue;
2787         }
2788
2789         if(gtk_combo_box_get_active(GTK_COMBO_BOX(cmb_category)) == -1)
2790         {
2791             popup_error(dialog, _("Please specify a default category."));
2792             continue;
2793         }
2794
2795         query = gtk_entry_get_text(GTK_ENTRY(oti.txt_query));
2796         if(!strlen(query))
2797         {
2798             popup_error(dialog, _("Please specify a query."));
2799             continue;
2800         }
2801
2802         /* Construct the URL. */
2803         {
2804             gchar *origin_escaped;
2805             gchar *query_escaped;
2806
2807             origin_escaped = gnome_vfs_escape_string(origin);
2808             query_escaped = gnome_vfs_escape_string(query);
2809             file_uri_str = g_strdup_printf(
2810                     source_url, origin_escaped, query_escaped,
2811                     hildon_number_editor_get_value(
2812                         HILDON_NUMBER_EDITOR(num_page)));
2813             g_free(origin_escaped);
2814             g_free(query_escaped);
2815         }
2816
2817         /* Parse the given file as GPX. */
2818         if(GNOME_VFS_OK != (vfs_result = gnome_vfs_read_entire_file(
2819                         file_uri_str, &size, &bytes)))
2820         {
2821             popup_error(dialog, gnome_vfs_result_to_string(vfs_result));
2822         }
2823         else if(strncmp(bytes, "<?xml", strlen("<?xml")))
2824         {
2825             /* Not an XML document - must be bad locations. */
2826             popup_error(dialog, _("Invalid origin or query."));
2827             printf("bytes: %s\n", bytes);
2828         }
2829         else if(gpx_poi_parse(bytes, size, &poi_list))
2830         {
2831             /* Insert the POIs into the database. */
2832             gint num_inserts = poi_list_insert(dialog, poi_list,
2833                     GTK_COMBO_BOX(cmb_category));
2834
2835             if(num_inserts)
2836             {
2837                 /* Create a new dialog with the results. */
2838                 poi_list_dialog(dialog, unitx, unity, poi_list);
2839             }
2840
2841             poi_list_free(poi_list);
2842         }
2843         else
2844             popup_error(dialog, _("Error parsing GPX file."));
2845
2846         g_free(file_uri_str);
2847         g_free(bytes);
2848
2849         /* Increment the page number for them. */
2850         hildon_number_editor_set_value(HILDON_NUMBER_EDITOR(num_page),
2851             hildon_number_editor_get_value(HILDON_NUMBER_EDITOR(num_page)) +1);
2852     }
2853
2854     /* Hide the dialog. */
2855     gtk_widget_hide(dialog);
2856
2857     vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
2858     return TRUE;
2859 }
2860
2861 gboolean
2862 poi_browse_dialog(gint unitx, gint unity)
2863 {
2864     static GtkWidget *dialog = NULL;
2865     static GtkWidget *table = NULL;
2866     static GtkWidget *table2 = NULL;
2867     static GtkWidget *label = NULL;
2868     static GtkWidget *cmb_category = NULL;
2869     static OriginToggleInfo oti;
2870     printf("%s()\n", __PRETTY_FUNCTION__);
2871
2872     if(!dialog)
2873     {
2874         GtkEntryCompletion *origin_comp;
2875
2876         dialog = gtk_dialog_new_with_buttons(_("Browse POIs"),
2877                 GTK_WINDOW(_window), GTK_DIALOG_MODAL,
2878                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
2879                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
2880                 NULL);
2881
2882         /* Enable the help button. */
2883 #ifndef LEGACY
2884         hildon_help_dialog_help_enable(
2885 #else
2886         ossohelp_dialog_help_enable(
2887 #endif
2888                 GTK_DIALOG(dialog), HELP_ID_BROWSEPOI, _osso);
2889
2890         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
2891                 table = gtk_table_new(3, 4, FALSE), TRUE, TRUE, 0);
2892
2893         /* Auto. */
2894         gtk_table_attach(GTK_TABLE(table),
2895                 oti.rad_use_gps = gtk_radio_button_new_with_label(NULL,
2896                     _("Use GPS Location")),
2897                 0, 1, 0, 1, GTK_FILL, 0, 2, 4);
2898
2899         /* Use End of Route. */
2900         gtk_table_attach(GTK_TABLE(table),
2901                oti.rad_use_route = gtk_radio_button_new_with_label_from_widget(
2902                    GTK_RADIO_BUTTON(oti.rad_use_gps), _("Use End of Route")),
2903                0, 1, 1, 2, GTK_FILL, 0, 2, 4);
2904
2905         gtk_table_attach(GTK_TABLE(table),
2906                 gtk_vseparator_new(),
2907                 1, 2, 0, 2, GTK_FILL, GTK_FILL, 2, 4);
2908
2909         /* Category. */
2910         gtk_table_attach(GTK_TABLE(table),
2911                 label = gtk_label_new(_("Category")),
2912                 2, 3, 0, 1, GTK_FILL, 0, 2, 4);
2913         gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
2914         gtk_table_attach(GTK_TABLE(table),
2915                 cmb_category = poi_create_cat_combo(),
2916                 3, 4, 0, 1, GTK_FILL, 0, 2, 4);
2917         /* Add an extra, "<any>" category. */
2918         {
2919             GtkTreeIter iter;
2920             GtkListStore *store = GTK_LIST_STORE(gtk_combo_box_get_model(
2921                         GTK_COMBO_BOX(cmb_category)));
2922             gtk_list_store_prepend(store, &iter);
2923             gtk_list_store_set(store, &iter, 0, -1, 1, "<any>", -1);
2924             gtk_combo_box_set_active_iter(GTK_COMBO_BOX(cmb_category), &iter);
2925         }
2926
2927
2928         /* Another table for the Origin and Query. */
2929         gtk_table_attach(GTK_TABLE(table),
2930                 table2 = gtk_table_new(2, 2, FALSE),
2931                 0, 4, 2, 3, GTK_EXPAND | GTK_FILL, 0, 2, 4);
2932
2933         /* Origin. */
2934         gtk_table_attach(GTK_TABLE(table2),
2935                 oti.rad_use_text = gtk_radio_button_new_with_label_from_widget(
2936                     GTK_RADIO_BUTTON(oti.rad_use_gps), _("Origin")),
2937                 0, 1, 0, 1, GTK_FILL, 0, 2, 4);
2938         gtk_table_attach(GTK_TABLE(table2),
2939                 oti.txt_origin = gtk_entry_new(),
2940                 1, 2, 0, 1, GTK_EXPAND | GTK_FILL, 0, 2, 4);
2941         gtk_entry_set_width_chars(GTK_ENTRY(oti.txt_origin), 25);
2942 #ifdef MAEMO_CHANGES
2943 #ifndef LEGACY
2944         g_object_set(G_OBJECT(oti.txt_origin), "hildon-input-mode",
2945                 HILDON_GTK_INPUT_MODE_FULL, NULL);
2946 #else
2947         g_object_set(G_OBJECT(oti.txt_origin), HILDON_AUTOCAP, FALSE, NULL);
2948 #endif
2949 #endif
2950
2951         /* Destination. */
2952         gtk_table_attach(GTK_TABLE(table2),
2953                 label = gtk_label_new(_("Query")),
2954                 0, 1, 1, 2, GTK_FILL, 0, 2, 4);
2955         gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
2956         gtk_table_attach(GTK_TABLE(table2),
2957                 oti.txt_query = gtk_entry_new(),
2958                 1, 2, 1, 2, GTK_EXPAND | GTK_FILL, 0, 2, 4);
2959         gtk_entry_set_width_chars(GTK_ENTRY(oti.txt_query), 25);
2960 #ifdef MAEMO_CHANGES
2961 #ifndef LEGACY
2962         g_object_set(G_OBJECT(oti.txt_query), "hildon-input-mode",
2963                 HILDON_GTK_INPUT_MODE_FULL, NULL);
2964 #else
2965         g_object_set(G_OBJECT(oti.txt_query), HILDON_AUTOCAP, FALSE, NULL);
2966 #endif
2967 #endif
2968
2969         /* Set up auto-completion. */
2970         origin_comp = gtk_entry_completion_new();
2971         gtk_entry_completion_set_model(origin_comp,GTK_TREE_MODEL(_loc_model));
2972         gtk_entry_completion_set_text_column(origin_comp, 0);
2973         gtk_entry_set_completion(GTK_ENTRY(oti.txt_origin), origin_comp);
2974
2975         g_signal_connect(G_OBJECT(oti.rad_use_gps), "toggled",
2976                           G_CALLBACK(origin_type_selected), &oti);
2977         g_signal_connect(G_OBJECT(oti.rad_use_route), "toggled",
2978                           G_CALLBACK(origin_type_selected), &oti);
2979         g_signal_connect(G_OBJECT(oti.rad_use_text), "toggled",
2980                           G_CALLBACK(origin_type_selected), &oti);
2981     }
2982
2983     /* Initialize fields. */
2984
2985     if(unity != 0)
2986     {
2987         gchar buffer[80];
2988         gchar strlat[32];
2989         gchar strlon[32];
2990         gdouble lat, lon;
2991
2992         unit2latlon(unitx, unity, lat, lon);
2993
2994         g_ascii_formatd(strlat, 32, "%.06f", lat);
2995         g_ascii_formatd(strlon, 32, "%.06f", lon);
2996         snprintf(buffer, sizeof(buffer), "%s, %s", strlat, strlon);
2997
2998         gtk_entry_set_text(GTK_ENTRY(oti.txt_origin), buffer);
2999         gtk_toggle_button_set_active(
3000                 GTK_TOGGLE_BUTTON(oti.rad_use_text), TRUE);
3001     }
3002     /* Else use "End of Route" by default if they have a route. */
3003     else if(_route.head != _route.tail)
3004     {
3005         /* There is no route, so make it the default. */
3006         gtk_widget_set_sensitive(oti.rad_use_route, TRUE);
3007         gtk_toggle_button_set_active(
3008                 GTK_TOGGLE_BUTTON(oti.rad_use_route), TRUE);
3009         gtk_widget_grab_focus(oti.rad_use_route);
3010     }
3011     /* Else use "GPS Location" if they have GPS enabled. */
3012     else
3013     {
3014         /* There is no route, so desensitize "Use End of Route." */
3015         gtk_widget_set_sensitive(oti.rad_use_route, FALSE);
3016         if(_enable_gps)
3017         {
3018             gtk_toggle_button_set_active(
3019                     GTK_TOGGLE_BUTTON(oti.rad_use_gps), TRUE);
3020             gtk_widget_grab_focus(oti.rad_use_gps);
3021         }
3022         /* Else use text. */
3023         else
3024         {
3025             gtk_toggle_button_set_active(
3026                     GTK_TOGGLE_BUTTON(oti.rad_use_text), TRUE);
3027             gtk_widget_grab_focus(oti.txt_origin);
3028         }
3029     }
3030
3031     gtk_widget_show_all(dialog);
3032
3033     while(gtk_dialog_run(GTK_DIALOG(dialog)) ==GTK_RESPONSE_ACCEPT)
3034     {
3035         gchar buffer[BUFFER_SIZE];
3036         const gchar *origin, *query;
3037         gdouble lat, lon;
3038         GList *poi_list = NULL;
3039         gint cat_id;
3040         gboolean is_cat = FALSE;
3041         sqlite3_stmt *stmt;
3042
3043         if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(oti.rad_use_gps)))
3044         {
3045             gchar strlat[32];
3046             gchar strlon[32];
3047             latlon2unit(_gps.lat, _gps.lon, unitx, unity);
3048             g_ascii_formatd(strlat, 32, "%.06f", _gps.lat);
3049             g_ascii_formatd(strlon, 32, "%.06f", _gps.lon);
3050             snprintf(buffer, sizeof(buffer), "%s, %s", strlat, strlon);
3051             origin = buffer;
3052         }
3053         else if(gtk_toggle_button_get_active(
3054                     GTK_TOGGLE_BUTTON(oti.rad_use_route)))
3055         {
3056             gchar strlat[32];
3057             gchar strlon[32];
3058             Point *p;
3059             gdouble lat, lon;
3060
3061             /* Use last non-zero route point. */
3062             for(p = _route.tail; !p->unity; p--) { }
3063
3064             unitx = p->unitx;
3065             unity = p->unity;
3066             unit2latlon(p->unitx, p->unity, lat, lon);
3067             g_ascii_formatd(strlat, 32, "%.06f", lat);
3068             g_ascii_formatd(strlon, 32, "%.06f", lon);
3069             snprintf(buffer, sizeof(buffer), "%s, %s", strlat, strlon);
3070             origin = buffer;
3071         }
3072         else
3073         {
3074             Point porig;
3075             origin = gtk_entry_get_text(GTK_ENTRY(oti.txt_origin));
3076             porig = locate_address(dialog, origin);
3077             if(!porig.unity)
3078                 continue;
3079         }
3080
3081         if(!strlen(origin))
3082         {
3083             popup_error(dialog, _("Please specify an origin."));
3084             continue;
3085         }
3086
3087         /* Check if we're doing a category search. */
3088         {
3089             GtkTreeIter iter;
3090             if(gtk_combo_box_get_active_iter(
3091                     GTK_COMBO_BOX(cmb_category), &iter))
3092             {
3093                 gtk_tree_model_get(
3094                         gtk_combo_box_get_model(GTK_COMBO_BOX(cmb_category)),
3095                         &iter, 0, &cat_id, -1);
3096                 if(cat_id >= 0)
3097                 {
3098                     is_cat = TRUE;
3099                 }
3100             }
3101         }
3102
3103         query = g_strdup_printf("%%%s%%",
3104                 gtk_entry_get_text(GTK_ENTRY(oti.txt_query)));
3105
3106         unit2latlon(unitx, unity, lat, lon);
3107
3108         if(is_cat)
3109         {
3110             if(SQLITE_OK != sqlite3_bind_int(_stmt_browsecat_poi, 1, cat_id) ||
3111                SQLITE_OK != sqlite3_bind_text(_stmt_browsecat_poi, 2, query,
3112                    -1, g_free) ||
3113                SQLITE_OK != sqlite3_bind_double(_stmt_browsecat_poi, 3, lat) ||
3114                SQLITE_OK != sqlite3_bind_double(_stmt_browsecat_poi, 4, lon))
3115             {
3116                 g_printerr("Failed to bind values for _stmt_browsecat_poi\n");
3117                 continue;
3118             }
3119             stmt = _stmt_browsecat_poi;
3120         }
3121         else
3122         {
3123             if(SQLITE_OK != sqlite3_bind_text(_stmt_browse_poi, 1, query,
3124                         -1, g_free) ||
3125                SQLITE_OK != sqlite3_bind_double(_stmt_browse_poi, 2, lat) ||
3126                SQLITE_OK != sqlite3_bind_double(_stmt_browse_poi, 3, lon))
3127             {
3128                 g_printerr("Failed to bind values for _stmt_browse_poi\n");
3129                 continue;
3130             }
3131             stmt = _stmt_browse_poi;
3132         }
3133
3134         while(SQLITE_ROW == sqlite3_step(stmt))
3135         {
3136             PoiInfo *poi = g_slice_new(PoiInfo);
3137             poi->poi_id = sqlite3_column_int(stmt, 0);
3138             poi->cat_id = sqlite3_column_int(stmt, 1);
3139             poi->lat = sqlite3_column_double(stmt, 2);
3140             poi->lon = sqlite3_column_double(stmt, 3);
3141             poi->label =g_strdup(sqlite3_column_text(stmt, 4));
3142             poi->desc = g_strdup(sqlite3_column_text(stmt, 5));
3143             poi->clabel=g_strdup(sqlite3_column_text(stmt, 6));
3144             poi_list = g_list_prepend(poi_list, poi);
3145         }
3146         sqlite3_reset(stmt);
3147
3148         if(poi_list)
3149         {
3150             /* Create a new dialog with the results. */
3151             poi_list_dialog(dialog, unitx, unity, poi_list);
3152             poi_list_free(poi_list);
3153         }
3154         else
3155             popup_error(dialog, _("No POIs found."));
3156     }
3157
3158     map_force_redraw();
3159
3160     /* Hide the dialog. */
3161     gtk_widget_hide(dialog);
3162
3163     vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
3164     return TRUE;
3165 }
3166
3167 /**
3168  * Render all the POI data.  This should be done before rendering track data.
3169  */
3170 void
3171 map_render_poi()
3172 {
3173     gint unitx, unity;
3174     gdouble lat1, lat2, lon1, lon2;
3175     gchar buffer[100];
3176     gint poix, poiy;
3177     GdkPixbuf *pixbuf = NULL;
3178     GError *error = NULL;
3179     printf("%s()\n", __PRETTY_FUNCTION__);
3180
3181     if(_poi_db && _poi_zoom > _zoom)
3182     {
3183         gint diag_offset = pixel2unit(MAX(_view_width_pixels,
3184                     _view_height_pixels) / 2);
3185         buf2unit(0, _view_height_pixels, unitx, unity);
3186         unitx = _center.unitx - diag_offset;
3187         unity = _center.unity + diag_offset;
3188         unit2latlon(unitx, unity, lat1, lon1);
3189         unitx = _center.unitx + diag_offset;
3190         unity = _center.unity - diag_offset;
3191         unit2latlon(unitx, unity, lat2, lon2);
3192
3193         if(SQLITE_OK != sqlite3_bind_double(_stmt_select_poi, 1, lat1) ||
3194            SQLITE_OK != sqlite3_bind_double(_stmt_select_poi, 2, lat2) ||
3195            SQLITE_OK != sqlite3_bind_double(_stmt_select_poi, 3, lon1) ||
3196            SQLITE_OK != sqlite3_bind_double(_stmt_select_poi, 4, lon2))
3197         {
3198             g_printerr("Failed to bind values for _stmt_select_poi\n");
3199             return;
3200         }
3201
3202         while(SQLITE_ROW == sqlite3_step(_stmt_select_poi))
3203         {
3204             lat1 = sqlite3_column_double(_stmt_select_poi, 0);
3205             lon1 = sqlite3_column_double(_stmt_select_poi, 1);
3206             gchar *poi_label = g_utf8_strdown(sqlite3_column_text(
3207                     _stmt_select_poi, 3), -1);
3208             gchar *cat_label = g_utf8_strdown(sqlite3_column_text(
3209                     _stmt_select_poi, 6), -1);
3210
3211             latlon2unit(lat1, lon1, unitx, unity);
3212             unit2buf(unitx, unity, poix, poiy);
3213
3214             /* Try to get icon for specific POI first. */
3215             snprintf(buffer, sizeof(buffer), "%s/%s.jpg",
3216                     _poi_db_dirname, poi_label);
3217             pixbuf = gdk_pixbuf_new_from_file(buffer, &error);
3218             if(error)
3219             {
3220                 /* No icon for specific POI - try for category. */
3221                 error = NULL;
3222                 snprintf(buffer, sizeof(buffer), "%s/%s.jpg",
3223                         _poi_db_dirname, cat_label);
3224                 pixbuf = gdk_pixbuf_new_from_file(buffer, &error);
3225             }
3226             if(error)
3227             {
3228                 /* No icon for POI or for category.
3229                  * Try default POI icon file. */
3230                 error = NULL;
3231                 snprintf(buffer, sizeof(buffer), "%s/poi.jpg",
3232                         _poi_db_dirname);
3233                 pixbuf = gdk_pixbuf_new_from_file(buffer, &error);
3234             }
3235             if(error)
3236             {
3237                 /* No icon for POI or for category or default POI icon file.
3238                    Draw default purple square. */
3239                 error = NULL;
3240                 gdk_draw_rectangle(_map_pixmap, _gc[COLORABLE_POI], TRUE,
3241                         poix - (gint)(1.5f * _draw_width),
3242                         poiy - (gint)(1.5f * _draw_width),
3243                         3 * _draw_width,
3244                         3 * _draw_width);
3245             }
3246             else
3247             {
3248                 /* We found an icon to draw. */
3249                 gdk_draw_pixbuf(
3250                         _map_pixmap,
3251                         _gc[COLORABLE_POI],
3252                         pixbuf,
3253                         0, 0,
3254                         poix - gdk_pixbuf_get_width(pixbuf) / 2,
3255                         poiy - gdk_pixbuf_get_height(pixbuf) / 2,
3256                         -1,-1,
3257                         GDK_RGB_DITHER_NONE, 0, 0);
3258                 g_object_unref(pixbuf);
3259             }
3260
3261             g_free(poi_label);
3262             g_free(cat_label);
3263         }
3264         sqlite3_reset(_stmt_select_poi);
3265     }
3266
3267     vprintf("%s(): return\n", __PRETTY_FUNCTION__);
3268 }
3269
3270 void
3271 poi_destroy()
3272 {
3273     printf("%s()\n", __PRETTY_FUNCTION__);
3274
3275     if(_poi_db) 
3276     { 
3277         sqlite3_close(_poi_db); 
3278         _poi_db = NULL; 
3279     }
3280
3281     vprintf("%s(): return\n", __PRETTY_FUNCTION__);
3282 }
3283
3284 #ifdef INCLUDE_APRS
3285 extern AprsDataRow *n_first;               // pointer to first element in name sorted station list
3286
3287
3288 /////////////////////
3289
3290 /**
3291  * Render all the APRS data.
3292  */
3293 void
3294 map_render_aprs()
3295 {
3296
3297     printf("%s()\n", __PRETTY_FUNCTION__);
3298     int mm;
3299
3300     if(_poi_zoom > _zoom)
3301     {
3302
3303         AprsDataRow *p_station = n_first;
3304
3305         while ( (p_station) != NULL) 
3306         {
3307             if( p_station->coord_lat != 0.0f 
3308                 || p_station->coord_lon != 0.0f  ) 
3309             {
3310                         plot_aprs_station( p_station, FALSE);
3311             } // If valid data
3312
3313             (p_station) = (p_station)->n_next;  // Next element in list
3314         } // End of while loop
3315
3316
3317     } // check for zoom level
3318
3319     vprintf("%s(): return\n", __PRETTY_FUNCTION__);
3320 }
3321
3322 #endif // INCLUDE_APRS
3323