OSDN Git Service

LinGui: make Help->Guide work on windows/mingw
[handbrake-jp/handbrake-jp-git.git] / win / C# / Services / Queue.cs
index 695b585..d3d9430 100644 (file)
@@ -9,6 +9,7 @@ namespace Handbrake.Services
     using System.Collections.Generic;\r
     using System.Collections.ObjectModel;\r
     using System.IO;\r
+    using System.Linq;\r
     using System.Threading;\r
     using System.Windows.Forms;\r
     using System.Xml.Serialization;\r
@@ -36,6 +37,17 @@ namespace Handbrake.Services
         private int nextJobId;\r
 \r
         /// <summary>\r
+        /// Fires when the Queue has started\r
+        /// </summary>\r
+        public event EventHandler QueueStarted;\r
+\r
+        /// <summary>\r
+        /// Fires when a job is Added, Removed or Re-Ordered.\r
+        /// Should be used for triggering an update of the Queue Window.\r
+        /// </summary>\r
+        public event EventHandler QueueListChanged;\r
+\r
+        /// <summary>\r
         /// Fires when a pause to the encode queue has been requested.\r
         /// </summary>\r
         public event EventHandler QueuePauseRequested;\r
@@ -45,22 +57,17 @@ namespace Handbrake.Services
         /// </summary>\r
         public event EventHandler QueueCompleted;\r
 \r
-        #region Queue\r
-\r
+        #region Properties\r
         /// <summary>\r
-        /// Gets and removes the next job in the queue.\r
+        /// Gets or sets the last encode that was processed.\r
         /// </summary>\r
-        /// <returns>The job that was removed from the queue.</returns>\r
-        private Job GetNextJob()\r
-        {\r
-            Job job = this.queue[0];\r
-            this.LastEncode = job;\r
-            this.Remove(0); // Remove the item which we are about to pass out.\r
-\r
-            this.WriteQueueStateToFile("hb_queue_recovery.xml");\r
+        /// <returns></returns> \r
+        public Job LastEncode { get; set; }\r
 \r
-            return job;\r
-        }\r
+        /// <summary>\r
+        /// Gets a value indicating whether Request Pause\r
+        /// </summary>\r
+        public bool Paused { get; private set; }\r
 \r
         /// <summary>\r
         /// Gets the current state of the encode queue.\r
@@ -77,6 +84,24 @@ namespace Handbrake.Services
         {\r
             get { return this.queue.Count; }\r
         }\r
+        #endregion\r
+\r
+        #region Queue\r
+\r
+        /// <summary>\r
+        /// Gets and removes the next job in the queue.\r
+        /// </summary>\r
+        /// <returns>The job that was removed from the queue.</returns>\r
+        private Job GetNextJob()\r
+        {\r
+            Job job = this.queue[0];\r
+            this.LastEncode = job;\r
+            this.Remove(0); // Remove the item which we are about to pass out.\r
+\r
+            this.WriteQueueStateToFile("hb_queue_recovery.xml");\r
+\r
+            return job;\r
+        }\r
 \r
         /// <summary>\r
         /// Adds an item to the queue.\r
@@ -102,14 +127,17 @@ namespace Handbrake.Services
                              {\r
                                  Id = this.nextJobId++,\r
                                  Title = title,\r
-                                 Query = query, \r
-                                 Source = source, \r
-                                 Destination = destination, \r
+                                 Query = query,\r
+                                 Source = source,\r
+                                 Destination = destination,\r
                                  CustomQuery = customJob\r
                              };\r
 \r
             this.queue.Add(newJob);\r
             this.WriteQueueStateToFile("hb_queue_recovery.xml");\r
+\r
+            if (this.QueueListChanged != null)\r
+                this.QueueListChanged(this, new EventArgs());\r
         }\r
 \r
         /// <summary>\r
@@ -120,6 +148,9 @@ namespace Handbrake.Services
         {\r
             this.queue.RemoveAt(index);\r
             this.WriteQueueStateToFile("hb_queue_recovery.xml");\r
+\r
+            if (this.QueueListChanged != null)\r
+                this.QueueListChanged(this, new EventArgs());\r
         }\r
 \r
         /// <summary>\r
@@ -150,6 +181,9 @@ namespace Handbrake.Services
             }\r
 \r
             WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file\r
+\r
+            if (this.QueueListChanged != null)\r
+                this.QueueListChanged(this, new EventArgs());\r
         }\r
 \r
         /// <summary>\r
@@ -167,6 +201,9 @@ namespace Handbrake.Services
             }\r
 \r
             this.WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file\r
+\r
+            if (this.QueueListChanged != null)\r
+                this.QueueListChanged(this, new EventArgs());\r
         }\r
 \r
         /// <summary>\r
@@ -175,7 +212,7 @@ namespace Handbrake.Services
         /// <param name="file">The location of the file to write the queue to.</param>\r
         public void WriteQueueStateToFile(string file)\r
         {\r
-            string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), \r
+            string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),\r
                                               @"HandBrake\hb_queue_recovery.xml");\r
             string tempPath = file == "hb_queue_recovery.xml" ? appDataPath : file;\r
 \r
