]> err.no Git - mapper/blob - src/poi.c
f20c80ed470cb5a42b46231e57a04f4f7e86425d
[mapper] / src / poi.c
1 #include <config.h>
2
3 #define _GNU_SOURCE
4
5 #include <unistd.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <strings.h>
9 #include <stddef.h>
10 #include <locale.h>
11 #include <math.h>
12 #include <errno.h>
13 #include <glib/gstdio.h>
14 #include <gtk/gtk.h>
15 #include <fcntl.h>
16 #include <gconf/gconf-client.h>
17 #include <libxml/parser.h>
18 #include <libintl.h>
19 #include <locale.h>
20 #include <sqlite3.h>
21
22 #include "utils.h"
23 #include "poi.h"
24 #include "gps.h"
25 #include "map.h"
26 #include "mapper-types.h"
27 #include "settings.h"
28 #include "db.h"
29 #include "osm.h"
30 #include "osm-db.h"
31
32 struct _poi_categories {
33         node_type_t type;
34         const gchar *name, *desc, *icon, *color;
35 };
36
37 /* The default POI categories */
38 static struct _poi_categories default_poi_categories[] = {
39         { NODE_AMENITY_PARKING, "Parking", "Parking place for vehicles."  , "vehicle/parking", "#2020ff" },
40         { NODE_AMENITY_FUEL, "Fuel", "Stations for purchasing fuel for vehicles."  , "vehicle/fuel_station", "#4040f0" },
41         { NODE_AMENITY_SPEEDCAM, "Speed Cam", "Automatic speed cameras."  , "vehicle/restrictions/speed_trap", "#ff0000" },
42         { NODE_AMENITY_HOSPITAL, "Hospital", ""  , "health/hospital", "#ff4040" },
43         { NODE_AMENITY_PHARMACY, "Pharmacy", "Place to get drugs."  , "health/pharmacy", "#40f040" },
44         { NODE_AMENITY_POLICE, "Police", "Police station" , "public/police", "#8570ff" },
45         { NODE_TOURISM_HOTEL, "Hotel", "Places to stay temporarily or for the night." , "accommodation/hotel", "#ba20ba" },
46         { NODE_TOURISM_HOSTEL, "Hostel", "Places to stay temporarily or for the night. Cheap." , "accommodation/hostel", "#ba30ba" },
47         { NODE_TOURISM_MOTEL, "Motel", "Places to stay temporarily or for the night. Cheap." , "accommodation/motel", "#ba40ba" },
48         { NODE_AMENITY_ATM, "ATM", "Automatic Teller Machine/Cashpoint." , "money/atm", "#40a040" },
49         { NODE_AMENITY_BANK, "Bank", "Place to take care of your money." , "money/bank", "#50b550" },
50         { NODE_AMENITY_POST, "Post office", "Place to handle mail." , "public/post_office", "#ff6868" },
51         { NODE_AMENITY_POST_BOX, "Post box", "Send letters." , "public/post_box", "#ff6060" },
52         { NODE_TOURISM_INFO, "Tourism info", "A place for tourists to get information." , "misc/information", "#4af04a" },
53         { NODE_AMENITY_TAXI, "Taxi station", "Get a Taxi here." , "", "#50ffff" },
54         { NODE_RAILWAY_STATION, "Railway station", "Transportation by train." , "transport/railway", "#fa7070" },
55         { NODE_RAILWAY_HALT, "Railway halt", "" , "transport/railway_small", "#f27777" },
56         { NODE_AMENITY_BUS_STATION, "Bus station", "Transportation by bus." , "transport/bus", "#f07070" },
57         { NODE_AMENITY_BOAT, "Harbour", "Transportation by boat." , "transport/ferry", "#9090ee" },
58         { NODE_AIRPORT_TERMINAL, "Airport", "Transportation by air." , "transport/airport", "#909099" },
59         { NODE_TOURISM_CAMP_SITE, "Camp site", "Place to go camping" , "accommodation/camping", "#90f080" },
60         { NODE_TOURISM_CARAVAN_SITE, "Caravan site", "" , "accommodation/camping/caravan", "#90ff80" },
61         { NODE_TOURISM_PICNIC_SITE, "Picnic", "Place to have a Picnic" , "recreation/picnic", "#a5f8e5" },
62         { NODE_AMENITY_FOOD, "Restaurant, Fast food", "Places to eat or drink." , "food/fastfood", "#e5960d" },
63         { NODE_AMENITY_PUB, "Pub, Disco, Club", "Place to drink and party." , "food/pub", "#f5960d" },
64         { NODE_AMENITY_CAFE, "Cafe", "Place to drink coffe or tee and eat." , "food/cafe", "#ff960d" },
65         { NODE_AMENITY_CINEMA, "Cinema", "Place to see movies" , "recreation/cinema", "#9090a0" },
66         { NODE_AMENITY_THEATRE, "Theatre", "Place to see people performing" , "recreation/theater", "#9595a5" },
67         { NODE_AMENITY_SHOP, "Shopping", "Places to shop or acquire services." , "shopping", "#61ef1b" },
68         { NODE_AMENITY_POW, "Place of Worchip", "" , "religion/church", "#7c5b0b" },
69         { NODE_TOURISM_THEME_PARK, "Theme Park", "Place to have fun and ride for example rollercoasters." , "recreation/theme_park", "#8eef1b" },
70         { NODE_AMENITY_COLLEGE, "College Campus/Building", "" , "education/college", "#813fdc" },
71         { NODE_AMENITY_SCHOOL, "School", "" , "education/school", "#813fdc" },
72         { NODE_AMENITY_LIBRARY, "Library", "Place to read and borrow books and magazines" , "shopping/rental/library", "#803090" },
73         { NODE_AMENITY_TOWNHALL, "Townhall", "" , "", "#408090" },
74         { NODE_AMENITY_WC, "WC/Toilets", "" , "public/toilets", "#e1d62c" },
75         { NODE_AMENITY_TELEPHONE, "Telephone", "Public telephone" , "public/telephone", "#208060" },
76         { NODE_TOURISM_ATTRACTION, "Attraction", "Something interesting" , "generic", "#004000" },
77
78         { NODE_SPORT_CENTER, "Sport Center", "" , "sports/centre", "#101080" },
79         { NODE_SPORT_STADIUM, "Sport Stadium", "" , "sports/stadium", "#101080" },
80         { NODE_SPORT_SKIING, "Skiing", "" , "sports/skiing", "#5050A0" },
81         { NODE_SPORT_SWIMMING, "Swimming", "" , "sports/swimming", "#102080" },
82         { NODE_SPORT_FOOTBALL, "Football", "" , "sports/football", "#102080" },
83         { NODE_SPORT_SOCCER, "Soccer", "" , "sports/soccer", "#102080" },
84         { NODE_SPORT_GOLF, "Golf", "" , "sports/golf", "#102080" },
85         { NODE_SPORT_TENNIS, "Tennis", "" , "sports/tennis", "#101080" },
86         { NODE_SPORT_BOWLING, "Bowling", "" , "", "#101080" },
87         { NODE_SPORT_RUGBY, "Rugby", "" , "", "#101080" },
88         { NODE_SPORT_CLIMBING, "Climbing", "" , "", "#101080" },
89         { NODE_SPORT_CYCLING, "Cycling", "" , "sports/bicycle", "#101080" },
90         { NODE_SPORT_MOTOR, "Motor sport", "" , "", "#101080" },
91         { NODE_SPORT_HOCKEY, "Hockey", "" , "", "#5050A0" },
92         { NODE_SPORT_SKATING, "Skating", "" , "", "#5050A0" },
93         { NODE_SPORT_SKATEBOARD, "Skateboard", "" , "", "#101080" },
94         { NODE_SPORT_HORSES, "Horses", "Horse riding or racing" , "sports/riding", "#101080" },
95         { NODE_SPORT_DOG, "Dog racing", "" , "", "#101080" },
96         { NODE_SPORT_BASKETBALL, "Basketball", "" , "", "#101080" },
97         { NODE_SPORT_BASEBALL, "Baseball", "" , "", "#101080" },
98         { NODE_SPORT_CANOE, "Canoe", "" , "", "#101080" },
99         { NODE_SPORT_CROQUET, "Croquet", "" , "", "#101080" },
100         { NODE_SPORT_CRICKET, "Cricket", "" , "", "#101080" },
101         { NODE_SPORT_SHOOTING, "Shooting", "Shooting range" , "", "#101080" },
102         { NODE_SPORT_PAINTBALL, "Paintball", "Run around and shoot people with paintballs" , "", "#101080" },
103         { NODE_SPORT_TABLE_TENNIS, "Table tennis", "" , "", "#101080" },
104         { NODE_SPORT_PELOTA, "Pelota", "" , "", "#101080" },
105         { NODE_SPORT_RACQUET, "Racquet", "" , "sports/racquetball", "#101080" },
106         { NODE_SPORT_BOWLS, "Lawn Bowls", "" , "", "#101080" },
107         { NODE_SPORT_ATHLETICS, "Athletics", "" , "", "#101080" },
108         { NODE_SPORT_OTHER, "Other Sports", "" , "", "#101077" },
109
110         { NODE_AMENITY_GENERIC, "Other", "Miscellaneous category for everything else." , "generic", "#002000" },
111         { NODE_POI_END, NULL, NULL }
112 };
113
114 static gboolean
115 poi_populate_categories(sqlite3 *db)
116 {
117 sqlite3_stmt *sql_cat;
118 gint i;
119
120 g_printf("Checking default categories\n");
121 sqlite3_prepare_v2(db,"insert or replace into category (cat_id, label, desc, enabled, priority, icon, color)"
122                         " values (?, ?, ?, 1, ?, ?, ?)", 
123                         -1, &sql_cat, NULL);
124
125 for (i=0; default_poi_categories[i].name; i++) {
126         sqlite3_bind_int(sql_cat, 1, default_poi_categories[i].type);
127         sqlite3_bind_text(sql_cat, 2, default_poi_categories[i].name, -1, SQLITE_TRANSIENT);
128         sqlite3_bind_text(sql_cat, 3, default_poi_categories[i].desc, -1, SQLITE_TRANSIENT);
129         sqlite3_bind_int(sql_cat, 4, default_poi_categories[i].type/100);
130         sqlite3_bind_text(sql_cat, 5, default_poi_categories[i].icon, -1, SQLITE_TRANSIENT);
131         sqlite3_bind_text(sql_cat, 6, default_poi_categories[i].color, -1, SQLITE_TRANSIENT);
132         if (sqlite3_step(sql_cat)==SQLITE_OK)
133                 g_printf("Failed to update category: %d [%s]\n", i, default_poi_categories[i].name);
134         sqlite3_reset(sql_cat);
135         sqlite3_clear_bindings(sql_cat);
136 }
137
138 sqlite3_finalize(sql_cat);
139
140 return TRUE;
141 }
142
143 gboolean
144 poi_db_create(sqlite3 *db)
145 {
146 gchar **pszResult;
147 guint nRow, nColumn;
148
149 if (SQLITE_OK != sqlite3_get_table(db, "select label from poi limit 1",
150                                    &pszResult, &nRow, &nColumn, NULL)) {
151         gchar *create_sql;
152
153         g_printf("Creating initial tables\n");
154
155         create_sql = sqlite3_mprintf
156             ("create table poi "
157                 "(poi_id integer PRIMARY KEY, "
158                 "lat real, "
159                 "lon real, "
160                 "elev real, "
161                 "label text, "
162                 "desc text, "
163                 "public integer, "
164                 "source integer default 0, "
165                 "osm_id integer default 0, "
166                 "priority integer default 2, "
167                 "cat_id integer);"
168              "create table category (cat_id integer PRIMARY KEY,"
169                 "label text, "
170                 "desc text, "
171                 "icon text, "
172                 "color char(7), "
173                 "priority integer default 2, "
174                 "enabled integer);");
175
176         if (SQLITE_OK != sqlite3_exec(db, create_sql, NULL, NULL, NULL)
177             && (SQLITE_OK != sqlite3_get_table(db, "select label from poi limit 1",
178                                   &pszResult, &nRow, &nColumn, NULL))) {
179                 g_printf("%s:\n%s",_("Failed to open or create database"),sqlite3_errmsg(db));
180                 sqlite3_close(db);
181                 db = NULL;
182                 return FALSE;
183         }
184 } else {
185         sqlite3_free_table(pszResult);
186 }
187
188 /* Make sure default categories exists */
189 poi_populate_categories(db);
190 return TRUE;
191 }
192
193 gboolean
194 poi_db_prepare(sqlite3 *db)
195 {
196         /* select from poi */
197         sqlite3_prepare_v2(db,
198                         "select p.lat, p.lon, p.poi_id, p.label, p.desc,"
199                         " p.cat_id, c.label, c.desc, c.icon, c.color"
200                         " from poi p, category c "
201                         " where p.lat between ? and ? "
202                         " and p.lon between ? and ? "
203                         " and c.enabled = 1 and p.cat_id = c.cat_id",
204                         -1, &poisql.select_poi, NULL);
205
206         /* select nearest pois */
207         sqlite3_prepare_v2(db,
208                         "select p.lat, p.lon, p.label, c.label, c.icon, c.color"
209                         " from poi p, category c "
210                         " where c.enabled = 1 and p.cat_id = c.cat_id "
211                         " and p.lat between $LAT-0.10 and $LAT+0.10 "
212                         " and p.lon between $LON-0.10 and $LAT+0.10 "
213                         " order by (($LAT - p.lat) * ($LAT - p.lat) "
214                         "+ ($LON - p.lon) * ($LON - p.lon)) limit 1",
215                         -1, &poisql.select_nearest_poi, NULL);
216
217         /* Search POI label */
218         sqlite3_prepare_v2(db,
219                         "select p.lat, p.lon, p.poi_id, p.label, p.desc,"
220                         " p.cat_id, c.label, c.desc, c.icon, c.color"
221                         " from poi p, category c "
222                         " where p.lat between ? and ? "
223                         " and p.lon between ? and ? "
224                         " and c.enabled = 1 and p.cat_id = c.cat_id and p.label like ?",
225                         -1, &poisql.select_poi_search, NULL);
226
227         /* Search POI label && category */
228         sqlite3_prepare_v2(db,
229                         "select p.lat, p.lon, p.poi_id, p.label, p.desc,"
230                         " p.cat_id, c.label, c.desc, c.icon, c.color"
231                         " from poi p, category c "
232                         " where p.lat between ? and ? "
233                         " and p.lon between ? and ? "
234                         " and c.enabled = 1 and p.cat_id = c.cat_id and p.label like ? and c.cat_id = ?",
235                         -1, &poisql.select_poi_search_cat, NULL);
236
237         /* insert poi */
238         sqlite3_prepare_v2(db,
239                         "insert into poi (lat, lon, label, desc, cat_id, public)"
240                         " values (?, ?, ?, ?, ?, 1)", -1, &poisql.insert_poi, NULL);
241         /* update poi */
242         sqlite3_prepare_v2(db, "update poi set label = ?, desc = ?, "
243                         "cat_id = ? where poi_id = ?",
244                         -1, &poisql.update_poi, NULL);
245         /* delete from poi */
246         sqlite3_prepare_v2(db, "delete from poi where poi_id = ?",
247                         -1, &poisql.delete_poi, NULL);
248         /* delete from poi by cat_id */
249         sqlite3_prepare_v2(db, "delete from poi where cat_id = ?",
250                         -1, &poisql.delete_poi_by_catid, NULL);
251         /* get next poilabel */
252         sqlite3_prepare_v2(db, "select ifnull(max(poi_id) + 1,1) from poi",
253                         -1, &poisql.nextlabel_poi, NULL);
254
255         /* select from category */
256         sqlite3_prepare_v2(db,
257                         "select c.label, c.desc, c.enabled"
258                         " from category c where c.cat_id = ?",
259                         -1, &poisql.select_cat, NULL);
260         /* insert into category */
261         sqlite3_prepare_v2(db,
262                         "insert into category (label, desc, enabled)"
263                         " values (?, ?, ?)", -1, &poisql.insert_cat, NULL);
264         /* update category */
265         sqlite3_prepare_v2(db,
266                         "update category set label = ?, desc = ?,"
267                         " enabled = ? where poi_id = ?",
268                         -1, &poisql.update_cat, NULL);
269         /* delete from category */
270         sqlite3_prepare_v2(db,"delete from category where cat_id = ?",
271                         -1, &poisql.delete_cat, NULL);
272         /* enable category */
273         sqlite3_prepare_v2(db,
274                         "update category set enabled = ?"
275                         " where cat_id = ?", -1, &poisql.toggle_cat, NULL);
276         /* select all category */
277         sqlite3_prepare_v2(db,
278                         "select c.cat_id, c.label, c.desc, c.enabled, c.icon, c.color,"
279                         " count(p.poi_id)"
280                         " from category c"
281                         " left outer join poi p on c.cat_id = p.cat_id"
282                         " group by c.cat_id, c.label, c.desc, c.enabled "
283                         " order by c.priority,c.label", -1, &poisql.selall_cat, NULL);
284
285         /* Select quick categories */
286         sqlite3_prepare_v2(db,
287                         "select c.cat_id, c.label, c.icon, c.color"
288                         " from category c where c.enabled=1 "
289                         " order by c.priority,c.label limit 9", -1, &poisql.select_quick_cat, NULL);
290
291         return TRUE;
292 }
293
294 gboolean
295 mapper_db_disconnect(sqlite3 **db)
296 {
297 if (db && *db) {
298         /* XXX: Handle the prepared statements */
299 #if 0
300         poi_db_unprepare(*db);
301         osm_db_unprepare(*db);
302 #endif  
303         sqlite3_close(*db);
304         return TRUE;
305 }
306 return FALSE;
307 }
308
309 gboolean
310 mapper_db_connect(sqlite3 **db, const gchar *data_db)
311 {
312 if (db_connect(db, data_db)) {
313         poi_db_create(*db);
314         if (poi_db_prepare(*db)==FALSE) {
315                 g_printerr("Failed to prepare POI SQL statements:");
316                 g_printf("SQLITE: %s\n", sqlite3_errmsg(*db));
317                 return FALSE;
318         }
319         if (osm_db_prepare(*db)==FALSE) {
320                 g_printerr("Failed to prepare OSM SQL statements:");
321                 g_printf("SQLITE: %s\n", sqlite3_errmsg(*db));
322                 return FALSE;
323         }
324         return TRUE;
325 }
326 return FALSE;
327 }
328
329 poi_info *
330 poi_new(void)
331 {
332 return g_slice_new0(poi_info);
333 }
334
335 void
336 poi_free(poi_info *p)
337 {
338 g_slice_free(poi_info, p);
339 }
340
341 /*************************************
342  * POI Category functions
343  *
344  */
345
346 poi_category *
347 poi_category_new(void)
348 {
349 return g_slice_new0(poi_category);
350 }
351
352 void
353 poi_category_free(poi_category *c)
354 {
355 if (c->label)
356         g_free(c->label);
357 if (c->desc)
358         g_free(c->desc);
359 g_slice_free(poi_category, c);
360 }
361
362 gboolean
363 poi_category_toggle(gint cat_id, gboolean cat_enabled) 
364 {
365 if (SQLITE_OK != sqlite3_bind_int(poisql.toggle_cat, 1, cat_enabled) ||
366     SQLITE_OK != sqlite3_bind_int(poisql.toggle_cat, 2, cat_id) ||
367     SQLITE_DONE != sqlite3_step(poisql.toggle_cat)) {
368                 return FALSE;
369         }
370 return TRUE;
371 }
372
373 gboolean
374 poi_category_get(gint cat_id, poi_category **c)
375 {
376 poi_category *cc;
377
378 if (SQLITE_OK != sqlite3_bind_int(poisql.select_cat, 1, cat_id)
379     || SQLITE_ROW != sqlite3_step(poisql.select_cat)) {
380         sqlite3_reset(poisql.select_cat);
381         return FALSE;
382 }
383
384 cc=poi_category_new();
385 cc->id=cat_id;
386 cc->label = g_strdup(sqlite3_column_text(poisql.select_cat, 0));
387 cc->desc = g_strdup(sqlite3_column_text(poisql.select_cat, 1));
388 cc->enabled = sqlite3_column_int(poisql.select_cat, 2);
389
390 sqlite3_reset(poisql.select_cat);
391 sqlite3_clear_bindings(poisql.select_cat);
392 *c=cc;
393 return TRUE;
394 }
395
396 gboolean
397 poi_category_update(gint cat_id, poi_category *c)
398 {
399 gboolean results=TRUE;
400
401 if (!_db)
402         return FALSE;
403
404 if (cat_id > 0) {
405 /* edit category */
406                 if (SQLITE_OK !=
407                     sqlite3_bind_text(poisql.update_cat, 1, c->label, -1, g_free)
408                     || SQLITE_OK != sqlite3_bind_text(poisql.update_cat, 2, c->desc, -1, g_free)
409                     || SQLITE_OK != sqlite3_bind_int(poisql.update_cat, 3, c->enabled)
410                     || SQLITE_OK != sqlite3_bind_int(poisql.update_cat, 4, c->id)
411                     || SQLITE_DONE != sqlite3_step(poisql.update_cat)) {
412                         results = FALSE;
413                 }
414                 sqlite3_reset(poisql.update_cat);
415                 sqlite3_clear_bindings(poisql.update_cat);
416         } else {
417                 /* add category */
418                 if (SQLITE_OK !=
419                     sqlite3_bind_text(poisql.insert_cat, 1, c->label, -1, g_free)
420                     || SQLITE_OK != sqlite3_bind_text(poisql.insert_cat, 2, c->desc, -1, g_free)
421                     || SQLITE_OK != sqlite3_bind_int(poisql.insert_cat, 3, c->enabled)
422                     || SQLITE_DONE != sqlite3_step(poisql.insert_cat)) {
423                         results = FALSE;
424                 }
425                 sqlite3_reset(poisql.insert_cat);
426                 sqlite3_clear_bindings(poisql.insert_cat);
427         }
428 return results;
429 }
430
431 gboolean 
432 poi_category_delete(delete_poi *dpoi)
433 {
434 if (!_db)
435         return FALSE;
436
437 if (SQLITE_OK != sqlite3_bind_int(poisql.delete_poi_by_catid, 1, dpoi->id)
438             || SQLITE_DONE != sqlite3_step(poisql.delete_poi_by_catid)) {
439                 sqlite3_reset(poisql.delete_poi_by_catid);
440                 return FALSE;
441 }
442 sqlite3_reset(poisql.delete_poi_by_catid);
443 sqlite3_clear_bindings(poisql.delete_poi_by_catid);
444
445 if (SQLITE_OK != sqlite3_bind_int(poisql.delete_cat, 1, dpoi->id) ||
446     SQLITE_DONE != sqlite3_step(poisql.delete_cat)) {
447         sqlite3_reset(poisql.delete_cat);
448         return FALSE;
449 }
450 sqlite3_reset(poisql.delete_cat);
451 sqlite3_clear_bindings(poisql.delete_cat);
452 return TRUE;
453 }
454
455
456 gboolean 
457 poi_delete(delete_poi *dpoi)
458 {
459 if (!_db)
460         return FALSE;
461
462 if (SQLITE_OK != sqlite3_bind_int(poisql.delete_poi, 1, dpoi->id) ||
463     SQLITE_DONE != sqlite3_step(poisql.delete_poi)) {
464         sqlite3_reset(poisql.delete_poi);
465         return FALSE;
466 } else {
467         sqlite3_reset(poisql.delete_poi);
468         sqlite3_clear_bindings(poisql.delete_poi);
469 }
470 return TRUE;
471 }
472
473 gboolean
474 poi_get_list(guint unitx, guint unity, GtkListStore **store, guint *_num_cats)
475 {
476 guint x, y;
477 gdouble lat1, lon1, lat2, lon2;
478 GtkTreeIter iter;
479 gchar tmp1[16], tmp2[16];
480 gint num_cats=0;
481
482 if (!_db)
483         return FALSE;
484
485 *store = gtk_list_store_new(POI_NUM_COLUMNS, G_TYPE_INT,        /* POI ID */
486                            G_TYPE_INT,  /* Category ID */
487                            G_TYPE_DOUBLE,       /* Latitude */
488                            G_TYPE_DOUBLE,       /* Longitude */
489                            G_TYPE_STRING,       /* Lat/Lon */
490                            G_TYPE_STRING,       /* POI Label */
491                            G_TYPE_STRING,       /* POI Desc. */
492                            G_TYPE_STRING);      /* Category Label */
493
494 x = unitx - pixel2unit(3 * _draw_width);
495 y = unity + pixel2unit(3 * _draw_width);
496 unit2latlon(x, y, lat1, lon1);
497
498 x = unitx + pixel2unit(3 * _draw_width);
499 y = unity - pixel2unit(3 * _draw_width);
500 unit2latlon(x, y, lat2, lon2); 
501
502 if (SQLITE_OK != sqlite3_bind_double(poisql.select_poi, 1, lat1) ||
503     SQLITE_OK != sqlite3_bind_double(poisql.select_poi, 2, lat2) ||
504     SQLITE_OK != sqlite3_bind_double(poisql.select_poi, 3, lon1) ||
505     SQLITE_OK != sqlite3_bind_double(poisql.select_poi, 4, lon2)) {
506         g_printerr("Failed to bind values for poisql.select_poi\n");
507         return FALSE;
508 }
509
510 while (SQLITE_ROW == sqlite3_step(poisql.select_poi)) {
511         gdouble lat, lon;
512         lat = sqlite3_column_double(poisql.select_poi, 0);
513         lon = sqlite3_column_double(poisql.select_poi, 1);
514         lat_format(lat, tmp1);
515         lon_format(lon, tmp2);
516
517         gtk_list_store_append(*store, &iter);
518         gtk_list_store_set(*store, &iter,
519                 POI_POIID, sqlite3_column_int(poisql.select_poi, 2),
520                 POI_CATID, sqlite3_column_int(poisql.select_poi, 5),
521                 POI_LAT, lat, POI_LON, lon, POI_LATLON,
522                 g_strdup_printf("%s, %s", tmp1, tmp2),
523                 POI_LABEL, sqlite3_column_text(poisql.select_poi, 3),
524                 POI_DESC, sqlite3_column_text(poisql.select_poi, 4),
525                 POI_CATLAB, sqlite3_column_text(poisql.select_poi, 6),
526                 -1);
527         num_cats++;
528 }
529 sqlite3_reset(poisql.select_poi);
530 sqlite3_clear_bindings(poisql.select_poi);
531 *_num_cats=num_cats;
532 return TRUE;
533 }
534
535 gboolean
536 poi_update(gint poi_id, gint cat_id, gchar *poi_label, gchar *poi_desc)
537 {
538 if (!_db)
539         return FALSE;
540
541 if (SQLITE_OK != sqlite3_bind_text(poisql.update_poi, 1, poi_label, -1, SQLITE_STATIC)
542    || SQLITE_OK != sqlite3_bind_text(poisql.update_poi, 2, poi_desc, -1, g_free)
543    || SQLITE_OK != sqlite3_bind_int(poisql.update_poi, 3, cat_id)
544    || SQLITE_OK != sqlite3_bind_int(poisql.update_poi, 4, poi_id)
545    || SQLITE_DONE != sqlite3_step(poisql.update_poi)) {
546                 return FALSE;
547         }
548 sqlite3_reset(poisql.update_poi);
549 sqlite3_clear_bindings(poisql.update_poi);
550 return TRUE;
551 }
552
553 gboolean
554 poi_add(gdouble lat, gdouble lon, gint cat_id, gchar *poi_label, gchar *poi_desc)
555 {
556 if (!_db)
557         return FALSE;
558
559 if (SQLITE_OK != sqlite3_bind_double(poisql.insert_poi, 1, lat)
560     || SQLITE_OK != sqlite3_bind_double(poisql.insert_poi, 2, lon)
561     || SQLITE_OK != sqlite3_bind_text(poisql.insert_poi, 3, poi_label, -1, g_free)
562     || SQLITE_OK != sqlite3_bind_text(poisql.insert_poi, 4, poi_desc, -1, g_free)
563     || SQLITE_OK != sqlite3_bind_int(poisql.insert_poi, 5, cat_id) 
564         || SQLITE_DONE != sqlite3_step(poisql.insert_poi)) {
565                 return FALSE;
566         }
567 sqlite3_reset(poisql.insert_poi);
568 sqlite3_clear_bindings(poisql.insert_poi);
569 return TRUE;
570 }
571
572 poi_info *
573 poi_find_nearest(gdouble lat, gdouble lon) 
574 {
575 poi_info *p;
576
577 if (!_db)
578         return FALSE;
579
580 sqlite3_reset(poisql.select_nearest_poi);
581 sqlite3_clear_bindings(poisql.select_nearest_poi);
582
583 if (SQLITE_OK == sqlite3_bind_double(poisql.select_nearest_poi, 1, lat)
584     && SQLITE_OK == sqlite3_bind_double(poisql.select_nearest_poi, 2, lon)
585     && SQLITE_ROW == sqlite3_step(poisql.select_nearest_poi)) {
586
587         p=poi_new();
588         p->lat = sqlite3_column_double(poisql.select_nearest_poi, 0);
589         p->lon = sqlite3_column_double(poisql.select_nearest_poi, 1);
590         p->label=g_strdup(sqlite3_column_text(poisql.select_nearest_poi, 2));
591         p->cat_label=g_strdup(sqlite3_column_text(poisql.select_nearest_poi, 3));
592         return p;
593 }
594 return NULL;
595 }
596
597 GtkListStore *
598 poi_category_generate_store()
599 {
600 GtkTreeIter iter;
601 GtkListStore *store;
602
603 if (!_db)
604         return NULL;
605
606 store = gtk_list_store_new(CAT_NUM_COLUMNS,
607                            G_TYPE_UINT,
608                            G_TYPE_BOOLEAN,
609                            G_TYPE_STRING, G_TYPE_STRING, G_TYPE_UINT);
610
611 while (SQLITE_ROW == sqlite3_step(poisql.selall_cat)) {
612         gtk_list_store_append(store, &iter);
613         gtk_list_store_set(store, &iter,
614                         CAT_ID, sqlite3_column_int(poisql.selall_cat, 0), 
615                         CAT_ENABLED, sqlite3_column_int(poisql.selall_cat, 3),
616                         CAT_LABEL, sqlite3_column_text(poisql.selall_cat, 1),
617                         CAT_DESC, sqlite3_column_text(poisql.selall_cat, 2),
618                         CAT_POI_CNT, sqlite3_column_int(poisql.selall_cat, 6), -1);
619 }
620
621 sqlite3_reset(poisql.selall_cat);
622 sqlite3_clear_bindings(poisql.selall_cat);
623
624 return store;
625 }