From 82823041b2a4f139f5807e41f483dbf84b0beea2 Mon Sep 17 00:00:00 2001 From: Koisu Date: Sun, 22 Jun 2025 13:45:42 -0700 Subject: [PATCH] basic features of renaming files works --- .../Helper/RenameThing.cs | 44 +++++++++++-- .../Languages/en.xaml | 2 + .../Views/RenameFile.xaml | 1 - .../Views/RenameFile.xaml.cs | 61 +++++++++---------- 4 files changed, 70 insertions(+), 38 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Helper/RenameThing.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Helper/RenameThing.cs index a2e298210..1ba6c2709 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Helper/RenameThing.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Helper/RenameThing.cs @@ -13,11 +13,15 @@ namespace Flow.Launcher.Plugin.Explorer.Helper { if (info is FileInfo) { + if (newName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0) + { + throw new InvalidNameException(); + } FileInfo file = new FileInfo(info.FullName); DirectoryInfo directory; - + directory = file.Directory ?? new DirectoryInfo(Path.GetPathRoot(file.FullName)); - if (Path.Join(info.FullName, directory.Name) == newName) + if (info.FullName == Path.Join(directory.FullName, newName)) { throw new NotANewNameException("New name was the same as the old name"); } @@ -26,10 +30,18 @@ namespace Flow.Launcher.Plugin.Explorer.Helper } else if (info is DirectoryInfo) { + var invalidChars = Path.GetInvalidPathChars().ToList(); + invalidChars.Add('/'); + invalidChars.Add('\\'); + if (newName.IndexOfAny(invalidChars.ToArray()) >= 0) + { + throw new InvalidNameException(); + } DirectoryInfo directory = new DirectoryInfo(info.FullName); DirectoryInfo parent; parent = directory.Parent ?? new DirectoryInfo(Path.GetPathRoot(directory.FullName)); - if (Path.Join(parent.FullName, directory.Name) == newName) + + if (info.FullName == Path.Join(parent.FullName, newName)) { throw new NotANewNameException("New name was the same as the old name"); } @@ -42,8 +54,8 @@ namespace Flow.Launcher.Plugin.Explorer.Helper } } } - [Serializable] - public class NotANewNameException : IOException + + internal class NotANewNameException : IOException { public NotANewNameException() { } public NotANewNameException(string message) : base(message) { } @@ -52,4 +64,26 @@ 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( + 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) { } + } + + } + diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Languages/en.xaml b/Plugins/Flow.Launcher.Plugin.Explorer/Languages/en.xaml index 89c9b2abd..425655f87 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Languages/en.xaml +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Languages/en.xaml @@ -197,4 +197,6 @@ Rename The given name: {0} was not new. {0} may not be empty. + {0} is an invalid name. + The specified file: {0} was not found diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Views/RenameFile.xaml b/Plugins/Flow.Launcher.Plugin.Explorer/Views/RenameFile.xaml index 592ec284b..a69d2e49d 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Views/RenameFile.xaml +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Views/RenameFile.xaml @@ -84,7 +84,6 @@ Width="135" HorizontalAlignment="Left" VerticalAlignment="Center" - DataObject.Pasting="RenameTb_Pasting" PreviewKeyDown="RenameTb_OnKeyDown" Text="{Binding NewFileName}"/> diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Views/RenameFile.xaml.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Views/RenameFile.xaml.cs index c38c0905d..deb26ec6c 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Views/RenameFile.xaml.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Views/RenameFile.xaml.cs @@ -2,9 +2,11 @@ using System; using System.IO; using System.Linq; using System.Windows; +using System.Windows.Controls; using System.Windows.Input; using CommunityToolkit.Mvvm.ComponentModel; using Flow.Launcher.Plugin.Explorer.Helper; +using Microsoft.VisualBasic.Logging; namespace Flow.Launcher.Plugin.Explorer.Views @@ -26,15 +28,7 @@ namespace Flow.Launcher.Plugin.Explorer.Views private string _newFileName; - private string renamingText = "fes"; - public string RenamingText - { - get => renamingText; - set - { - _ = SetProperty(ref renamingText, value); - } - } + private readonly IPublicAPI _api; private readonly string _oldFilePath; @@ -48,29 +42,32 @@ namespace Flow.Launcher.Plugin.Explorer.Views InitializeComponent(); RenameTb.Focus(); - + ShowInTaskbar = false; RenameTb.SelectAll(); + _info = info; _oldFilePath = _info.FullName; - _newFileName = _info.Name; + NewFileName = _info.Name; + } private void OnDoneButtonClick(object sender, RoutedEventArgs e) { + // if it's just whitespace and nothing else - if (_newFileName.Trim() == "") + _api.LogInfo(nameof(RenameFile),$"THIS IS NEW FILE NAME: {NewFileName}"); + if (NewFileName.Trim() == "" || NewFileName == "") { _api.ShowMsgError(string.Format(_api.GetTranslation("plugin_explorer_field_may_not_be_empty"), "New file name")); - Show(); return; } - if () + try { - _info.Rename(_newFileName); + _info.Rename(NewFileName); } catch (Exception exception) { @@ -80,16 +77,30 @@ namespace Flow.Launcher.Plugin.Explorer.Views _api.ShowMsgError(string.Format(_api.GetTranslation("plugin_explorer_file_not_found"), _oldFilePath)); break; - case NotANewNameException notANewNameException: - _api.ShowMsgError(string.Format(_api.GetTranslation("plugin_explorer_not_a_new_name"), _newFileName)); + case NotANewNameException: + _api.ShowMsgError(string.Format(_api.GetTranslation("plugin_explorer_not_a_new_name"), NewFileName)); _api.ShowMainWindow(); break; + case InvalidNameException: + _api.ShowMsgError(string.Format(_api.GetTranslation("plugin_explorer_invalid_name"), NewFileName)); + break; + case IOException iOException: + if (iOException.Message.Contains("incorrect")) + { + _api.ShowMsgError(string.Format(_api.GetTranslation("plugin_explorer_invalid_name"), NewFileName)); + break; + } + else + { + goto default; + } default: _api.ShowMsgError(exception.ToString()); break; } } Close(); + } private void BtnCancel(object sender, RoutedEventArgs e) @@ -112,20 +123,6 @@ namespace Flow.Launcher.Plugin.Explorer.Views } - private void RenameTb_Pasting(object sender, DataObjectPastingEventArgs e) - { - if (e.DataObject.GetDataPresent(DataFormats.Text)) - { - string text = e.DataObject.GetData(DataFormats.Text) as string; - if (!string.IsNullOrEmpty(text) && text.Any(char.IsWhiteSpace)) - { - e.CancelCommand(); - } - } - else - { - e.CancelCommand(); - } - } + } }