~ubuntu-branches/debian/wheezy/foxtrotgps/wheezy

« back to all changes in this revision

Viewing changes to src/poi.c

  • Committer: Package Import Robot
  • Author(s): Daniel Baumann
  • Date: 2012-06-14 07:23:27 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20120614072327-5uemq9atulmfwhn1
Tags: 1.1.1-1
* Merging upstream version 1.1.1.
* Updating to standers version 3.9.3.
* Updating copyright file to format 1.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
#include <glib.h>
17
17
#include <glib/gprintf.h>
 
18
 
 
19
#include <unistd.h>
 
20
#include <sys/types.h>
 
21
#include <sys/wait.h>
 
22
#include <glib/gstdio.h>
 
23
 
18
24
#include <stdio.h>
19
25
#include <sqlite3.h>
20
26
#include <stdlib.h>
25
31
 
26
32
GtkListStore *list_store;
27
33
 
28
 
gchar *my_strescape (const gchar *source, const gchar *exceptions);
29
34
gchar *my_strescape_back (const gchar *source,  const gchar *exceptions);
30
35
 
31
36
enum {
98
103
        return 0;
99
104
}
100
105
 
101
 
 
102
 
 
103
 
 
104
 
 
 
106
static GdkPixbuf *
 
107
get_poi_icon (poi_t *poi)
 
108
{
 
109
        GdkPixbuf *icon;
 
110
 
 
111
        static GHashTable *icon_cache = NULL;
 
112
        static GdkPixbuf *poi_icon_default = NULL;
 
113
 
 
114
        if (!icon_cache) {
 
115
                /* Initialise icon-cache: */
 
116
                icon_cache = g_hash_table_new (g_str_hash, g_str_equal);
 
117
        }
 
118
 
 
119
        if (!poi_icon_default) {
 
120
                /* Initialise the default POI icon: */
 
121
                poi_icon_default = gdk_pixbuf_new_from_file_at_size
 
122
                        (PACKAGE_PIXMAPS_DIR "/" PACKAGE "-poi.png",
 
123
                         25, 25,
 
124
                         NULL);
 
125
        }
 
126
 
 
127
 
 
128
        icon = g_hash_table_lookup (icon_cache, poi->keywords);
 
129
 
 
130
        if (!icon) {
 
131
                char *icon_dir =
 
132
                        g_build_filename (foxtrotgps_dir, "poi-icons", NULL);
 
133
 
 
134
                /* Not all characters that might be in a POI's name
 
135
                   are suitable for use in icon-filenames, e.g.: 
 
136
                   path-separators need to be escaped so that
 
137
                   a POI named something like "../../evil"
 
138
                   doesn't result in us reading/writing a file
 
139
                   somewhere where we shouldn't, and a number of
 
140
                   characters need to be escaped to allow files
 
141
                   to be stored on some filesystems like FAT.
 
142
                 */
 
143
                char *icon_basename =
 
144
                        g_uri_escape_string (poi->keywords, NULL, TRUE);
 
145
 
 
146
                char *icon_path =
 
147
                        g_build_filename (icon_dir, icon_basename, NULL);
 
148
 
 
149
                g_free (icon_basename);
 
150
 
 
151
                g_mkdir (icon_dir, 0777);
 
152
                g_free (icon_dir);
 
153
 
 
154
                if (!g_file_test (icon_path, G_FILE_TEST_EXISTS)) {
 
155
                        pid_t mkvisualid_pid;
 
156
                        int mkvisualid_status;
 
157
        
 
158
                        char *autocache_dir =
 
159
                                g_build_filename (global_home_dir,
 
160
                                                  ".VisualIDs",
 
161
                                                  PACKAGE, "poi-icons",
 
162
                                                  NULL);
 
163
 
 
164
                        g_mkdir_with_parents (autocache_dir, 0777);
 
165
                        /* Free autocache_dir later--we need to pass it
 
166
                           to mkvisualid, below... */
 
167
        
 
168
                        mkvisualid_pid = fork();
 
169
        
 
170
                        switch (mkvisualid_pid) {
 
171
                        case 0:
 
172
                                execlp ("mkvisualid", "mkvisualid",
 
173
                                        "--autocache",
 
174
                                        "--autocache-dir", autocache_dir,
 
175
                                        "--output", icon_path,
 
176
                                        "--linewidth=.1",
 
177
                                        "--outline-width=.2",
 
178
 
 
179
                                        /* Don't use glyph-types that are
 
180
                                           easily confused with streets: */
 
181
                                        "--line=0",
 
182
                                        "--path=0",
 
183
 
 
184
                                        poi->keywords,
 
185
 
 
186
                                        NULL);
 
187
 
 
188
                                _exit (1); /* execlp failed */
 
189
                        default:
 
190
                                waitpid (mkvisualid_pid, &mkvisualid_status, 0);
 
191
                                /* Fall through to cleanup... */
 
192
                        case -1:
 
193
                                g_free (autocache_dir);
 
194
                        }
 
195
                }
 
196
        
 
197
                icon = gdk_pixbuf_new_from_file_at_size
 
198
                        (icon_path, 32, 32, NULL);
 
199
                /*^Note that this is slightly bigger than
 
200
                   the `generic POI star' fallback-icon:
 
201
                   we budget an additional 25% of the icon-radius
 
202
                   for a border in case the icon needs one,
 
203
                   e.g.: to ensure contrast against the map.
 
204
                 */
 
205
 
 
206
                g_free (icon_path);
 
207
 
 
208
                if (icon) {
 
209
                        g_hash_table_insert (icon_cache,
 
210
                                             poi->keywords,
 
211
                                             icon);
 
212
                }
 
213
        }
 
214
 
 
215
        if (!icon)
 
216
        {
 
217
                icon = poi_icon_default;
 
218
        }
 
219
 
 
220
        return icon;
 
221
}
105
222
 
