X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/e890a6e6cdaec70e3231eff0b111c7232f42a140..64d557a287f0e695856d6bc579293b399b8a07ea:/src/clipboard.c?ds=inline diff --git a/src/clipboard.c b/src/clipboard.c index e62e9729..3a6f4910 100644 --- a/src/clipboard.c +++ b/src/clipboard.c @@ -19,19 +19,32 @@ * */ -#include +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include +#include +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif + +#include + #include "viking.h" -#define DATA_NONE 0 -#define DATA_LAYER 1 -#define DATA_SUBLAYER 2 typedef struct { gpointer clipboard; - guint8 type; + gint pid; + VikClipboardDataType type; gint subtype; guint16 layer_type; + guint len; + guint8 data[0]; } vik_clipboard_t; static GtkTargetEntry target_table[] = { @@ -39,42 +52,28 @@ static GtkTargetEntry target_table[] = { { "STRING", 0, 1 }, }; -/************************************** - ** paste from clipboard functions ** - **************************************/ +/***************************************************************** + ** functions which send to the clipboard client (we are owner) ** + *****************************************************************/ static void clip_get ( GtkClipboard *c, GtkSelectionData *selection_data, guint info, gpointer p ) { vik_clipboard_t *vc = p; - g_print("clip_get\n"); if (info==0) { - gtk_selection_data_set ( selection_data, selection_data->target, 8, (void *)vc, sizeof(*vc) ); - } - if (info==1) { - if (vc->type == DATA_LAYER) { - gtk_selection_data_set_text ( selection_data, VIK_LAYER(vc->clipboard)->name, -1 ); - } + // g_print("clip_get: vc = %p, size = %d\n", vc, sizeof(*vc) + vc->len); + gtk_selection_data_set ( selection_data, selection_data->target, 8, (void *)vc, sizeof(*vc) + vc->len ); } } static void clip_clear ( GtkClipboard *c, gpointer p ) { - vik_clipboard_t *vc = p; - g_print("clip_clear\n"); - - if ( vc->clipboard && vc->type == DATA_LAYER ) - g_object_unref ( G_OBJECT(vc->clipboard) ); - else if ( vc->clipboard && vc->type == DATA_SUBLAYER ) - if ( vik_layer_get_interface(vc->layer_type)->free_copied_item ) - vik_layer_get_interface(vc->layer_type)->free_copied_item(vc->subtype,vc->clipboard); - vc->clipboard = NULL; - vc->type = DATA_NONE; + g_free(p); } -/****************************************************** - ** functions which receive from the clipboard owner ** - ******************************************************/ +/************************************************************************** + ** functions which receive from the clipboard owner (we are the client) ** + **************************************************************************/ /* our own data type */ static void clip_receive_viking ( GtkClipboard *c, GtkSelectionData *sd, gpointer p ) @@ -82,75 +81,196 @@ static void clip_receive_viking ( GtkClipboard *c, GtkSelectionData *sd, gpointe VikLayersPanel *vlp = p; vik_clipboard_t *vc; if (sd->length == -1) { - g_print("receive failed\n"); + g_warning ( _("paste failed") ); return; } - g_print("clip receive: target = %s, type = %s\n", gdk_atom_name(sd->target), gdk_atom_name(sd->type)); + // g_print("clip receive: target = %s, type = %s\n", gdk_atom_name(sd->target), gdk_atom_name(sd->type)); g_assert(!strcmp(gdk_atom_name(sd->target), target_table[0].target)); - g_assert(sd->length == sizeof(*vc)); vc = (vik_clipboard_t *)sd->data; + // g_print(" sd->data = %p, sd->length = %d, vc->len = %d\n", sd->data, sd->length, vc->len); - if ( vc->clipboard && vc->type == DATA_LAYER ) + if (sd->length != sizeof(*vc) + vc->len) { + g_warning ( _("wrong clipboard data size") ); + return; + } + + if ( vc->type == VIK_CLIPBOARD_DATA_LAYER ) { - /* oooh, _private_ ... so sue me, there's no other way to do this. */ - if ( G_OBJECT(vc->clipboard)->ref_count == 1 ) - { /* optimization -- if layer has been deleted, don't copy the layer. */ - g_object_ref ( G_OBJECT(vc->clipboard) ); - vik_layers_panel_add_layer ( vlp, VIK_LAYER(vc->clipboard) ); - } - else - { - VikLayer *new_layer = vik_layer_copy ( VIK_LAYER(vc->clipboard), vik_layers_panel_get_viewport(vlp) ); - if ( new_layer ) - { - vik_layers_panel_add_layer ( vlp, new_layer ); - } - } + VikLayer *new_layer = vik_layer_unmarshall ( vc->data, vc->len, vik_layers_panel_get_viewport(vlp) ); + vik_layers_panel_add_layer ( vlp, new_layer ); } - else if ( vc->clipboard && vc->type == DATA_SUBLAYER ) + else if ( vc->type == VIK_CLIPBOARD_DATA_SUBLAYER ) { VikLayer *sel = vik_layers_panel_get_selected ( vlp ); if ( sel && sel->type == vc->layer_type) { if ( vik_layer_get_interface(vc->layer_type)->paste_item ) - vik_layer_get_interface(vc->layer_type)->paste_item ( sel, vc->subtype, vc->clipboard ); + vik_layer_get_interface(vc->layer_type)->paste_item ( sel, vc->subtype, vc->data, vc->len); } else - 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 ); + a_dialog_error_msg_extra ( VIK_GTK_WINDOW_FROM_WIDGET(GTK_WIDGET(vlp)), + _("The clipboard contains sublayer data for %s layers. " + "You must select a layer of this type to paste the clipboard data."), + vik_layer_get_interface(vc->layer_type)->name ); } } + /* - * - search for N dd.dddddd W dd.dddddd, N dd° dd.dddd W dd° dd.ddddd and so forth + * utility func to handle pasted text: + * search for N dd.dddddd W dd.dddddd, N dd° dd.dddd W dd° dd.ddddd and so forth */ +static gboolean clip_parse_latlon ( const gchar *text, struct LatLon *coord ) +{ + gint latdeg, londeg; + gdouble latm, lonm; + gdouble lat, lon; + gchar *cand; + gint len, i; + gchar *s = g_strdup(text); + + // g_print("parsing %s\n", s); + + len = strlen(s); + + /* remove non-digits following digits; gets rid of degree symbols or whatever people use, and + * punctuation + */ + for (i=0; i= -90.0 && lat <= 90.0 && lon >= -180.0 && lon <= 180.0) { + coord->lat = lat; + coord->lon = lon; + return TRUE; + } + return FALSE; +} + +static void clip_add_wp(VikLayersPanel *vlp, struct LatLon *coord) +{ + VikCoord vc; + VikLayer *sel = vik_layers_panel_get_selected ( vlp ); + + + vik_coord_load_from_latlon ( &vc, VIK_COORD_LATLON, coord ); + + if (sel && sel->type == VIK_LAYER_TRW) { + vik_trw_layer_new_waypoint ( VIK_TRW_LAYER(sel), VIK_GTK_WINDOW_FROM_LAYER(sel), &vc ); + } else { + 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); + } +} + static void clip_receive_text (GtkClipboard *c, const gchar *text, gpointer p) { VikLayersPanel *vlp = p; - g_print("Pasted text is ""%s""\n", text); + struct LatLon coord; + // g_print("got text: %s\n", text); + if (clip_parse_latlon(text, &coord)) { + clip_add_wp(vlp, &coord); + } } -/* - * - copying from Mozilla seems to give html in UTF-16. - * - search for ... like on geocaching.com and make a new waypoint - * - search for N dd.dddddd W dd.dddddd, N dd° dd.dddd W dd° dd.ddddd and so forth - */ static void clip_receive_html ( GtkClipboard *c, GtkSelectionData *sd, gpointer p ) { VikLayersPanel *vlp = p; - gint r, w; + gsize r, w; GError *err = NULL; - gchar *s; + gchar *s, *span; + gint tag = 0, i; + struct LatLon coord; if (sd->length == -1) { return; } - if (!(s = g_convert ( sd->data, sd->length, "UTF-8", "UTF-16", &r, &w, &err))) { + /* - copying from Mozilla seems to give html in UTF-16. */ + if (!(s = g_convert ( (gchar *)sd->data, sd->length, "UTF-8", "UTF-16", &r, &w, &err))) { return; } - g_print("html is %d bytes long: %s\n", sd->length, s); + // g_print("html is %d bytes long: %s\n", sd->length, s); + + /* scrape a coordinate pasted from a geocaching.com page: look for a + * telltale tag if possible, and then remove tags + */ + if (!(span = g_strstr_len(s, w, ""))) { + span = s; + } + for (i=0; i0) { + span[i] = ' '; + } + if (c == '>') { + if (tag>0) tag--; + } + } + if (clip_parse_latlon(span, &coord)) { + clip_add_wp(vlp, &coord); + } + g_free(s); } @@ -162,9 +282,9 @@ void clip_receive_targets ( GtkClipboard *c, GdkAtom *a, gint n, gpointer p ) VikLayersPanel *vlp = p; gint i; - g_print("got targets\n"); + /* g_print("got targets\n"); */ for (i=0; ivt, &iter ); + layer_type = sel->type; - if ( vik_treeview_item_get_type ( sel->vt, &iter ) == VIK_TREEVIEW_TYPE_SUBLAYER ) - { - vc->layer_type = sel->type; - if ( vik_layer_get_interface(vc->layer_type)->copy_item && (vc->clipboard = vik_layer_get_interface(vc->layer_type)-> - copy_item(sel,vc->subtype=vik_treeview_item_get_data(sel->vt,&iter),vik_treeview_item_get_pointer(sel->vt,&iter)) )) - vc->type = DATA_SUBLAYER; - else - return; /* selected sublayer is uncopyable */ + if ( vik_treeview_item_get_type ( sel->vt, &iter ) == VIK_TREEVIEW_TYPE_SUBLAYER ) { + type = VIK_CLIPBOARD_DATA_SUBLAYER; + if ( vik_layer_get_interface(layer_type)->copy_item) { + subtype = vik_treeview_item_get_data(sel->vt, &iter); + vik_layer_get_interface(layer_type)->copy_item(sel, subtype, vik_treeview_item_get_pointer(sel->vt, &iter), &data, &len ); + } } else { - vc->clipboard = sel; - g_object_ref ( G_OBJECT(sel) ); - vc->type = DATA_LAYER; + gint ilen; + type = VIK_CLIPBOARD_DATA_LAYER; + vik_layer_marshall ( sel, &data, &ilen ); + len = ilen; } + + if (data) + a_clipboard_copy( type, layer_type, subtype, len, data); + +} + +void a_clipboard_copy( VikClipboardDataType type, guint16 layer_type, gint subtype, guint len, guint8 * data) +{ + vik_clipboard_t * vc = g_malloc(sizeof(*vc) + len); + GtkClipboard *c = gtk_clipboard_get ( GDK_SELECTION_CLIPBOARD ); + + vc->type = type; + vc->layer_type = layer_type; + vc->subtype = subtype; + vc->len = len; + memcpy(vc->data, data, len); + g_free(data); + vc->pid = getpid(); gtk_clipboard_set_with_data ( c, target_table, G_N_ELEMENTS(target_table), clip_get, clip_clear, vc ); } @@ -227,3 +368,4 @@ gboolean a_clipboard_paste ( VikLayersPanel *vlp ) gtk_clipboard_request_targets ( c, clip_receive_targets, vlp ); return TRUE; } +