2 This file is part of the HandBrake source code.
\r
3 Homepage: <http://handbrake.fr/>.
\r
4 It may be used under the terms of the GNU General Public License. */
\r
6 namespace HandBrake.ApplicationServices.Services
\r
9 using System.Diagnostics;
\r
10 using System.Windows.Forms;
\r
12 using HandBrake.ApplicationServices.EventArgs;
\r
13 using HandBrake.ApplicationServices.Functions;
\r
14 using HandBrake.ApplicationServices.Model;
\r
15 using HandBrake.ApplicationServices.Services.Interfaces;
\r
18 /// The HandBrake Queue
\r
20 public class QueueProcessor : IQueueProcessor
\r
23 /// Initializes a new instance of the <see cref="QueueProcessor"/> class.
\r
25 /// <param name="queueManager">
\r
26 /// The queue manager.
\r
28 /// <param name="encodeService">
\r
29 /// The encode Service.
\r
31 /// <exception cref="ArgumentNullException">
\r
33 public QueueProcessor(IQueueManager queueManager, IEncode encodeService)
\r
35 this.QueueManager = queueManager;
\r
36 this.EncodeService = encodeService;
\r
38 if (this.QueueManager == null)
\r
40 throw new ArgumentNullException("queueManager");
\r
43 if (this.QueueManager == null)
\r
45 throw new ArgumentNullException("queueManager");
\r
50 /// Initializes a new instance of the <see cref="QueueProcessor"/> class.
\r
51 /// This call also initializes the Encode and QueueManager services
\r
53 /// <param name="instanceId">
\r
54 /// The instance id.
\r
56 public QueueProcessor(int instanceId)
\r
58 this.EncodeService = new Encode();
\r
59 this.QueueManager = new QueueManager(instanceId);
\r
65 /// Queue Progess Status
\r
67 /// <param name="sender">
\r
70 /// <param name="e">
\r
71 /// The QueueProgressEventArgs.
\r
73 public delegate void QueueProgressStatus(object sender, QueueProgressEventArgs e);
\r
76 /// Fires when the Queue has started
\r
78 public event QueueProgressStatus JobProcessingStarted;
\r
81 /// Fires when a pause to the encode queue has been requested.
\r
83 public event EventHandler QueuePaused;
\r
86 /// Fires when the entire encode queue has completed.
\r
88 public event EventHandler QueueCompleted;
\r
91 /// Invoke the JobProcessingStarted event
\r
93 /// <param name="e">
\r
94 /// The QueueProgressEventArgs.
\r
96 private void InvokeJobProcessingStarted(QueueProgressEventArgs e)
\r
98 QueueProgressStatus handler = this.JobProcessingStarted;
\r
99 if (handler != null)
\r
106 /// Invoke the QueuePaused event
\r
108 /// <param name="e">
\r
111 private void InvokeQueuePaused(EventArgs e)
\r
113 EventHandler handler = this.QueuePaused;
\r
114 if (handler != null)
\r
121 /// Invoke the QueueCompleted event.
\r
123 /// <param name="e">
\r
126 private void InvokeQueueCompleted(EventArgs e)
\r
128 this.IsProcessing = false;
\r
130 EventHandler handler = this.QueueCompleted;
\r
131 if (handler != null)
\r
142 /// Gets a value indicating whether IsProcessing.
\r
144 public bool IsProcessing { get; private set; }
\r
147 /// Gets the IEncodeService instance.
\r
149 public IEncode EncodeService { get; private set; }
\r
152 /// Gets the IQueueManager instance.
\r
154 public IQueueManager QueueManager { get; private set; }
\r
159 /// Starts encoding the first job in the queue and continues encoding until all jobs
\r
160 /// have been encoded.
\r
162 public void Start()
\r
166 throw new Exception("Already Processing the Queue");
\r
169 IsProcessing = true;
\r
170 this.EncodeService.EncodeCompleted += this.EncodeServiceEncodeCompleted;
\r
171 this.ProcessNextJob();
\r
175 /// Requests a pause of the encode queue.
\r
177 public void Pause()
\r
179 this.EncodeService.EncodeCompleted -= this.EncodeServiceEncodeCompleted;
\r
180 this.InvokeQueuePaused(EventArgs.Empty);
\r
181 this.IsProcessing = false;
\r
185 /// After an encode is complete, move onto the next job.
\r
187 /// <param name="sender">
\r
190 /// <param name="e">
\r
191 /// The EncodeCompletedEventArgs.
\r
193 private void EncodeServiceEncodeCompleted(object sender, EncodeCompletedEventArgs e)
\r
196 if (Init.GrowlEncode)
\r
197 GrowlCommunicator.Notify("Encode Completed",
\r
198 "Put down that cocktail...\nyour Handbrake encode is done.");
\r
203 MessageBox.Show(e.Exception + e.ErrorInformation);
\r
206 // Handling Log Data
\r
207 this.EncodeService.ProcessLogs(this.QueueManager.LastProcessedJob.Destination);
\r
209 // Move onto the next job.
\r
210 this.ProcessNextJob();
\r
214 /// Run through all the jobs on the queue.
\r
216 private void ProcessNextJob()
\r
218 if (this.EncodeService.IsEncoding || !this.IsProcessing)
\r
220 // We don't want to try start a second encode, so just return out. The event will trigger the next encode automatically.
\r
221 // Also, we don't want to start a new encode if we are paused.
\r
225 QueueTask job = this.QueueManager.GetNextJobForProcessing();
\r
228 this.EncodeService.Start(job, true);
\r
229 this.InvokeJobProcessingStarted(new QueueProgressEventArgs(job));
\r
233 // No more jobs to process, so unsubscribe the event
\r
234 this.EncodeService.EncodeCompleted -= this.EncodeServiceEncodeCompleted;
\r
236 // Fire the event to tell connected services.
\r
237 this.InvokeQueueCompleted(EventArgs.Empty);
\r
239 // Run the After encode completeion work
\r
245 /// Perform an action after an encode. e.g a shutdown, standby, restart etc.
\r
247 private static void Finish()
\r
250 if (Init.GrowlQueue)
\r
251 GrowlCommunicator.Notify("Queue Completed", "Put down that cocktail...\nyour Handbrake queue is done.");
\r
253 // Do something whent he encode ends.
\r
254 switch (Init.CompletionOption)
\r
257 Process.Start("Shutdown", "-s -t 60");
\r
260 Win32.ExitWindowsEx(0, 0);
\r
263 Application.SetSuspendState(PowerState.Suspend, true, true);
\r
266 Application.SetSuspendState(PowerState.Hibernate, true, true);
\r
268 case "Lock System":
\r
269 Win32.LockWorkStation();
\r
271 case "Quit HandBrake":
\r
272 Application.Exit();
\r