]>
Commit | Line | Data |
---|---|---|
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 | #ifdef HAVE_CONFIG_H | |
23 | #include "config.h" | |
24 | #endif | |
25 | ||
26 | #include <string.h> | |
27 | #include <stdlib.h> | |
28 | #ifdef HAVE_SYS_TYPES_H | |
29 | #include <sys/types.h> | |
30 | #endif | |
31 | #ifdef HAVE_UNISTD_H | |
32 | #include <unistd.h> | |
33 | #endif | |
34 | ||
35 | #include <glib/gi18n.h> | |
36 | ||
37 | #include "viking.h" | |
38 | ||
39 | ||
40 | typedef struct { | |
41 | gpointer clipboard; | |
42 | gint pid; | |
43 | VikClipboardDataType type; | |
44 | gint subtype; | |
45 | guint16 layer_type; | |
46 | guint len; | |
47 | guint8 data[0]; | |
48 | } vik_clipboard_t; | |
49 | ||
50 | static GtkTargetEntry target_table[] = { | |
51 | { "application/viking", 0, 0 }, | |
52 | { "STRING", 0, 1 }, | |
53 | }; | |
54 | ||
55 | /***************************************************************** | |
56 | ** functions which send to the clipboard client (we are owner) ** | |
57 | *****************************************************************/ | |
58 | ||
59 | static void clip_get ( GtkClipboard *c, GtkSelectionData *selection_data, guint info, gpointer p ) | |
60 | { | |
61 | vik_clipboard_t *vc = p; | |
62 | if (info==0) { | |
63 | // g_print("clip_get: vc = %p, size = %d\n", vc, sizeof(*vc) + vc->len); | |
64 | gtk_selection_data_set ( selection_data, selection_data->target, 8, (void *)vc, sizeof(*vc) + vc->len ); | |
65 | } | |
66 | } | |
67 | ||
68 | static void clip_clear ( GtkClipboard *c, gpointer p ) | |
69 | { | |
70 | g_free(p); | |
71 | } | |
72 | ||
73 | ||
74 | /************************************************************************** | |
75 | ** functions which receive from the clipboard owner (we are the client) ** | |
76 | **************************************************************************/ | |
77 | ||
78 | /* our own data type */ | |
79 | static void clip_receive_viking ( GtkClipboard *c, GtkSelectionData *sd, gpointer p ) | |
80 | { | |
81 | VikLayersPanel *vlp = p; | |
82 | vik_clipboard_t *vc; | |
83 | if (sd->length == -1) { | |
84 | g_warning ( _("paste failed") ); | |
85 | return; | |
86 | } | |
87 | // g_print("clip receive: target = %s, type = %s\n", gdk_atom_name(sd->target), gdk_atom_name(sd->type)); | |
88 | g_assert(!strcmp(gdk_atom_name(sd->target), target_table[0].target)); | |
89 | ||
90 | vc = (vik_clipboard_t *)sd->data; | |
91 | // g_print(" sd->data = %p, sd->length = %d, vc->len = %d\n", sd->data, sd->length, vc->len); | |
92 | ||
93 | if (sd->length != sizeof(*vc) + vc->len) { | |
94 | g_warning ( _("wrong clipboard data size") ); | |
95 | return; | |
96 | } | |
97 | ||
98 | if ( vc->type == VIK_CLIPBOARD_DATA_LAYER ) | |
99 | { | |
100 | VikLayer *new_layer = vik_layer_unmarshall ( vc->data, vc->len, vik_layers_panel_get_viewport(vlp) ); | |
101 | vik_layers_panel_add_layer ( vlp, new_layer ); | |
102 | } | |
103 | else if ( vc->type == VIK_CLIPBOARD_DATA_SUBLAYER ) | |
104 | { | |
105 | VikLayer *sel = vik_layers_panel_get_selected ( vlp ); | |
106 | if ( sel && sel->type == vc->layer_type) | |
107 | { | |
108 | if ( vik_layer_get_interface(vc->layer_type)->paste_item ) | |
109 | vik_layer_get_interface(vc->layer_type)->paste_item ( sel, vc->subtype, vc->data, vc->len); | |
110 | } | |
111 | else | |
112 | a_dialog_error_msg_extra ( VIK_GTK_WINDOW_FROM_WIDGET(GTK_WIDGET(vlp)), | |
113 | _("The clipboard contains sublayer data for a %s layers." | |
114 | "You must select a layer of this type to paste the clipboard data."), | |
115 | vik_layer_get_interface(vc->layer_type)->name ); | |
116 | } | |
117 | } | |
118 | ||
119 | ||
120 | ||
121 | /* | |
122 | * utility func to handle pasted text: | |
123 | * search for N dd.dddddd W dd.dddddd, N dd° dd.dddd W dd° dd.ddddd and so forth | |
124 | */ | |
125 | static gboolean clip_parse_latlon ( const gchar *text, struct LatLon *coord ) | |
126 | { | |
127 | gint latdeg, londeg; | |
128 | gdouble latm, lonm; | |
129 | gdouble lat, lon; | |
130 | gchar *cand; | |
131 | gint len, i; | |
132 | gchar *s = g_strdup(text); | |
133 | ||
134 | // g_print("parsing %s\n", s); | |
135 | ||
136 | len = strlen(s); | |
137 | ||
138 | /* remove non-digits following digits; gets rid of degree symbols or whatever people use, and | |
139 | * punctuation | |
140 | */ | |
141 | for (i=0; i<len-2; i++) { | |
142 | if (g_ascii_isdigit (s[i]) && s[i+1] != '.' && !g_ascii_isdigit (s[i+1])) { | |
143 | s[i+1] = ' '; | |
144 | if (!g_ascii_isalnum (s[i+2]) && s[i+2] != '-') { | |
145 | s[i+2] = ' '; | |
146 | } | |
147 | } | |
148 | } | |
149 | ||
150 | /* now try reading coordinates */ | |
151 | for (i=0; i<len; i++) { | |
152 | if (s[i] == 'N' || s[i] == 'S' || g_ascii_isdigit (s[i])) { | |
153 | gchar latc[2] = "SN"; | |
154 | gchar lonc[2] = "WE"; | |
155 | gint j, k; | |
156 | cand = s+i; | |
157 | ||
158 | for (j=0; j<2; j++) { | |
159 | for (k=0; k<2; k++) { | |
160 | gchar fmt1[] = "N %d%*[ ]%lf W %d%*[ ]%lf"; | |
161 | gchar fmt2[] = "%d%*[ ]%lf N %d%*[ ]%lf W"; | |
162 | gchar fmt3[] = "N %lf W %lf"; | |
163 | gchar fmt4[] = "%lf N %lf W"; | |
164 | ||
165 | fmt1[0] = latc[j]; fmt1[13] = lonc[k]; | |
166 | fmt2[11] = latc[j]; fmt2[24] = lonc[k]; | |
167 | fmt3[0] = latc[j]; fmt3[6] = lonc[k]; | |
168 | fmt4[4] = latc[j]; fmt4[10] = lonc[k]; | |
169 | ||
170 | if (sscanf(cand, fmt1, &latdeg, &latm, &londeg, &lonm) == 4 || | |
171 | sscanf(cand, fmt2, &latdeg, &latm, &londeg, &lonm) == 4) { | |
172 | lat = (j*2-1) * (latdeg + latm / 60); | |
173 | lon = (k*2-1) * (londeg + lonm / 60); | |
174 | break; | |
175 | } | |
176 | if (sscanf(cand, fmt3, &lat, &lon) == 2 || | |
177 | sscanf(cand, fmt4, &lat, &lon) == 2) { | |
178 | lat *= (j*2-1); | |
179 | lon *= (k*2-1); | |
180 | break; | |
181 | } | |
182 | } | |
183 | if (k!=2) break; | |
184 | } | |
185 | if (j!=2) break; | |
186 | ||
187 | if (sscanf(cand, "%d%*[ ]%lf %d%*[ ]%lf", &latdeg, &latm, &londeg, &lonm) == 4) { | |
188 | lat = latdeg/abs(latdeg) * (abs(latdeg) + latm / 60); | |
189 | lon = londeg/abs(londeg) * (abs(londeg) + lonm / 60); | |
190 | break; | |
191 | } | |
192 | if (sscanf(cand, "%lf %lf", &lat, &lon) == 2) { | |
193 | break; | |
194 | } | |
195 | } | |
196 | } | |
197 | g_free(s); | |
198 | ||
199 | /* did we get to the end without actually finding a coordinate? */ | |
200 | if (i<len && lat >= -90.0 && lat <= 90.0 && lon >= -180.0 && lon <= 180.0) { | |
201 | coord->lat = lat; | |
202 | coord->lon = lon; | |
203 | return TRUE; | |
204 | } | |
205 | return FALSE; | |
206 | } | |
207 | ||
208 | static void clip_add_wp(VikLayersPanel *vlp, struct LatLon *coord) | |
209 | { | |
210 | VikCoord vc; | |
211 | VikLayer *sel = vik_layers_panel_get_selected ( vlp ); | |
212 | ||
213 | ||
214 | vik_coord_load_from_latlon ( &vc, VIK_COORD_LATLON, coord ); | |
215 | ||
216 | if (sel && sel->type == VIK_LAYER_TRW) { | |
217 | vik_trw_layer_new_waypoint ( VIK_TRW_LAYER(sel), VIK_GTK_WINDOW_FROM_LAYER(sel), &vc ); | |
218 | } else { | |
219 | 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); | |
220 | } | |
221 | } | |
222 | ||
223 | static void clip_receive_text (GtkClipboard *c, const gchar *text, gpointer p) | |
224 | { | |
225 | VikLayersPanel *vlp = p; | |
226 | struct LatLon coord; | |
227 | // g_print("got text: %s\n", text); | |
228 | if (clip_parse_latlon(text, &coord)) { | |
229 | clip_add_wp(vlp, &coord); | |
230 | } | |
231 | } | |
232 | ||
233 | static void clip_receive_html ( GtkClipboard *c, GtkSelectionData *sd, gpointer p ) | |
234 | { | |
235 | VikLayersPanel *vlp = p; | |
236 | gsize r, w; | |
237 | GError *err = NULL; | |
238 | gchar *s, *span; | |
239 | gint tag = 0, i; | |
240 | struct LatLon coord; | |
241 | ||
242 | if (sd->length == -1) { | |
243 | return; | |
244 | } | |
245 | ||
246 | /* - copying from Mozilla seems to give html in UTF-16. */ | |
247 | if (!(s = g_convert ( (gchar *)sd->data, sd->length, "UTF-8", "UTF-16", &r, &w, &err))) { | |
248 | return; | |
249 | } | |
250 | // g_print("html is %d bytes long: %s\n", sd->length, s); | |
251 | ||
252 | /* scrape a coordinate pasted from a geocaching.com page: look for a | |
253 | * telltale tag if possible, and then remove tags | |
254 | */ | |
255 | if (!(span = g_strstr_len(s, w, "<span id=\"LatLon\">"))) { | |
256 | span = s; | |
257 | } | |
258 | for (i=0; i<strlen(span); i++) { | |
259 | gchar c = span[i]; | |
260 | if (c == '<') { | |
261 | tag++; | |
262 | } | |
263 | if (tag>0) { | |
264 | span[i] = ' '; | |
265 | } | |
266 | if (c == '>') { | |
267 | if (tag>0) tag--; | |
268 | } | |
269 | } | |
270 | if (clip_parse_latlon(span, &coord)) { | |
271 | clip_add_wp(vlp, &coord); | |
272 | } | |
273 | ||
274 | g_free(s); | |
275 | } | |
276 | ||
277 | /* | |
278 | * deal with various data types a clipboard may hold | |
279 | */ | |
280 | void clip_receive_targets ( GtkClipboard *c, GdkAtom *a, gint n, gpointer p ) | |
281 | { | |
282 | VikLayersPanel *vlp = p; | |
283 | gint i; | |
284 | ||
285 | /* g_print("got targets\n"); */ | |
286 | for (i=0; i<n; i++) { | |
287 | /* g_print(" ""%s""\n", gdk_atom_name(a[i])); */ | |
288 | if (!strcmp(gdk_atom_name(a[i]), "text/html")) { | |
289 | gtk_clipboard_request_contents ( c, gdk_atom_intern("text/html", TRUE), clip_receive_html, vlp ); | |
290 | break; | |
291 | } | |
292 | if (a[i] == GDK_TARGET_STRING) { | |
293 | gtk_clipboard_request_text ( c, clip_receive_text, vlp ); | |
294 | break; | |
295 | } | |
296 | if (!strcmp(gdk_atom_name(a[i]), "application/viking")) { | |
297 | gtk_clipboard_request_contents ( c, gdk_atom_intern("application/viking", TRUE), clip_receive_viking, vlp ); | |
298 | break; | |
299 | } | |
300 | } | |
301 | } | |
302 | ||
303 | /********************************************************************************* | |
304 | ** public functions ** | |
305 | *********************************************************************************/ | |
306 | ||
307 | /* | |
308 | * make a copy of selected object and associate ourselves with the clipboard | |
309 | */ | |
310 | void a_clipboard_copy_selected ( VikLayersPanel *vlp ) | |
311 | { | |
312 | VikLayer *sel = vik_layers_panel_get_selected ( vlp ); | |
313 | GtkTreeIter iter; | |
314 | VikClipboardDataType type; | |
315 | guint16 layer_type = 0; | |
316 | gint subtype = 0; | |
317 | guint8 *data = NULL; | |
318 | guint len; | |
319 | ||
320 | if ( ! sel ) | |
321 | return; | |
322 | ||
323 | vik_treeview_get_selected_iter ( sel->vt, &iter ); | |
324 | ||
325 | if ( vik_treeview_item_get_type ( sel->vt, &iter ) == VIK_TREEVIEW_TYPE_SUBLAYER ) { | |
326 | type = VIK_CLIPBOARD_DATA_SUBLAYER; | |
327 | layer_type = sel->type; | |
328 | if ( vik_layer_get_interface(layer_type)->copy_item) { | |
329 | subtype = vik_treeview_item_get_data(sel->vt, &iter); | |
330 | vik_layer_get_interface(layer_type)->copy_item(sel, subtype, vik_treeview_item_get_pointer(sel->vt, &iter), &data, &len ); | |
331 | } | |
332 | } | |
333 | else | |
334 | { | |
335 | gint ilen; | |
336 | type = VIK_CLIPBOARD_DATA_LAYER; | |
337 | vik_layer_marshall ( sel, &data, &ilen ); | |
338 | len = ilen; | |
339 | } | |
340 | ||
341 | if (data) | |
342 | a_clipboard_copy( type, layer_type, subtype, len, data); | |
343 | ||
344 | } | |
345 | ||
346 | void a_clipboard_copy( VikClipboardDataType type, guint16 layer_type, gint subtype, guint len, guint8 * data) | |
347 | { | |
348 | vik_clipboard_t * vc = g_malloc(sizeof(*vc) + len); | |
349 | GtkClipboard *c = gtk_clipboard_get ( GDK_SELECTION_CLIPBOARD ); | |
350 | ||
351 | vc->type = type; | |
352 | vc->layer_type = layer_type; | |
353 | vc->subtype = subtype; | |
354 | vc->len = len; | |
355 | memcpy(vc->data, data, len); | |
356 | g_free(data); | |
357 | vc->pid = getpid(); | |
358 | gtk_clipboard_set_with_data ( c, target_table, G_N_ELEMENTS(target_table), clip_get, clip_clear, vc ); | |
359 | } | |
360 | ||
361 | /* | |
362 | * to deal with multiple data types, we first request the type of data on the clipboard, | |
363 | * and handle them in the callback | |
364 | */ | |
365 | gboolean a_clipboard_paste ( VikLayersPanel *vlp ) | |
366 | { | |
367 | GtkClipboard *c = gtk_clipboard_get ( GDK_SELECTION_CLIPBOARD ); | |
368 | gtk_clipboard_request_targets ( c, clip_receive_targets, vlp ); | |
369 | return TRUE; | |
370 | } | |
371 |