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