PageRenderTime 46ms CodeModel.GetById 22ms 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. //===================================================================================
  17. using System;
  18. using System.Net;
  19. namespace Microsoft.Practices.Prism.Modularity
  20. {
  21. /// <summary>
  22. /// Defines the component used to download files.
  23. /// </summary>
  24. /// <remarks>This is mainly a wrapper for the <see cref="WebClient"/> class that implements <see cref="IFileDownloader"/>.</remarks>
  25. public class FileDownloader : IFileDownloader
  26. {
  27. private readonly WebClient webClient = new WebClient();
  28. private event EventHandler<DownloadProgressChangedEventArgs> _downloadProgressChanged;
  29. private event EventHandler<DownloadCompletedEventArgs> _downloadCompleted;
  30. /// <summary>
  31. /// Raised whenever the download progress changes.
  32. /// </summary>
  33. public event EventHandler<DownloadProgressChangedEventArgs> DownloadProgressChanged
  34. {
  35. add
  36. {
  37. if (this._downloadProgressChanged == null)
  38. {
  39. this.webClient.DownloadProgressChanged += this.WebClient_DownloadProgressChanged;
  40. }
  41. this._downloadProgressChanged += value;
  42. }
  43. remove
  44. {
  45. this._downloadProgressChanged -= value;
  46. if (this._downloadProgressChanged == null)
  47. {
  48. this.webClient.DownloadProgressChanged -= this.WebClient_DownloadProgressChanged;
  49. }
  50. }
  51. }
  52. /// <summary>
  53. /// Raised download is complete.
  54. /// </summary>
  55. public event EventHandler<DownloadCompletedEventArgs> DownloadCompleted
  56. {
  57. add
  58. {
  59. if (this._downloadCompleted == null)
  60. {
  61. this.webClient.OpenReadCompleted += this.WebClient_OpenReadCompleted;
  62. }
  63. this._downloadCompleted += value;
  64. }
  65. remove
  66. {
  67. this._downloadCompleted -= value;
  68. if (this._downloadCompleted == null)
  69. {
  70. this.webClient.OpenReadCompleted -= this.WebClient_OpenReadCompleted;
  71. }
  72. }
  73. }
  74. /// <summary>
  75. /// Starts downloading asynchronously a file from <paramref name="uri"/>.
  76. /// </summary>
  77. /// <param name="uri">The location of the file to be downloaded.</param>
  78. /// <param name="userToken">Provides a user-specified identifier for the asynchronous task.</param>
  79. public void DownloadAsync(Uri uri, object userToken)
  80. {
  81. this.webClient.OpenReadAsync(uri, userToken);
  82. }
  83. private static DownloadCompletedEventArgs ConvertArgs(OpenReadCompletedEventArgs args)
  84. {
  85. return new DownloadCompletedEventArgs(args.Error == null ? args.Result : null, args.Error, args.Cancelled, args.UserState);
  86. }
  87. void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  88. {
  89. this._downloadProgressChanged(this, e);
  90. }
  91. private void WebClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
  92. {
  93. this._downloadCompleted(this, ConvertArgs(e));
  94. }
  95. }
  96. }