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.EncodeQueue
\r
9 using System.Collections.Generic;
\r
10 using System.Collections.ObjectModel;
\r
12 using System.Threading;
\r
13 using System.Windows.Forms;
\r
14 using System.Xml.Serialization;
\r
18 /// The HandBrake Queue
\r
20 public class Queue : Encode
\r
23 /// An XML Serializer
\r
25 private static XmlSerializer serializer;
\r
28 /// The Queue Job List
\r
30 private readonly List<Job> queue = new List<Job>();
\r
35 private int nextJobId;
\r
38 /// Fires when a pause to the encode queue has been requested.
\r
40 public event EventHandler QueuePauseRequested;
\r
43 /// Fires when the entire encode queue has completed.
\r
45 public event EventHandler QueueCompleted;
\r
49 /// Gets and removes the next job in the queue.
\r
51 /// <returns>The job that was removed from the queue.</returns>
\r
52 private Job GetNextJob()
\r
54 Job job = this.queue[0];
\r
55 this.LastEncode = job;
\r
56 this.Remove(0); // Remove the item which we are about to pass out.
\r
58 this.WriteQueueStateToFile("hb_queue_recovery.xml");
\r
64 /// Gets the current state of the encode queue.
\r
66 public ReadOnlyCollection<Job> CurrentQueue
\r
68 get { return this.queue.AsReadOnly(); }
\r
72 /// Gets the number of items in the queue.
\r
76 get { return this.queue.Count; }
\r
80 /// Adds an item to the queue.
\r
82 /// <param name="query">
\r
83 /// The query that will be passed to the HandBrake CLI.
\r
85 /// <param name="source">
\r
86 /// The location of the source video.
\r
88 /// <param name="destination">
\r
89 /// The location where the encoded video will be.
\r
91 /// <param name="customJob">
\r
94 public void Add(string query, string source, string destination, bool customJob)
\r
96 Job newJob = new Job { Id = this.nextJobId++, Query = query, Source = source, Destination = destination, CustomQuery = customJob };
\r
98 this.queue.Add(newJob);
\r
99 this.WriteQueueStateToFile("hb_queue_recovery.xml");
\r
103 /// Removes an item from the queue.
\r
105 /// <param name="index">The zero-based location of the job in the queue.</param>
\r
106 public void Remove(int index)
\r
108 this.queue.RemoveAt(index);
\r
109 this.WriteQueueStateToFile("hb_queue_recovery.xml");
\r
113 /// Retrieve a job from the queue
\r
115 /// <param name="index">the job id</param>
\r
116 /// <returns>A job for the given index or blank job object</returns>
\r
117 public Job GetJob(int index)
\r
119 if (this.queue.Count >= (index + 1))
\r
120 return this.queue[index];
\r
126 /// Moves an item up one position in the queue.
\r
128 /// <param name="index">The zero-based location of the job in the queue.</param>
\r
129 public void MoveUp(int index)
\r
133 Job item = queue[index];
\r
135 queue.RemoveAt(index);
\r
136 queue.Insert((index - 1), item);
\r
139 WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
143 /// Moves an item down one position in the queue.
\r
145 /// <param name="index">The zero-based location of the job in the queue.</param>
\r
146 public void MoveDown(int index)
\r
148 if (index < this.queue.Count - 1)
\r
150 Job item = this.queue[index];
\r
152 this.queue.RemoveAt(index);
\r
153 this.queue.Insert((index + 1), item);
\r
156 this.WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
160 /// Writes the current state of the queue to a file.
\r
162 /// <param name="file">The location of the file to write the queue to.</param>
\r
163 public void WriteQueueStateToFile(string file)
\r
165 string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"HandBrake\hb_queue_recovery.xml");
\r
166 string tempPath = file == "hb_queue_recovery.xml" ? appDataPath : file;
\r
170 using (FileStream strm = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
\r
172 if (serializer == null)
\r
173 serializer = new XmlSerializer(typeof(List<Job>));
\r
174 serializer.Serialize(strm, queue);
\r
186 /// Writes the current state of the queue in the form of a batch (.bat) file.
\r
188 /// <param name="file">The location of the file to write the batch file to.</param>
\r
189 public void WriteBatchScriptToFile(string file)
\r
191 string queries = string.Empty;
\r
192 foreach (Job queueItem in this.queue)
\r
194 string qItem = queueItem.Query;
\r
195 string fullQuery = '"' + Application.StartupPath + "\\HandBrakeCLI.exe" + '"' + qItem;
\r
197 if (queries == string.Empty)
\r
198 queries = queries + fullQuery;
\r
200 queries = queries + " && " + fullQuery;
\r
202 string strCmdLine = queries;
\r
204 if (file != string.Empty)
\r
208 // Create a StreamWriter and open the file, Write the batch file query to the file and
\r
209 // Close the stream
\r
210 using (StreamWriter line = new StreamWriter(file))
\r
212 line.WriteLine(strCmdLine);
\r
215 MessageBox.Show("Your batch script has been sucessfully saved.", "Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
\r
219 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
226 /// Reads a serialized XML file that represents a queue of encoding jobs.
\r
228 /// <param name="file">The location of the file to read the queue from.</param>
\r
229 public void LoadQueueFromFile(string file)
\r
231 string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"HandBrake\hb_queue_recovery.xml");
\r
232 string tempPath = file == "hb_queue_recovery.xml" ? appDataPath : file;
\r
234 if (File.Exists(tempPath))
\r
236 using (FileStream strm = new FileStream(tempPath, FileMode.Open, FileAccess.Read))
\r
238 if (strm.Length != 0)
\r
240 if (serializer == null)
\r
241 serializer = new XmlSerializer(typeof(List<Job>));
\r
243 List<Job> list = serializer.Deserialize(strm) as List<Job>;
\r
246 foreach (Job item in list)
\r
247 this.queue.Add(item);
\r
249 if (file != "hb_queue_recovery.xml")
\r
250 this.WriteQueueStateToFile("hb_queue_recovery.xml");
\r
257 /// Checks the current queue for an existing instance of the specified destination.
\r
259 /// <param name="destination">The destination of the encode.</param>
\r
260 /// <returns>Whether or not the supplied destination is already in the queue.</returns>
\r
261 public bool CheckForDestinationDuplicate(string destination)
\r
263 foreach (Job checkItem in this.queue)
\r
265 if (checkItem.Destination.Contains(destination.Replace("\\\\", "\\")))
\r
277 /// Gets or sets the last encode that was processed.
\r
279 /// <returns></returns>
\r
280 public Job LastEncode { get; set; }
\r
283 /// Gets a value indicating whether Request Pause
\r
285 public bool PauseRequested { get; private set; }
\r
288 /// Starts encoding the first job in the queue and continues encoding until all jobs
\r
289 /// have been encoded.
\r
291 public void Start()
\r
293 if (this.Count != 0)
\r
295 if (this.PauseRequested)
\r
296 this.PauseRequested = false;
\r
299 this.PauseRequested = false;
\r
302 Thread theQueue = new Thread(this.StartQueue) {IsBackground = true};
\r
305 catch (Exception exc)
\r
307 MessageBox.Show(exc.ToString());
\r
314 /// Requests a pause of the encode queue.
\r
316 public void Pause()
\r
318 this.PauseRequested = true;
\r
320 if (this.QueuePauseRequested != null)
\r
321 this.QueuePauseRequested(this, new EventArgs());
\r
325 /// Run through all the jobs on the queue.
\r
327 /// <param name="state">Object State</param>
\r
328 private void StartQueue(object state)
\r
330 // Run through each item on the queue
\r
331 while (this.Count != 0)
\r
333 Job encJob = this.GetNextJob();
\r
334 string query = encJob.Query;
\r
335 this.WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
339 HbProcess.WaitForExit();
\r
341 AddCLIQueryToLog(encJob);
\r
342 this.CopyLog(this.LastEncode.Destination);
\r
345 HbProcess.Dispose();
\r
347 IsEncoding = false;
\r
350 if (Properties.Settings.Default.growlEncode)
\r
351 GrowlCommunicator.Notify("Encode Completed", "Put down that cocktail...\nyour Handbrake encode is done.");
\r
353 while (this.PauseRequested) // Need to find a better way of doing this.
\r
355 Thread.Sleep(2000);
\r
358 this.LastEncode = new Job();
\r
360 if (this.QueueCompleted != null)
\r
361 this.QueueCompleted(this, new EventArgs());
\r
363 // After the encode is done, we may want to shutdown, suspend etc.
\r