From 1695a8bb1468dfdb3bf2aa5f355f2f792c2d32fa Mon Sep 17 00:00:00 2001 From: niksedk Date: Sat, 19 Aug 2023 16:31:09 +0200 Subject: [PATCH] Use PluginUpdateChecker in form "PluginsGet" --- src/ui/Forms/PluginsGet.cs | 48 +++++------ src/ui/Logic/Plugins/PluginUpdateChecker.cs | 88 +++++++++------------ 2 files changed, 59 insertions(+), 77 deletions(-) diff --git a/src/ui/Forms/PluginsGet.cs b/src/ui/Forms/PluginsGet.cs index 8725b626d..214c53af3 100644 --- a/src/ui/Forms/PluginsGet.cs +++ b/src/ui/Forms/PluginsGet.cs @@ -7,9 +7,9 @@ using System.Collections.Generic; using System.Drawing; using System.Globalization; using System.IO; +using System.Linq; using System.Threading; using System.Windows.Forms; -using System.Xml; using MessageBox = Nikse.SubtitleEdit.Forms.SeMsgBox.MessageBox; namespace Nikse.SubtitleEdit.Forms @@ -91,27 +91,29 @@ namespace Nikse.SubtitleEdit.Forms _fetchingData = true; labelPleaseWait.Text = LanguageSettings.Current.General.PleaseWait; Refresh(); - ShowInstalledPlugins(); - ShowOnlinePlugins(); + var installedPlugins = new InstalledPluginMetadataProvider().GetPlugins(); + ShowInstalledPlugins(installedPlugins); + ShowOnlinePlugins(installedPlugins); labelPleaseWait.Text = string.Empty; } catch (Exception exception) { labelPleaseWait.Text = string.Empty; ChangeControlsState(true); - MessageBox.Show($"Unable to get plugin list!" + Environment.NewLine + Environment.NewLine + + MessageBox.Show("Unable to get plugin list!" + Environment.NewLine + Environment.NewLine + exception.Message + Environment.NewLine + Environment.NewLine + exception.StackTrace); } _fetchingData = false; } - private void ShowOnlinePlugins() + private void ShowOnlinePlugins(IEnumerable installedPlugins) { _downloadList = new List(); listViewGetPlugins.BeginUpdate(); _updateAllListUrls = new List(); var onlinePluginInfo = new OnlinePluginMetadataProvider(GetPluginXmlFileUrl()); - LoadAvailablePlugins(onlinePluginInfo.GetPlugins()); + _downloadList = onlinePluginInfo.GetPlugins().ToList(); + LoadAvailablePlugins(installedPlugins, _downloadList); ShowAvailablePlugins(); listViewGetPlugins.EndUpdate(); @@ -131,26 +133,18 @@ namespace Nikse.SubtitleEdit.Forms } } - private void LoadAvailablePlugins(IReadOnlyCollection plugins) + private void LoadAvailablePlugins(IEnumerable installedPlugins, IEnumerable onlinePlugins) { - foreach (var item in plugins) + var updates = PluginUpdateChecker.GetAvailableUpdates(installedPlugins, onlinePlugins.ToArray()); + foreach (ListViewItem installed in listViewInstalledPlugins.Items) { - _downloadList.Add(item); - - foreach (ListViewItem installed in listViewInstalledPlugins.Items) + var update = updates.FirstOrDefault(p => p.InstalledPlugin.Name == installed.Text); + if (update != null) { - if (string.Compare(installed.Text, item.Name.Trim('.'), StringComparison.OrdinalIgnoreCase) == 0) - { - var installedVer = MakeComparableVersionNumber(installed.SubItems[2].Text); - var currentVer = MakeComparableVersionNumber(item.Version.ToString(CultureInfo.InvariantCulture)); - if (installedVer < currentVer) - { - installed.BackColor = Configuration.Settings.General.UseDarkTheme ? Color.IndianRed : Color.LightPink; - installed.SubItems[1].Text = $"{_language.UpdateAvailable} {installed.SubItems[1].Text}"; - buttonUpdateAll.Visible = true; - _updateAllListUrls.Add(item.Url); - } - } + installed.BackColor = Configuration.Settings.General.UseDarkTheme ? Color.IndianRed : Color.LightPink; + installed.SubItems[1].Text = $"{_language.UpdateAvailable} {installed.SubItems[1].Text}"; + buttonUpdateAll.Visible = true; + _updateAllListUrls.Add(update.OnlinePlugin.Url); } } } @@ -211,12 +205,11 @@ namespace Nikse.SubtitleEdit.Forms listViewGetPlugins.EndUpdate(); } - private void ShowInstalledPlugins() + private void ShowInstalledPlugins(IEnumerable installedPlugins) { listViewInstalledPlugins.BeginUpdate(); listViewInstalledPlugins.Items.Clear(); - var localPluginInfo = new InstalledPluginMetadataProvider(); - foreach (var pluginInfo in localPluginInfo.GetPlugins()) + foreach (var pluginInfo in installedPlugins) { var item = new ListViewItem(pluginInfo.Name) { Tag = pluginInfo }; item.SubItems.Add(pluginInfo.Description); @@ -331,7 +324,8 @@ namespace Nikse.SubtitleEdit.Forms MessageBox.Show(string.Format(_language.PluginXDownloaded, _downloadedPluginName)); } - ShowInstalledPlugins(); + var installedPlugins = new InstalledPluginMetadataProvider().GetPlugins(); + ShowInstalledPlugins(installedPlugins); } private void ChangeControlsState(bool enable) diff --git a/src/ui/Logic/Plugins/PluginUpdateChecker.cs b/src/ui/Logic/Plugins/PluginUpdateChecker.cs index ce758e970..65fa97a4e 100644 --- a/src/ui/Logic/Plugins/PluginUpdateChecker.cs +++ b/src/ui/Logic/Plugins/PluginUpdateChecker.cs @@ -1,67 +1,55 @@ -using System; +using Nikse.SubtitleEdit.Core.Common; using System.Collections.Generic; using System.Linq; -using System.Threading.Tasks; namespace Nikse.SubtitleEdit.Logic.Plugins { public class PluginUpdateChecker { - private readonly IPluginMetadataProvider _localPluginMetadataProvider; - private readonly IPluginMetadataProvider _onlinePluginMetadataProvider; - - public PluginUpdateChecker( - IPluginMetadataProvider localPluginMetadataProvider, - IPluginMetadataProvider onlinePluginMetadataProvider) + public class PluginUpdate { - _localPluginMetadataProvider = localPluginMetadataProvider; - _onlinePluginMetadataProvider = onlinePluginMetadataProvider; + public PluginInfoItem InstalledPlugin { get; set; } + public PluginInfoItem OnlinePlugin { get; set; } } - // public PluginUpdateCheckResult Check() - // { - // var installedPlugins = _localPluginMetadataProvider.GetPlugins(); - // if (!installedPlugins.Any()) - // { - // return new PluginUpdateCheckResult(); - // } + public static List GetAvailableUpdates(IEnumerable installedPlugins, PluginInfoItem[] onlinePlugins) + { + var list = new List(); + foreach (var installedPlugin in installedPlugins) + { + var onlinePlugin = onlinePlugins.FirstOrDefault(p => p.Name == installedPlugin.Name); + if (onlinePlugin != null && + MakeComparableVersionNumber(installedPlugin.Version) < + MakeComparableVersionNumber(onlinePlugin.Version)) + { + list.Add(new PluginUpdate { InstalledPlugin = installedPlugin, OnlinePlugin = onlinePlugin }); + } + } - // // plugin repository - // var onlinePlugins = _onlinePluginMetadataProvider.GetPlugins(); - // if (!onlinePlugins.Any()) - // { - // return new PluginUpdateCheckResult(); - // } + return list; + } - // var pluginUpdateCheckResult = new PluginUpdateCheckResult(); - // foreach (var installedPlugin in installedPlugins) - // { - // var updateCheckInfo = installedPlugin.CheckUpdate(onlinePlugins); - // if (updateCheckInfo.IsNewUpdateAvailable()) - // { - // pluginUpdateCheckResult.Add(updateCheckInfo); - // } - // } + private static long MakeComparableVersionNumber(string versionNumber) + { + var s = versionNumber.Replace(',', '.').Replace(" ", string.Empty); + var arr = s.Split('.'); + if (arr.Length == 1 && long.TryParse(arr[0], out var a0)) + { + return a0 * 1_000_000; + } - // return pluginUpdateCheckResult; - // } - //} + if (arr.Length == 2 && long.TryParse(arr[0], out var b0) && long.TryParse(arr[1], out var b1)) + { + return b0 * 1_000_000 + b1 * 1_000; + } - //public class PluginUpdateCheckResult - //{ - // private ICollection _pluginUpdates; + if (arr.Length == 3 && long.TryParse(arr[0], out var c0) && long.TryParse(arr[1], out var c1) && long.TryParse(arr[2], out var c2)) + { + return c0 * 1_000_000 + c1 * 1_000 + c2; + } - // public bool Available => PluginUpdates.Any(); - - // public IEnumerable PluginUpdates => _pluginUpdates ?? Array.Empty(); - - // /// - // /// Adds available update information - // /// - // public void Add(PluginUpdate pluginUpdate) - // { - // _pluginUpdates = _pluginUpdates ?? Array.Empty(); - // _pluginUpdates.Add(pluginUpdate); - // } + SeLogger.Error("Bad plugin version number: " + versionNumber); + return 0; + } } } \ No newline at end of file