]> err.no Git - mapper/blobdiff - src/poi.c
Include settings.h so the banner macro hildon version works.
[mapper] / src / poi.c
index db258c4715bc70d8098abf16438b41852fbb4e9a..107fd92ac1c092c6af0eb9dfe5e975fbbdc755c1 100644 (file)
--- a/src/poi.c
+++ b/src/poi.c
@@ -1,7 +1,5 @@
 #include <config.h>
 
-#define _GNU_SOURCE
-
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
 
 static sqlite3 *poidb;
 
+/* POI Icon theme. "classic" or "square". Should be made into a configuration option */
+static gchar *theme="square";
+static gchar *theme_base=DATADIR "/icons/map-icons";
+
+/* Hash table for caching POI icons */
+static GHashTable *poi_icon_hash = NULL;
+
 struct _poi_categories {
        node_type_t type;
        const gchar *name, *desc, *icon, *color;
@@ -42,7 +47,7 @@ static struct _poi_categories default_poi_categories[] = {
        { NODE_AMENITY_PARKING, "Parking", "Parking place for vehicles."  , "vehicle/parking", "#2020ff" },
        { NODE_AMENITY_FUEL, "Fuel", "Stations for purchasing fuel for vehicles."  , "vehicle/fuel_station", "#4040f0" },
        { NODE_AMENITY_SPEEDCAM, "Speed Cam", "Automatic speed cameras."  , "vehicle/restrictions/speed_trap", "#ff0000" },
-       { NODE_AMENITY_HOSPITAL, "Hospital", ""  , "health/hospital", "#ff4040" },
+       { NODE_AMENITY_HOSPITAL, "Hospital", "Get medical help"  , "health/hospital", "#ff4040" },
        { NODE_AMENITY_PHARMACY, "Pharmacy", "Place to get drugs."  , "health/pharmacy", "#40f040" },
        { NODE_AMENITY_POLICE, "Police", "Police station" , "public/police", "#8570ff" },
        { NODE_TOURISM_HOTEL, "Hotel", "Places to stay temporarily or for the night." , "accommodation/hotel", "#ba20ba" },
@@ -53,7 +58,7 @@ static struct _poi_categories default_poi_categories[] = {
        { NODE_AMENITY_POST, "Post office", "Place to handle mail." , "public/post_office", "#ff6868" },
        { NODE_AMENITY_POST_BOX, "Post box", "Send letters." , "public/post_box", "#ff6060" },
        { NODE_TOURISM_INFO, "Tourism info", "A place for tourists to get information." , "misc/information", "#4af04a" },
-       { NODE_AMENITY_TAXI, "Taxi station", "Get a Taxi here." , "", "#50ffff" },
+       { NODE_AMENITY_TAXI, "Taxi station", "Get a Taxi here." , "transport/taxi", "#50ffff" },
        { NODE_RAILWAY_STATION, "Railway station", "Transportation by train." , "transport/railway", "#fa7070" },
        { NODE_RAILWAY_HALT, "Railway halt", "" , "transport/railway_small", "#f27777" },
        { NODE_AMENITY_BUS_STATION, "Bus station", "Transportation by bus." , "transport/bus", "#f07070" },
@@ -62,12 +67,15 @@ static struct _poi_categories default_poi_categories[] = {
        { 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_PUB, "Pub, Disco, Club", "Place to drink and party." , "food/pub", "#f5960d" },
+       { 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" },
        { NODE_AMENITY_CINEMA, "Cinema", "Place to see movies" , "recreation/cinema", "#9090a0" },
        { NODE_AMENITY_THEATRE, "Theatre", "Place to see people performing" , "recreation/theater", "#9595a5" },
        { NODE_AMENITY_SHOP, "Shopping", "Places to shop or acquire services." , "shopping", "#61ef1b" },
+       { NODE_AMENITY_SHOP_ADULT, "Adult shop", "Shops providing adult items, such as sex toys,pornography and fetish clothing" , "", "#ff0000" },
        { NODE_AMENITY_POW, "Place of Worchip", "" , "religion/church", "#7c5b0b" },
        { NODE_TOURISM_THEME_PARK, "Theme Park", "Place to have fun and ride for example rollercoasters." , "recreation/theme_park", "#8eef1b" },
        { NODE_AMENITY_COLLEGE, "College Campus/Building", "" , "education/college", "#813fdc" },
@@ -76,7 +84,7 @@ static struct _poi_categories default_poi_categories[] = {
        { NODE_AMENITY_TOWNHALL, "Townhall", "" , "", "#408090" },
        { NODE_AMENITY_WC, "WC/Toilets", "" , "public/toilets", "#e1d62c" },
        { NODE_AMENITY_TELEPHONE, "Telephone", "Public telephone" , "public/telephone", "#208060" },
-       { NODE_TOURISM_ATTRACTION, "Attraction", "Something interesting" , "generic", "#005000" },
+       { NODE_TOURISM_ATTRACTION, "Attraction", "Something interesting" , "misc", "#005000" },
 
        { NODE_HISTORIC_MUSEUM, "Museum", "A place where objects of historical, artistic, or scientific interest are exhibited, preserved or studied." , "sightseeing/museum", "#202020" },
        { 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" },
@@ -89,31 +97,31 @@ static struct _poi_categories default_poi_categories[] = {
        { NODE_SPORT_SOCCER, "Soccer", "" , "sports/soccer", "#102080" },
        { NODE_SPORT_GOLF, "Golf", "" , "sports/golf", "#102080" },
        { NODE_SPORT_TENNIS, "Tennis", "" , "sports/tennis", "#101080" },
-       { NODE_SPORT_BOWLING, "Bowling", "" , "", "#101080" },
-       { NODE_SPORT_RUGBY, "Rugby", "" , "", "#101080" },
-       { NODE_SPORT_CLIMBING, "Climbing", "" , "", "#101080" },
+       { NODE_SPORT_BOWLING, "Bowling", "" , "sports", "#101080" },
+       { NODE_SPORT_RUGBY, "Rugby", "" , "sports", "#101080" },
+       { NODE_SPORT_CLIMBING, "Climbing", "" , "sports", "#101080" },
        { NODE_SPORT_CYCLING, "Cycling", "" , "sports/bicycle", "#101080" },
-       { NODE_SPORT_MOTOR, "Motor sport", "" , "", "#101080" },
-       { NODE_SPORT_HOCKEY, "Hockey", "" , "", "#5050A0" },
-       { NODE_SPORT_SKATING, "Skating", "" , "", "#5050A0" },
-       { NODE_SPORT_SKATEBOARD, "Skateboard", "" , "", "#101080" },
+       { NODE_SPORT_MOTOR, "Motor sport", "" , "sports", "#101080" },
+       { NODE_SPORT_HOCKEY, "Hockey", "" , "sports", "#5050A0" },
+       { NODE_SPORT_SKATING, "Skating", "" , "sports", "#5050A0" },
+       { NODE_SPORT_SKATEBOARD, "Skateboard", "" , "sports", "#101080" },
        { NODE_SPORT_HORSES, "Horses", "Horse riding or racing" , "sports/riding", "#101080" },
-       { NODE_SPORT_DOG, "Dog racing", "" , "", "#101080" },
-       { NODE_SPORT_BASKETBALL, "Basketball", "" , "", "#101080" },
-       { NODE_SPORT_BASEBALL, "Baseball", "" , "", "#101080" },
+       { NODE_SPORT_DOG, "Dog racing", "" , "sports", "#101080" },
+       { NODE_SPORT_BASKETBALL, "Basketball", "" , "sports", "#101080" },
+       { NODE_SPORT_BASEBALL, "Baseball", "" , "sports", "#101080" },
        { NODE_SPORT_CANOE, "Canoe", "" , "", "#101080" },
-       { NODE_SPORT_CROQUET, "Croquet", "" , "", "#101080" },
-       { NODE_SPORT_CRICKET, "Cricket", "" , "", "#101080" },
-       { NODE_SPORT_SHOOTING, "Shooting", "Shooting range" , "", "#101080" },
-       { NODE_SPORT_PAINTBALL, "Paintball", "Run around and shoot people with paintballs" , "", "#101080" },
-       { NODE_SPORT_TABLE_TENNIS, "Table tennis", "" , "", "#101080" },
-       { NODE_SPORT_PELOTA, "Pelota", "" , "", "#101080" },
+       { NODE_SPORT_CROQUET, "Croquet", "" , "sports", "#101080" },
+       { NODE_SPORT_CRICKET, "Cricket", "" , "sports", "#101080" },
+       { NODE_SPORT_SHOOTING, "Shooting", "Shooting range" , "sports", "#101080" },
+       { NODE_SPORT_PAINTBALL, "Paintball", "Run around and shoot people with paintballs" , "sports", "#101080" },
+       { NODE_SPORT_TABLE_TENNIS, "Table tennis", "" , "sports", "#101080" },
+       { NODE_SPORT_PELOTA, "Pelota", "" , "sports", "#101080" },
        { NODE_SPORT_RACQUET, "Racquet", "" , "sports/racquetball", "#101080" },
-       { NODE_SPORT_BOWLS, "Lawn Bowls", "" , "", "#101080" },
-       { NODE_SPORT_ATHLETICS, "Athletics", "" , "", "#101080" },
-       { NODE_SPORT_OTHER, "Other Sports", "" , "", "#101077" },
+       { NODE_SPORT_BOWLS, "Lawn Bowls", "" , "sports", "#101080" },
+       { NODE_SPORT_ATHLETICS, "Athletics", "" , "sports", "#101080" },
+       { NODE_SPORT_OTHER, "Other Sports", "" , "sports/stadium", "#101077" },
 
-       { NODE_AMENITY_GENERIC, "Other", "Miscellaneous category for everything else." , "generic", "#002000" },
+       { NODE_AMENITY_GENERIC, "Other", "Miscellaneous category for everything else." , "misc", "#002000" },
        { NODE_POI_END, NULL, NULL }
 };
 
@@ -121,7 +129,7 @@ static gboolean
 poi_populate_categories(sqlite3 *db)
 {
 sqlite3_stmt *sql_cat;
-gint i;
+guint i;
 
 sqlite3_prepare_v2(db,"insert or replace into category (cat_id, label, desc, enabled, priority, icon, color)"
                        " values (?, ?, ?, 1, ?, ?, ?)", 
@@ -145,6 +153,18 @@ sqlite3_finalize(sql_cat);
 return TRUE;
 }
 
+const gchar *
+poi_get_icon_from_type(node_type_t t) 
+{
+guint i;
+
+for (i=0; default_poi_categories[i].name; i++) {
+       if (t==default_poi_categories[i].type)
+               return default_poi_categories[i].icon;
+}
+return NULL;
+}
+
 gboolean
 poi_db_create(sqlite3 *db)
 {
@@ -166,13 +186,15 @@ if (SQLITE_OK != sqlite3_get_table(db, "select label from poi limit 1",
                "label text, "
                "desc text, "
                "url text, "
+               "postal_code text,"
                "public int default 1, "
                "source int default 0, "
+               "cat_id int default 900,"
                "osm_id int default 0, "
                "isin_c int default 0, "
                "isin_p int default 0, "
                "priority int default 2, "
-               "cat_id int);"
+               "addtime int);"
             "create table category (cat_id INTEGER PRIMARY KEY AUTOINCREMENT,"
                "label text, "
                "desc text, "
@@ -249,7 +271,7 @@ poi_db_prepare(sqlite3 *db)
                        " 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 ? order by p.label, c.label",
+                       " 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;
 
@@ -260,7 +282,7 @@ poi_db_prepare(sqlite3 *db)
                        " 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 ? and c.cat_id = ? order by p.label",
+                       " 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;
 
@@ -289,8 +311,8 @@ poi_db_prepare(sqlite3 *db)
        return FALSE;
 
        /* Insert POI */
-       sqlite3_prepare_v2(db,  "insert into poi (lat, lon, label, desc, url, postal_code, cat_id, public, source)"
-                                                       " values (?, ?, ?, ?, ?, ?, ?, 1, ?)", -1, &poisql.insert_poi, NULL);
+       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 */
@@ -337,9 +359,24 @@ poi_db_prepare(sqlite3 *db)
        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
+}
+
 void
 poi_deinit(sqlite3 *db)
 {
+if (poi_icon_hash) {
+       g_hash_table_destroy(poi_icon_hash);
+       poi_icon_hash=NULL;
+}
+
 sqlite3_finalize(poisql.select_quick_cat);
 sqlite3_finalize(poisql.selall_cat);
 sqlite3_finalize(poisql.toggle_cat);
@@ -360,6 +397,9 @@ sqlite3_finalize(poisql.select_poi_search_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;
 
@@ -624,8 +664,8 @@ while (SQLITE_ROW == sqlite3_step(sql)) {
 
        rlat=sqlite3_column_double(sql, 0);
        rlon=sqlite3_column_double(sql, 1);
-       lat_format(rlat, tmp1);
-       lon_format(rlon, tmp2);
+       lat_format(_degformat, rlat, tmp1);
+       lon_format(_degformat, rlon, tmp2);
        dist=calculate_distance(lat, lon, rlat, rlon) * UNITS_CONVERT[_units];
 
        gtk_list_store_append(*store, &iter);
@@ -677,8 +717,8 @@ while (SQLITE_ROW == sqlite3_step(poisql.select_poi)) {
 
        lat=sqlite3_column_double(poisql.select_poi, 0);
        lon=sqlite3_column_double(poisql.select_poi, 1);
-       lat_format(lat, tmp1);
-       lon_format(lon, tmp2);
+       lat_format(_degformat, lat, tmp1);
+       lon_format(_degformat, lon, tmp2);
 
        gtk_list_store_append(*store, &iter);
        gtk_list_store_set(*store, &iter,
@@ -743,6 +783,9 @@ if (SQLITE_ROW==sqlite3_step(poisql.select_poi_by_id)) {
        p->cat_desc=g_strdup(sqlite3_column_text(poisql.select_poi_by_id, PS_CAT_DESC));
 }
 
+sqlite3_reset(poisql.select_poi_by_id);
+sqlite3_clear_bindings(poisql.select_poi_by_id);
+
 return p;
 }
 
@@ -771,19 +814,28 @@ return TRUE;
 gboolean
 poi_add(poi_info *p)
 {
+time_t t;
+
 if (!poidb)
        return FALSE;
 
 if (p->poi_id!=0)
        return FALSE;
 
+t=time(NULL);
+
 if (SQLITE_OK != sqlite3_bind_double(poisql.insert_poi, 1, p->lat)
     || SQLITE_OK != sqlite3_bind_double(poisql.insert_poi, 2, p->lon)
     || SQLITE_OK != sqlite3_bind_text(poisql.insert_poi, 3, p->label, -1, SQLITE_STATIC)
     || SQLITE_OK != sqlite3_bind_text(poisql.insert_poi, 4, p->desc, -1, SQLITE_STATIC)
-    || SQLITE_OK != sqlite3_bind_int(poisql.insert_poi, 5, p->cat_id) 
-    || SQLITE_OK != sqlite3_bind_double(poisql.insert_poi, 6, p->source)
+    || SQLITE_OK != sqlite3_bind_text(poisql.insert_poi, 5, p->url, -1, SQLITE_STATIC)
+    || SQLITE_OK != sqlite3_bind_text(poisql.insert_poi, 6, p->postal_code, -1, SQLITE_STATIC)
+    || SQLITE_OK != sqlite3_bind_int(poisql.insert_poi, 7, p->cat_id) 
+    || SQLITE_OK != sqlite3_bind_int(poisql.insert_poi, 8, t) 
+    || SQLITE_OK != sqlite3_bind_int(poisql.insert_poi, 9, p->source)
        || SQLITE_DONE != sqlite3_step(poisql.insert_poi)) {
+               sqlite3_reset(poisql.insert_poi);
+               sqlite3_clear_bindings(poisql.insert_poi);
                return FALSE;
        }
 sqlite3_reset(poisql.insert_poi);
@@ -814,11 +866,42 @@ if (SQLITE_OK == sqlite3_bind_double(poisql.select_nearest_poi, 1, lat)
        p->desc=g_strdup(sqlite3_column_text(poisql.select_nearest_poi, 4));
        p->cat_id=sqlite3_column_double(poisql.select_nearest_poi, 5);
        p->cat_desc=g_strdup(sqlite3_column_text(poisql.select_nearest_poi, 6));
+       sqlite3_reset(poisql.select_nearest_poi);
+       sqlite3_clear_bindings(poisql.select_nearest_poi);
        return p;
 }
 return NULL;
 }
 
+GdkPixbuf *
+poi_get_icon(gchar *icon, gboolean big)
+{
+gchar buffer[100];
+GdkPixbuf *pixbuf;
+GError *error = NULL;
+
+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;
+
+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;
+}
+
 GtkListStore *
 poi_category_generate_store(void)
 {
@@ -828,11 +911,13 @@ GtkListStore *store;
 if (!poidb)
        return NULL;
 
-store=gtk_list_store_new(CAT_NUM_COLUMNS,
+store=gtk_list_store_new(CAT_NUM_COLUMNS, /* pixbuf */
                                G_TYPE_UINT,
                                G_TYPE_BOOLEAN,
                                G_TYPE_STRING, 
-                               G_TYPE_STRING, G_TYPE_UINT);
+                               G_TYPE_STRING, 
+                               G_TYPE_UINT,
+                               GDK_TYPE_PIXBUF);
 
 while (SQLITE_ROW == sqlite3_step(poisql.selall_cat)) {
        gtk_list_store_append(store, &iter);
@@ -841,7 +926,9 @@ while (SQLITE_ROW == sqlite3_step(poisql.selall_cat)) {
                CAT_ENABLED, sqlite3_column_int(poisql.selall_cat, 3),
                CAT_LABEL, sqlite3_column_text(poisql.selall_cat, 1),
                CAT_DESC, sqlite3_column_text(poisql.selall_cat, 2),
-               CAT_POI_CNT, sqlite3_column_int(poisql.selall_cat, 6), -1);
+               CAT_POI_CNT, sqlite3_column_int(poisql.selall_cat, 6), 
+               CAT_ICON, poi_get_icon(sqlite3_column_text(poisql.selall_cat, 4),TRUE),
+               -1);
 }
 
 sqlite3_reset(poisql.selall_cat);