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) { curProgressDialog?.Invoke(new Action(() => curProgressDialog.Hide())); var dres = default(DialogResult); parentForm.Invoke(new Action(() => { var dialog = new UpdatesAvailableDialog(MyAppIcon, updateClient.CurrentVersion.Version.ToString(), updateClient.CurrentVersion.Channel.ToString(), Conversions.ToString(updateClient.CurrentVersion.Build), pkg.Version.Version.ToString(), pkg.Version.Channel.ToString(), Conversions.ToString(pkg.Version.Build), 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) { e.Cancel = true; EndUpdating(); } else { 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); } }