-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathPluginBrowser.xaml.cs
More file actions
208 lines (179 loc) · 8.09 KB
/
PluginBrowser.xaml.cs
File metadata and controls
208 lines (179 loc) · 8.09 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Newtonsoft.Json;
using NLog;
using Torch.API.WebAPI;
using Torch.Collections;
using Torch.Server.Annotations;
using Torch.Managers;
using Torch.API.Managers;
namespace Torch.Server.Views
{
/// <summary>
/// Interaction logic for PluginBrowser.xaml
/// </summary>
public partial class PluginBrowser : Window, INotifyPropertyChanged
{
private static Logger Log = LogManager.GetCurrentClassLogger();
public MtObservableList<PluginItem> PluginsSource { get; set; } = new MtObservableList<PluginItem>();
public MtObservableList<PluginItem> Plugins { get; set; } = new MtObservableList<PluginItem>();
public PluginItem CurrentItem { get; set; }
public const string PLUGINS_SEARCH_TEXT = "Plugins search...";
private string PreviousSearchQuery = "";
private string _description = "Loading data from server, please wait..";
private static object _syncLock = new object();
public string CurrentDescription
{
get { return _description; }
set
{
_description = value;
OnPropertyChanged();
}
}
public PluginBrowser(IPluginManager pluginManager)
{
InitializeComponent();
var installedPlugins = pluginManager.Plugins;
BindingOperations.EnableCollectionSynchronization(Plugins,_syncLock);
Task.Run(async () =>
{
var res = await PluginQuery.Instance.QueryAll();
if (res == null)
return;
foreach (var item in res.Plugins.OrderBy(i => i.Name)) {
lock (_syncLock)
{
if (installedPlugins.Keys.Contains(Guid.Parse(item.ID)))
item.Installed = true;
Plugins.Add(item);
PluginsSource.Add(item);
}
}
CurrentDescription = "Please select a plugin...";
});
MarkdownFlow.CommandBindings.Add(new CommandBinding(NavigationCommands.GoToPage, (sender, e) => OpenUri((string)e.Parameter)));
}
public static bool IsValidUri(string uri)
{
if (!Uri.IsWellFormedUriString(uri, UriKind.Absolute))
return false;
Uri tmp;
if (!Uri.TryCreate(uri, UriKind.Absolute, out tmp))
return false;
return tmp.Scheme == Uri.UriSchemeHttp || tmp.Scheme == Uri.UriSchemeHttps;
}
public static bool OpenUri(string uri)
{
if (!IsValidUri(uri))
return false;
System.Diagnostics.Process.Start(uri);
return true;
}
private void PluginsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
CurrentItem = (PluginItem)PluginsList.SelectedItem;
if (CurrentItem != null) {
CurrentDescription = CurrentItem.Description;
DownloadButton.IsEnabled = !string.IsNullOrEmpty(CurrentItem.LatestVersion);
UninstallButton.IsEnabled = !string.IsNullOrEmpty(CurrentItem.LatestVersion);
}
}
private void DownloadButton_OnClick(object sender, RoutedEventArgs e)
{
var SelectedItems = PluginsList.SelectedItems;
foreach(PluginItem PluginItem in SelectedItems)
TorchBase.Instance.Config.Plugins.Add(new Guid(PluginItem.ID));
TorchBase.Instance.Config.Save();
Log.Info($"Started to download {SelectedItems.Count} plugin(s)");
PluginDownloader DownloadProgress = new PluginDownloader(SelectedItems);
DownloadProgress.Show();
}
private void UninstallButton_OnClick(object sender, RoutedEventArgs e) {
var SelectedItems = PluginsList.SelectedItems;
if(SelectedItems.Cast<PluginItem>().Any(x => x.Installed == false)) {
MessageBox.Show($"Error! You have selected at least 1 plugin which isnt currently installed. Please de-select and try again!", "Uninstall Error", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
var result = MessageBox.Show($"Are you sure you want to attempt uninstall of {SelectedItems.Count} plugin(s)?", "Uninstall Confirmation", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes) {
foreach(PluginItem PluginItem in SelectedItems) {
if(TorchBase.Instance.Config.Plugins.Contains(Guid.Parse(PluginItem.ID))) {
TorchBase.Instance.Config.Plugins.Remove(Guid.Parse(PluginItem.ID));
string path = $"Plugins\\{PluginItem.Name}.zip";
if (File.Exists(path))
File.Delete(path);
Log.Info($"Uninstalled {PluginItem.Name}");
}
}
MessageBox.Show($"Plugins removed... Please restart your server for changes to take effect.", "Uninstall Confirmation", MessageBoxButton.OK);
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void TxtPluginsSearch_GotFocus(object sender, RoutedEventArgs e) {
if (txtPluginsSearch.Text == PLUGINS_SEARCH_TEXT) {
txtPluginsSearch.Clear();
txtPluginsSearch.Foreground = Brushes.Black;
return;
}
}
private void TxtPluginsSearch_LostFocus(object sender, RoutedEventArgs e) {
if(txtPluginsSearch.Text == "") {
txtPluginsSearch.Foreground = Brushes.Gray;
txtPluginsSearch.Text = PLUGINS_SEARCH_TEXT;
return;
}
}
private void TxtPluginsSearch_TextChanged(object sender, TextChangedEventArgs e) {
string SearchQueryString = txtPluginsSearch.Text;
if(SearchQueryString.Length < PreviousSearchQuery.Length) {
ResetSearchFilter();
}
if (SearchQueryString != PLUGINS_SEARCH_TEXT && SearchQueryString != string.Empty) {
SearchPlugins(SearchQueryString);
} else {
ResetSearchFilter();
}
PreviousSearchQuery = SearchQueryString;
}
private void SearchPlugins(string SearchQueryString) {
foreach (var plugin in Plugins.Where(p => !p.Name.Contains(SearchQueryString, StringComparison.OrdinalIgnoreCase) &&
!p.Author.Contains(SearchQueryString, StringComparison.OrdinalIgnoreCase))) {
Plugins.Remove(plugin);
}
foreach (var plugin in Plugins.Where(p => p.Name.Contains(SearchQueryString, StringComparison.OrdinalIgnoreCase) ||
p.Author.Contains(SearchQueryString, StringComparison.OrdinalIgnoreCase))) {
if (!Plugins.Contains(plugin))
Plugins.Add(plugin);
}
}
private void ResetSearchFilter() {
Plugins.Clear();
foreach (var plugin in PluginsSource) {
if (!Plugins.Contains(plugin))
Plugins.Add(plugin);
}
}
}
}