Skip to content

Commit

Permalink
Some more corrections.
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantina Chremmou <[email protected]>
  • Loading branch information
kc284 authored and CitrixChris committed Oct 30, 2023
1 parent 91bd5e0 commit ed71d8e
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 145 deletions.
30 changes: 12 additions & 18 deletions XenAdmin/Alerts/Types/ClientUpdateAlert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
using XenAdmin.Actions.Updates;
using XenAdmin.Core;
using XenAdmin.Dialogs;
using XenAdmin.Dialogs.WarningDialogs;
using XenCenterLib;

namespace XenAdmin.Alerts
Expand Down Expand Up @@ -124,10 +125,13 @@ public static void DownloadAndInstallNewClient(ClientUpdateAlert updateAlert, IW

if (currentTasks)
{
if (new Dialogs.WarningDialogs.CloseXenCenterWarningDialog(true).ShowDialog(parent) != DialogResult.OK)
using (var dlg = new CloseXenCenterWarningDialog(true))
{
downloadAndInstallClientAction.ReleaseDownloadedContent();
return;
if (dlg.ShowDialog(parent) != DialogResult.OK)
{
downloadAndInstallClientAction.ReleaseDownloadedContent();
return;
}
}
}

Expand All @@ -153,40 +157,30 @@ public static void DownloadSource(IWin32Window parent)
// If no update no need to show where to save dialog
var clientVersion = Updates.ClientVersions.FirstOrDefault();

DownloadSourceAction downloadSourceAction;
if (clientVersion == null)
{
// There is no XCupdates.xml so direct to website.
// There is no XCUpdates.xml so direct to website.
Program.OpenURL(InvisibleMessages.WEBSITE_DOWNLOADS);
return;
}
else
{
string outputPathAndFileName;
using (var saveSourceDialog = new SaveFileDialog())
{
saveSourceDialog.FileName = string.Format(Messages.SOURCE_FILE_NAME, BrandManager.BrandConsole, clientVersion.Version) + ".zip";
saveSourceDialog.FileName = $"{BrandManager.BrandConsole}-v{clientVersion.Version}-source.zip";
saveSourceDialog.DefaultExt = "zip";
saveSourceDialog.Filter = "(*.zip)|*.zip|All files (*.*)|*.*";
saveSourceDialog.InitialDirectory = Win32.GetKnownFolderPath(Win32.KnownFolders.Downloads);

if (saveSourceDialog.ShowDialog() != DialogResult.OK)
if (saveSourceDialog.ShowDialog(parent) != DialogResult.OK)
{
return;
}
outputPathAndFileName = saveSourceDialog.FileName;
}

downloadSourceAction = new DownloadSourceAction(clientVersion.Name, clientVersion.Version, new Uri(clientVersion.SourceUrl), outputPathAndFileName);

using (var dlg = new ActionProgressDialog(downloadSourceAction, ProgressBarStyle.Continuous))
{
dlg.ShowCancel = true;
dlg.ShowDialog(parent);
}

if (!downloadSourceAction.Succeeded)
return;
var downloadSourceAction = new DownloadSourceAction(clientVersion.Name, new Uri(clientVersion.SourceUrl), outputPathAndFileName);
downloadSourceAction.RunAsync();
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions XenAdmin/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2719,12 +2719,12 @@ private void SetClientUpdateAlert()
if (updateAlert != null)
{
relNotesToolStripMenuItem.Text = string.Format(Messages.MAINWINDOW_UPDATE_RELEASE, updateAlert.NewVersion.Version);
downloadSourceToolStripMenuItem.Text = string.Format(Messages.DOWNLOAD_SOURCE, updateAlert.NewVersion.Version);
downloadSourceToolStripMenuItem.Text = string.Format(Messages.DOWNLOAD_SOURCE, BrandManager.BrandConsole, updateAlert.NewVersion.Version);
}
var clientVersion = Updates.ClientVersions.FirstOrDefault();
var clientVersion = Updates.ClientVersions.FirstOrDefault();
downloadLatestSourceToolStripMenuItem.Text = clientVersion != null
? string.Format(Messages.DOWNLOAD_SOURCE, clientVersion.Version)
: Messages.DOWNLOAD_LATEST_SOURCE;
? string.Format(Messages.DOWNLOAD_SOURCE, BrandManager.BrandConsole, clientVersion.Version)
: string.Format(Messages.DOWNLOAD_LATEST_SOURCE, BrandManager.BrandConsole);
updateClientToolStripMenuItem.Visible = updateAlert != null;
}

Expand Down
110 changes: 38 additions & 72 deletions XenModel/Actions/Updates/DownloadFileAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,10 @@ public class DownloadFileAction : AsyncAction, IByteProgressAction
private const int SLEEP_TIME_TO_CHECK_DOWNLOAD_STATUS_MS = 900;
private const int SLEEP_TIME_BEFORE_RETRY_MS = 5000;

//If you consider increasing this for any reason (I think 5 is already more than enough), have a look at the usage of SLEEP_TIME_BEFORE_RETRY_MS in DownloadFile() as well.
//If you consider increasing this for any reason (I think 5 is already more than enough),
//have a look at the usage of SLEEP_TIME_BEFORE_RETRY_MS in DownloadFile() as well.
private const int MAX_NUMBER_OF_TRIES = 5;

protected Uri Address => _address;
protected string OutputPathAndFileName => _outputPathAndFileName;
protected string FileName => _fileName;
protected bool CanDownloadFile => _canDownloadFile;

private readonly Uri _address;
private readonly string _outputPathAndFileName;
private readonly string _fileName;
Expand All @@ -66,24 +62,20 @@ public class DownloadFileAction : AsyncAction, IByteProgressAction
private readonly string _authToken;
private WebClient _client;


protected string OutputPathAndFileName => _outputPathAndFileName;
public string ByteProgressDescription { get; set; }



public DownloadFileAction(string fileName, Uri uri, string outputFileName, string title, bool suppressHistory)
protected DownloadFileAction(string fileName, Uri uri, string outputFileName, string title, bool suppressHistory)
: base(null, title, fileName, suppressHistory)
{
_fileName = fileName;
_address = uri;
_canDownloadFile = _address != null;
_outputPathAndFileName = outputFileName;
_authToken = XenAdminConfigManager.Provider.GetClientUpdatesQueryParam();
Title = string.Format(Messages.DOWNLOADING_FILE, fileName);
Description = Title;
}

protected void DownloadFile()
private void DownloadFile()
{
int errorCount = 0;
bool needToRetry = false;
Expand Down Expand Up @@ -195,7 +187,7 @@ protected override void Run()
if (!_canDownloadFile)
return;

log.InfoFormat("Downloading '{0}' (from '{1}') to '{2}'", FileName, _address, _outputPathAndFileName);
log.InfoFormat("Downloading '{0}' (from '{1}') to '{2}'", _fileName, _address, _outputPathAndFileName);
LogDescriptionChanges = false;
DownloadFile();
LogDescriptionChanges = true;
Expand All @@ -207,7 +199,9 @@ protected override void Run()
throw new CancelledException();

if (!File.Exists(_outputPathAndFileName))
throw new Exception(Messages.DOWNLOAD_FILE_NOT_FOUND);
throw new Exception(GetDownloadedFileNotFoundMessage());

ValidateDownloadedFile();

Description = Messages.COMPLETED;
}
Expand All @@ -217,7 +211,16 @@ protected override void CleanOnError()
ReleaseDownloadedContent(true);
}

