Files
Pilz.Updating/Pilz.Updating.Client.GUI/UpdateClientGUI.cs
2024-06-24 09:07:46 +02:00

134 lines
4.3 KiB
C#

using Microsoft.VisualBasic.CompilerServices;
using Pilz.UI;
using Pilz.UI.Telerik.Dialogs;
using Pilz.Updating.GUIBase;
using System.ComponentModel;
using Telerik.WinControls;
namespace Pilz.Updating.Client.GUI;
public class UpdateClientGUI
{
// F i e l d s
private Form parentForm;
private SimpleActionDialog curProgressDialog;
private readonly UpdateClient updateClient;
// P r o p e r t i e s
public bool UseHiddenSearch { get; set; } = false;
private static Image MyAppIcon => Icon.ExtractAssociatedIcon(IO.Extensions.GetExecutablePath()).ToBitmap();
// C o n s t r u c t o r s
public UpdateClientGUI(UpdateClient updateClient)
{
this.updateClient = updateClient;
updateClient.UpdateStatusChanged += MyUpdateClient_UpdateStatusChanged;
updateClient.DownloadingUpdate += MyUpdateClient_DownloadingUpdate;
updateClient.InstallingUpdate += MyUpdateClient_InstallingUpdate;
updateClient.UpdateInstallerStarted += MyUpdateClient_FinishWork;
updateClient.NoUpdatesFound += MyUpdateClient_NoUpdatesFound;
}
~UpdateClientGUI()
{
updateClient.UpdateStatusChanged -= MyUpdateClient_UpdateStatusChanged;
updateClient.DownloadingUpdate -= MyUpdateClient_DownloadingUpdate;
updateClient.InstallingUpdate -= MyUpdateClient_InstallingUpdate;
updateClient.UpdateInstallerStarted -= MyUpdateClient_FinishWork;
updateClient.NoUpdatesFound -= MyUpdateClient_NoUpdatesFound;
}
// F e a t u r e s
public async Task UpdateInteractive(Form parentForm)
{
this.parentForm = parentForm;
await updateClient.UpdateInteractive();
}
private void EndUpdating()
{
curProgressDialog?.Invoke(new Action(() => curProgressDialog.Close()));
}
private void MyUpdateClient_UpdateStatusChanged(UpdateStatus newStatus)
{
bool useGui = false;
if (!(newStatus == UpdateStatus.Searching && UseHiddenSearch))
{
useGui = true;
}
if (useGui && curProgressDialog is null)
{
parentForm.Invoke(new Action(() =>
{
curProgressDialog = new SimpleActionDialog();
curProgressDialog.SetCurrentState(UpdateStatus.Waiting);
curProgressDialog.Show(parentForm);
}));
}
curProgressDialog?.Invoke(new Action(() => curProgressDialog.SetCurrentState(newStatus)));
}
private void MyUpdateClient_DownloadingUpdate(UpdatePackageInfo pkg, CancelEventArgs e)
{
var dres = default(DialogResult);
// Hide progress dialog
curProgressDialog?.Invoke(new Action(curProgressDialog.Hide));
// Show updates available dialog
parentForm.Invoke(() =>
{
var dialog = new UpdatesAvailableDialog(
MyAppIcon,
updateClient.CurrentVersion.Version.ToString(),
updateClient.CurrentVersion.Channel.ToString(),
updateClient.CurrentVersion.Build.ToString(),
pkg.Version.Version.ToString(),
pkg.Version.Channel.ToString(),
pkg.Version.Build.ToString(),
pkg.Notes,
updateClient.InstallAsAdmin);
var symbol = GlobalSymbolFactory.Instance.GetImage(GlobalSymbols.software_installer, UI.Telerik.SvgImageSize.Small).ToIcon();
dres = RadDialogBase.ShowDialog(dialog, parentForm, LangRes.Title_UpdatesAvailable, symbol).Result;
});
if (dres != DialogResult.OK)
{
// Finish updating
e.Cancel = true;
EndUpdating();
}
else
{
// Cancel
e.Cancel = false;
curProgressDialog?.Invoke(new Action(curProgressDialog.Show));
}
}
private void MyUpdateClient_InstallingUpdate(UpdatePackageInfo pkg, CancelEventArgs e)
{
e.Cancel = false;
}
private void MyUpdateClient_FinishWork()
{
EndUpdating();
}
private void MyUpdateClient_NoUpdatesFound()
{
EndUpdating();
if (!UseHiddenSearch)
RadMessageBox.Show(UpdatingClientGuiLangRes.MsgBox_NoUpdatesFound, UpdatingClientGuiLangRes.MsgBox_NoUpdatesFound_Titel, MessageBoxButtons.OK, RadMessageIcon.Info);
}
}