-
Notifications
You must be signed in to change notification settings - Fork 3
/
PluginSettings.cs
98 lines (78 loc) · 4.54 KB
/
PluginSettings.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.PowerToys.Settings.UI.Library;
using OnePassword.Vaults;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using Wox.Plugin;
namespace Community.PowerToys.Run.Plugin._1Password
{
public class PluginSettings
{
public string? OnePasswordInstallPath { get; set; }
public string? OnePasswordInitVault { get; set; }
public string? OnePasswordExcludeVault { get; set; }
public string? OnePasswordEmail { get; set; }
public bool OnePasswordPreloadFavorite { get; set; }
public bool WindowsEnableHistory { get; set; }
public bool WindowsEnableRoaming { get; set; }
private ResourceManager _rm;
public PluginSettings(ResourceManager rm) {
_rm = rm;
}
public IEnumerable<PluginAdditionalOption> Options => new List<PluginAdditionalOption>
{
CreateOption(nameof(OnePasswordInstallPath), _rm.GetString("one_password_install_path"), _rm.GetString("one_password_install_path_desc")),
CreateOption(nameof(OnePasswordInitVault), _rm.GetString("one_password_init_vault"), _rm.GetString("one_password_init_vault_desc")),
CreateOption(nameof(OnePasswordExcludeVault), _rm.GetString("one_password_exclude_vault"), _rm.GetString("one_password_exclude_vault_desc")),
CreateOption(nameof(OnePasswordEmail), _rm.GetString("one_password_email"), _rm.GetString("one_password_email_desc")),
CreateOption(nameof(OnePasswordPreloadFavorite), _rm.GetString("one_password_preload_favorite"), optionType: PluginAdditionalOption.AdditionalOptionType.Checkbox),
CreateOption(nameof(WindowsEnableHistory), _rm.GetString("windows_enable_history"), _rm.GetString("windows_enable_history_desc"), PluginAdditionalOption.AdditionalOptionType.Checkbox),
CreateOption(nameof(WindowsEnableRoaming), _rm.GetString("windows_enable_roaming"), _rm.GetString("windows_enable_roaming_desc"), PluginAdditionalOption.AdditionalOptionType.Checkbox)
};
public void UpdateSettings(PowerLauncherPluginSettings settings)
{
OnePasswordInstallPath = GetSettingOrDefault<string>(Options,settings, nameof(OnePasswordInstallPath));
OnePasswordEmail = GetSettingOrDefault<string>(Options,settings, nameof(OnePasswordEmail));
OnePasswordInitVault = GetSettingOrDefault<string>(Options,settings, nameof(OnePasswordInitVault));
OnePasswordExcludeVault = GetSettingOrDefault<string>(Options, settings, nameof(OnePasswordExcludeVault));
OnePasswordPreloadFavorite = GetSettingOrDefault<bool>(Options, settings, nameof(OnePasswordPreloadFavorite));
WindowsEnableHistory = GetSettingOrDefault<bool>(Options, settings, nameof(WindowsEnableHistory));
WindowsEnableRoaming = GetSettingOrDefault<bool>(Options, settings, nameof(WindowsEnableRoaming));
}
private static PluginAdditionalOption CreateOption(string key, string displayLabel, string displayDescription = "", PluginAdditionalOption.AdditionalOptionType optionType = PluginAdditionalOption.AdditionalOptionType.Textbox)
{
return new PluginAdditionalOption
{
Key = key,
DisplayLabel = displayLabel,
DisplayDescription = displayDescription,
PluginOptionType = optionType
};
}
private static T GetSettingOrDefault<T>(IEnumerable<PluginAdditionalOption> options, PowerLauncherPluginSettings settings, string key)
{
var defaultOption = options.First(x => x.Key == key);
var option = settings?.AdditionalOptions?.FirstOrDefault(x => x.Key == key);
object value;
switch (defaultOption.PluginOptionType)
{
case PluginAdditionalOption.AdditionalOptionType.Textbox:
value = option?.TextValue ?? defaultOption.TextValue;
break;
case PluginAdditionalOption.AdditionalOptionType.Checkbox:
value = option?.Value ?? defaultOption.Value;
break;
default:
throw new NotSupportedException();
}
return (T)Convert.ChangeType(value, typeof(T));
}
}
}