PageRenderTime 31ms CodeModel.GetById 14ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/V4/PrismLibrary/Silverlight/Prism/Modularity/FileDownloader.cs

#
C# | 109 lines | 65 code | 13 blank | 31 comment | 9 complexity | 3dacecc3a3d3edadcc313abedcf98438 MD5 | raw file
  1//===================================================================================
  2// Microsoft patterns & practices
  3// Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4//===================================================================================
  5// Copyright (c) Microsoft Corporation.  All rights reserved.
  6// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9// FITNESS FOR A PARTICULAR PURPOSE.
 10//===================================================================================
 11// The example companies, organizations, products, domain names,
 12// e-mail addresses, logos, people, places, and events depicted
 13// herein are fictitious.  No association with any real company,
 14// organization, product, domain name, email address, logo, person,
 15// places, or events is intended or should be inferred.
 16//===================================================================================
 17using System;
 18using System.Net;
 19
 20namespace Microsoft.Practices.Prism.Modularity
 21{
 22    /// <summary>
 23    /// Defines the component used to download files.
 24    /// </summary>
 25    /// <remarks>This is mainly a wrapper for the <see cref="WebClient"/> class that implements <see cref="IFileDownloader"/>.</remarks>
 26    public class FileDownloader : IFileDownloader
 27    {
 28        private readonly WebClient webClient = new WebClient();
 29
 30        private event EventHandler<DownloadProgressChangedEventArgs> _downloadProgressChanged;
 31        private event EventHandler<DownloadCompletedEventArgs> _downloadCompleted;
 32
 33        /// <summary>
 34        /// Raised whenever the download progress changes.
 35        /// </summary>
 36        public event EventHandler<DownloadProgressChangedEventArgs> DownloadProgressChanged
 37        {
 38            add
 39            {
 40                if (this._downloadProgressChanged == null)
 41                {
 42                    this.webClient.DownloadProgressChanged += this.WebClient_DownloadProgressChanged;
 43                }
 44
 45                this._downloadProgressChanged += value;
 46            }
 47
 48            remove
 49            {
 50                this._downloadProgressChanged -= value;
 51                if (this._downloadProgressChanged == null)
 52                {
 53                    this.webClient.DownloadProgressChanged -= this.WebClient_DownloadProgressChanged;
 54                }
 55            }
 56        }
 57
 58
 59        /// <summary>
 60        /// Raised download is complete.
 61        /// </summary>
 62        public event EventHandler<DownloadCompletedEventArgs> DownloadCompleted
 63        {
 64            add
 65            {
 66                if (this._downloadCompleted == null)
 67                {                    
 68                    this.webClient.OpenReadCompleted += this.WebClient_OpenReadCompleted;
 69                }
 70                
 71                this._downloadCompleted += value;
 72            }
 73
 74            remove
 75            {
 76                this._downloadCompleted -= value;
 77                if (this._downloadCompleted == null)
 78                {
 79                    this.webClient.OpenReadCompleted -= this.WebClient_OpenReadCompleted;
 80                }
 81            }
 82        }                
 83
 84        /// <summary>
 85        /// Starts downloading asynchronously a file from <paramref name="uri"/>.
 86        /// </summary>
 87        /// <param name="uri">The location of the file to be downloaded.</param>
 88        /// <param name="userToken">Provides a user-specified identifier for the asynchronous task.</param>
 89        public void DownloadAsync(Uri uri, object userToken)
 90        {
 91            this.webClient.OpenReadAsync(uri, userToken);
 92        }
 93
 94        private static DownloadCompletedEventArgs ConvertArgs(OpenReadCompletedEventArgs args)
 95        {            
 96            return new DownloadCompletedEventArgs(args.Error == null ? args.Result : null, args.Error, args.Cancelled, args.UserState);
 97        }
 98
 99        void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
100        {
101            this._downloadProgressChanged(this, e);
102        }
103
104        private void WebClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
105        {
106            this._downloadCompleted(this, ConvertArgs(e));
107        }
108    }
109}