2 This file is part of the HandBrake source code.
\r
3 Homepage: <http://handbrake.fr/>.
\r
4 It may be used under the terms of the GNU General Public License. */
\r
6 namespace Handbrake.Services
\r
9 using System.Collections.Generic;
\r
10 using System.Collections.ObjectModel;
\r
13 using System.Threading;
\r
14 using System.Windows.Forms;
\r
15 using System.Xml.Serialization;
\r
20 /// The HandBrake Queue
\r
22 public class Queue : Encode
\r
25 /// The Queue Job List
\r
27 private readonly List<Job> queue = new List<Job>();
\r
30 /// An XML Serializer
\r
32 private static XmlSerializer serializer;
\r
37 private int nextJobId;
\r
40 /// Fires when the Queue has started
\r
42 public event EventHandler QueueStarted;
\r
45 /// Fires when a job is Added, Removed or Re-Ordered.
\r
46 /// Should be used for triggering an update of the Queue Window.
\r
48 public event EventHandler QueueListChanged;
\r
51 /// Fires when a pause to the encode queue has been requested.
\r
53 public event EventHandler QueuePauseRequested;
\r
56 /// Fires when the entire encode queue has completed.
\r
58 public event EventHandler QueueCompleted;
\r
62 /// Gets or sets the last encode that was processed.
\r
64 /// <returns></returns>
\r
65 public Job LastEncode { get; set; }
\r
68 /// Gets a value indicating whether Request Pause
\r
70 public bool Paused { get; private set; }
\r
73 /// Gets the current state of the encode queue.
\r
75 public ReadOnlyCollection<Job> CurrentQueue
\r
77 get { return this.queue.AsReadOnly(); }
\r
81 /// Gets the number of items in the queue.
\r
85 get { return this.queue.Count; }
\r
92 /// Gets and removes the next job in the queue.
\r
94 /// <returns>The job that was removed from the queue.</returns>
\r
95 private Job GetNextJob()
\r
97 Job job = this.queue[0];
\r
98 this.LastEncode = job;
\r
99 this.Remove(0); // Remove the item which we are about to pass out.
\r
101 this.WriteQueueStateToFile("hb_queue_recovery.xml");
\r
107 /// Adds an item to the queue.
\r
109 /// <param name="query">
\r
110 /// The query that will be passed to the HandBrake CLI.
\r
112 /// <param name="title">
\r
115 /// <param name="source">
\r
116 /// The location of the source video.
\r
118 /// <param name="destination">
\r
119 /// The location where the encoded video will be.
\r
121 /// <param name="customJob">
\r
124 public void Add(string query, int title, string source, string destination, bool customJob)
\r
126 Job newJob = new Job
\r
128 Id = this.nextJobId++,
\r
132 Destination = destination,
\r
133 CustomQuery = customJob
\r
136 this.queue.Add(newJob);
\r
137 this.WriteQueueStateToFile("hb_queue_recovery.xml");
\r
139 if (this.QueueListChanged != null)
\r
140 this.QueueListChanged(this, new EventArgs());
\r
144 /// Removes an item from the queue.
\r
146 /// <param name="index">The zero-based location of the job in the queue.</param>
\r
147 public void Remove(int index)
\r
149 this.queue.RemoveAt(index);
\r
150 this.WriteQueueStateToFile("hb_queue_recovery.xml");
\r
152 if (this.QueueListChanged != null)
\r
153 this.QueueListChanged(this, new EventArgs());
\r
157 /// Retrieve a job from the queue
\r
159 /// <param name="index">the job id</param>
\r
160 /// <returns>A job for the given index or blank job object</returns>
\r
161 public Job GetJob(int index)
\r
163 if (this.queue.Count >= (index + 1))
\r
164 return this.queue[index];
\r
170 /// Moves an item up one position in the queue.
\r
172 /// <param name="index">The zero-based location of the job in the queue.</param>
\r
173 public void MoveUp(int index)
\r
177 Job item = queue[index];
\r
179 queue.RemoveAt(index);
\r
180 queue.Insert((index - 1), item);
\r
183 WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
185 if (this.QueueListChanged != null)
\r
186 this.QueueListChanged(this, new EventArgs());
\r
190 /// Moves an item down one position in the queue.
\r
192 /// <param name="index">The zero-based location of the job in the queue.</param>
\r
193 public void MoveDown(int index)
\r
195 if (index < this.queue.Count - 1)
\r
197 Job item = this.queue[index];
\r
199 this.queue.RemoveAt(index);
\r
200 this.queue.Insert((index + 1), item);
\r
203 this.WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
205 if (this.QueueListChanged != null)
\r
206 this.QueueListChanged(this, new EventArgs());
\r
210 /// Writes the current state of the queue to a file.
\r
212 /// <param name="file">The location of the file to write the queue to.</param>
\r
213 public void WriteQueueStateToFile(string file)
\r
215 string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
\r
216 @"HandBrake\hb_queue_recovery.xml");
\r
217 string tempPath = file == "hb_queue_recovery.xml" ? appDataPath : file;
\r
221 using (FileStream strm = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
\r
223 if (serializer == null)
\r
224 serializer = new XmlSerializer(typeof(List<Job>));
\r
225 serializer.Serialize(strm, queue);
\r
237 /// Writes the current state of the queue in the form of a batch (.bat) file.
\r
239 /// <param name="file">The location of the file to write the batch file to.</param>
\r
240 public void WriteBatchScriptToFile(string file)
\r
242 string queries = string.Empty;
\r
243 foreach (Job queueItem in this.queue)
\r
245 string qItem = queueItem.Query;
\r
246 string fullQuery = '"' + Application.StartupPath + "\\HandBrakeCLI.exe" + '"' + qItem;
\r
248 if (queries == string.Empty)
\r
249 queries = queries + fullQuery;
\r
251 queries = queries + " && " + fullQuery;
\r
253 string strCmdLine = queries;
\r
255 if (file != string.Empty)
\r
259 // Create a StreamWriter and open the file, Write the batch file query to the file and
\r
260 // Close the stream
\r
261 using (StreamWriter line = new StreamWriter(file))
\r
263 line.WriteLine(strCmdLine);
\r
266 MessageBox.Show("Your batch script has been sucessfully saved.", "Status", MessageBoxButtons.OK,
\r
267 MessageBoxIcon.Asterisk);
\r
269 catch (Exception exc)
\r
271 Main.ShowExceptiowWindow("Unable to write to the file. Please make sure that the location has the correct permissions for file writing.", exc.ToString());
\r
277 /// Reads a serialized XML file that represents a queue of encoding jobs.
\r
279 /// <param name="file">The location of the file to read the queue from.</param>
\r
280 public void LoadQueueFromFile(string file)
\r
282 string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
\r
283 @"HandBrake\hb_queue_recovery.xml");
\r
284 string tempPath = file == "hb_queue_recovery.xml" ? appDataPath : file;
\r
286 if (File.Exists(tempPath))
\r
288 using (FileStream strm = new FileStream(tempPath, FileMode.Open, FileAccess.Read))
\r
290 if (strm.Length != 0)
\r
292 if (serializer == null)
\r
293 serializer = new XmlSerializer(typeof(List<Job>));
\r
295 List<Job> list = serializer.Deserialize(strm) as List<Job>;
\r
298 foreach (Job item in list)
\r
299 this.queue.Add(item);
\r
301 if (file != "hb_queue_recovery.xml")
\r
302 this.WriteQueueStateToFile("hb_queue_recovery.xml");
\r
309 /// Checks the current queue for an existing instance of the specified destination.
\r
311 /// <param name="destination">The destination of the encode.</param>
\r
312 /// <returns>Whether or not the supplied destination is already in the queue.</returns>
\r
313 public bool CheckForDestinationDuplicate(string destination)
\r
315 return this.queue.Any(checkItem => checkItem.Destination.Contains(destination.Replace("\\\\", "\\")));
\r
323 /// Starts encoding the first job in the queue and continues encoding until all jobs
\r
324 /// have been encoded.
\r
326 public void Start()
\r
328 if (this.Count != 0)
\r
331 this.Paused = false;
\r
334 this.Paused = false;
\r
337 Thread theQueue = new Thread(this.StartQueue) { IsBackground = true };
\r
340 if (this.QueueStarted != null)
\r
341 this.QueueStarted(this, new EventArgs());
\r
343 catch (Exception exc)
\r
345 Main.ShowExceptiowWindow("Unable to Start Queue", exc.ToString());
\r
352 /// Requests a pause of the encode queue.
\r
354 public void Pause()
\r
356 this.Paused = true;
\r
358 if (this.QueuePauseRequested != null)
\r
359 this.QueuePauseRequested(this, new EventArgs());
\r
363 /// Run through all the jobs on the queue.
\r
365 /// <param name="state">Object State</param>
\r
366 private void StartQueue(object state)
\r
368 // Run through each item on the queue
\r
369 while (this.Count != 0)
\r
371 Job encJob = this.GetNextJob();
\r
372 this.WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
374 Run(encJob, false);
\r
376 if (HbProcess == null)
\r
380 HbProcess.WaitForExit();
\r
382 AddCLIQueryToLog(encJob);
\r
383 this.CopyLog(this.LastEncode.Destination);
\r
386 HbProcess.Dispose();
\r
389 if (Properties.Settings.Default.growlEncode)
\r
390 GrowlCommunicator.Notify("Encode Completed",
\r
391 "Put down that cocktail...\nyour Handbrake encode is done.");
\r
393 while (this.Paused) // Need to find a better way of doing this.
\r
395 Thread.Sleep(2000);
\r
398 this.LastEncode = new Job();
\r
400 if (this.QueueCompleted != null)
\r
401 this.QueueCompleted(this, new EventArgs());
\r
403 // After the encode is done, we may want to shutdown, suspend etc.
\r