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
7 namespace Handbrake.Services
\r
10 using System.Collections.Generic;
\r
11 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 /// An XML Serializer
\r
27 private static XmlSerializer serializer;
\r
30 /// The Queue Job List
\r
32 private readonly List<Job> queue = new List<Job>();
\r
37 private int nextJobId;
\r
40 /// Fires when a pause to the encode queue has been requested.
\r
42 public event EventHandler QueuePauseRequested;
\r
45 /// Fires when the entire encode queue has completed.
\r
47 public event EventHandler QueueCompleted;
\r
52 /// Gets and removes the next job in the queue.
\r
54 /// <returns>The job that was removed from the queue.</returns>
\r
55 private Job GetNextJob()
\r
57 Job job = this.queue[0];
\r
58 this.LastEncode = job;
\r
59 this.Remove(0); // Remove the item which we are about to pass out.
\r
61 this.WriteQueueStateToFile("hb_queue_recovery.xml");
\r
67 /// Gets the current state of the encode queue.
\r
69 public ReadOnlyCollection<Job> CurrentQueue
\r
71 get { return this.queue.AsReadOnly(); }
\r
75 /// Gets the number of items in the queue.
\r
79 get { return this.queue.Count; }
\r
83 /// Adds an item to the queue.
\r
85 /// <param name="query">
\r
86 /// The query that will be passed to the HandBrake CLI.
\r
88 /// <param name="source">
\r
89 /// The location of the source video.
\r
91 /// <param name="destination">
\r
92 /// The location where the encoded video will be.
\r
94 /// <param name="customJob">
\r
97 public void Add(string query, string source, string destination, bool customJob)
\r
99 Job newJob = new Job
\r
101 Id = this.nextJobId++,
\r
104 Destination = destination,
\r
105 CustomQuery = customJob
\r
108 this.queue.Add(newJob);
\r
109 this.WriteQueueStateToFile("hb_queue_recovery.xml");
\r
113 /// Removes an item from the queue.
\r
115 /// <param name="index">The zero-based location of the job in the queue.</param>
\r
116 public void Remove(int index)
\r
118 this.queue.RemoveAt(index);
\r
119 this.WriteQueueStateToFile("hb_queue_recovery.xml");
\r
123 /// Retrieve a job from the queue
\r
125 /// <param name="index">the job id</param>
\r
126 /// <returns>A job for the given index or blank job object</returns>
\r
127 public Job GetJob(int index)
\r
129 if (this.queue.Count >= (index + 1))
\r
130 return this.queue[index];
\r
136 /// Moves an item up one position in the queue.
\r
138 /// <param name="index">The zero-based location of the job in the queue.</param>
\r
139 public void MoveUp(int index)
\r
143 Job item = queue[index];
\r
145 queue.RemoveAt(index);
\r
146 queue.Insert((index - 1), item);
\r
149 WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
153 /// Moves an item down one position in the queue.
\r
155 /// <param name="index">The zero-based location of the job in the queue.</param>
\r
156 public void MoveDown(int index)
\r
158 if (index < this.queue.Count - 1)
\r
160 Job item = this.queue[index];
\r
162 this.queue.RemoveAt(index);
\r
163 this.queue.Insert((index + 1), item);
\r
166 this.WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
170 /// Writes the current state of the queue to a file.
\r
172 /// <param name="file">The location of the file to write the queue to.</param>
\r
173 public void WriteQueueStateToFile(string file)
\r
175 string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
\r
176 @"HandBrake\hb_queue_recovery.xml");
\r
177 string tempPath = file == "hb_queue_recovery.xml" ? appDataPath : file;
\r
181 using (FileStream strm = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
\r
183 if (serializer == null)
\r
184 serializer = new XmlSerializer(typeof (List<Job>));
\r
185 serializer.Serialize(strm, queue);
\r
197 /// Writes the current state of the queue in the form of a batch (.bat) file.
\r
199 /// <param name="file">The location of the file to write the batch file to.</param>
\r
200 public void WriteBatchScriptToFile(string file)
\r
202 string queries = string.Empty;
\r
203 foreach (Job queueItem in this.queue)
\r
205 string qItem = queueItem.Query;
\r
206 string fullQuery = '"' + Application.StartupPath + "\\HandBrakeCLI.exe" + '"' + qItem;
\r
208 if (queries == string.Empty)
\r
209 queries = queries + fullQuery;
\r
211 queries = queries + " && " + fullQuery;
\r
213 string strCmdLine = queries;
\r
215 if (file != string.Empty)
\r
219 // Create a StreamWriter and open the file, Write the batch file query to the file and
\r
220 // Close the stream
\r
221 using (StreamWriter line = new StreamWriter(file))
\r
223 line.WriteLine(strCmdLine);
\r
226 MessageBox.Show("Your batch script has been sucessfully saved.", "Status", MessageBoxButtons.OK,
\r
227 MessageBoxIcon.Asterisk);
\r
232 "Unable to write to the file. Please make sure that the location has the correct permissions for file writing.",
\r
233 "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
\r
239 /// Reads a serialized XML file that represents a queue of encoding jobs.
\r
241 /// <param name="file">The location of the file to read the queue from.</param>
\r
242 public void LoadQueueFromFile(string file)
\r
244 string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
\r
245 @"HandBrake\hb_queue_recovery.xml");
\r
246 string tempPath = file == "hb_queue_recovery.xml" ? appDataPath : file;
\r
248 if (File.Exists(tempPath))
\r
250 using (FileStream strm = new FileStream(tempPath, FileMode.Open, FileAccess.Read))
\r
252 if (strm.Length != 0)
\r
254 if (serializer == null)
\r
255 serializer = new XmlSerializer(typeof (List<Job>));
\r
257 List<Job> list = serializer.Deserialize(strm) as List<Job>;
\r
260 foreach (Job item in list)
\r
261 this.queue.Add(item);
\r
263 if (file != "hb_queue_recovery.xml")
\r
264 this.WriteQueueStateToFile("hb_queue_recovery.xml");
\r
271 /// Checks the current queue for an existing instance of the specified destination.
\r
273 /// <param name="destination">The destination of the encode.</param>
\r
274 /// <returns>Whether or not the supplied destination is already in the queue.</returns>
\r
275 public bool CheckForDestinationDuplicate(string destination)
\r
277 foreach (Job checkItem in this.queue)
\r
279 if (checkItem.Destination.Contains(destination.Replace("\\\\", "\\")))
\r
291 /// Gets or sets the last encode that was processed.
\r
293 /// <returns></returns>
\r
294 public Job LastEncode { get; set; }
\r
297 /// Gets a value indicating whether Request Pause
\r
299 public bool PauseRequested { get; private set; }
\r
302 /// Starts encoding the first job in the queue and continues encoding until all jobs
\r
303 /// have been encoded.
\r
305 public void Start()
\r
307 if (this.Count != 0)
\r
309 if (this.PauseRequested)
\r
310 this.PauseRequested = false;
\r
313 this.PauseRequested = false;
\r
316 Thread theQueue = new Thread(this.StartQueue) {IsBackground = true};
\r
319 catch (Exception exc)
\r
321 MessageBox.Show(exc.ToString());
\r
328 /// Requests a pause of the encode queue.
\r
330 public void Pause()
\r
332 this.PauseRequested = true;
\r
334 if (this.QueuePauseRequested != null)
\r
335 this.QueuePauseRequested(this, new EventArgs());
\r
339 /// Run through all the jobs on the queue.
\r
341 /// <param name="state">Object State</param>
\r
342 private void StartQueue(object state)
\r
344 // Run through each item on the queue
\r
345 while (this.Count != 0)
\r
347 Job encJob = this.GetNextJob();
\r
348 this.WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
352 if (HbProcess == null)
\r
356 HbProcess.WaitForExit();
\r
358 AddCLIQueryToLog(encJob);
\r
359 this.CopyLog(this.LastEncode.Destination);
\r
362 HbProcess.Dispose();
\r
364 IsEncoding = false;
\r
367 if (Properties.Settings.Default.growlEncode)
\r
368 GrowlCommunicator.Notify("Encode Completed",
\r
369 "Put down that cocktail...\nyour Handbrake encode is done.");
\r
371 while (this.PauseRequested) // Need to find a better way of doing this.
\r
373 Thread.Sleep(2000);
\r
376 this.LastEncode = new Job();
\r
378 if (this.QueueCompleted != null)
\r
379 this.QueueCompleted(this, new EventArgs());
\r
381 // After the encode is done, we may want to shutdown, suspend etc.
\r