106
223
 
107
224
void
113
230
        float lat, lon;
114
231
        GSList *list;
115
232
        GdkColor color;
116
 
        GError  *error = NULL;
117
 
        static GdkPixbuf *photo_icon = NULL;
118
233
        static GdkGC *gc;
119
234
        
120
235
 
126
241
        gdk_gc_set_rgb_fg_color(gc, &color);
127
242
        
128
243
 
129
 
        if(!photo_icon)
130
 
        {
131
 
                photo_icon = gdk_pixbuf_new_from_file_at_size (
132
 
                        PACKAGE_PIXMAPS_DIR "/" PACKAGE "-poi.png", 25,25,
133
 
                        &error);
134
 
        }
135
 
 
136
244
        if(global_show_pois)
137
245
        {
138
246
                get_pois();
140
248
                for(list = poi_list; list != NULL; list = list->next)
141
249
                {
142
250
                        poi_t *p = list->data;
 
251
 
 
252
                        GdkPixbuf *photo_icon = get_poi_icon (p);
 
253
                        int icon_width = gdk_pixbuf_get_width (photo_icon);
 
254
                        int icon_height = gdk_pixbuf_get_width (photo_icon);
143
255
                
144
256
                        lat = deg2rad(p->lat_deg);
145
257
                        lon = deg2rad(p->lon_deg);
175
287
                                        NULL,
176
288
                                        photo_icon,
177
289
                                        0,0,
178
 
                                        x-12,y-12,
179
 
                                        24,24,
 
290
                                        x - icon_width/2, y - icon_height/2,
 
291
                                        -1, -1,
180
292
                                        GDK_RGB_DITHER_NONE, 0, 0);
181
293
                                
182
294
                        }
183
295
                        
184
296
                        gtk_widget_queue_draw_area (
185
297
                                        map_drawable, 
186
 
                                        x-12, y-12,
187
 
                                        24,24);
 
298
                                        x - icon_width/2, y - icon_height/2,
 
299
                                        icon_width, icon_height);
188
300
                        
189
301
                        printf("POI: %s lat %f - lon %f\n",p->keywords,p->lat_deg, p->lon_deg);
190
302
                }
376
488
        GtkTextIter start, end;
377
489
        GtkWidget *entry, *radiobutton;
378
490
 
379
 
        const char *keyword_raw;
380
 
        char *desc, *desc_raw, *keyword;
 
491
        const char *keyword;
 
492
        char *desc;
381
493
        char *sql;
382
494
        char *db;
383
495
        int visibility, price_range = 0, extended_open;
400
512
        category = gtk_combo_box_get_active(combo_box);
401
513
        subcategory = gtk_combo_box_get_active(GTK_COMBO_BOX(combobox_subcat));
402
514
        entry = lookup_widget(dialog, "entry13");
403
 
        keyword_raw = gtk_entry_get_text(GTK_ENTRY(entry));
404
 
        keyword = my_strescape(keyword_raw, NULL);
 
515
        keyword = gtk_entry_get_text(GTK_ENTRY(entry));
405
516
        text_view = GTK_TEXT_VIEW(lookup_widget(dialog, "textview1"));
406
517
        buffer = gtk_text_view_get_buffer(text_view);
407
518
        gtk_text_buffer_get_start_iter (buffer, &start);
408
519
        gtk_text_buffer_get_end_iter (buffer, &end);
409
 
        desc_raw = gtk_text_buffer_get_text(buffer, &start, &end, TRUE);
410
 
        desc = my_strescape(desc_raw, NULL);
 
520
        desc = gtk_text_buffer_get_text(buffer, &start, &end, TRUE);
411
521
        radiobutton = lookup_widget(dialog, "radiobutton8");
412
522
        price_range = (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radiobutton))) ? 1 : price_range;
413
523
        radiobutton = lookup_widget(dialog, "radiobutton9");
424
534
        
