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