]> git.street.me.uk Git - andy/viking.git/blob - src/clipboard.c
sanity check pasted lat/lon coordinate
[andy/viking.git] / src / clipboard.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include "viking.h"
26
27 #define DATA_NONE 0
28 #define DATA_LAYER 1
29 #define DATA_SUBLAYER 2
30
31
32 typedef struct {
33   gpointer clipboard;
34   guint8 type;
35   gint subtype;
36   guint16 layer_type;
37 } vik_clipboard_t;
38
39 static GtkTargetEntry target_table[] = {
40   { "application/viking", 0, 0 },
41   { "STRING", 0, 1 },
42 };
43
44 /*****************************************************************
45  ** functions which send to the clipboard client (we are owner) **
46  *****************************************************************/
47
48 static void clip_get ( GtkClipboard *c, GtkSelectionData *selection_data, guint info, gpointer p ) 
49 {
50   vik_clipboard_t *vc = p;
51   //  g_print("clip_get\n");
52   if (info==0) {
53     gtk_selection_data_set ( selection_data, selection_data->target, 8, (void *)vc, sizeof(*vc) );
54   }
55   if (info==1) {
56     if (vc->type == DATA_LAYER) {
57       gtk_selection_data_set_text ( selection_data, VIK_LAYER(vc->clipboard)->name, -1 );
58     } 
59   }
60 }
61
62 static void clip_clear ( GtkClipboard *c, gpointer p )
63 {
64   vik_clipboard_t *vc = p;
65   //  g_print("clip_clear\n");
66
67   if ( vc->clipboard && vc->type == DATA_LAYER )
68     g_object_unref ( G_OBJECT(vc->clipboard) );
69   else if ( vc->clipboard && vc->type == DATA_SUBLAYER )
70     if ( vik_layer_get_interface(vc->layer_type)->free_copied_item )
71       vik_layer_get_interface(vc->layer_type)->free_copied_item(vc->subtype,vc->clipboard);
72   vc->clipboard = NULL;
73   vc->type = DATA_NONE;
74 }
75
76
77 /**************************************************************************
78  ** functions which receive from the clipboard owner (we are the client) **
79  **************************************************************************/
80
81 /* our own data type */
82 static void clip_receive_viking ( GtkClipboard *c, GtkSelectionData *sd, gpointer p ) 
83 {
84   VikLayersPanel *vlp = p;
85   vik_clipboard_t *vc;
86   if (sd->length == -1) {
87     g_print("receive failed\n");
88     return;
89   } 
90   //  g_print("clip receive: target = %s, type = %s\n", gdk_atom_name(sd->target), gdk_atom_name(sd->type));
91   g_assert(!strcmp(gdk_atom_name(sd->target), target_table[0].target));
92   g_assert(sd->length == sizeof(*vc));
93
94   vc = (vik_clipboard_t *)sd->data;
95
96   if ( vc->clipboard && vc->type == DATA_LAYER )
97   {
98     /* oooh, _private_ ... so sue me, there's no other way to do this. */
99     if ( G_OBJECT(vc->clipboard)->ref_count == 1 )
100     { /* optimization -- if layer has been deleted, don't copy the layer. */
101       g_object_ref ( G_OBJECT(vc->clipboard) );
102       vik_layers_panel_add_layer ( vlp, VIK_LAYER(vc->clipboard) );
103     }
104     else
105     {
106       VikLayer *new_layer = vik_layer_copy ( VIK_LAYER(vc->clipboard), vik_layers_panel_get_viewport(vlp) );
107       if ( new_layer )
108       {
109         vik_layers_panel_add_layer ( vlp, new_layer );
110       }
111     }
112   }
113   else if ( vc->clipboard && vc->type == DATA_SUBLAYER )
114   {
115     VikLayer *sel = vik_layers_panel_get_selected ( vlp );
116     if ( sel && sel->type == vc->layer_type)
117     {
118       if ( vik_layer_get_interface(vc->layer_type)->paste_item )
119         vik_layer_get_interface(vc->layer_type)->paste_item ( sel, vc->subtype, vc->clipboard );
120     }
121     else
122       a_dialog_error_msg_extra ( VIK_GTK_WINDOW_FROM_WIDGET(GTK_WIDGET(vlp)), "The clipboard contains sublayer data for a %s layers. You must select a layer of this type to paste the clipboard data.", vik_layer_get_interface(vc->layer_type)->name );
123   }
124 }
125
126
127
128 /*
129  * utility func to handle pasted text:
130  * search for N dd.dddddd W dd.dddddd, N dd° dd.dddd W dd° dd.ddddd and so forth
131  */
132 static gboolean clip_parse_latlon ( const gchar *text, struct LatLon *coord ) 
133 {
134   gint latdeg, londeg;
135   gdouble latm, lonm;
136   gdouble lat, lon;
137   gchar *cand;
138   gint len, i;
139   gchar *s = g_strdup(text);
140
141   //  g_print("parsing %s\n", s);
142
143   len = strlen(s);
144
145   /* remove non-digits following digits; gets rid of degree symbols or whatever people use, and 
146    * punctuation
147    */
148   for (i=0; i<len-2; i++) {
149     if (g_ascii_isdigit (s[i]) && s[i+1] != '.' && !g_ascii_isdigit (s[i+1])) {
150       s[i+1] = ' ';
151       if (!g_ascii_isalnum (s[i+2]) && s[i+2] != '-') {
152         s[i+2] = ' ';
153       }
154     }
155   }
156
157   /* now try reading coordinates */
158   for (i=0; i<len; i++) {
159     if (s[i] == 'N' || s[i] == 'S' || g_ascii_isdigit (s[i])) {
160       gchar latc[2] = "SN";
161       gchar lonc[2] = "WE";
162       gint j, k;
163       cand = s+i;
164
165       //      printf("Trying >>>>> %s\n", cand);
166
167       for (j=0; j<2; j++) {
168         for (k=0; k<2; k++) {
169           gchar fmt1[] = "N %d%*[ ]%lf W %d%*[ ]%lf";
170           gchar fmt2[] = "%d%*[ ]%lf N %d%*[ ]%lf W";
171           gchar fmt3[] = "N %lf W %lf";
172           gchar fmt4[] = "%lf N %lf W";
173
174           fmt1[0]  = latc[j];     fmt1[13] = lonc[k];
175           fmt2[11] = latc[j];     fmt2[24] = lonc[k];
176           fmt3[0]  = latc[j];     fmt3[6]  = lonc[k];
177           fmt4[4]  = latc[j];     fmt4[10] = lonc[k];
178
179           if (sscanf(cand, fmt1, &latdeg, &latm, &londeg, &lonm) == 4 ||
180               sscanf(cand, fmt2, &latdeg, &latm, &londeg, &lonm) == 4) {
181             lat = (j*2-1) * (latdeg + latm / 60);
182             lon = (k*2-1) * (londeg + lonm / 60);
183             break;
184           }
185           if (sscanf(cand, fmt3, &lat, &lon) == 2 ||
186               sscanf(cand, fmt4, &lat, &lon) == 2) {
187             lat *= (j*2-1);
188             lon *= (k*2-1);
189             break;
190           }
191         }
192         if (k!=2) break;
193       }
194       if (j!=2) break;
195
196       if (sscanf(cand, "%d%*[ ]%lf %d%*[ ]%lf", &latdeg, &latm, &londeg, &lonm) == 4) {
197         lat = latdeg/abs(latdeg) * (abs(latdeg) + latm / 60);
198         lon = londeg/abs(londeg) * (abs(londeg) + lonm / 60);
199         break;
200       }
201       if (sscanf(cand, "%lf %lf", &lat, &lon) == 2) {
202         break;
203       }
204     }
205   }
206   g_free(s);
207
208   /* did we get to the end without actually finding a coordinate? */
209   if (i<len && lat >= -90.0 && lat <= 90.0 && lon >= -180.0 && lon <= 180.0) {
210     coord->lat = lat;
211     coord->lon = lon;
212     return TRUE;
213   }
214   return FALSE;
215 }
216
217 static void clip_add_wp(VikLayersPanel *vlp, struct LatLon *coord) 
218 {
219   VikCoord vc;
220   VikLayer *sel = vik_layers_panel_get_selected ( vlp );
221
222
223   vik_coord_load_from_latlon ( &vc, VIK_COORD_LATLON, coord );
224
225   if (sel && sel->type == VIK_LAYER_TRW) {
226     vik_trw_layer_new_waypoint ( VIK_TRW_LAYER(sel), VIK_GTK_WINDOW_FROM_LAYER(sel), &vc );
227   } else {
228     a_dialog_error_msg_extra ( VIK_GTK_WINDOW_FROM_WIDGET(GTK_WIDGET(vlp)), "In order to paste a waypoint, please select an appropriate layer to paste into.", NULL);
229   }
230 }
231
232 static void clip_receive_text (GtkClipboard *c, const gchar *text, gpointer p)
233 {
234   VikLayersPanel *vlp = p;
235   struct LatLon coord;
236   //  g_print("got text: %s\n", text);
237   if (clip_parse_latlon(text, &coord)) {
238     clip_add_wp(vlp, &coord);
239   }
240 }
241
242 static void clip_receive_html ( GtkClipboard *c, GtkSelectionData *sd, gpointer p ) 
243 {
244   VikLayersPanel *vlp = p;
245   gint r, w;
246   GError *err = NULL;
247   gchar *s, *span;
248   gint tag = 0, i;
249   struct LatLon coord;
250
251   if (sd->length == -1) {
252     return;
253   } 
254
255   /* - copying from Mozilla seems to give html in UTF-16. */
256   if (!(s =  g_convert ( sd->data, sd->length, "UTF-8", "UTF-16", &r, &w, &err))) {
257     return;
258   }
259   //  g_print("html is %d bytes long: %s\n", sd->length, s);
260
261   /* scrape a coordinate pasted from a geocaching.com page: look for a 
262    * telltale tag if possible, and then remove tags 
263    */
264   if (!(span = g_strstr_len(s, w, "<span id=\"LatLon\">"))) {
265     span = s;
266   }
267   for (i=0; i<strlen(span); i++) {
268     gchar c = span[i];
269     if (c == '<') {
270       tag++;
271     }
272     if (tag>0) {
273       span[i] = ' ';
274     }
275     if (c == '>') {
276       if (tag>0) tag--;
277     }
278   }
279   if (clip_parse_latlon(span, &coord)) {
280     clip_add_wp(vlp, &coord);
281   }
282
283   g_free(s);
284 }
285
286 /*
287  * deal with various data types a clipboard may hold 
288  */
289 void clip_receive_targets ( GtkClipboard *c, GdkAtom *a, gint n, gpointer p )
290 {
291   VikLayersPanel *vlp = p;
292   gint i;
293
294   /*  g_print("got targets\n"); */
295   for (i=0; i<n; i++) {
296     /* g_print("  ""%s""\n", gdk_atom_name(a[i])); */
297     if (!strcmp(gdk_atom_name(a[i]), "text/html")) {
298       gtk_clipboard_request_contents ( c, gdk_atom_intern("text/html", TRUE), clip_receive_html, vlp );
299       break;
300     }
301     if (a[i] == GDK_TARGET_STRING) {
302       gtk_clipboard_request_text ( c, clip_receive_text, vlp );
303       break;
304     }
305     if (!strcmp(gdk_atom_name(a[i]), "application/viking")) {
306       gtk_clipboard_request_contents ( c, gdk_atom_intern("application/viking", TRUE), clip_receive_viking, vlp );
307       break;
308     }
309   }
310 }
311
312 /*********************************************************************************
313  ** public functions                                                            **
314  *********************************************************************************/
315
316 /* 
317  * make a copy of selected object and associate ourselves with the clipboard
318  */
319 void a_clipboard_copy ( VikLayersPanel *vlp )
320 {
321   VikLayer *sel = vik_layers_panel_get_selected ( vlp );
322   GtkTreeIter iter;
323   vik_clipboard_t *vc = g_malloc(sizeof(*vc));
324   GtkClipboard *c = gtk_clipboard_get ( GDK_SELECTION_CLIPBOARD );
325
326   if ( ! sel )
327     return;
328
329   vik_treeview_get_selected_iter ( sel->vt, &iter );
330
331   if ( vik_treeview_item_get_type ( sel->vt, &iter ) == VIK_TREEVIEW_TYPE_SUBLAYER )
332   {
333     vc->layer_type = sel->type;
334     if ( vik_layer_get_interface(vc->layer_type)->copy_item && (vc->clipboard = vik_layer_get_interface(vc->layer_type)->
335         copy_item(sel,vc->subtype=vik_treeview_item_get_data(sel->vt,&iter),vik_treeview_item_get_pointer(sel->vt,&iter)) ))
336       vc->type = DATA_SUBLAYER;
337     else
338       return; /* selected sublayer is uncopyable */
339   }
340   else
341   {
342     vc->clipboard = sel;
343     g_object_ref ( G_OBJECT(sel) );
344     vc->type = DATA_LAYER;
345   }
346   gtk_clipboard_set_with_data ( c, target_table, G_N_ELEMENTS(target_table), clip_get, clip_clear, vc );
347 }
348
349 /*
350  * to deal with multiple data types, we first request the type of data on the clipboard,
351  * and handle them in the callback
352  */
353 gboolean a_clipboard_paste ( VikLayersPanel *vlp )
354 {
355   GtkClipboard *c = gtk_clipboard_get ( GDK_SELECTION_CLIPBOARD );
356   gtk_clipboard_request_targets ( c, clip_receive_targets, vlp );
357   return TRUE;
358 }
359