[chronojump] report save 1st to temp (avoid problems if previously opened a windows browser)



commit 65674f139d8683da87c0aa3e0da846352b811697
Author: Xavier de Blas <xaviblas gmail com>
Date:   Thu Feb 15 14:20:15 2018 +0100

    report save 1st to temp (avoid problems if previously opened a windows browser)

 src/exportSession.cs |   59 +++++++++++++++++++++++++++++++------------------
 1 files changed, 37 insertions(+), 22 deletions(-)
---
diff --git a/src/exportSession.cs b/src/exportSession.cs
index c40d3e4..31bd14b 100644
--- a/src/exportSession.cs
+++ b/src/exportSession.cs
@@ -98,26 +98,15 @@ public class ExportSession
                                //add ".csv" if needed
                                fileName = Util.AddCsvIfNeeded(fileName);
                        }
-                       try {
-                               if (File.Exists(fileName)) {
-                                       LogB.Warning(string.Format("File {0} exists with attributes {1}, 
created at {2}", 
-                                                               fileName, File.GetAttributes(fileName), 
File.GetCreationTime(fileName)));
-                                       LogB.Information("Overwrite...");
-                                       ConfirmWindow confirmWin = ConfirmWindow.Show(Catalog.GetString("Are 
you sure you want to overwrite file: "), "", fileName);
-                                       confirmWin.Button_accept.Clicked += new 
EventHandler(on_overwrite_file_accepted);
-                               } else {
-                                       writer = File.CreateText(fileName);
-                                       getData();
-                                       printData();
-                                       closeWriter();
-
-                                       string myString = string.Format(Catalog.GetString("Saved to {0}"), 
fileName) + spreadsheetString;
-                                       new DialogMessage(Constants.MessageTypes.INFO, myString);
-                               }
-                       } 
-                       catch {
-                               string myString = string.Format(Catalog.GetString("Cannot export to file {0} 
"), fileName);
-                               new DialogMessage(Constants.MessageTypes.WARNING, myString);
+
+                       if (File.Exists(fileName)) {
+                               LogB.Warning(string.Format("File {0} exists with attributes {1}, created at 
{2}",
+                                                       fileName, File.GetAttributes(fileName), 
File.GetCreationTime(fileName)));
+                               LogB.Information("Overwrite...");
+                               ConfirmWindow confirmWin = ConfirmWindow.Show(Catalog.GetString("Are you sure 
you want to overwrite file: "), "", fileName);
+                               confirmWin.Button_accept.Clicked += new 
EventHandler(on_overwrite_file_accepted);
+                       } else {
+                               writeFile();
                        }
                }
                else {
@@ -138,11 +127,37 @@ public class ExportSession
 
        private void on_overwrite_file_accepted(object o, EventArgs args)
        {
-               writer = File.CreateText(fileName);
+               writeFile();
+       }
+
+       private void writeFile()
+       {
+               // 1) create temp file to not have problems with a previously opened html on windows browser
+               string tempfile = Path.GetTempFileName();
+               try {
+                       writer = File.CreateText(tempfile);
+               } catch {
+                       LogB.Information("Couldn't create tempfile: " + tempfile);
+                       new DialogMessage( Constants.MessageTypes.WARNING,
+                                       string.Format(Catalog.GetString("Cannot export to file {0} "), 
tempfile) );
+                       return;
+               }
+
                getData();
                printData();
                closeWriter();
-                               
+
+               // 2) copy temp file to user destination
+               try {
+                       File.Copy(tempfile, fileName, true); //can be overwritten
+               } catch {
+                       LogB.Information("Couldn't copy to: " + fileName);
+                       new DialogMessage( Constants.MessageTypes.WARNING,
+                                       string.Format(Catalog.GetString("Cannot export to file {0} "), 
fileName) );
+                       return;
+               }
+
+               // 3) show message
                string myString = string.Format(Catalog.GetString("Saved to {0}"), fileName) + 
spreadsheetString;
                new DialogMessage(Constants.MessageTypes.INFO, myString);
        }


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