OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / EncodeQueue / Queue.cs
1 /*  Queue.cs $\r
2         \r
3            This file is part of the HandBrake source code.\r
4            Homepage: <http://handbrake.fr/>.\r
5            It may be used under the terms of the GNU General Public License. */\r
6 \r
7 using System;\r
8 using System.Collections.Generic;\r
9 using System.Collections.ObjectModel;\r
10 using System.IO;\r
11 using System.Threading;\r
12 using System.Windows.Forms;\r
13 using System.Xml.Serialization;\r
14 using Handbrake.Functions;\r
15 \r
16 namespace Handbrake.EncodeQueue\r
17 {\r
18     public class Queue : Encode\r
19     {\r
20         private static XmlSerializer serializer;\r
21         private readonly List<Job> queue = new List<Job>();\r
22         private int NextJobId;\r
23 \r
24         /// <summary>\r
25         /// Fires when a pause to the encode queue has been requested.\r
26         /// </summary>\r
27         public event EventHandler QueuePauseRequested;\r
28 \r
29         /// <summary>\r
30         /// Fires when the entire encode queue has completed.\r
31         /// </summary>\r
32         public event EventHandler QueueCompleted;\r
33 \r
34         #region Queue\r
35         /// <summary>\r
36         /// Gets and removes the next job in the queue.\r
37         /// </summary>\r
38         /// <returns>The job that was removed from the queue.</returns>\r
39         private Job GetNextJob()\r
40         {\r
41             Job job = queue[0];\r
42             LastEncode = job;\r
43             Remove(0); // Remove the item which we are about to pass out.\r
44 \r
45             WriteQueueStateToFile("hb_queue_recovery.xml");\r
46 \r
47             return job;\r
48         }\r
49 \r
50         /// <summary>\r
51         /// Gets the current state of the encode queue.\r
52         /// </summary>\r
53         public ReadOnlyCollection<Job> CurrentQueue\r
54         {\r
55             get { return queue.AsReadOnly(); }\r
56         }\r
57 \r
58         /// <summary>\r
59         /// Gets the number of items in the queue.\r
60         /// </summary>\r
61         public int Count\r
62         {\r
63             get { return queue.Count; }\r
64         }\r
65 \r
66         /// <summary>\r
67         /// Adds an item to the queue.\r
68         /// </summary>\r
69         /// <param name="query">The query that will be passed to the HandBrake CLI.</param>\r
70         /// <param name="source">The location of the source video.</param>\r
71         /// <param name="destination">The location where the encoded video will be.</param>\r
72         /// <param name="customJob">Custom job</param>\r
73         /// <param name="scanInfo">The Scan</param>\r
74         public void Add(string query, string source, string destination, bool customJob)\r
75         {\r
76             Job newJob = new Job { Id = NextJobId++, Query = query, Source = source, Destination = destination, CustomQuery = customJob };\r
77 \r
78             queue.Add(newJob);\r
79             WriteQueueStateToFile("hb_queue_recovery.xml");\r
80         }\r
81 \r
82         /// <summary>\r
83         /// Removes an item from the queue.\r
84         /// </summary>\r
85         /// <param name="index">The zero-based location of the job in the queue.</param>\r
86         public void Remove(int index)\r
87         {\r
88             queue.RemoveAt(index);\r
89             WriteQueueStateToFile("hb_queue_recovery.xml");\r
90         }\r
91 \r
92         /// <summary>\r
93         /// Retrieve a job from the queue\r
94         /// </summary>\r
95         /// <param name="index"></param>\r
96         /// <returns></returns>\r
97         public Job GetJob(int index)\r
98         {\r
99             if (queue.Count >= (index +1))\r
100                 return queue[index];\r
101 \r
102             return new Job();\r
103         }\r
104 \r
105         /// <summary>\r
106         /// Moves an item up one position in the queue.\r
107         /// </summary>\r
108         /// <param name="index">The zero-based location of the job in the queue.</param>\r
109         public void MoveUp(int index)\r
110         {\r
111             if (index > 0)\r
112             {\r
113                 Job item = queue[index];\r
114 \r
115                 queue.RemoveAt(index);\r
116                 queue.Insert((index - 1), item);\r
117             }\r
118 \r
119             WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file\r
120         }\r
121 \r
122         /// <summary>\r
123         /// Moves an item down one position in the queue.\r
124         /// </summary>\r
125         /// <param name="index">The zero-based location of the job in the queue.</param>\r
126         public void MoveDown(int index)\r
127         {\r
128             if (index < queue.Count - 1)\r
129             {\r
130                 Job item = queue[index];\r
131 \r
132                 queue.RemoveAt(index);\r
133                 queue.Insert((index + 1), item);\r
134             }\r
135 \r
136             WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file\r
137         }\r
138 \r
139         /// <summary>\r
140         /// Writes the current state of the queue to a file.\r
141         /// </summary>\r
142         /// <param name="file">The location of the file to write the queue to.</param>\r
143         public void WriteQueueStateToFile(string file)\r
144         {\r
145             string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"HandBrake\hb_queue_recovery.xml");\r
146             string tempPath = file == "hb_queue_recovery.xml" ? appDataPath : file;\r
147 \r
148             try\r
149             {\r
150                 using (FileStream strm = new FileStream(tempPath, FileMode.Create, FileAccess.Write))\r
151                 {\r
152                     if (serializer == null)\r
153                         serializer = new XmlSerializer(typeof(List<Job>));\r
154                     serializer.Serialize(strm, queue);\r
155                     strm.Close();\r
156                     strm.Dispose();\r
157                 }\r
158             }\r
159             catch (Exception)\r
160             {\r
161                 // Any Errors will be out of diskspace/permissions problems. \r
162                 // Don't report them as they'll annoy the user.\r
163             }\r
164         }\r
165 \r
166         /// <summary>\r
167         /// Writes the current state of the queue in the form of a batch (.bat) file.\r
168         /// </summary>\r
169         /// <param name="file">The location of the file to write the batch file to.</param>\r
170         public void WriteBatchScriptToFile(string file)\r
171         {\r
172             string queries = "";\r
173             foreach (Job queue_item in queue)\r
174             {\r
175                 string q_item = queue_item.Query;\r
176                 string fullQuery = '"' + Application.StartupPath + "\\HandBrakeCLI.exe" + '"' + q_item;\r
177 \r
178                 if (queries == string.Empty)\r
179                     queries = queries + fullQuery;\r
180                 else\r
181                     queries = queries + " && " + fullQuery;\r
182             }\r
183             string strCmdLine = queries;\r
184 \r
185             if (file != "")\r
186             {\r
187                 try\r
188                 {\r
189                     // Create a StreamWriter and open the file, Write the batch file query to the file and \r
190                     // Close the stream\r
191                     using (StreamWriter line = new StreamWriter(file))\r
192                     {\r
193                         line.WriteLine(strCmdLine);\r
194                     }\r
195 \r
196                     MessageBox.Show("Your batch script has been sucessfully saved.", "Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);\r
197                 }\r
198                 catch (Exception)\r
199                 {\r
200                     MessageBox.Show("Unable to write to the file. Please make sure that the location has the correct permissions for file writing.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
201                 }\r
202 \r
203             }\r
204         }\r
205 \r
206         /// <summary>\r
207         /// Reads a serialized XML file that represents a queue of encoding jobs.\r
208         /// </summary>\r
209         /// <param name="file">The location of the file to read the queue from.</param>\r
210         public void LoadQueueFromFile(string file)\r
211         {\r
212             string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"HandBrake\hb_queue_recovery.xml");\r
213             string tempPath = file == "hb_queue_recovery.xml" ? appDataPath : file;\r
214 \r
215             if (File.Exists(tempPath))\r
216             {\r
217                 using (FileStream strm = new FileStream(tempPath, FileMode.Open, FileAccess.Read))\r
218                 {\r
219                     if (strm.Length != 0)\r
220                     {\r
221                         if (serializer == null)\r
222                             serializer = new XmlSerializer(typeof(List<Job>));\r
223 \r
224                         List<Job> list = serializer.Deserialize(strm) as List<Job>;\r
225 \r
226                         if (list != null)\r
227                             foreach (Job item in list)\r
228                                 queue.Add(item);\r
229 \r
230                         if (file != "hb_queue_recovery.xml")\r
231                             WriteQueueStateToFile("hb_queue_recovery.xml");\r
232                     }\r
233                 }\r
234             }\r
235         }\r
236 \r
237         /// <summary>\r
238         /// Checks the current queue for an existing instance of the specified destination.\r
239         /// </summary>\r
240         /// <param name="destination">The destination of the encode.</param>\r
241         /// <returns>Whether or not the supplied destination is already in the queue.</returns>\r
242         public bool CheckForDestinationDuplicate(string destination)\r
243         {\r
244             foreach (Job checkItem in queue)\r
245             {\r
246                 if (checkItem.Destination.Contains(destination.Replace("\\\\", "\\")))\r
247                     return true;\r
248             }\r
249 \r
250             return false;\r
251         }\r
252 \r
253         #endregion\r
254 \r
255         #region Encoding\r
256 \r
257         /// <summary>\r
258         /// Gets the last encode that was processed.\r
259         /// </summary>\r
260         /// <returns></returns> \r
261         public Job LastEncode { get; set; }\r
262 \r
263         /// <summary>\r
264         /// Request Pause\r
265         /// </summary>\r
266         public Boolean PauseRequested { get; private set; }\r
267 \r
268         /// <summary>\r
269         /// Starts encoding the first job in the queue and continues encoding until all jobs\r
270         /// have been encoded.\r
271         /// </summary>\r
272         public void Start()\r
273         {\r
274             if (this.Count != 0)\r
275             {\r
276                 if (PauseRequested)\r
277                     PauseRequested = false;\r
278                 else\r
279                 {\r
280                     PauseRequested = false;\r
281                     try\r
282                     {\r
283                         Thread theQueue = new Thread(StartQueue) { IsBackground = true };\r
284                         theQueue.Start();\r
285                     }\r
286                     catch (Exception exc)\r
287                     {\r
288                         MessageBox.Show(exc.ToString());\r
289                     }\r
290                 }\r
291             }\r
292         }\r
293 \r
294         /// <summary>\r
295         /// Requests a pause of the encode queue.\r
296         /// </summary>\r
297         public void Pause()\r
298         {\r
299             PauseRequested = true;\r
300 \r
301             if (QueuePauseRequested != null)\r
302                 QueuePauseRequested(this, new EventArgs());\r
303         }\r
304 \r
305         /// <summary>\r
306         /// Run through all the jobs on the queue.\r
307         /// </summary>\r
308         /// <param name="state"></param>\r
309         private void StartQueue(object state)\r
310         {\r
311             // Run through each item on the queue\r
312             while (this.Count != 0)\r
313             {\r
314                 Job encJob = GetNextJob();\r
315                 string query = encJob.Query;\r
316                 WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file\r
317 \r
318                 Run(query);\r
319 \r
320                 HbProcess.WaitForExit();\r
321 \r
322                 AddCLIQueryToLog(encJob);\r
323                 CopyLog(LastEncode.Destination);\r
324 \r
325                 HbProcess.Close();\r
326                 HbProcess.Dispose();\r
327 \r
328                 IsEncoding = false;\r
329 \r
330                 //Growl\r
331                 if (Properties.Settings.Default.growlEncode)\r
332                     GrowlCommunicator.Notify("Encode Completed", "Put down that cocktail...\nyour Handbrake encode is done.");\r
333 \r
334                 while (PauseRequested) // Need to find a better way of doing this.\r
335                 {\r
336                     Thread.Sleep(2000);\r
337                 }\r
338             }\r
339             LastEncode = new Job();\r
340 \r
341             if (QueueCompleted != null)\r
342                 QueueCompleted(this, new EventArgs());\r
343 \r
344             // After the encode is done, we may want to shutdown, suspend etc.\r
345             Finish();\r
346         }\r
347 \r
348         #endregion\r
349     }\r
350 }