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

242 lines
10 KiB
C#
Raw 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
2025-06-25 12:24:50 +00:00
/// <summary>
/// Update plugin hotkey information in metadata and plugin setting.
/// </summary>
/// <param name="hotkeyPluginInfo"></param>
2025-07-06 12:28:26 +00:00
public void UpdatePluginHotkeyInfo(IDictionary<PluginPair, List<BasePluginHotkey>> hotkeyPluginInfo)
2025-06-25 12:24:50 +00:00
{
foreach (var info in hotkeyPluginInfo)
{
var pluginPair = info.Key;
var hotkeyInfo = info.Value;
var metadata = pluginPair.Metadata;
if (Plugins.TryGetValue(pluginPair.Metadata.ID, out var plugin))
{
if (plugin.pluginHotkeys == null || plugin.pluginHotkeys.Count == 0)
{
// If plugin hotkeys does not exist, create a new one and initialize with default values
plugin.pluginHotkeys = new List<PluginHotkey>();
foreach (var hotkey in hotkeyInfo)
{
plugin.pluginHotkeys.Add(new PluginHotkey
{
Id = hotkey.Id,
DefaultHotkey = hotkey.DefaultHotkey, // hotkey info provides default values
Hotkey = hotkey.DefaultHotkey // use default value
});
metadata.PluginHotkeys.Add(new PluginHotkey
{
Id = hotkey.Id,
DefaultHotkey = hotkey.DefaultHotkey, // hotkey info provides default values
Hotkey = hotkey.DefaultHotkey // use default value
});
}
}
else
{
// If plugin hotkeys exist, update the existing hotkeys with the new values
foreach (var hotkey in hotkeyInfo)
{
var existingHotkey = plugin.pluginHotkeys.Find(h => h.Id == hotkey.Id);
if (existingHotkey != null)
{
// Update existing hotkey
existingHotkey.DefaultHotkey = hotkey.DefaultHotkey; // hotkey info provides default values
metadata.PluginHotkeys.Add(new PluginHotkey
{
Id = hotkey.Id,
DefaultHotkey = hotkey.DefaultHotkey, // hotkey info provides default values
Hotkey = existingHotkey.Hotkey // use settings value
});
}
else
{
// Add new hotkey if it does not exist
plugin.pluginHotkeys.Add(new PluginHotkey
{
Id = hotkey.Id,
DefaultHotkey = hotkey.DefaultHotkey, // hotkey info provides default values
Hotkey = hotkey.DefaultHotkey // use default value
});
metadata.PluginHotkeys.Add(new PluginHotkey
{
Id = hotkey.Id,
DefaultHotkey = hotkey.DefaultHotkey, // hotkey info provides default values
Hotkey = hotkey.DefaultHotkey // use default value
});
}
}
}
}
else
{
// If settings does not exist, create a new one
Plugins[metadata.ID] = new Plugin
{
ID = metadata.ID,
Name = metadata.Name,
Version = metadata.Version,
DefaultActionKeywords = metadata.ActionKeywords, // metadata provides default values
ActionKeywords = metadata.ActionKeywords, // use default value
Disabled = metadata.Disabled,
HomeDisabled = metadata.HomeDisabled,
Priority = metadata.Priority,
DefaultSearchDelayTime = metadata.SearchDelayTime, // metadata provides default values
SearchDelayTime = metadata.SearchDelayTime, // use default value
};
foreach (var hotkey in hotkeyInfo)
{
Plugins[metadata.ID].pluginHotkeys.Add(new PluginHotkey
{
Id = hotkey.Id,
DefaultHotkey = hotkey.DefaultHotkey, // hotkey info provides default values
Hotkey = hotkey.DefaultHotkey // use default value
});
metadata.PluginHotkeys.Add(new PluginHotkey
{
Id = hotkey.Id,
DefaultHotkey = hotkey.DefaultHotkey, // hotkey info provides default values
Hotkey = hotkey.DefaultHotkey // 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; }
2025-06-25 12:24:50 +00:00
public List<PluginHotkey> pluginHotkeys { get; set; } = new List<PluginHotkey>();
/// <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; }
}
}