Skip to content

Commit 1a59da4

Browse files
committed
add plugin uninstall method
1 parent 311222e commit 1a59da4

File tree

3 files changed

+73
-28
lines changed

3 files changed

+73
-28
lines changed

Plugins/Flow.Launcher.Plugin.PluginsManager/Languages/en.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<system:String x:Key="plugin_pluginsmanager_downloading_plugin">Downloading plugin</system:String>
77
<system:String x:Key="plugin_pluginsmanager_please_wait">Please wait...</system:String>
88
<system:String x:Key="plugin_pluginsmanager_download_success">Successfully downloaded</system:String>
9+
<system:String x:Key="plugin_pluginsmanager_uninstall_prompt">Do you want to uninstall the following plugin?</system:String>
910
<!--Controls-->
1011

1112
<!--Plugin Infos-->

Plugins/Flow.Launcher.Plugin.PluginsManager/Main.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@ public List<Result> Query(Query query)
3939
{
4040
var search = query.Search;
4141

42-
var pluginManager = new PluginsManager(Context);
42+
var uninstallCmd = "uninstall";
4343

44-
return pluginManager.Search(search);
44+
var pluginManager = new PluginsManager(Context);
45+
46+
if (search.ToLower().StartsWith($"{uninstallCmd} ") || search.ToLower() == uninstallCmd)
47+
return pluginManager.RequestUninstall(search.Replace(uninstallCmd, string.Empty).Trim());
48+
49+
return pluginManager.RequestInstallOrUpdate(search);
4550
}
4651

4752
public void Save()

Plugins/Flow.Launcher.Plugin.PluginsManager/PluginsManager.cs

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ namespace Flow.Launcher.Plugin.PluginsManager
1515
internal class PluginsManager
1616
{
1717
private PluginsManifest pluginsManifest;
18-
private PluginInitContext context { get; set; }
18+
private PluginInitContext Context { get; set; }
1919

2020
private readonly string icoPath = "Images\\plugin.png";
2121

2222
internal PluginsManager(PluginInitContext context)
2323
{
2424
pluginsManifest = new PluginsManifest();
25-
this.context = context;
25+
Context = context;
2626
}
2727
internal void InstallOrUpdate(UserPlugin plugin)
2828
{
@@ -39,13 +39,13 @@ internal void InstallOrUpdate(UserPlugin plugin)
3939
{
4040
Utilities.Download(plugin.UrlDownload, filePath);
4141

42-
context.API.ShowMsg(context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"),
43-
context.API.GetTranslation("plugin_pluginsmanager_download_success"));
42+
Context.API.ShowMsg(Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"),
43+
Context.API.GetTranslation("plugin_pluginsmanager_download_success"));
4444
}
4545
catch (Exception e)
4646
{
47-
context.API.ShowMsg(context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"),
48-
context.API.GetTranslation("plugin_pluginsmanager_download_success"));
47+
Context.API.ShowMsg(Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"),
48+
Context.API.GetTranslation("plugin_pluginsmanager_download_success"));
4949

5050
Log.Exception("PluginsManager", "An error occured while downloading plugin", e, "PluginDownload");
5151
}
@@ -68,7 +68,18 @@ internal void PluginsManifestSiteOpen()
6868
//Open https://git.vcmq.workers.dev/Flow-Launcher/Flow.Launcher.PluginsManifest
6969
}
7070

71-
internal List<Result> Search(string searchName)
71+
internal List<Result> Search(List<Result> results, string searchName)
72+
{
73+
if (string.IsNullOrEmpty(searchName))
74+
return results;
75+
76+
return results
77+
.Where(x => StringMatcher.FuzzySearch(searchName, x.Title).IsSearchPrecisionScoreMet())
78+
.Select(x => x)
79+
.ToList();
80+
}
81+
82+
internal List<Result> RequestInstallOrUpdate(string searchName)
7283
{
7384
var results = new List<Result>();
7485

@@ -82,22 +93,16 @@ internal List<Result> Search(string searchName)
8293
IcoPath = icoPath,
8394
Action = e =>
8495
{
85-
context.API.ShowMsg(context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"),
86-
context.API.GetTranslation("plugin_pluginsmanager_please_wait"));
96+
Context.API.ShowMsg(Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"),
97+
Context.API.GetTranslation("plugin_pluginsmanager_please_wait"));
8798
Application.Current.MainWindow.Hide();
8899
InstallOrUpdate(x);
89100

90101
return true;
91102
}
92103
}));
93104

