diff options
author | sr55 <[email protected]> | 2009-07-04 14:24:59 +0000 |
---|---|---|
committer | sr55 <[email protected]> | 2009-07-04 14:24:59 +0000 |
commit | bdf19259ac2d6ac94abf0fad38ce6c98e3e58460 (patch) | |
tree | 3b92fa3d9ef24eeb3e71a45574b1c7f96d720558 /win/C#/Functions | |
parent | f3ea70992ab591a46754f68f5b7b02ca9d0529cc (diff) |
WinGui:
- Update checker code cleaned up and multi-threaded. (Thanks to darkassassin ) Made some minor changes to this code to fit it in. http://forum.handbrake.fr/viewtopic.php?f=4&t=11353
- Queue window no longer takes focus on each new addition to the queue. ( Quite annoying otherwise )
git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@2661 b64f7644-9d1e-0410-96f1-a4d463321fa5
Diffstat (limited to 'win/C#/Functions')
-rw-r--r-- | win/C#/Functions/AppcastReader.cs | 11 | ||||
-rw-r--r-- | win/C#/Functions/Main.cs | 122 | ||||
-rw-r--r-- | win/C#/Functions/UpdateCheckInformation.cs | 23 |
3 files changed, 117 insertions, 39 deletions
diff --git a/win/C#/Functions/AppcastReader.cs b/win/C#/Functions/AppcastReader.cs index 944258597..861bd0bae 100644 --- a/win/C#/Functions/AppcastReader.cs +++ b/win/C#/Functions/AppcastReader.cs @@ -7,6 +7,7 @@ using System;
using System.Xml;
using System.Text.RegularExpressions;
+using System.IO;
namespace Handbrake.Functions
{
@@ -15,15 +16,15 @@ namespace Handbrake.Functions /// <summary>
/// Get the build information from the required appcasts. Run before accessing the public vars.
/// </summary>
- public void getInfo()
+ public void getInfo(string input)
{
// Get the correct Appcast and set input.
- XmlNode nodeItem = Properties.Settings.Default.hb_build.ToString().EndsWith("1") ? readRss(new XmlTextReader(Properties.Settings.Default.appcast_unstable)) : readRss(new XmlTextReader(Properties.Settings.Default.appcast));
- string input = nodeItem.InnerXml;
+ XmlNode nodeItem = readRss(new XmlTextReader(new StringReader(input)));
+ string result = nodeItem.InnerXml;
// Regular Expressions
- Match ver = Regex.Match(input, @"sparkle:version=""([0-9]*)\""");
- Match verShort = Regex.Match(input, @"sparkle:shortVersionString=""([0-9].[0-9].[0-9]*)\""");
+ Match ver = Regex.Match(result, @"sparkle:version=""([0-9]*)\""");
+ Match verShort = Regex.Match(result, @"sparkle:shortVersionString=""([0-9].[0-9].[0-9]*)\""");
build = ver.ToString().Replace("sparkle:version=", "").Replace("\"", "");
version = verShort.ToString().Replace("sparkle:shortVersionString=", "").Replace("\"", "");
diff --git a/win/C#/Functions/Main.cs b/win/C#/Functions/Main.cs index 04d4d16b4..1b223babd 100644 --- a/win/C#/Functions/Main.cs +++ b/win/C#/Functions/Main.cs @@ -13,6 +13,7 @@ using System.Collections.Generic; using System.Xml.Serialization;
using System.Threading;
using Handbrake.EncodeQueue;
+using System.Net;
namespace Handbrake.Functions
{
@@ -189,40 +190,6 @@ namespace Handbrake.Functions }
/// <summary>
- /// Checks for updates and returns true if an update is available.
- /// </summary>
- /// <param name="debug">Turns on debug mode. Don't use on program startup</param>
- /// <returns>Boolean True = Update available</returns>
- public static Boolean updateCheck(Boolean debug)
- {
- try
- {
- AppcastReader rssRead = new AppcastReader();
- rssRead.getInfo(); // Initializes the class.
- string build = rssRead.build;
-
- int latest = int.Parse(build);
- int current = Properties.Settings.Default.hb_build;
- int skip = Properties.Settings.Default.skipversion;
-
- if (latest == skip)
- return false;
-
- Properties.Settings.Default.lastUpdateCheckDate = DateTime.Now;
- Properties.Settings.Default.Save();
-
- Boolean update = (latest > current);
- return update;
- }
- catch (Exception exc)
- {
- if (debug)
- MessageBox.Show("Unable to check for updates, Please try again later. \n" + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return false;
- }
- }
-
- /// <summary>
/// Get's HandBrakes version data from the CLI.
/// </summary>
/// <returns>Arraylist of Version Data. 0 = hb_version 1 = hb_build</returns>
@@ -370,5 +337,92 @@ namespace Handbrake.Functions }
}
+ /// <summary>
+ /// Begins checking for an update to HandBrake.
+ /// </summary>
+ /// <param name="callback">The method that will be called when the check is finished.</param>
+ /// <param name="debug">Whether or not to execute this in debug mode.</param>
+ public static void BeginCheckForUpdates(AsyncCallback callback, bool debug)
+ {
+ ThreadPool.QueueUserWorkItem(new WaitCallback(delegate
+ {
+ try
+ {
+ // Is this a stable or unstable build?
+ string url = Properties.Settings.Default.hb_build.ToString().EndsWith("1") ? Properties.Settings.Default.appcast_unstable : Properties.Settings.Default.appcast;
+
+ // Initialize variables
+ WebRequest request = WebRequest.Create(url);
+ WebResponse response = request.GetResponse();
+ AppcastReader reader = new AppcastReader();
+
+ // Get the data, convert it to a string, and parse it into the AppcastReader
+ reader.getInfo(new StreamReader(response.GetResponseStream()).ReadToEnd());
+
+ // Further parse the information
+ string build = reader.build;
+
+ int latest = int.Parse(build);
+ int current = Properties.Settings.Default.hb_build;
+ int skip = Properties.Settings.Default.skipversion;
+
+ // If the user wanted to skip this version, don't report the update
+ if (latest == skip)
+ {
+ UpdateCheckInformation info = new UpdateCheckInformation() { NewVersionAvailable = false, BuildInformation = null };
+ callback(new UpdateCheckResult(debug, info));
+ return;
+ }
+
+ // Set when the last update was
+ Properties.Settings.Default.lastUpdateCheckDate = DateTime.Now;
+ Properties.Settings.Default.Save();
+
+ UpdateCheckInformation info2 = new UpdateCheckInformation() { NewVersionAvailable = latest > current, BuildInformation = reader };
+ callback(new UpdateCheckResult(debug, info2));
+ }
+ catch (Exception exc)
+ {
+ callback(new UpdateCheckResult(debug, new UpdateCheckInformation() { Error = exc }));
+ }
+ }));
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="result"></param>
+ /// <returns></returns>
+ public static UpdateCheckInformation EndCheckForUpdates(IAsyncResult result)
+ {
+ UpdateCheckResult checkResult = (UpdateCheckResult)result;
+ return checkResult.Result;
+ }
+
+ /// <summary>
+ /// Used in EndUpdateCheck() for update checking and the IAsyncResult design pattern.
+ /// </summary>
+ private class UpdateCheckResult : IAsyncResult
+ {
+ public UpdateCheckResult(object asyncState, UpdateCheckInformation info)
+ {
+ AsyncState = asyncState;
+ Result = info;
+ }
+
+ /// <summary>
+ /// Gets whether the check was executed in debug mode.
+ /// </summary>
+ public object AsyncState { get; private set; }
+
+ /// <summary>
+ /// Gets the result of the update check.
+ /// </summary>
+ public UpdateCheckInformation Result { get; private set; }
+
+ public WaitHandle AsyncWaitHandle { get { throw new NotImplementedException(); } }
+ public bool CompletedSynchronously { get { throw new NotImplementedException(); } }
+ public bool IsCompleted { get { throw new NotImplementedException(); } }
+ }
}
}
diff --git a/win/C#/Functions/UpdateCheckInformation.cs b/win/C#/Functions/UpdateCheckInformation.cs new file mode 100644 index 000000000..253817866 --- /dev/null +++ b/win/C#/Functions/UpdateCheckInformation.cs @@ -0,0 +1,23 @@ +using System;
+
+namespace Handbrake.Functions
+{
+ /// <summary>
+ /// Provides information about an update check.
+ /// </summary>
+ public struct UpdateCheckInformation
+ {
+ public bool NewVersionAvailable { get; set; }
+ public bool ErrorOccured { get { return Error != null; } }
+
+ /// <summary>
+ /// Gets information about the new build, if any. This will be null if there is no new verison.
+ /// </summary>
+ public AppcastReader BuildInformation { get; set; }
+
+ /// <summary>
+ /// Gets the error that occurred, if any. This will be null if no error occured.
+ /// </summary>
+ public Exception Error { get; set; }
+ }
+}
|