425
535
        db = g_strconcat(foxtrotgps_dir, "/", POI_DB, NULL);    
426
536
        
427
 
        sql = g_strdup_printf(  
 
537
        sql = sqlite3_mprintf (         
428
538
                        "INSERT INTO poi "
429
539
                        "(idmd5, lat, lon, visibility, cat, subcat, keywords, desc, price_range, extended_open) "
430
 
                        "VALUES ('%.0f%.0f',%f,%f,%d,%d,%d,'%s','%s',%d,%d)",
 
540
                        "VALUES ('%.0f%.0f',%f,%f,%d,%d,%d,%Q,%Q,%d,%d)",
431
541
                        rand1, rand2, lat_deg, lon_deg, visibility, category, subcategory, 
432
542
                        keyword, desc, price_range, extended_open);
433
543
                  
443
553
        }
444
554
 
445
555
        g_free(desc);
446
 
        g_free(desc_raw);
447
 
        g_free(sql);
 
556
        sqlite3_free (sql);
448
557
        gtk_widget_destroy(dialog);
449
558
        
450
559
        
461
570
        GtkTextIter start, end;
462
571
        GtkWidget *entry, *widget; 
463
572
 
464
 
        const char *keyword_raw, *idmd5;
465
 
        char *desc, *desc_raw, *keyword;
 
573
        const char *keyword, *idmd5;
 
574
        char *desc;
466
575
        char *sql;
467
576
        char *db;
468
577
 
491
600
 
492
601
 
493
602
        entry = lookup_widget(dialog, "entry19");
494
 
        keyword_raw = gtk_entry_get_text(GTK_ENTRY(entry));
495
 
        keyword = my_strescape(keyword_raw, NULL);
 
603
        keyword = gtk_entry_get_text(GTK_ENTRY(entry));
496
604
        text_view = GTK_TEXT_VIEW(lookup_widget(dialog, "textview2"));
497
605
        buffer = gtk_text_view_get_buffer(text_view);
498
606
        gtk_text_buffer_get_start_iter (buffer, &start);
499
607
        gtk_text_buffer_get_end_iter (buffer, &end);
500
 
        desc_raw = gtk_text_buffer_get_text(buffer, &start, &end, TRUE);
501
 
        desc = my_strescape(desc_raw, NULL);
 
608
        desc = gtk_text_buffer_get_text(buffer, &start, &end, TRUE);
502
609
 
503
610
 
504
611
        db = g_strconcat(foxtrotgps_dir, "/", POI_DB, NULL);    
505
612
        
506
613
 
507
614
 
508
 
        sql = g_strdup_printf(  
 
615
        sql = sqlite3_mprintf (         
509
616
                        "UPDATE "
510
617
                                "poi "
511
618
                        "SET "
512
619
                                "lat=%f,"
513
620
                                "lon=%f,"
514
 
                                "keywords='%s',"
515
 
                                "desc='%s'"
 
621
                                "keywords=%Q,"
 
622
                                "desc=%Q"
516
623
                        "WHERE "
517
624
                                "idmd5='%s'" 
518
625
                        ,
531
638
        }
532
639
 
533
640
        g_free(desc);
534
 
        g_free(desc_raw);
535
 
        g_free(sql);
 
641
        sqlite3_free (sql);
536
642
        
537
643
        gtk_widget_destroy(dialog);
538
644
        
767
873
 
768
874
}
769
875
 
770
 
gchar *
771
 
my_strescape (const gchar *source,
772
 
             const gchar *exceptions)
773
 
{
774
 
  const guchar *p;
775
 
  gchar *dest;
776
 
  gchar *q;
777
 
  guchar excmap[256];
778
 
 
779
 
  g_return_val_if_fail (source != NULL, NULL);
780
 
 
781
 
  p = (guchar *) source;
782
 
  
783
 
  q = dest = g_malloc (strlen (source) * 4 + 1);
784
 
 
785
 
  memset (excmap, 0, 256);
786
 
  if (exceptions)
787
 
    {
788
 
      guchar *e = (guchar *) exceptions;
789
 
 
790
 
      while (*e)
791
 
        {
792
 
          excmap[*e] = 1;
793
 
          e++;
794
 
        }
795
 
    }
796
 
 
797
 
  while (*p)
798
 
    {
799
 
      if (excmap[*p])
800
 
        *q++ = *p;
801
 
      else
802
 
        {
803
 
          switch (*p)
804
 
            {
805
 
            case '"':
806
 
              
807
 
              *q++ = '`';
808
 
              break;
809
 
            case '\'':
810
 
              
811
 
              *q++ = '`';
812
 
              break;
813
 
            default:
814
 
                *q++ = *p;
815
 
              break;
816
 
            }
817
 
        }
818
 
      p++;
819
 
    }
820
 
  *q = 0;
821
 
  return dest;
822
 
}
823
 
 
824
876
 
825
877
gchar *
826
878
my_strescape_back (const gchar *source,