[Spice-devel] [PATCH spice-gtk V2 2/3] drag-n-drop: handle "drag-data-received" signal

Dunrong Huang riegamaths at gmail.com
Fri Nov 23 03:11:39 PST 2012


When user drags a file to SpiceDisplay and drops it, a signal named
"drag-data-received" will be emitted, the signal will be received by
SpiceDisplay, then our signal handler should receive data which contains
file path, and call spice_main_file_xfer() to transfer file to guest.

Signed-off-by: Dunrong Huang <riegamaths at gmail.com>
---
 gtk/spice-widget.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

diff --git a/gtk/spice-widget.c b/gtk/spice-widget.c
index cdcff03..8e5e412 100644
--- a/gtk/spice-widget.c
+++ b/gtk/spice-widget.c
@@ -464,14 +464,56 @@ static gboolean grab_broken(SpiceDisplay *self, GdkEventGrabBroken *event,
     return false;
 }
 
+/* TODO: support transfering directories. */
+static void drag_data_received_callback(SpiceDisplay *self,
+                                        GdkDragContext *drag_context,
+                                        gint x,
+                                        gint y,
+                                        GtkSelectionData *data,
+                                        guint info,
+                                        guint time,
+                                        gpointer *user_data)
+{
+    int len;
+    char *buf;
+    GList *file_list = NULL;
+    gchar **file_urls, **p;
+    SpiceDisplayPrivate *d = SPICE_DISPLAY_GET_PRIVATE(self);
+
+    /* We get a buf like:
+     * file:///root/a.txt\r\nfile:///root/b.txt\r\n
+     */
+    SPICE_DEBUG("%s: drag a file", __FUNCTION__);
+    buf = (char *)gtk_selection_data_get_data_with_length(data, &len);
+    if (buf == NULL && len <= 0) {
+        return ;
+    }
+
+    p = file_urls = g_uri_list_extract_uris(buf);
+    while (p && *p) {
+        gchar *url = g_strdup(*p);
+        file_list = g_list_append(file_list, url);
+        p++;
+    }
+    g_strfreev(file_urls);
+
+    spice_main_file_xfer(d->main, file_list, NULL);
+
+    gtk_drag_finish(drag_context, TRUE, FALSE, time);
+}
+
 static void spice_display_init(SpiceDisplay *display)
 {
     GtkWidget *widget = GTK_WIDGET(display);
     SpiceDisplayPrivate *d;
+    GtkTargetEntry targets = {"text/plain", 0, 0};
 
     d = display->priv = SPICE_DISPLAY_GET_PRIVATE(display);
 
     g_signal_connect(display, "grab-broken-event", G_CALLBACK(grab_broken), NULL);
+    gtk_drag_dest_set(widget, GTK_DEST_DEFAULT_ALL, &targets, 1, GDK_ACTION_COPY);
+    g_signal_connect(display, "drag-data-received",
+                     G_CALLBACK(drag_data_received_callback), NULL);
     gtk_widget_add_events(widget,
                           GDK_STRUCTURE_MASK |
                           GDK_POINTER_MOTION_MASK |
-- 
1.8.0



More information about the Spice-devel mailing list