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, int title, string source, string destination, bool customJob)
\r
99 Job newJob = new Job
\r
101 Id = this.nextJobId++,
\r
105 Destination = destination,
\r
106 CustomQuery = customJob
\r
109 this.queue.Add(newJob);
\r
110 this.WriteQueueStateToFile("hb_queue_recovery.xml");
\r
114 /// Removes an item from the queue.
\r
116 /// <param name="index">The zero-based location of the job in the queue.</param>
\r
117 public void Remove(int index)
\r
119 this.queue.RemoveAt(index);
\r
120 this.WriteQueueStateToFile("hb_queue_recovery.xml");
\r
124 /// Retrieve a job from the queue
\r
126 /// <param name="index">the job id</param>
\r
127 /// <returns>A job for the given index or blank job object</returns>
\r
128 public Job GetJob(int index)
\r
130 if (this.queue.Count >= (index + 1))
\r
131 return this.queue[index];
\r
137 /// Moves an item up one position in the queue.
\r
139 /// <param name="index">The zero-based location of the job in the queue.</param>
\r
140 public void MoveUp(int index)
\r
144 Job item = queue[index];
\r
146 queue.RemoveAt(index);
\r
147 queue.Insert((index - 1), item);
\r
150 WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
154 /// Moves an item down one position in the queue.
\r
156 /// <param name="index">The zero-based location of the job in the queue.</param>
\r
157 public void MoveDown(int index)
\r
159 if (index < this.queue.Count - 1)
\r
161 Job item = this.queue[index];
\r
163 this.queue.RemoveAt(index);
\r
164 this.queue.Insert((index + 1), item);
\r
167 this.WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
171 /// Writes the current state of the queue to a file.
\r
173 /// <param name="file">The location of the file to write the queue to.</param>
\r
174 public void WriteQueueStateToFile(string file)
\r
176 string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
\r
177 @"HandBrake\hb_queue_recovery.xml");
\r
178 string tempPath = file == "hb_queue_recovery.xml" ? appDataPath : file;
\r
182 using (FileStream strm = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
\r
184 if (serializer == null)
\r
185 serializer = new XmlSerializer(typeof (List<Job>));
\r
186 serializer.Serialize(strm, queue);
\r
198 /// Writes the current state of the queue in the form of a batch (.bat) file.
\r
200 /// <param name="file">The location of the file to write the batch file to.</param>
\r
201 public void WriteBatchScriptToFile(string file)
\r
203 string queries = string.Empty;
\r
204 foreach (Job queueItem in this.queue)
\r
206 string qItem = queueItem.Query;
\r
207 string fullQuery = '"' + Application.StartupPath + "\\HandBrakeCLI.exe" + '"' + qItem;
\r
209 if (queries == string.Empty)
\r
210 queries = queries + fullQuery;
\r
212 queries = queries + " && " + fullQuery;
\r
214 string strCmdLine = queries;
\r
216 if (file != string.Empty)
\r
220 // Create a StreamWriter and open the file, Write the batch file query to the file and
\r
221 // Close the stream
\r
222 using (StreamWriter line = new StreamWriter(file))
\r
224 line.WriteLine(strCmdLine);
\r
227 MessageBox.Show("Your batch script has been sucessfully saved.", "Status", MessageBoxButtons.OK,
\r
228 MessageBoxIcon.Asterisk);
\r
233 "Unable to write to the file. Please make sure that the location has the correct permissions for file writing.",
\r
234 "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
\r
240 /// Reads a serialized XML file that represents a queue of encoding jobs.
\r
242 /// <param name="file">The location of the file to read the queue from.</param>
\r
243 public void LoadQueueFromFile(string file)
\r
245 string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
\r
246 @"HandBrake\hb_queue_recovery.xml");
\r
247 string tempPath = file == "hb_queue_recovery.xml" ? appDataPath : file;
\r
249 if (File.Exists(tempPath))
\r
251 using (FileStream strm = new FileStream(tempPath, FileMode.Open, FileAccess.Read))
\r
253 if (strm.Length != 0)
\r
255 if (serializer == null)
\r
256 serializer = new XmlSerializer(typeof (List<Job>));
\r
258 List<Job> list = serializer.Deserialize(strm) as List<Job>;
\r
261 foreach (Job item in list)
\r
262 this.queue.Add(item);
\r
264 if (file != "hb_queue_recovery.xml")
\r
265 this.WriteQueueStateToFile("hb_queue_recovery.xml");
\r
272 /// Checks the current queue for an existing instance of the specified destination.
\r
274 /// <param name="destination">The destination of the encode.</param>
\r
275 /// <returns>Whether or not the supplied destination is already in the queue.</returns>
\r
276 public bool CheckForDestinationDuplicate(string destination)
\r
278 foreach (Job checkItem in this.queue)
\r
280 if (checkItem.Destination.Contains(destination.Replace("\\\\", "\\")))
\r
292 /// Gets or sets the last encode that was processed.
\r
294 /// <returns></returns>
\r
295 public Job LastEncode { get; set; }
\r
298 /// Gets a value indicating whether Request Pause
\r
300 public bool PauseRequested { get; private set; }
\r
303 /// Starts encoding the first job in the queue and continues encoding until all jobs
\r
304 /// have been encoded.
\r
306 public void Start()
\r
308 if (this.Count != 0)
\r
310 if (this.PauseRequested)
\r
311 this.PauseRequested = false;
\r
314 this.PauseRequested = false;
\r
317 Thread theQueue = new Thread(this.StartQueue) {IsBackground = true};
\r
320 catch (Exception exc)
\r
322 MessageBox.Show(exc.ToString());
\r
329 /// Requests a pause of the encode queue.
\r
331 public void Pause()
\r
333 this.PauseRequested = true;
\r
335 if (this.QueuePauseRequested != null)
\r
336 this.QueuePauseRequested(this, new EventArgs());
\r
340 /// Run through all the jobs on the queue.
\r
342 /// <param name="state">Object State</param>
\r
343 private void StartQueue(object state)
\r
345 // Run through each item on the queue
\r
346 while (this.Count != 0)
\r
348 Job encJob = this.GetNextJob();
\r
349 this.WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
353 if (HbProcess == null)
\r
357 HbProcess.WaitForExit();
\r
359 AddCLIQueryToLog(encJob);
\r
360 this.CopyLog(this.LastEncode.Destination);
\r
363 HbProcess.Dispose();
\r
365 IsEncoding = false;
\r
368 if (Properties.Settings.Default.growlEncode)
\r
369 GrowlCommunicator.Notify("Encode Completed",
\r
370 "Put down that cocktail...\nyour Handbrake encode is done.");
\r
372 while (this.PauseRequested) // Need to find a better way of doing this.
\r
374 Thread.Sleep(2000);
\r
377 this.LastEncode = new Job();
\r
379 if (this.QueueCompleted != null)
\r
380 this.QueueCompleted(this, new EventArgs());
\r
382 // After the encode is done, we may want to shutdown, suspend etc.
\r