13 #include <glib/gstdio.h>
16 #include <gconf/gconf-client.h>
17 #include <libxml/parser.h>
28 #include "mapper-types.h"
33 sqlite3_stmt *_stmt_select_poi = NULL;
34 sqlite3_stmt *_stmt_select_nearest_poi = NULL;
35 sqlite3_stmt *_stmt_insert_poi = NULL;
36 sqlite3_stmt *_stmt_update_poi = NULL;
37 sqlite3_stmt *_stmt_delete_poi = NULL;
38 sqlite3_stmt *_stmt_delete_poi_by_catid = NULL;
39 sqlite3_stmt *_stmt_nextlabel_poi = NULL;
41 sqlite3_stmt *_stmt_select_cat = NULL;
42 sqlite3_stmt *_stmt_insert_cat = NULL;
43 sqlite3_stmt *_stmt_update_cat = NULL;
44 sqlite3_stmt *_stmt_delete_cat = NULL;
45 sqlite3_stmt *_stmt_toggle_cat = NULL;
46 sqlite3_stmt *_stmt_selall_cat = NULL;
48 struct _poi_categories {
52 static struct _poi_categories default_poi_categories[] = {
53 {NODE_AMENITY_PARKING, "Parking", "Parking place for vehicles." },
54 {NODE_AMENITY_FUEL, "Fuel", "Stations for purchasing fuel for vehicles." },
55 {NODE_AMENITY_HOSPITAL, "Hospital", "" },
56 {NODE_AMENITY_PHARMACY, "Pharmacy", ""},
57 {NODE_AMENITY_POLICE, "Police", "Police station"},
58 {NODE_AMENITY_HOTEL, "Hotel", "Places to stay temporarily or for the night."},
59 {NODE_AMENITY_HOSTEL, "Hostel", "Places to stay temporarily or for the night."},
60 {NODE_AMENITY_ATM, "ATM", "Automatic Teller Machine/Cashpoint."},
61 {NODE_AMENITY_BANK, "Bank", "Place to take care of your money."},
62 {NODE_AMENITY_POST, "Post office", "Place to handle mail."},
63 {NODE_AMENITY_POST_BOX, "Post box", "Send letters."},
64 {NODE_AMENITY_TAXI, "Taxi station", "Get a Taxi here."},
65 {NODE_AMENITY_RAILWAY_STATION, "Railway station", "Transportation by train."},
66 {NODE_AMENITY_RAILWAY_HALT, "Railway halt", ""},
67 {NODE_AMENITY_BUS_STATION, "Bus station", "Transportation by bus."},
68 {NODE_AMENITY_BOAT, "Harbour", "Transportation by boat."},
69 {NODE_AMENITY_AIRPORT, "Airport", "Transportation by air."},
70 {NODE_AMENITY_FOOD, "Restaurant, Fast food", "Places to eat or drink."},
71 {NODE_AMENITY_PUB, "Pub, Disco, Club", "Place to drink and party."},
72 {NODE_AMENITY_CINEMA, "Cinema", "Place to see movies"},
73 {NODE_AMENITY_THEATRE, "Theatre", "Place to see people performing"},
74 {NODE_AMENITY_SHOP, "Shopping", "Places to shop or acquire services."},
75 {NODE_AMENITY_POW, "Place of Worchip", ""},
76 {NODE_AMENITY_COLLEGE, "College Campus/Building", ""},
77 {NODE_AMENITY_SCHOOL, "School", ""},
78 {NODE_AMENITY_GENERIC, "Other", "Miscellaneous category for everything else."},
79 {NODE_POI_END, NULL, NULL }
83 poi_cb_populate_categories(sqlite3 *db)
85 sqlite3_stmt *sql_cat;
88 g_printf("Checking default categories\n");
89 sqlite3_prepare(db,"insert into category (cat_id, label, desc, enabled)"
90 " values (?, ?, ?, 1)",
93 for (i=0; default_poi_categories[i].name; i++) {
94 sqlite3_bind_int(sql_cat, 1, default_poi_categories[i].type);
95 sqlite3_bind_text(sql_cat, 2, default_poi_categories[i].name , -1, SQLITE_TRANSIENT);
96 sqlite3_bind_text(sql_cat, 3, default_poi_categories[i].desc , -1, SQLITE_TRANSIENT);
97 sqlite3_step(sql_cat);
98 sqlite3_reset(sql_cat);
99 sqlite3_clear_bindings(sql_cat);
102 sqlite3_finalize(sql_cat);
108 poi_db_create(sqlite3 *db)
112 if (SQLITE_OK != sqlite3_get_table(db, "select label from poi limit 1",
113 &pszResult, &nRow, &nColumn, NULL)) {
116 g_printf("Creating initial tables\n");
118 create_sql = sqlite3_mprintf
120 "(poi_id integer PRIMARY KEY, "
127 "source integer default 0, "
128 "osm_id integer default 0, "
129 "priority integer default 2, "
131 "create table category (cat_id integer PRIMARY KEY,"
136 "enabled integer);");
138 if (SQLITE_OK != sqlite3_exec(db, create_sql, NULL, NULL, NULL)
139 && (SQLITE_OK != sqlite3_get_table(db, "select label from poi limit 1",
140 &pszResult, &nRow, &nColumn, NULL))) {
141 g_printf("%s:\n%s",_("Failed to open or create database"),sqlite3_errmsg(db));
147 sqlite3_free_table(pszResult);
149 /* Make sure default categories exists */
150 poi_cb_populate_categories(db);
155 poi_db_prepare(sqlite3 *db)
157 /* select from poi */
159 "select p.lat, p.lon, p.poi_id, p.label, p.desc,"
160 " p.cat_id, c.label, c.desc, c.icon, c.color"
161 " from poi p, category c "
162 " where p.lat between ? and ? "
163 " and p.lon between ? and ? "
164 " and c.enabled = 1 and p.cat_id = c.cat_id",
165 -1, &_stmt_select_poi, NULL);
167 /* select nearest pois */
169 "select p.lat, p.lon, p.label, c.label, c.icon, c.color"
170 " from poi p, category c "
171 " where c.enabled = 1 and p.cat_id = c.cat_id "
172 " and p.lat between $LAT-0.15 and $LAT+0.15 "
173 " and p.lon between $LON-0.15 and $LAT+0.15 "
174 " order by (($LAT - p.lat) * ($LAT - p.lat) "
175 "+ ($LON - p.lon) * ($LON - p.lon)) limit 1",
176 -1, &_stmt_select_nearest_poi, NULL);
180 "insert into poi (lat, lon, label, desc, cat_id, public)"
181 " values (?, ?, ?, ?, ?, 1)", -1, &_stmt_insert_poi, NULL);
183 sqlite3_prepare(db, "update poi set label = ?, desc = ?, "
184 "cat_id = ? where poi_id = ?",
185 -1, &_stmt_update_poi, NULL);
186 /* delete from poi */
187 sqlite3_prepare(db, "delete from poi where poi_id = ?",
188 -1, &_stmt_delete_poi, NULL);
189 /* delete from poi by cat_id */
190 sqlite3_prepare(db, "delete from poi where cat_id = ?",
191 -1, &_stmt_delete_poi_by_catid, NULL);
192 /* get next poilabel */
193 sqlite3_prepare(db, "select ifnull(max(poi_id) + 1,1) from poi",
194 -1, &_stmt_nextlabel_poi, NULL);
196 /* select from category */
198 "select c.label, c.desc, c.enabled"
199 " from category c where c.cat_id = ?",
200 -1, &_stmt_select_cat, NULL);
201 /* insert into category */
203 "insert into category (label, desc, enabled)"
204 " values (?, ?, ?)", -1, &_stmt_insert_cat, NULL);
205 /* update category */
207 "update category set label = ?, desc = ?,"
208 " enabled = ? where poi_id = ?",
209 -1, &_stmt_update_cat, NULL);
210 /* delete from category */
211 sqlite3_prepare(db,"delete from category where cat_id = ?",
212 -1, &_stmt_delete_cat, NULL);
213 /* enable category */
215 "update category set enabled = ?"
216 " where cat_id = ?", -1, &_stmt_toggle_cat, NULL);
217 /* select all category */
219 "select c.cat_id, c.label, c.desc, c.enabled, c.icon, c.color,"
222 " left outer join poi p on c.cat_id = p.cat_id"
223 " group by c.cat_id, c.label, c.desc, c.enabled "
224 " order by c.label", -1, &_stmt_selall_cat, NULL);
230 poi_db_disconnect(sqlite3 **db)
233 /* XXX: Handle prepared statements */
241 poi_db_connect(sqlite3 **db, const gchar *poi_db)
243 if (db_connect(db, poi_db)) {
252 poi_category_update(gint cat_id, gchar * cat_label, gchar * cat_desc,
255 gboolean results=TRUE;
260 sqlite3_bind_text(_stmt_update_cat, 1, cat_label, -1,
262 || SQLITE_OK != sqlite3_bind_text(_stmt_update_cat, 2,
263 cat_desc, -1, g_free)
264 || SQLITE_OK != sqlite3_bind_int(_stmt_update_cat, 3,
266 || SQLITE_OK != sqlite3_bind_int(_stmt_update_cat, 4,
268 || SQLITE_DONE != sqlite3_step(_stmt_update_cat)) {
271 sqlite3_reset(_stmt_update_cat);
272 sqlite3_clear_bindings(_stmt_update_cat);
276 sqlite3_bind_text(_stmt_insert_cat, 1, cat_label, -1,
278 || SQLITE_OK != sqlite3_bind_text(_stmt_insert_cat, 2,
279 cat_desc, -1, g_free)
280 || SQLITE_OK != sqlite3_bind_int(_stmt_insert_cat, 3,
282 || SQLITE_DONE != sqlite3_step(_stmt_insert_cat)) {
285 sqlite3_reset(_stmt_insert_cat);
286 sqlite3_clear_bindings(_stmt_insert_cat);
292 poi_category_delete(DeletePOI * dpoi)
294 if (SQLITE_OK != sqlite3_bind_int(_stmt_delete_poi_by_catid, 1, dpoi->id)
295 || SQLITE_DONE != sqlite3_step(_stmt_delete_poi_by_catid)) {
296 sqlite3_reset(_stmt_delete_poi_by_catid);
299 sqlite3_reset(_stmt_delete_poi_by_catid);
300 sqlite3_clear_bindings(_stmt_delete_poi_by_catid);
302 if (SQLITE_OK != sqlite3_bind_int(_stmt_delete_cat, 1, dpoi->id) ||
303 SQLITE_DONE != sqlite3_step(_stmt_delete_cat)) {
304 sqlite3_reset(_stmt_delete_cat);
307 sqlite3_reset(_stmt_delete_cat);
308 sqlite3_clear_bindings(_stmt_delete_cat);
314 poi_delete(DeletePOI * dpoi)
316 if (SQLITE_OK != sqlite3_bind_int(_stmt_delete_poi, 1, dpoi->id) ||
317 SQLITE_DONE != sqlite3_step(_stmt_delete_poi)) {
318 sqlite3_reset(_stmt_delete_poi);
321 sqlite3_reset(_stmt_delete_poi);
322 sqlite3_clear_bindings(_stmt_delete_poi);
328 poi_update(gint poi_id, gint cat_id, gchar *poi_label, gchar *poi_desc)
330 if (SQLITE_OK != sqlite3_bind_text(_stmt_update_poi, 1, poi_label, -1, SQLITE_STATIC)
331 || SQLITE_OK != sqlite3_bind_text(_stmt_update_poi, 2, poi_desc, -1, g_free)
332 || SQLITE_OK != sqlite3_bind_int(_stmt_update_poi, 3, cat_id)
333 || SQLITE_OK != sqlite3_bind_int(_stmt_update_poi, 4, poi_id)
334 || SQLITE_DONE != sqlite3_step(_stmt_update_poi)) {
337 sqlite3_reset(_stmt_update_poi);
338 sqlite3_clear_bindings(_stmt_update_poi);
343 poi_add(gdouble lat, gdouble lon, gint cat_id, gchar *poi_label, gchar *poi_desc)
345 if (SQLITE_OK != sqlite3_bind_double(_stmt_insert_poi, 1, lat)
346 || SQLITE_OK != sqlite3_bind_double(_stmt_insert_poi, 2, lon)
347 || SQLITE_OK != sqlite3_bind_text(_stmt_insert_poi, 3, poi_label, -1, g_free)
348 || SQLITE_OK != sqlite3_bind_text(_stmt_insert_poi, 4, poi_desc, -1, g_free)
349 || SQLITE_OK != sqlite3_bind_int(_stmt_insert_poi, 5, cat_id)
350 || SQLITE_DONE != sqlite3_step(_stmt_insert_poi)) {
353 sqlite3_reset(_stmt_insert_poi);
354 sqlite3_clear_bindings(_stmt_insert_poi);