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