mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
basic features of renaming files works
This commit is contained in:
parent
5ae6f971a0
commit
82823041b2
4 changed files with 70 additions and 38 deletions
|
|
@ -13,11 +13,15 @@ namespace Flow.Launcher.Plugin.Explorer.Helper
|
||||||
{
|
{
|
||||||
if (info is FileInfo)
|
if (info is FileInfo)
|
||||||
{
|
{
|
||||||
|
if (newName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
|
||||||
|
{
|
||||||
|
throw new InvalidNameException();
|
||||||
|
}
|
||||||
FileInfo file = new FileInfo(info.FullName);
|
FileInfo file = new FileInfo(info.FullName);
|
||||||
DirectoryInfo directory;
|
DirectoryInfo directory;
|
||||||
|
|
||||||
directory = file.Directory ?? new DirectoryInfo(Path.GetPathRoot(file.FullName));
|
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");
|
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)
|
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 directory = new DirectoryInfo(info.FullName);
|
||||||
DirectoryInfo parent;
|
DirectoryInfo parent;
|
||||||
parent = directory.Parent ?? new DirectoryInfo(Path.GetPathRoot(directory.FullName));
|
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");
|
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() { }
|
||||||
public NotANewNameException(string message) : base(message) { }
|
public NotANewNameException(string message) : base(message) { }
|
||||||
|
|
@ -52,4 +64,26 @@ namespace Flow.Launcher.Plugin.Explorer.Helper
|
||||||
System.Runtime.Serialization.SerializationInfo info,
|
System.Runtime.Serialization.SerializationInfo info,
|
||||||
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
|
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) { }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -197,4 +197,6 @@
|
||||||
<system:String x:Key="plugin_explorer_rename_a_file">Rename</system:String>
|
<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_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_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>
|
</ResourceDictionary>
|
||||||
|
|
|
||||||
|
|
@ -84,7 +84,6 @@
|
||||||
Width="135"
|
Width="135"
|
||||||
HorizontalAlignment="Left"
|
HorizontalAlignment="Left"
|
||||||
VerticalAlignment="Center"
|
VerticalAlignment="Center"
|
||||||
DataObject.Pasting="RenameTb_Pasting"
|
|
||||||
PreviewKeyDown="RenameTb_OnKeyDown"
|
PreviewKeyDown="RenameTb_OnKeyDown"
|
||||||
Text="{Binding NewFileName}"/>
|
Text="{Binding NewFileName}"/>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,11 @@ using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
|
using System.Windows.Controls;
|
||||||
using System.Windows.Input;
|
using System.Windows.Input;
|
||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
using Flow.Launcher.Plugin.Explorer.Helper;
|
using Flow.Launcher.Plugin.Explorer.Helper;
|
||||||
|
using Microsoft.VisualBasic.Logging;
|
||||||
|
|
||||||
|
|
||||||
namespace Flow.Launcher.Plugin.Explorer.Views
|
namespace Flow.Launcher.Plugin.Explorer.Views
|
||||||
|
|
@ -26,15 +28,7 @@ namespace Flow.Launcher.Plugin.Explorer.Views
|
||||||
|
|
||||||
|
|
||||||
private string _newFileName;
|
private string _newFileName;
|
||||||
private string renamingText = "fes";
|
|
||||||
public string RenamingText
|
|
||||||
{
|
|
||||||
get => renamingText;
|
|
||||||
set
|
|
||||||
{
|
|
||||||
_ = SetProperty(ref renamingText, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
private readonly IPublicAPI _api;
|
private readonly IPublicAPI _api;
|
||||||
private readonly string _oldFilePath;
|
private readonly string _oldFilePath;
|
||||||
|
|
||||||
|
|
@ -48,29 +42,32 @@ namespace Flow.Launcher.Plugin.Explorer.Views
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
|
||||||
RenameTb.Focus();
|
RenameTb.Focus();
|
||||||
|
|
||||||
ShowInTaskbar = false;
|
ShowInTaskbar = false;
|
||||||
|
|
||||||
RenameTb.SelectAll();
|
RenameTb.SelectAll();
|
||||||
|
|
||||||
_info = info;
|
_info = info;
|
||||||
_oldFilePath = _info.FullName;
|
_oldFilePath = _info.FullName;
|
||||||
_newFileName = _info.Name;
|
NewFileName = _info.Name;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnDoneButtonClick(object sender, RoutedEventArgs e)
|
private void OnDoneButtonClick(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
|
|
||||||
// if it's just whitespace and nothing else
|
// 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"));
|
_api.ShowMsgError(string.Format(_api.GetTranslation("plugin_explorer_field_may_not_be_empty"), "New file name"));
|
||||||
Show();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ()
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_info.Rename(_newFileName);
|
_info.Rename(NewFileName);
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
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));
|
_api.ShowMsgError(string.Format(_api.GetTranslation("plugin_explorer_file_not_found"), _oldFilePath));
|
||||||
break;
|
break;
|
||||||
case NotANewNameException notANewNameException:
|
case NotANewNameException:
|
||||||
_api.ShowMsgError(string.Format(_api.GetTranslation("plugin_explorer_not_a_new_name"), _newFileName));
|
_api.ShowMsgError(string.Format(_api.GetTranslation("plugin_explorer_not_a_new_name"), NewFileName));
|
||||||
_api.ShowMainWindow();
|
_api.ShowMainWindow();
|
||||||
break;
|
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:
|
default:
|
||||||
_api.ShowMsgError(exception.ToString());
|
_api.ShowMsgError(exception.ToString());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Close();
|
Close();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void BtnCancel(object sender, RoutedEventArgs e)
|
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue