]> git.street.me.uk Git - andy/viking.git/blame - src/clipboard.c
Fix stdout/stderr variable usage.
[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>
a482007a 5 * Copyright (C) 2005, Alex Foobarian <foobarian@gmail.com>
50a14534
EB
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
45acf79e
MA
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
4df1abf8 27#include <string.h>
2f50c650 28#include <stdlib.h>
45acf79e
MA
29#ifdef HAVE_SYS_TYPES_H
30#include <sys/types.h>
31#endif
32#ifdef HAVE_UNISTD_H
33#include <unistd.h>
34#endif
4c77d5e0
GB
35
36#include <glib/gi18n.h>
37
50a14534
EB
38#include "viking.h"
39
2f50c650 40
4df1abf8
EB
41typedef struct {
42 gpointer clipboard;
c434d571 43 gint pid;
2cebc318 44 VikClipboardDataType type;
4df1abf8
EB
45 gint subtype;
46 guint16 layer_type;
c434d571 47 guint len;
80fb9ed8 48 gchar *text;
c434d571 49 guint8 data[0];
4df1abf8 50} vik_clipboard_t;
50a14534 51
4df1abf8
EB
52static GtkTargetEntry target_table[] = {
53 { "application/viking", 0, 0 },
54 { "STRING", 0, 1 },
55};
50a14534 56
2f50c650
AF
57/*****************************************************************
58 ** functions which send to the clipboard client (we are owner) **
59 *****************************************************************/
50a14534 60
4df1abf8
EB
61static void clip_get ( GtkClipboard *c, GtkSelectionData *selection_data, guint info, gpointer p )
62{
63 vik_clipboard_t *vc = p;
80fb9ed8
RN
64 if ( info == 0 ) {
65 // Viking Data Type
0a6cab71 66 // g_print("clip_get: vc = %p, size = %d\n", vc, sizeof(*vc) + vc->len);
9b082b39 67 gtk_selection_data_set ( selection_data, gtk_selection_data_get_target(selection_data), 8, (void *)vc, sizeof(*vc) + vc->len );
50a14534 68 }
80fb9ed8 69 if ( info == 1 ) {
c9570f86
RN
70 // Should be a string, but make sure it's something
71 if ( vc->text )
72 gtk_selection_data_set_text ( selection_data, vc->text, -1); // string text is null terminated
80fb9ed8
RN
73 }
74
50a14534
EB
75}
76
4df1abf8 77static void clip_clear ( GtkClipboard *c, gpointer p )
50a14534 78{
80fb9ed8
RN
79 vik_clipboard_t* vc = (vik_clipboard_t*)p;
80 g_free(vc->text);
81 g_free(vc);
4df1abf8
EB
82}
83
84
2f50c650
AF
85/**************************************************************************
86 ** functions which receive from the clipboard owner (we are the client) **
87 **************************************************************************/
4df1abf8
EB
88
89/* our own data type */
90static void clip_receive_viking ( GtkClipboard *c, GtkSelectionData *sd, gpointer p )
91{
92 VikLayersPanel *vlp = p;
93 vik_clipboard_t *vc;
9b082b39 94 if (gtk_selection_data_get_length(sd) == -1) {
4c77d5e0 95 g_warning ( _("paste failed") );
4df1abf8
EB
96 return;
97 }
9b082b39 98 // g_print("clip receive: target = %s, type = %s\n", gdk_atom_name(gtk_selection_data_get_target(sd), gdk_atom_name(sd->type));
20045e96 99 //g_assert(!strcmp(gdk_atom_name(gtk_selection_data_get_target(sd)), target_table[0].target));
4df1abf8 100
9b082b39 101 vc = (vik_clipboard_t *)gtk_selection_data_get_data(sd);
0a6cab71
AF
102 // g_print(" sd->data = %p, sd->length = %d, vc->len = %d\n", sd->data, sd->length, vc->len);
103
9b082b39 104 if (gtk_selection_data_get_length(sd) != sizeof(*vc) + vc->len) {
4c77d5e0 105 g_warning ( _("wrong clipboard data size") );
0a6cab71
AF
106 return;
107 }
4df1abf8 108
2cebc318 109 if ( vc->type == VIK_CLIPBOARD_DATA_LAYER )
50a14534 110 {
c434d571
AF
111 VikLayer *new_layer = vik_layer_unmarshall ( vc->data, vc->len, vik_layers_panel_get_viewport(vlp) );
112 vik_layers_panel_add_layer ( vlp, new_layer );
50a14534 113 }
2cebc318 114 else if ( vc->type == VIK_CLIPBOARD_DATA_SUBLAYER )
50a14534
EB
115 {
116 VikLayer *sel = vik_layers_panel_get_selected ( vlp );
4df1abf8 117 if ( sel && sel->type == vc->layer_type)
50a14534 118 {
4df1abf8 119 if ( vik_layer_get_interface(vc->layer_type)->paste_item )
ddc47a46 120 vik_layer_get_interface(vc->layer_type)->paste_item ( sel, vc->subtype, vc->data, vc->len);
50a14534
EB
121 }
122 else
4c77d5e0 123 a_dialog_error_msg_extra ( VIK_GTK_WINDOW_FROM_WIDGET(GTK_WIDGET(vlp)),
f5f53833 124 _("The clipboard contains sublayer data for %s layers. "
4c77d5e0
GB
125 "You must select a layer of this type to paste the clipboard data."),
126 vik_layer_get_interface(vc->layer_type)->name );
4df1abf8
EB
127 }
128}
129
130
2f50c650 131
317cbf8a
GB
132/**
133 * clip_parse_latlon:
134 * @text: text containing LatLon data.
135 * @coord: computed coordinates.
136 *
137 * Utility func to handle pasted text:
138 * search for N dd.dddddd W dd.dddddd, N dd° dd.dddd W dd° dd.ddddd and so forth.
139 *
140 * Returns: TRUE if coordinates are set.
4df1abf8 141 */
2f50c650
AF
142static gboolean clip_parse_latlon ( const gchar *text, struct LatLon *coord )
143{
85e43e66
RN
144 gint latdeg, londeg, latmi, lonmi;
145 gdouble lats, lons;
2f50c650
AF
146 gdouble latm, lonm;
147 gdouble lat, lon;
148 gchar *cand;
149 gint len, i;
150 gchar *s = g_strdup(text);
151
152 // g_print("parsing %s\n", s);
153
154 len = strlen(s);
155
156 /* remove non-digits following digits; gets rid of degree symbols or whatever people use, and
157 * punctuation
158 */
159 for (i=0; i<len-2; i++) {
160 if (g_ascii_isdigit (s[i]) && s[i+1] != '.' && !g_ascii_isdigit (s[i+1])) {
161 s[i+1] = ' ';
e0fca367 162 if (!g_ascii_isalnum (s[i+2]) && s[i+2] != '-') {
85e43e66 163 s[i+2] = ' ';
2f50c650
AF
164 }
165 }
166 }
167
168 /* now try reading coordinates */
169 for (i=0; i<len; i++) {
170 if (s[i] == 'N' || s[i] == 'S' || g_ascii_isdigit (s[i])) {
171 gchar latc[2] = "SN";
172 gchar lonc[2] = "WE";
173 gint j, k;
174 cand = s+i;
175
85e43e66 176 // First try matching strings containing the cardinal directions
2f50c650 177 for (j=0; j<2; j++) {
85e43e66
RN
178 for (k=0; k<2; k++) {
179 // DMM
180 gchar fmt1[] = "N %d%*[ ]%lf W %d%*[ ]%lf";
181 gchar fmt2[] = "%d%*[ ]%lf N %d%*[ ]%lf W";
182 // DDD
183 gchar fmt3[] = "N %lf W %lf";
184 gchar fmt4[] = "%lf N %lf W";
185 // DMS
186 gchar fmt5[] = "N%d%*[ ]%d%*[ ]%lf%*[ ]W%d%*[ ]%d%*[ ]%lf";
187
188 // Substitute in 'N','E','S' or 'W' values for each attempt
189 fmt1[0] = latc[j]; fmt1[13] = lonc[k];
190 fmt2[11] = latc[j]; fmt2[24] = lonc[k];
191 fmt3[0] = latc[j]; fmt3[6] = lonc[k];
192 fmt4[4] = latc[j]; fmt4[10] = lonc[k];
193 fmt5[0] = latc[j]; fmt5[23] = lonc[k];
194
195 if (sscanf(cand, fmt1, &latdeg, &latm, &londeg, &lonm) == 4 ||
196 sscanf(cand, fmt2, &latdeg, &latm, &londeg, &lonm) == 4) {
197 lat = (j*2-1) * (latdeg + latm / 60);
198 lon = (k*2-1) * (londeg + lonm / 60);
199 break;
200 }
201 if (sscanf(cand, fmt3, &lat, &lon) == 2 ||
202 sscanf(cand, fmt4, &lat, &lon) == 2) {
203 lat *= (j*2-1);
204 lon *= (k*2-1);
205 break;
206 }
207 gint am = sscanf(cand, fmt5, &latdeg, &latmi, &lats, &londeg, &lonmi, &lons);
208 if ( am == 6 ) {
209 lat = (j*2-1) * (latdeg + latmi / 60.0 + lats / 3600.0);
210 lon = (k*2-1) * (londeg + lonmi / 60.0 + lons / 3600.0);
211 break;
212 }
213 }
214 if (k!=2) break;
2f50c650
AF
215 }
216 if (j!=2) break;
217
85e43e66
RN
218 // DMM without Cardinal directions
219 if (sscanf(cand, "%d%*[ ]%lf*[ ]%d%*[ ]%lf", &latdeg, &latm, &londeg, &lonm) == 4) {
220 lat = latdeg/abs(latdeg) * (abs(latdeg) + latm / 60);
221 lon = londeg/abs(londeg) * (abs(londeg) + lonm / 60);
222 break;
2f50c650 223 }
85e43e66
RN
224 // DMS without Cardinal directions
225 if (sscanf(cand, "%d%*[ ]%d%*[ ]%lf%*[ ]%d%*[ ]%d%*[ ]%lf", &latdeg, &latmi, &lats, &londeg, &lonmi, &lons) == 6) {
226 lat = latdeg/abs(latdeg) * (abs(latdeg) + latm / 60.0 + lats / 3600.0);
227 lon = londeg/abs(londeg) * (abs(londeg) + lonm / 60.0 + lons / 3600.0);
228 break;
229 }
230 // Raw values
2f50c650 231 if (sscanf(cand, "%lf %lf", &lat, &lon) == 2) {
85e43e66 232 break;
2f50c650
AF
233 }
234 }
235 }
236 g_free(s);
237
7680e9fe
AF
238 /* did we get to the end without actually finding a coordinate? */
239 if (i<len && lat >= -90.0 && lat <= 90.0 && lon >= -180.0 && lon <= 180.0) {
2f50c650
AF
240 coord->lat = lat;
241 coord->lon = lon;
242 return TRUE;
2f50c650 243 }
7680e9fe 244 return FALSE;
2f50c650
AF
245}
246
247static void clip_add_wp(VikLayersPanel *vlp, struct LatLon *coord)
248{
249 VikCoord vc;
250 VikLayer *sel = vik_layers_panel_get_selected ( vlp );
251
252
253 vik_coord_load_from_latlon ( &vc, VIK_COORD_LATLON, coord );
254
255 if (sel && sel->type == VIK_LAYER_TRW) {
256 vik_trw_layer_new_waypoint ( VIK_TRW_LAYER(sel), VIK_GTK_WINDOW_FROM_LAYER(sel), &vc );
00de8de3
RN
257 trw_layer_calculate_bounds_waypoints ( VIK_TRW_LAYER(sel) );
258 vik_layer_emit_update ( VIK_LAYER(sel) );
2f50c650 259 } else {
4c77d5e0 260 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
261 }
262}
263
4df1abf8
EB
264static void clip_receive_text (GtkClipboard *c, const gchar *text, gpointer p)
265{
266 VikLayersPanel *vlp = p;
ee4c7107
RN
267
268 g_debug ( "got text: %s", text );
269
270 VikLayer *sel = vik_layers_panel_get_selected ( vlp );
271 if ( sel && vik_treeview_get_editing ( sel->vt ) ) {
272 GtkTreeIter iter;
273 if ( vik_treeview_get_selected_iter ( sel->vt, &iter ) ) {
274 // Try to sanitize input:
275 gchar *name = g_strescape ( text, NULL );
276 vik_layer_rename ( sel, name );
277 vik_treeview_item_set_name ( sel->vt, &iter, name );
278 g_free ( name );
279 }
280 return;
281 }
282
2f50c650
AF
283 struct LatLon coord;
284 if (clip_parse_latlon(text, &coord)) {
285 clip_add_wp(vlp, &coord);
286 }
4df1abf8
EB
287}
288
4df1abf8
EB
289static void clip_receive_html ( GtkClipboard *c, GtkSelectionData *sd, gpointer p )
290{
291 VikLayersPanel *vlp = p;
00bb93da 292 gsize r, w;
4df1abf8 293 GError *err = NULL;
2f50c650
AF
294 gchar *s, *span;
295 gint tag = 0, i;
296 struct LatLon coord;
4df1abf8 297
9b082b39 298 if (gtk_selection_data_get_length(sd) == -1) {
4df1abf8
EB
299 return;
300 }
301
2f50c650 302 /* - copying from Mozilla seems to give html in UTF-16. */
9b082b39 303 if (!(s = g_convert ( (gchar *)gtk_selection_data_get_data(sd), gtk_selection_data_get_length(sd), "UTF-8", "UTF-16", &r, &w, &err))) {
4df1abf8
EB
304 return;
305 }
9b082b39 306 // g_print("html is %d bytes long: %s\n", gtk_selection_data_get_length(sd), s);
2f50c650
AF
307
308 /* scrape a coordinate pasted from a geocaching.com page: look for a
309 * telltale tag if possible, and then remove tags
310 */
311 if (!(span = g_strstr_len(s, w, "<span id=\"LatLon\">"))) {
312 span = s;
313 }
314 for (i=0; i<strlen(span); i++) {
c60f82df
RN
315 gchar ch = span[i];
316 if (ch == '<') {
2f50c650
AF
317 tag++;
318 }
319 if (tag>0) {
320 span[i] = ' ';
321 }
c60f82df 322 if (ch == '>') {
2f50c650
AF
323 if (tag>0) tag--;
324 }
325 }
326 if (clip_parse_latlon(span, &coord)) {
327 clip_add_wp(vlp, &coord);
328 }
329
4df1abf8
EB
330 g_free(s);
331}
332
317cbf8a
GB
333/**
334 * clip_receive_targets:
335 *
336 * Deal with various data types a clipboard may hold.
4df1abf8
EB
337 */
338void clip_receive_targets ( GtkClipboard *c, GdkAtom *a, gint n, gpointer p )
339{
340 VikLayersPanel *vlp = p;
341 gint i;
342
4df1abf8 343 for (i=0; i<n; i++) {
20045e96
RN
344 gchar* name = gdk_atom_name(a[i]);
345 //g_print(" ""%s""\n", name);
346 gboolean breaktime = FALSE;
347 if (!g_strcmp0(name, "text/html")) {
4df1abf8 348 gtk_clipboard_request_contents ( c, gdk_atom_intern("text/html", TRUE), clip_receive_html, vlp );
20045e96 349 breaktime = TRUE;
4df1abf8
EB
350 }
351 if (a[i] == GDK_TARGET_STRING) {
352 gtk_clipboard_request_text ( c, clip_receive_text, vlp );
20045e96 353 breaktime = TRUE;
4df1abf8 354 }
20045e96 355 if (!g_strcmp0(name, "application/viking")) {
4df1abf8 356 gtk_clipboard_request_contents ( c, gdk_atom_intern("application/viking", TRUE), clip_receive_viking, vlp );
20045e96 357 breaktime = TRUE;
4df1abf8 358 }
20045e96
RN
359
360 g_free ( name );
361
362 if ( breaktime )
363 break;
50a14534 364 }
4df1abf8
EB
365}
366
367/*********************************************************************************
368 ** public functions **
369 *********************************************************************************/
370
317cbf8a
GB
371/**
372 * a_clipboard_copy_selected:
373 *
374 * Make a copy of selected object and associate ourselves with the clipboard.
4df1abf8 375 */
2cebc318 376void a_clipboard_copy_selected ( VikLayersPanel *vlp )
4df1abf8
EB
377{
378 VikLayer *sel = vik_layers_panel_get_selected ( vlp );
379 GtkTreeIter iter;
9414408e 380 VikClipboardDataType type = VIK_CLIPBOARD_DATA_NONE;
2cebc318
QT
381 guint16 layer_type = 0;
382 gint subtype = 0;
ddc47a46 383 guint8 *data = NULL;
9414408e 384 guint len = 0;
80fb9ed8 385 const gchar *name = NULL;
4df1abf8
EB
386
387 if ( ! sel )
388 return;
389
390 vik_treeview_get_selected_iter ( sel->vt, &iter );
f5f53833 391 layer_type = sel->type;
4df1abf8 392
ee4c7107
RN
393 // Since we intercept copy and paste keyboard operations, this is called even when a cell is being edited
394 if ( vik_treeview_get_editing ( sel->vt ) ) {
395 type = VIK_CLIPBOARD_DATA_TEXT;
396 // I don't think we can access what is actually selected (internal to GTK) so we go for the name of the item
397 // At least this is better than copying the layer data - which is even further away from what the user would be expecting...
398 name = vik_treeview_item_get_name ( sel->vt, &iter );
399 len = 0;
4df1abf8 400 }
ee4c7107
RN
401 else {
402 if ( vik_treeview_item_get_type ( sel->vt, &iter ) == VIK_TREEVIEW_TYPE_SUBLAYER ) {
403 type = VIK_CLIPBOARD_DATA_SUBLAYER;
404 if ( vik_layer_get_interface(layer_type)->copy_item) {
405 subtype = vik_treeview_item_get_data(sel->vt, &iter);
406 vik_layer_get_interface(layer_type)->copy_item(sel, subtype, vik_treeview_item_get_pointer(sel->vt, &iter), &data, &len );
407 // This name is used in setting the text representation of the item on the clipboard.
408 name = vik_treeview_item_get_name(sel->vt, &iter);
409 }
410 }
411 else {
412 gint ilen;
413 type = VIK_CLIPBOARD_DATA_LAYER;
414 vik_layer_marshall ( sel, &data, &ilen );
415 len = ilen;
416 name = vik_layer_get_name ( vik_treeview_item_get_pointer(sel->vt, &iter) );
417 }
80fb9ed8 418 }
ee4c7107 419 a_clipboard_copy ( type, layer_type, subtype, len, name, data );
2cebc318 420}
ddc47a46 421
80fb9ed8 422void a_clipboard_copy( VikClipboardDataType type, guint16 layer_type, gint subtype, guint len, const gchar* text, guint8 * data)
2cebc318
QT
423{
424 vik_clipboard_t * vc = g_malloc(sizeof(*vc) + len);
425 GtkClipboard *c = gtk_clipboard_get ( GDK_SELECTION_CLIPBOARD );
ddc47a46 426
2cebc318
QT
427 vc->type = type;
428 vc->layer_type = layer_type;
429 vc->subtype = subtype;
430 vc->len = len;
80fb9ed8 431 vc->text = g_strdup (text);
ee4c7107
RN
432 if ( data ) {
433 memcpy(vc->data, data, len);
434 g_free(data);
435 }
2cebc318 436 vc->pid = getpid();
ee4c7107
RN
437
438 // Simple clipboard copy when necessary
439 if ( type == VIK_CLIPBOARD_DATA_TEXT )
440 gtk_clipboard_set_text ( c, text, -1 );
441 else
442 gtk_clipboard_set_with_data ( c, target_table, G_N_ELEMENTS(target_table), clip_get, clip_clear, vc );
4df1abf8
EB
443}
444
317cbf8a
GB
445/**
446 * a_clipboard_paste:
447 *
448 * To deal with multiple data types, we first request the type of data on the clipboard,
449 * and handle them in the callback.
4df1abf8
EB
450 */
451gboolean a_clipboard_paste ( VikLayersPanel *vlp )
452{
453 GtkClipboard *c = gtk_clipboard_get ( GDK_SELECTION_CLIPBOARD );
454 gtk_clipboard_request_targets ( c, clip_receive_targets, vlp );
455 return TRUE;
50a14534 456}
2f50c650 457
6760787b
RN
458/**
459 *
460 * Detect our own data types
461 */
462static void clip_determine_viking_type ( GtkClipboard *c, GtkSelectionData *sd, gpointer p )
463{
464 VikClipboardDataType *vdct = p;
465 // Default value
466 *vdct = VIK_CLIPBOARD_DATA_NONE;
467 vik_clipboard_t *vc;
9b082b39 468 if (gtk_selection_data_get_length(sd) == -1) {
6760787b
RN
469 g_warning ("DETERMINING TYPE: length failure");
470 return;
471 }
472
9b082b39 473 vc = (vik_clipboard_t *)gtk_selection_data_get_data(sd);
6760787b
RN
474
475 if ( !vc->type )
476 return;
477
478 if ( vc->type == VIK_CLIPBOARD_DATA_LAYER ) {
479 *vdct = VIK_CLIPBOARD_DATA_LAYER;
480 }
481 else if ( vc->type == VIK_CLIPBOARD_DATA_SUBLAYER ) {
482 *vdct = VIK_CLIPBOARD_DATA_SUBLAYER;
483 }
484 else {
485 g_warning ("DETERMINING TYPE: THIS SHOULD NEVER HAPPEN");
486 }
487}
488
489static void clip_determine_type ( GtkClipboard *c, GdkAtom *a, gint n, gpointer p )
490{
491 gint i;
492 for (i=0; i<n; i++) {
20045e96
RN
493 gchar *name = gdk_atom_name(a[i]);
494 // g_print(" ""%s""\n", name);
495 gboolean breaktime = FALSE;
496 if (!g_strcmp0(name, "application/viking")) {
6760787b 497 gtk_clipboard_request_contents ( c, gdk_atom_intern("application/viking", TRUE), clip_determine_viking_type, p );
20045e96 498 breaktime = TRUE;
6760787b 499 }
20045e96
RN
500
501 g_free ( name );
502
503 if ( breaktime )
504 break;
6760787b
RN
505 }
506}
507
508/**
509 * a_clipboard_type:
510 *
511 * Return the type of data held in the clipboard if any
512 */
513VikClipboardDataType a_clipboard_type ( )
514{
515 GtkClipboard *c = gtk_clipboard_get ( GDK_SELECTION_CLIPBOARD );
516 VikClipboardDataType *vcdt = g_malloc ( sizeof (VikClipboardDataType) );
517
518 gtk_clipboard_request_targets ( c, clip_determine_type, vcdt );
519 gint answer = *vcdt;
520 g_free ( vcdt );
521 return answer;
522}