]> spindle.queued.net Git - midori/commitdiff
Copy Image s/Address// always copy both URL and data
authorChristian Dywan <christian@twotoasts.de>
Mon, 17 Sep 2012 22:35:19 +0000 (00:35 +0200)
committerChristian Dywan <christian@twotoasts.de>
Mon, 17 Sep 2012 23:51:17 +0000 (01:51 +0200)
midori/midori-view.c
midori/sokoke.c
midori/sokoke.h

index a691dff86000a7fbf3a00ecd48affe5d26cb51a6..a2337f4e87c6966d338d952b035120953914beea 100644 (file)
@@ -2256,9 +2256,9 @@ midori_web_view_menu_link_copy_activate_cb (GtkWidget*  widget,
                                             MidoriView* view)
 {
     if (g_str_has_prefix (view->link_uri, "mailto:"))
-        sokoke_widget_copy_clipboard (widget, view->link_uri + 7);
+        sokoke_widget_copy_clipboard (widget, view->link_uri + 7, NULL, NULL);
     else
-        sokoke_widget_copy_clipboard (widget, view->link_uri);
+        sokoke_widget_copy_clipboard (widget, view->link_uri, NULL, NULL);
 }
 
 static void
@@ -2291,12 +2291,73 @@ midori_web_view_menu_image_new_tab_activate_cb (GtkWidget*  widget,
     g_free (uri);
 }
 
+static GString*
+midori_view_get_data_for_uri (MidoriView*  view,
+                              const gchar* uri)
+{
+    WebKitWebView* web_view = WEBKIT_WEB_VIEW (view->web_view);
+    WebKitWebFrame* frame = webkit_web_view_get_main_frame (web_view);
+    WebKitWebDataSource* data_source = webkit_web_frame_get_data_source (frame);
+    GList* resources = webkit_web_data_source_get_subresources (data_source);
+    GList* list;
+    GString* result;
+
+    for (list = resources; list; list = g_list_next (list))
+    {
+        WebKitWebResource* resource = WEBKIT_WEB_RESOURCE (list->data);
+        GString* data = webkit_web_resource_get_data (resource);
+        if (!g_strcmp0 (webkit_web_resource_get_uri (resource), uri))
+        {
+            result = data;
+            break;
+        }
+    }
+    g_list_free (resources);
+    return result;
+}
+
+static void
+midori_view_clipboard_get_image_cb (GtkClipboard*     clipboard,
+                                    GtkSelectionData* selection_data,
+                                    guint             info,
+                                    gpointer          user_data)
+{
+    MidoriView* view = MIDORI_VIEW (g_object_get_data (user_data, "view"));
+    WebKitHitTestResult* hit_test = user_data;
+    gchar* uri = katze_object_get_string (hit_test, "image-uri");
+    /* if (gtk_selection_data_targets_include_image (selection_data, TRUE)) */
+    {
+        GString* data = midori_view_get_data_for_uri (view, uri);
+        if (data != NULL)
+        {
+            GInputStream* stream = g_memory_input_stream_new_from_data (data->str, data->len, NULL);
+            GError* error = NULL;
+            GdkPixbuf* pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, &error);
+            g_object_unref (stream);
+            if (error != NULL)
+            {
+                g_critical ("Error copying pixbuf: %s\n", error->message);
+                g_error_free (error);
+            }
+            gtk_selection_data_set_pixbuf (selection_data, pixbuf);
+            g_object_unref (pixbuf);
+        }
+        else
+            g_warn_if_reached ();
+    }
+    /* if (gtk_selection_data_targets_include_text (selection_data)) */
+        gtk_selection_data_set_text (selection_data, uri, -1);
+    g_free (uri);
+}
+
 static void
 midori_web_view_menu_image_copy_activate_cb (GtkWidget*  widget,
                                              MidoriView* view)
 {
     gchar* uri = katze_object_get_string (view->hit_test, "image-uri");
-    sokoke_widget_copy_clipboard (widget, uri);
+    g_object_set_data (G_OBJECT (view->hit_test), "view", view);
+    sokoke_widget_copy_clipboard (widget,
+        uri, midori_view_clipboard_get_image_cb, view->hit_test);
     g_free (uri);
 }
 
