Use PluginUpdateChecker in form "PluginsGet"

This commit is contained in:
niksedk 2023-08-19 16:31:09 +02:00
parent 076b527e81
commit 1695a8bb14
2 changed files with 59 additions and 77 deletions

View File

@ -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<PluginInfoItem> installedPlugins)
{
_downloadList = new List<PluginInfoItem>();
listViewGetPlugins.BeginUpdate();
_updateAllListUrls = new List<string>();
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<PluginInfoItem> plugins)
private void LoadAvailablePlugins(IEnumerable<PluginInfoItem> installedPlugins, IEnumerable<PluginInfoItem> 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<PluginInfoItem> 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)

View File

@ -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<PluginUpdate> GetAvailableUpdates(IEnumerable<PluginInfoItem> installedPlugins, PluginInfoItem[] onlinePlugins)
{
var list = new List<PluginUpdate>();
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<PluginUpdate> _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<PluginUpdate> PluginUpdates => _pluginUpdates ?? Array.Empty<PluginUpdate>();
// /// <summary>
// /// Adds available update information
// /// </summary>
// public void Add(PluginUpdate pluginUpdate)
// {
// _pluginUpdates = _pluginUpdates ?? Array.Empty<PluginUpdate>();
// _pluginUpdates.Add(pluginUpdate);
// }
SeLogger.Error("Bad plugin version number: " + versionNumber);
return 0;
}
}
}