94-
if (string.IsNullOrEmpty(searchName))
95-
return results;
96-
97-
return results
98-
.Where(x => StringMatcher.FuzzySearch(searchName, x.Title).IsSearchPrecisionScoreMet())
99-
.Select(x => x)
100-
.ToList();
105+
return Search(results, searchName);
101106
}
102107

103108
private void Install(UserPlugin plugin, string downloadedFilePath)
@@ -155,7 +160,7 @@ private void Install(UserPlugin plugin, string downloadedFilePath)
155160
$"Version: {plugin.Version}{Environment.NewLine}" +
156161
$"Author: {plugin.Author}";
157162

158-
var existingPlugin = context.API.GetAllPlugins().Where(x => x.Metadata.ID == plugin.ID).FirstOrDefault();
163+
var existingPlugin = Context.API.GetAllPlugins().Where(x => x.Metadata.ID == plugin.ID).FirstOrDefault();
159164

160165
if (existingPlugin != null)
161166
{
@@ -177,22 +182,56 @@ private void Install(UserPlugin plugin, string downloadedFilePath)
177182

178183
Directory.Move(pluginFolderPath, newPluginPath);
179184

180-
//exsiting plugins may be has loaded by application,
181-
//if we try to delelte those kind of plugins, we will get a error that indicate the
182-
//file is been used now.
183-
//current solution is to restart Flow Launcher. Ugly.
184-
//if (MainWindow.Initialized)
185-
//{
186-
// Plugins.Initialize();
187-
//}
188185
if (MessageBox.Show($"You have installed plugin {plugin.Name} successfully.{Environment.NewLine}" +
189186
"Restart Flow Launcher to take effect?",
190187
"Install plugin", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
191188
{
192-
context.API.RestartApp();
189+
Context.API.RestartApp();
193190
}
194191
}
195192
}
196193
}
194+
195+
internal List<Result> RequestUninstall(string search)
196+
{
197+
var results = new List<Result>();
198+
199+
Context.API.GetAllPlugins()
200+
.ForEach(x => results.Add(
201+
new Result
202+
{
203+
Title = $"{x.Metadata.Name} by {x.Metadata.Author}",
204+
SubTitle = x.Metadata.Description,
205+
IcoPath = icoPath,
206+
Action = e =>
207+
{
208+
Application.Current.MainWindow.Hide();
209+
Uninstall(x.Metadata);
210+
211+
return true;
212+
}
213+
}));
214+
215+
return Search(results, search);
216+
}
217+
218+
private void Uninstall(PluginMetadata plugin)
219+
{
220+
string message = Context.API.GetTranslation("plugin_pluginsmanager_uninstall_prompt")+
221+
$"{Environment.NewLine}{Environment.NewLine}" +
222+
$"{plugin.Name} by {plugin.Author}";
223+
224+
if (MessageBox.Show(message, "Flow Launcher", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
225+
{
226+
File.Create(Path.Combine(plugin.PluginDirectory, "NeedDelete.txt")).Close();
227+
var result = MessageBox.Show($"You have uninstalled plugin {plugin.Name} successfully.{Environment.NewLine}" +
228+
"Restart Flow Launcher to take effect?",
229+
"Install plugin", MessageBoxButton.YesNo, MessageBoxImage.Question);
230+
if (result == MessageBoxResult.Yes)
231+
{
232+
Context.API.RestartApp();
233+
}
234+
}
235+
}
197236
}
198237
}

0 commit comments

Comments
 (0)