Explorar o código

ajout d'un controles avant la sauvegradre de la config du backup

olinox14 %!s(int64=9) %!d(string=hai) anos
pai
achega
58c4520b78

BIN=BIN
.vs/barry/v14/.suo


+ 11 - 1
barry/MainWindow.xaml.cs

@@ -15,6 +15,7 @@ using System.Windows.Shapes;
 using System.Threading;
 using System.Windows.Threading;
 using System.Diagnostics;
+using System.IO;
 
 namespace barry
 {
@@ -39,7 +40,16 @@ namespace barry
             
             print("**** BARRY Backup ****");
 
-            bkp = methods.load_this_cpu();
+            try
+            {
+                bkp = methods.load_this_cpu();
+            }
+            catch (DirectoryNotFoundException e)
+            {
+                bkp = null;
+                MessageBox.Show("Error while loading the backup: " + e.Data);
+            }
+
             if (bkp == null)
             {
                 print(">> unknown machine");

+ 2 - 1
barry/OptionsWindow.xaml

@@ -12,7 +12,7 @@
         <TextBox Name="txt_path" HorizontalAlignment="Left" Height="23" Margin="10,28,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="323" TextChanged="txt_path_TextChanged"/>
 
         <Label Content="Sub-directory of the USB Device" HorizontalAlignment="Left" Margin="10,56,0,0" VerticalAlignment="Top" Grid.RowSpan="2"/>
-        <TextBox Name="txt_subdir" HorizontalAlignment="Left" Height="23" Margin="10,11,0,0" Grid.Row="1" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="323" TextChanged="txt_subdir_TextChanged"/>
+        <TextBox Name="txt_subdir" HorizontalAlignment="Left" Height="23" Margin="49,11,0,0" Grid.Row="1" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="284" TextChanged="txt_subdir_TextChanged"/>
 
         <Label Content="What should barry do:" HorizontalAlignment="Left" Margin="10,52,0,0" Grid.Row="1" VerticalAlignment="Top"/>
 
@@ -28,6 +28,7 @@
         <Button Name="btn_cancel" Content="Cancel" HorizontalAlignment="Left" Margin="49,201,0,0" Grid.Row="1" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.413,2.136" Click="btn_cancel_Click"/>
         <Button x:Name="btn_explorecpu" Content="..." HorizontalAlignment="Left" Margin="338,28,0,0" VerticalAlignment="Top" Width="33" Height="23" Click="btn_explorecpu_Click"/>
         <Button x:Name="btn_exploredevice" Content="..." HorizontalAlignment="Left" Margin="338,11,0,0" VerticalAlignment="Top" Width="33" Height="23" Grid.Row="1" RenderTransformOrigin="0.303,0.435" Click="btn_exploredevice_Click"/>
+        <Label x:Name="lbl_drive" Content="\\" HorizontalAlignment="Left" Margin="10,8,0,0" Grid.Row="1" VerticalAlignment="Top" Width="34"/>
 
     </Grid>
 </Window>

+ 9 - 60
barry/OptionsWindow.xaml.cs

@@ -34,6 +34,7 @@ namespace barry
 
             bkp = current_bkp;
 
+            lbl_drive.Content = methods.current_drive_letter();
             txt_path.Text = bkp.dir_path;
             txt_subdir.Text = bkp.local_subdir;
             for (int i = 0; i < cb_bkptype.Items.Count; i++)
@@ -48,6 +49,12 @@ namespace barry
 
         private void btn_save_Click(object sender, RoutedEventArgs e)
         {
+            string diag = bkp.checkup();
+            if (diag.Length > 0)
+            {
+                MessageBox.Show(diag, "Check-Up", MessageBoxButton.OK, MessageBoxImage.Exclamation);
+                return;
+            }
             methods.save_this_cpu(bkp);
             Close();
         }
@@ -92,72 +99,14 @@ namespace barry
 
         private void btn_explorecpu_Click(object sender, RoutedEventArgs e)
         {
-            var dlg = new CommonOpenFileDialog();
-            dlg.Title = "Select a directory on your computer";
-            dlg.IsFolderPicker = true;
-            dlg.InitialDirectory = "";
-            dlg.EnsurePathExists = true;
-            dlg.ShowPlacesList = true;
-
-            if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
-            {
-                var folder = dlg.FileName;
-                Console.WriteLine(folder + " - " + methods.current_drive_letter() + " - " + is_subdir(folder, methods.current_drive_letter()));
-                if (is_subdir(folder, methods.current_drive_letter()))
-                {
-                    MessageBox.Show("This directory should NOT be on your usb device", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
-                }
-                else
-                {
-                    txt_path.Text = folder;
-                }
-            }
+            txt_path.Text = methods.select_directory_dialog("Select a directory on your computer");
         }
 
         private void btn_exploredevice_Click(object sender, RoutedEventArgs e)
         {
-            var dlg = new CommonOpenFileDialog();
-            dlg.Title = "Select a directory on your device";
-            dlg.IsFolderPicker = true;
-            dlg.InitialDirectory = "";
-            dlg.EnsurePathExists = true;
-            dlg.ShowPlacesList = true;
-
-            if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
-            {
-                var folder = dlg.FileName;
-                if (!is_subdir(folder, methods.current_drive_letter()))
-                {
-                    MessageBox.Show("This directory should be on your usb device", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
-                }
-                else
-                {
-                    txt_subdir.Text = folder;
-                }
-            }
-
-
+            txt_subdir.Text = methods.select_directory_dialog("Select a directory on your device");
         }
 
-        /// <summary>
-        /// is dir1 a subdir of dir2
-        /// </summary>
-        /// <param name="dir1"></param>
-        /// <param name="dir2"></param>
-        private bool is_subdir(string dir1, string dir2)
-        {
-            DirectoryInfo di1 = new DirectoryInfo(dir1);
-            DirectoryInfo di2 = new DirectoryInfo(dir2);
-            while (di1.Parent != null)
-            {
-                if (di1.Parent.FullName == di2.FullName)
-                {
-                    return true;
-                }
-                else di1 = di1.Parent;
-            }
-            return false;
-        }
 
     }
 }

+ 12 - 9
barry/bin/Debug/barry.dat

@@ -1,29 +1,32 @@
 {
   "00359-OEM-8703996-83031": {
     "machine_guid": "00359-OEM-8703996-83031",
-    "dir_path": "e:\\test\\",
-    "local_subdir": "projets\\",
     "bkp_type": 1,
     "last_bkp_date": "Never",
     "last_bkp_result": "",
-    "last_bkp_target": ""
+    "last_bkp_target": "",
+    "dir_path": "e:\\test\\",
+    "local_subdir": "projets\\",
+    "local_subdir_path": "O:\\projets\\"
   },
   "55041-007-1341865-86915": {
     "machine_guid": "55041-007-1341865-86915",
-    "dir_path": "C:\\Users\\olivier.massot\\Documents\\barry_test\\",
-    "local_subdir": "apps\\portable\\VLCPortable\\",
     "bkp_type": 1,
     "last_bkp_date": "Never",
     "last_bkp_result": "",
-    "last_bkp_target": ""
+    "last_bkp_target": "",
+    "dir_path": "C:\\Users\\olivier.massot\\Documents\\barry_test\\",
+    "local_subdir": "apps\\portable\\VLCPortable\\",
+    "local_subdir_path": "O:\\apps\\portable\\VLCPortable\\"
   },
   "00359-OEM-8992687-00006": {
     "machine_guid": "00359-OEM-8992687-00006",
-    "dir_path": "a:\\barry_bkp\\",
-    "local_subdir": "",
     "bkp_type": 1,
     "last_bkp_date": "Never",
     "last_bkp_result": "",
-    "last_bkp_target": ""
+    "last_bkp_target": "",
+    "dir_path": "a:\\barry_bkp\\",
+    "local_subdir": "",
+    "local_subdir_path": "O:\\"
   }
 }

BIN=BIN
barry/bin/Debug/barry.exe


BIN=BIN
barry/bin/Debug/barry.pdb


+ 74 - 3
barry/core/BackupConfig.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.IO;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
@@ -8,14 +9,48 @@ namespace barry
 {
     public class BackupConfig
     {
+        Methods methods = new Methods();
+
         // ID
         public string machine_guid;
 
         // target or source of the backup (it is a directory on the local computer) 
-        public string dir_path;
+        private string _dir_path;
+        public string dir_path
+        {
+            get
+            {
+                return _dir_path;
+            }
+            set
+            {
+                _dir_path = value;
+            }
+        }
 
         // (optional) a sub-diretory of the current device
-        public string local_subdir = "";
+        private string _local_subdir = "";
+        public string local_subdir
+        {
+            get
+            {
+                return _local_subdir;
+            }
+            set
+            {
+                _local_subdir = value;
+            }
+        }
+
+        public string local_subdir_path
+        {
+            get
+            {
+                return Path.Combine(methods.current_drive_letter(), _local_subdir);
+            }
+        }
+        
+
 
         // operation type
         // 1 for a backup of the device on the computer (default)
@@ -35,7 +70,7 @@ namespace barry
         /// <returns></returns>
         public string describe()
         {
-            Methods methods = new Methods();
+            
             string msg = "";
             if (bkp_type == 0)
             {
@@ -58,6 +93,42 @@ namespace barry
             return msg;
         }
 
+        /// <summary>
+        /// return an empty string if the current configration is valid
+        /// a list of problems if not
+        /// </summary>
+        /// <returns></returns>
+        public string checkup()
+        {
+            if (machine_guid != methods.get_machine_guid())
+            {
+                return "Checkup: Barry need to be on the good computer to check-up this config";
+            }
+
+            if (!Directory.Exists(dir_path))
+            {
+                return "This directory does not exists: " + dir_path;
+            }
+            else if (methods.is_subdir(dir_path, methods.current_drive_letter()))
+            {
+                //"This directory should NOT be on your usb device"
+                //throw new ArgumentException();
+                return "This directory should NOT be on your usb device: " + dir_path;
+            }
+
+            if (!Directory.Exists(local_subdir_path))
+            {
+                return "This directory does not exists: " + local_subdir_path;
+            }
+            else if (local_subdir.Length > 0 && !methods.is_subdir(local_subdir_path, methods.current_drive_letter()))
+            {
+                //"This directory should be on your usb device"
+                return "This directory should be on your usb device: " + local_subdir_path;
+            }
+            return "";
+        }
+
+
     }
 
 }

+ 43 - 0
barry/core/methods.cs

@@ -10,6 +10,7 @@ using Newtonsoft.Json;
 using System.IO;
 using System.Windows;
 using System.Windows.Threading;
+using Microsoft.WindowsAPICodePack.Dialogs;
 
 namespace barry
 {
@@ -214,6 +215,48 @@ namespace barry
             barry_config[get_machine_guid()] = bkp;
             save_config_to_file(barry_config);
         }
+
+        /// <summary>
+        /// returns true if dir1 is a subdir of dir2
+        /// </summary>
+        /// <param name="dir1"></param>
+        /// <param name="dir2"></param>
+        public bool is_subdir(string dir1, string dir2)
+        {
+            DirectoryInfo di1 = new DirectoryInfo(dir1);
+            DirectoryInfo di2 = new DirectoryInfo(dir2);
+            while (di1.Parent != null)
+            {
+                if (di1.Parent.FullName == di2.FullName)
+                {
+                    return true;
+                }
+                else di1 = di1.Parent;
+            }
+            return false;
+        }
+
+        public string select_directory_dialog(string title = "Select a directory", string initial_dir = "")
+        {
+            var dlg = new CommonOpenFileDialog();
+            dlg.Title = title;
+            dlg.IsFolderPicker = true;
+            dlg.InitialDirectory = initial_dir;
+            dlg.EnsurePathExists = true;
+            dlg.ShowPlacesList = true;
+
+            if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
+            {
+                var folder = dlg.FileName;
+                return (string)folder;
+            }
+            return "";
+        }
+
+
+
+
+
     }
 
 

BIN=BIN
barry/obj/Debug/OptionsWindow.baml


+ 12 - 1
barry/obj/Debug/OptionsWindow.g.cs

@@ -1,4 +1,4 @@
-#pragma checksum "..\..\OptionsWindow.xaml" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "5708FA111ED630C1E493A69993002E22"
+#pragma checksum "..\..\OptionsWindow.xaml" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "4A7714C6B9B4E66560C8074CA4C8382B"
 //------------------------------------------------------------------------------
 // <auto-generated>
 //     Ce code a été généré par un outil.
@@ -103,6 +103,14 @@ namespace barry {
         #line default
         #line hidden
         
+        
+        #line 31 "..\..\OptionsWindow.xaml"
+        [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
+        internal System.Windows.Controls.Label lbl_drive;
+        
+        #line default
+        #line hidden
+        
         private bool _contentLoaded;
         
         /// <summary>
@@ -199,6 +207,9 @@ namespace barry {
             #line default
             #line hidden
             return;
+            case 9:
+            this.lbl_drive = ((System.Windows.Controls.Label)(target));
+            return;
             }
             this._contentLoaded = true;
         }

+ 12 - 1
barry/obj/Debug/OptionsWindow.g.i.cs

@@ -1,4 +1,4 @@
-#pragma checksum "..\..\OptionsWindow.xaml" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "5708FA111ED630C1E493A69993002E22"
+#pragma checksum "..\..\OptionsWindow.xaml" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "4A7714C6B9B4E66560C8074CA4C8382B"
 //------------------------------------------------------------------------------
 // <auto-generated>
 //     Ce code a été généré par un outil.
@@ -103,6 +103,14 @@ namespace barry {
         #line default
         #line hidden
         
+        
+        #line 31 "..\..\OptionsWindow.xaml"
+        [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
+        internal System.Windows.Controls.Label lbl_drive;
+        
+        #line default
+        #line hidden
+        
         private bool _contentLoaded;
         
         /// <summary>
@@ -199,6 +207,9 @@ namespace barry {
             #line default
             #line hidden
             return;
+            case 9:
+            this.lbl_drive = ((System.Windows.Controls.Label)(target));
+            return;
             }
             this._contentLoaded = true;
         }

BIN=BIN
barry/obj/Debug/barry.exe


BIN=BIN
barry/obj/Debug/barry.g.resources


BIN=BIN
barry/obj/Debug/barry.pdb