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
50 /// Gets and removes the next job in the queue.
\r
52 /// <returns>The job that was removed from the queue.</returns>
\r
53 private Job GetNextJob()
\r
55 Job job = this.queue[0];
\r
56 this.LastEncode = job;
\r
57 this.Remove(0); // Remove the item which we are about to pass out.
\r
59 this.WriteQueueStateToFile("hb_queue_recovery.xml");
\r
65 /// Gets the current state of the encode queue.
\r
67 public ReadOnlyCollection<Job> CurrentQueue
\r
69 get { return this.queue.AsReadOnly(); }
\r
73 /// Gets the number of items in the queue.
\r
77 get { return this.queue.Count; }
\r
81 /// Adds an item to the queue.
\r
83 /// <param name="query">
\r
84 /// The query that will be passed to the HandBrake CLI.
\r
86 /// <param name="source">
\r
87 /// The location of the source video.
\r
89 /// <param name="destination">
\r
90 /// The location where the encoded video will be.
\r
92 /// <param name="customJob">
\r
95 public void Add(string query, string source, string destination, bool customJob)
\r
97 Job newJob = new Job
\r
99 Id = this.nextJobId++,
\r
102 Destination = destination,
\r
103 CustomQuery = customJob
\r
106 this.queue.Add(newJob);
\r
107 this.WriteQueueStateToFile("hb_queue_recovery.xml");
\r
111 /// Removes an item from the queue.
\r
113 /// <param name="index">The zero-based location of the job in the queue.</param>
\r
114 public void Remove(int index)
\r
116 this.queue.RemoveAt(index);
\r
117 this.WriteQueueStateToFile("hb_queue_recovery.xml");
\r
121 /// Retrieve a job from the queue
\r
123 /// <param name="index">the job id</param>
\r
124 /// <returns>A job for the given index or blank job object</returns>
\r
125 public Job GetJob(int index)
\r
127 if (this.queue.Count >= (index + 1))
\r
128 return this.queue[index];
\r
134 /// Moves an item up one position in the queue.
\r
136 /// <param name="index">The zero-based location of the job in the queue.</param>
\r
137 public void MoveUp(int index)
\r
141 Job item = queue[index];
\r
143 queue.RemoveAt(index);
\r
144 queue.Insert((index - 1), item);
\r
147 WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
151 /// Moves an item down one position in the queue.
\r
153 /// <param name="index">The zero-based location of the job in the queue.</param>
\r
154 public void MoveDown(int index)
\r
156 if (index < this.queue.Count - 1)
\r
158 Job item = this.queue[index];
\r
160 this.queue.RemoveAt(index);
\r
161 this.queue.Insert((index + 1), item);
\r
164 this.WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
168 /// Writes the current state of the queue to a file.
\r
170 /// <param name="file">The location of the file to write the queue to.</param>
\r
171 public void WriteQueueStateToFile(string file)
\r
173 string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
\r
174 @"HandBrake\hb_queue_recovery.xml");
\r
175 string tempPath = file == "hb_queue_recovery.xml" ? appDataPath : file;
\r
179 using (FileStream strm = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
\r
181 if (serializer == null)
\r
182 serializer = new XmlSerializer(typeof (List<Job>));
\r
183 serializer.Serialize(strm, queue);
\r
195 /// Writes the current state of the queue in the form of a batch (.bat) file.
\r
197 /// <param name="file">The location of the file to write the batch file to.</param>
\r
198 public void WriteBatchScriptToFile(string file)
\r
200 string queries = string.Empty;
\r
201 foreach (Job queueItem in this.queue)
\r
203 string qItem = queueItem.Query;
\r
204 string fullQuery = '"' + Application.StartupPath + "\\HandBrakeCLI.exe" + '"' + qItem;
\r
206 if (queries == string.Empty)
\r
207 queries = queries + fullQuery;
\r
209 queries = queries + " && " + fullQuery;
\r
211 string strCmdLine = queries;
\r
213 if (file != string.Empty)
\r
217 // Create a StreamWriter and open the file, Write the batch file query to the file and
\r
218 // Close the stream
\r
219 using (StreamWriter line = new StreamWriter(file))
\r
221 line.WriteLine(strCmdLine);
\r
224 MessageBox.Show("Your batch script has been sucessfully saved.", "Status", MessageBoxButtons.OK,
\r
225 MessageBoxIcon.Asterisk);
\r
230 "Unable to write to the file. Please make sure that the location has the correct permissions for file writing.",
\r
231 "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
\r
237 /// Reads a serialized XML file that represents a queue of encoding jobs.
\r
239 /// <param name="file">The location of the file to read the queue from.</param>
\r
240 public void LoadQueueFromFile(string file)
\r
242 string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
\r
243 @"HandBrake\hb_queue_recovery.xml");
\r
244 string tempPath = file == "hb_queue_recovery.xml" ? appDataPath : file;
\r
246 if (File.Exists(tempPath))
\r
248 using (FileStream strm = new FileStream(tempPath, FileMode.Open, FileAccess.Read))
\r
250 if (strm.Length != 0)
\r
252 if (serializer == null)
\r
253 serializer = new XmlSerializer(typeof (List<Job>));
\r
255 List<Job> list = serializer.Deserialize(strm) as List<Job>;
\r
258 foreach (Job item in list)
\r
259 this.queue.Add(item);
\r
261 if (file != "hb_queue_recovery.xml")
\r
262 this.WriteQueueStateToFile("hb_queue_recovery.xml");
\r
269 /// Checks the current queue for an existing instance of the specified destination.
\r
271 /// <param name="destination">The destination of the encode.</param>
\r
272 /// <returns>Whether or not the supplied destination is already in the queue.</returns>
\r
273 public bool CheckForDestinationDuplicate(string destination)
\r
275 foreach (Job checkItem in this.queue)
\r
277 if (checkItem.Destination.Contains(destination.Replace("\\\\", "\\")))
\r
289 /// Gets or sets the last encode that was processed.
\r
291 /// <returns></returns>
\r
292 public Job LastEncode { get; set; }
\r
295 /// Gets a value indicating whether Request Pause
\r
297 public bool PauseRequested { get; private set; }
\r
300 /// Starts encoding the first job in the queue and continues encoding until all jobs
\r
301 /// have been encoded.
\r
303 public void Start()
\r
305 if (this.Count != 0)
\r
307 if (this.PauseRequested)
\r
308 this.PauseRequested = false;
\r
311 this.PauseRequested = false;
\r
314 Thread theQueue = new Thread(this.StartQueue) {IsBackground = true};
\r
317 catch (Exception exc)
\r
319 MessageBox.Show(exc.ToString());
\r
326 /// Requests a pause of the encode queue.
\r
328 public void Pause()
\r
330 this.PauseRequested = true;
\r
332 if (this.QueuePauseRequested != null)
\r
333 this.QueuePauseRequested(this, new EventArgs());
\r
337 /// Run through all the jobs on the queue.
\r
339 /// <param name="state">Object State</param>
\r
340 private void StartQueue(object state)
\r
342 // Run through each item on the queue
\r
343 while (this.Count != 0)
\r
345 Job encJob = this.GetNextJob();
\r
346 string query = encJob.Query;
\r
347 this.WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
351 HbProcess.WaitForExit();
\r
353 AddCLIQueryToLog(encJob);
\r
354 this.CopyLog(this.LastEncode.Destination);
\r
357 HbProcess.Dispose();
\r
359 IsEncoding = false;
\r
362 if (Properties.Settings.Default.growlEncode)
\r
363 GrowlCommunicator.Notify("Encode Completed",
\r
364 "Put down that cocktail...\nyour Handbrake encode is done.");
\r
366 while (this.PauseRequested) // Need to find a better way of doing this.
\r
368 Thread.Sleep(2000);
\r
371 this.LastEncode = new Job();
\r
373 if (this.QueueCompleted != null)
\r
374 this.QueueCompleted(this, new EventArgs());
\r
376 // After the encode is done, we may want to shutdown, suspend etc.
\r