summaryrefslogtreecommitdiffstats
path: root/win/CS/HandBrakeWPF/Services
diff options
context:
space:
mode:
authorsr55 <[email protected]>2012-06-30 14:07:50 +0000
committersr55 <[email protected]>2012-06-30 14:07:50 +0000
commitca023df6b2119d7bf29ade5c07f644fdf2de8731 (patch)
tree6ac3b68670cc9db3876c18239d56f0461ae7953b /win/CS/HandBrakeWPF/Services
parent4d6d9e007cbfadd9f63860038a8048a2c8da390a (diff)
WinGui: Refactor the Update service and add a new options tab to deal with updates.
git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@4799 b64f7644-9d1e-0410-96f1-a4d463321fa5
Diffstat (limited to 'win/CS/HandBrakeWPF/Services')
-rw-r--r--win/CS/HandBrakeWPF/Services/Interfaces/IUpdateService.cs51
-rw-r--r--win/CS/HandBrakeWPF/Services/UpdateService.cs206
2 files changed, 257 insertions, 0 deletions
diff --git a/win/CS/HandBrakeWPF/Services/Interfaces/IUpdateService.cs b/win/CS/HandBrakeWPF/Services/Interfaces/IUpdateService.cs
new file mode 100644
index 000000000..cd1b50e9f
--- /dev/null
+++ b/win/CS/HandBrakeWPF/Services/Interfaces/IUpdateService.cs
@@ -0,0 +1,51 @@
+// --------------------------------------------------------------------------------------------------------------------
+// <copyright file="IUpdateService.cs" company="HandBrake Project (http://handbrake.fr)">
+// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
+// </copyright>
+// <summary>
+// An Interface for the Update Service
+// </summary>
+// --------------------------------------------------------------------------------------------------------------------
+
+namespace HandBrakeWPF.Services.Interfaces
+{
+ using System;
+
+ using HandBrakeWPF.Model;
+
+ /// <summary>
+ /// An Interface for the Update Service
+ /// </summary>
+ public interface IUpdateService
+ {
+ /// <summary>
+ /// Perform an update check at application start, but only daily, weekly or monthly depending on the users settings.
+ /// </summary>
+ /// <param name="callback">
+ /// The callback.
+ /// </param>
+ void PerformStartupUpdateCheck(Action<UpdateCheckInformation> callback);
+
+ /// <summary>
+ /// Perform an Update check and execute the Action when complete.
+ /// </summary>
+ /// <param name="callback">
+ /// The callback.
+ /// </param>
+ void CheckForUpdates(Action<UpdateCheckInformation> callback);
+
+ /// <summary>
+ /// Download the update file.
+ /// </summary>
+ /// <param name="url">
+ /// The url.
+ /// </param>
+ /// <param name="completed">
+ /// The complete.
+ /// </param>
+ /// <param name="progress">
+ /// The progress.
+ /// </param>
+ void DownloadFile(string url, Action<DownloadStatus> completed, Action<DownloadStatus> progress);
+ }
+}
diff --git a/win/CS/HandBrakeWPF/Services/UpdateService.cs b/win/CS/HandBrakeWPF/Services/UpdateService.cs
new file mode 100644
index 000000000..2b6cfa852
--- /dev/null
+++ b/win/CS/HandBrakeWPF/Services/UpdateService.cs
@@ -0,0 +1,206 @@
+// --------------------------------------------------------------------------------------------------------------------
+// <copyright file="UpdateService.cs" company="HandBrake Project (http://handbrake.fr)">
+// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
+// </copyright>
+// <summary>
+// The Update Service
+// </summary>
+// --------------------------------------------------------------------------------------------------------------------
+
+namespace HandBrakeWPF.Services
+{
+ using System;
+ using System.IO;
+ using System.Net;
+ using System.Threading;
+
+ using HandBrake.ApplicationServices;
+ using HandBrake.ApplicationServices.Services.Interfaces;
+ using HandBrake.ApplicationServices.Utilities;
+
+ using HandBrakeWPF.Model;
+ using HandBrakeWPF.Services.Interfaces;
+
+ /// <summary>
+ /// The Update Service
+ /// </summary>
+ public class UpdateService : IUpdateService
+ {
+ #region Constants and Fields
+
+ /// <summary>
+ /// Backing field for the update service
+ /// </summary>
+ private readonly IUserSettingService userSettingService;
+
+ #endregion
+
+ #region Constructors and Destructors
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="UpdateService"/> class.
+ /// </summary>
+ /// <param name="userSettingService">
+ /// The user setting service.
+ /// </param>
+ public UpdateService(IUserSettingService userSettingService)
+ {
+ this.userSettingService = userSettingService;
+ }
+
+ #endregion
+
+ #region Public Methods
+
+ /// <summary>
+ /// Perform an update check at application start, but only daily, weekly or monthly depending on the users settings.
+ /// </summary>
+ /// <param name="callback">
+ /// The callback.
+ /// </param>
+ public void PerformStartupUpdateCheck(Action<UpdateCheckInformation> callback)
+ {
+ // Make sure it's running on the calling thread
+ if (this.userSettingService.GetUserSetting<bool>(UserSettingConstants.UpdateStatus))
+ {
+ if (DateTime.Now.Subtract(this.userSettingService.GetUserSetting<DateTime>(UserSettingConstants.LastUpdateCheckDate)).TotalDays
+ > this.userSettingService.GetUserSetting<int>(UserSettingConstants.DaysBetweenUpdateCheck))
+ {
+ this.userSettingService.SetUserSetting(UserSettingConstants.LastUpdateCheckDate, DateTime.Now);
+
+ this.CheckForUpdates(callback);
+ }
+ }
+ }
+
+ /// <summary>
+ /// Check for Updates
+ /// </summary>
+ /// <param name="callback">
+ /// The callback.
+ /// </param>
+ public void CheckForUpdates(Action<UpdateCheckInformation> callback)
+ {
+ ThreadPool.QueueUserWorkItem(
+ delegate
+ {
+ try
+ {
+ string url =
+ this.userSettingService.GetUserSetting<string>(ASUserSettingConstants.HandBrakePlatform)
+ .Contains("x86_64")
+ ? this.userSettingService.GetUserSetting<string>(UserSettingConstants.Appcast_x64)
+ : this.userSettingService.GetUserSetting<string>(UserSettingConstants.Appcast_i686);
+
+ var currentBuild =
+ this.userSettingService.GetUserSetting<int>(ASUserSettingConstants.HandBrakeBuild);
+ var skipBuild = this.userSettingService.GetUserSetting<int>(
+ UserSettingConstants.Skipversion);
+
+ // Initialize variables
+ WebRequest request = WebRequest.Create(url);
+ WebResponse response = request.GetResponse();
+ var reader = new AppcastReader();
+
+ // Get the data, convert it to a string, and parse it into the AppcastReader
+ reader.GetUpdateInfo(new StreamReader(response.GetResponseStream()).ReadToEnd());
+
+ // Further parse the information
+ string build = reader.Build;
+
+ int latest = int.Parse(build);
+ int current = currentBuild;
+ int skip = skipBuild;
+
+ // If the user wanted to skip this version, don't report the update
+ if (latest == skip)
+ {
+ var info = new UpdateCheckInformation { NewVersionAvailable = false };
+ callback(info);
+ return;
+ }
+
+ var info2 = new UpdateCheckInformation
+ {
+ NewVersionAvailable = latest > current,
+ DescriptionUrl = reader.DescriptionUrl,
+ DownloadFile = reader.DownloadFile,
+ Build = reader.Build,
+ Version = reader.Version,
+ };
+
+ callback(info2);
+ }
+ catch (Exception exc)
+ {
+ callback(new UpdateCheckInformation { NewVersionAvailable = false, Error = exc });
+ }
+ });
+ }
+
+ /// <summary>
+ /// Download the update file.
+ /// </summary>
+ /// <param name="url">
+ /// The url.
+ /// </param>
+ /// <param name="completed">
+ /// The complete.
+ /// </param>
+ /// <param name="progress">
+ /// The progress.
+ /// </param>
+ public void DownloadFile(string url, Action<DownloadStatus> completed, Action<DownloadStatus> progress)
+ {
+ ThreadPool.QueueUserWorkItem(
+ delegate
+ {
+ string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe");
+ WebClient wcDownload = new WebClient();
+
+ try
+ {
+ if (File.Exists(tempPath))
+ File.Delete(tempPath);
+
+ HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
+ webRequest.Credentials = CredentialCache.DefaultCredentials;
+ HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
+ long fileSize = webResponse.ContentLength;
+
+ Stream responceStream = wcDownload.OpenRead(url);
+ Stream localStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.None);
+
+ int bytesSize;
+ byte[] downBuffer = new byte[2048];
+
+ long flength = 0;
+ while ((bytesSize = responceStream.Read(downBuffer, 0, downBuffer.Length)) > 0)
+ {
+ localStream.Write(downBuffer, 0, bytesSize);
+ flength = localStream.Length;
+ progress(new DownloadStatus { BytesRead = localStream.Length, TotalBytes = fileSize});
+ }
+
+ responceStream.Close();
+ localStream.Close();
+
+ completed(
+ flength != fileSize
+ ? new DownloadStatus
+ {
+ WasSuccessful = false,
+ Message = "Partial Download. File is Incomplete. Please Retry the download."
+ }
+ : new DownloadStatus { WasSuccessful = true, Message = "Download Complete." });
+ }
+ catch (Exception exc)
+ {
+ progress(new DownloadStatus { WasSuccessful = false, Exception = exc, Message = "Download Failed." });
+ }
+ });
+ }
+
+ #endregion
+ }
+} \ No newline at end of file