1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
/* 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.Diagnostics;
using System.IO;
using System.Windows.Forms;
using HandBrake.ApplicationServices.EventArgs;
using HandBrake.ApplicationServices.Exceptions;
using HandBrake.ApplicationServices.Functions;
using HandBrake.ApplicationServices.Model;
using HandBrake.ApplicationServices.Services.Interfaces;
/// <summary>
/// The HandBrake Queue
/// </summary>
public class QueueProcessor : IQueueProcessor
{
/// <summary>
/// The User Setting Service
/// </summary>
private static IUserSettingService userSettingService = ServiceManager.UserSettingService;
/// <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">
/// Services are not setup
/// </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 = File.Exists("hb.dll") ? (IEncode)new LibEncode() : 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.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)
{
this.QueueManager.LastProcessedJob.Status = QueueItemStatus.Completed;
// Clear the completed item of the queue if the setting is set.
if (userSettingService.GetUserSetting<bool>(ASUserSettingConstants.ClearCompletedFromQueue))
{
this.QueueManager.ClearCompleted();
}
// Growl
if (userSettingService.GetUserSetting<bool>(ASUserSettingConstants.GrowlEncode))
GrowlCommunicator.Notify("Encode Completed",
"Put down that cocktail...\nyour Handbrake encode is done.");
if (!e.Successful)
{
this.QueueManager.LastProcessedJob.Status = QueueItemStatus.Error;
this.Pause();
if (e.Exception.GetType() == typeof(GeneralApplicationException))
{
throw e.Exception;
}
throw new GeneralApplicationException(e.ErrorInformation, e.Exception.Message, e.Exception);
}
// Handling Log Data
this.EncodeService.ProcessLogs(this.QueueManager.LastProcessedJob.Task.Destination);
// Post-Processing
if (e.Successful)
{
SendToApplication(this.QueueManager.LastProcessedJob.Task.Destination);
}
// Move onto the next job.
if (this.IsProcessing)
{
this.ProcessNextJob();
}
else
{
this.EncodeService.EncodeCompleted -= this.EncodeServiceEncodeCompleted;
this.InvokeQueueCompleted(EventArgs.Empty);
this.QueueManager.BackupQueue(string.Empty);
}
}
/// <summary>
/// Run through all the jobs on the queue.
/// </summary>
private void ProcessNextJob()
{
if (this.EncodeService.IsEncoding || !this.IsProcessing)
{
// We don't want to try start a second encode, so just return out. The event will trigger the next encode automatically.
// Also, we don't want to start a new encode if we are paused.
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>
/// Send a file to a 3rd party application after encoding has completed.
/// </summary>
/// <param name="file"> The file path</param>
private static void SendToApplication(string file)
{
if (userSettingService.GetUserSetting<bool>(ASUserSettingConstants.SendFile) && !string.IsNullOrEmpty(userSettingService.GetUserSetting<string>(ASUserSettingConstants.SendFileTo)))
{
string args = string.Format("{0} \"{1}\"", userSettingService.GetUserSetting<string>(ASUserSettingConstants.SendFileToArgs), file);
ProcessStartInfo vlc = new ProcessStartInfo(userSettingService.GetUserSetting<string>(ASUserSettingConstants.SendFileTo), args);
Process.Start(vlc);
}
}
/// <summary>
/// Perform an action after an encode. e.g a shutdown, standby, restart etc.
/// </summary>
private static void Finish()
{
// Growl
if (userSettingService.GetUserSetting<bool>(ASUserSettingConstants.GrowlQueue))
{
GrowlCommunicator.Notify("Queue Completed", "Put down that cocktail...\nyour Handbrake queue is done.");
}
// Do something whent he encode ends.
switch (userSettingService.GetUserSetting<string>(ASUserSettingConstants.WhenCompleteAction))
{
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;
}
}
}
}
|