aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.ClickOnce/ApplicationUpdateCheck.cs
blob: 72c42f586638a9ceb0f6720b770fbfc2c18d60dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using MediaBrowser.Model.Updates;
using System;
using System.Deployment.Application;
using System.Threading;
using System.Threading.Tasks;

namespace MediaBrowser.ClickOnce
{
    /// <summary>
    /// Class ApplicationUpdateCheck
    /// </summary>
    public class ApplicationUpdateCheck
    {
        /// <summary>
        /// The _task completion source
        /// </summary>
        private TaskCompletionSource<CheckForUpdateResult> _taskCompletionSource;

        /// <summary>
        /// The _progress
        /// </summary>
        private IProgress<double> _progress;

        /// <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>
        public Task<CheckForUpdateResult> CheckForApplicationUpdate(CancellationToken cancellationToken, IProgress<double> progress)
        {
            if (!ApplicationDeployment.IsNetworkDeployed)
            {
                throw new InvalidOperationException("Current deployment is not network deployed.");
            }

            _progress = progress;

            _taskCompletionSource = new TaskCompletionSource<CheckForUpdateResult>();

            var deployment = ApplicationDeployment.CurrentDeployment;

            cancellationToken.Register(deployment.CheckForUpdateAsyncCancel);

            cancellationToken.ThrowIfCancellationRequested();

            deployment.CheckForUpdateCompleted += deployment_CheckForUpdateCompleted;
            deployment.CheckForUpdateProgressChanged += deployment_CheckForUpdateProgressChanged;

            deployment.CheckForUpdateAsync();

            return _taskCompletionSource.Task;
        }

        /// <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
            };
        }

        /// <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
            {
                _taskCompletionSource.SetResult(ToResult(e));
            }
        }

        /// <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)
        {
            _progress.Report(e.ProgressPercentage);
        }
    }
}