Flow.Launcher/Flow.Launcher.Infrastructure/UserSettings/PluginSettings.cs

136 lines
4.7 KiB
C#
Raw Permalink Normal View History

2022-10-23 09:05:12 +00:00
using System.Collections.Generic;
2025-03-30 06:41:44 +00:00
using System.Text.Json.Serialization;
2020-04-21 09:12:17 +00:00
using Flow.Launcher.Plugin;
2020-04-21 09:12:17 +00:00
namespace Flow.Launcher.Infrastructure.UserSettings
{
public class PluginsSettings : BaseModel
{
private string pythonExecutablePath = string.Empty;
2025-03-30 05:57:35 +00:00
public string PythonExecutablePath
{
get => pythonExecutablePath;
set
{
2022-11-21 22:20:27 +00:00
pythonExecutablePath = value;
Constant.PythonPath = value;
}
}
2022-10-23 09:05:12 +00:00
private string nodeExecutablePath = string.Empty;
2022-11-21 22:20:27 +00:00
public string NodeExecutablePath
{
2025-03-30 05:57:35 +00:00
get => nodeExecutablePath;
set
{
2022-11-21 22:20:27 +00:00
nodeExecutablePath = value;
Constant.NodePath = value;
}
}
2022-10-23 09:05:12 +00:00
2025-03-30 06:41:44 +00:00
/// <summary>
/// Only used for serialization
/// </summary>
public Dictionary<string, Plugin> Plugins { get; set; } = new();
2025-03-30 06:41:44 +00:00
/// <summary>
/// Update plugin settings with metadata.
/// FL will get default values from metadata first and then load settings to metadata
/// </summary>
/// <param name="metadatas">Parsed plugin metadatas</param>
2016-05-12 01:45:35 +00:00
public void UpdatePluginSettings(List<PluginMetadata> metadatas)
{
foreach (var metadata in metadatas)
{
2025-02-24 03:35:41 +00:00
if (Plugins.TryGetValue(metadata.ID, out var settings))
{
2025-03-30 06:41:44 +00:00
// If settings exist, update settings & metadata value
// update settings values with metadata
2020-07-19 12:11:55 +00:00
if (string.IsNullOrEmpty(settings.Version))
2025-03-30 06:41:44 +00:00
{
2020-07-19 12:11:55 +00:00
settings.Version = metadata.Version;
2025-03-30 06:41:44 +00:00
}
settings.DefaultActionKeywords = metadata.ActionKeywords; // metadata provides default values
2025-03-31 05:26:09 +00:00
settings.DefaultSearchDelayTime = metadata.SearchDelayTime; // metadata provides default values
2020-07-19 12:11:55 +00:00
2025-03-30 06:41:44 +00:00
// update metadata values with settings
if (settings.ActionKeywords?.Count > 0)
{
metadata.ActionKeywords = settings.ActionKeywords;
metadata.ActionKeyword = settings.ActionKeywords[0];
}
else
{
metadata.ActionKeywords = new List<string>();
metadata.ActionKeyword = string.Empty;
}
metadata.Disabled = settings.Disabled;
2021-01-05 08:11:38 +00:00
metadata.Priority = settings.Priority;
2025-03-31 05:26:09 +00:00
metadata.SearchDelayTime = settings.SearchDelayTime;
2025-05-04 03:26:00 +00:00
metadata.HomeDisabled = settings.HomeDisabled;
}
else
{
2025-03-30 06:41:44 +00:00
// If settings does not exist, create a new one
Plugins[metadata.ID] = new Plugin
{
ID = metadata.ID,
Name = metadata.Name,
2020-07-19 12:11:55 +00:00
Version = metadata.Version,
2025-03-30 06:41:44 +00:00
DefaultActionKeywords = metadata.ActionKeywords, // metadata provides default values
ActionKeywords = metadata.ActionKeywords, // use default value
2021-01-05 08:11:38 +00:00
Disabled = metadata.Disabled,
2025-05-04 03:26:00 +00:00
HomeDisabled = metadata.HomeDisabled,
2025-03-18 01:11:15 +00:00
Priority = metadata.Priority,
2025-03-31 05:26:09 +00:00
DefaultSearchDelayTime = metadata.SearchDelayTime, // metadata provides default values
SearchDelayTime = metadata.SearchDelayTime, // use default value
};
}
}
}
2025-03-30 05:59:06 +00:00
public Plugin GetPluginSettings(string id)
{
if (Plugins.TryGetValue(id, out var plugin))
{
return plugin;
2025-03-30 06:41:44 +00:00
}
2025-03-30 05:59:06 +00:00
return null;
}
public Plugin RemovePluginSettings(string id)
{
Plugins.Remove(id, out var plugin);
return plugin;
}
}
2025-03-30 05:57:35 +00:00
public class Plugin
{
public string ID { get; set; }
2025-03-30 05:57:35 +00:00
public string Name { get; set; }
2025-03-30 05:57:35 +00:00
2020-07-19 12:11:55 +00:00
public string Version { get; set; }
2025-03-30 06:41:44 +00:00
[JsonIgnore]
public List<string> DefaultActionKeywords { get; set; }
// a reference of the action keywords from plugin manager
public List<string> ActionKeywords { get; set; }
2021-01-05 08:11:38 +00:00
public int Priority { get; set; }
2025-03-30 06:41:44 +00:00
[JsonIgnore]
public int? DefaultSearchDelayTime { get; set; }
2025-03-30 06:41:44 +00:00
public int? SearchDelayTime { get; set; }
/// <summary>
/// Used only to save the state of the plugin in settings
/// </summary>
public bool Disabled { get; set; }
2025-05-04 03:26:00 +00:00
public bool HomeDisabled { get; set; }
}
}