Flow.Launcher/Plugins/Flow.Launcher.Plugin.Explorer/Views/QuickAccessLinkSettings.xaml.cs

219 lines
6.7 KiB
C#
Raw Permalink Normal View History

using System;
2025-05-25 19:05:26 +00:00
using System.Collections.ObjectModel;
using System.ComponentModel;
2025-07-08 01:45:54 +00:00
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Forms;
2025-05-25 17:45:44 +00:00
using Flow.Launcher.Plugin.Explorer.Helper;
2025-06-17 08:32:18 +00:00
using Flow.Launcher.Plugin.Explorer.Search;
using Flow.Launcher.Plugin.Explorer.Search.QuickAccessLinks;
2025-06-17 08:55:23 +00:00
using CommunityToolkit.Mvvm.ComponentModel;
namespace Flow.Launcher.Plugin.Explorer.Views;
2025-06-17 08:55:23 +00:00
[INotifyPropertyChanged]
public partial class QuickAccessLinkSettings
{
2025-07-08 01:50:12 +00:00
private static readonly string ClassName = nameof(QuickAccessLinkSettings);
private string _selectedPath;
public string SelectedPath
{
get => _selectedPath;
set
{
if (_selectedPath != value)
{
_selectedPath = value;
OnPropertyChanged();
2025-06-07 05:52:12 +00:00
if (string.IsNullOrEmpty(_selectedName))
{
SelectedName = _selectedPath.GetPathName();
}
if (!string.IsNullOrEmpty(_selectedPath))
{
2025-06-17 08:32:18 +00:00
_accessLinkType = GetResultType(_selectedPath);
2025-06-07 05:52:12 +00:00
}
}
}
}
private string _selectedName;
public string SelectedName
{
2025-05-25 06:04:18 +00:00
get
{
2025-06-07 05:52:12 +00:00
return string.IsNullOrEmpty(_selectedName) ? _selectedPath.GetPathName() : _selectedName;
2025-05-25 06:04:18 +00:00
}
set
{
if (_selectedName != value)
{
_selectedName = value;
OnPropertyChanged();
}
}
}
2025-06-17 08:37:17 +00:00
public bool IsFileSelected { get; set; }
public bool IsFolderSelected { get; set; } = true; // Default to Folder
2025-06-17 08:31:11 +00:00
private bool IsEdit { get; }
2025-06-07 05:52:12 +00:00
private AccessLink SelectedAccessLink { get; }
2025-05-25 19:48:20 +00:00
public ObservableCollection<AccessLink> QuickAccessLinks { get; }
2025-06-17 08:37:17 +00:00
private ResultType _accessLinkType = ResultType.Folder; // Default to Folder
2025-05-25 19:05:26 +00:00
public QuickAccessLinkSettings(ObservableCollection<AccessLink> quickAccessLinks)
{
2025-06-07 06:07:07 +00:00
IsEdit = false;
2025-05-25 19:05:26 +00:00
QuickAccessLinks = quickAccessLinks;
InitializeComponent();
}
2025-05-25 07:31:08 +00:00
2025-06-07 05:52:12 +00:00
public QuickAccessLinkSettings(ObservableCollection<AccessLink> quickAccessLinks, AccessLink selectedAccessLink)
2025-05-25 07:31:08 +00:00
{
IsEdit = true;
_selectedName = selectedAccessLink.Name;
_selectedPath = selectedAccessLink.Path;
2025-06-17 08:33:25 +00:00
_accessLinkType = GetResultType(_selectedPath); // Initialize link type
IsFileSelected = selectedAccessLink.Type == ResultType.File; // Initialize default selection
IsFolderSelected = !IsFileSelected;
2025-05-25 07:31:08 +00:00
SelectedAccessLink = selectedAccessLink;
2025-05-25 19:05:26 +00:00
QuickAccessLinks = quickAccessLinks;
2025-05-25 07:31:08 +00:00
InitializeComponent();
}
private void BtnCancel_OnClick(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}
private void OnDoneButtonClick(object sender, RoutedEventArgs e)
{
// Validate the input before proceeding
2025-05-25 07:51:56 +00:00
if (string.IsNullOrEmpty(SelectedName) || string.IsNullOrEmpty(SelectedPath))
2025-05-25 06:04:18 +00:00
{
var warning = Localize.plugin_explorer_quick_access_link_no_folder_selected();
2025-05-25 06:04:18 +00:00
Main.Context.API.ShowMsgBox(warning);
return;
}
// Check if the path already exists in the quick access links
2025-05-25 19:48:20 +00:00
if (QuickAccessLinks.Any(x =>
x.Path.Equals(SelectedPath, StringComparison.OrdinalIgnoreCase) &&
x.Name.Equals(SelectedName, StringComparison.OrdinalIgnoreCase)))
2025-05-25 17:26:47 +00:00
{
var warning = Localize.plugin_explorer_quick_access_link_path_already_exists();
2025-05-25 17:26:47 +00:00
Main.Context.API.ShowMsgBox(warning);
return;
}
// If editing, update the existing link
if (IsEdit)
{
2025-06-17 08:34:43 +00:00
if (SelectedAccessLink != null)
{
2025-06-17 08:34:43 +00:00
var index = QuickAccessLinks.IndexOf(SelectedAccessLink);
if (index >= 0)
{
2025-06-17 08:34:43 +00:00
var updatedLink = new AccessLink
{
Name = SelectedName,
Type = _accessLinkType,
Path = SelectedPath
};
QuickAccessLinks[index] = updatedLink;
}
DialogResult = true;
Close();
}
// Add a new one if the selected access link is null (should not happen in edit mode, but just in case)
else
{
AddNewAccessLink();
}
}
// Otherwise, add a new one
else
2025-06-17 08:34:43 +00:00
{
AddNewAccessLink();
}
void AddNewAccessLink()
{
var newAccessLink = new AccessLink
{
Name = SelectedName,
2025-06-17 08:32:18 +00:00
Type = _accessLinkType,
Path = SelectedPath
};
QuickAccessLinks.Add(newAccessLink);
DialogResult = true;
Close();
2025-05-25 07:31:08 +00:00
}
}
private void SelectPath_OnClick(object commandParameter, RoutedEventArgs e)
{
// Open file or folder selection dialog based on the selected radio button
if (IsFileSelected)
{
2025-06-17 08:31:25 +00:00
var openFileDialog = new OpenFileDialog
{
Multiselect = false,
CheckFileExists = true,
CheckPathExists = true
};
2025-05-25 07:31:08 +00:00
2025-06-17 08:32:18 +00:00
if (openFileDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK ||
string.IsNullOrEmpty(openFileDialog.FileName))
return;
SelectedPath = openFileDialog.FileName;
}
else // Folder selection
{
2025-06-17 08:32:18 +00:00
var folderBrowserDialog = new FolderBrowserDialog
{
ShowNewFolderButton = true
};
if (folderBrowserDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK ||
string.IsNullOrEmpty(folderBrowserDialog.SelectedPath))
return;
SelectedPath = folderBrowserDialog.SelectedPath;
}
}
2025-06-17 08:32:18 +00:00
private static ResultType GetResultType(string path)
{
// Check if the path is a file or folder
2025-07-08 01:45:54 +00:00
if (File.Exists(path))
2025-06-17 08:32:18 +00:00
{
return ResultType.File;
}
2025-07-08 01:45:54 +00:00
else if (Directory.Exists(path))
2025-06-17 08:32:18 +00:00
{
2025-07-08 01:45:54 +00:00
if (string.Equals(Path.GetPathRoot(path), path, StringComparison.OrdinalIgnoreCase))
{
2025-06-17 08:32:18 +00:00
return ResultType.Volume;
}
2025-06-17 08:32:18 +00:00
else
{
return ResultType.Folder;
}
}
else
{
// This should not happen, but just in case, we assume it's a folder
2025-07-08 01:50:12 +00:00
Main.Context.API.LogError(ClassName, $"The path '{path}' does not exist or is invalid. Defaulting to Folder type.");
2025-06-17 08:32:18 +00:00
return ResultType.Folder;
}
}
}