]> err.no Git - mapper/blob - src/poi.c
f87d7763c838dd251830b0ccc15895bd640f47d3
[mapper] / src / poi.c
1 #include <config.h>
2
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <strings.h>
7 #include <stddef.h>
8 #include <locale.h>
9 #include <math.h>
10 #include <errno.h>
11 #include <glib/gstdio.h>
12 #include <gtk/gtk.h>
13 #include <fcntl.h>
14 #include <gconf/gconf-client.h>
15 #include <libxml/parser.h>
16 #include <libintl.h>
17 #include <locale.h>
18 #include <sqlite3.h>
19
20 #include "utils.h"
21 #include "poi.h"
22 #include "gps.h"
23 #include "map.h"
24 #include "mapper-types.h"
25 #include "settings.h"
26 #include "db.h"
27 #include "osm.h"
28 #include "osm-db.h"
29 #include "latlon.h"
30
31 static sqlite3 *poidb;
32
33 /* POI Icon theme. "classic" or "square". Should be made into a configuration option */
34 static gchar *theme="square";
35
36 /* Hash table for caching POI icons */
37 static GHashTable *poi_icon_hash = NULL;
38
39 struct _poi_categories {
40         node_type_t type;
41         const gchar *name, *desc, *icon, *color;
42 };
43
44 /* The default POI categories */
45 static struct _poi_categories default_poi_categories[] = {
46         { NODE_AMENITY_PARKING, "Parking", "Parking place for vehicles."  , "vehicle/parking", "#2020ff" },
47         { NODE_AMENITY_FUEL, "Fuel", "Stations for purchasing fuel for vehicles."  , "vehicle/fuel_station", "#4040f0" },
48         { NODE_AMENITY_SPEEDCAM, "Speed Cam", "Automatic speed cameras."  , "vehicle/restrictions/speed_trap", "#ff0000" },
49         { NODE_AMENITY_HOSPITAL, "Hospital", "Get medical help"  , "health/hospital", "#ff4040" },
50         { NODE_AMENITY_PHARMACY, "Pharmacy", "Place to get drugs."  , "health/pharmacy", "#40f040" },
51         { NODE_AMENITY_POLICE, "Police", "Police station" , "public/police", "#8570ff" },
52         { NODE_TOURISM_HOTEL, "Hotel", "Places to stay temporarily or for the night." , "accommodation/hotel", "#ba20ba" },
53         { NODE_TOURISM_HOSTEL, "Hostel", "Places to stay temporarily or for the night. Cheap." , "accommodation/hostel", "#ba30ba" },
54         { NODE_TOURISM_MOTEL, "Motel", "Places to stay temporarily or for the night. Cheap." , "accommodation/motel", "#ba40ba" },
55         { NODE_AMENITY_ATM, "ATM", "Automatic Teller Machine/Cashpoint." , "money/atm", "#40a040" },
56         { NODE_AMENITY_BANK, "Bank", "Place to take care of your money." , "money/bank", "#50b550" },
57         { NODE_AMENITY_POST, "Post office", "Place to handle mail." , "public/post_office", "#ff6868" },
58         { NODE_AMENITY_POST_BOX, "Post box", "Send letters." , "public/post_box", "#ff6060" },
59         { NODE_TOURISM_INFO, "Tourism info", "A place for tourists to get information." , "misc/information", "#4af04a" },
60         { NODE_AMENITY_TAXI, "Taxi station", "Get a Taxi here." , "transport/taxi", "#50ffff" },
61         { NODE_RAILWAY_STATION, "Railway station", "Transportation by train." , "transport/railway", "#fa7070" },
62         { NODE_RAILWAY_HALT, "Railway halt", "" , "transport/railway_small", "#f27777" },
63         { NODE_AMENITY_BUS_STATION, "Bus station", "Transportation by bus." , "transport/bus", "#f07070" },
64         { NODE_AMENITY_BOAT, "Harbour", "Transportation by boat." , "transport/ferry", "#9090ee" },
65         { NODE_AIRPORT_TERMINAL, "Airport", "Transportation by air." , "transport/airport", "#909099" },
66         { NODE_TOURISM_CAMP_SITE, "Camp site", "Place to go camping" , "accommodation/camping", "#90f080" },
67         { NODE_TOURISM_CARAVAN_SITE, "Caravan site", "" , "accommodation/camping/caravan", "#90ff80" },
68         { NODE_TOURISM_PICNIC_SITE, "Picnic", "Place to have a Picnic" , "recreation/picnic", "#a5f8e5" },
69         { NODE_AMENITY_FOOD, "Restaurant, Fast food", "Places to eat or drink." , "food/fastfood", "#e5960d" },
70         { NODE_AMENITY_PUB, "Pub", "Place to drink." , "food/pub", "#f5960d" },
71         { NODE_AMENITY_NIGHTCLUB, "Disco, Club", "Place to drink, party and dance." , "food/bar", "#f59c1d" },
72         { NODE_AMENITY_CAFE, "Cafe", "Place to drink coffe or tee and eat." , "food/cafe", "#ff960d" },
73         { NODE_AMENITY_CINEMA, "Cinema", "Place to see movies" , "recreation/cinema", "#9090a0" },
74         { NODE_AMENITY_THEATRE, "Theatre", "Place to see people performing" , "recreation/theater", "#9595a5" },
75         { NODE_AMENITY_SHOP, "Shopping", "Places to shop or acquire services." , "shopping", "#61ef1b" },
76         { NODE_AMENITY_POW, "Place of Worchip", "" , "religion/church", "#7c5b0b" },
77         { NODE_TOURISM_THEME_PARK, "Theme Park", "Place to have fun and ride for example rollercoasters." , "recreation/theme_park", "#8eef1b" },
78         { NODE_AMENITY_COLLEGE, "College Campus/Building", "" , "education/college", "#813fdc" },
79         { NODE_AMENITY_SCHOOL, "School", "" , "education/school", "#813fdc" },
80         { NODE_AMENITY_LIBRARY, "Library", "Place to read and borrow books and magazines" , "shopping/rental/library", "#803090" },
81         { NODE_AMENITY_TOWNHALL, "Townhall", "" , "", "#408090" },
82         { NODE_AMENITY_WC, "WC/Toilets", "" , "public/toilets", "#e1d62c" },
83         { NODE_AMENITY_TELEPHONE, "Telephone", "Public telephone" , "public/telephone", "#208060" },
84         { NODE_TOURISM_ATTRACTION, "Attraction", "Something interesting" , "generic", "#005000" },
85
86         { NODE_HISTORIC_MUSEUM, "Museum", "A place where objects of historical, artistic, or scientific interest are exhibited, preserved or studied." , "sightseeing/museum", "#202020" },
87         { NODE_HISTORIC_CASTLE, "Castle", "Historical building or group of building used for defense by military forces, whose main structures are walls and towers." , "sightseeing/castle", "#404040" },
88
89         { NODE_SPORT_CENTER, "Sport Center", "" , "sports/centre", "#101080" },
90         { NODE_SPORT_STADIUM, "Sport Stadium", "" , "sports/stadium", "#101080" },
91         { NODE_SPORT_SKIING, "Skiing", "" , "sports/skiing", "#5050A0" },
92         { NODE_SPORT_SWIMMING, "Swimming", "" , "sports/swimming", "#102080" },
93         { NODE_SPORT_FOOTBALL, "Football", "" , "sports/football", "#102080" },
94         { NODE_SPORT_SOCCER, "Soccer", "" , "sports/soccer", "#102080" },
95         { NODE_SPORT_GOLF, "Golf", "" , "sports/golf", "#102080" },
96         { NODE_SPORT_TENNIS, "Tennis", "" , "sports/tennis", "#101080" },
97         { NODE_SPORT_BOWLING, "Bowling", "" , "", "#101080" },
98         { NODE_SPORT_RUGBY, "Rugby", "" , "", "#101080" },
99         { NODE_SPORT_CLIMBING, "Climbing", "" , "", "#101080" },
100         { NODE_SPORT_CYCLING, "Cycling", "" , "sports/bicycle", "#101080" },
101         { NODE_SPORT_MOTOR, "Motor sport", "" , "", "#101080" },
102         { NODE_SPORT_HOCKEY, "Hockey", "" , "", "#5050A0" },
103         { NODE_SPORT_SKATING, "Skating", "" , "", "#5050A0" },
104         { NODE_SPORT_SKATEBOARD, "Skateboard", "" , "", "#101080" },
105         { NODE_SPORT_HORSES, "Horses", "Horse riding or racing" , "sports/riding", "#101080" },
106         { NODE_SPORT_DOG, "Dog racing", "" , "", "#101080" },
107         { NODE_SPORT_BASKETBALL, "Basketball", "" , "", "#101080" },
108         { NODE_SPORT_BASEBALL, "Baseball", "" , "", "#101080" },
109         { NODE_SPORT_CANOE, "Canoe", "" , "", "#101080" },
110         { NODE_SPORT_CROQUET, "Croquet", "" , "", "#101080" },
111         { NODE_SPORT_CRICKET, "Cricket", "" , "", "#101080" },
112         { NODE_SPORT_SHOOTING, "Shooting", "Shooting range" , "", "#101080" },
113         { NODE_SPORT_PAINTBALL, "Paintball", "Run around and shoot people with paintballs" , "", "#101080" },
114         { NODE_SPORT_TABLE_TENNIS, "Table tennis", "" , "", "#101080" },
115         { NODE_SPORT_PELOTA, "Pelota", "" , "", "#101080" },
116         { NODE_SPORT_RACQUET, "Racquet", "" , "sports/racquetball", "#101080" },
117         { NODE_SPORT_BOWLS, "Lawn Bowls", "" , "", "#101080" },
118         { NODE_SPORT_ATHLETICS, "Athletics", "" , "", "#101080" },
119         { NODE_SPORT_OTHER, "Other Sports", "" , "sports/stadium", "#101077" },
120
121         { NODE_AMENITY_GENERIC, "Other", "Miscellaneous category for everything else." , "generic", "#002000" },
122         { NODE_POI_END, NULL, NULL }
123 };
124
125 static gboolean
126 poi_populate_categories(sqlite3 *db)
127 {
128 sqlite3_stmt *sql_cat;
129 gint i;
130
131 sqlite3_prepare_v2(db,"insert or replace into category (cat_id, label, desc, enabled, priority, icon, color)"
132                         " values (?, ?, ?, 1, ?, ?, ?)", 
133                         -1, &sql_cat, NULL);
134
135 for (i=0; default_poi_categories[i].name; i++) {
136         sqlite3_bind_int(sql_cat, 1, default_poi_categories[i].type);
137         sqlite3_bind_text(sql_cat, 2, default_poi_categories[i].name, -1, SQLITE_STATIC);
138         sqlite3_bind_text(sql_cat, 3, default_poi_categories[i].desc, -1, SQLITE_STATIC);
139         sqlite3_bind_int(sql_cat, 4, default_poi_categories[i].type/100);
140         sqlite3_bind_text(sql_cat, 5, default_poi_categories[i].icon, -1, SQLITE_STATIC);
141         sqlite3_bind_text(sql_cat, 6, default_poi_categories[i].color, -1, SQLITE_STATIC);
142         if (sqlite3_step(sql_cat)==SQLITE_OK)
143                 g_printf("Failed to update category: %d [%s]\n", i, default_poi_categories[i].name);
144         sqlite3_reset(sql_cat);
145         sqlite3_clear_bindings(sql_cat);
146 }
147
148 sqlite3_finalize(sql_cat);
149
150 return TRUE;
151 }
152
153 gboolean
154 poi_db_create(sqlite3 *db)
155 {
156 gchar **pszResult;
157 guint nRow, nColumn;
158
159 if (SQLITE_OK != sqlite3_get_table(db, "select label from poi limit 1",
160                                    &pszResult, &nRow, &nColumn, NULL)) {
161         gchar *create_sql;
162
163         g_printerr("Creating initial POI tables\n");
164
165         create_sql = sqlite3_mprintf
166             ("create table poi "
167                 "(poi_id INTEGER PRIMARY KEY AUTOINCREMENT, "
168                 "lat real, "
169                 "lon real, "
170                 "elev real, "
171                 "label text, "
172                 "desc text, "
173                 "url text, "
174                 "postal_code text,"
175                 "public int default 1, "
176                 "source int default 0, "
177                 "cat_id int default 900,"
178                 "osm_id int default 0, "
179                 "isin_c int default 0, "
180                 "isin_p int default 0, "
181                 "priority int default 2, "
182                 "addtime int);"
183              "create table category (cat_id INTEGER PRIMARY KEY AUTOINCREMENT,"
184                 "label text, "
185                 "desc text, "
186                 "icon text, "
187                 "color char(7), "
188                 "priority int default 2, "
189                 "enabled int);");
190
191         if (SQLITE_OK != sqlite3_exec(db, create_sql, NULL, NULL, NULL)
192             && (SQLITE_OK != sqlite3_get_table(db, "select label from poi limit 1",
193                                   &pszResult, &nRow, &nColumn, NULL))) {
194                 g_printf("%s:\n%s",_("Failed to open or create database"),sqlite3_errmsg(db));
195                 sqlite3_close(db);
196                 db = NULL;
197                 return FALSE;
198         }
199 } else {
200         sqlite3_free_table(pszResult);
201 }
202
203 /* Make sure default categories exists */
204 poi_populate_categories(db);
205 return TRUE;
206 }
207
208 typedef enum {
209         PS_LAT=0,
210         PS_LON,
211         PS_ID,
212         PS_LABEL,
213         PS_DESC,
214         PS_CAT_ID,
215         PS_CAT_LABEL,
216         PS_CAT_DESC,
217         PS_CAT_ICON,
218         PS_CAT_COLOR,
219         PS_SOURCE,
220         PS_PUBLIC,
221         PS_URL,
222         PS_POSTAL_CODE,
223 } poi_sql_column;
224
225 #define POI_BASE_SQL_FIELDS "p.lat, p.lon, p.poi_id, p.label, p.desc, p.cat_id, c.label, c.desc, c.icon, c.color, p.source, p.public, p.url, p.postal_code"
226 #define POI_MINI_SQL_FIELDS "p.lat, p.lon, p.poi_id, p.label, p.desc, p.cat_id, c.label, c.desc"
227
228 gboolean
229 poi_db_prepare(sqlite3 *db)
230 {
231         /* Select POIs inside given minmax lat,lon */
232         if (sqlite3_prepare_v2(db,
233                         "select " 
234                         POI_BASE_SQL_FIELDS
235                         " from poi p, category c "
236                         " where p.lat between ? and ? "
237                         " and p.lon between ? and ? "
238                         " and c.enabled=1 and p.cat_id=c.cat_id order by c.priority limit 500",
239                         -1, &poisql.select_poi, NULL)!=SQLITE_OK)
240         return FALSE;
241
242         /* Get POI with given ID */
243         if (sqlite3_prepare_v2(db,
244                         "select "
245                         POI_BASE_SQL_FIELDS
246                         " from poi p, category c "
247                         " where p.poi_id = ? "
248                         " and p.cat_id=c.cat_id",
249                         -1, &poisql.select_poi_by_id, NULL)!=SQLITE_OK)
250         return FALSE;
251
252         /* Search POIs by label */
253         if (sqlite3_prepare_v2(db,
254                         "select "
255                         POI_BASE_SQL_FIELDS
256                         " from poi p, category c "
257                         " where p.lat between ? and ? "
258                         " and p.lon between ? and ? "
259                         " and c.enabled=1 and p.cat_id = c.cat_id and (p.label like $NAME or p.postal_code like $NAME) order by p.label, c.label",
260                         -1, &poisql.select_poi_search, NULL)!=SQLITE_OK)
261         return FALSE;
262
263         /* Search POI by label and specific category */
264         if (sqlite3_prepare_v2(db,
265                         "select "
266                         POI_BASE_SQL_FIELDS
267                         " from poi p, category c "
268                         " where p.lat between ? and ? "
269                         " and p.lon between ? and ? "
270                         " and c.enabled=1 and p.cat_id = c.cat_id and (p.label like $NAME or p.postal_code like $NAME) and c.cat_id = ? order by p.label",
271                         -1, &poisql.select_poi_search_cat, NULL)!=SQLITE_OK)
272         return FALSE;
273
274         /* Search specific category */
275         if (sqlite3_prepare_v2(db,
276                         "select "
277                         POI_BASE_SQL_FIELDS
278                         " from poi p, category c "
279                         " where p.lat between ? and ? "
280                         " and p.lon between ? and ? "
281                         " and c.enabled=1 and p.cat_id=c.cat_id and c.cat_id=? order by p.label",
282                         -1, &poisql.select_poi_by_cat, NULL)!=SQLITE_OK)
283         return FALSE;
284
285         /* Select nearest POI */
286         if (sqlite3_prepare_v2(db,
287                         "select "
288                         POI_MINI_SQL_FIELDS
289                         " from poi p, category c "
290                         " where c.enabled = 1 and p.cat_id = c.cat_id "
291                         " and p.lat between $LAT-0.10 and $LAT+0.10 "
292                         " and p.lon between $LON-0.10 and $LAT+0.10 "
293                         " order by (($LAT - p.lat) * ($LAT - p.lat) "
294                         "+ ($LON - p.lon) * ($LON - p.lon)) limit 1",
295                         -1, &poisql.select_nearest_poi, NULL)!=SQLITE_OK)
296         return FALSE;
297
298         /* Insert POI */
299         sqlite3_prepare_v2(db,  "insert into poi (lat, lon, label, desc, url, postal_code, cat_id, addtime, public, source)"
300                                                         " values (?, ?, ?, ?, ?, ?, ?, ?, 1, ?)", -1, &poisql.insert_poi, NULL);
301         /* update poi */
302         sqlite3_prepare_v2(db, "update poi set label=?, desc=?, cat_id=? where poi_id=?", -1, &poisql.update_poi, NULL);
303         /* delete from poi */
304         sqlite3_prepare_v2(db, "delete from poi where poi_id=?", -1, &poisql.delete_poi, NULL);
305         /* delete from poi by cat_id */
306         sqlite3_prepare_v2(db, "delete from poi where cat_id=?", -1, &poisql.delete_poi_by_catid, NULL);
307
308         /* select from category */
309         sqlite3_prepare_v2(db,
310                         "select c.label, c.desc, c.enabled"
311                         " from category c where c.cat_id = ?",
312                         -1, &poisql.select_cat, NULL);
313         /* insert into category */
314         sqlite3_prepare_v2(db,
315                         "insert into category (label, desc, enabled)"
316                         " values (?, ?, ?)", -1, &poisql.insert_cat, NULL);
317         /* update category */
318         sqlite3_prepare_v2(db,
319                         "update category set label = ?, desc = ?,"
320                         " enabled = ? where poi_id = ?",
321                         -1, &poisql.update_cat, NULL);
322         /* delete from category */
323         sqlite3_prepare_v2(db,"delete from category where cat_id = ?",
324                         -1, &poisql.delete_cat, NULL);
325         /* enable category */
326         sqlite3_prepare_v2(db,
327                         "update category set enabled = ?"
328                         " where cat_id = ?", -1, &poisql.toggle_cat, NULL);
329         /* select all category */
330         sqlite3_prepare_v2(db,
331                         "select c.cat_id, c.label, c.desc, c.enabled, c.icon, c.color,"
332                         " count(p.poi_id)"
333                         " from category c"
334                         " left outer join poi p on c.cat_id = p.cat_id"
335                         " group by c.cat_id, c.label, c.desc, c.enabled "
336                         " order by c.priority,c.label", -1, &poisql.selall_cat, NULL);
337
338         /* Select quick categories */
339         sqlite3_prepare_v2(db,
340                         "select c.cat_id, c.label, c.icon, c.color"
341                         " from category c where c.enabled=1 "
342                         " order by c.priority,c.label limit 9", -1, &poisql.select_quick_cat, NULL);
343
344         return TRUE;
345 }
346
347 void
348 poi_icon_hash_clear(void)
349 {
350 #if (GLIB_CHECK_VERSION (2, 12, 0))
351 g_hash_table_remove_all(poi_icon_hash);
352 #else
353 g_hash_table_foreach_remove(poi_icon_hash, gtk_true, NULL);
354 #endif
355 }
356
357 void
358 poi_deinit(sqlite3 *db)
359 {
360 if (poi_icon_hash) {
361         g_hash_table_destroy(poi_icon_hash);
362         poi_icon_hash=NULL;
363 }
364
365 sqlite3_finalize(poisql.select_quick_cat);
366 sqlite3_finalize(poisql.selall_cat);
367 sqlite3_finalize(poisql.toggle_cat);
368 sqlite3_finalize(poisql.delete_cat);
369 sqlite3_finalize(poisql.update_cat);
370 sqlite3_finalize(poisql.insert_cat);
371 sqlite3_finalize(poisql.select_cat);
372 sqlite3_finalize(poisql.insert_poi);
373 sqlite3_finalize(poisql.update_poi);
374 sqlite3_finalize(poisql.delete_poi);
375 sqlite3_finalize(poisql.delete_poi_by_catid);
376 sqlite3_finalize(poisql.select_nearest_poi);
377 sqlite3_finalize(poisql.select_poi);
378 sqlite3_finalize(poisql.select_poi_search);
379 sqlite3_finalize(poisql.select_poi_search_cat);
380 }
381
382 gboolean
383 poi_init(sqlite3 **db)
384 {
385 if (!poi_icon_hash)
386         poi_icon_hash=g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
387
388 if (!db || !*db)
389         return FALSE;
390
391 poidb=*db;
392 poi_db_create(poidb);
393 if (poi_db_prepare(poidb)==FALSE) {
394         g_printerr("Failed to prepare POI SQL statements:");
395         g_printf("SQLITE: %s\n", sqlite3_errmsg(poidb));
396         return FALSE;
397 }
398 return TRUE;
399 }
400
401 poi_info *
402 poi_new(void)
403 {
404 poi_info *p;
405
406 p=g_slice_new0(poi_info);
407 p->source=POI_SOURCE_USER;
408 /* XXX: Set defaults ? */
409 return p;
410 }
411
412 void
413 poi_free(poi_info *p)
414 {
415 if (p->label)
416         g_free(p->label);
417 if (p->desc)
418         g_free(p->desc);
419 if (p->url)
420         g_free(p->url);
421 if (p->postal_code)
422         g_free(p->postal_code);
423 if (p->cat_label)
424         g_free(p->cat_label);
425 if (p->cat_desc)
426         g_free(p->cat_desc);
427 g_slice_free(poi_info, p);
428 }
429
430 static GtkListStore *
431 poi_list_store_new(void) {
432 return gtk_list_store_new(ITEM_NUM_COLUMNS, 
433                         G_TYPE_INT,     /* POI ID */
434                         G_TYPE_INT,     /* Category ID */
435                         G_TYPE_DOUBLE,  /* Latitude */
436                         G_TYPE_DOUBLE,  /* Longitude */
437                         G_TYPE_DOUBLE,  /* Dist */
438                         G_TYPE_STRING,  /* Lat/Lon */
439                         G_TYPE_STRING,  /* Label */
440                         G_TYPE_STRING,  /* Desc. */
441                         G_TYPE_STRING,  /* Category Label */
442                         G_TYPE_STRING,  /* Icon */
443                         G_TYPE_STRING); /* Color */
444 }
445
446 /*************************************
447  * POI Category functions
448  *
449  */
450
451 poi_category *
452 poi_category_new(void)
453 {
454 return g_slice_new0(poi_category);
455 }
456
457 void
458 poi_category_free(poi_category *c)
459 {
460 if (c->label)
461         g_free(c->label);
462 if (c->desc)
463         g_free(c->desc);
464 g_slice_free(poi_category, c);
465 }
466
467 gboolean
468 poi_category_toggle(guint cat_id, gboolean cat_enabled) 
469 {
470 if (SQLITE_OK != sqlite3_bind_int(poisql.toggle_cat, 1, cat_enabled) ||
471     SQLITE_OK != sqlite3_bind_int(poisql.toggle_cat, 2, cat_id) ||
472     SQLITE_DONE != sqlite3_step(poisql.toggle_cat)) {
473                 return FALSE;
474         }
475 return TRUE;
476 }
477
478 gboolean
479 poi_category_get(guint cat_id, poi_category **c)
480 {
481 poi_category *cc;
482
483 if (SQLITE_OK != sqlite3_bind_int(poisql.select_cat, 1, cat_id) || SQLITE_ROW != sqlite3_step(poisql.select_cat)) {
484         sqlite3_reset(poisql.select_cat);
485         return FALSE;
486 }
487
488 cc=poi_category_new();
489 cc->id=cat_id;
490 cc->label = g_strdup(sqlite3_column_text(poisql.select_cat, 0));
491 cc->desc = g_strdup(sqlite3_column_text(poisql.select_cat, 1));
492 cc->enabled = sqlite3_column_int(poisql.select_cat, 2);
493
494 sqlite3_reset(poisql.select_cat);
495 sqlite3_clear_bindings(poisql.select_cat);
496 *c=cc;
497 return TRUE;
498 }
499
500 gboolean
501 poi_category_update(guint cat_id, poi_category *c)
502 {
503 gboolean results=TRUE;
504
505 if (!_db)
506         return FALSE;
507
508 if (cat_id > 0) {
509 /* edit category */
510                 if (SQLITE_OK != sqlite3_bind_text(poisql.update_cat, 1, c->label, -1, SQLITE_STATIC)
511                     || SQLITE_OK != sqlite3_bind_text(poisql.update_cat, 2, c->desc, -1, SQLITE_STATIC)
512                     || SQLITE_OK != sqlite3_bind_int(poisql.update_cat, 3, c->enabled)
513                     || SQLITE_OK != sqlite3_bind_int(poisql.update_cat, 4, c->id)
514                     || SQLITE_DONE != sqlite3_step(poisql.update_cat)) {
515                         results = FALSE;
516                 }
517                 sqlite3_reset(poisql.update_cat);
518                 sqlite3_clear_bindings(poisql.update_cat);
519         } else {
520                 /* add category */
521                 if (SQLITE_OK != sqlite3_bind_text(poisql.insert_cat, 1, c->label, -1, SQLITE_STATIC)
522                     || SQLITE_OK != sqlite3_bind_text(poisql.insert_cat, 2, c->desc, -1, SQLITE_STATIC)
523                     || SQLITE_OK != sqlite3_bind_int(poisql.insert_cat, 3, c->enabled)
524                     || SQLITE_DONE != sqlite3_step(poisql.insert_cat)) {
525                         results = FALSE;
526                 }
527                 sqlite3_reset(poisql.insert_cat);
528                 sqlite3_clear_bindings(poisql.insert_cat);
529         }
530 return results;
531 }
532
533 gboolean 
534 poi_category_delete(guint id)
535 {
536 if (!poidb)
537         return FALSE;
538
539 if (SQLITE_OK != sqlite3_bind_int(poisql.delete_poi_by_catid, 1, id) || SQLITE_DONE != sqlite3_step(poisql.delete_poi_by_catid)) {
540         sqlite3_reset(poisql.delete_poi_by_catid);
541         return FALSE;
542 }
543 sqlite3_reset(poisql.delete_poi_by_catid);
544 sqlite3_clear_bindings(poisql.delete_poi_by_catid);
545
546 if (SQLITE_OK != sqlite3_bind_int(poisql.delete_cat, 1, id) || SQLITE_DONE != sqlite3_step(poisql.delete_cat)) {
547         sqlite3_reset(poisql.delete_cat);
548         return FALSE;
549 }
550 sqlite3_reset(poisql.delete_cat);
551 sqlite3_clear_bindings(poisql.delete_cat);
552 return TRUE;
553 }
554
555
556 gboolean 
557 poi_delete(guint id)
558 {
559 if (!poidb)
560         return FALSE;
561
562 if (SQLITE_OK != sqlite3_bind_int(poisql.delete_poi, 1, id) || SQLITE_DONE != sqlite3_step(poisql.delete_poi)) {
563         sqlite3_reset(poisql.delete_poi);
564         return FALSE;
565 }
566 sqlite3_reset(poisql.delete_poi);
567 sqlite3_clear_bindings(poisql.delete_poi);
568 return TRUE;
569 }
570
571 gboolean
572 poi_search(poi_search_type pst, gdouble lat, gdouble lon, gchar *text, guint cat, GtkListStore **store)
573 {
574 GtkTreeIter iter;
575 sqlite3_stmt *sql=NULL;
576 gchar *ltext=NULL;
577 guint rows=0;
578 gchar tmp1[16], tmp2[16];
579 guint range=1;
580
581 if (!poidb)
582         return FALSE;
583
584 g_printf("POI Search: [%s] around %.6f %.6f (%d %d)\n", text, lat, lon, cat, pst);
585
586 switch (pst) {
587         case POI_SEARCH_NEAR:
588                 if (SQLITE_OK != sqlite3_bind_double(poisql.select_poi, 1, lat-0.5) ||
589                     SQLITE_OK != sqlite3_bind_double(poisql.select_poi, 4, lon+0.5)) {
590                                 g_printerr("Failed to bind values for poisql.select_poi\n");
591                                 sqlite3_clear_bindings(poisql.select_poi);
592                                 return FALSE;
593                 }
594                 sql=poisql.select_poi;
595         break;
596         case POI_SEARCH_TEXT:
597                 ltext=g_strdup_printf("%s%%", text);
598                 
599                 if (SQLITE_OK != sqlite3_bind_text(poisql.select_poi_search,   5, ltext, -1, SQLITE_TRANSIENT)) {
600                                 g_printerr("Failed to bind values for poisql.select_poi_search\n");
601                                 sqlite3_clear_bindings(poisql.select_poi_search);
602                                 g_free(ltext);
603                                 return FALSE;
604                 }
605                 g_free(ltext);
606                 sql=poisql.select_poi_search;
607         break;
608         case POI_SEARCH_TEXT_CAT:
609                 ltext=g_strdup_printf("%s%%", text);
610
611                 if (SQLITE_OK != sqlite3_bind_int(poisql.select_poi_search_cat, 6, cat) ||
612                         SQLITE_OK != sqlite3_bind_text(poisql.select_poi_search_cat, 5, ltext, -1, SQLITE_TRANSIENT)) {
613                                 g_printerr("Failed to bind values for poisql.select_poi_search_cat\n");
614                                 sqlite3_clear_bindings(poisql.select_poi_search_cat);
615                                 g_free(ltext);
616                                 return FALSE;
617                 }
618                 g_free(ltext);
619                 sql=poisql.select_poi_search_cat;
620         break;
621         case POI_SEARCH_CAT:
622                 if (SQLITE_OK != sqlite3_bind_int(poisql.select_poi_by_cat, 5, cat)) {
623                                 g_printerr("Failed to bind values for poisql.select_poi_by_cat\n");
624                                 sqlite3_clear_bindings(poisql.select_poi_by_cat);
625                                 return FALSE;
626                 }
627                 sql=poisql.select_poi_by_cat;
628         break;
629         default:
630                 g_assert_not_reached();
631                 return FALSE;
632         break;
633 }
634
635 /* XXX: Use common bind for common variables */
636 if (SQLITE_OK != sqlite3_bind_double(sql, 1, lat-range) ||
637     SQLITE_OK != sqlite3_bind_double(sql, 2, lat+range) ||
638     SQLITE_OK != sqlite3_bind_double(sql, 3, lon-range) ||
639     SQLITE_OK != sqlite3_bind_double(sql, 4, lon+range)) {
640         g_printerr("Failed to bind common variables for POI search\n");
641         sqlite3_clear_bindings(sql);
642         return FALSE;
643 }
644
645 *store = poi_list_store_new();
646
647 while (SQLITE_ROW == sqlite3_step(sql)) {
648         gdouble rlat, rlon, dist;
649
650         rlat=sqlite3_column_double(sql, 0);
651         rlon=sqlite3_column_double(sql, 1);
652         lat_format(_degformat, rlat, tmp1);
653         lon_format(_degformat, rlon, tmp2);
654         dist=calculate_distance(lat, lon, rlat, rlon) * UNITS_CONVERT[_units];
655
656         gtk_list_store_append(*store, &iter);
657         gtk_list_store_set(*store, &iter,
658                 ITEM_ID, sqlite3_column_int(sql, 2),
659                 ITEM_CATID, sqlite3_column_int(sql, 5),
660                 ITEM_LAT, rlat, 
661                 ITEM_LON, rlon, 
662                 ITEM_DIST, dist, 
663                 ITEM_LATLON, g_strdup_printf("%s, %s", tmp1, tmp2),
664                 ITEM_LABEL, sqlite3_column_text(sql, 3),
665                 ITEM_DESC, sqlite3_column_text(sql, 4),
666                 ITEM_CATLAB, sqlite3_column_text(sql, 6),
667                 -1);
668         rows++;
669 }
670
671 g_printf("Found: %d items\n", rows);
672
673 sqlite3_reset(sql);
674 sqlite3_clear_bindings(sql);
675
676 return TRUE;
677 }
678
679 gboolean
680 poi_get_list_inside(gdouble lat1, gdouble lon1, gdouble lat2, gdouble lon2, GtkListStore **store, guint *num_poi)
681 {
682 GtkTreeIter iter;
683 gchar tmp1[16], tmp2[16];
684
685 num_poi=0;
686
687 if (!_db)
688         return FALSE;
689
690 if (SQLITE_OK != sqlite3_bind_double(poisql.select_poi, 1, lat1) ||
691     SQLITE_OK != sqlite3_bind_double(poisql.select_poi, 2, lat2) ||
692     SQLITE_OK != sqlite3_bind_double(poisql.select_poi, 3, lon1) ||
693     SQLITE_OK != sqlite3_bind_double(poisql.select_poi, 4, lon2)) {
694         g_printerr("Failed to bind values for poisql.select_poi\n");
695         return FALSE;
696 }
697
698 *store = poi_list_store_new();
699
700 while (SQLITE_ROW == sqlite3_step(poisql.select_poi)) {
701         gdouble lat, lon, dist=0;
702
703         lat=sqlite3_column_double(poisql.select_poi, 0);
704         lon=sqlite3_column_double(poisql.select_poi, 1);
705         lat_format(_degformat, lat, tmp1);
706         lon_format(_degformat, lon, tmp2);
707
708         gtk_list_store_append(*store, &iter);
709         gtk_list_store_set(*store, &iter,
710                 ITEM_ID, sqlite3_column_int(poisql.select_poi, 2),
711                 ITEM_CATID, sqlite3_column_int(poisql.select_poi, 5),
712                 ITEM_LAT, lat,
713                 ITEM_LON, lon,
714                 ITEM_DIST, dist,
715                 ITEM_LATLON, g_strdup_printf("%s, %s", tmp1, tmp2),
716                 ITEM_LABEL, sqlite3_column_text(poisql.select_poi, 3),
717                 ITEM_DESC, sqlite3_column_text(poisql.select_poi, 4),
718                 ITEM_CATLAB, sqlite3_column_text(poisql.select_poi, 6),
719                 ITEM_ICON, sqlite3_column_text(poisql.select_poi, 8),
720                 ITEM_COLOR, sqlite3_column_text(poisql.select_poi, 9),
721                 -1);
722         *num_poi++;
723 }
724 sqlite3_reset(poisql.select_poi);
725 sqlite3_clear_bindings(poisql.select_poi);
726 return TRUE;
727 }
728
729 gboolean
730 poi_get_list_near_unit(guint unitx, guint unity, guint range, GtkListStore **store, guint *num_poi)
731 {
732 gdouble lat1, lon1, lat2, lon2;
733 guint x, y;
734
735 x=unitx-pixel2unit(3*range);
736 y=unity+pixel2unit(3*range);
737 unit2latlon(x, y, lat1, lon1);
738
739 x=unitx+pixel2unit(3*range);
740 y=unity-pixel2unit(3*range);
741 unit2latlon(x, y, lat2, lon2); 
742
743 return poi_get_list_inside(lat1, lon1, lat2, lon2, store, num_poi);
744 }
745
746 poi_info *
747 poi_get_by_id(guint id)
748 {
749 poi_info *p=NULL;
750
751 if (SQLITE_OK!=sqlite3_bind_double(poisql.select_poi_by_id, 1, id))
752         return NULL;
753
754 if (SQLITE_ROW==sqlite3_step(poisql.select_poi_by_id)) {
755         p=poi_new();
756         p->poi_id=sqlite3_column_int(poisql.select_poi_by_id, PS_ID);
757         p->lat=sqlite3_column_double(poisql.select_poi_by_id, PS_LAT);
758         p->lon=sqlite3_column_double(poisql.select_poi_by_id, PS_LON);
759         p->source=sqlite3_column_int(poisql.select_poi_by_id, PS_SOURCE);
760         p->public=sqlite3_column_int(poisql.select_poi_by_id, PS_PUBLIC)==1 ? TRUE : FALSE;
761         p->label=g_strdup(sqlite3_column_text(poisql.select_poi_by_id, PS_LABEL));
762         p->desc=g_strdup(sqlite3_column_text(poisql.select_poi_by_id, PS_DESC));
763         p->url=g_strdup(sqlite3_column_text(poisql.select_poi_by_id, PS_URL));
764         p->postal_code=g_strdup(sqlite3_column_text(poisql.select_poi_by_id, PS_POSTAL_CODE));
765
766         p->cat_id=sqlite3_column_int(poisql.select_poi_by_id, PS_CAT_ID);
767         p->cat_label=g_strdup(sqlite3_column_text(poisql.select_poi_by_id, PS_CAT_LABEL));
768         p->cat_desc=g_strdup(sqlite3_column_text(poisql.select_poi_by_id, PS_CAT_DESC));
769 }
770
771 sqlite3_reset(poisql.select_poi_by_id);
772 sqlite3_clear_bindings(poisql.select_poi_by_id);
773
774 return p;
775 }
776
777 gboolean
778 poi_update(poi_info *p)
779 {
780 if (!poidb)
781         return FALSE;
782
783 if (p->poi_id==0)
784         return FALSE;
785
786 if (SQLITE_OK != sqlite3_bind_text(poisql.update_poi, 1, p->label, -1, SQLITE_STATIC)
787    || SQLITE_OK != sqlite3_bind_text(poisql.update_poi, 2, p->desc, -1, SQLITE_STATIC)
788    || SQLITE_OK != sqlite3_bind_int(poisql.update_poi, 3, p->cat_id)
789    || SQLITE_OK != sqlite3_bind_int(poisql.update_poi, 4, p->poi_id)
790    || SQLITE_DONE != sqlite3_step(poisql.update_poi)) {
791                 return FALSE;
792         }
793 sqlite3_reset(poisql.update_poi);
794 sqlite3_clear_bindings(poisql.update_poi);
795 return TRUE;
796 }
797
798 /* XXX: Add url and postal_code */
799 gboolean
800 poi_add(poi_info *p)
801 {
802 time_t t;
803
804 if (!poidb)
805         return FALSE;
806
807 if (p->poi_id!=0)
808         return FALSE;
809
810 t=time(NULL);
811
812 if (SQLITE_OK != sqlite3_bind_double(poisql.insert_poi, 1, p->lat)
813     || SQLITE_OK != sqlite3_bind_double(poisql.insert_poi, 2, p->lon)
814     || SQLITE_OK != sqlite3_bind_text(poisql.insert_poi, 3, p->label, -1, SQLITE_STATIC)
815     || SQLITE_OK != sqlite3_bind_text(poisql.insert_poi, 4, p->desc, -1, SQLITE_STATIC)
816     || SQLITE_OK != sqlite3_bind_text(poisql.insert_poi, 5, p->url, -1, SQLITE_STATIC)
817     || SQLITE_OK != sqlite3_bind_text(poisql.insert_poi, 6, p->postal_code, -1, SQLITE_STATIC)
818     || SQLITE_OK != sqlite3_bind_int(poisql.insert_poi, 7, p->cat_id) 
819     || SQLITE_OK != sqlite3_bind_int(poisql.insert_poi, 8, t) 
820     || SQLITE_OK != sqlite3_bind_int(poisql.insert_poi, 9, p->source)
821         || SQLITE_DONE != sqlite3_step(poisql.insert_poi)) {
822                 sqlite3_reset(poisql.insert_poi);
823                 sqlite3_clear_bindings(poisql.insert_poi);
824                 return FALSE;
825         }
826 sqlite3_reset(poisql.insert_poi);
827 sqlite3_clear_bindings(poisql.insert_poi);
828 return TRUE;
829 }
830
831 poi_info *
832 poi_find_nearest(gdouble lat, gdouble lon) 
833 {
834 poi_info *p;
835
836 if (!poidb)
837         return FALSE;
838
839 sqlite3_reset(poisql.select_nearest_poi);
840 sqlite3_clear_bindings(poisql.select_nearest_poi);
841
842 if (SQLITE_OK == sqlite3_bind_double(poisql.select_nearest_poi, 1, lat)
843     && SQLITE_OK == sqlite3_bind_double(poisql.select_nearest_poi, 2, lon)
844     && SQLITE_ROW == sqlite3_step(poisql.select_nearest_poi)) {
845
846         p=poi_new();
847         p->lat=sqlite3_column_double(poisql.select_nearest_poi, 0);
848         p->lon=sqlite3_column_double(poisql.select_nearest_poi, 1);
849         p->poi_id=sqlite3_column_double(poisql.select_nearest_poi, 2);
850         p->label=g_strdup(sqlite3_column_text(poisql.select_nearest_poi, 3));
851         p->desc=g_strdup(sqlite3_column_text(poisql.select_nearest_poi, 4));
852         p->cat_id=sqlite3_column_double(poisql.select_nearest_poi, 5);
853         p->cat_desc=g_strdup(sqlite3_column_text(poisql.select_nearest_poi, 6));
854         sqlite3_reset(poisql.select_nearest_poi);
855         sqlite3_clear_bindings(poisql.select_nearest_poi);
856         return p;
857 }
858 return NULL;
859 }
860
861 GdkPixbuf *
862 poi_get_icon(gchar *icon, gboolean big)
863 {
864 gchar buffer[100];
865 GdkPixbuf *pixbuf;
866 GError *error = NULL;
867
868 if (icon==NULL)
869         return NULL;
870
871 if (strlen(icon)==0)
872         return NULL;
873
874 g_snprintf(buffer, sizeof(buffer), DATADIR "/map-icons/%s.%s/%s.png", 
875         theme, (big==TRUE) ? "big" : "small", icon);
876
877 pixbuf=g_hash_table_lookup(poi_icon_hash, buffer);
878 if (pixbuf)
879         return pixbuf;
880
881 pixbuf=gdk_pixbuf_new_from_file(buffer, &error);
882
883 if (error)
884         return NULL;
885
886 g_hash_table_insert(poi_icon_hash, g_strdup(buffer), pixbuf);
887 return pixbuf;
888 }
889
890 GtkListStore *
891 poi_category_generate_store(void)
892 {
893 GtkTreeIter iter;
894 GtkListStore *store;
895
896 if (!poidb)
897         return NULL;
898
899 store=gtk_list_store_new(CAT_NUM_COLUMNS, /* pixbuf */
900                                 G_TYPE_UINT,
901                                 G_TYPE_BOOLEAN,
902                                 G_TYPE_STRING, 
903                                 G_TYPE_STRING, 
904                                 G_TYPE_UINT,
905                                 GDK_TYPE_PIXBUF);
906
907 while (SQLITE_ROW == sqlite3_step(poisql.selall_cat)) {
908         gtk_list_store_append(store, &iter);
909         gtk_list_store_set(store, &iter,
910                 CAT_ID, sqlite3_column_int(poisql.selall_cat, 0), 
911                 CAT_ENABLED, sqlite3_column_int(poisql.selall_cat, 3),
912                 CAT_LABEL, sqlite3_column_text(poisql.selall_cat, 1),
913                 CAT_DESC, sqlite3_column_text(poisql.selall_cat, 2),
914                 CAT_POI_CNT, sqlite3_column_int(poisql.selall_cat, 6), 
915                 CAT_ICON, poi_get_icon(sqlite3_column_text(poisql.selall_cat, 4),TRUE),
916                 -1);
917 }
918
919 sqlite3_reset(poisql.selall_cat);
920 sqlite3_clear_bindings(poisql.selall_cat);
921
922 return store;
923 }