2022-12-06 12:34:40 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
using Flow.Launcher.Plugin.Program.Views;
|
|
|
|
|
|
using Flow.Launcher.Plugin.Program.Views.Models;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Flow.Launcher.Plugin.Program.ViewModels
|
|
|
|
|
|
{
|
|
|
|
|
|
public class AddProgramSourceViewModel : BaseModel
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly Settings Settings;
|
|
|
|
|
|
|
|
|
|
|
|
private bool enabled = true;
|
|
|
|
|
|
public bool Enabled
|
|
|
|
|
|
{
|
|
|
|
|
|
get => enabled;
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
enabled = value;
|
2022-12-07 05:58:20 +00:00
|
|
|
|
StatusModified = true;
|
2022-12-06 12:34:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private string location = string.Empty;
|
|
|
|
|
|
public string Location
|
|
|
|
|
|
{
|
|
|
|
|
|
get => location;
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
location = value;
|
2022-12-07 05:58:20 +00:00
|
|
|
|
LocationModified = true;
|
2022-12-06 12:34:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ProgramSource Source { get; init; }
|
|
|
|
|
|
public IPublicAPI API { get; init; }
|
|
|
|
|
|
public string AddBtnText { get; init; }
|
|
|
|
|
|
private bool LocationModified = false;
|
|
|
|
|
|
private bool StatusModified = false;
|
|
|
|
|
|
public bool IsCustomSource { get; init; } = true;
|
|
|
|
|
|
public bool IsNotCustomSource => !IsCustomSource;
|
|
|
|
|
|
|
|
|
|
|
|
public AddProgramSourceViewModel(PluginInitContext context, Settings settings)
|
|
|
|
|
|
{
|
|
|
|
|
|
API = context.API;
|
|
|
|
|
|
Settings = settings;
|
|
|
|
|
|
AddBtnText = API.GetTranslation("flowlauncher_plugin_program_add");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public AddProgramSourceViewModel(PluginInitContext context, Settings settings, ProgramSource programSource) : this(context, settings)
|
|
|
|
|
|
{
|
|
|
|
|
|
Source = programSource;
|
|
|
|
|
|
enabled = Source.Enabled;
|
|
|
|
|
|
location = Source.Location;
|
|
|
|
|
|
AddBtnText = API.GetTranslation("flowlauncher_plugin_program_update");
|
|
|
|
|
|
IsCustomSource = Settings.ProgramSources.Any(x => x.UniqueIdentifier == Source.UniqueIdentifier);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Browse()
|
|
|
|
|
|
{
|
|
|
|
|
|
var dialog = new FolderBrowserDialog();
|
|
|
|
|
|
DialogResult result = dialog.ShowDialog();
|
|
|
|
|
|
if (result == DialogResult.OK)
|
|
|
|
|
|
{
|
|
|
|
|
|
Location = dialog.SelectedPath;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-06 14:54:12 +00:00
|
|
|
|
public (bool, string) AddProgramSource()
|
2022-12-06 12:34:40 +00:00
|
|
|
|
{
|
|
|
|
|
|
if (!Directory.Exists(Location))
|
|
|
|
|
|
{
|
2022-12-06 14:54:12 +00:00
|
|
|
|
return (false, API.GetTranslation("flowlauncher_plugin_program_invalid_path"));
|
2022-12-06 12:34:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
else if (DuplicateSource(Location))
|
|
|
|
|
|
{
|
2022-12-06 14:54:12 +00:00
|
|
|
|
return (false, API.GetTranslation("flowlauncher_plugin_program_duplicate_program_source"));
|
2022-12-06 12:34:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var source = new ProgramSource(Location, Enabled);
|
|
|
|
|
|
Settings.ProgramSources.Insert(0, source);
|
|
|
|
|
|
ProgramSetting.ProgramSettingDisplayList.Add(source);
|
2022-12-06 14:54:12 +00:00
|
|
|
|
return (true, null);
|
2022-12-06 12:34:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-06 14:54:12 +00:00
|
|
|
|
public (bool, string) UpdateProgramSource()
|
2022-12-06 12:34:40 +00:00
|
|
|
|
{
|
|
|
|
|
|
if (LocationModified)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!Directory.Exists(Location))
|
|
|
|
|
|
{
|
2022-12-06 14:54:12 +00:00
|
|
|
|
return (false, API.GetTranslation("flowlauncher_plugin_program_invalid_path"));
|
2022-12-06 12:34:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
else if (DuplicateSource(Location))
|
|
|
|
|
|
{
|
2022-12-06 14:54:12 +00:00
|
|
|
|
return (false, API.GetTranslation("flowlauncher_plugin_program_duplicate_program_source"));
|
2022-12-06 12:34:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Source.Location = Location; // Changes UniqueIdentifier internally
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (StatusModified)
|
|
|
|
|
|
{
|
|
|
|
|
|
Source.Enabled = Enabled;
|
|
|
|
|
|
}
|
2022-12-06 14:54:12 +00:00
|
|
|
|
return (StatusModified || LocationModified, null);
|
2022-12-06 12:34:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-06 14:54:12 +00:00
|
|
|
|
public (bool, string) AddOrUpdate()
|
2022-12-06 12:34:40 +00:00
|
|
|
|
{
|
|
|
|
|
|
if (Source == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return AddProgramSource();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return UpdateProgramSource();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static bool DuplicateSource(string location)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ProgramSetting.ProgramSettingDisplayList.Any(x => x.UniqueIdentifier.Equals(location, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|