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