using System.Collections.Generic;\r
using System.Collections.ObjectModel;\r
using System.IO;\r
+ using System.Linq;\r
using System.Threading;\r
using System.Windows.Forms;\r
using System.Xml.Serialization;\r
private int nextJobId;\r
\r
/// <summary>\r
+ /// Fires when the Queue has started\r
+ /// </summary>\r
+ public event EventHandler QueueStarted;\r
+\r
+ /// <summary>\r
+ /// Fires when a job is Added, Removed or Re-Ordered.\r
+ /// Should be used for triggering an update of the Queue Window.\r
+ /// </summary>\r
+ public event EventHandler QueueListChanged;\r
+\r
+ /// <summary>\r
/// Fires when a pause to the encode queue has been requested.\r
/// </summary>\r
public event EventHandler QueuePauseRequested;\r
/// </summary>\r
public event EventHandler QueueCompleted;\r
\r
- #region Queue\r
-\r
+ #region Properties\r
/// <summary>\r
- /// Gets and removes the next job in the queue.\r
+ /// Gets or sets the last encode that was processed.\r
/// </summary>\r
- /// <returns>The job that was removed from the queue.</returns>\r
- private Job GetNextJob()\r
- {\r
- Job job = this.queue[0];\r
- this.LastEncode = job;\r
- this.Remove(0); // Remove the item which we are about to pass out.\r
-\r
- this.WriteQueueStateToFile("hb_queue_recovery.xml");\r
+ /// <returns></returns> \r
+ public Job LastEncode { get; set; }\r
\r
- return job;\r
- }\r
+ /// <summary>\r
+ /// Gets a value indicating whether Request Pause\r
+ /// </summary>\r
+ public bool Paused { get; private set; }\r
\r
/// <summary>\r
/// Gets the current state of the encode queue.\r
{\r
get { return this.queue.Count; }\r
}\r
+ #endregion\r
+\r
+ #region Queue\r
+\r
+ /// <summary>\r
+ /// Gets and removes the next job in the queue.\r
+ /// </summary>\r
+ /// <returns>The job that was removed from the queue.</returns>\r
+ private Job GetNextJob()\r
+ {\r
+ Job job = this.queue[0];\r
+ this.LastEncode = job;\r
+ this.Remove(0); // Remove the item which we are about to pass out.\r
+\r
+ this.WriteQueueStateToFile("hb_queue_recovery.xml");\r
+\r
+ return job;\r
+ }\r
\r
/// <summary>\r
/// Adds an item to the queue.\r
{\r
Id = this.nextJobId++,\r
Title = title,\r
- Query = query, \r
- Source = source, \r
- Destination = destination, \r
+ Query = query,\r
+ Source = source,\r
+ Destination = destination,\r
CustomQuery = customJob\r
};\r
\r
this.queue.Add(newJob);\r
this.WriteQueueStateToFile("hb_queue_recovery.xml");\r
+\r
+ if (this.QueueListChanged != null)\r
+ this.QueueListChanged(this, new EventArgs());\r
}\r
\r
/// <summary>\r
{\r
this.queue.RemoveAt(index);\r
this.WriteQueueStateToFile("hb_queue_recovery.xml");\r
+\r
+ if (this.QueueListChanged != null)\r
+ this.QueueListChanged(this, new EventArgs());\r
}\r
\r
/// <summary>\r
}\r
\r
WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file\r
+\r
+ if (this.QueueListChanged != null)\r
+ this.QueueListChanged(this, new EventArgs());\r
}\r
\r
/// <summary>\r
}\r
\r
this.WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file\r
+\r
+ if (this.QueueListChanged != null)\r
+ this.QueueListChanged(this, new EventArgs());\r
}\r
\r
/// <summary>\r
/// <param name="file">The location of the file to write the queue to.</param>\r
public void WriteQueueStateToFile(string file)\r
{\r
- string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), \r
+ string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),\r
@"HandBrake\hb_queue_recovery.xml");\r
string tempPath = file == "hb_queue_recovery.xml" ? appDataPath : file;\r
\r
using (FileStream strm = new FileStream(tempPath, FileMode.Create, FileAccess.Write))\r
{\r
if (serializer == null)\r
- serializer = new XmlSerializer(typeof (List<Job>));\r
+ serializer = new XmlSerializer(typeof(List<Job>));\r
serializer.Serialize(strm, queue);\r
strm.Close();\r
strm.Dispose();\r
line.WriteLine(strCmdLine);\r
}\r
\r
- MessageBox.Show("Your batch script has been sucessfully saved.", "Status", MessageBoxButtons.OK, \r
+ MessageBox.Show("Your batch script has been sucessfully saved.", "Status", MessageBoxButtons.OK,\r
MessageBoxIcon.Asterisk);\r
}\r
- catch (Exception)\r
+ catch (Exception exc)\r
{\r
- MessageBox.Show(\r
- "Unable to write to the file. Please make sure that the location has the correct permissions for file writing.", \r
- "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
+ Main.ShowExceptiowWindow("Unable to write to the file. Please make sure that the location has the correct permissions for file writing.", exc.ToString());\r
}\r
}\r
}\r
/// <param name="file">The location of the file to read the queue from.</param>\r
public void LoadQueueFromFile(string file)\r
{\r
- string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), \r
+ string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),\r
@"HandBrake\hb_queue_recovery.xml");\r
string tempPath = file == "hb_queue_recovery.xml" ? appDataPath : file;\r
\r
if (strm.Length != 0)\r
{\r
if (serializer == null)\r
- serializer = new XmlSerializer(typeof (List<Job>));\r
+ serializer = new XmlSerializer(typeof(List<Job>));\r
\r
List<Job> list = serializer.Deserialize(strm) as List<Job>;\r
\r
/// <returns>Whether or not the supplied destination is already in the queue.</returns>\r
public bool CheckForDestinationDuplicate(string destination)\r
{\r
- foreach (Job checkItem in this.queue)\r
- {\r
- if (checkItem.Destination.Contains(destination.Replace("\\\\", "\\")))\r
- return true;\r
- }\r
-\r
- return false;\r
+ return this.queue.Any(checkItem => checkItem.Destination.Contains(destination.Replace("\\\\", "\\")));\r
}\r
\r
#endregion\r
#region Encoding\r
\r
/// <summary>\r
- /// Gets or sets the last encode that was processed.\r
- /// </summary>\r
- /// <returns></returns> \r
- public Job LastEncode { get; set; }\r
-\r
- /// <summary>\r
- /// Gets a value indicating whether Request Pause\r
- /// </summary>\r
- public bool PauseRequested { get; private set; }\r
-\r
- /// <summary>\r
/// Starts encoding the first job in the queue and continues encoding until all jobs\r
/// have been encoded.\r
/// </summary>\r
{\r
if (this.Count != 0)\r
{\r
- if (this.PauseRequested)\r
- this.PauseRequested = false;\r
+ if (this.Paused)\r
+ this.Paused = false;\r
else\r
{\r
- this.PauseRequested = false;\r
+ this.Paused = false;\r
try\r
{\r
- Thread theQueue = new Thread(this.StartQueue) {IsBackground = true};\r
+ Thread theQueue = new Thread(this.StartQueue) { IsBackground = true };\r
theQueue.Start();\r
+\r
+ if (this.QueueStarted != null)\r
+ this.QueueStarted(this, new EventArgs());\r
}\r
catch (Exception exc)\r
{\r
- MessageBox.Show(exc.ToString());\r
+ Main.ShowExceptiowWindow("Unable to Start Queue", exc.ToString());\r
}\r
}\r
}\r
/// </summary>\r
public void Pause()\r
{\r
- this.PauseRequested = true;\r
+ this.Paused = true;\r
\r
if (this.QueuePauseRequested != null)\r
this.QueuePauseRequested(this, new EventArgs());\r
\r
// Growl\r
if (Properties.Settings.Default.growlEncode)\r
- GrowlCommunicator.Notify("Encode Completed", \r
+ GrowlCommunicator.Notify("Encode Completed",\r
"Put down that cocktail...\nyour Handbrake encode is done.");\r
\r
- while (this.PauseRequested) // Need to find a better way of doing this.\r
+ while (this.Paused) // Need to find a better way of doing this.\r
{\r
Thread.Sleep(2000);\r
}\r