#include "osm.h"
#include "osm-db.h"
#include "latlon.h"
+#include "image-cache.h"
static sqlite3 *poidb;
static gchar *theme="square";
static gchar *theme_base=DATADIR "/icons/map-icons";
-/* Hash table for caching POI icons */
-static GHashTable *poi_icon_hash = NULL;
+static ImageCache *poi_ic=NULL;
struct _poi_categories {
node_type_t type;
{ NODE_TOURISM_CAMP_SITE, "Camp site", "Place to go camping" , "accommodation/camping", "#90f080" },
{ NODE_TOURISM_CARAVAN_SITE, "Caravan site", "" , "accommodation/camping/caravan", "#90ff80" },
{ NODE_TOURISM_PICNIC_SITE, "Picnic", "Place to have a Picnic" , "recreation/picnic", "#a5f8e5" },
- { NODE_AMENITY_FOOD, "Restaurant, Fast food", "Places to eat or drink." , "food/fastfood", "#e5960d" },
+ { NODE_AMENITY_FOOD, "Fast food", "Places to eat or drink." , "food/fastfood", "#e5960d" },
+ { NODE_AMENITY_RESTAURANT, "Restaurant", "Fancy places to eat or drink." , "food/restaurant", "#e5960d" },
{ NODE_AMENITY_PUB, "Pub", "Place to drink." , "food/pub", "#f5960d" },
{ NODE_AMENITY_NIGHTCLUB, "Disco, Club", "Place to drink, party and dance." , "food/bar", "#f59c1d" },
{ NODE_AMENITY_CAFE, "Cafe", "Place to drink coffe or tee and eat." , "food/cafe", "#ff960d" },
sqlite3_stmt *sql_cat;
guint i;
-sqlite3_prepare_v2(db,"insert or replace into category (cat_id, label, desc, enabled, priority, icon, color)"
- " values (?, ?, ?, 1, ?, ?, ?)",
- -1, &sql_cat, NULL);
+DB_PREP(db,"insert or replace into category (cat_id, label, desc, enabled, priority, icon, color)"
+ " values (?, ?, ?, 1, ?, ?, ?)", sql_cat);
for (i=0; default_poi_categories[i].name; i++) {
sqlite3_bind_int(sql_cat, 1, default_poi_categories[i].type);
gchar **pszResult;
guint nRow, nColumn;
-if (SQLITE_OK != sqlite3_get_table(db, "select label from poi limit 1",
- &pszResult, &nRow, &nColumn, NULL)) {
+if (SQLITE_OK != sqlite3_get_table(db, "select label from poi limit 1", &pszResult, &nRow, &nColumn, NULL)) {
gchar *create_sql;
g_printerr("Creating initial POI tables\n");
gboolean
poi_db_prepare(sqlite3 *db)
{
- /* Select POIs inside given minmax lat,lon */
- if (sqlite3_prepare_v2(db,
- "select "
- POI_BASE_SQL_FIELDS
- " from poi p, category c "
- " where p.lat between ? and ? "
- " and p.lon between ? and ? "
- " and c.enabled=1 and p.cat_id=c.cat_id order by c.priority limit 500",
- -1, &poisql.select_poi, NULL)!=SQLITE_OK)
- return FALSE;
-
- /* Get POI with given ID */
- if (sqlite3_prepare_v2(db,
- "select "
- POI_BASE_SQL_FIELDS
- " from poi p, category c "
- " where p.poi_id = ? "
- " and p.cat_id=c.cat_id",
- -1, &poisql.select_poi_by_id, NULL)!=SQLITE_OK)
- return FALSE;
-
- /* Search POIs by label */
- if (sqlite3_prepare_v2(db,
- "select "
- POI_BASE_SQL_FIELDS
- " from poi p, category c "
- " where p.lat between ? and ? "
- " and p.lon between ? and ? "
- " 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",
- -1, &poisql.select_poi_search, NULL)!=SQLITE_OK)
- return FALSE;
-
- /* Search POI by label and specific category */
- if (sqlite3_prepare_v2(db,
- "select "
- POI_BASE_SQL_FIELDS
- " from poi p, category c "
- " where p.lat between ? and ? "
- " and p.lon between ? and ? "
- " 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",
- -1, &poisql.select_poi_search_cat, NULL)!=SQLITE_OK)
- return FALSE;
-
- /* Search specific category */
- if (sqlite3_prepare_v2(db,
- "select "
- POI_BASE_SQL_FIELDS
- " from poi p, category c "
- " where p.lat between ? and ? "
- " and p.lon between ? and ? "
- " and c.enabled=1 and p.cat_id=c.cat_id and c.cat_id=? order by p.label",
- -1, &poisql.select_poi_by_cat, NULL)!=SQLITE_OK)
- return FALSE;
-
- /* Select nearest POI */
- if (sqlite3_prepare_v2(db,
- "select "
- POI_MINI_SQL_FIELDS
- " from poi p, category c "
- " where c.enabled = 1 and p.cat_id = c.cat_id "
- " and p.lat between $LAT-0.10 and $LAT+0.10 "
- " and p.lon between $LON-0.10 and $LAT+0.10 "
- " order by (($LAT - p.lat) * ($LAT - p.lat) "
- "+ ($LON - p.lon) * ($LON - p.lon)) limit 1",
- -1, &poisql.select_nearest_poi, NULL)!=SQLITE_OK)
- return FALSE;
-
- /* Insert POI */
- sqlite3_prepare_v2(db, "insert into poi (lat, lon, label, desc, url, postal_code, cat_id, addtime, public, source)"
- " values (?, ?, ?, ?, ?, ?, ?, ?, 1, ?)", -1, &poisql.insert_poi, NULL);
- /* update poi */
- sqlite3_prepare_v2(db, "update poi set label=?, desc=?, cat_id=? where poi_id=?", -1, &poisql.update_poi, NULL);
- /* delete from poi */
- sqlite3_prepare_v2(db, "delete from poi where poi_id=?", -1, &poisql.delete_poi, NULL);
- /* delete from poi by cat_id */
- sqlite3_prepare_v2(db, "delete from poi where cat_id=?", -1, &poisql.delete_poi_by_catid, NULL);
-
- /* select from category */
- sqlite3_prepare_v2(db,
- "select c.label, c.desc, c.enabled"
- " from category c where c.cat_id = ?",
- -1, &poisql.select_cat, NULL);
- /* insert into category */
- sqlite3_prepare_v2(db,
- "insert into category (label, desc, enabled)"
- " values (?, ?, ?)", -1, &poisql.insert_cat, NULL);
- /* update category */
- sqlite3_prepare_v2(db,
- "update category set label = ?, desc = ?,"
- " enabled = ? where poi_id = ?",
- -1, &poisql.update_cat, NULL);
- /* delete from category */
- sqlite3_prepare_v2(db,"delete from category where cat_id = ?",
- -1, &poisql.delete_cat, NULL);
- /* enable category */
- sqlite3_prepare_v2(db,
- "update category set enabled = ?"
- " where cat_id = ?", -1, &poisql.toggle_cat, NULL);
- /* select all category */
- sqlite3_prepare_v2(db,
- "select c.cat_id, c.label, c.desc, c.enabled, c.icon, c.color,"
- " count(p.poi_id)"
- " from category c"
- " left outer join poi p on c.cat_id = p.cat_id"
- " group by c.cat_id, c.label, c.desc, c.enabled "
- " order by c.priority,c.label", -1, &poisql.selall_cat, NULL);
-
- /* Select quick categories */
- sqlite3_prepare_v2(db,
- "select c.cat_id, c.label, c.icon, c.color"
- " from category c where c.enabled=1 "
- " order by c.priority,c.label limit 9", -1, &poisql.select_quick_cat, NULL);
-
- return TRUE;
+/* Select POIs inside given minmax lat,lon */
+DB_PREP(db, "select "
+ POI_BASE_SQL_FIELDS
+ " from poi p, category c "
+ " where p.lat between ? and ? "
+ " and p.lon between ? and ? "
+ " and c.enabled=1 and p.cat_id=c.cat_id order by c.priority limit 500",
+ poisql.select_poi);
+
+/* Get POI with given ID */
+DB_PREP(db, "select "
+ POI_BASE_SQL_FIELDS
+ " from poi p, category c "
+ " where p.poi_id = ? "
+ " and p.cat_id=c.cat_id",
+ poisql.select_poi_by_id);
+
+/* Search POIs by label and any category */
+DB_PREP(db, "select "
+ POI_BASE_SQL_FIELDS
+ " from poi p, category c "
+ " where p.lat between ? and ? "
+ " and p.lon between ? and ? "
+ " 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",
+ poisql.select_poi_search);
+
+/* Search POI by label and category */
+DB_PREP(db, "select "
+ POI_BASE_SQL_FIELDS
+ " from poi p, category c "
+ " where p.lat between ? and ? "
+ " and p.lon between ? and ? "
+ " 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",
+ poisql.select_poi_search_cat);
+
+/* Search POIs by category */
+DB_PREP(db, "select "
+ POI_BASE_SQL_FIELDS
+ " from poi p, category c "
+ " where p.lat between ? and ? "
+ " and p.lon between ? and ? "
+ " and c.enabled=1 and p.cat_id=c.cat_id and c.cat_id=? order by p.label",
+ poisql.select_poi_by_cat);
+
+/* Select any nearest POI */
+DB_PREP(db, "select "
+ POI_MINI_SQL_FIELDS
+ " from poi p, category c "
+ " where c.enabled = 1 and p.cat_id = c.cat_id "
+ " and p.lat between $LAT-0.10 and $LAT+0.10 "
+ " and p.lon between $LON-0.10 and $LAT+0.10 "
+ " order by (($LAT - p.lat) * ($LAT - p.lat) "
+ "+ ($LON - p.lon) * ($LON - p.lon)) limit 1",
+ poisql.select_nearest_poi);
+
+/* Insert POI */
+DB_PREP(db, "insert into poi (lat, lon, label, desc, url, postal_code, cat_id, addtime, public, source)"
+ " values (?, ?, ?, ?, ?, ?, ?, ?, 1, ?)", poisql.insert_poi);
+/* update poi */
+DB_PREP(db, "update poi set label=?, desc=?, cat_id=? where poi_id=?", poisql.update_poi);
+/* delete from poi */
+DB_PREP(db, "delete from poi where poi_id=?", poisql.delete_poi);
+/* delete from poi by cat_id */
+DB_PREP(db, "delete from poi where cat_id=?", poisql.delete_poi_by_catid);
+
+/* select from category */
+DB_PREP(db, "select c.label, c.desc, c.enabled"
+ " from category c where c.cat_id = ?",
+ poisql.select_cat);
+/* insert into category */
+DB_PREP(db, "insert into category (label, desc, enabled)"
+ " values (?, ?, ?)",
+ poisql.insert_cat);
+/* update category */
+DB_PREP(db, "update category set label = ?, desc = ?,"
+ " enabled = ? where poi_id = ?",
+ poisql.update_cat);
+/* delete from category */
+DB_PREP(db,"delete from category where cat_id = ?",
+ poisql.delete_cat);
+/* enable category */
+DB_PREP(db, "update category set enabled = ?"
+ " where cat_id = ?",
+ poisql.toggle_cat);
+/* select all category */
+DB_PREP(db, "select c.cat_id, c.label, c.desc, c.enabled, c.icon, c.color,"
+ " count(p.poi_id)"
+ " from category c"
+ " left outer join poi p on c.cat_id = p.cat_id"
+ " group by c.cat_id, c.label, c.desc, c.enabled "
+ " order by c.priority,c.label",
+ poisql.selall_cat);
+/* Select quick categories */
+DB_PREP(db, "select c.cat_id, c.label, c.icon, c.color"
+ " from category c where c.enabled=1 "
+ " order by c.priority,c.label limit 9",
+ poisql.select_quick_cat);
+return TRUE;
}
void
poi_icon_hash_clear(void)
{
-#if (GLIB_CHECK_VERSION (2, 12, 0))
-g_hash_table_remove_all(poi_icon_hash);
-#else
-g_hash_table_foreach_remove(poi_icon_hash, gtk_true, NULL);
-#endif
+image_cache_clear(poi_ic);
}
void
poi_deinit(sqlite3 *db)
{
-if (poi_icon_hash) {
- g_hash_table_destroy(poi_icon_hash);
- poi_icon_hash=NULL;
+if (poi_ic) {
+ image_cache_free(poi_ic);
+ poi_ic=NULL;
}
sqlite3_finalize(poisql.select_quick_cat);
gboolean
poi_init(sqlite3 **db)
{
-if (!poi_icon_hash)
- poi_icon_hash=g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
-
if (!db || !*db)
return FALSE;
+if (!poi_ic)
+ poi_ic=image_cache_new(128);
+
poidb=*db;
poi_db_create(poidb);
if (poi_db_prepare(poidb)==FALSE) {
gboolean
poi_category_toggle(guint cat_id, gboolean cat_enabled)
{
+g_return_val_if_fail(poisql.toggle_cat, FALSE);
+
if (SQLITE_OK != sqlite3_bind_int(poisql.toggle_cat, 1, cat_enabled) ||
SQLITE_OK != sqlite3_bind_int(poisql.toggle_cat, 2, cat_id) ||
SQLITE_DONE != sqlite3_step(poisql.toggle_cat)) {
{
poi_category *cc;
+g_return_val_if_fail(poisql.select_cat, FALSE);
if (SQLITE_OK != sqlite3_bind_int(poisql.select_cat, 1, cat_id) || SQLITE_ROW != sqlite3_step(poisql.select_cat)) {
sqlite3_reset(poisql.select_cat);
return FALSE;
if (!_db)
return FALSE;
+g_return_val_if_fail(poisql.update_cat, FALSE);
+g_return_val_if_fail(poisql.insert_cat, FALSE);
+
if (cat_id > 0) {
/* edit category */
if (SQLITE_OK != sqlite3_bind_text(poisql.update_cat, 1, c->label, -1, SQLITE_STATIC)
if (!poidb)
return FALSE;
+g_return_val_if_fail(poisql.delete_poi_by_catid, FALSE);
+g_return_val_if_fail(poisql.delete_cat, FALSE);
+
if (SQLITE_OK != sqlite3_bind_int(poisql.delete_poi_by_catid, 1, id) || SQLITE_DONE != sqlite3_step(poisql.delete_poi_by_catid)) {
sqlite3_reset(poisql.delete_poi_by_catid);
return FALSE;
if (!poidb)
return FALSE;
+g_return_val_if_fail(poisql.delete_poi, FALSE);
+
if (SQLITE_OK != sqlite3_bind_int(poisql.delete_poi, 1, id) || SQLITE_DONE != sqlite3_step(poisql.delete_poi)) {
sqlite3_reset(poisql.delete_poi);
return FALSE;
if (!poidb)
return FALSE;
+g_return_val_if_fail(poisql.select_poi, FALSE);
+
g_printf("POI Search: [%s] around %.6f %.6f (%d %d)\n", text, lat, lon, cat, pst);
switch (pst) {
return FALSE;
}
-*store = poi_list_store_new();
+if (*store==NULL)
+ *store = poi_list_store_new();
while (SQLITE_ROW == sqlite3_step(sql)) {
gdouble rlat, rlon, dist;
gboolean
poi_get_list_inside(gdouble lat1, gdouble lon1, gdouble lat2, gdouble lon2, GtkListStore **store, guint *num_poi)
{
+static gboolean active=FALSE;
GtkTreeIter iter;
gchar tmp1[16], tmp2[16];
if (!_db)
return FALSE;
+g_return_val_if_fail(poisql.select_poi, FALSE);
+
+if (active)
+ return FALSE;
+
+active=TRUE;
if (SQLITE_OK != sqlite3_bind_double(poisql.select_poi, 1, lat1) ||
SQLITE_OK != sqlite3_bind_double(poisql.select_poi, 2, lat2) ||
SQLITE_OK != sqlite3_bind_double(poisql.select_poi, 3, lon1) ||
return FALSE;
}
-*store = poi_list_store_new();
+if (*store==NULL)
+ *store = poi_list_store_new();
while (SQLITE_ROW == sqlite3_step(poisql.select_poi)) {
gdouble lat, lon, dist=0;
}
sqlite3_reset(poisql.select_poi);
sqlite3_clear_bindings(poisql.select_poi);
+active=FALSE;
return TRUE;
}
{
poi_info *p=NULL;
+g_return_val_if_fail(poisql.select_poi_by_id, FALSE);
+
if (SQLITE_OK!=sqlite3_bind_double(poisql.select_poi_by_id, 1, id))
return NULL;
if (!poidb)
return FALSE;
+g_return_val_if_fail(poisql.update_poi, FALSE);
+
if (p->poi_id==0)
return FALSE;
if (!poidb)
return FALSE;
+g_return_val_if_fail(poisql.insert_poi, FALSE);
+
if (p->poi_id!=0)
return FALSE;
if (!poidb)
return FALSE;
+g_return_val_if_fail(poisql.select_nearest_poi, FALSE);
+
sqlite3_reset(poisql.select_nearest_poi);
sqlite3_clear_bindings(poisql.select_nearest_poi);
}
GdkPixbuf *
-poi_get_icon(gchar *icon, gboolean big)
+poi_get_icon(const gchar *icon, gboolean big)
{
-gchar buffer[100];
-GdkPixbuf *pixbuf;
-GError *error = NULL;
+gchar buffer[128];
+gchar key[32];
if (icon==NULL)
return NULL;
if (strlen(icon)==0)
return NULL;
-g_snprintf(buffer, sizeof(buffer), "%s/%s.%s/%s.png",
- theme_base, theme, (big==TRUE) ? "big" : "small", icon);
-
-pixbuf=g_hash_table_lookup(poi_icon_hash, buffer);
-if (pixbuf)
- return pixbuf;
+g_snprintf(buffer, sizeof(buffer), "%s/%s.%s/%s.png", theme_base, theme, (big==TRUE) ? "big" : "small", icon);
+g_snprintf(key, sizeof(key), "%s:%s:%s", theme, (big==TRUE) ? "big" : "small", icon);
-pixbuf=gdk_pixbuf_new_from_file(buffer, &error);
-
-if (error)
- return NULL;
-
-g_hash_table_insert(poi_icon_hash, g_strdup(buffer), pixbuf);
-return pixbuf;
+return image_cache_get(poi_ic, key, buffer);
}
GtkListStore *
if (!poidb)
return NULL;
+g_return_val_if_fail(poisql.selall_cat, FALSE);
+
store=gtk_list_store_new(CAT_NUM_COLUMNS, /* pixbuf */
G_TYPE_UINT,
G_TYPE_BOOLEAN,