1 /* QueueHandler.cs $
\r
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
10 using System.Windows.Forms;
\r
11 using System.Xml.Serialization;
\r
12 using System.Threading;
\r
14 namespace Handbrake.EncodeQueue
\r
16 public class QueueHandler
\r
18 public Encode encodeHandler = new Encode();
\r
19 private static XmlSerializer ser = new XmlSerializer(typeof(List<Job>));
\r
20 List<Job> queue = new List<Job>();
\r
21 int id; // Unique identifer number for each job
\r
23 #region Queue Handling
\r
24 public List<Job> getQueue()
\r
30 /// Get's the next CLI query for encoding
\r
32 /// <returns>String</returns>
\r
33 private Job getNextJobForEncoding()
\r
36 lastQueueItem = job;
\r
37 remove(0); // Remove the item which we are about to pass out.
\r
39 updateQueueRecoveryFile("hb_queue_recovery.xml");
\r
45 /// Get the last query that was returned by getNextItemForEncoding()
\r
47 /// <returns></returns>
\r
48 public Job lastQueueItem { get; set; }
\r
51 /// Add's a new item to the queue
\r
53 /// <param name="query">String</param>
\r
54 /// <param name="source"></param>
\r
55 /// <param name="destination"></param>
\r
56 public void add(string query, string source, string destination)
\r
58 Job newJob = new Job { Id = id, Query = query, Source = source, Destination = destination };
\r
62 updateQueueRecoveryFile("hb_queue_recovery.xml");
\r
66 /// Removes an item from the queue.
\r
68 /// <param name="index">Index</param>
\r
69 /// <returns>Bolean true if successful</returns>
\r
70 public void remove(int index)
\r
72 queue.RemoveAt(index);
\r
73 updateQueueRecoveryFile("hb_queue_recovery.xml");
\r
77 /// Returns how many items are in the queue
\r
79 /// <returns>Int</returns>
\r
86 /// Move an item with an index x, up in the queue
\r
88 /// <param name="index">Int</param>
\r
89 public void moveUp(int index)
\r
93 Job item = queue[index];
\r
95 queue.RemoveAt(index);
\r
96 queue.Insert((index - 1), item);
\r
98 updateQueueRecoveryFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
102 /// Move an item with an index x, down in the queue
\r
104 /// <param name="index">Int</param>
\r
105 public void moveDown(int index)
\r
107 if (index < queue.Count - 1)
\r
109 Job item = queue[index];
\r
111 queue.RemoveAt(index);
\r
112 queue.Insert((index + 1), item);
\r
114 updateQueueRecoveryFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
118 /// Writes the current queue to disk. hb_queue_recovery.xml
\r
119 /// This function is called after getNextItemForEncoding()
\r
121 public void updateQueueRecoveryFile(string file)
\r
123 string tempPath = file == "hb_queue_recovery.xml" ? Path.Combine(Path.GetTempPath(), "hb_queue_recovery.xml") : file;
\r
127 using (FileStream strm = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
\r
129 ser.Serialize(strm, queue);
\r
136 // Any Errors will be out of diskspace/permissions problems.
\r
137 // Don't report them as they'll annoy the user.
\r
142 /// Writes the current queue to disk to the location specified in file
\r
144 /// <param name="file"></param>
\r
145 public void writeBatchScript(string file)
\r
147 string queries = "";
\r
148 foreach (Job queue_item in queue)
\r
150 string q_item = queue_item.Query;
\r
151 string fullQuery = '"' + Application.StartupPath + "\\HandBrakeCLI.exe" + '"' + q_item;
\r
153 if (queries == string.Empty)
\r
154 queries = queries + fullQuery;
\r
156 queries = queries + " && " + fullQuery;
\r
158 string strCmdLine = queries;
\r
164 // Create a StreamWriter and open the file, Write the batch file query to the file and
\r
165 // Close the stream
\r
166 StreamWriter line = new StreamWriter(file);
\r
167 line.WriteLine(strCmdLine);
\r
170 MessageBox.Show("Your batch script has been sucessfully saved.", "Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
\r
174 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
181 /// Recover the queue from hb_queue_recovery.xml
\r
183 public void recoverQueue(string file)
\r
185 string tempPath = file == "hb_queue_recovery.xml" ? Path.Combine(Path.GetTempPath(), "hb_queue_recovery.xml") : file;
\r
187 if (File.Exists(tempPath))
\r
189 using (FileStream strm = new FileStream(tempPath, FileMode.Open, FileAccess.Read))
\r
191 if (strm.Length != 0)
\r
193 List<Job> list = ser.Deserialize(strm) as List<Job>;
\r
196 foreach (Job item in list)
\r
199 if (file != "hb_queue_recovery.xml")
\r
200 updateQueueRecoveryFile("hb_queue_recovery.xml");
\r
207 /// Check to see if a destination path is already on the queue
\r
209 /// <param name="destination">Destination path</param>
\r
210 /// <returns>Boolean True/False. True = Path Exists</returns>
\r
211 public Boolean checkDestinationPath(string destination)
\r
213 foreach (Job checkItem in queue)
\r
215 if (checkItem.Destination.Contains(destination.Replace("\\\\", "\\")))
\r
224 public Boolean isEncodeStarted { get; private set; }
\r
225 public Boolean isPaused { get; private set; }
\r
226 public Boolean isEncoding { get; private set; }
\r
228 public void startEncode()
\r
230 if (this.count() != 0)
\r
239 Thread theQueue = new Thread(startProcess) { IsBackground = true };
\r
242 catch (Exception exc)
\r
244 MessageBox.Show(exc.ToString());
\r
249 public void pauseEncodeQueue()
\r
252 EncodePaused(null);
\r
254 public void endEncode()
\r
256 encodeHandler.closeCLI();
\r
259 private void startProcess(object state)
\r
263 // Run through each item on the queue
\r
264 while (this.count() != 0)
\r
266 string query = getNextJobForEncoding().Query;
\r
267 updateQueueRecoveryFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
269 encodeHandler.runCli(query);
\r
270 EncodeStarted(null);
\r
271 encodeHandler.hbProcess.WaitForExit();
\r
273 encodeHandler.addCLIQueryToLog(query);
\r
274 encodeHandler.copyLog(lastQueueItem.Destination);
\r
276 encodeHandler.hbProcess.Close();
\r
277 encodeHandler.hbProcess.Dispose();
\r
278 EncodeFinished(null);
\r
280 while (isPaused) // Need to find a better way of doing this.
\r
282 Thread.Sleep(5000);
\r
285 EncodeQueueFinished(null);
\r
287 // After the encode is done, we may want to shutdown, suspend etc.
\r
288 encodeHandler.afterEncodeAction();
\r
290 catch (Exception exc)
\r
292 throw new Exception(exc.ToString());
\r
298 public event EventHandler OnEncodeStart;
\r
299 public event EventHandler OnPaused;
\r
300 public event EventHandler OnEncodeEnded;
\r
301 public event EventHandler OnQueueFinished;
\r
303 // Invoke the Changed event; called whenever encodestatus changes:
\r
304 protected virtual void EncodeStarted(EventArgs e)
\r
306 if (OnEncodeStart != null)
\r
307 OnEncodeStart(this, e);
\r
311 protected virtual void EncodePaused(EventArgs e)
\r
313 if (OnPaused != null)
\r
316 protected virtual void EncodeFinished(EventArgs e)
\r
318 if (OnEncodeEnded != null)
\r
319 OnEncodeEnded(this, e);
\r
321 isEncoding = false;
\r
323 protected virtual void EncodeQueueFinished(EventArgs e)
\r
325 if (OnQueueFinished != null)
\r
326 OnQueueFinished(this, e);
\r