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
16 namespace Handbrake.EncodeQueue
\r
18 public class Queue : Encode
\r
20 private static XmlSerializer serializer;
\r
21 private readonly List<Job> queue = new List<Job>();
\r
22 private int NextJobId;
\r
25 /// Fires when a pause to the encode queue has been requested.
\r
27 public event EventHandler QueuePauseRequested;
\r
30 /// Fires when the entire encode queue has completed.
\r
32 public event EventHandler QueueCompleted;
\r
36 /// Gets and removes the next job in the queue.
\r
38 /// <returns>The job that was removed from the queue.</returns>
\r
39 private Job GetNextJob()
\r
43 Remove(0); // Remove the item which we are about to pass out.
\r
45 WriteQueueStateToFile("hb_queue_recovery.xml");
\r
51 /// Gets the current state of the encode queue.
\r
53 public ReadOnlyCollection<Job> CurrentQueue
\r
55 get { return queue.AsReadOnly(); }
\r
59 /// Gets the number of items in the queue.
\r
63 get { return queue.Count; }
\r
67 /// Adds an item to the queue.
\r
69 /// <param name="query">The query that will be passed to the HandBrake CLI.</param>
\r
70 /// <param name="source">The location of the source video.</param>
\r
71 /// <param name="destination">The location where the encoded video will be.</param>
\r
72 /// <param name="customJob">Custom job</param>
\r
73 /// <param name="scanInfo">The Scan</param>
\r
74 public void Add(string query, string source, string destination, bool customJob)
\r
76 Job newJob = new Job { Id = NextJobId++, Query = query, Source = source, Destination = destination, CustomQuery = customJob };
\r
79 WriteQueueStateToFile("hb_queue_recovery.xml");
\r
83 /// Removes an item from the queue.
\r
85 /// <param name="index">The zero-based location of the job in the queue.</param>
\r
86 public void Remove(int index)
\r
88 queue.RemoveAt(index);
\r
89 WriteQueueStateToFile("hb_queue_recovery.xml");
\r
93 /// Retrieve a job from the queue
\r
95 /// <param name="index"></param>
\r
96 /// <returns></returns>
\r
97 public Job GetJob(int index)
\r
99 if (queue.Count >= (index +1))
\r
100 return queue[index];
\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
166 /// Writes the current state of the queue in the form of a batch (.bat) file.
\r
168 /// <param name="file">The location of the file to write the batch file to.</param>
\r
169 public void WriteBatchScriptToFile(string file)
\r
171 string queries = "";
\r
172 foreach (Job queue_item in queue)
\r
174 string q_item = queue_item.Query;
\r
175 string fullQuery = '"' + Application.StartupPath + "\\HandBrakeCLI.exe" + '"' + q_item;
\r
177 if (queries == string.Empty)
\r
178 queries = queries + fullQuery;
\r
180 queries = queries + " && " + fullQuery;
\r
182 string strCmdLine = queries;
\r
188 // Create a StreamWriter and open the file, Write the batch file query to the file and
\r
189 // Close the stream
\r
190 using (StreamWriter line = new StreamWriter(file))
\r
192 line.WriteLine(strCmdLine);
\r
195 MessageBox.Show("Your batch script has been sucessfully saved.", "Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
\r
199 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
206 /// Reads a serialized XML file that represents a queue of encoding jobs.
\r
208 /// <param name="file">The location of the file to read the queue from.</param>
\r
209 public void LoadQueueFromFile(string file)
\r
211 string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"HandBrake\hb_queue_recovery.xml");
\r
212 string tempPath = file == "hb_queue_recovery.xml" ? appDataPath : file;
\r
214 if (File.Exists(tempPath))
\r
216 using (FileStream strm = new FileStream(tempPath, FileMode.Open, FileAccess.Read))
\r
218 if (strm.Length != 0)
\r
220 if (serializer == null)
\r
221 serializer = new XmlSerializer(typeof(List<Job>));
\r
223 List<Job> list = serializer.Deserialize(strm) as List<Job>;
\r
226 foreach (Job item in list)
\r
229 if (file != "hb_queue_recovery.xml")
\r
230 WriteQueueStateToFile("hb_queue_recovery.xml");
\r
237 /// Checks the current queue for an existing instance of the specified destination.
\r
239 /// <param name="destination">The destination of the encode.</param>
\r
240 /// <returns>Whether or not the supplied destination is already in the queue.</returns>
\r
241 public bool CheckForDestinationDuplicate(string destination)
\r
243 foreach (Job checkItem in queue)
\r
245 if (checkItem.Destination.Contains(destination.Replace("\\\\", "\\")))
\r
257 /// Gets the last encode that was processed.
\r
259 /// <returns></returns>
\r
260 public Job LastEncode { get; set; }
\r
265 public Boolean PauseRequested { get; private set; }
\r
268 /// Starts encoding the first job in the queue and continues encoding until all jobs
\r
269 /// have been encoded.
\r
271 public void Start()
\r
273 if (this.Count != 0)
\r
275 if (PauseRequested)
\r
276 PauseRequested = false;
\r
279 PauseRequested = false;
\r
282 Thread theQueue = new Thread(StartQueue) { IsBackground = true };
\r
285 catch (Exception exc)
\r
287 MessageBox.Show(exc.ToString());
\r
294 /// Requests a pause of the encode queue.
\r
296 public void Pause()
\r
298 PauseRequested = true;
\r
300 if (QueuePauseRequested != null)
\r
301 QueuePauseRequested(this, new EventArgs());
\r
305 /// Run through all the jobs on the queue.
\r
307 /// <param name="state"></param>
\r
308 private void StartQueue(object state)
\r
310 // Run through each item on the queue
\r
311 while (this.Count != 0)
\r
313 Job encJob = GetNextJob();
\r
314 string query = encJob.Query;
\r
315 WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
319 HbProcess.WaitForExit();
\r
321 AddCLIQueryToLog(encJob);
\r
322 CopyLog(LastEncode.Destination);
\r
325 HbProcess.Dispose();
\r
327 IsEncoding = false;
\r
330 if (Properties.Settings.Default.growlEncode)
\r
331 GrowlCommunicator.Notify("Encode Completed", "Put down that cocktail...\nyour Handbrake encode is done.");
\r
333 while (PauseRequested) // Need to find a better way of doing this.
\r
335 Thread.Sleep(2000);
\r
338 LastEncode = new Job();
\r
340 if (QueueCompleted != null)
\r
341 QueueCompleted(this, new EventArgs());
\r
343 // After the encode is done, we may want to shutdown, suspend etc.
\r