/// </summary>\r
public class EncodeTask\r
{\r
+ /// <summary>\r
+ /// Initializes a new instance of the <see cref="EncodeTask"/> class.\r
+ /// </summary>\r
+ public EncodeTask()\r
+ {\r
+ }\r
+\r
#region Source\r
/// <summary>\r
/// Gets or sets Source.\r
*/\r
\r
/// <summary>\r
+ /// Initializes a new instance of the <see cref="QueueTask"/> class.\r
+ /// </summary>\r
+ public QueueTask()\r
+ { \r
+ }\r
+\r
+ /// <summary>\r
+ /// Initializes a new instance of the <see cref="QueueTask"/> class.\r
+ /// </summary>\r
+ /// <param name="query">\r
+ /// The query.\r
+ /// </param>\r
+ public QueueTask(string query)\r
+ {\r
+ this.Query = query;\r
+ }\r
+\r
+ /// <summary>\r
/// Gets or sets the job ID.\r
/// </summary>\r
public int Id { get; set; }\r
using System.Windows.Forms;\r
\r
using HandBrake.ApplicationServices.EventArgs;\r
- using HandBrake.Framework.Services;\r
- using HandBrake.Framework.Services.Interfaces;\r
using HandBrake.ApplicationServices.Functions;\r
using HandBrake.ApplicationServices.Model;\r
using HandBrake.ApplicationServices.Parsing;\r
#region Private Variables\r
\r
/// <summary>\r
- /// The Error Service\r
- /// </summary>\r
- protected IErrorService errorService;\r
-\r
- /// <summary>\r
/// The Log Buffer\r
/// </summary>\r
private StringBuilder logBuffer;\r
/// <summary>\r
/// Gets the Process ID\r
/// </summary>\r
- private int processID;\r
+ private int processId;\r
\r
/// <summary>\r
/// Windows 7 API Pack wrapper\r
\r
#endregion\r
\r
- /* Constructor */\r
-\r
/// <summary>\r
/// Initializes a new instance of the <see cref="Encode"/> class.\r
/// </summary>\r
public Encode()\r
{\r
- this.EncodeStarted += Encode_EncodeStarted;\r
+ this.EncodeStarted += this.EncodeEncodeStarted;\r
GrowlCommunicator.Register();\r
-\r
- this.errorService = new ErrorService();\r
}\r
\r
#region Delegates and Event Handlers\r
\r
/// <summary>\r
- /// Encode Progess Status\r
- /// </summary>\r
- /// <param name="sender">\r
- /// The sender.\r
- /// </param>\r
- /// <param name="e">\r
- /// The EncodeProgressEventArgs.\r
- /// </param>\r
- public delegate void EncodeProgessStatus(object sender, EncodeProgressEventArgs e);\r
-\r
- /* Event Handlers */\r
-\r
- /// <summary>\r
- /// Fires when a new CLI Job starts\r
+ /// Fires when a new CLI QueueTask starts\r
/// </summary>\r
public event EventHandler EncodeStarted;\r
\r
/// <summary>\r
- /// Fires when a CLI job finishes.\r
+ /// Fires when a CLI QueueTask finishes.\r
/// </summary>\r
- public event EventHandler EncodeEnded;\r
+ public event EncodeCompletedStatus EncodeCompleted;\r
\r
/// <summary>\r
/// Encode process has progressed\r
public event EncodeProgessStatus EncodeStatusChanged;\r
#endregion\r
\r
- /* Properties */\r
+ #region Properties\r
\r
/// <summary>\r
/// Gets or sets The HB Process\r
}\r
}\r
\r
- /* Public Methods */\r
+ #endregion\r
\r
- /// <summary>\r
- /// Create a preview sample video\r
- /// </summary>\r
- /// <param name="query">\r
- /// The CLI Query\r
- /// </param>\r
- public void CreatePreviewSample(string query)\r
- {\r
- this.Run(new QueueTask { Query = query }, false);\r
- }\r
+ #region Public Methods\r
\r
/// <summary>\r
/// Execute a HandBrakeCLI process.\r
/// </summary>\r
- /// <param name="encJob">\r
- /// The enc Job.\r
+ /// <param name="encodeQueueTask">\r
+ /// The encodeQueueTask.\r
/// </param>\r
/// <param name="enableLogging">\r
/// Enable Logging. When Disabled we onlt parse Standard Ouput for progress info. Standard Error log data is ignored.\r
/// </param>\r
- protected void Run(QueueTask encJob, bool enableLogging)\r
+ public void Start(QueueTask encodeQueueTask, bool enableLogging)\r
{\r
try\r
{\r
+ QueueTask QueueTask = encodeQueueTask as QueueTask;\r
+\r
+ if (QueueTask == null)\r
+ {\r
+ throw new ArgumentNullException("QueueTask was null");\r
+ }\r
+\r
+ if (IsEncoding)\r
+ {\r
+ throw new Exception("HandBrake is already encodeing.");\r
+ }\r
+\r
IsEncoding = true;\r
\r
if (enableLogging)\r
- SetupLogging(encJob);\r
+ {\r
+ if (!SetupLogging(QueueTask))\r
+ {\r
+ if (this.EncodeCompleted != null)\r
+ this.EncodeCompleted(\r
+ this,\r
+ new EncodeCompletedEventArgs(false, null, "Unable to Start Encode Logging process."));\r
+\r
+ return;\r
+ }\r
+ }\r
\r
if (Init.PreventSleep)\r
{\r
}\r
\r
string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
- ProcessStartInfo cliStart = new ProcessStartInfo(handbrakeCLIPath, encJob.Query)\r
+ ProcessStartInfo cliStart = new ProcessStartInfo(handbrakeCLIPath, QueueTask.Query)\r
{\r
RedirectStandardOutput = true,\r
RedirectStandardError = enableLogging ? true : false,\r
this.HbProcess.BeginErrorReadLine();\r
}\r
\r
- this.processID = HbProcess.Id;\r
+ this.processId = HbProcess.Id;\r
this.processHandle = HbProcess.Handle;\r
\r
// Set the process Priority\r
- if (this.processID != -1)\r
+ if (this.processId != -1)\r
{\r
this.HbProcess.EnableRaisingEvents = true;\r
- this.HbProcess.Exited += HbProcess_Exited;\r
+ this.HbProcess.Exited += this.HbProcessExited;\r
}\r
\r
// Set the Process Priority\r
}\r
catch (Exception exc)\r
{\r
- errorService.ShowError("It would appear that HandBrakeCLI has not started correctly." +\r
- "You should take a look at the Activity log as it may indicate the reason why.\n\nDetailed Error Information: error occured in runCli()",\r
- exc.ToString());\r
+ if (this.EncodeCompleted != null)\r
+ this.EncodeCompleted(this, new EncodeCompletedEventArgs(false, exc, "An Error has occured in EncodeService.Run()"));\r
}\r
}\r
\r
/// <summary>\r
- /// Kill the CLI process\r
+ /// Stop the Encode\r
/// </summary>\r
public void Stop()\r
{\r
+ this.Stop(null);\r
+ }\r
+\r
+ /// <summary>\r
+ /// Kill the CLI process\r
+ /// </summary>\r
+ /// <param name="exc">\r
+ /// The Exception that has occured.\r
+ /// This will get bubbled up through the EncodeCompletedEventArgs\r
+ /// </param>\r
+ public void Stop(Exception exc)\r
+ {\r
try\r
{\r
if (this.HbProcess != null && !this.HbProcess.HasExited)\r
this.HbProcess.Kill();\r
}\r
}\r
- catch (Exception exc)\r
+ catch (Exception)\r
{\r
- errorService.ShowError("Unable to stop HandBrakeCLI. It may not be running.", exc.ToString());\r
+ // No need to report anything to the user. If it fails, it's probably already stopped.\r
}\r
\r
- if (this.EncodeEnded != null)\r
- this.EncodeEnded(this, new EventArgs());\r
+\r
+ if (exc == null)\r
+ {\r
+ if (this.EncodeCompleted != null)\r
+ this.EncodeCompleted(this, new EncodeCompletedEventArgs(true, null, string.Empty));\r
+ }\r
+ else\r
+ {\r
+ if (this.EncodeCompleted != null)\r
+ this.EncodeCompleted(this, new EncodeCompletedEventArgs(false, exc, "An Error has occured."));\r
+ }\r
}\r
\r
/// <summary>\r
/// NOTE: This will not work with a MinGW CLI\r
/// Note: http://www.cygwin.com/ml/cygwin/2006-03/msg00330.html\r
/// </summary>\r
- public void SafelyClose()\r
+ public void SafelyStop()\r
{\r
if ((int)this.processHandle == 0)\r
return;\r
//}*/\r
}\r
\r
- /* Helpers */\r
+ #endregion\r
+\r
+ #region Private Helper Methods\r
\r
/// <summary>\r
/// Save a copy of the log to the users desired location or a default location\r
}\r
catch (Exception exc)\r
{\r
- errorService.ShowError("Unable to make a copy of the log file", exc.ToString());\r
+ // This exception doesn't warrent user interaction, but it should be logged (TODO)\r
}\r
}\r
\r
/// <param name="e">\r
/// The EventArgs.\r
/// </param>\r
- private void HbProcess_Exited(object sender, EventArgs e)\r
+ private void HbProcessExited(object sender, EventArgs e)\r
{\r
IsEncoding = false;\r
- if (this.EncodeEnded != null)\r
- this.EncodeEnded(this, new EventArgs());\r
+ if (this.EncodeCompleted != null)\r
+ this.EncodeCompleted(this, new EncodeCompletedEventArgs(true, null, string.Empty));\r
\r
if (windowsSeven.IsWindowsSeven)\r
{\r
}\r
catch (Exception exc)\r
{\r
- errorService.ShowError("Unable to close the log file wrtier", exc.ToString());\r
+ // This exception doesn't warrent user interaction, but it should be logged (TODO)\r
}\r
}\r
\r
}\r
catch (Exception exc)\r
{\r
- throw new Exception("Unable to read log file" + Environment.NewLine + exc);\r
+ logBuffer.Append(\r
+ Environment.NewLine + "Unable to read Log file..." + Environment.NewLine + exc +\r
+ Environment.NewLine);\r
}\r
}\r
}\r
/// <summary>\r
/// Setup the logging.\r
/// </summary>\r
- /// <param name="encodeJob">\r
- /// The encode Job.\r
+ /// <param name="encodeQueueTask">\r
+ /// The encode QueueTask.\r
/// </param>\r
- private void SetupLogging(QueueTask encodeJob)\r
+ /// <returns>\r
+ /// The setup logging.\r
+ /// </returns>\r
+ private bool SetupLogging(QueueTask encodeQueueTask)\r
{\r
string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
string logFile = Path.Combine(logDir, string.Format("last_encode_log{0}.txt", Init.InstanceId));\r
\r
fileWriter = new StreamWriter(logFile) { AutoFlush = true };\r
\r
- fileWriter.WriteLine(Logging.CreateCliLogHeader(encodeJob));\r
- logBuffer.AppendLine(Logging.CreateCliLogHeader(encodeJob));\r
+ fileWriter.WriteLine(Logging.CreateCliLogHeader(encodeQueueTask));\r
+ logBuffer.AppendLine(Logging.CreateCliLogHeader(encodeQueueTask));\r
+\r
+ return true;\r
}\r
catch (Exception exc)\r
{\r
fileWriter.Dispose();\r
}\r
\r
- errorService.ShowError("Error", exc.ToString());\r
+ return false;\r
}\r
}\r
\r
// If the logging grows past 100MB, kill the encode and stop.\r
if (fileWriter.BaseStream.Length > 100000000)\r
{\r
- this.Stop();\r
- 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.",\r
- "Please check the encode log for an indication of what the problem is.");\r
+ 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." +\r
+ "Please check the encode log for an indication of what the problem is."));\r
}\r
- } \r
+ }\r
}\r
catch (Exception exc)\r
{\r
}\r
}\r
\r
- #region Encode Status from Standard Output\r
-\r
/// <summary>\r
/// Encode Started\r
/// </summary>\r
/// <param name="e">\r
/// The EventArgs.\r
/// </param>\r
- private void Encode_EncodeStarted(object sender, EventArgs e)\r
+ private void EncodeEncodeStarted(object sender, EventArgs e)\r
{\r
Thread monitor = new Thread(EncodeMonitor);\r
monitor.Start();\r
}\r
\r
/// <summary>\r
- /// Monitor the Job\r
+ /// Monitor the QueueTask\r
/// </summary>\r
private void EncodeMonitor()\r
{\r
private void EncodeOnEncodeProgress(object sender, int currentTask, int taskCount, float percentComplete, float currentFps, float avg, string timeRemaining)\r
{\r
EncodeProgressEventArgs eventArgs = new EncodeProgressEventArgs\r
- {\r
- AverageFrameRate = avg,\r
- CurrentFrameRate = currentFps,\r
- EstimatedTimeLeft = Converters.EncodeToTimespan(timeRemaining),\r
- PercentComplete = percentComplete,\r
- Task = currentTask,\r
- TaskCount = taskCount\r
- };\r
+ {\r
+ AverageFrameRate = avg,\r
+ CurrentFrameRate = currentFps,\r
+ EstimatedTimeLeft = Converters.EncodeToTimespan(timeRemaining),\r
+ PercentComplete = percentComplete,\r
+ Task = currentTask,\r
+ TaskCount = taskCount\r
+ };\r
\r
if (this.EncodeStatusChanged != null)\r
this.EncodeStatusChanged(this, eventArgs);\r
{\r
using System;\r
\r
+ using HandBrake.ApplicationServices.EventArgs;\r
+ using HandBrake.ApplicationServices.Model;\r
+\r
+ /// <summary>\r
+ /// Encode Progess Status\r
+ /// </summary>\r
+ /// <param name="sender">\r
+ /// The sender.\r
+ /// </param>\r
+ /// <param name="e">\r
+ /// The EncodeProgressEventArgs.\r
+ /// </param>\r
+ public delegate void EncodeProgessStatus(object sender, EncodeProgressEventArgs e);\r
+\r
+ /// <summary>\r
+ /// Encode Progess Status\r
+ /// </summary>\r
+ /// <param name="sender">\r
+ /// The sender.\r
+ /// </param>\r
+ /// <param name="e">\r
+ /// The EncodeProgressEventArgs.\r
+ /// </param>\r
+ public delegate void EncodeCompletedStatus(object sender, EncodeCompletedEventArgs e);\r
+\r
/// <summary>\r
/// The IEncode Interface\r
/// </summary>\r
/// <summary>\r
/// Fires when a CLI job finishes.\r
/// </summary>\r
- event EventHandler EncodeEnded;\r
+ event EncodeCompletedStatus EncodeCompleted;\r
\r
/// <summary>\r
/// Encode process has progressed\r
/// </summary>\r
- event Encode.EncodeProgessStatus EncodeStatusChanged;\r
+ event EncodeProgessStatus EncodeStatusChanged;\r
\r
/// <summary>\r
/// Gets a value indicating whether IsEncoding.\r
string ActivityLog { get; }\r
\r
/// <summary>\r
- /// Create a preview sample video\r
+ /// Start with a LibHb EncodeJob Object\r
/// </summary>\r
- /// <param name="query">\r
- /// The CLI Query\r
+ /// <param name="job">\r
+ /// The job.\r
+ /// </param>\r
+ /// <param name="enableLogging">\r
+ /// The enable Logging.\r
/// </param>\r
- void CreatePreviewSample(string query);\r
+ void Start(QueueTask job, bool enableLogging);\r
\r
/// <summary>\r
/// Kill the CLI process\r
/// NOTE: This will not work with a MinGW CLI\r
/// Note: http://www.cygwin.com/ml/cygwin/2006-03/msg00330.html\r
/// </summary>\r
- void SafelyClose();\r
+ void SafelyStop();\r
}\r
}
\ No newline at end of file
}\r
catch (Exception exc)\r
{\r
- errorService.ShowError("Unable to write to the file. Please make sure that the location has the correct permissions for file writing.", exc.ToString());\r
+ throw new Exception("Unable to write to the file. Please make sure that the location has the correct permissions for file writing.", exc);\r
}\r
}\r
return false;\r
}\r
catch (Exception exc)\r
{\r
- errorService.ShowError("Unable to Start Queue", exc.ToString());\r
+ throw new Exception("Unable to Start Queue", exc);\r
}\r
}\r
}\r
QueueTask encJob = this.GetNextJob();\r
this.SaveQueue(); // Update the queue recovery file\r
\r
- Run(encJob, true);\r
+ Start(encJob, true);\r
\r
if (HbProcess == null)\r
{\r
namespace HandBrake.ApplicationServices.Services\r
{\r
using System;\r
- using System.Collections.Generic;\r
- using System.Collections.ObjectModel;\r
using System.Diagnostics;\r
- using System.IO;\r
- using System.Linq;\r
- using System.Threading;\r
using System.Windows.Forms;\r
- using System.Xml.Serialization;\r
\r
using HandBrake.ApplicationServices.EventArgs;\r
using HandBrake.ApplicationServices.Functions;\r
/// <summary>\r
/// The HandBrake Queue\r
/// </summary>\r
- public class QueueProcessor // : IQueueProcessor\r
+ public class QueueProcessor : IQueueProcessor\r
{\r
- /*\r
- * TODO\r
- * - Hook this up when the new encode service is in place\r
- */\r
+ /*\r
+ * TODO\r
+ * - Hookup the logging to write to disk\r
+ * \r
+ */\r
\r
/// <summary>\r
/// Initializes a new instance of the <see cref="QueueProcessor"/> class.\r
/// </exception>\r
public QueueProcessor(IQueueManager queueManager, IEncode encodeService)\r
{\r
- //this.QueueManager = queueManager;\r
- //this.EncodeService = encodeService;\r
-\r
- //if (this.QueueManager == null)\r
- //{\r
- // throw new ArgumentNullException("queueManager");\r
- //}\r
-\r
- //if (this.QueueManager == null)\r
- //{\r
- // throw new ArgumentNullException("queueManager");\r
- //}\r
+ this.QueueManager = queueManager;\r
+ this.EncodeService = encodeService;\r
+\r
+ if (this.QueueManager == null)\r
+ {\r
+ throw new ArgumentNullException("queueManager");\r
+ }\r
+\r
+ if (this.QueueManager == null)\r
+ {\r
+ throw new ArgumentNullException("queueManager");\r
+ }\r
}\r
\r
/// <summary>\r
/// </param>\r
public QueueProcessor(int instanceId)\r
{\r
- //this.EncodeService = new Encode();\r
- // this.QueueManager = new QueueManager(instanceId);\r
+ this.EncodeService = new Encode();\r
+ this.QueueManager = new QueueManager(instanceId);\r
}\r
\r
- //#region Events\r
+ #region Events\r
\r
/// <summary>\r
/// Queue Progess Status\r
/// </param>\r
public delegate void QueueProgressStatus(object sender, QueueProgressEventArgs e);\r
\r
- ///// <summary>\r
- ///// Fires when the Queue has started\r
- ///// </summary>\r
- //public event QueueProgressStatus JobProcessingStarted;\r
-\r
- ///// <summary>\r
- ///// Fires when a pause to the encode queue has been requested.\r
- ///// </summary>\r
- //public event EventHandler QueuePaused;\r
-\r
- ///// <summary>\r
- ///// Fires when the entire encode queue has completed.\r
- ///// </summary>\r
- //public event EventHandler QueueCompleted;\r
-\r
- ///// <summary>\r
- ///// Invoke the JobProcessingStarted event\r
- ///// </summary>\r
- ///// <param name="e">\r
- ///// The QueueProgressEventArgs.\r
- ///// </param>\r
- //private void InvokeJobProcessingStarted(QueueProgressEventArgs e)\r
- //{\r
- // QueueProgressStatus handler = this.JobProcessingStarted;\r
- // if (handler != null)\r
- // {\r
- // handler(this, e);\r
- // }\r
- //}\r
-\r
- ///// <summary>\r
- ///// Invoke the QueuePaused event\r
- ///// </summary>\r
- ///// <param name="e">\r
- ///// The EventArgs.\r
- ///// </param>\r
- //private void InvokeQueuePaused(EventArgs e)\r
- //{\r
- // EventHandler handler = this.QueuePaused;\r
- // if (handler != null)\r
- // {\r
- // handler(this, e);\r
- // }\r
- //}\r
-\r
- ///// <summary>\r
- ///// Invoke the QueueCompleted event.\r
- ///// </summary>\r
- ///// <param name="e">\r
- ///// The EventArgs.\r
- ///// </param>\r
- //private void InvokeQueueCompleted(EventArgs e)\r
- //{\r
- // this.IsProcessing = false;\r
-\r
- // EventHandler handler = this.QueueCompleted;\r
- // if (handler != null)\r
- // {\r
- // handler(this, e);\r
- // }\r
- //}\r
-\r
- //#endregion\r
-\r
- //#region Properties\r
-\r
- ///// <summary>\r
- ///// Gets a value indicating whether IsProcessing.\r
- ///// </summary>\r
- //public bool IsProcessing { get; private set; }\r
-\r
- ///// <summary>\r
- ///// Gets the IEncodeService instance.\r
- ///// </summary>\r
- //public IEncode EncodeService { get; private set; }\r
-\r
- ///// <summary>\r
- ///// Gets the IQueueManager instance.\r
- ///// </summary>\r
- //public IQueueManager QueueManager { get; private set; }\r
-\r
- //#endregion\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
- //public void Start()\r
- //{\r
- // if (IsProcessing)\r
- // {\r
- // throw new Exception("Already Processing the Queue");\r
- // }\r
-\r
- // IsProcessing = true;\r
- // this.EncodeService.EncodeCompleted += this.EncodeServiceEncodeCompleted;\r
- // this.ProcessNextJob();\r
- //}\r
-\r
- ///// <summary>\r
- ///// Requests a pause of the encode queue.\r
- ///// </summary>\r
- //public void Pause()\r
- //{\r
- // this.EncodeService.EncodeCompleted -= this.EncodeServiceEncodeCompleted; \r
- // this.InvokeQueuePaused(EventArgs.Empty);\r
- // this.IsProcessing = false;\r
- //}\r
-\r
- ///// <summary>\r
- ///// After an encode is complete, move onto the next job.\r
- ///// </summary>\r
- ///// <param name="sender">\r
- ///// The sender.\r
- ///// </param>\r
- ///// <param name="e">\r
- ///// The EncodeCompletedEventArgs.\r
- ///// </param>\r
- //private void EncodeServiceEncodeCompleted(object sender, EncodeCompletedEventArgs e)\r
- //{\r
- // // Growl\r
- // if (Init.GrowlEncode)\r
- // GrowlCommunicator.Notify("Encode Completed",\r
- // "Put down that cocktail...\nyour Handbrake encode is done.");\r
-\r
- // // Handling Log Data \r
- // // TODO - Still need to re-write this using CopyLog()\r
-\r
- // // Move onto the next job.\r
- // this.ProcessNextJob();\r
- //}\r
-\r
- ///// <summary>\r
- ///// Run through all the jobs on the queue.\r
- ///// </summary>\r
- //private void ProcessNextJob()\r
- //{\r
- // if (this.EncodeService.IsEncoding)\r
- // {\r
- // // We don't want to try start a second encode, so just return out. The event will trigger the next encode automatically.\r
- // return;\r
- // }\r
-\r
- // QueueTask job = this.QueueManager.GetNextJobForProcessing();\r
- // if (job != null)\r
- // {\r
- // this.EncodeService.Start(job, true);\r
- // this.InvokeJobProcessingStarted(new QueueProgressEventArgs(job));\r
- // } \r
- // else\r
- // {\r
- // // No more jobs to process, so unsubscribe the event\r
- // this.EncodeService.EncodeCompleted -= this.EncodeServiceEncodeCompleted;\r
-\r
- // // Fire the event to tell connected services.\r
- // this.InvokeQueueCompleted(EventArgs.Empty);\r
-\r
- // // Run the After encode completeion work\r
- // Finish();\r
- // }\r
- //}\r
-\r
- ///// <summary>\r
- ///// Perform an action after an encode. e.g a shutdown, standby, restart etc.\r
- ///// </summary>\r
- //private static void Finish()\r
- //{\r
- // // Growl\r
- // if (Init.GrowlQueue)\r
- // GrowlCommunicator.Notify("Queue Completed", "Put down that cocktail...\nyour Handbrake queue is done.");\r
-\r
- // // Do something whent he encode ends.\r
- // switch (Init.CompletionOption)\r
- // {\r
- // case "Shutdown":\r
- // Process.Start("Shutdown", "-s -t 60");\r
- // break;\r
- // case "Log Off":\r
- // Win32.ExitWindowsEx(0, 0);\r
- // break;\r
- // case "Suspend":\r
- // Application.SetSuspendState(PowerState.Suspend, true, true);\r
- // break;\r
- // case "Hibernate":\r
- // Application.SetSuspendState(PowerState.Hibernate, true, true);\r
- // break;\r
- // case "Lock System":\r
- // Win32.LockWorkStation();\r
- // break;\r
- // case "Quit HandBrake":\r
- // Application.Exit();\r
- // break;\r
- // default:\r
- // break;\r
- // }\r
- //}\r
+ /// <summary>\r
+ /// Fires when the Queue has started\r
+ /// </summary>\r
+ public event QueueProgressStatus JobProcessingStarted;\r
+\r
+ /// <summary>\r
+ /// Fires when a pause to the encode queue has been requested.\r
+ /// </summary>\r
+ public event EventHandler QueuePaused;\r
+\r
+ /// <summary>\r
+ /// Fires when the entire encode queue has completed.\r
+ /// </summary>\r
+ public event EventHandler QueueCompleted;\r
+\r
+ /// <summary>\r
+ /// Invoke the JobProcessingStarted event\r
+ /// </summary>\r
+ /// <param name="e">\r
+ /// The QueueProgressEventArgs.\r
+ /// </param>\r
+ private void InvokeJobProcessingStarted(QueueProgressEventArgs e)\r
+ {\r
+ QueueProgressStatus handler = this.JobProcessingStarted;\r
+ if (handler != null)\r
+ {\r
+ handler(this, e);\r
+ }\r
+ }\r
+\r
+ /// <summary>\r
+ /// Invoke the QueuePaused event\r
+ /// </summary>\r
+ /// <param name="e">\r
+ /// The EventArgs.\r
+ /// </param>\r
+ private void InvokeQueuePaused(EventArgs e)\r
+ {\r
+ EventHandler handler = this.QueuePaused;\r
+ if (handler != null)\r
+ {\r
+ handler(this, e);\r
+ }\r
+ }\r
+\r
+ /// <summary>\r
+ /// Invoke the QueueCompleted event.\r
+ /// </summary>\r
+ /// <param name="e">\r
+ /// The EventArgs.\r
+ /// </param>\r
+ private void InvokeQueueCompleted(EventArgs e)\r
+ {\r
+ this.IsProcessing = false;\r
+\r
+ EventHandler handler = this.QueueCompleted;\r
+ if (handler != null)\r
+ {\r
+ handler(this, e);\r
+ }\r
+ }\r
+\r
+ #endregion\r
+\r
+ #region Properties\r
+\r
+ /// <summary>\r
+ /// Gets a value indicating whether IsProcessing.\r
+ /// </summary>\r
+ public bool IsProcessing { get; private set; }\r
+\r
+ /// <summary>\r
+ /// Gets the IEncodeService instance.\r
+ /// </summary>\r
+ public IEncode EncodeService { get; private set; }\r
+\r
+ /// <summary>\r
+ /// Gets the IQueueManager instance.\r
+ /// </summary>\r
+ public IQueueManager QueueManager { get; private set; }\r
+\r
+ #endregion\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
+ public void Start()\r
+ {\r
+ if (IsProcessing)\r
+ {\r
+ throw new Exception("Already Processing the Queue");\r
+ }\r
+\r
+ IsProcessing = true;\r
+ this.EncodeService.EncodeCompleted += this.EncodeServiceEncodeCompleted;\r
+ this.ProcessNextJob();\r
+ }\r
+\r
+ /// <summary>\r
+ /// Requests a pause of the encode queue.\r
+ /// </summary>\r
+ public void Pause()\r
+ {\r
+ this.EncodeService.EncodeCompleted -= this.EncodeServiceEncodeCompleted;\r
+ this.InvokeQueuePaused(EventArgs.Empty);\r
+ this.IsProcessing = false;\r
+ }\r
+\r
+ /// <summary>\r
+ /// After an encode is complete, move onto the next job.\r
+ /// </summary>\r
+ /// <param name="sender">\r
+ /// The sender.\r
+ /// </param>\r
+ /// <param name="e">\r
+ /// The EncodeCompletedEventArgs.\r
+ /// </param>\r
+ private void EncodeServiceEncodeCompleted(object sender, EncodeCompletedEventArgs e)\r
+ {\r
+ // Growl\r
+ if (Init.GrowlEncode)\r
+ GrowlCommunicator.Notify("Encode Completed",\r
+ "Put down that cocktail...\nyour Handbrake encode is done.");\r
+\r
+ // Handling Log Data \r
+ // TODO - Still need to re-write this using CopyLog()\r
+\r
+ // Move onto the next job.\r
+ this.ProcessNextJob();\r
+ }\r
+\r
+ /// <summary>\r
+ /// Run through all the jobs on the queue.\r
+ /// </summary>\r
+ private void ProcessNextJob()\r
+ {\r
+ if (this.EncodeService.IsEncoding)\r
+ {\r
+ // We don't want to try start a second encode, so just return out. The event will trigger the next encode automatically.\r
+ return;\r
+ }\r
+\r
+ QueueTask job = this.QueueManager.GetNextJobForProcessing();\r
+ if (job != null)\r
+ {\r
+ this.EncodeService.Start(job, true);\r
+ this.InvokeJobProcessingStarted(new QueueProgressEventArgs(job));\r
+ }\r
+ else\r
+ {\r
+ // No more jobs to process, so unsubscribe the event\r
+ this.EncodeService.EncodeCompleted -= this.EncodeServiceEncodeCompleted;\r
+\r
+ // Fire the event to tell connected services.\r
+ this.InvokeQueueCompleted(EventArgs.Empty);\r
+\r
+ // Run the After encode completeion work\r
+ Finish();\r
+ }\r
+ }\r
+\r
+ /// <summary>\r
+ /// Perform an action after an encode. e.g a shutdown, standby, restart etc.\r
+ /// </summary>\r
+ private static void Finish()\r
+ {\r
+ // Growl\r
+ if (Init.GrowlQueue)\r
+ GrowlCommunicator.Notify("Queue Completed", "Put down that cocktail...\nyour Handbrake queue is done.");\r
+\r
+ // Do something whent he encode ends.\r
+ switch (Init.CompletionOption)\r
+ {\r
+ case "Shutdown":\r
+ Process.Start("Shutdown", "-s -t 60");\r
+ break;\r
+ case "Log Off":\r
+ Win32.ExitWindowsEx(0, 0);\r
+ break;\r
+ case "Suspend":\r
+ Application.SetSuspendState(PowerState.Suspend, true, true);\r
+ break;\r
+ case "Hibernate":\r
+ Application.SetSuspendState(PowerState.Hibernate, true, true);\r
+ break;\r
+ case "Lock System":\r
+ Win32.LockWorkStation();\r
+ break;\r
+ case "Quit HandBrake":\r
+ Application.Exit();\r
+ break;\r
+ default:\r
+ break;\r
+ }\r
+ }\r
}\r
}
\ No newline at end of file