Skip to content

Commit 69dad1b

Browse files
Check if plugin has been modified when installing/updating/uninstalling
1 parent b7a7836 commit 69dad1b

File tree

3 files changed

+81
-15
lines changed

3 files changed

+81
-15
lines changed

Flow.Launcher.Core/Plugin/PluginManager.cs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public static class PluginManager
3131

3232
private static PluginsSettings Settings;
3333
private static List<PluginMetadata> _metadatas;
34+
private static List<string> _modifiedPlugins = new List<string>();
3435

3536
/// <summary>
3637
/// Directories that will hold Flow Launcher plugin directory
@@ -358,8 +359,42 @@ private static bool SameOrLesserPluginVersionExists(string metadataPath)
358359
&& newMetadata.Version.CompareTo(x.Metadata.Version) <= 0);
359360
}
360361

361-
public static void Install(UserPlugin plugin, string downloadedFilePath)
362+
#region Public functions
363+
364+
public static bool PluginModified(string uuid)
365+
{
366+
return _modifiedPlugins.Contains(uuid);
367+
}
368+
369+
public static void UpdatePlugin(PluginMetadata existingVersion, UserPlugin newVersion, string downloadedFilePath)
370+
{
371+
InstallPlugin(newVersion, downloadedFilePath, checkModified:false);
372+
UninstallPlugin(existingVersion, removeSettings:false, checkModified:false);
373+
_modifiedPlugins.Add(existingVersion.ID);
374+
}
375+
376+
public static void InstallPlugin(UserPlugin plugin, string downloadedFilePath)
377+
{
378+
InstallPlugin(plugin, downloadedFilePath, true);
379+
}
380+
381+
public static void UninstallPlugin(PluginMetadata plugin, bool removeSettings = true)
382+
{
383+
UninstallPlugin(plugin, removeSettings, true);
384+
}
385+
386+
#endregion
387+
388+
#region Internal functions
389+
390+
internal static void InstallPlugin(UserPlugin plugin, string downloadedFilePath, bool checkModified)
362391
{
392+
if (checkModified && PluginModified(plugin.ID))
393+
{
394+
// Distinguish exception from installing same or less version
395+
throw new ArgumentException($"Plugin {plugin.Name} {plugin.ID} has been modified.", nameof(plugin));
396+
}
397+
363398
var tempFolderPath = Path.Combine(Path.GetTempPath(), "flowlauncher");
364399
var tempFolderPluginPath = Path.Combine(tempFolderPath, "plugin");
365400

@@ -420,10 +455,20 @@ public static void Install(UserPlugin plugin, string downloadedFilePath)
420455
FilesFolders.CopyAll(pluginFolderPath, newPluginPath);
421456

422457
Directory.Delete(pluginFolderPath, true);
458+
459+
if (checkModified)
460+
{
461+
_modifiedPlugins.Add(plugin.ID);
462+
}
423463
}
424464

