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
13 using System.Diagnostics;
\r
15 namespace Handbrake.Queue
\r
17 public class QueueHandler
\r
19 private static XmlSerializer ser = new XmlSerializer(typeof(List<QueueItem>));
\r
20 List<QueueItem> queue = new List<QueueItem>();
\r
21 int id; // Unique identifer number for each job
\r
22 private QueueItem lastItem;
\r
24 #region Queue Handling
\r
25 public List<QueueItem> getQueue()
\r
31 /// Get's the next CLI query for encoding
\r
33 /// <returns>String</returns>
\r
34 public string getNextItemForEncoding()
\r
36 QueueItem job = queue[0];
\r
37 String query = job.Query;
\r
39 remove(0); // Remove the item which we are about to pass out.
\r
44 /// Get the last query that was returned by getNextItemForEncoding()
\r
46 /// <returns></returns>
\r
47 public QueueItem getLastQueryItem()
\r
53 /// Add's a new item to the queue
\r
55 /// <param name="query">String</param>
\r
56 /// <param name="source"></param>
\r
57 /// <param name="destination"></param>
\r
58 public void add(string query, string source, string destination)
\r
60 QueueItem newJob = new QueueItem();
\r
62 newJob.Query = query;
\r
63 newJob.Source = source;
\r
64 newJob.Destination = destination;
\r
67 // Adds the job to the queue
\r
72 /// Removes an item from the queue.
\r
74 /// <param name="index">Index</param>
\r
75 /// <returns>Bolean true if successful</returns>
\r
76 public Boolean remove(int index)
\r
78 queue.RemoveAt(index);
\r
83 /// Returns how many items are in the queue
\r
85 /// <returns>Int</returns>
\r
92 /// Move an item with an index x, up in the queue
\r
94 /// <param name="index">Int</param>
\r
95 public void moveUp(int index)
\r
99 QueueItem item = queue[index];
\r
101 queue.RemoveAt(index);
\r
102 queue.Insert((index - 1), item);
\r
107 /// Move an item with an index x, down in the queue
\r
109 /// <param name="index">Int</param>
\r
110 public void moveDown(int index)
\r
112 if (index < queue.Count - 1)
\r
114 QueueItem item = queue[index];
\r
116 queue.RemoveAt(index);
\r
117 queue.Insert((index + 1), item);
\r
122 /// Writes the current queue to disk. hb_queue_recovery.xml
\r
123 /// This function is called after getNextItemForEncoding()
\r
125 public void write2disk(string file)
\r
128 if (file == "hb_queue_recovery.xml")
\r
129 tempPath = Path.Combine(Path.GetTempPath(), "hb_queue_recovery.xml");
\r
135 using (FileStream strm = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
\r
137 ser.Serialize(strm, queue);
\r
144 // Any Errors will be out of diskspace/permissions problems.
\r
145 // Don't report them as they'll annoy the user.
\r
150 /// Writes the current queue to disk to the location specified in file
\r
152 /// <param name="file"></param>
\r
153 public void writeBatchScript(string file)
\r
155 string queries = "";
\r
156 foreach (QueueItem queue_item in queue)
\r
158 string q_item = queue_item.Query;
\r
159 string fullQuery = '"' + Application.StartupPath + "\\HandBrakeCLI.exe" + '"' + q_item;
\r
161 if (queries == string.Empty)
\r
162 queries = queries + fullQuery;
\r
164 queries = queries + " && " + fullQuery;
\r
166 string strCmdLine = queries;
\r
172 // Create a StreamWriter and open the file, Write the batch file query to the file and
\r
173 // Close the stream
\r
174 StreamWriter line = new StreamWriter(file);
\r
175 line.WriteLine(strCmdLine);
\r
178 MessageBox.Show("Your batch script has been sucessfully saved.", "Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
\r
182 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
189 /// Recover the queue from hb_queue_recovery.xml
\r
191 public void recoverQueue(string file)
\r
194 if (file == "hb_queue_recovery.xml")
\r
195 tempPath = Path.Combine(Path.GetTempPath(), "hb_queue_recovery.xml");
\r
199 if (File.Exists(tempPath))
\r
201 using (FileStream strm = new FileStream(tempPath, FileMode.Open, FileAccess.Read))
\r
203 if (strm.Length != 0)
\r
205 List<QueueItem> list = ser.Deserialize(strm) as List<QueueItem>;
\r
207 foreach (QueueItem item in list)
\r
210 if (file != "hb_queue_recovery.xml")
\r
211 write2disk("hb_queue_recovery.xml");
\r
218 //------------------------------------------------------------------------
\r
219 Functions.Encode encodeHandler = new Functions.Encode();
\r
220 private Boolean started = false;
\r
221 private Boolean paused;
\r
222 private Boolean encoding;
\r
226 public Boolean isEncodeStarted
\r
228 get { return started; }
\r
230 public Boolean isPaused
\r
232 get { return paused; }
\r
234 public Boolean isEncoding
\r
236 get { return encoding; }
\r
239 public void startEncode()
\r
242 if (this.count() != 0)
\r
251 theQueue = new Thread(startProc) {IsBackground = true};
\r
254 catch (Exception exc)
\r
256 MessageBox.Show(exc.ToString());
\r
261 public void pauseEncode()
\r
264 EncodePaused(null);
\r
267 private void startProc(object state)
\r
272 // Run through each item on the queue
\r
273 while (this.count() != 0)
\r
275 string query = getNextItemForEncoding();
\r
276 write2disk("hb_queue_recovery.xml"); // Update the queue recovery file
\r
278 EncodeStarted(null);
\r
279 hbProc = encodeHandler.runCli(this, query);
\r
280 hbProc.WaitForExit();
\r
282 encodeHandler.addCLIQueryToLog(query);
\r
283 encodeHandler.copyLog(query, getLastQueryItem().Destination);
\r
287 EncodeFinished(null);
\r
289 while (paused) // Need to find a better way of doing this.
\r
291 Thread.Sleep(10000);
\r
294 EncodeQueueFinished(null);
\r
296 // After the encode is done, we may want to shutdown, suspend etc.
\r
297 encodeHandler.afterEncodeAction();
\r
299 catch (Exception exc)
\r
301 throw new Exception(exc.ToString());
\r
307 public event EventHandler OnEncodeStart;
\r
308 public event EventHandler OnPaused;
\r
309 public event EventHandler OnEncodeEnded;
\r
310 public event EventHandler OnQueueFinished;
\r
312 // Invoke the Changed event; called whenever encodestatus changes:
\r
313 protected virtual void EncodeStarted(EventArgs e)
\r
315 if (OnEncodeStart != null)
\r
316 OnEncodeStart(this, e);
\r
320 protected virtual void EncodePaused(EventArgs e)
\r
322 if (OnPaused != null)
\r
325 protected virtual void EncodeFinished(EventArgs e)
\r
327 if (OnEncodeEnded != null)
\r
328 OnEncodeEnded(this, e);
\r
332 protected virtual void EncodeQueueFinished(EventArgs e)
\r
334 if (OnQueueFinished != null)
\r
335 OnQueueFinished(this, e);
\r