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