|
1 | | -using System; |
| 1 | +using System; |
2 | 2 | using System.IO; |
| 3 | +using System.Runtime.Serialization; |
3 | 4 |
|
4 | | -namespace Flow.Launcher.Plugin.Explorer.Helper |
| 5 | +namespace Flow.Launcher.Plugin.Explorer.Helper; |
| 6 | + |
| 7 | +public static class RenameThing |
5 | 8 | { |
6 | | - public static class RenameThing |
| 9 | + private static void Rename(this FileSystemInfo info, string newName) |
7 | 10 | { |
8 | | - private static void _rename(this FileSystemInfo info, string newName, IPublicAPI api) |
| 11 | + if (info is FileInfo file) |
9 | 12 | { |
10 | | - if (info is FileInfo) |
| 13 | + if (!SharedCommands.FilesFolders.IsValidFileName(newName)) |
11 | 14 | { |
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(); |
28 | 16 | } |
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) |
30 | 23 | { |
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"); |
47 | 25 | } |
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; |
53 | 29 | } |
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) |
62 | 31 | { |
63 | | - // if it's just whitespace and nothing else |
64 | | - if (NewFileName.Trim() == "" || NewFileName == "") |
| 32 | + if (!SharedCommands.FilesFolders.IsValidDirectoryName(newName)) |
65 | 33 | { |
66 | | - api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_field_may_not_be_empty"), "New file name")); |
67 | | - return; |
| 34 | + throw new InvalidNameException(); |
68 | 35 | } |
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) |
71 | 42 | { |
72 | | - oldInfo._rename(NewFileName, api); |
| 43 | + throw new NotANewNameException("New name was the same as the old name"); |
73 | 44 | } |
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) |
75 | 78 | { |
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)); |
83 | 96 | 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 | + } |
101 | 102 |
|
102 | | - return; |
103 | | - } |
| 103 | + return; |
104 | 104 | } |
105 | | - api.ShowMsg(string.Format(api.GetTranslation("plugin_explorer_successful_rename"), NewFileName)); |
106 | | - } |
107 | 105 | } |
| 106 | + api.ShowMsg(string.Format(api.GetTranslation("plugin_explorer_successful_rename"), NewFileName)); |
108 | 107 | } |
| 108 | +} |
109 | 109 |
|
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 | +} |
127 | 127 |
|
128 | | - internal class InvalidNameException : Exception |
129 | | - { |
| 128 | +internal class InvalidNameException : Exception |
| 129 | +{ |
130 | 130 | public InvalidNameException() { } |
131 | 131 | public InvalidNameException(string message) : base(message) { } |
132 | 132 | public InvalidNameException(string message, Exception inner) : base(message, inner) { } |
133 | 133 | 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 | +} |
0 commit comments