Skip to content

Commit 8ed495d

Browse files
committed
Improve code quality
1 parent 4d77bad commit 8ed495d

File tree

2 files changed

+113
-136
lines changed

2 files changed

+113
-136
lines changed
Lines changed: 109 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,140 +1,136 @@
1-
using System;
1+
using System;
22
using System.IO;
3+
using System.Runtime.Serialization;
34

4-
namespace Flow.Launcher.Plugin.Explorer.Helper
5+
namespace Flow.Launcher.Plugin.Explorer.Helper;
6+
7+
public static class RenameThing
58
{
6-
public static class RenameThing
9+
private static void Rename(this FileSystemInfo info, string newName)
710
{
8-
private static void _rename(this FileSystemInfo info, string newName, IPublicAPI api)
11+
if (info is FileInfo file)
912
{
10-
if (info is FileInfo)
13+
if (!SharedCommands.FilesFolders.IsValidFileName(newName))
1114
{
12-
if (!SharedCommands.FilesFolders.IsValidFileName(newName))
13-
{
14-
throw new InvalidNameException();
15-
}
16-
FileInfo file = (FileInfo)info;
17-
DirectoryInfo directory;
18-
19-
directory = file.Directory ?? new DirectoryInfo(Path.GetPathRoot(file.FullName));
20-
string newPath = Path.Join(directory.FullName, newName);
21-
if (info.FullName == newPath)
22-
{
23-
throw new NotANewNameException("New name was the same as the old name");
24-
}
25-
if (File.Exists(newPath)) throw new ElementAlreadyExistsException();
26-
File.Move(info.FullName, newPath);
27-
return;
15+
throw new InvalidNameException();
2816
}
29-
else if (info is DirectoryInfo)
17+
DirectoryInfo directory;
18+
var rootPath = Path.GetPathRoot(file.FullName);
19+
if (string.IsNullOrEmpty(rootPath)) return;
20+
directory = file.Directory ?? new DirectoryInfo(rootPath);
21+
string newPath = Path.Join(directory.FullName, newName);
22+
if (info.FullName == newPath)
3023
{
31-
if (!SharedCommands.FilesFolders.IsValidDirectoryName(newName))
32-
{
33-
throw new InvalidNameException();
34-
}
35-
DirectoryInfo directory = (DirectoryInfo)info;
36-
DirectoryInfo parent;
37-
parent = directory.Parent ?? new DirectoryInfo(Path.GetPathRoot(directory.FullName));
38-
string newPath = Path.Join(parent.FullName, newName);
39-
if (info.FullName == newPath)
40-
{
41-
throw new NotANewNameException("New name was the same as the old name");
42-
}
43-
if (Directory.Exists(newPath)) throw new ElementAlreadyExistsException();
44-
45-
Directory.Move(info.FullName, newPath);
46-
24+
throw new NotANewNameException("New name was the same as the old name");
4725
}
48-
else
49-
{
50-
throw new ArgumentException($"{nameof(info)} must be either, {nameof(FileInfo)} or {nameof(DirectoryInfo)}");
51-
}
52-
26+
if (File.Exists(newPath)) throw new ElementAlreadyExistsException();
27+
File.Move(info.FullName, newPath);
28+
return;
5329
}
54-
/// <summary>
55-
/// Renames a file system element (directory or file)
56-
/// </summary>
57-
/// <param name="NewFileName">The requested new name</param>
58-
/// <param name="oldInfo"> The <see cref="FileInfo"/> or <see cref="DirectoryInfo"/> representing the old file</param>
59-
/// <param name="api">An instance of <see cref="IPublicAPI"/>so this can create msgboxes</param>
60-
61-
public static void Rename(string NewFileName, FileSystemInfo oldInfo, IPublicAPI api)
30+
else if (info is DirectoryInfo directory)
6231
{
63-
// if it's just whitespace and nothing else
64-
if (NewFileName.Trim() == "" || NewFileName == "")
32+
if (!SharedCommands.FilesFolders.IsValidDirectoryName(newName))
6533
{
66-
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_field_may_not_be_empty"), "New file name"));
67-
return;
34+
throw new InvalidNameException();
6835
}
69-
70-
try
36+
DirectoryInfo parent;
37+
var rootPath = Path.GetPathRoot(directory.FullName);
38+
if (string.IsNullOrEmpty(rootPath)) return;
39+
parent = directory.Parent ?? new DirectoryInfo(rootPath);
40+
string newPath = Path.Join(parent.FullName, newName);
41+
if (info.FullName == newPath)
7142
{
72-
oldInfo._rename(NewFileName, api);
43+
throw new NotANewNameException("New name was the same as the old name");
7344
}
74-
catch (Exception exception)
45+
if (Directory.Exists(newPath)) throw new ElementAlreadyExistsException();
46+
47+
Directory.Move(info.FullName, newPath);
48+
49+
}
50+
else
51+
{
52+
throw new ArgumentException($"{nameof(info)} must be either, {nameof(FileInfo)} or {nameof(DirectoryInfo)}");
53+
}
54+
}
55+
56+
/// <summary>
57+
/// Renames a file system element (directory or file)
58+
/// </summary>
59+
/// <param name="NewFileName">The requested new name</param>
60+
/// <param name="oldInfo"> The <see cref="FileInfo"/> or <see cref="DirectoryInfo"/> representing the old file</param>
61+
/// <param name="api">An instance of <see cref="IPublicAPI"/>so this can create msgboxes</param>
62+
public static void Rename(string NewFileName, FileSystemInfo oldInfo, IPublicAPI api)
63+
{
64+
// if it's just whitespace and nothing else
65+
if (NewFileName.Trim() == "" || NewFileName == "")
66+
{
67+
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_field_may_not_be_empty"), "New file name"));
68+
return;
69+
}
70+
71+
try
72+
{
73+
oldInfo.Rename(NewFileName);
74+
}
75+
catch (Exception exception)
76+
{
77+
switch (exception)
7578
{
76-
switch (exception)
77-
{
78-
case FileNotFoundException:
79-
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_file_not_found"), oldInfo.FullName));
80-
return;
81-
case NotANewNameException:
82-
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_not_a_new_name"), NewFileName));
79+
case FileNotFoundException:
80+
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_file_not_found"), oldInfo.FullName));
81+
return;
82+
case NotANewNameException:
83+
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_not_a_new_name"), NewFileName));
84+
return;
85+
case InvalidNameException:
86+
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_invalid_name"), NewFileName));
87+
return;
88+
case ElementAlreadyExistsException:
89+
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_element_already_exists"), NewFileName));
90+
break;
91+
default:
92+
string msg = exception.Message;
93+
if (!string.IsNullOrEmpty(msg))
94+
{
95+
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_exception"), exception.Message));
8396
return;
84-
case InvalidNameException:
85-
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_invalid_name"), NewFileName));
86-
return;
87-
case ElementAlreadyExistsException:
88-
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_element_already_exists"), NewFileName));
89-
break;
90-
default:
91-
string msg = exception.Message;
92-
if (!string.IsNullOrEmpty(msg))
93-
{
94-
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_exception"), exception.Message));
95-
return;
96-
}
97-
else
98-
{
99-
api.ShowMsgError(api.GetTranslation("plugin_explorer_no_reason_given_exception"));
100-
}
97+
}
98+
else
99+
{
100+
api.ShowMsgError(api.GetTranslation("plugin_explorer_no_reason_given_exception"));
101+
}
101102

102-
return;
103-
}
103+
return;
104104
}
105-
api.ShowMsg(string.Format(api.GetTranslation("plugin_explorer_successful_rename"), NewFileName));
106-
}
107105
}
106+
api.ShowMsg(string.Format(api.GetTranslation("plugin_explorer_successful_rename"), NewFileName));
108107
}
108+
}
109109

