PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Code/Client/Inbox2/Core/Threading/Tasks/Application/CheckForUpdateTask.cs

http://github.com/waseems/inbox2_desktop
C# | 94 lines | 71 code | 19 blank | 4 comment | 4 complexity | 86c299c69caea0ede64111972c5fb2a1 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Xml.Linq;
  6. using Inbox2.Core.Configuration;
  7. using Inbox2.Core.Threading.Repeat;
  8. using Inbox2.Framework;
  9. using Inbox2.Framework.Interfaces.Enumerations;
  10. using Inbox2.Framework.Threading;
  11. using Inbox2.Framework.VirtualMailBox.Entities;
  12. using Inbox2.Platform.Logging;
  13. using Microsoft.Win32;
  14. namespace Inbox2.Core.Threading.Tasks.Application
  15. {
  16. public class CheckForUpdateTask : BackgroundTask
  17. {
  18. public override bool RequiresNetworkConnection
  19. {
  20. get { return true; }
  21. }
  22. string AssetBaseUrl
  23. {
  24. get
  25. {
  26. return String.Format("http://download{0}.inbox2.com/",
  27. String.IsNullOrEmpty(CommandLine.Current.Environment) ? String.Empty : "." + CommandLine.Current.Environment);
  28. }
  29. }
  30. protected override void ExecuteCore()
  31. {
  32. string versionString;
  33. if (CheckNeedsUpgrade(out versionString))
  34. DownloadUpdate(versionString);
  35. //DownloadRssUpdates("Twitter", "http://twitter.com/statuses/user_timeline/16018665.rss");
  36. //DownloadRssUpdates("Blog", "http://feeds.feedburner.com/inbox2");
  37. //DownloadRssUpdates("Facebook", "http://www.facebook.com/feeds/page.php?format=rss20&id=82632103211");
  38. EventBroker.Publish(AppEvents.UpdateCheckFinished);
  39. }
  40. public bool CheckNeedsUpgrade(out string versionString)
  41. {
  42. var wc = new WebClient();
  43. var currentVersion = GetType().Assembly.GetName().Version;
  44. var clientId = SettingsManager.ClientSettings.AppConfiguration.ClientId;
  45. versionString = wc.DownloadString(AssetBaseUrl + String.Format("version/latest?clientId={0}&version={1}", clientId, currentVersion));
  46. return (new Version(versionString) > currentVersion);
  47. }
  48. void DownloadUpdate(string versionString)
  49. {
  50. Logger.Debug("Starting download of upgrade archive", LogSource.BackgroundTask);
  51. var wc = new WebClient();
  52. var filename = Path.GetTempFileName();
  53. string url;
  54. if (String.IsNullOrEmpty(CommandLine.Current.Environment))
  55. url = String.Format("http://cdn.inbox2.com/client/x86/upgrade {0}.zip", versionString);
  56. else
  57. url = String.Format("http://download.{0}.inbox2.com/client/x86/upgrade {1}.zip", CommandLine.Current.Environment, versionString);
  58. wc.DownloadFile(url, filename);
  59. RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\Inbox2\\Upgrade", true);
  60. if (key == null)
  61. key = Registry.CurrentUser.CreateSubKey("Software\\Inbox2\\Upgrade");
  62. key.SetValue("filename", filename);
  63. key.SetValue("version", GetType().Assembly.GetName().Version.ToString());
  64. key.Close();
  65. Logger.Debug("Finished download of upgrade archive", LogSource.BackgroundTask);
  66. Logger.Debug("Pending upgrade filename = {0}", LogSource.BackgroundTask, filename);
  67. }
  68. public override void OnCompleted()
  69. {
  70. // Schedule next execution
  71. new Run("UpdateCheck").After(60).Minutes().Call(Tasks.CheckForUpdate);
  72. base.OnCompleted();
  73. }
  74. }
  75. }