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.ApplicationServices.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
17 using HandBrake.ApplicationServices.Functions;
\r
18 using HandBrake.ApplicationServices.Model;
\r
21 /// The HandBrake Queue
\r
23 public class Queue : Encode
\r
26 /// The Queue Job List
\r
28 private readonly List<Job> queue = new List<Job>();
\r
31 /// An XML Serializer
\r
33 private static XmlSerializer serializer;
\r
38 private int nextJobId;
\r
41 /// Fires when the Queue has started
\r
43 public event EventHandler QueueStarted;
\r
46 /// Fires when a job is Added, Removed or Re-Ordered.
\r
47 /// Should be used for triggering an update of the Queue Window.
\r
49 public event EventHandler QueueListChanged;
\r
52 /// Fires when a pause to the encode queue has been requested.
\r
54 public event EventHandler QueuePauseRequested;
\r
57 /// Fires when the entire encode queue has completed.
\r
59 public event EventHandler QueueCompleted;
\r
63 /// Gets or sets the last encode that was processed.
\r
65 /// <returns></returns>
\r
66 public Job LastEncode { get; set; }
\r
69 /// Gets a value indicating whether Request Pause
\r
71 public bool Paused { get; private set; }
\r
74 /// Gets the current state of the encode queue.
\r
76 public ReadOnlyCollection<Job> CurrentQueue
\r
78 get { return this.queue.AsReadOnly(); }
\r
82 /// Gets the number of items in the queue.
\r
86 get { return this.queue.Count; }
\r
93 /// Gets and removes the next job in the queue.
\r
95 /// <returns>The job that was removed from the queue.</returns>
\r
96 private Job GetNextJob()
\r
98 Job job = this.queue[0];
\r
99 this.LastEncode = job;
\r
100 this.Remove(0); // Remove the item which we are about to pass out.
\r
102 this.WriteQueueStateToFile("hb_queue_recovery.xml");
\r
108 /// Adds an item to the queue.
\r
110 /// <param name="query">
\r
111 /// The query that will be passed to the HandBrake CLI.
\r
113 /// <param name="title">
\r
116 /// <param name="source">
\r
117 /// The location of the source video.
\r
119 /// <param name="destination">
\r
120 /// The location where the encoded video will be.
\r
122 /// <param name="customJob">
\r
125 public void Add(string query, int title, string source, string destination, bool customJob)
\r
127 Job newJob = new Job
\r
129 Id = this.nextJobId++,
\r
133 Destination = destination,
\r
134 CustomQuery = customJob
\r
137 this.queue.Add(newJob);
\r
138 this.WriteQueueStateToFile("hb_queue_recovery.xml");
\r
140 if (this.QueueListChanged != null)
\r
141 this.QueueListChanged(this, new EventArgs());
\r
145 /// Removes an item from the queue.
\r
147 /// <param name="index">The zero-based location of the job in the queue.</param>
\r
148 public void Remove(int index)
\r
150 this.queue.RemoveAt(index);
\r
151 this.WriteQueueStateToFile("hb_queue_recovery.xml");
\r
153 if (this.QueueListChanged != null)
\r
154 this.QueueListChanged(this, new EventArgs());
\r
158 /// Retrieve a job from the queue
\r
160 /// <param name="index">the job id</param>
\r
161 /// <returns>A job for the given index or blank job object</returns>
\r
162 public Job GetJob(int index)
\r
164 if (this.queue.Count >= (index + 1))
\r
165 return this.queue[index];
\r
171 /// Moves an item up one position in the queue.
\r
173 /// <param name="index">The zero-based location of the job in the queue.</param>
\r
174 public void MoveUp(int index)
\r
178 Job item = queue[index];
\r
180 queue.RemoveAt(index);
\r
181 queue.Insert((index - 1), item);
\r
184 WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
186 if (this.QueueListChanged != null)
\r
187 this.QueueListChanged(this, new EventArgs());
\r
191 /// Moves an item down one position in the queue.
\r
193 /// <param name="index">The zero-based location of the job in the queue.</param>
\r
194 public void MoveDown(int index)
\r
196 if (index < this.queue.Count - 1)
\r
198 Job item = this.queue[index];
\r
200 this.queue.RemoveAt(index);
\r
201 this.queue.Insert((index + 1), item);
\r
204 this.WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
206 if (this.QueueListChanged != null)
\r
207 this.QueueListChanged(this, new EventArgs());
\r
211 /// Writes the current state of the queue to a file.
\r
213 /// <param name="file">The location of the file to write the queue to.</param>
\r
214 public void WriteQueueStateToFile(string file)
\r
216 string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
\r
217 @"HandBrake\hb_queue_recovery.xml");
\r
218 string tempPath = file == "hb_queue_recovery.xml" ? appDataPath : file;
\r
222 using (FileStream strm = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
\r
224 if (serializer == null)
\r
225 serializer = new XmlSerializer(typeof(List<Job>));
\r
226 serializer.Serialize(strm, queue);
\r
238 /// Writes the current state of the queue in the form of a batch (.bat) file.
\r
240 /// <param name="file">The location of the file to write the batch file to.</param>
\r
241 public bool WriteBatchScriptToFile(string file)
\r
243 string queries = string.Empty;
\r
244 foreach (Job queueItem in this.queue)
\r
246 string qItem = queueItem.Query;
\r
247 string fullQuery = '"' + Application.StartupPath + "\\HandBrakeCLI.exe" + '"' + qItem;
\r
249 if (queries == string.Empty)
\r
250 queries = queries + fullQuery;
\r
252 queries = queries + " && " + fullQuery;
\r
254 string strCmdLine = queries;
\r
256 if (file != string.Empty)
\r
260 // Create a StreamWriter and open the file, Write the batch file query to the file and
\r
261 // Close the stream
\r
262 using (StreamWriter line = new StreamWriter(file))
\r
264 line.WriteLine(strCmdLine);
\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
278 /// Reads a serialized XML file that represents a queue of encoding jobs.
\r
280 /// <param name="file">The location of the file to read the queue from.</param>
\r
281 public void LoadQueueFromFile(string file)
\r
283 string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
\r
284 @"HandBrake\hb_queue_recovery.xml");
\r
285 string tempPath = file == "hb_queue_recovery.xml" ? appDataPath : file;
\r
287 if (File.Exists(tempPath))
\r
289 using (FileStream strm = new FileStream(tempPath, FileMode.Open, FileAccess.Read))
\r
291 if (strm.Length != 0)
\r
293 if (serializer == null)
\r
294 serializer = new XmlSerializer(typeof(List<Job>));
\r
296 List<Job> list = serializer.Deserialize(strm) as List<Job>;
\r
299 foreach (Job item in list)
\r
300 this.queue.Add(item);
\r
302 if (file != "hb_queue_recovery.xml")
\r
303 this.WriteQueueStateToFile("hb_queue_recovery.xml");
\r
310 /// Checks the current queue for an existing instance of the specified destination.
\r
312 /// <param name="destination">The destination of the encode.</param>
\r
313 /// <returns>Whether or not the supplied destination is already in the queue.</returns>
\r
314 public bool CheckForDestinationDuplicate(string destination)
\r
316 return this.queue.Any(checkItem => checkItem.Destination.Contains(destination.Replace("\\\\", "\\")));
\r
324 /// Starts encoding the first job in the queue and continues encoding until all jobs
\r
325 /// have been encoded.
\r
327 public void Start()
\r
329 if (this.Count != 0)
\r
332 this.Paused = false;
\r
335 this.Paused = false;
\r
338 Thread theQueue = new Thread(this.StartQueue) { IsBackground = true };
\r
341 if (this.QueueStarted != null)
\r
342 this.QueueStarted(this, new EventArgs());
\r
344 catch (Exception exc)
\r
346 Main.ShowExceptiowWindow("Unable to Start Queue", exc.ToString());
\r
353 /// Requests a pause of the encode queue.
\r
355 public void Pause()
\r
357 this.Paused = true;
\r
359 if (this.QueuePauseRequested != null)
\r
360 this.QueuePauseRequested(this, new EventArgs());
\r
364 /// Run through all the jobs on the queue.
\r
366 /// <param name="state">Object State</param>
\r
367 private void StartQueue(object state)
\r
369 // Run through each item on the queue
\r
370 while (this.Count != 0)
\r
372 Job encJob = this.GetNextJob();
\r
373 this.WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
375 Run(encJob, false);
\r
377 if (HbProcess == null)
\r
381 HbProcess.WaitForExit();
\r
383 AddCLIQueryToLog(encJob);
\r
384 this.CopyLog(this.LastEncode.Destination);
\r
387 HbProcess.Dispose();
\r
390 if (Properties.Settings.Default.growlEncode)
\r
391 GrowlCommunicator.Notify("Encode Completed",
\r
392 "Put down that cocktail...\nyour Handbrake encode is done.");
\r
394 while (this.Paused) // Need to find a better way of doing this.
\r
396 Thread.Sleep(2000);
\r
399 this.LastEncode = new Job();
\r
401 if (this.QueueCompleted != null)
\r
402 this.QueueCompleted(this, new EventArgs());
\r
404 // After the encode is done, we may want to shutdown, suspend etc.
\r