425-
public static void Uninstall(PluginMetadata plugin, bool removeSettings = true)
465+
internal static void UninstallPlugin(PluginMetadata plugin, bool removeSettings, bool checkModified)
426466
{
467+
if (checkModified && PluginModified(plugin.ID))
468+
{
469+
throw new ArgumentException($"Plugin {plugin.Name} has been modified");
470+
}
471+
427472
if (removeSettings)
428473
{
429474
Settings.Plugins.Remove(plugin.ID);
@@ -432,6 +477,13 @@ public static void Uninstall(PluginMetadata plugin, bool removeSettings = true)
432477

433478
// Marked for deletion. Will be deleted on next start up
434479
using var _ = File.CreateText(Path.Combine(plugin.PluginDirectory, "NeedDelete.txt"));
480+
481+
if (checkModified)
482+
{
483+
_modifiedPlugins.Add(plugin.ID);
484+
}
435485
}
486+
487+
#endregion
436488
}
437489
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<system:String x:Key="plugin_pluginsmanager_install_error_duplicate">Error: A plugin which has the same or greater version with {0} already exists.</system:String>
2121
<system:String x:Key="plugin_pluginsmanager_install_error_title">Error installing plugin</system:String>
2222
<system:String x:Key="plugin_pluginsmanager_install_error_subtitle">Error occurred while trying to install {0}</system:String>
23+
<system:String x:Key="plugin_pluginsmanager_uninstall_error_title">Error uninstalling plugin</system:String>
2324
<system:String x:Key="plugin_pluginsmanager_update_noresult_title">No update available</system:String>
2425
<system:String x:Key="plugin_pluginsmanager_update_noresult_subtitle">All plugins are up to date</system:String>
2526
<system:String x:Key="plugin_pluginsmanager_update_prompt">{0} by {1} {2}{3}Would you like to update this plugin? After the update Flow will automatically restart.</system:String>
@@ -36,7 +37,8 @@
3637
<system:String x:Key="plugin_pluginsmanager_install_success_no_restart">Plugin {0} successfully installed. Please restart Flow.</system:String>
3738
<system:String x:Key="plugin_pluginsmanager_uninstall_success_no_restart">Plugin {0} successfully uninstalled. Please restart Flow.</system:String>
3839
<system:String x:Key="plugin_pluginsmanager_update_success_no_restart">Plugin {0} successfully updated. Please restart Flow.</system:String>
39-
40+
<system:String x:Key="plugin_pluginsmanager_plugin_modified_error">Plugin {0} has already been modified. Please restart Flow before making any further changes.</system:String>
41+
4042
<!-- Plugin Infos -->
4143
<system:String x:Key="plugin_pluginsmanager_plugin_name">Plugins Manager</system:String>
4244
<system:String x:Key="plugin_pluginsmanager_plugin_description">Management of installing, uninstalling or updating Flow Launcher plugins</system:String>

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

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,6 @@ where existingPlugin.Metadata.Version.CompareTo(pluginFromManifest.Version) <
239239
Context.API.GetTranslation("plugin_pluginsmanager_update_title"),
240240
MessageBoxButton.YesNo) == MessageBoxResult.Yes)
241241
{
242-
Uninstall(x.PluginExistingMetadata, false);
243-
244242
var downloadToFilePath = Path.Combine(DataLocation.PluginsDirectory,
245243
$"{x.Name}-{x.NewVersion}.zip");
246244

@@ -249,7 +247,7 @@ where existingPlugin.Metadata.Version.CompareTo(pluginFromManifest.Version) <
249247
await Http.DownloadAsync(x.PluginNewUserPlugin.UrlDownload, downloadToFilePath)
250248
.ConfigureAwait(false);
251249

252-
Install(x.PluginNewUserPlugin, downloadToFilePath);
250+
PluginManager.UpdatePlugin(x.PluginExistingMetadata, x.PluginNewUserPlugin, downloadToFilePath);
253251

254252
if (Settings.AutoRestartAfterChanging)
255253
{
@@ -413,19 +411,24 @@ private void Install(UserPlugin plugin, string downloadedFilePath)
413411
throw new FileNotFoundException($"Plugin {plugin.ID} zip file not found at {downloadedFilePath}", downloadedFilePath);
414412
try
415413
{
416-
PluginManager.Install(plugin, downloadedFilePath);
414+
PluginManager.InstallPlugin(plugin, downloadedFilePath);
417415
}
418-
catch(FileNotFoundException e)
416+
catch (FileNotFoundException e)
419417
{
418+
Context.API.ShowMsgError(Context.API.GetTranslation("plugin_pluginsmanager_install_error_title"),
419+
Context.API.GetTranslation("plugin_pluginsmanager_install_errormetadatafile"));
420420
Log.Exception("Flow.Launcher.Plugin.PluginsManager", e.Message, e);
421-
MessageBox.Show(Context.API.GetTranslation("plugin_pluginsmanager_install_errormetadatafile"),
422-
Context.API.GetTranslation("plugin_pluginsmanager_install_error_title"));
423421
}
424-
catch(InvalidOperationException e)
422+
catch (InvalidOperationException e)
425423
{
424+
Context.API.ShowMsgError(Context.API.GetTranslation("plugin_pluginsmanager_install_error_title"),
425+
string.Format(Context.API.GetTranslation("plugin_pluginsmanager_install_error_duplicate"), plugin.Name));
426+
Log.Exception("Flow.Launcher.Plugin.PluginsManager", e.Message, e);
427+
}
428+
catch (ArgumentException e) {
429+
Context.API.ShowMsgError(Context.API.GetTranslation("plugin_pluginsmanager_install_error_title"),
430+
string.Format(Context.API.GetTranslation("plugin_pluginsmanager_plugin_modified_error"), plugin.Name));
426431
Log.Exception("Flow.Launcher.Plugin.PluginsManager", e.Message, e);
427-
MessageBox.Show(string.Format(Context.API.GetTranslation("plugin_pluginsmanager_install_error_duplicate"), plugin.Name),
428-
Context.API.GetTranslation("plugin_pluginsmanager_install_error_title"));
429432
}
430433
}
431434

@@ -482,9 +485,18 @@ internal List<Result> RequestUninstall(string search)
482485
return Search(results, search);
483486
}
484487

485-
private static void Uninstall(PluginMetadata plugin, bool removeSettings = true)
488+
private void Uninstall(PluginMetadata plugin)
486489
{
487-
PluginManager.Uninstall(plugin, removeSettings);
490+
try
491+
{
492+
PluginManager.UninstallPlugin(plugin, removeSettings:true);
493+
}
494+
catch (ArgumentException e)
495+
{
496+
Log.Exception("Flow.Launcher.Plugin.PluginsManager", e.Message, e);
497+
Context.API.ShowMsgError(Context.API.GetTranslation("plugin_pluginsmanager_uninstall_error_title"),
498+
Context.API.GetTranslation("plugin_pluginsmanager_plugin_modified_error"));
499+
}
488500
}
489501
}
490502
}

0 commit comments

Comments
 (0)