3 This file is part of the HandBrake source code.
\r
4 Homepage: <http://handbrake.fr/>.
\r
5 It may be used under the terms of the GNU General Public License. */
\r
8 using System.Collections.Generic;
\r
9 using System.Collections.ObjectModel;
\r
11 using System.Threading;
\r
12 using System.Windows.Forms;
\r
13 using System.Xml.Serialization;
\r
14 using Handbrake.Functions;
\r
15 using Handbrake.Parsing;
\r
17 namespace Handbrake.EncodeQueue
\r
19 public class Queue : Encode
\r
21 private static XmlSerializer serializer;
\r
22 private readonly List<Job> queue = new List<Job>();
\r
23 private int NextJobId;
\r
25 #region Event Handlers
\r
27 /// Fires when an encode job has been started.
\r
29 public event EventHandler NewJobStarted;
\r
32 /// Fires when a pause to the encode queue has been requested.
\r
34 public event EventHandler QueuePauseRequested;
\r
37 /// Fires when an encode job has been completed.
\r
39 public event EventHandler CurrentJobCompleted;
\r
42 /// Fires when the entire encode queue has completed.
\r
44 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
56 Remove(0); // Remove the item which we are about to pass out.
\r
58 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 queue.AsReadOnly(); }
\r
72 /// Gets the number of items in the queue.
\r
76 get { return queue.Count; }
\r
80 /// Adds an item to the queue.
\r
82 /// <param name="query">The query that will be passed to the HandBrake CLI.</param>
\r
83 /// <param name="source">The location of the source video.</param>
\r
84 /// <param name="destination">The location where the encoded video will be.</param>
\r
85 /// <param name="customJob">Custom job</param>
\r
86 /// <param name="scanInfo">The Scan</param>
\r
87 public void Add(string query, string source, string destination, bool customJob)
\r
89 Job newJob = new Job { Id = NextJobId++, Query = query, Source = source, Destination = destination, CustomQuery = customJob };
\r
92 WriteQueueStateToFile("hb_queue_recovery.xml");
\r
96 /// Removes an item from the queue.
\r
98 /// <param name="index">The zero-based location of the job in the queue.</param>
\r
99 public void Remove(int index)
\r
101 queue.RemoveAt(index);
\r
102 WriteQueueStateToFile("hb_queue_recovery.xml");
\r
106 /// Moves an item up one position in the queue.
\r
108 /// <param name="index">The zero-based location of the job in the queue.</param>
\r
109 public void MoveUp(int index)
\r
113 Job item = queue[index];
\r
115 queue.RemoveAt(index);
\r
116 queue.Insert((index - 1), item);
\r
119 WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
123 /// Moves an item down one position in the queue.
\r
125 /// <param name="index">The zero-based location of the job in the queue.</param>
\r
126 public void MoveDown(int index)
\r
128 if (index < queue.Count - 1)
\r
130 Job item = queue[index];
\r
132 queue.RemoveAt(index);
\r
133 queue.Insert((index + 1), item);
\r
136 WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
140 /// Writes the current state of the queue to a file.
\r
142 /// <param name="file">The location of the file to write the queue to.</param>
\r
143 public void WriteQueueStateToFile(string file)
\r
145 string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"HandBrake\hb_queue_recovery.xml");
\r
146 string tempPath = file == "hb_queue_recovery.xml" ? appDataPath : file;
\r
150 using (FileStream strm = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
\r
152 if (serializer == null)
\r
153 serializer = new XmlSerializer(typeof(List<Job>));
\r
154 serializer.Serialize(strm, queue);
\r
161 // Any Errors will be out of diskspace/permissions problems.
\r
162 // Don't report them as they'll annoy the user.
\r
167 /// Writes the current state of the queue in the form of a batch (.bat) file.
\r
169 /// <param name="file">The location of the file to write the batch file to.</param>
\r
170 public void WriteBatchScriptToFile(string file)
\r
172 string queries = "";
\r
173 foreach (Job queue_item in queue)
\r
175 string q_item = queue_item.Query;
\r
176 string fullQuery = '"' + Application.StartupPath + "\\HandBrakeCLI.exe" + '"' + q_item;
\r
178 if (queries == string.Empty)
\r
179 queries = queries + fullQuery;
\r
181 queries = queries + " && " + fullQuery;
\r
183 string strCmdLine = queries;
\r
189 // Create a StreamWriter and open the file, Write the batch file query to the file and
\r
190 // Close the stream
\r
191 using (StreamWriter line = new StreamWriter(file))
\r
193 line.WriteLine(strCmdLine);
\r
196 MessageBox.Show("Your batch script has been sucessfully saved.", "Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
\r
200 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
207 /// Reads a serialized XML file that represents a queue of encoding jobs.
\r
209 /// <param name="file">The location of the file to read the queue from.</param>
\r
210 public void LoadQueueFromFile(string file)
\r
212 string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"HandBrake\hb_queue_recovery.xml");
\r
213 string tempPath = file == "hb_queue_recovery.xml" ? appDataPath : file;
\r
215 if (File.Exists(tempPath))
\r
217 using (FileStream strm = new FileStream(tempPath, FileMode.Open, FileAccess.Read))
\r
219 if (strm.Length != 0)
\r
221 if (serializer == null)
\r
222 serializer = new XmlSerializer(typeof(List<Job>));
\r
224 List<Job> list = serializer.Deserialize(strm) as List<Job>;
\r
227 foreach (Job item in list)
\r
230 if (file != "hb_queue_recovery.xml")
\r
231 WriteQueueStateToFile("hb_queue_recovery.xml");
\r
238 /// Checks the current queue for an existing instance of the specified destination.
\r
240 /// <param name="destination">The destination of the encode.</param>
\r
241 /// <returns>Whether or not the supplied destination is already in the queue.</returns>
\r
242 public bool CheckForDestinationDuplicate(string destination)
\r
244 foreach (Job checkItem in queue)
\r
246 if (checkItem.Destination.Contains(destination.Replace("\\\\", "\\")))
\r
258 /// Gets the last encode that was processed.
\r
260 /// <returns></returns>
\r
261 public Job LastEncode { get; set; }
\r
266 public Boolean PauseRequested { get; private set; }
\r
269 /// Starts encoding the first job in the queue and continues encoding until all jobs
\r
270 /// have been encoded.
\r
272 public void Start()
\r
274 if (this.Count != 0)
\r
276 if (PauseRequested)
\r
277 PauseRequested = false;
\r
280 PauseRequested = false;
\r
283 Thread theQueue = new Thread(StartQueue) { IsBackground = true };
\r
286 catch (Exception exc)
\r
288 MessageBox.Show(exc.ToString());
\r
295 /// Requests a pause of the encode queue.
\r
297 public void Pause()
\r
299 PauseRequested = true;
\r
301 if (QueuePauseRequested != null)
\r
302 QueuePauseRequested(this, new EventArgs());
\r
306 /// Stops the current job.
\r
313 private void StartQueue(object state)
\r
315 // Run through each item on the queue
\r
316 while (this.Count != 0)
\r
318 Job encJob = GetNextJob();
\r
319 string query = encJob.Query;
\r
320 WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
324 if (NewJobStarted != null)
\r
325 NewJobStarted(this, new EventArgs());
\r
327 HbProcess.WaitForExit();
\r
329 AddCLIQueryToLog(encJob);
\r
330 CopyLog(LastEncode.Destination);
\r
333 HbProcess.Dispose();
\r
335 IsEncoding = false;
\r
338 if (Properties.Settings.Default.growlEncode)
\r
339 GrowlCommunicator.Notify("Encode Completed", "Put down that cocktail...\nyour Handbrake encode is done.");
\r
341 if (CurrentJobCompleted != null)
\r
342 CurrentJobCompleted(this, new EventArgs());
\r
344 while (PauseRequested) // Need to find a better way of doing this.
\r
346 Thread.Sleep(2000);
\r
349 LastEncode = new Job();
\r
351 if (QueueCompleted != null)
\r
352 QueueCompleted(this, new EventArgs());
\r
354 // After the encode is done, we may want to shutdown, suspend etc.
\r