jellyfin-server/MediaBrowser.ClickOnce/ApplicationUpdateCheck.cs

107 lines
4.0 KiB
C#
Raw Normal View History

2013-02-22 04:23:06 +00:00
using MediaBrowser.Model.Updates;
2013-02-21 01:33:05 +00:00
using System;
using System.Deployment.Application;
using System.Threading;
using System.Threading.Tasks;
2013-02-22 04:23:06 +00:00
namespace MediaBrowser.ClickOnce
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// Class ApplicationUpdateCheck
/// </summary>
public class ApplicationUpdateCheck
{
/// <summary>
/// The _task completion source
/// </summary>
2013-02-22 04:23:06 +00:00
private TaskCompletionSource<CheckForUpdateResult> _taskCompletionSource;
2013-02-21 01:33:05 +00:00
/// <summary>
/// The _progress
/// </summary>
2013-02-22 04:23:06 +00:00
private IProgress<double> _progress;
2013-02-21 01:33:05 +00:00
/// <summary>
/// Checks for application update.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <param name="progress">The progress.</param>
/// <returns>Task{CheckForUpdateCompletedEventArgs}.</returns>
/// <exception cref="System.InvalidOperationException">Current deployment is not a ClickOnce deployment</exception>
2013-02-22 04:23:06 +00:00
public Task<CheckForUpdateResult> CheckForApplicationUpdate(CancellationToken cancellationToken, IProgress<double> progress)
2013-02-21 01:33:05 +00:00
{
if (!ApplicationDeployment.IsNetworkDeployed)
{
throw new InvalidOperationException("Current deployment is not network deployed.");
}
_progress = progress;
2013-02-22 04:23:06 +00:00
_taskCompletionSource = new TaskCompletionSource<CheckForUpdateResult>();
2013-02-21 01:33:05 +00:00
var deployment = ApplicationDeployment.CurrentDeployment;
cancellationToken.Register(deployment.CheckForUpdateAsyncCancel);
cancellationToken.ThrowIfCancellationRequested();
deployment.CheckForUpdateCompleted += deployment_CheckForUpdateCompleted;
deployment.CheckForUpdateProgressChanged += deployment_CheckForUpdateProgressChanged;
deployment.CheckForUpdateAsync();
return _taskCompletionSource.Task;
}
2013-02-22 04:23:06 +00:00
/// <summary>
/// To the result.
/// </summary>
/// <param name="args">The <see cref="CheckForUpdateCompletedEventArgs" /> instance containing the event data.</param>
/// <returns>CheckForUpdateResult.</returns>
private CheckForUpdateResult ToResult(CheckForUpdateCompletedEventArgs args)
{
return new CheckForUpdateResult
{
AvailableVersion = args.AvailableVersion,
IsUpdateAvailable = args.UpdateAvailable
};
}
2013-02-21 01:33:05 +00:00
/// <summary>
/// Handles the CheckForUpdateCompleted event of the deployment control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="CheckForUpdateCompletedEventArgs" /> instance containing the event data.</param>
void deployment_CheckForUpdateCompleted(object sender, CheckForUpdateCompletedEventArgs e)
{
var deployment = ApplicationDeployment.CurrentDeployment;
deployment.CheckForUpdateCompleted -= deployment_CheckForUpdateCompleted;
deployment.CheckForUpdateProgressChanged -= deployment_CheckForUpdateProgressChanged;
if (e.Error != null)
{
_taskCompletionSource.SetException(e.Error);
}
else if (e.Cancelled)
{
_taskCompletionSource.SetCanceled();
}
else
{
2013-02-22 04:23:06 +00:00
_taskCompletionSource.SetResult(ToResult(e));
2013-02-21 01:33:05 +00:00
}
}
/// <summary>
/// Handles the CheckForUpdateProgressChanged event of the deployment control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="DeploymentProgressChangedEventArgs" /> instance containing the event data.</param>
void deployment_CheckForUpdateProgressChanged(object sender, DeploymentProgressChangedEventArgs e)
{
2013-02-22 04:23:06 +00:00
_progress.Report(e.ProgressPercentage);
2013-02-21 01:33:05 +00:00
}
}
}