[simple-scan] Refactor emailing documents



commit aff678b9a524bdaf458dceabd0a7ff106cabb366
Author: Stéphane Fillion <stphanef3724 gmail com>
Date:   Sun May 21 23:24:46 2017 -0400

    Refactor emailing documents
    
    AppWindow.email_document now save using Book.save everytime in
    preparation of making saves asynchronous. Files attached to email now
    got nicer filenames. Solve bug 782151
    (https://bugzilla.gnome.org/show_bug.cgi?id=782151).

 src/app-window.vala |   82 +++++++++++++++++++++++++--------------------------
 1 files changed, 40 insertions(+), 42 deletions(-)
---
diff --git a/src/app-window.vala b/src/app-window.vala
index 3d73454..663fd61 100644
--- a/src/app-window.vala
+++ b/src/app-window.vala
@@ -1159,58 +1159,56 @@ public class AppWindow : Gtk.ApplicationWindow
 
     private void email_document ()
     {
-        var saved = false;
-        var command_line = "xdg-email";
+        show_progress_dialog ();
 
-        /* Save text files as PDFs */
-        if (document_hint == "text")
+        string type = (document_hint == "text") ? "pdf" : "jpeg";
+        string command_line = null;
+        string path;
+        bool file_exist = true;
+        File file = null;
+
+        do
         {
-            /* Open a temporary file */
-            var path = get_temporary_filename ("scan", "pdf");
-            if (path != null)
+
+            /* get temporary filename */
+            file_exist = false;
+            path = get_temporary_filename (/* base filename of images attached to email */
+                                           _("scan"), type);
+            file = File.new_for_path (path);
+            command_line = "xdg-email";
+
+            if (type == "pdf")
             {
-                var file = File.new_for_path (path);
-                show_progress_dialog ();
-                try
-                {
-                    book.save ("pdf", 0, file);
-                }
-                catch (Error e)
-                {
-                    hide_progress_dialog ();
-                    warning ("Unable to save email file: %s", e.message);
-                    return;
-                }
+                /* prepare command line */
                 command_line += " --attach %s".printf (path);
             }
-        }
-        else
-        {
-            for (var i = 0; i < book.n_pages; i++)
+            else /* (type == "jpeg") */
             {
-                var path = get_temporary_filename ("scan", "jpg");
-                if (path == null)
-                {
-                    saved = false;
-                    break;
-                }
-
-                var file = File.new_for_path (path);
-                try
+                /* make sure files doesn't exist and prepare command line */
+                for (var i = 0; i < book.n_pages; i++)
                 {
-                    book.get_page (i).save ("jpeg", settings.get_int ("jpeg-quality"), file);
-                }
-                catch (Error e)
-                {
-                    warning ("Unable to save email file: %s", e.message);
-                    return;
+                    var indexed_file = book.make_indexed_file (file.get_uri (), i);
+                    if (indexed_file.query_exists ())
+                    {
+                        file_exist = true;
+                        break;
+                    }
+                    command_line += " --attach %s".printf (indexed_file.get_path ());
                 }
-                command_line += " --attach %s".printf (path);
-
-                if (!saved)
-                    break;
             }
         }
+        while (file_exist);
+
+        try
+        {
+            book.save (type, settings.get_int ("jpeg-quality"), file);
+        }
+        catch (Error e)
+        {
+            hide_progress_dialog ();
+            warning ("Unable to save email file: %s", e.message);
+            return;
+        }
 
         debug ("Launching email client: %s", command_line);
         try


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]