← Back to team overview

simple-scan-team team mailing list archive

[Merge] lp:~victor-mireyev/simple-scan/483393 into lp:simple-scan

 

Victor Mireyev has proposed merging lp:~victor-mireyev/simple-scan/483393 into lp:simple-scan.

Requested reviews:
  Robert Ancell (robert-ancell)
Related bugs:
  Bug #483393 in Simple Scan: "Allow drag and drop of pages to external programs"
  https://bugs.launchpad.net/simple-scan/+bug/483393

For more details, see:
https://code.launchpad.net/~victor-mireyev/simple-scan/483393/+merge/108289

Allow drag and drop of pages to external programs.
-- 
https://code.launchpad.net/~victor-mireyev/simple-scan/483393/+merge/108289
Your team Simple Scan Development Team is subscribed to branch lp:simple-scan.
=== modified file 'src/book-view.vala'
--- src/book-view.vala	2012-02-07 02:45:32 +0000
+++ src/book-view.vala	2012-06-01 06:17:19 +0000
@@ -588,4 +588,9 @@
         else
             return null;
     }
+    
+    public Gtk.Widget get_event_source ()
+    {
+        return drawing_area;
+    }
 }

=== modified file 'src/ui.vala'
--- src/ui.vala	2012-05-31 22:54:11 +0000
+++ src/ui.vala	2012-06-01 06:17:19 +0000
@@ -61,6 +61,7 @@
 
     private Gtk.FileChooserDialog? save_dialog;
     private ProgressBarDialog progress_dialog;
+    private DragAndDropHandler dnd_handler = null;
 
     private bool have_error;
     private string error_title;
@@ -773,28 +774,6 @@
         updating_page_menu = false;
     }
 
-    // FIXME: Duplicated from simple-scan.vala
-    private string? get_temporary_filename (string prefix, string extension)
-    {
-        /* NOTE: I'm not sure if this is a 100% safe strategy to use g_file_open_tmp(), close and
-         * use the filename but it appears to work in practise */
-
-        var filename = "%sXXXXXX.%s".printf (prefix, extension);
-        string path;
-        try
-        {
-            var fd = FileUtils.open_tmp (filename, out path);
-            Posix.close (fd);
-        }
-        catch (Error e)
-        {
-            warning ("Error saving email attachment: %s", e.message);
-            return null;
-        }
-
-        return path;
-    }
-
     private void show_page_cb (BookView view, Page page)
     {
         var path = get_temporary_filename ("scanned-page", "tiff");
@@ -1425,6 +1404,8 @@
         
         progress_dialog = new ProgressBarDialog (window, _("Saving document..."));
         book.saving.connect (book_saving_cb);
+        
+        dnd_handler = new DragAndDropHandler(book_view);
     }
     
     private void book_saving_cb (int page_number)
@@ -1536,3 +1517,98 @@
         bar.set_text (message);
     }
 }
+
+class DragAndDropHandler
+{
+    private enum TargetType
+    {
+        IMAGE,
+        URI
+    }
+    
+    private BookView book_view;
+    
+    public DragAndDropHandler (BookView book_view)
+    {
+        this.book_view = book_view;
+        var event_source = book_view.get_event_source ();
+        
+        set_targets (event_source);
+        event_source.drag_data_get.connect (on_drag_data_get);
+    }
+    
+    private void set_targets (Gtk.Widget event_source)
+    {
+        var table = new Gtk.TargetEntry [0];
+        var targets = new Gtk.TargetList (table);
+        targets.add_uri_targets (TargetType.URI);
+        targets.add_image_targets (TargetType.IMAGE, true);
+        
+        Gtk.drag_source_set (event_source, Gdk.ModifierType.BUTTON1_MASK, table, Gdk.DragAction.COPY);
+        Gtk.drag_source_set_target_list (event_source, targets);
+    }
+    
+    private void on_drag_data_get (Gdk.DragContext context, Gtk.SelectionData selection,
+        uint target_type, uint time)
+    {
+        var page = book_view.get_selected ();
+        return_if_fail (page != null);
+
+        switch (target_type)
+        {
+        case TargetType.IMAGE:
+            var image = page.get_image (true);
+            selection.set_pixbuf (image);
+            
+            debug ("Saving page to pixbuf");
+        break;
+        
+        case TargetType.URI:
+            var filetype = "png";
+            var path = get_temporary_filename ("scanned-page", filetype);
+            return_if_fail (path != null);
+            
+            var file = File.new_for_path (path);
+            var uri = file.get_uri ();
+
+            try
+            {
+                page.save (filetype, file);
+                selection.set_uris ({ uri });
+                debug ("Saving page to %s", uri);
+            }
+            catch (Error e)
+            {
+                warning ("Unable to save file using drag-drop %s", e.message);
+            }
+        break;
+        
+        default:
+            warning ("Invalid DND target type %u", target_type);
+        break;
+        }
+    }
+}
+
+// FIXME: Duplicated from simple-scan.vala
+private string? get_temporary_filename (string prefix, string extension)
+{
+    /* NOTE: I'm not sure if this is a 100% safe strategy to use g_file_open_tmp(), close and
+     * use the filename but it appears to work in practise */
+
+    var filename = "%sXXXXXX.%s".printf (prefix, extension);
+    string path;
+    try
+    {
+        var fd = FileUtils.open_tmp (filename, out path);
+        Posix.close (fd);
+    }
+    catch (Error e)
+    {
+        warning ("Error saving email attachment: %s", e.message);
+        return null;
+    }
+
+    return path;
+}
+


Follow ups