110-
internal class NotANewNameException : IOException
111-
{
112-
public NotANewNameException() { }
113-
public NotANewNameException(string message) : base(message) { }
114-
public NotANewNameException(string message, Exception inner) : base(message, inner) { }
115-
protected NotANewNameException(
116-
System.Runtime.Serialization.SerializationInfo info,
117-
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
118-
}
119-
internal class ElementAlreadyExistsException : IOException {
120-
public ElementAlreadyExistsException() { }
121-
public ElementAlreadyExistsException(string message) : base(message) { }
122-
public ElementAlreadyExistsException(string message, Exception inner) : base(message, inner) { }
123-
protected ElementAlreadyExistsException(
124-
System.Runtime.Serialization.SerializationInfo info,
125-
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
126-
}
110+
internal class NotANewNameException : IOException
111+
{
112+
public NotANewNameException() { }
113+
public NotANewNameException(string message) : base(message) { }
114+
public NotANewNameException(string message, Exception inner) : base(message, inner) { }
115+
protected NotANewNameException(
116+
SerializationInfo info,
117+
StreamingContext context) : base(info, context) { }
118+
}
119+
internal class ElementAlreadyExistsException : IOException {
120+
public ElementAlreadyExistsException() { }
121+
public ElementAlreadyExistsException(string message) : base(message) { }
122+
public ElementAlreadyExistsException(string message, Exception inner) : base(message, inner) { }
123+
protected ElementAlreadyExistsException(
124+
SerializationInfo info,
125+
StreamingContext context) : base(info, context) { }
126+
}
127127

128-
internal class InvalidNameException : Exception
129-
{
128+
internal class InvalidNameException : Exception
129+
{
130130
public InvalidNameException() { }
131131
public InvalidNameException(string message) : base(message) { }
132132
public InvalidNameException(string message, Exception inner) : base(message, inner) { }
133133
protected InvalidNameException(
134-
System.Runtime.Serialization.SerializationInfo info,
135-
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
136-
}
137-
138-
139-
140-
134+
SerializationInfo info,
135+
StreamingContext context) : base(info, context) { }
136+
}
Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.IO;
1+
using System.IO;
22
using System.Linq;
33
using System.Windows;
44
using System.Windows.Controls;
@@ -7,15 +7,11 @@
77
using CommunityToolkit.Mvvm.ComponentModel;
88
using Flow.Launcher.Plugin.Explorer.Helper;
99

10-
1110
namespace Flow.Launcher.Plugin.Explorer.Views
1211
{
13-
1412
[INotifyPropertyChanged]
1513
public partial class RenameFile : Window
1614
{
17-
18-
1915
public string NewFileName
2016
{
2117
get => _newFileName;
@@ -25,7 +21,6 @@ public string NewFileName
2521
}
2622
}
2723

28-
2924
private string _newFileName;
3025

3126
private readonly IPublicAPI _api;
@@ -42,25 +37,16 @@ public RenameFile(IPublicAPI api, FileSystemInfo info)
4237

4338
InitializeComponent();
4439

45-
46-
4740
ShowInTaskbar = false;
48-
49-
50-
5141
RenameTb.Focus();
52-
53-
54-
5542
}
43+
5644
/// <summary>
5745
/// https://stackoverflow.com/a/59560352/24045055
5846
/// </summary>
59-
6047
private async void SelectAll_OnTextBoxGotFocus(object sender, RoutedEventArgs e)
6148
{
62-
63-
var textBox = sender as TextBox;
49+
if (sender is not TextBox textBox) return;
6450
if (_info is DirectoryInfo)
6551
{
6652
await Application.Current.Dispatcher.InvokeAsync(textBox.SelectAll, DispatcherPriority.Background);
@@ -70,15 +56,13 @@ private async void SelectAll_OnTextBoxGotFocus(object sender, RoutedEventArgs e)
7056
{
7157
string properName = Path.GetFileNameWithoutExtension(info.Name);
7258
Application.Current.Dispatcher.Invoke(textBox.Select, DispatcherPriority.Background, textBox.Text.IndexOf(properName), properName.Length );
73-
7459
}
75-
7660
}
61+
7762
private void OnDoneButtonClick(object sender, RoutedEventArgs e)
7863
{
7964
RenameThing.Rename(NewFileName, _info, _api);
8065
Close();
81-
8266
}
8367

8468
private void BtnCancel(object sender, RoutedEventArgs e)
@@ -94,9 +78,6 @@ private void RenameTb_OnKeyDown(object sender, KeyEventArgs e)
9478
OnDoneButtonClick(sender, e);
9579
e.Handled = true;
9680
}
97-
98-
99-
10081
}
10182
}
10283
}

0 commit comments

Comments
 (0)