From f0174a9336841e3c660eedf861e03102a3f8d89c Mon Sep 17 00:00:00 2001 From: Lolle2000la Date: Mon, 19 Feb 2018 18:05:10 +0100 Subject: [PATCH] Fixed crashes when trying to move or skip images, that don't exist. (#13) --- .../Image Sort Installer.vdproj | 12 +- Image sort.Logic/FolderSelector.cs | 2 + Image sort.Logic/ImageSelectorQuery.cs | 7 +- Image sort.Logic/Properties/AssemblyInfo.cs | 4 +- .../Properties/AssemblyInfo.cs | 4 +- Image sort.UI/MainWindow.xaml.cs | 385 ++++++++++-------- Image sort.UI/Properties/AssemblyInfo.cs | 4 +- Image sort.Update/Properties/AssemblyInfo.cs | 4 +- .../Properties/Resources.Designer.cs | 2 +- Image sort.Update/Properties/Resources.resx | 2 +- 10 files changed, 247 insertions(+), 179 deletions(-) diff --git a/Image Sort Installer/Image Sort Installer.vdproj b/Image Sort Installer/Image Sort Installer.vdproj index 2b473279..5468db2d 100644 --- a/Image Sort Installer/Image Sort Installer.vdproj +++ b/Image Sort Installer/Image Sort Installer.vdproj @@ -232,7 +232,7 @@ { "AssemblyRegister" = "3:1" "AssemblyIsInGAC" = "11:FALSE" - "AssemblyAsmDisplayName" = "8:Image sort.Update, Version=1.7.1.0, Culture=neutral, processorArchitecture=MSIL" + "AssemblyAsmDisplayName" = "8:Image sort.Update, Version=1.7.2.0, Culture=neutral, processorArchitecture=MSIL" "ScatterAssemblies" { "_2444875B8A476DC2CCC6570801FD1260" @@ -325,7 +325,7 @@ { "AssemblyRegister" = "3:1" "AssemblyIsInGAC" = "11:FALSE" - "AssemblyAsmDisplayName" = "8:Image sort.Logic, Version=1.7.1.0, Culture=neutral, processorArchitecture=MSIL" + "AssemblyAsmDisplayName" = "8:Image sort.Logic, Version=1.7.2.0, Culture=neutral, processorArchitecture=MSIL" "ScatterAssemblies" { "_A87679A42B444F8385B7C65A7F9ACF32" @@ -356,7 +356,7 @@ { "AssemblyRegister" = "3:1" "AssemblyIsInGAC" = "11:FALSE" - "AssemblyAsmDisplayName" = "8:Image sort.UI.Dialogs, Version=1.7.1.0, Culture=neutral, processorArchitecture=MSIL" + "AssemblyAsmDisplayName" = "8:Image sort.UI.Dialogs, Version=1.7.2.0, Culture=neutral, processorArchitecture=MSIL" "ScatterAssemblies" { "_A87C41C6DEB6942B8A32BC581162B42C" @@ -479,15 +479,15 @@ { "Name" = "8:Microsoft Visual Studio" "ProductName" = "8:Image Sort" - "ProductCode" = "8:{C2320D1A-5531-4750-B73D-EFBC41F373CC}" - "PackageCode" = "8:{206BD599-AE40-440A-8C7A-299A528152F1}" + "ProductCode" = "8:{A732DAB4-9E13-4DDF-928D-2A8A78985A86}" + "PackageCode" = "8:{5D3D18BB-3DB0-4FF6-AD06-67C2F8EBBCB0}" "UpgradeCode" = "8:{1AC9D063-74DA-4D56-BA4A-C7F4216CFAF3}" "AspNetVersion" = "8:4.0.30319.0" "RestartWWWService" = "11:FALSE" "RemovePreviousVersions" = "11:TRUE" "DetectNewerInstalledVersion" = "11:TRUE" "InstallAllUsers" = "11:FALSE" - "ProductVersion" = "8:1.7.2" + "ProductVersion" = "8:1.7.3" "Manufacturer" = "8:Lolle2000la" "ARPHELPTELEPHONE" = "8:" "ARPHELPLINK" = "8:" diff --git a/Image sort.Logic/FolderSelector.cs b/Image sort.Logic/FolderSelector.cs index 68411f78..23065dbf 100644 --- a/Image sort.Logic/FolderSelector.cs +++ b/Image sort.Logic/FolderSelector.cs @@ -140,6 +140,8 @@ public void MoveFileTo(string source,string destination) try { + // Only run, if there is an existing file, that has been given back. + if (File.Exists(source)) // Making sure the file doesn't already exist at destination if (!File.Exists(destination)) { diff --git a/Image sort.Logic/ImageSelectorQuery.cs b/Image sort.Logic/ImageSelectorQuery.cs index 4ae0b96f..09a7446d 100644 --- a/Image sort.Logic/ImageSelectorQuery.cs +++ b/Image sort.Logic/ImageSelectorQuery.cs @@ -302,7 +302,12 @@ public string GetImagePath() imagePathPool.Dequeue(); } - return imagePathPool.Dequeue(); + if (imagePathPool.Count > 0) + // SUCCESS + return imagePathPool.Dequeue(); + else + // FAILURE + return ""; } /// diff --git a/Image sort.Logic/Properties/AssemblyInfo.cs b/Image sort.Logic/Properties/AssemblyInfo.cs index 0f2978d7..2b22c795 100644 --- a/Image sort.Logic/Properties/AssemblyInfo.cs +++ b/Image sort.Logic/Properties/AssemblyInfo.cs @@ -33,7 +33,7 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.7.2.0")] -[assembly: AssemblyFileVersion("1.7.2.0")] +[assembly: AssemblyVersion("1.7.3.0")] +[assembly: AssemblyFileVersion("1.7.3.0")] [assembly: NeutralResourcesLanguage("en-US")] diff --git a/Image sort.UI.Dialogs/Properties/AssemblyInfo.cs b/Image sort.UI.Dialogs/Properties/AssemblyInfo.cs index 40021c3c..38d1a437 100644 --- a/Image sort.UI.Dialogs/Properties/AssemblyInfo.cs +++ b/Image sort.UI.Dialogs/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.7.2.0")] -[assembly: AssemblyFileVersion("1.7.2.0")] +[assembly: AssemblyVersion("1.7.3.0")] +[assembly: AssemblyFileVersion("1.7.3.0")] diff --git a/Image sort.UI/MainWindow.xaml.cs b/Image sort.UI/MainWindow.xaml.cs index 4f701e8f..065723fb 100644 --- a/Image sort.UI/MainWindow.xaml.cs +++ b/Image sort.UI/MainWindow.xaml.cs @@ -113,64 +113,8 @@ public MainWindow() /* METHODS */ /* */ /*********************************************************************/ - - /// - /// Loads the folder at the path given, and then adds the folders in that folder - /// to the selection in the - /// - /// The folder that should be selected/loaded. - /// - /// Indicates, whether the main window should be hidden during the loading process. - /// - private async void LoadFolderAsync(string folder, bool hideMainWindow=false) - { - if (Directory.Exists(folder)) - { - // Hides the main window before loading. - if (hideMainWindow) - Hide(); - - // Do what ever with the value - await SelectAndLoadFolder(folder); - - // Refresh folders - AddFoldersToFoldersStack(); - - // Shows the main window after loading is complete. - if (hideMainWindow) - Show(); - } - } - - - /// - /// Loads an image into the window - /// - /// The that should be displayed - private void LoadImage(BitmapImage image) - { - PreviewImage.Source = image; - } - - /// - /// Gives back, whether any folder is visible or not - /// - public bool IsAnyFolderVisible - { - get - { - // Get every item in the list - foreach (ListBoxItem item in FoldersStack.Items) - { - // if one of them turns out to be visible, then return true - if (item.Visibility == Visibility.Visible) - return true; - } - // Else false - return false; - } - } - + + #region Folder-Selection Management /// /// Shifts up the selected folder, to one that is visible, in the /// @@ -216,44 +160,52 @@ private void MoveFolderSelectionDown() FoldersStack.SelectedIndex = 0; } } + #endregion + #region Folder-Navigation/Loading /// - /// Gives the user to select a (new) folder, - /// only loads other folder if the user wants to, - /// and the path given is valid. + /// Lets the user select a resolution for the loaded images /// - private async void SelectFolder() + public void SetResolution() { - // Creates a dialog for the folder to sort - FolderBrowserDialog folderBrowser = new FolderBrowserDialog() - { - Description = "Which folder needs sorting?", - ShowNewFolderButton = true - }; - - // Shows it and does things if it works out - if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK) + string response = InputBox.Show("Please set the horizontal resolution.\n\n\n" + + "Note: Everything equal or smaller to 0, as well as writing \"default\" reverts the resolution to default (1000),\n" + + "Note: The higher the resolution, the higher the loading times and RAM usage\n\n" + + "Will be applied on next loading.", + "Resolution", Properties.Settings.Default.MaxHorizontalResolution.ToString(), -1, -1); + // Stores the resolution selected by the user + /* Gets the resolution by the user via an input box, returns a bool whether + he inputted a number or not*/ + bool result = int.TryParse(response, out int resolution); + // If he did, continue + if (result) { - // Disable all user input to prevent unwanted behavior - DisableAllControls(); - - // if the folder could not be selected, redo the thing - if (await SelectAndLoadFolder(folderBrowser.SelectedPath) == false) - SelectFolder(); - // otherwise load the image and enable the controls, if there is an image + // if the resolution is higher 0, then save it in the settings file + if (resolution > 0) + { + MaxHorizontalResolution = resolution; + } + // otherwise, revert to default else { - - // Make folders on the left up to date - AddFoldersToFoldersStack(); + MaxHorizontalResolution = 1000; } - - // Enable all controls again to allow for user input - EnableAllControls(); } + // If the response is "" or "default, revert to default + else if (response == "default") + { + MaxHorizontalResolution = 1000; + } + // If nothing was given back, then don't change anything + else if (response == "") + { + // Clear so that nothing happens + } + // If the user did not input valid numbers, than repeat + else + SetResolution(); } - /// /// Selects and loads a folder /// @@ -292,7 +244,61 @@ private async Task SelectAndLoadFolder(string folder) EnableAllControls(); return true; } + } + /// + /// Gives the user to select a (new) folder, + /// only loads other folder if the user wants to, + /// and the path given is valid. + /// + private async void SelectFolder() + { + // Creates a dialog for the folder to sort + FolderBrowserDialog folderBrowser = new FolderBrowserDialog() + { + Description = "Which folder needs sorting?", + ShowNewFolderButton = true + }; + + // Shows it and does things if it works out + if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK) + { + // Disable all user input to prevent unwanted behavior + DisableAllControls(); + + // if the folder could not be selected, ask the user if he wants to retry. + // Also clean-up and give the user only the ability to select another folder. + if (await SelectAndLoadFolder(folderBrowser.SelectedPath) == false) + { + // Clean-Up + FoldersStack.Items.Clear(); + DisableAllControls(); + SelectFolderButton.IsEnabled = true; + ResolutionBox.IsEnabled = true; + + // Ask the user if he wants to retry + if (System.Windows.Forms.MessageBox.Show("Folder could not be opened. " + + "The process was either aborted or the folder at the given destination " + + "can't be accessed.", "Could not open", + MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == System.Windows.Forms.DialogResult.Retry) + SelectFolder(); + + // end the function + return; + } + + + // otherwise load the image and enable the controls, if there is an image + else + { + + // Make folders on the left up to date + AddFoldersToFoldersStack(); + } + + // Enable all controls again to allow for user input + EnableAllControls(); + } } /// @@ -313,20 +319,31 @@ private async void EnterFolder() // if the folder could not be selected, show the user that it couldn't if (await SelectAndLoadFolder(folderToEnter) == false) - System.Windows.Forms.MessageBox.Show("Folder could not be opened." + - " Please check if the folder is working as it should.", - "Could not open folder", MessageBoxButtons.OK, MessageBoxIcon.Error); - //SelectFolder(); + { + // notification + System.Windows.Forms.MessageBox.Show("Folder could not be opened. " + + "The process was either aborted or the folder at the given destination " + + "can't be accessed.", "Could not open", + MessageBoxButtons.OK, MessageBoxIcon.Error); + + // Clean-Up + FoldersStack.Items.Clear(); + DisableAllControls(); + SelectFolderButton.IsEnabled = true; + ResolutionBox.IsEnabled = true; + + } + // otherwise load the image and enable the controls, if there is an image. else { - + // Clearing the search bar after entering the folder, // so that it will be more comfortable searching. SearchBarBox.Text = ""; } - + // Enable all controls again to allow for user input EnableAllControls(); } @@ -335,7 +352,44 @@ private async void EnterFolder() AddFoldersToFoldersStack(); } } + /// + /// Loads the folder at the path given, and then adds the folders in that folder + /// to the selection in the + /// + /// The folder that should be selected/loaded. + /// + /// Indicates, whether the main window should be hidden during the loading process. + /// + private async void LoadFolderAsync(string folder, bool hideMainWindow = false) + { + if (Directory.Exists(folder)) + { + // Hides the main window before loading. + if (hideMainWindow) + Hide(); + + // Load the folder + if(await SelectAndLoadFolder(folder)) + { + // Refresh folders + AddFoldersToFoldersStack(); + } + // if it doesn't work, clean up and only enable opening another one. + else + { + DisableAllControls(); + SelectFolderButton.IsEnabled = true; + ResolutionBox.IsEnabled = true; + } + + // Shows the main window after loading is complete. + if (hideMainWindow) + Show(); + } + } + #endregion + #region Folder Management /// /// Lets the user create a new folder /// @@ -369,6 +423,17 @@ private void NewFolder() } } } + #endregion + + #region Data-Refreshing + /// + /// Loads an image into the window + /// + /// The that should be displayed + private void LoadImage(BitmapImage image) + { + PreviewImage.Source = image; + } /// /// Brings the folders in the FoldersStack up to date (if possible) @@ -428,6 +493,44 @@ ListBoxItem listBoxItem LoadImage(null); } } + #endregion + + #region UI-Control-Management + /// + /// Focuses and enables + /// + private void UseResolutionBox() + { + ResolutionBox.Focusable = true; + ResolutionBox.Focus(); + } + + /// + /// Unfocuses + /// + private void UnuseResolutionBox() + { + ResolutionBox.Focusable = false; + } + + /// + /// Gives back, whether any folder is visible or not + /// + public bool IsAnyFolderVisible + { + get + { + // Get every item in the list + foreach (ListBoxItem item in FoldersStack.Items) + { + // if one of them turns out to be visible, then return true + if (item.Visibility == Visibility.Visible) + return true; + } + // Else false + return false; + } + } /// /// Enables all the controls beside the @@ -476,7 +579,9 @@ public void DisableAllControls() SelectFolderButton.IsEnabled = false; ResolutionBox.IsEnabled = false; } + #endregion + #region Image-Management /// /// Skips the current image and loads the next one /// @@ -537,50 +642,9 @@ private void DoMove() MessageBoxButton.OK, MessageBoxImage.Warning); } } - - /// - /// Lets the user select a resolution for the loaded images - /// - public void SetResolution() - { - string response = InputBox.Show("Please set the horizontal resolution.\n\n\n" + - "Note: Everything equal or smaller to 0, as well as writing \"default\" reverts the resolution to default (1000),\n" + - "Note: The higher the resolution, the higher the loading times and RAM usage\n\n" + - "Will be applied on next loading.", - "Resolution", Properties.Settings.Default.MaxHorizontalResolution.ToString(), -1, -1); - // Stores the resolution selected by the user - /* Gets the resolution by the user via an input box, returns a bool whether - he inputted a number or not*/ - bool result = int.TryParse(response, out int resolution); - // If he did, continue - if (result) - { - // if the resolution is higher 0, then save it in the settings file - if(resolution > 0) - { - MaxHorizontalResolution = resolution; - } - // otherwise, revert to default - else - { - MaxHorizontalResolution = 1000; - } - } - // If the response is "" or "default, revert to default - else if (response == "default") - { - MaxHorizontalResolution = 1000; - } - // If nothing was given back, then don't change anything - else if (response == "") - { - // Clear so that nothing happens - } - // If the user did not input valid numbers, than repeat - else - SetResolution(); - } - + #endregion + + #region Performance /// /// Tells the garbage collector to collect garbage, reduces memory usage when called /// @@ -589,23 +653,8 @@ private void CollectGarbage() GC.Collect(); GC.WaitForPendingFinalizers(); } + #endregion - /// - /// Focuses and enables - /// - private void UseResolutionBox() - { - ResolutionBox.Focusable = true; - ResolutionBox.Focus(); - } - - /// - /// Unfocuses - /// - private void UnuseResolutionBox() - { - ResolutionBox.Focusable = false; - } #endregion @@ -739,24 +788,29 @@ private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) // Move the file when the right key has been pressed to the selected folder. case Key.Right: - DoMove(); + if(MoveFolderButton.IsEnabled) + DoMove(); break; // Skips the file when the left key has been pressed case Key.Left: - DoSkip(); + if (SkipFileButton.IsEnabled) + DoSkip(); break; // When the back button is pressed, remove one char from the search bar. // Do that no matter what is focused. case Key.Back: - if(SearchBarBox.Text.Count() != 0) + if(SearchBarBox.Text.Count() != 0 + && IsAnyFolderVisible + && FoldersStack.Items.Count > 1) SearchBarBox.Text = SearchBarBox.Text.Remove(SearchBarBox.Text.Count() - 1); break; // Add Text when space has been pressed case Key.Space: - SearchBarBox.Text += " "; + if (IsAnyFolderVisible && FoldersStack.Items.Count > 1) + SearchBarBox.Text += " "; break; // Opens Select Folder dialog @@ -766,29 +820,36 @@ private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) // Opens new folder Dialog case Key.F3: - NewFolder(); + if (NewFolderButton.IsEnabled) + NewFolder(); break; // Opens dialog for resolution preference case Key.F4: - //SetResolution(); - UseResolutionBox(); + if (ResolutionBox.IsEnabled) + UseResolutionBox(); break; // "Enters" the folder case Key.Enter: - EnterFolder(); + if (IsAnyFolderVisible) + EnterFolder(); break; // Goes a folder upwards case Key.Escape: - FoldersStack.SelectedIndex = 0; - EnterFolder(); + if (IsAnyFolderVisible) + { + FoldersStack.SelectedIndex = 0; + EnterFolder(); + } break; // Insert Characters and numbers only default: - if (!ResolutionBox.Focusable) + if (!ResolutionBox.Focusable + && IsAnyFolderVisible + && FoldersStack.Items.Count > 1) if (Regex.IsMatch(e.Key.ToString(), @"^[a-zA-Z0-9_]+$") && (e.Key.ToString().Count() < 2)) SearchBarBox.Text += e.Key.ToString().ToLower(); break; diff --git a/Image sort.UI/Properties/AssemblyInfo.cs b/Image sort.UI/Properties/AssemblyInfo.cs index 9993867e..0f5a4b7f 100644 --- a/Image sort.UI/Properties/AssemblyInfo.cs +++ b/Image sort.UI/Properties/AssemblyInfo.cs @@ -51,7 +51,7 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.7.2.0")] -[assembly: AssemblyFileVersion("1.7.2.0")] +[assembly: AssemblyVersion("1.7.3.0")] +[assembly: AssemblyFileVersion("1.7.3.0")] [assembly: NeutralResourcesLanguage("en-GB")] diff --git a/Image sort.Update/Properties/AssemblyInfo.cs b/Image sort.Update/Properties/AssemblyInfo.cs index be65f3dd..18ee4e0f 100644 --- a/Image sort.Update/Properties/AssemblyInfo.cs +++ b/Image sort.Update/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.7.2.0")] -[assembly: AssemblyFileVersion("1.7.2.0")] +[assembly: AssemblyVersion("1.7.3.0")] +[assembly: AssemblyFileVersion("1.7.3.0")] diff --git a/Image sort.Update/Properties/Resources.Designer.cs b/Image sort.Update/Properties/Resources.Designer.cs index 38c1efce..6984e6f6 100644 --- a/Image sort.Update/Properties/Resources.Designer.cs +++ b/Image sort.Update/Properties/Resources.Designer.cs @@ -70,7 +70,7 @@ internal static string UpdateRegistryUrl { } /// - /// Looks up a localized string similar to 1.7.2. + /// Looks up a localized string similar to 1.7.3. /// internal static string version { get { diff --git a/Image sort.Update/Properties/Resources.resx b/Image sort.Update/Properties/Resources.resx index b8448b9d..5a31a20c 100644 --- a/Image sort.Update/Properties/Resources.resx +++ b/Image sort.Update/Properties/Resources.resx @@ -121,6 +121,6 @@ https://raw.githubusercontent.com/Lolle2000la/Image-Sort-Updates/master/update-reg.json - 1.7.2 + 1.7.3 \ No newline at end of file