@@ -184,7 +221,7 @@ namespace Handbrake.Services
                 using (FileStream strm = new FileStream(tempPath, FileMode.Create, FileAccess.Write))\r
                 {\r
                     if (serializer == null)\r
-                        serializer = new XmlSerializer(typeof (List<Job>));\r
+                        serializer = new XmlSerializer(typeof(List<Job>));\r
                     serializer.Serialize(strm, queue);\r
                     strm.Close();\r
                     strm.Dispose();\r
@@ -226,14 +263,12 @@ namespace Handbrake.Services
                         line.WriteLine(strCmdLine);\r
                     }\r
 \r
-                    MessageBox.Show("Your batch script has been sucessfully saved.", "Status", MessageBoxButtons.OK, \r
+                    MessageBox.Show("Your batch script has been sucessfully saved.", "Status", MessageBoxButtons.OK,\r
                                     MessageBoxIcon.Asterisk);\r
                 }\r
-                catch (Exception)\r
+                catch (Exception exc)\r
                 {\r
-                    MessageBox.Show(\r
-                        "Unable to write to the file. Please make sure that the location has the correct permissions for file writing.", \r
-                        "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
+                    Main.ShowExceptiowWindow("Unable to write to the file. Please make sure that the location has the correct permissions for file writing.", exc.ToString());\r
                 }\r
             }\r
         }\r
@@ -244,7 +279,7 @@ namespace Handbrake.Services
         /// <param name="file">The location of the file to read the queue from.</param>\r
         public void LoadQueueFromFile(string file)\r
         {\r
-            string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), \r
+            string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),\r
                                               @"HandBrake\hb_queue_recovery.xml");\r
             string tempPath = file == "hb_queue_recovery.xml" ? appDataPath : file;\r
 \r
@@ -255,7 +290,7 @@ namespace Handbrake.Services
                     if (strm.Length != 0)\r
                     {\r
                         if (serializer == null)\r
-                            serializer = new XmlSerializer(typeof (List<Job>));\r
+                            serializer = new XmlSerializer(typeof(List<Job>));\r
 \r
                         List<Job> list = serializer.Deserialize(strm) as List<Job>;\r
 \r
@@ -277,13 +312,7 @@ namespace Handbrake.Services
         /// <returns>Whether or not the supplied destination is already in the queue.</returns>\r
         public bool CheckForDestinationDuplicate(string destination)\r
         {\r
-            foreach (Job checkItem in this.queue)\r
-            {\r
-                if (checkItem.Destination.Contains(destination.Replace("\\\\", "\\")))\r
-                    return true;\r
-            }\r
-\r
-            return false;\r
+            return this.queue.Any(checkItem => checkItem.Destination.Contains(destination.Replace("\\\\", "\\")));\r
         }\r
 \r
         #endregion\r
@@ -291,17 +320,6 @@ namespace Handbrake.Services
         #region Encoding\r
 \r
         /// <summary>\r
-        /// Gets or sets the last encode that was processed.\r
-        /// </summary>\r
-        /// <returns></returns> \r
-        public Job LastEncode { get; set; }\r
-\r
-        /// <summary>\r
-        /// Gets a value indicating whether Request Pause\r
-        /// </summary>\r
-        public bool PauseRequested { get; private set; }\r
-\r
-        /// <summary>\r
         /// Starts encoding the first job in the queue and continues encoding until all jobs\r
         /// have been encoded.\r
         /// </summary>\r
@@ -309,19 +327,22 @@ namespace Handbrake.Services
         {\r
             if (this.Count != 0)\r
             {\r
-                if (this.PauseRequested)\r
-                    this.PauseRequested = false;\r
+                if (this.Paused)\r
+                    this.Paused = false;\r
                 else\r
                 {\r
-                    this.PauseRequested = false;\r
+                    this.Paused = false;\r
                     try\r
                     {\r
-                        Thread theQueue = new Thread(this.StartQueue) {IsBackground = true};\r
+                        Thread theQueue = new Thread(this.StartQueue) { IsBackground = true };\r
                         theQueue.Start();\r
+\r
+                        if (this.QueueStarted != null)\r
+                            this.QueueStarted(this, new EventArgs());\r
                     }\r
                     catch (Exception exc)\r
                     {\r
-                        MessageBox.Show(exc.ToString());\r
+                        Main.ShowExceptiowWindow("Unable to Start Queue", exc.ToString());\r
                     }\r
                 }\r
             }\r
@@ -332,7 +353,7 @@ namespace Handbrake.Services
         /// </summary>\r
         public void Pause()\r
         {\r
-            this.PauseRequested = true;\r
+            this.Paused = true;\r
 \r
             if (this.QueuePauseRequested != null)\r
                 this.QueuePauseRequested(this, new EventArgs());\r
@@ -366,10 +387,10 @@ namespace Handbrake.Services
 \r
                 // Growl\r
                 if (Properties.Settings.Default.growlEncode)\r
-                    GrowlCommunicator.Notify("Encode Completed", \r
+                    GrowlCommunicator.Notify("Encode Completed",\r
                                              "Put down that cocktail...\nyour Handbrake encode is done.");\r
 \r
-                while (this.PauseRequested) // Need to find a better way of doing this.\r
+                while (this.Paused) // Need to find a better way of doing this.\r
                 {\r
                     Thread.Sleep(2000);\r
                 }\r