OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / Functions / Main.cs
index 04d4d16..d2e136a 100644 (file)
@@ -13,6 +13,7 @@ using System.Collections.Generic;
 using System.Xml.Serialization;\r
 using System.Threading;\r
 using Handbrake.EncodeQueue;\r
+using System.Net;\r
 \r
 namespace Handbrake.Functions\r
 {\r
@@ -161,10 +162,13 @@ namespace Handbrake.Functions
 \r
                 // Add the appropriate file extension\r
                 if (format == 0)\r
-                    destination_filename += ".mp4";\r
+                {\r
+                    if (Properties.Settings.Default.useM4v)\r
+                        destination_filename += ".m4v";\r
+                    else\r
+                        destination_filename += ".mp4";\r
+                }\r
                 else if (format == 1)\r
-                    destination_filename += ".m4v";\r
-                else if (format == 2)\r
                     destination_filename += ".mkv";\r
 \r
                 // Now work out the path where the file will be stored.\r
@@ -189,40 +193,6 @@ namespace Handbrake.Functions
         }\r
 \r
         /// <summary>\r
-        /// Checks for updates and returns true if an update is available.\r
-        /// </summary>\r
-        /// <param name="debug">Turns on debug mode. Don't use on program startup</param>\r
-        /// <returns>Boolean True = Update available</returns>\r
-        public static Boolean updateCheck(Boolean debug)\r
-        {\r
-            try\r
-            {\r
-                AppcastReader rssRead = new AppcastReader();\r
-                rssRead.getInfo(); // Initializes the class.\r
-                string build = rssRead.build;\r
-\r
-                int latest = int.Parse(build);\r
-                int current = Properties.Settings.Default.hb_build;\r
-                int skip = Properties.Settings.Default.skipversion;\r
-\r
-                if (latest == skip)\r
-                    return false;\r
-\r
-                Properties.Settings.Default.lastUpdateCheckDate = DateTime.Now;\r
-                Properties.Settings.Default.Save();\r
-\r
-                Boolean update = (latest > current);\r
-                return update;\r
-            }\r
-            catch (Exception exc)\r
-            {\r
-                if (debug)\r
-                    MessageBox.Show("Unable to check for updates, Please try again later. \n" + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
-                return false;\r
-            }\r
-        }\r
-\r
-        /// <summary>\r
         /// Get's HandBrakes version data from the CLI.\r
         /// </summary>\r
         /// <returns>Arraylist of Version Data. 0 = hb_version 1 = hb_build</returns>\r
@@ -370,5 +340,92 @@ namespace Handbrake.Functions
             }\r
         }\r
 \r
+        /// <summary>\r
+        /// Begins checking for an update to HandBrake.\r
+        /// </summary>\r
+        /// <param name="callback">The method that will be called when the check is finished.</param>\r
+        /// <param name="debug">Whether or not to execute this in debug mode.</param>\r
+        public static void BeginCheckForUpdates(AsyncCallback callback, bool debug)\r
+        {\r
+            ThreadPool.QueueUserWorkItem(new WaitCallback(delegate\r
+            {\r
+                try\r
+                {\r
+                    // Is this a stable or unstable build?\r
+                    string url = Properties.Settings.Default.hb_build.ToString().EndsWith("1") ? Properties.Settings.Default.appcast_unstable : Properties.Settings.Default.appcast;\r
+\r
+                    // Initialize variables\r
+                    WebRequest request = WebRequest.Create(url);\r
+                    WebResponse response = request.GetResponse();\r
+                    AppcastReader reader = new AppcastReader();\r
+\r
+                    // Get the data, convert it to a string, and parse it into the AppcastReader\r
+                    reader.getInfo(new StreamReader(response.GetResponseStream()).ReadToEnd());\r
+\r
+                    // Further parse the information\r
+                    string build = reader.build;\r
+\r
+                    int latest = int.Parse(build);\r
+                    int current = Properties.Settings.Default.hb_build;\r
+                    int skip = Properties.Settings.Default.skipversion;\r
+\r
+                    // If the user wanted to skip this version, don't report the update\r
+                    if (latest == skip)\r
+                    {\r
+                        UpdateCheckInformation info = new UpdateCheckInformation() { NewVersionAvailable = false, BuildInformation = null };\r
+                        callback(new UpdateCheckResult(debug, info));\r
+                        return;\r
+                    }\r
+\r
+                    // Set when the last update was\r
+                    Properties.Settings.Default.lastUpdateCheckDate = DateTime.Now;\r
+                    Properties.Settings.Default.Save();\r
+\r
+                    UpdateCheckInformation info2 = new UpdateCheckInformation() { NewVersionAvailable = latest > current, BuildInformation = reader };\r
+                    callback(new UpdateCheckResult(debug, info2));\r
+                }\r
+                catch (Exception exc)\r
+                {\r
+                    callback(new UpdateCheckResult(debug, new UpdateCheckInformation() { Error = exc }));\r
+                }\r
+            }));\r
+        }\r
+\r
+        /// <summary>\r
+        /// \r
+        /// </summary>\r
+        /// <param name="result"></param>\r
+        /// <returns></returns>\r
+        public static UpdateCheckInformation EndCheckForUpdates(IAsyncResult result)\r
+        {\r
+            UpdateCheckResult checkResult = (UpdateCheckResult)result;\r
+            return checkResult.Result;\r
+        }\r
+\r
+        /// <summary>\r
+        /// Used in EndUpdateCheck() for update checking and the IAsyncResult design pattern.\r
+        /// </summary>\r
+        private class UpdateCheckResult : IAsyncResult\r
+        {\r
+            public UpdateCheckResult(object asyncState, UpdateCheckInformation info)\r
+            {\r
+                AsyncState = asyncState;\r
+                Result = info;\r
+            }\r
+\r
+            /// <summary>\r
+            /// Gets whether the check was executed in debug mode.\r
+            /// </summary>\r
+            public object AsyncState { get; private set; }\r
+\r
+            /// <summary>\r
+            /// Gets the result of the update check.\r
+            /// </summary>\r
+            public UpdateCheckInformation Result { get; private set; }\r
+\r
+            public WaitHandle AsyncWaitHandle { get { throw new NotImplementedException(); } }\r
+            public bool CompletedSynchronously { get { throw new NotImplementedException(); } }\r
+            public bool IsCompleted { get { throw new NotImplementedException(); } }\r
+        }\r
     }\r
 }\r