diff options
author | sr55 <[email protected]> | 2011-01-09 14:57:25 +0000 |
---|---|---|
committer | sr55 <[email protected]> | 2011-01-09 14:57:25 +0000 |
commit | 39f2230dd490a6ed1f0c94a3202982f3733a6c22 (patch) | |
tree | d4086f78ecd9156b06485b225c4d10eb3aff9973 /win | |
parent | 49c029031755e78e10cc4c0d004dc93d42714566 (diff) |
WinGui:
- Further Application Services refactoring
* Updates to the Encode Service and models
git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@3739 b64f7644-9d1e-0410-96f1-a4d463321fa5
Diffstat (limited to 'win')
6 files changed, 382 insertions, 319 deletions
diff --git a/win/C#/HandBrake.ApplicationServices/Model/EncodeTask.cs b/win/C#/HandBrake.ApplicationServices/Model/EncodeTask.cs index 32a7c4f4b..b10168aa5 100644 --- a/win/C#/HandBrake.ApplicationServices/Model/EncodeTask.cs +++ b/win/C#/HandBrake.ApplicationServices/Model/EncodeTask.cs @@ -14,6 +14,13 @@ namespace HandBrake.ApplicationServices.Model /// </summary>
public class EncodeTask
{
+ /// <summary>
+ /// Initializes a new instance of the <see cref="EncodeTask"/> class.
+ /// </summary>
+ public EncodeTask()
+ {
+ }
+
#region Source
/// <summary>
/// Gets or sets Source.
diff --git a/win/C#/HandBrake.ApplicationServices/Model/QueueTask.cs b/win/C#/HandBrake.ApplicationServices/Model/QueueTask.cs index 3d648e586..9f92ec609 100644 --- a/win/C#/HandBrake.ApplicationServices/Model/QueueTask.cs +++ b/win/C#/HandBrake.ApplicationServices/Model/QueueTask.cs @@ -17,6 +17,24 @@ namespace HandBrake.ApplicationServices.Model */
/// <summary>
+ /// Initializes a new instance of the <see cref="QueueTask"/> class.
+ /// </summary>
+ public QueueTask()
+ {
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="QueueTask"/> class.
+ /// </summary>
+ /// <param name="query">
+ /// The query.
+ /// </param>
+ public QueueTask(string query)
+ {
+ this.Query = query;
+ }
+
+ /// <summary>
/// Gets or sets the job ID.
/// </summary>
public int Id { get; set; }
diff --git a/win/C#/HandBrake.ApplicationServices/Services/Encode.cs b/win/C#/HandBrake.ApplicationServices/Services/Encode.cs index 64aa51b2a..90c224ac4 100644 --- a/win/C#/HandBrake.ApplicationServices/Services/Encode.cs +++ b/win/C#/HandBrake.ApplicationServices/Services/Encode.cs @@ -13,8 +13,6 @@ namespace HandBrake.ApplicationServices.Services using System.Windows.Forms;
using HandBrake.ApplicationServices.EventArgs;
- using HandBrake.Framework.Services;
- using HandBrake.Framework.Services.Interfaces;
using HandBrake.ApplicationServices.Functions;
using HandBrake.ApplicationServices.Model;
using HandBrake.ApplicationServices.Parsing;
@@ -28,11 +26,6 @@ namespace HandBrake.ApplicationServices.Services #region Private Variables
/// <summary>
- /// The Error Service
- /// </summary>
- protected IErrorService errorService;
-
- /// <summary>
/// The Log Buffer
/// </summary>
private StringBuilder logBuffer;
@@ -50,7 +43,7 @@ namespace HandBrake.ApplicationServices.Services /// <summary>
/// Gets the Process ID
/// </summary>
- private int processID;
+ private int processId;
/// <summary>
/// Windows 7 API Pack wrapper
@@ -59,43 +52,26 @@ namespace HandBrake.ApplicationServices.Services #endregion
- /* Constructor */
-
/// <summary>
/// Initializes a new instance of the <see cref="Encode"/> class.
/// </summary>
public Encode()
{
- this.EncodeStarted += Encode_EncodeStarted;
+ this.EncodeStarted += this.EncodeEncodeStarted;
GrowlCommunicator.Register();
-
- this.errorService = new ErrorService();
}
#region Delegates and Event Handlers
/// <summary>
- /// Encode Progess Status
- /// </summary>
- /// <param name="sender">
- /// The sender.
- /// </param>
- /// <param name="e">
- /// The EncodeProgressEventArgs.
- /// </param>
- public delegate void EncodeProgessStatus(object sender, EncodeProgressEventArgs e);
-
- /* Event Handlers */
-
- /// <summary>
- /// Fires when a new CLI Job starts
+ /// Fires when a new CLI QueueTask starts
/// </summary>
public event EventHandler EncodeStarted;
/// <summary>
- /// Fires when a CLI job finishes.
+ /// Fires when a CLI QueueTask finishes.
/// </summary>
- public event EventHandler EncodeEnded;
+ public event EncodeCompletedStatus EncodeCompleted;
/// <summary>
/// Encode process has progressed
@@ -103,7 +79,7 @@ namespace HandBrake.ApplicationServices.Services public event EncodeProgessStatus EncodeStatusChanged;
#endregion
- /* Properties */
+ #region Properties
/// <summary>
/// Gets or sets The HB Process
@@ -138,36 +114,49 @@ namespace HandBrake.ApplicationServices.Services }
}
- /* Public Methods */
+ #endregion
- /// <summary>
- /// Create a preview sample video
- /// </summary>
- /// <param name="query">
- /// The CLI Query
- /// </param>
- public void CreatePreviewSample(string query)
- {
- this.Run(new QueueTask { Query = query }, false);
- }
+ #region Public Methods
/// <summary>
/// Execute a HandBrakeCLI process.
/// </summary>
- /// <param name="encJob">
- /// The enc Job.
+ /// <param name="encodeQueueTask">
+ /// The encodeQueueTask.
/// </param>
/// <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(QueueTask encJob, bool enableLogging)
+ public void Start(QueueTask encodeQueueTask, bool enableLogging)
{
try
{
+ QueueTask QueueTask = encodeQueueTask as QueueTask;
+
+ if (QueueTask == null)
+ {
+ throw new ArgumentNullException("QueueTask was null");
+ }
+
+ if (IsEncoding)
+ {
+ throw new Exception("HandBrake is already encodeing.");
+ }
+
IsEncoding = true;
if (enableLogging)
- SetupLogging(encJob);
+ {
+ if (!SetupLogging(QueueTask))
+ {
+ if (this.EncodeCompleted != null)
+ this.EncodeCompleted(
+ this,
+ new EncodeCompletedEventArgs(false, null, "Unable to Start Encode Logging process."));
+
+ return;
+ }
+ }
if (Init.PreventSleep)
{
@@ -175,7 +164,7 @@ namespace HandBrake.ApplicationServices.Services }
string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");
- ProcessStartInfo cliStart = new ProcessStartInfo(handbrakeCLIPath, encJob.Query)
+ ProcessStartInfo cliStart = new ProcessStartInfo(handbrakeCLIPath, QueueTask.Query)
{
RedirectStandardOutput = true,
RedirectStandardError = enableLogging ? true : false,
@@ -191,14 +180,14 @@ namespace HandBrake.ApplicationServices.Services this.HbProcess.BeginErrorReadLine();
}
- this.processID = HbProcess.Id;
+ this.processId = HbProcess.Id;
this.processHandle = HbProcess.Handle;
// Set the process Priority
- if (this.processID != -1)
+ if (this.processId != -1)
{
this.HbProcess.EnableRaisingEvents = true;
- this.HbProcess.Exited += HbProcess_Exited;
+ this.HbProcess.Exited += this.HbProcessExited;
}
// Set the Process Priority
@@ -230,17 +219,28 @@ namespace HandBrake.ApplicationServices.Services }
catch (Exception exc)
{
- errorService.ShowError("It would appear that HandBrakeCLI has not started correctly." +
- "You should take a look at the Activity log as it may indicate the reason why.\n\nDetailed Error Information: error occured in runCli()",
- exc.ToString());
+ if (this.EncodeCompleted != null)
+ this.EncodeCompleted(this, new EncodeCompletedEventArgs(false, exc, "An Error has occured in EncodeService.Run()"));
}
}
/// <summary>
- /// Kill the CLI process
+ /// Stop the Encode
/// </summary>
public void Stop()
{
+ this.Stop(null);
+ }
+
+ /// <summary>
+ /// Kill the CLI process
+ /// </summary>
+ /// <param name="exc">
+ /// The Exception that has occured.
+ /// This will get bubbled up through the EncodeCompletedEventArgs
+ /// </param>
+ public void Stop(Exception exc)
+ {
try
{
if (this.HbProcess != null && !this.HbProcess.HasExited)
@@ -248,13 +248,22 @@ namespace HandBrake.ApplicationServices.Services this.HbProcess.Kill();
}
}
- catch (Exception exc)
+ catch (Exception)
{
- errorService.ShowError("Unable to stop HandBrakeCLI. It may not be running.", exc.ToString());
+ // No need to report anything to the user. If it fails, it's probably already stopped.
}
- if (this.EncodeEnded != null)
- this.EncodeEnded(this, new EventArgs());
+
+ if (exc == null)
+ {
+ if (this.EncodeCompleted != null)
+ this.EncodeCompleted(this, new EncodeCompletedEventArgs(true, null, string.Empty));
+ }
+ else
+ {
+ if (this.EncodeCompleted != null)
+ this.EncodeCompleted(this, new EncodeCompletedEventArgs(false, exc, "An Error has occured."));
+ }
}
/// <summary>
@@ -262,7 +271,7 @@ namespace HandBrake.ApplicationServices.Services /// NOTE: This will not work with a MinGW CLI
/// Note: http://www.cygwin.com/ml/cygwin/2006-03/msg00330.html
/// </summary>
- public void SafelyClose()
+ public void SafelyStop()
{
if ((int)this.processHandle == 0)
return;
@@ -279,7 +288,9 @@ namespace HandBrake.ApplicationServices.Services //}*/
}
- /* Helpers */
+ #endregion
+
+ #region Private Helper Methods
/// <summary>
/// Save a copy of the log to the users desired location or a default location
@@ -319,7 +330,7 @@ namespace HandBrake.ApplicationServices.Services }
catch (Exception exc)
{
- errorService.ShowError("Unable to make a copy of the log file", exc.ToString());
+ // This exception doesn't warrent user interaction, but it should be logged (TODO)
}
}
@@ -332,11 +343,11 @@ namespace HandBrake.ApplicationServices.Services /// <param name="e">
/// The EventArgs.
/// </param>
- private void HbProcess_Exited(object sender, EventArgs e)
+ private void HbProcessExited(object sender, EventArgs e)
{
IsEncoding = false;
- if (this.EncodeEnded != null)
- this.EncodeEnded(this, new EventArgs());
+ if (this.EncodeCompleted != null)
+ this.EncodeCompleted(this, new EncodeCompletedEventArgs(true, null, string.Empty));
if (windowsSeven.IsWindowsSeven)
{
@@ -358,7 +369,7 @@ namespace HandBrake.ApplicationServices.Services }
catch (Exception exc)
{
- errorService.ShowError("Unable to close the log file wrtier", exc.ToString());
+ // This exception doesn't warrent user interaction, but it should be logged (TODO)
}
}
@@ -405,7 +416,9 @@ namespace HandBrake.ApplicationServices.Services }
catch (Exception exc)
{
- throw new Exception("Unable to read log file" + Environment.NewLine + exc);
+ logBuffer.Append(
+ Environment.NewLine + "Unable to read Log file..." + Environment.NewLine + exc +
+ Environment.NewLine);
}
}
}
@@ -413,10 +426,13 @@ namespace HandBrake.ApplicationServices.Services /// <summary>
/// Setup the logging.
/// </summary>
- /// <param name="encodeJob">
- /// The encode Job.
+ /// <param name="encodeQueueTask">
+ /// The encode QueueTask.
/// </param>
- private void SetupLogging(QueueTask encodeJob)
+ /// <returns>
+ /// The setup logging.
+ /// </returns>
+ private bool SetupLogging(QueueTask encodeQueueTask)
{
string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
string logFile = Path.Combine(logDir, string.Format("last_encode_log{0}.txt", Init.InstanceId));
@@ -432,8 +448,10 @@ namespace HandBrake.ApplicationServices.Services fileWriter = new StreamWriter(logFile) { AutoFlush = true };
- fileWriter.WriteLine(Logging.CreateCliLogHeader(encodeJob));
- logBuffer.AppendLine(Logging.CreateCliLogHeader(encodeJob));
+ fileWriter.WriteLine(Logging.CreateCliLogHeader(encodeQueueTask));
+ logBuffer.AppendLine(Logging.CreateCliLogHeader(encodeQueueTask));
+
+ return true;
}
catch (Exception exc)
{
@@ -443,7 +461,7 @@ namespace HandBrake.ApplicationServices.Services fileWriter.Dispose();
}
- errorService.ShowError("Error", exc.ToString());
+ return false;
}
}
@@ -472,11 +490,10 @@ namespace HandBrake.ApplicationServices.Services // If the logging grows past 100MB, kill the encode and stop.
if (fileWriter.BaseStream.Length > 100000000)
{
- this.Stop();
- errorService.ShowError("The encode has been stopped. The log file has grown to over 100MB which indicates a serious problem has occured with the encode.",
- "Please check the encode log for an indication of what the problem is.");
+ this.Stop(new Exception("The encode has been stopped. The log file has grown to over 100MB which indicates a serious problem has occured with the encode." +
+ "Please check the encode log for an indication of what the problem is."));
}
- }
+ }
}
catch (Exception exc)
{
@@ -485,8 +502,6 @@ namespace HandBrake.ApplicationServices.Services }
}
- #region Encode Status from Standard Output
-
/// <summary>
/// Encode Started
/// </summary>
@@ -496,14 +511,14 @@ namespace HandBrake.ApplicationServices.Services /// <param name="e">
/// The EventArgs.
/// </param>
- private void Encode_EncodeStarted(object sender, EventArgs e)
+ private void EncodeEncodeStarted(object sender, EventArgs e)
{
Thread monitor = new Thread(EncodeMonitor);
monitor.Start();
}
/// <summary>
- /// Monitor the Job
+ /// Monitor the QueueTask
/// </summary>
private void EncodeMonitor()
{
@@ -533,14 +548,14 @@ namespace HandBrake.ApplicationServices.Services private void EncodeOnEncodeProgress(object sender, int currentTask, int taskCount, float percentComplete, float currentFps, float avg, string timeRemaining)
{
EncodeProgressEventArgs eventArgs = new EncodeProgressEventArgs
- {
- AverageFrameRate = avg,
- CurrentFrameRate = currentFps,
- EstimatedTimeLeft = Converters.EncodeToTimespan(timeRemaining),
- PercentComplete = percentComplete,
- Task = currentTask,
- TaskCount = taskCount
- };
+ {
+ AverageFrameRate = avg,
+ CurrentFrameRate = currentFps,
+ EstimatedTimeLeft = Converters.EncodeToTimespan(timeRemaining),
+ PercentComplete = percentComplete,
+ Task = currentTask,
+ TaskCount = taskCount
+ };
if (this.EncodeStatusChanged != null)
this.EncodeStatusChanged(this, eventArgs);
diff --git a/win/C#/HandBrake.ApplicationServices/Services/Interfaces/IEncode.cs b/win/C#/HandBrake.ApplicationServices/Services/Interfaces/IEncode.cs index 9a0681c2c..399afdec7 100644 --- a/win/C#/HandBrake.ApplicationServices/Services/Interfaces/IEncode.cs +++ b/win/C#/HandBrake.ApplicationServices/Services/Interfaces/IEncode.cs @@ -7,6 +7,31 @@ namespace HandBrake.ApplicationServices.Services.Interfaces {
using System;
+ using HandBrake.ApplicationServices.EventArgs;
+ using HandBrake.ApplicationServices.Model;
+
+ /// <summary>
+ /// Encode Progess Status
+ /// </summary>
+ /// <param name="sender">
+ /// The sender.
+ /// </param>
+ /// <param name="e">
+ /// The EncodeProgressEventArgs.
+ /// </param>
+ public delegate void EncodeProgessStatus(object sender, EncodeProgressEventArgs e);
+
+ /// <summary>
+ /// Encode Progess Status
+ /// </summary>
+ /// <param name="sender">
+ /// The sender.
+ /// </param>
+ /// <param name="e">
+ /// The EncodeProgressEventArgs.
+ /// </param>
+ public delegate void EncodeCompletedStatus(object sender, EncodeCompletedEventArgs e);
+
/// <summary>
/// The IEncode Interface
/// </summary>
@@ -20,12 +45,12 @@ namespace HandBrake.ApplicationServices.Services.Interfaces /// <summary>
/// Fires when a CLI job finishes.
/// </summary>
- event EventHandler EncodeEnded;
+ event EncodeCompletedStatus EncodeCompleted;
/// <summary>
/// Encode process has progressed
/// </summary>
- event Encode.EncodeProgessStatus EncodeStatusChanged;
+ event EncodeProgessStatus EncodeStatusChanged;
/// <summary>
/// Gets a value indicating whether IsEncoding.
@@ -38,12 +63,15 @@ namespace HandBrake.ApplicationServices.Services.Interfaces string ActivityLog { get; }
/// <summary>
- /// Create a preview sample video
+ /// Start with a LibHb EncodeJob Object
/// </summary>
- /// <param name="query">
- /// The CLI Query
+ /// <param name="job">
+ /// The job.
+ /// </param>
+ /// <param name="enableLogging">
+ /// The enable Logging.
/// </param>
- void CreatePreviewSample(string query);
+ void Start(QueueTask job, bool enableLogging);
/// <summary>
/// Kill the CLI process
@@ -55,6 +83,6 @@ namespace HandBrake.ApplicationServices.Services.Interfaces /// NOTE: This will not work with a MinGW CLI
/// Note: http://www.cygwin.com/ml/cygwin/2006-03/msg00330.html
/// </summary>
- void SafelyClose();
+ void SafelyStop();
}
}
\ 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 fc9d7afc1..937e37a78 100644 --- a/win/C#/HandBrake.ApplicationServices/Services/Queue.cs +++ b/win/C#/HandBrake.ApplicationServices/Services/Queue.cs @@ -287,7 +287,7 @@ namespace HandBrake.ApplicationServices.Services }
catch (Exception exc)
{
- errorService.ShowError("Unable to write to the file. Please make sure that the location has the correct permissions for file writing.", exc.ToString());
+ 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;
@@ -364,7 +364,7 @@ namespace HandBrake.ApplicationServices.Services }
catch (Exception exc)
{
- errorService.ShowError("Unable to Start Queue", exc.ToString());
+ throw new Exception("Unable to Start Queue", exc);
}
}
}
@@ -393,7 +393,7 @@ namespace HandBrake.ApplicationServices.Services QueueTask encJob = this.GetNextJob();
this.SaveQueue(); // Update the queue recovery file
- Run(encJob, true);
+ Start(encJob, true);
if (HbProcess == null)
{
diff --git a/win/C#/HandBrake.ApplicationServices/Services/QueueProcessor.cs b/win/C#/HandBrake.ApplicationServices/Services/QueueProcessor.cs index fc56c3ad8..d49d4379a 100644 --- a/win/C#/HandBrake.ApplicationServices/Services/QueueProcessor.cs +++ b/win/C#/HandBrake.ApplicationServices/Services/QueueProcessor.cs @@ -6,14 +6,8 @@ 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;
@@ -23,12 +17,13 @@ namespace HandBrake.ApplicationServices.Services /// <summary>
/// The HandBrake Queue
/// </summary>
- public class QueueProcessor // : IQueueProcessor
+ public class QueueProcessor : IQueueProcessor
{
- /*
- * TODO
- * - Hook this up when the new encode service is in place
- */
+ /*
+ * TODO
+ * - Hookup the logging to write to disk
+ *
+ */
/// <summary>
/// Initializes a new instance of the <see cref="QueueProcessor"/> class.
@@ -43,18 +38,18 @@ namespace HandBrake.ApplicationServices.Services /// </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");
- //}
+ this.QueueManager = queueManager;
+ this.EncodeService = encodeService;
+
+ if (this.QueueManager == null)
+ {
+ throw new ArgumentNullException("queueManager");
+ }
+
+ if (this.QueueManager == null)
+ {
+ throw new ArgumentNullException("queueManager");
+ }
}
/// <summary>
@@ -66,11 +61,11 @@ namespace HandBrake.ApplicationServices.Services /// </param>
public QueueProcessor(int instanceId)
{
- //this.EncodeService = new Encode();
- // this.QueueManager = new QueueManager(instanceId);
+ this.EncodeService = new Encode();
+ this.QueueManager = new QueueManager(instanceId);
}
- //#region Events
+ #region Events
/// <summary>
/// Queue Progess Status
@@ -83,201 +78,201 @@ namespace HandBrake.ApplicationServices.Services /// </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;
- // }
- //}
+ /// <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 |