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.EncodeQueue
\r
17 public class QueueHandler
\r
19 Encode encodeHandler = new Encode();
\r
20 private static XmlSerializer ser = new XmlSerializer(typeof(List<QueueItem>));
\r
21 List<QueueItem> queue = new List<QueueItem>();
\r
22 int id; // Unique identifer number for each job
\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 private string getNextItemForEncoding()
\r
36 QueueItem job = queue[0];
\r
37 String query = job.Query;
\r
38 lastQueueItem = job;
\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 lastQueueItem { get; set; }
\r
50 /// Add's a new item to the queue
\r
52 /// <param name="query">String</param>
\r
53 /// <param name="source"></param>
\r
54 /// <param name="destination"></param>
\r
55 public void add(string query, string source, string destination)
\r
57 QueueItem newJob = new QueueItem { Id = id, Query = query, Source = source, Destination = destination };
\r
64 /// Check to see if a destination path is already on the queue
\r
66 /// <param name="destination">Destination path</param>
\r
67 /// <returns>Boolean True/False. True = Path Exists</returns>
\r
68 public Boolean checkDestinationPath(string destination)
\r
70 foreach (QueueItem checkItem in queue)
\r
72 if (checkItem.Destination.Contains(destination.Replace("\\\\", "\\")))
\r
79 /// Removes an item from the queue.
\r
81 /// <param name="index">Index</param>
\r
82 /// <returns>Bolean true if successful</returns>
\r
83 public void remove(int index)
\r
85 queue.RemoveAt(index);
\r
89 /// Returns how many items are in the queue
\r
91 /// <returns>Int</returns>
\r
98 /// Move an item with an index x, up in the queue
\r
100 /// <param name="index">Int</param>
\r
101 public void moveUp(int index)
\r
105 QueueItem item = queue[index];
\r
107 queue.RemoveAt(index);
\r
108 queue.Insert((index - 1), item);
\r
113 /// Move an item with an index x, down in the queue
\r
115 /// <param name="index">Int</param>
\r
116 public void moveDown(int index)
\r
118 if (index < queue.Count - 1)
\r
120 QueueItem item = queue[index];
\r
122 queue.RemoveAt(index);
\r
123 queue.Insert((index + 1), item);
\r
128 /// Writes the current queue to disk. hb_queue_recovery.xml
\r
129 /// This function is called after getNextItemForEncoding()
\r
131 public void write2disk(string file)
\r
133 string tempPath = file == "hb_queue_recovery.xml" ? Path.Combine(Path.GetTempPath(), "hb_queue_recovery.xml") : file;
\r
137 using (FileStream strm = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
\r
139 ser.Serialize(strm, queue);
\r
146 // Any Errors will be out of diskspace/permissions problems.
\r
147 // Don't report them as they'll annoy the user.
\r
152 /// Writes the current queue to disk to the location specified in file
\r
154 /// <param name="file"></param>
\r
155 public void writeBatchScript(string file)
\r
157 string queries = "";
\r
158 foreach (QueueItem queue_item in queue)
\r
160 string q_item = queue_item.Query;
\r
161 string fullQuery = '"' + Application.StartupPath + "\\HandBrakeCLI.exe" + '"' + q_item;
\r
163 if (queries == string.Empty)
\r
164 queries = queries + fullQuery;
\r
166 queries = queries + " && " + fullQuery;
\r
168 string strCmdLine = queries;
\r
174 // Create a StreamWriter and open the file, Write the batch file query to the file and
\r
175 // Close the stream
\r
176 StreamWriter line = new StreamWriter(file);
\r
177 line.WriteLine(strCmdLine);
\r
180 MessageBox.Show("Your batch script has been sucessfully saved.", "Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
\r
184 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
191 /// Recover the queue from hb_queue_recovery.xml
\r
193 public void recoverQueue(string file)
\r
196 if (file == "hb_queue_recovery.xml")
\r
197 tempPath = Path.Combine(Path.GetTempPath(), "hb_queue_recovery.xml");
\r
201 if (File.Exists(tempPath))
\r
203 using (FileStream strm = new FileStream(tempPath, FileMode.Open, FileAccess.Read))
\r
205 if (strm.Length != 0)
\r
207 List<QueueItem> list = ser.Deserialize(strm) as List<QueueItem>;
\r
210 foreach (QueueItem item in list)
\r
213 if (file != "hb_queue_recovery.xml")
\r
214 write2disk("hb_queue_recovery.xml");
\r
223 public Boolean isEncodeStarted { get; private set; }
\r
224 public Boolean isPaused { get; private set; }
\r
225 public Boolean isEncoding { get; private set; }
\r
226 public EncodeProcess encodeProcess { get; set; }
\r
228 public void startEncode()
\r
231 if (this.count() != 0)
\r
240 theQueue = new Thread(startProc) { IsBackground = true };
\r
243 catch (Exception exc)
\r
245 MessageBox.Show(exc.ToString());
\r
250 public void pauseEncodeQueue()
\r
253 EncodePaused(null);
\r
255 public void endEncode()
\r
257 encodeHandler.closeCLI(encodeProcess);
\r
260 private void startProc(object state)
\r
264 // Run through each item on the queue
\r
265 while (this.count() != 0)
\r
267 string query = getNextItemForEncoding();
\r
268 write2disk("hb_queue_recovery.xml"); // Update the queue recovery file
\r
270 encodeProcess = encodeHandler.runCli(query);
\r
271 EncodeStarted(null);
\r
272 encodeProcess.hbProcProcess.WaitForExit();
\r
274 encodeHandler.addCLIQueryToLog(query);
\r
275 encodeHandler.copyLog(lastQueueItem.Destination);
\r
277 encodeProcess.hbProcProcess.Close();
\r
278 encodeProcess.hbProcProcess.Dispose();
\r
279 EncodeFinished(null);
\r
281 while (isPaused) // Need to find a better way of doing this.
\r
283 Thread.Sleep(10000);
\r
286 EncodeQueueFinished(null);
\r
288 // After the encode is done, we may want to shutdown, suspend etc.
\r
289 encodeHandler.afterEncodeAction(encodeProcess);
\r
291 catch (Exception exc)
\r
293 throw new Exception(exc.ToString());
\r
299 public event EventHandler OnEncodeStart;
\r
300 public event EventHandler OnPaused;
\r
301 public event EventHandler OnEncodeEnded;
\r
302 public event EventHandler OnQueueFinished;
\r
304 // Invoke the Changed event; called whenever encodestatus changes:
\r
305 protected virtual void EncodeStarted(EventArgs e)
\r
307 if (OnEncodeStart != null)
\r
308 OnEncodeStart(this, e);
\r
312 protected virtual void EncodePaused(EventArgs e)
\r
314 if (OnPaused != null)
\r
317 protected virtual void EncodeFinished(EventArgs e)
\r
319 if (OnEncodeEnded != null)
\r
320 OnEncodeEnded(this, e);
\r
322 isEncoding = false;
\r
324 protected virtual void EncodeQueueFinished(EventArgs e)
\r
326 if (OnQueueFinished != null)
\r
327 OnQueueFinished(this, e);
\r