diff options
author | sr55 <[email protected]> | 2011-01-09 14:37:29 +0000 |
---|---|---|
committer | sr55 <[email protected]> | 2011-01-09 14:37:29 +0000 |
commit | 49c029031755e78e10cc4c0d004dc93d42714566 (patch) | |
tree | a1d88a51560ea03fdf03dd3da1179b95436f4365 /win/C#/HandBrake.ApplicationServices/Services | |
parent | 9f60eb35c21d513c6ce6b272e371a279b8a32ae5 (diff) |
WinGui:
- Continuing on with the Application Services re-factoring:
* Added new QueueManager - Manages queue jobs, add, remove, up, down, save, etc
* Queue Processor - Processes a Queue
* New Models (Encode Task + associated model objects) Used for storing jobs as an object rather than query.
This code isn't used yet, that is coming later.
git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@3738 b64f7644-9d1e-0410-96f1-a4d463321fa5
Diffstat (limited to 'win/C#/HandBrake.ApplicationServices/Services')
7 files changed, 808 insertions, 23 deletions
diff --git a/win/C#/HandBrake.ApplicationServices/Services/Encode.cs b/win/C#/HandBrake.ApplicationServices/Services/Encode.cs index ea336a245..64aa51b2a 100644 --- a/win/C#/HandBrake.ApplicationServices/Services/Encode.cs +++ b/win/C#/HandBrake.ApplicationServices/Services/Encode.cs @@ -148,7 +148,7 @@ namespace HandBrake.ApplicationServices.Services /// </param>
public void CreatePreviewSample(string query)
{
- this.Run(new Job { Query = query }, false);
+ this.Run(new QueueTask { Query = query }, false);
}
/// <summary>
@@ -160,7 +160,7 @@ namespace HandBrake.ApplicationServices.Services /// <param name="enableLogging">
/// Enable Logging. When Disabled we onlt parse Standard Ouput for progress info. Standard Error log data is ignored.
/// </param>
- protected void Run(Job encJob, bool enableLogging)
+ protected void Run(QueueTask encJob, bool enableLogging)
{
try
{
@@ -416,7 +416,7 @@ namespace HandBrake.ApplicationServices.Services /// <param name="encodeJob">
/// The encode Job.
/// </param>
- private void SetupLogging(Job encodeJob)
+ private void SetupLogging(QueueTask encodeJob)
{
string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
string logFile = Path.Combine(logDir, string.Format("last_encode_log{0}.txt", Init.InstanceId));
diff --git a/win/C#/HandBrake.ApplicationServices/Services/Interfaces/IQueue.cs b/win/C#/HandBrake.ApplicationServices/Services/Interfaces/IQueue.cs index b489f75bd..ed649fd39 100644 --- a/win/C#/HandBrake.ApplicationServices/Services/Interfaces/IQueue.cs +++ b/win/C#/HandBrake.ApplicationServices/Services/Interfaces/IQueue.cs @@ -40,7 +40,7 @@ namespace HandBrake.ApplicationServices.Services.Interfaces /// Gets or sets the last encode that was processed.
/// </summary>
/// <returns></returns>
- Job LastEncode { get; set; }
+ QueueTask LastEncode { get; set; }
/// <summary>
/// Gets a value indicating whether Request Pause
@@ -50,7 +50,7 @@ namespace HandBrake.ApplicationServices.Services.Interfaces /// <summary>
/// Gets the current state of the encode queue.
/// </summary>
- ReadOnlyCollection<Job> CurrentQueue { get; }
+ ReadOnlyCollection<QueueTask> CurrentQueue { get; }
/// <summary>
/// Gets the number of items in the queue.
@@ -88,7 +88,7 @@ namespace HandBrake.ApplicationServices.Services.Interfaces /// </summary>
/// <param name="index">the job id</param>
/// <returns>A job for the given index or blank job object</returns>
- Job GetJob(int index);
+ QueueTask GetJob(int index);
/// <summary>
/// Moves an item up one position in the queue.
diff --git a/win/C#/HandBrake.ApplicationServices/Services/Interfaces/IQueueManager.cs b/win/C#/HandBrake.ApplicationServices/Services/Interfaces/IQueueManager.cs new file mode 100644 index 000000000..2c9005cfb --- /dev/null +++ b/win/C#/HandBrake.ApplicationServices/Services/Interfaces/IQueueManager.cs @@ -0,0 +1,113 @@ +/* IQueueManager.cs $
+ This file is part of the HandBrake source code.
+ Homepage: <http://handbrake.fr/>.
+ It may be used under the terms of the GNU General Public License. */
+
+namespace HandBrake.ApplicationServices.Services.Interfaces
+{
+ using System;
+ using System.Collections.ObjectModel;
+
+ using HandBrake.ApplicationServices.Model;
+
+ /// <summary>
+ /// The Queue Manager Interface
+ /// </summary>
+ public interface IQueueManager
+ {
+ /// <summary>
+ /// Fires when a job is Added, Removed or Re-Ordered.
+ /// Should be used for triggering an update of the Queue Window.
+ /// </summary>
+ event EventHandler QueueChanged;
+
+ /// <summary>
+ /// Gets or sets Last Processed Job.
+ /// This is set when the job is poped of the queue by GetNextJobForProcessing();
+ /// </summary>
+ QueueTask LastProcessedJob { get; set; }
+
+ /// <summary>
+ /// Gets The current queue.
+ /// </summary>
+ ReadOnlyCollection<QueueTask> Queue { get; }
+
+ /// <summary>
+ /// Gets the number of jobs in the queue
+ /// </summary>
+ int Count { get; }
+
+ /// <summary>
+ /// Add a job to the Queue.
+ /// This method is Thread Safe.
+ /// </summary>
+ /// <param name="job">
+ /// The encode Job object.
+ /// </param>
+ void Add(QueueTask job);
+
+ /// <summary>
+ /// Remove a job from the Queue.
+ /// This method is Thread Safe
+ /// </summary>
+ /// <param name="job">
+ /// The job.
+ /// </param>
+ void Remove(QueueTask job);
+
+ /// <summary>
+ /// Get the first job on the queue for processing.
+ /// This also removes the job from the Queue and sets the LastProcessedJob
+ /// </summary>
+ /// <returns>
+ /// An encode Job object.
+ /// </returns>
+ QueueTask GetNextJobForProcessing();
+
+ /// <summary>
+ /// Moves an item up one position in the queue.
+ /// </summary>
+ /// <param name="index">The zero-based location of the job in the queue.</param>
+ void MoveUp(int index);
+
+ /// <summary>
+ /// Moves an item down one position in the queue.
+ /// </summary>
+ /// <param name="index">The zero-based location of the job in the queue.</param>
+ void MoveDown(int index);
+
+ /// <summary>
+ /// Backup any changes to the queue file
+ /// </summary>
+ /// <param name="exportPath">
+ /// If this is not null or empty, this will be used instead of the standard backup location.
+ /// </param>
+ void BackupQueue(string exportPath);
+
+ /// <summary>
+ /// Restore a Queue from file or from the queue backup file.
+ /// </summary>
+ /// <param name="importPath">
+ /// The import path. String.Empty or null will result in the default file being loaded.
+ /// </param>
+ void RestoreQueue(string importPath);
+
+ /// <summary>
+ /// Checks the current queue for an existing instance of the specified destination.
+ /// </summary>
+ /// <param name="destination">The destination of the encode.</param>
+ /// <returns>Whether or not the supplied destination is already in the queue.</returns>
+ bool CheckForDestinationPathDuplicates(string destination);
+
+ /// <summary>
+ /// Create a batch script from the queue
+ /// </summary>
+ /// <param name="path">
+ /// The path to the location for the script to be saved.
+ /// </param>
+ /// <returns>
+ /// True if sucessful
+ /// </returns>
+ bool WriteBatchScriptToFile(string path);
+ }
+}
\ No newline at end of file diff --git a/win/C#/HandBrake.ApplicationServices/Services/Interfaces/IQueueProcessor.cs b/win/C#/HandBrake.ApplicationServices/Services/Interfaces/IQueueProcessor.cs new file mode 100644 index 000000000..3d71688cb --- /dev/null +++ b/win/C#/HandBrake.ApplicationServices/Services/Interfaces/IQueueProcessor.cs @@ -0,0 +1,51 @@ +namespace HandBrake.ApplicationServices.Services.Interfaces
+{
+ using System;
+
+ /// <summary>
+ /// The Queue Processor
+ /// </summary>
+ public interface IQueueProcessor
+ {
+ /// <summary>
+ /// Fires when the Queue has started
+ /// </summary>
+ event QueueProcessor.QueueProgressStatus JobProcessingStarted;
+
+ /// <summary>
+ /// Fires when a pause to the encode queue has been requested.
+ /// </summary>
+ event EventHandler QueuePaused;
+
+ /// <summary>
+ /// Fires when the entire encode queue has completed.
+ /// </summary>
+ event EventHandler QueueCompleted;
+
+ /// <summary>
+ /// Gets the IEncodeService instance.
+ /// </summary>
+ IEncode EncodeService { get; }
+
+ /// <summary>
+ /// Gets the IQueueManager instance.
+ /// </summary>
+ IQueueManager QueueManager { get; }
+
+ /// <summary>
+ /// Gets a value indicating whether IsProcessing.
+ /// </summary>
+ bool IsProcessing { get; }
+
+ /// <summary>
+ /// Starts encoding the first job in the queue and continues encoding until all jobs
+ /// have been encoded.
+ /// </summary>
+ void Start();
+
+ /// <summary>
+ /// Requests a pause of the encode queue.
+ /// </summary>
+ void Pause();
+ }
+}
\ No newline at end of file diff --git a/win/C#/HandBrake.ApplicationServices/Services/Queue.cs b/win/C#/HandBrake.ApplicationServices/Services/Queue.cs index ae5c437ad..fc9d7afc1 100644 --- a/win/C#/HandBrake.ApplicationServices/Services/Queue.cs +++ b/win/C#/HandBrake.ApplicationServices/Services/Queue.cs @@ -27,7 +27,7 @@ namespace HandBrake.ApplicationServices.Services /// <summary>
/// The Queue Job List
/// </summary>
- private readonly List<Job> queue = new List<Job>();
+ private readonly List<QueueTask> queue = new List<QueueTask>();
/// <summary>
/// An XML Serializer
@@ -67,7 +67,7 @@ namespace HandBrake.ApplicationServices.Services /// Gets or sets the last encode that was processed.
/// </summary>
/// <returns></returns>
- public Job LastEncode { get; set; }
+ public QueueTask LastEncode { get; set; }
/// <summary>
/// Gets a value indicating whether Request Pause
@@ -77,7 +77,7 @@ namespace HandBrake.ApplicationServices.Services /// <summary>
/// Gets the current state of the encode queue.
/// </summary>
- public ReadOnlyCollection<Job> CurrentQueue
+ public ReadOnlyCollection<QueueTask> CurrentQueue
{
get { return this.queue.AsReadOnly(); }
}
@@ -97,9 +97,9 @@ namespace HandBrake.ApplicationServices.Services /// Gets and removes the next job in the queue.
/// </summary>
/// <returns>The job that was removed from the queue.</returns>
- private Job GetNextJob()
+ private QueueTask GetNextJob()
{
- Job job = this.queue[0];
+ QueueTask job = this.queue[0];
this.LastEncode = job;
this.Remove(0); // Remove the item which we are about to pass out.
@@ -128,7 +128,7 @@ namespace HandBrake.ApplicationServices.Services /// </param>
public void Add(string query, int title, string source, string destination, bool customJob)
{
- Job newJob = new Job
+ QueueTask newJob = new QueueTask
{
Id = this.nextJobId++,
Title = title,
@@ -163,12 +163,12 @@ namespace HandBrake.ApplicationServices.Services /// </summary>
/// <param name="index">the job id</param>
/// <returns>A job for the given index or blank job object</returns>
- public Job GetJob(int index)
+ public QueueTask GetJob(int index)
{
if (this.queue.Count >= (index + 1))
return this.queue[index];
- return new Job();
+ return new QueueTask();
}
/// <summary>
@@ -179,7 +179,7 @@ namespace HandBrake.ApplicationServices.Services {
if (index > 0)
{
- Job item = queue[index];
+ QueueTask item = queue[index];
queue.RemoveAt(index);
queue.Insert((index - 1), item);
@@ -199,7 +199,7 @@ namespace HandBrake.ApplicationServices.Services {
if (index < this.queue.Count - 1)
{
- Job item = this.queue[index];
+ QueueTask item = this.queue[index];
this.queue.RemoveAt(index);
this.queue.Insert((index + 1), item);
@@ -236,7 +236,7 @@ namespace HandBrake.ApplicationServices.Services using (FileStream strm = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
{
if (serializer == null)
- serializer = new XmlSerializer(typeof(List<Job>));
+ serializer = new XmlSerializer(typeof(List<QueueTask>));
serializer.Serialize(strm, queue);
strm.Close();
strm.Dispose();
@@ -260,7 +260,7 @@ namespace HandBrake.ApplicationServices.Services public bool WriteBatchScriptToFile(string file)
{
string queries = string.Empty;
- foreach (Job queueItem in this.queue)
+ foreach (QueueTask queueItem in this.queue)
{
string qItem = queueItem.Query;
string fullQuery = '"' + Application.StartupPath + "\\HandBrakeCLI.exe" + '"' + qItem;
@@ -309,12 +309,12 @@ namespace HandBrake.ApplicationServices.Services if (strm.Length != 0)
{
if (serializer == null)
- serializer = new XmlSerializer(typeof(List<Job>));
+ serializer = new XmlSerializer(typeof(List<QueueTask>));
- List<Job> list = serializer.Deserialize(strm) as List<Job>;
+ List<QueueTask> list = serializer.Deserialize(strm) as List<QueueTask>;
if (list != null)
- foreach (Job item in list)
+ foreach (QueueTask item in list)
this.queue.Add(item);
if (!file.Contains("hb_queue_recovery"))
@@ -390,7 +390,7 @@ namespace HandBrake.ApplicationServices.Services // Run through each item on the queue
while (this.Count != 0)
{
- Job encJob = this.GetNextJob();
+ QueueTask encJob = this.GetNextJob();
this.SaveQueue(); // Update the queue recovery file
Run(encJob, true);
@@ -416,7 +416,7 @@ namespace HandBrake.ApplicationServices.Services Thread.Sleep(2000);
}
}
- this.LastEncode = new Job();
+ this.LastEncode = new QueueTask();
if (this.QueueCompleted != null)
this.QueueCompleted(this, new EventArgs());
diff --git a/win/C#/HandBrake.ApplicationServices/Services/QueueManager.cs b/win/C#/HandBrake.ApplicationServices/Services/QueueManager.cs new file mode 100644 index 000000000..05602f13d --- /dev/null +++ b/win/C#/HandBrake.ApplicationServices/Services/QueueManager.cs @@ -0,0 +1,338 @@ +namespace HandBrake.ApplicationServices.Services
+{
+ using System;
+ using System.Collections.Generic;
+ using System.Collections.ObjectModel;
+ using System.IO;
+ using System.Linq;
+ using System.Windows.Forms;
+ using System.Xml.Serialization;
+
+ using HandBrake.ApplicationServices.Model;
+ using HandBrake.ApplicationServices.Services.Interfaces;
+
+ using EventArgs = System.EventArgs;
+
+ /// <summary>
+ /// The Queue Manager.
+ /// Thread Safe.
+ /// </summary>
+ public class QueueManager : IQueueManager
+ {
+ /*
+ * TODO
+ * - Multi HandBrake Instance Support for Queue Saving.
+ * - Rewrite the batch script generator.
+ */
+
+ #region Private Variables
+
+ /// <summary>
+ /// HandBrakes Queue file with a place holder for an extra string.
+ /// Use this with String.Format()
+ /// </summary>
+ private const string QueueFile = "hb_queue_recovery{0}.xml";
+
+ /// <summary>
+ /// A Lock object to maintain thread safety
+ /// </summary>
+ static readonly object QueueLock = new object();
+
+ /// <summary>
+ /// The Queue of Job objects
+ /// </summary>
+ private readonly List<QueueTask> queue = new List<QueueTask>();
+
+ /// <summary>
+ /// The ID of the job last added
+ /// </summary>
+ private int lastJobId;
+
+ /// <summary>
+ /// The instance Id of this HandBrake instance.
+ /// </summary>
+ private int instanceId;
+
+ #endregion
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="QueueManager"/> class.
+ /// </summary>
+ /// <param name="instanceId">
+ /// The instance Id.
+ /// </param>
+ public QueueManager(int instanceId)
+ {
+ this.instanceId = instanceId;
+ }
+
+ #region Events
+ /// <summary>
+ /// Fires when a job is Added, Removed or Re-Ordered.
+ /// Should be used for triggering an update of the Queue Window.
+ /// </summary>
+ public event EventHandler QueueChanged;
+
+ /// <summary>
+ /// Invoke the Queue Changed Event
+ /// </summary>
+ /// <param name="e">
+ /// The e.
+ /// </param>
+ private void InvokeQueueChanged(EventArgs e)
+ {
+ try
+ {
+ this.BackupQueue(string.Empty);
+ }
+ catch (Exception)
+ {
+ // Do Nothing.
+ }
+
+ EventHandler handler = this.QueueChanged;
+ if (handler != null)
+ {
+ handler(this, e);
+ }
+ }
+
+ #endregion
+
+ #region Public Properties
+
+ /// <summary>
+ /// Gets or sets Last Processed Job.
+ /// This is set when the job is poped of the queue by GetNextJobForProcessing();
+ /// </summary>
+ public QueueTask LastProcessedJob { get; set; }
+
+ /// <summary>
+ /// Gets the number of jobs in the queue;
+ /// </summary>
+ public int Count
+ {
+ get
+ {
+ return this.queue.Count;
+ }
+ }
+
+ /// <summary>
+ /// Gets The current queue.
+ /// </summary>
+ public ReadOnlyCollection<QueueTask> Queue
+ {
+ get
+ {
+ return this.queue.AsReadOnly();
+ }
+ }
+
+ #endregion
+
+ #region Public Methods
+
+ /// <summary>
+ /// Add a job to the Queue.
+ /// This method is Thread Safe.
+ /// </summary>
+ /// <param name="job">
+ /// The encode Job object.
+ /// </param>
+ public void Add(QueueTask job)
+ {
+ lock (QueueLock)
+ {
+ // Tag the job with an ID
+ job.Id = lastJobId++;
+ queue.Add(job);
+ InvokeQueueChanged(EventArgs.Empty);
+ }
+ }
+
+ /// <summary>
+ /// Remove a job from the Queue.
+ /// This method is Thread Safe
+ /// </summary>
+ /// <param name="job">
+ /// The job.
+ /// </param>
+ public void Remove(QueueTask job)
+ {
+ lock (QueueLock)
+ {
+ // Tag the job with an ID
+ job.Id = lastJobId++;
+ queue.Add(job);
+ InvokeQueueChanged(EventArgs.Empty);
+ }
+ }
+
+ /// <summary>
+ /// Get the first job on the queue for processing.
+ /// This also removes the job from the Queue and sets the LastProcessedJob
+ /// </summary>
+ /// <returns>
+ /// An encode Job object.
+ /// </returns>
+ public QueueTask GetNextJobForProcessing()
+ {
+ if (this.queue.Count > 0)
+ {
+ QueueTask job = this.queue[0];
+ this.LastProcessedJob = job;
+ this.Remove(job); // Remove the item which we are about to pass out.
+
+ return job;
+ }
+
+ return null;
+ }
+
+ /// <summary>
+ /// Moves an item up one position in the queue.
+ /// </summary>
+ /// <param name="index">The zero-based location of the job in the queue.</param>
+ public void MoveUp(int index)
+ {
+ if (index > 0)
+ {
+ QueueTask item = queue[index];
+
+ queue.RemoveAt(index);
+ queue.Insert((index - 1), item);
+ }
+
+ this.InvokeQueueChanged(EventArgs.Empty);
+ }
+
+ /// <summary>
+ /// Moves an item down one position in the queue.
+ /// </summary>
+ /// <param name="index">The zero-based location of the job in the queue.</param>
+ public void MoveDown(int index)
+ {
+ if (index < this.queue.Count - 1)
+ {
+ QueueTask item = this.queue[index];
+
+ this.queue.RemoveAt(index);
+ this.queue.Insert((index + 1), item);
+ }
+
+ this.InvokeQueueChanged(EventArgs.Empty);
+ }
+
+ /// <summary>
+ /// Backup any changes to the queue file
+ /// </summary>
+ /// <param name="exportPath">
+ /// If this is not null or empty, this will be used instead of the standard backup location.
+ /// </param>
+ public void BackupQueue(string exportPath)
+ {
+ string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"HandBrake\");
+ string tempPath = !string.IsNullOrEmpty(exportPath) ? exportPath : appDataPath + string.Format(QueueFile, string.Empty);
+
+ if (File.Exists(tempPath))
+ {
+ using (FileStream strm = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
+ {
+ XmlSerializer serializer = new XmlSerializer(typeof(List<QueueTask>));
+ serializer.Serialize(strm, queue);
+ strm.Close();
+ strm.Dispose();
+ }
+ }
+ }
+
+ /// <summary>
+ /// Restore a Queue from file or from the queue backup file.
+ /// </summary>
+ /// <param name="importPath">
+ /// The import path. String.Empty or null will result in the default file being loaded.
+ /// </param>
+ public void RestoreQueue(string importPath)
+ {
+ string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"HandBrake\");
+ string tempPath = !string.IsNullOrEmpty(importPath) ? importPath : (appDataPath + string.Format(QueueFile, string.Empty));
+
+ if (File.Exists(tempPath))
+ {
+ using (FileStream strm = new FileStream((!string.IsNullOrEmpty(importPath) ? importPath : tempPath), FileMode.Open, FileAccess.Read))
+ {
+ if (strm.Length != 0)
+ {
+ XmlSerializer serializer = new XmlSerializer(typeof(List<QueueTask>));
+
+ List<QueueTask> list = serializer.Deserialize(strm) as List<QueueTask>;
+
+ if (list != null)
+ foreach (QueueTask item in list)
+ this.queue.Add(item);
+
+ this.InvokeQueueChanged(EventArgs.Empty);
+ }
+ }
+ }
+ }
+
+ /// <summary>
+ /// Checks the current queue for an existing instance of the specified destination.
+ /// </summary>
+ /// <param name="destination">The destination of the encode.</param>
+ /// <returns>Whether or not the supplied destination is already in the queue.</returns>
+ public bool CheckForDestinationPathDuplicates(string destination)
+ {
+ return this.queue.Any(checkItem => checkItem.Task.Destination.Contains(destination.Replace("\\\\", "\\")));
+ }
+
+ /// <summary>
+ /// Writes the current state of the queue in the form of a batch (.bat) file.
+ /// </summary>
+ /// <param name="file">
+ /// The location of the file to write the batch file to.
+ /// </param>
+ /// <returns>
+ /// The write batch script to file.
+ /// </returns>
+ public bool WriteBatchScriptToFile(string file)
+ {
+ string queries = string.Empty;
+ foreach (QueueTask queueItem in this.queue)
+ {
+ string qItem = queueItem.Query;
+ string fullQuery = '"' + Application.StartupPath + "\\HandBrakeCLI.exe" + '"' + qItem;
+
+ if (queries == string.Empty)
+ queries = queries + fullQuery;
+ else
+ queries = queries + " && " + fullQuery;
+ }
+ string strCmdLine = queries;
+
+ if (file != string.Empty)
+ {
+ try
+ {
+ // Create a StreamWriter and open the file, Write the batch file query to the file and
+ // Close the stream
+ using (StreamWriter line = new StreamWriter(file))
+ {
+ line.WriteLine(strCmdLine);
+ }
+
+ return true;
+ }
+ catch (Exception exc)
+ {
+ throw new Exception("Unable to write to the file. Please make sure that the location has the correct permissions for file writing.", exc);
+ }
+ }
+ return false;
+ }
+
+ #endregion
+ }
+}
diff --git a/win/C#/HandBrake.ApplicationServices/Services/QueueProcessor.cs b/win/C#/HandBrake.ApplicationServices/Services/QueueProcessor.cs new file mode 100644 index 000000000..fc56c3ad8 --- /dev/null +++ b/win/C#/HandBrake.ApplicationServices/Services/QueueProcessor.cs @@ -0,0 +1,283 @@ +/* Queue.cs $
+ This file is part of the HandBrake source code.
+ Homepage: <http://handbrake.fr/>.
+ It may be used under the terms of the GNU General Public License. */
+
+namespace HandBrake.ApplicationServices.Services
+{
+ using System;
+ using System.Collections.Generic;
+ using System.Collections.ObjectModel;
+ using System.Diagnostics;
+ using System.IO;
+ using System.Linq;
+ using System.Threading;
+ using System.Windows.Forms;
+ using System.Xml.Serialization;
+
+ using HandBrake.ApplicationServices.EventArgs;
+ using HandBrake.ApplicationServices.Functions;
+ using HandBrake.ApplicationServices.Model;
+ using HandBrake.ApplicationServices.Services.Interfaces;
+
+ /// <summary>
+ /// The HandBrake Queue
+ /// </summary>
+ public class QueueProcessor // : IQueueProcessor
+ {
+ /*
+ * TODO
+ * - Hook this up when the new encode service is in place
+ */
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="QueueProcessor"/> class.
+ /// </summary>
+ /// <param name="queueManager">
+ /// The queue manager.
+ /// </param>
+ /// <param name="encodeService">
+ /// The encode Service.
+ /// </param>
+ /// <exception cref="ArgumentNullException">
+ /// </exception>
+ public QueueProcessor(IQueueManager queueManager, IEncode encodeService)
+ {
+ //this.QueueManager = queueManager;
+ //this.EncodeService = encodeService;
+
+ //if (this.QueueManager == null)
+ //{
+ // throw new ArgumentNullException("queueManager");
+ //}
+
+ //if (this.QueueManager == null)
+ //{
+ // throw new ArgumentNullException("queueManager");
+ //}
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="QueueProcessor"/> class.
+ /// This call also initializes the Encode and QueueManager services
+ /// </summary>
+ /// <param name="instanceId">
+ /// The instance id.
+ /// </param>
+ public QueueProcessor(int instanceId)
+ {
+ //this.EncodeService = new Encode();
+ // this.QueueManager = new QueueManager(instanceId);
+ }
+
+ //#region Events
+
+ /// <summary>
+ /// Queue Progess Status
+ /// </summary>
+ /// <param name="sender">
+ /// The sender.
+ /// </param>
+ /// <param name="e">
+ /// The QueueProgressEventArgs.
+ /// </param>
+ public delegate void QueueProgressStatus(object sender, QueueProgressEventArgs e);
+
+ ///// <summary>
+ ///// Fires when the Queue has started
+ ///// </summary>
+ //public event QueueProgressStatus JobProcessingStarted;
+
+ ///// <summary>
+ ///// Fires when a pause to the encode queue has been requested.
+ ///// </summary>
+ //public event EventHandler QueuePaused;
+
+ ///// <summary>
+ ///// Fires when the entire encode queue has completed.
+ ///// </summary>
+ //public event EventHandler QueueCompleted;
+
+ ///// <summary>
+ ///// Invoke the JobProcessingStarted event
+ ///// </summary>
+ ///// <param name="e">
+ ///// The QueueProgressEventArgs.
+ ///// </param>
+ //private void InvokeJobProcessingStarted(QueueProgressEventArgs e)
+ //{
+ // QueueProgressStatus handler = this.JobProcessingStarted;
+ // if (handler != null)
+ // {
+ // handler(this, e);
+ // }
+ //}
+
+ ///// <summary>
+ ///// Invoke the QueuePaused event
+ ///// </summary>
+ ///// <param name="e">
+ ///// The EventArgs.
+ ///// </param>
+ //private void InvokeQueuePaused(EventArgs e)
+ //{
+ // EventHandler handler = this.QueuePaused;
+ // if (handler != null)
+ // {
+ // handler(this, e);
+ // }
+ //}
+
+ ///// <summary>
+ ///// Invoke the QueueCompleted event.
+ ///// </summary>
+ ///// <param name="e">
+ ///// The EventArgs.
+ ///// </param>
+ //private void InvokeQueueCompleted(EventArgs e)
+ //{
+ // this.IsProcessing = false;
+
+ // EventHandler handler = this.QueueCompleted;
+ // if (handler != null)
+ // {
+ // handler(this, e);
+ // }
+ //}
+
+ //#endregion
+
+ //#region Properties
+
+ ///// <summary>
+ ///// Gets a value indicating whether IsProcessing.
+ ///// </summary>
+ //public bool IsProcessing { get; private set; }
+
+ ///// <summary>
+ ///// Gets the IEncodeService instance.
+ ///// </summary>
+ //public IEncode EncodeService { get; private set; }
+
+ ///// <summary>
+ ///// Gets the IQueueManager instance.
+ ///// </summary>
+ //public IQueueManager QueueManager { get; private set; }
+
+ //#endregion
+
+ ///// <summary>
+ ///// Starts encoding the first job in the queue and continues encoding until all jobs
+ ///// have been encoded.
+ ///// </summary>
+ //public void Start()
+ //{
+ // if (IsProcessing)
+ // {
+ // throw new Exception("Already Processing the Queue");
+ // }
+
+ // IsProcessing = true;
+ // this.EncodeService.EncodeCompleted += this.EncodeServiceEncodeCompleted;
+ // this.ProcessNextJob();
+ //}
+
+ ///// <summary>
+ ///// Requests a pause of the encode queue.
+ ///// </summary>
+ //public void Pause()
+ //{
+ // this.EncodeService.EncodeCompleted -= this.EncodeServiceEncodeCompleted;
+ // this.InvokeQueuePaused(EventArgs.Empty);
+ // this.IsProcessing = false;
+ //}
+
+ ///// <summary>
+ ///// After an encode is complete, move onto the next job.
+ ///// </summary>
+ ///// <param name="sender">
+ ///// The sender.
+ ///// </param>
+ ///// <param name="e">
+ ///// The EncodeCompletedEventArgs.
+ ///// </param>
+ //private void EncodeServiceEncodeCompleted(object sender, EncodeCompletedEventArgs e)
+ //{
+ // // Growl
+ // if (Init.GrowlEncode)
+ // GrowlCommunicator.Notify("Encode Completed",
+ // "Put down that cocktail...\nyour Handbrake encode is done.");
+
+ // // Handling Log Data
+ // // TODO - Still need to re-write this using CopyLog()
+
+ // // Move onto the next job.
+ // this.ProcessNextJob();
+ //}
+
+ ///// <summary>
+ ///// Run through all the jobs on the queue.
+ ///// </summary>
+ //private void ProcessNextJob()
+ //{
+ // if (this.EncodeService.IsEncoding)
+ // {
+ // // We don't want to try start a second encode, so just return out. The event will trigger the next encode automatically.
+ // return;
+ // }
+
+ // QueueTask job = this.QueueManager.GetNextJobForProcessing();
+ // if (job != null)
+ // {
+ // this.EncodeService.Start(job, true);
+ // this.InvokeJobProcessingStarted(new QueueProgressEventArgs(job));
+ // }
+ // else
+ // {
+ // // No more jobs to process, so unsubscribe the event
+ // this.EncodeService.EncodeCompleted -= this.EncodeServiceEncodeCompleted;
+
+ // // Fire the event to tell connected services.
+ // this.InvokeQueueCompleted(EventArgs.Empty);
+
+ // // Run the After encode completeion work
+ // Finish();
+ // }
+ //}
+
+ ///// <summary>
+ ///// Perform an action after an encode. e.g a shutdown, standby, restart etc.
+ ///// </summary>
+ //private static void Finish()
+ //{
+ // // Growl
+ // if (Init.GrowlQueue)
+ // GrowlCommunicator.Notify("Queue Completed", "Put down that cocktail...\nyour Handbrake queue is done.");
+
+ // // Do something whent he encode ends.
+ // switch (Init.CompletionOption)
+ // {
+ // case "Shutdown":
+ // Process.Start("Shutdown", "-s -t 60");
+ // break;
+ // case "Log Off":
+ // Win32.ExitWindowsEx(0, 0);
+ // break;
+ // case "Suspend":
+ // Application.SetSuspendState(PowerState.Suspend, true, true);
+ // break;
+ // case "Hibernate":
+ // Application.SetSuspendState(PowerState.Hibernate, true, true);
+ // break;
+ // case "Lock System":
+ // Win32.LockWorkStation();
+ // break;
+ // case "Quit HandBrake":
+ // Application.Exit();
+ // break;
+ // default:
+ // break;
+ // }
+ //}
+ }
+}
\ No newline at end of file |