OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / Functions / Main.cs
index 84ba263..d2e136a 100644 (file)
@@ -13,13 +13,14 @@ 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
     static class Main\r
     {\r
         // Private Variables\r
-        private static readonly XmlSerializer ser = new XmlSerializer(typeof(List<QueueItem>));\r
+        private static readonly XmlSerializer ser = new XmlSerializer(typeof(List<Job>));\r
 \r
         /// <summary>\r
         /// Calculate the duration of the selected title and chapters\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,37 +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
-                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
@@ -259,7 +232,11 @@ namespace Handbrake.Functions
                         Properties.Settings.Default.hb_version = arr[0];\r
                     }\r
                     if (cliProcess.TotalProcessorTime.Seconds > 10) // Don't wait longer than 10 seconds.\r
-                        killCLI(cliProcess.Id);\r
+                    {\r
+                        Process cli = Process.GetProcessById(cliProcess.Id);\r
+                        if (!cli.HasExited)\r
+                            cli.Kill();\r
+                    }\r
                 }\r
             }\r
             catch (Exception e)\r
@@ -269,16 +246,6 @@ namespace Handbrake.Functions
         }\r
 \r
         /// <summary>\r
-        /// Search through the running processes on the system and kill HandBrakeCLI\r
-        /// </summary>\r
-        private static void killCLI(int id)\r
-        {\r
-            Process cli = Process.GetProcessById(id);\r
-            if (!cli.HasExited)\r
-                cli.Kill();\r
-        }\r
-\r
-        /// <summary>\r
         /// Check if the queue recovery file contains records.\r
         /// If it does, it means the last queue did not complete before HandBrake closed.\r
         /// So, return a boolean if true. \r
@@ -292,7 +259,7 @@ namespace Handbrake.Functions
                 {\r
                     using (FileStream strm = new FileStream(tempPath, FileMode.Open, FileAccess.Read))\r
                     {\r
-                        List<QueueItem> list = ser.Deserialize(strm) as List<QueueItem>;\r
+                        List<Job> list = ser.Deserialize(strm) as List<Job>;\r
                         if (list != null)\r
                             if (list.Count != 0)\r
                                 return true;\r
@@ -353,5 +320,112 @@ namespace Handbrake.Functions
             return -1;\r
         }\r
 \r
+        /// <summary>\r
+        ///  Clear all the encode log files.\r
+        /// </summary>\r
+        public static void clearLogs()\r
+        {\r
+            string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
+            if (Directory.Exists(logDir))\r
+            {\r
+                DirectoryInfo info = new DirectoryInfo(logDir);\r
+                FileInfo[] logFiles = info.GetFiles("*.txt");\r
+                foreach (FileInfo file in logFiles)\r
+                {\r
+                    if (!file.Name.Contains("last_scan_log") && !file.Name.Contains("last_encode_log") && !file.Name.Contains("tmp_appReadable_log.txt"))\r
+                    {\r
+                        File.Delete(file.FullName);\r
+                    }\r
+                }\r
+            }\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