diff --git a/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs b/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs index 9905d156d..cda46041b 100644 --- a/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs +++ b/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs @@ -229,20 +229,6 @@ namespace Flow.Launcher.Plugin MatchResult FuzzySearch(string query, string stringToCompare); - /// - /// Returns if the given name is valid file name even if it doesn't exist - /// - /// the name to check - public bool IsValidFileName(string name); - /// - /// Returns if the given name is valid directory name even if it doesn't exist - /// - /// the name to check - public bool IsValidDirectoryName(string name); - /// - /// Open A dialog to rename the given file or folder - /// - /// The directory or file info for the thing to rename. You must check if it actually exists or this will throw an exception /// diff --git a/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs b/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs index 6c506cfc0..239d27e3d 100644 --- a/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs +++ b/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; @@ -364,5 +365,38 @@ namespace Flow.Launcher.Plugin.SharedCommands } } } + /// + /// Return true is the given name is a valid file name + /// + public static bool IsValidFileName(string name) + { + if (IsReservedName(name)) return false; + if (name.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0 || string.IsNullOrWhiteSpace(name)) + { + return false; + } + return true; + } + + public static bool IsValidDirectoryName(string name) + { + if (IsReservedName(name)) return false; + char[] invalidChars = Path.GetInvalidPathChars().Append('/').ToArray().Append('\\').ToArray(); + if (name.IndexOfAny(invalidChars) >= 0) + { + return false; + } + return true; + } + private static bool IsReservedName(string name) + { + string[] reservedNames = new[] { "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" }; + string nameWithoutExtension = Path.GetFileNameWithoutExtension(name).ToUpperInvariant(); + if (reservedNames.Contains(nameWithoutExtension)) + { + return true; + } + return false; + } } } diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index 27ca106bc..875a54f80 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -222,39 +222,7 @@ namespace Flow.Launcher } } } - public bool IsValidFileName(string name) - { - if (IsReservedName(name)) return false; - if (name.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0 || string.IsNullOrWhiteSpace(name)) - { - return false; - } - return true; - } - public bool IsValidDirectoryName(string name) - { - List invalidChars = Path.GetInvalidPathChars().ToList(); - invalidChars.Add('/'); - invalidChars.Add('\\'); - if (name.IndexOfAny(invalidChars.ToArray()) >= 0 || string.IsNullOrWhiteSpace(name)) - { - return false; - } - if (IsReservedName(name)) return false; - return true; - } - - - private bool IsReservedName(string name) - { - string[] reservedNames = new[] { "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" }; - string nameWithoutExtension = Path.GetFileNameWithoutExtension(name).ToUpperInvariant(); - if (reservedNames.Contains(nameWithoutExtension)) - { - return true; - } - return false; - } + private static async Task RetryActionOnSTAThreadAsync(Action action, int retryCount = 6, int retryDelay = 150) { for (var i = 0; i < retryCount; i++) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Helper/RenameThing.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Helper/RenameThing.cs index a7f9bc3ef..b2921710a 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Helper/RenameThing.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Helper/RenameThing.cs @@ -9,7 +9,7 @@ namespace Flow.Launcher.Plugin.Explorer.Helper { if (info is FileInfo) { - if (!api.IsValidFileName(newName)) + if (!SharedCommands.FilesFolders.IsValidFileName(newName)) { throw new InvalidNameException(); } @@ -17,28 +17,32 @@ namespace Flow.Launcher.Plugin.Explorer.Helper DirectoryInfo directory; directory = file.Directory ?? new DirectoryInfo(Path.GetPathRoot(file.FullName)); - if (info.FullName == Path.Join(directory.FullName, newName)) + string newPath = Path.Join(directory.FullName, newName); + if (info.FullName == newPath) { throw new NotANewNameException("New name was the same as the old name"); } - File.Move(info.FullName, Path.Join(directory.FullName, newName)); + if (File.Exists(newPath)) throw new ElementAlreadyExistsException(); + File.Move(info.FullName, newPath); return; } else if (info is DirectoryInfo) { - if (!api.IsValidDirectoryName(newName)) + if (!SharedCommands.FilesFolders.IsValidDirectoryName(newName)) { throw new InvalidNameException(); } DirectoryInfo directory = (DirectoryInfo)info; DirectoryInfo parent; parent = directory.Parent ?? new DirectoryInfo(Path.GetPathRoot(directory.FullName)); - - if (info.FullName == Path.Join(parent.FullName, newName)) + string newPath = Path.Join(parent.FullName, newName); + if (info.FullName == newPath) { throw new NotANewNameException("New name was the same as the old name"); } - Directory.Move(info.FullName, Path.Join(parent.FullName, newName)); + if (Directory.Exists(newPath)) throw new ElementAlreadyExistsException(); + + Directory.Move(info.FullName, newPath); } else @@ -48,7 +52,7 @@ namespace Flow.Launcher.Plugin.Explorer.Helper } /// - /// Renames a file system elemnt (directory or file) + /// Renames a file system element (directory or file) /// /// The requested new name /// The or representing the old file @@ -72,16 +76,17 @@ namespace Flow.Launcher.Plugin.Explorer.Helper switch (exception) { case FileNotFoundException: - api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_file_not_found"), oldInfo.FullName)); return; case NotANewNameException: api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_not_a_new_name"), NewFileName)); - api.ShowMainWindow(); return; case InvalidNameException: api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_invalid_name"), NewFileName)); return; + case ElementAlreadyExistsException: + api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_element_already_exists"), NewFileName)); + break; case IOException iOException: if (iOException.Message.Contains("incorrect")) { @@ -111,24 +116,23 @@ namespace Flow.Launcher.Plugin.Explorer.Helper System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) { } } - internal class FileAlreadyExistsException : IOException - { - public FileAlreadyExistsException() { } - public FileAlreadyExistsException(string message) : base(message) { } - public FileAlreadyExistsException(string message, Exception inner) : base(message, inner) { } - protected FileAlreadyExistsException( + internal class ElementAlreadyExistsException : IOException { + public ElementAlreadyExistsException() { } + public ElementAlreadyExistsException(string message) : base(message) { } + public ElementAlreadyExistsException(string message, Exception inner) : base(message, inner) { } + protected ElementAlreadyExistsException( System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) { } } internal class InvalidNameException : Exception { - public InvalidNameException() { } - public InvalidNameException(string message) : base(message) { } - public InvalidNameException(string message, Exception inner) : base(message, inner) { } - protected InvalidNameException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + public InvalidNameException() { } + public InvalidNameException(string message) : base(message) { } + public InvalidNameException(string message, Exception inner) : base(message, inner) { } + protected InvalidNameException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } } diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Languages/en.xaml b/Plugins/Flow.Launcher.Plugin.Explorer/Languages/en.xaml index 4992e52d3..959738d6b 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Languages/en.xaml +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Languages/en.xaml @@ -197,8 +197,10 @@ The given name: {0} was not new. {0} may not be empty. {0} is an invalid name. - The specified file: {0} was not found + The specified item: {0} was not found Open a dialog to rename this. This cannot be renamed. Successfully renamed it to: {0} + There is already a file with the name: {0} in this location + Failed to open rename dialog.