public void ReleaseDownloadedContent(bool deleteDownloadedContent = false)
protected virtual string GetDownloadedFileNotFoundMessage()
{
return Messages.DOWNLOAD_FILE_ACTION_NOT_FOUND;
}

protected virtual void ValidateDownloadedFile()
{
}

public virtual void ReleaseDownloadedContent(bool deleteDownloadedContent = false)
{
if (!deleteDownloadedContent)
return;
Expand All @@ -237,7 +240,7 @@ private void client_DownloadProgressChanged(object sender, DownloadProgressChang
{
int pc = (int)(95.0 * e.BytesReceived / e.TotalBytesToReceive);

var descr = string.Format(Messages.DOWNLOAD_FILE_ACTION_PROGRESS_DESCRIPTION, FileName,
var descr = string.Format(Messages.DOWNLOAD_FILE_ACTION_PROGRESS_DESCRIPTION, _fileName,
Util.DiskSizeString(e.BytesReceived, "F1"),
Util.DiskSizeString(e.TotalBytesToReceive));
ByteProgressDescription = descr;
Expand All @@ -252,20 +255,20 @@ private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs
if (e.Cancelled)
{
_fileState = DownloadState.Cancelled;
log.DebugFormat("'{0}' download cancelled by the user", FileName);
log.DebugFormat("'{0}' download cancelled by the user", _fileName);
return;
}

if (e.Error != null)
{
_downloadError = e.Error;
log.DebugFormat("'{0}' download failed", FileName);
log.DebugFormat("'{0}' download failed", _fileName);
_fileState = DownloadState.Error;
return;
}

_fileState = DownloadState.Completed;
log.DebugFormat("'{0}' download completed successfully", FileName);
log.DebugFormat("'{0}' download completed successfully", _fileName);
}

public override void RecomputeCanCancel()
Expand All @@ -274,51 +277,28 @@ public override void RecomputeCanCancel()
}
}

public class DownloadAndUpdateClientAction : DownloadFileAction, IByteProgressAction
public class DownloadAndUpdateClientAction : DownloadFileAction
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

private readonly string _checksum;
private FileStream _msiStream;

public DownloadAndUpdateClientAction(string updateName, Uri uri, string outputFileName, string checksum)
: base(updateName,
public DownloadAndUpdateClientAction(string installerName, Uri uri, string outputFileName, string checksum)
: base(installerName,
uri,
outputFileName,
$"{updateName}.msi",
string.Format(Messages.DOWNLOAD_CLIENT_INSTALLER_ACTION_TITLE, installerName),
true)
{
_checksum = checksum;
Description = string.Format(Messages.DOWNLOAD_CLIENT_INSTALLER_ACTION_DESCRIPTION, installerName);
}

protected override void Run()
protected override string GetDownloadedFileNotFoundMessage()
{
if (!CanDownloadFile)
return;

log.InfoFormat("Downloading '{0}' installer (from '{1}') to '{2}'", FileName, Address, OutputPathAndFileName);
Description = string.Format(Messages.DOWNLOADING_FILE, FileName);
LogDescriptionChanges = false;
DownloadFile();
LogDescriptionChanges = true;

if (IsCompleted || Cancelled)
return;

if (Cancelling)
throw new CancelledException();

if (!File.Exists(OutputPathAndFileName))
throw new Exception(Messages.DOWNLOAD_CLIENT_INSTALLER_MSI_NOT_FOUND);

ValidateMsi();

Description = Messages.COMPLETED;
return Messages.DOWNLOAD_CLIENT_INSTALLER_MSI_NOT_FOUND;
}



private void ValidateMsi()
protected override void ValidateDownloadedFile()
{
Description = Messages.UPDATE_CLIENT_VALIDATING_INSTALLER;

Expand Down Expand Up @@ -353,38 +333,24 @@ private void ValidateMsi()
throw new Exception(Messages.UPDATE_CLIENT_INVALID_DIGITAL_CERTIFICATE);
}

public new void ReleaseDownloadedContent(bool deleteDownloadedContent = false)
public override void ReleaseDownloadedContent(bool deleteDownloadedContent = false)
{
_msiStream.Dispose();

if (!deleteDownloadedContent)
return;

try
{
if (File.Exists(OutputPathAndFileName))
File.Delete(OutputPathAndFileName);
}
catch
{
//ignore
}
base.ReleaseDownloadedContent(deleteDownloadedContent);
}
}

public class DownloadSourceAction : DownloadFileAction, IByteProgressAction
public class DownloadSourceAction : DownloadFileAction
{
public DownloadSourceAction(string sourceName, Version version, Uri uri, string outputFileName)
: base(String.Format(Messages.SOURCE_FILE_NAME, sourceName, version),
public DownloadSourceAction(string sourceName, Uri uri, string outputFileName)
: base(sourceName,
uri,
outputFileName,
string.Format(Messages.DOWNLOADING_FILE, version.ToString() + " " + outputFileName),
string.Format(Messages.DOWNLOAD_CLIENT_SOURCE_ACTION_TITLE, sourceName),
false)
{

Description = string.Format(Messages.DOWNLOAD_CLIENT_SOURCE_ACTION_DESCRIPTION, sourceName);
}
}


}

3 changes: 0 additions & 3 deletions XenModel/BrandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
using System.Diagnostics;
using System.Reflection;
using System.Resources;
using System.Web.UI;
using XenAdmin.Properties;


Expand Down Expand Up @@ -114,8 +113,6 @@ static BrandManager()

public static readonly string CfuUrl;

public static readonly string SourceUrl;

public static readonly string VmTools;

public static readonly string XenHost;
Expand Down
Loading

0 comments on commit ed71d8e

Please sign in to comment.