basic features of renaming files works

This commit is contained in:
Koisu 2025-06-22 13:45:42 -07:00
parent 5ae6f971a0
commit 82823041b2
4 changed files with 70 additions and 38 deletions

View file

@ -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) { }
}
}

View file

@ -197,4 +197,6 @@
<system:String x:Key="plugin_explorer_rename_a_file">Rename</system:String>
<system:String x:Key="plugin_explorer_not_a_new_name">The given name: {0} was not new.</system:String>
<system:String x:Key="plugin_explorer_field_may_not_be_empty">{0} may not be empty.</system:String>
<system:String x:Key="plugin_explorer_invalid_name">{0} is an invalid name.</system:String>
<system:String x:Key="plugin_explorer_file_not_found">The specified file: {0} was not found</system:String>
</ResourceDictionary>

View file

@ -84,7 +84,6 @@
Width="135"
HorizontalAlignment="Left"
VerticalAlignment="Center"
DataObject.Pasting="RenameTb_Pasting"
PreviewKeyDown="RenameTb_OnKeyDown"
Text="{Binding NewFileName}"/>
</StackPanel>

View file

@ -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();
}
}
}
}