|
| 1 | +using System; |
| 2 | +using System.Collections.ObjectModel; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Windows; |
| 7 | +using System.Windows.Forms; |
| 8 | +using Flow.Launcher.Plugin.Explorer.Helper; |
| 9 | +using Flow.Launcher.Plugin.Explorer.Search; |
| 10 | +using Flow.Launcher.Plugin.Explorer.Search.QuickAccessLinks; |
| 11 | +using CommunityToolkit.Mvvm.ComponentModel; |
| 12 | + |
| 13 | +namespace Flow.Launcher.Plugin.Explorer.Views; |
| 14 | + |
| 15 | +[INotifyPropertyChanged] |
| 16 | +public partial class QuickAccessLinkSettings |
| 17 | +{ |
| 18 | + private static readonly string ClassName = nameof(QuickAccessLinkSettings); |
| 19 | + |
| 20 | + private string _selectedPath; |
| 21 | + public string SelectedPath |
| 22 | + { |
| 23 | + get => _selectedPath; |
| 24 | + set |
| 25 | + { |
| 26 | + if (_selectedPath != value) |
| 27 | + { |
| 28 | + _selectedPath = value; |
| 29 | + OnPropertyChanged(); |
| 30 | + if (string.IsNullOrEmpty(_selectedName)) |
| 31 | + { |
| 32 | + SelectedName = _selectedPath.GetPathName(); |
| 33 | + } |
| 34 | + if (!string.IsNullOrEmpty(_selectedPath)) |
| 35 | + { |
| 36 | + _accessLinkType = GetResultType(_selectedPath); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private string _selectedName; |
| 43 | + public string SelectedName |
| 44 | + { |
| 45 | + get |
| 46 | + { |
| 47 | + return string.IsNullOrEmpty(_selectedName) ? _selectedPath.GetPathName() : _selectedName; |
| 48 | + } |
| 49 | + set |
| 50 | + { |
| 51 | + if (_selectedName != value) |
| 52 | + { |
| 53 | + _selectedName = value; |
| 54 | + OnPropertyChanged(); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public bool IsFileSelected { get; set; } |
| 60 | + public bool IsFolderSelected { get; set; } = true; // Default to Folder |
| 61 | + |
| 62 | + private bool IsEdit { get; } |
| 63 | + private AccessLink SelectedAccessLink { get; } |
| 64 | + |
| 65 | + public ObservableCollection<AccessLink> QuickAccessLinks { get; } |
| 66 | + |
| 67 | + private ResultType _accessLinkType = ResultType.Folder; // Default to Folder |
| 68 | + |
| 69 | + public QuickAccessLinkSettings(ObservableCollection<AccessLink> quickAccessLinks) |
| 70 | + { |
| 71 | + IsEdit = false; |
| 72 | + QuickAccessLinks = quickAccessLinks; |
| 73 | + InitializeComponent(); |
| 74 | + } |
| 75 | + |
| 76 | + public QuickAccessLinkSettings(ObservableCollection<AccessLink> quickAccessLinks, AccessLink selectedAccessLink) |
| 77 | + { |
| 78 | + IsEdit = true; |
| 79 | + _selectedName = selectedAccessLink.Name; |
| 80 | + _selectedPath = selectedAccessLink.Path; |
| 81 | + _accessLinkType = GetResultType(_selectedPath); // Initialize link type |
| 82 | + IsFileSelected = selectedAccessLink.Type == ResultType.File; // Initialize default selection |
| 83 | + IsFolderSelected = !IsFileSelected; |
| 84 | + SelectedAccessLink = selectedAccessLink; |
| 85 | + QuickAccessLinks = quickAccessLinks; |
| 86 | + InitializeComponent(); |
| 87 | + } |
| 88 | + |
| 89 | + private void BtnCancel_OnClick(object sender, RoutedEventArgs e) |
| 90 | + { |
| 91 | + DialogResult = false; |
| 92 | + Close(); |
| 93 | + } |
| 94 | + |
| 95 | + private void OnDoneButtonClick(object sender, RoutedEventArgs e) |
| 96 | + { |
| 97 | + // Validate the input before proceeding |
| 98 | + if (string.IsNullOrEmpty(SelectedName) || string.IsNullOrEmpty(SelectedPath)) |
| 99 | + { |
| 100 | + var warning = Main.Context.API.GetTranslation("plugin_explorer_quick_access_link_no_folder_selected"); |
| 101 | + Main.Context.API.ShowMsgBox(warning); |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + // Check if the path already exists in the quick access links |
| 106 | + if (QuickAccessLinks.Any(x => |
| 107 | + x.Path.Equals(SelectedPath, StringComparison.OrdinalIgnoreCase) && |
| 108 | + x.Name.Equals(SelectedName, StringComparison.OrdinalIgnoreCase))) |
| 109 | + { |
| 110 | + var warning = Main.Context.API.GetTranslation("plugin_explorer_quick_access_link_path_already_exists"); |
| 111 | + Main.Context.API.ShowMsgBox(warning); |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + // If editing, update the existing link |
| 116 | + if (IsEdit) |
| 117 | + { |
| 118 | + if (SelectedAccessLink != null) |
| 119 | + { |
| 120 | + var index = QuickAccessLinks.IndexOf(SelectedAccessLink); |
| 121 | + if (index >= 0) |
| 122 | + { |
| 123 | + var updatedLink = new AccessLink |
| 124 | + { |
| 125 | + Name = SelectedName, |
| 126 | + Type = _accessLinkType, |
| 127 | + Path = SelectedPath |
| 128 | + }; |
| 129 | + QuickAccessLinks[index] = updatedLink; |
| 130 | + } |
| 131 | + DialogResult = true; |
| 132 | + Close(); |
| 133 | + } |
| 134 | + // Add a new one if the selected access link is null (should not happen in edit mode, but just in case) |
| 135 | + else |
| 136 | + { |
| 137 | + AddNewAccessLink(); |
| 138 | + } |
| 139 | + } |
| 140 | + // Otherwise, add a new one |
| 141 | + else |
| 142 | + { |
| 143 | + AddNewAccessLink(); |
| 144 | + } |
| 145 | + |
| 146 | + void AddNewAccessLink() |
| 147 | + { |
| 148 | + var newAccessLink = new AccessLink |
| 149 | + { |
| 150 | + Name = SelectedName, |
| 151 | + Type = _accessLinkType, |
| 152 | + Path = SelectedPath |
| 153 | + }; |
| 154 | + QuickAccessLinks.Add(newAccessLink); |
| 155 | + DialogResult = true; |
| 156 | + Close(); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + private void SelectPath_OnClick(object commandParameter, RoutedEventArgs e) |
| 161 | + { |
| 162 | + // Open file or folder selection dialog based on the selected radio button |
| 163 | + if (IsFileSelected) |
| 164 | + { |
| 165 | + var openFileDialog = new OpenFileDialog |
| 166 | + { |
| 167 | + Multiselect = false, |
| 168 | + CheckFileExists = true, |
| 169 | + CheckPathExists = true |
| 170 | + }; |
| 171 | + |
| 172 | + if (openFileDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK || |
| 173 | + string.IsNullOrEmpty(openFileDialog.FileName)) |
| 174 | + return; |
| 175 | + |
| 176 | + SelectedPath = openFileDialog.FileName; |
| 177 | + } |
| 178 | + else // Folder selection |
| 179 | + { |
| 180 | + var folderBrowserDialog = new FolderBrowserDialog |
| 181 | + { |
| 182 | + ShowNewFolderButton = true |
| 183 | + }; |
| 184 | + |
| 185 | + if (folderBrowserDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK || |
| 186 | + string.IsNullOrEmpty(folderBrowserDialog.SelectedPath)) |
| 187 | + return; |
| 188 | + |
| 189 | + SelectedPath = folderBrowserDialog.SelectedPath; |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + private static ResultType GetResultType(string path) |
| 194 | + { |
| 195 | + // Check if the path is a file or folder |
| 196 | + if (File.Exists(path)) |
| 197 | + { |
| 198 | + return ResultType.File; |
| 199 | + } |
| 200 | + else if (Directory.Exists(path)) |
| 201 | + { |
| 202 | + if (string.Equals(Path.GetPathRoot(path), path, StringComparison.OrdinalIgnoreCase)) |
| 203 | + { |
| 204 | + return ResultType.Volume; |
| 205 | + } |
| 206 | + else |
| 207 | + { |
| 208 | + return ResultType.Folder; |
| 209 | + } |
| 210 | + } |
| 211 | + else |
| 212 | + { |
| 213 | + // This should not happen, but just in case, we assume it's a folder |
| 214 | + Main.Context.API.LogError(ClassName, $"The path '{path}' does not exist or is invalid. Defaulting to Folder type."); |
| 215 | + return ResultType.Folder; |
| 216 | + } |
| 217 | + } |
| 218 | +} |
0 commit comments