@@ -2323,7 +2384,7 @@ midori_web_view_menu_video_copy_activate_cb (GtkWidget*  widget,
                                              MidoriView* view)
 {
     gchar* uri = katze_object_get_string (view->hit_test, "media-uri");
-    sokoke_widget_copy_clipboard (widget, uri);
+    sokoke_widget_copy_clipboard (widget, uri, NULL, NULL);
     g_free (uri);
 }
 
@@ -2398,7 +2459,7 @@ static void
 midori_web_view_menu_copy_activate_cb (GtkWidget*  widget,
                                        MidoriView* view)
 {
-    sokoke_widget_copy_clipboard (widget, view->selected_text);
+    sokoke_widget_copy_clipboard (widget, view->selected_text, NULL, NULL);
 }
 
 static void
@@ -2689,7 +2750,7 @@ midori_view_populate_popup (MidoriView* view,
             _("Open _Image in New Tab"), STOCK_TAB_NEW,
             G_CALLBACK (midori_web_view_menu_image_new_tab_activate_cb), widget);
         midori_view_insert_menu_item (menu_shell, -1,
-            _("Copy Image _Address"), NULL,
+            _("Copy Im_age"), NULL,
             G_CALLBACK (midori_web_view_menu_image_copy_activate_cb), widget);
         midori_view_insert_menu_item (menu_shell, -1,
             _("Save I_mage"), GTK_STOCK_SAVE,
index 5ef53d4ecf701603ddecf103327254b4dfb9e457..8efc18483dfc4f6303eac5575b5e98a03c3106d2 100644 (file)
@@ -1209,17 +1209,43 @@ sokoke_register_privacy_item (const gchar* name,
     return NULL;
 }
 
+static void
+sokoke_widget_clipboard_owner_clear_func (GtkClipboard* clipboard,
+                                          gpointer      user_data)
+{
+    g_object_unref (user_data);
+}
+
 void
-sokoke_widget_copy_clipboard (GtkWidget*   widget,
-                              const gchar* text)
+sokoke_widget_copy_clipboard (GtkWidget*          widget,
+                              const gchar*        text,
+                              GtkClipboardGetFunc get_cb,
+                              gpointer            owner)
 {
     GdkDisplay* display = gtk_widget_get_display (widget);
     GtkClipboard* clipboard;
 
-    clipboard = gtk_clipboard_get_for_display (display, GDK_SELECTION_CLIPBOARD);
-    gtk_clipboard_set_text (clipboard, text ? text : "", -1);
+    g_return_if_fail (text != NULL);
+
     clipboard = gtk_clipboard_get_for_display (display, GDK_SELECTION_PRIMARY);
-    gtk_clipboard_set_text (clipboard, text ? text : "", -1);
+    gtk_clipboard_set_text (clipboard, text, -1);
+
+    clipboard = gtk_clipboard_get_for_display (display, GDK_SELECTION_CLIPBOARD);
+    if (get_cb == NULL)
+        gtk_clipboard_set_text (clipboard, text, -1);
+    else
+    {
+        GtkTargetList* target_list = gtk_target_list_new (NULL, 0);
+        GtkTargetEntry* targets;
+        gint n_targets;
+        gtk_target_list_add_text_targets (target_list, 0);
+        gtk_target_list_add_image_targets (target_list, 0, TRUE);
+        targets = gtk_target_table_new_from_list (target_list, &n_targets);
+        gtk_clipboard_set_with_owner (clipboard, targets, n_targets, get_cb,
+            sokoke_widget_clipboard_owner_clear_func, owner);
+        gtk_target_table_free (targets, n_targets);
+        gtk_target_list_unref (target_list);
+    }
 }
 
 static gboolean
index 1f003456cad2ab2908fcbae1370e45df55c131a8..b322f4f800e995c1ee099d8a3204ba3f2fc739a4 100644 (file)
@@ -134,8 +134,10 @@ sokoke_register_privacy_item (const gchar* name,
                               GCallback    clear);
 
 void
-sokoke_widget_copy_clipboard (GtkWidget*   widget,
-                              const gchar* text);
+sokoke_widget_copy_clipboard (GtkWidget*          widget,
+                              const gchar*        text,
+                              GtkClipboardGetFunc get_cb,
+                              gpointer            owner);
 
 GtkWidget*
 sokoke_search_entry_new               (const gchar*        placeholder_text);