Flow.Launcher/Plugins/Flow.Launcher.Plugin.Explorer/Helper/RenameThing.cs

141 lines
6 KiB
C#
Raw Normal View History

2025-06-22 19:41:52 +00:00
using System;
using System.IO;
namespace Flow.Launcher.Plugin.Explorer.Helper
{
public static class RenameThing
{
private static void _rename(this FileSystemInfo info, string newName, IPublicAPI api)
2025-06-22 19:41:52 +00:00
{
if (info is FileInfo)
{
2025-06-24 18:19:45 +00:00
if (!SharedCommands.FilesFolders.IsValidFileName(newName))
2025-06-22 20:45:42 +00:00
{
throw new InvalidNameException();
}
2025-06-24 17:37:58 +00:00
FileInfo file = (FileInfo)info;
2025-06-22 19:41:52 +00:00
DirectoryInfo directory;
2025-06-22 20:45:42 +00:00
2025-06-22 19:41:52 +00:00
directory = file.Directory ?? new DirectoryInfo(Path.GetPathRoot(file.FullName));
2025-06-24 18:19:45 +00:00
string newPath = Path.Join(directory.FullName, newName);
if (info.FullName == newPath)
2025-06-22 19:41:52 +00:00
{
throw new NotANewNameException("New name was the same as the old name");
}
2025-06-24 18:19:45 +00:00
if (File.Exists(newPath)) throw new ElementAlreadyExistsException();
File.Move(info.FullName, newPath);
2025-06-22 19:41:52 +00:00
return;
}
else if (info is DirectoryInfo)
{
2025-06-24 18:19:45 +00:00
if (!SharedCommands.FilesFolders.IsValidDirectoryName(newName))
2025-06-22 20:45:42 +00:00
{
throw new InvalidNameException();
}
2025-06-24 17:37:58 +00:00
DirectoryInfo directory = (DirectoryInfo)info;
2025-06-22 19:41:52 +00:00
DirectoryInfo parent;
parent = directory.Parent ?? new DirectoryInfo(Path.GetPathRoot(directory.FullName));
2025-06-24 18:19:45 +00:00
string newPath = Path.Join(parent.FullName, newName);
if (info.FullName == newPath)
2025-06-22 19:41:52 +00:00
{
throw new NotANewNameException("New name was the same as the old name");
}
2025-06-24 18:19:45 +00:00
if (Directory.Exists(newPath)) throw new ElementAlreadyExistsException();
Directory.Move(info.FullName, newPath);
2025-06-22 19:41:52 +00:00
}
else
2025-06-22 19:41:52 +00:00
{
throw new ArgumentException($"{nameof(info)} must be either, {nameof(FileInfo)} or {nameof(DirectoryInfo)}");
}
}
/// <summary>
2025-06-24 18:19:45 +00:00
/// Renames a file system element (directory or file)
/// </summary>
/// <param name="NewFileName">The requested new name</param>
/// <param name="oldInfo"> The <see cref="FileInfo"/> or <see cref="DirectoryInfo"/> representing the old file</param>
/// <param name="api">An instance of <see cref="IPublicAPI"/>so this can create msgboxes</param>
public static void Rename(string NewFileName, FileSystemInfo oldInfo, IPublicAPI api)
{
// if it's just whitespace and nothing else
if (NewFileName.Trim() == "" || NewFileName == "")
{
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_field_may_not_be_empty"), "New file name"));
return;
}
try
{
oldInfo._rename(NewFileName, api);
}
catch (Exception exception)
{
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));
return;
case InvalidNameException:
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_invalid_name"), NewFileName));
return;
2025-06-24 18:19:45 +00:00
case ElementAlreadyExistsException:
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_element_already_exists"), NewFileName));
break;
2025-06-24 18:28:51 +00:00
default:
string msg = exception.Message;
if (!string.IsNullOrEmpty(msg))
{
2025-06-24 18:28:51 +00:00
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_exception"), exception.Message));
return;
}
else
{
2025-06-24 18:28:51 +00:00
api.ShowMsgError(api.GetTranslation("plugin_explorer_no_reason_given_exception"));
}
2025-06-24 18:28:51 +00:00
return;
}
}
api.ShowMsg(string.Format(api.GetTranslation("plugin_explorer_successful_rename"), NewFileName));
}
}
2025-06-22 19:41:52 +00:00
}
2025-06-22 20:45:42 +00:00
internal class NotANewNameException : IOException
2025-06-22 19:41:52 +00:00
{
public NotANewNameException() { }
public NotANewNameException(string message) : base(message) { }
public NotANewNameException(string message, Exception inner) : base(message, inner) { }
protected NotANewNameException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}
2025-06-24 18:19:45 +00:00
internal class ElementAlreadyExistsException : IOException {
public ElementAlreadyExistsException() { }
public ElementAlreadyExistsException(string message) : base(message) { }
public ElementAlreadyExistsException(string message, Exception inner) : base(message, inner) { }
protected ElementAlreadyExistsException(
2025-06-22 20:45:42 +00:00
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}
internal class InvalidNameException : Exception
{
2025-06-24 18:19:45 +00:00
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) { }
2025-06-22 20:45:42 +00:00
}
2025-06-22 20:45:42 +00:00