OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / HandBrake.ApplicationServices / Services / QueueProcessor.cs
1 /*  Queue.cs $\r
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
5 \r
6 namespace HandBrake.ApplicationServices.Services\r
7 {\r
8     using System;\r
9     using System.Diagnostics;\r
10     using System.Windows.Forms;\r
11 \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
16 \r
17     /// <summary>\r
18     /// The HandBrake Queue\r
19     /// </summary>\r
20     public class QueueProcessor : IQueueProcessor\r
21     {\r
22         /// <summary>\r
23         /// Initializes a new instance of the <see cref="QueueProcessor"/> class.\r
24         /// </summary>\r
25         /// <param name="queueManager">\r
26         /// The queue manager.\r
27         /// </param>\r
28         /// <param name="encodeService">\r
29         /// The encode Service.\r
30         /// </param>\r
31         /// <exception cref="ArgumentNullException">\r
32         /// </exception>\r
33         public QueueProcessor(IQueueManager queueManager, IEncode encodeService)\r
34         {\r
35             this.QueueManager = queueManager;\r
36             this.EncodeService = encodeService;\r
37 \r
38             if (this.QueueManager == null)\r
39             {\r
40                 throw new ArgumentNullException("queueManager");\r
41             }\r
42 \r
43             if (this.QueueManager == null)\r
44             {\r
45                 throw new ArgumentNullException("queueManager");\r
46             }\r
47         }\r
48 \r
49         /// <summary>\r
50         /// Initializes a new instance of the <see cref="QueueProcessor"/> class.\r
51         /// This call also initializes the Encode and QueueManager services\r
52         /// </summary>\r
53         /// <param name="instanceId">\r
54         /// The instance id.\r
55         /// </param>\r
56         public QueueProcessor(int instanceId)\r
57         {\r
58             this.EncodeService = new Encode();\r
59             this.QueueManager = new QueueManager(instanceId);\r
60         }\r
61 \r
62         #region Events\r
63 \r
64         /// <summary>\r
65         /// Queue Progess Status\r
66         /// </summary>\r
67         /// <param name="sender">\r
68         /// The sender.\r
69         /// </param>\r
70         /// <param name="e">\r
71         /// The QueueProgressEventArgs.\r
72         /// </param>\r
73         public delegate void QueueProgressStatus(object sender, QueueProgressEventArgs e);\r
74 \r
75         /// <summary>\r
76         /// Fires when the Queue has started\r
77         /// </summary>\r
78         public event QueueProgressStatus JobProcessingStarted;\r
79 \r
80         /// <summary>\r
81         /// Fires when a pause to the encode queue has been requested.\r
82         /// </summary>\r
83         public event EventHandler QueuePaused;\r
84 \r
85         /// <summary>\r
86         /// Fires when the entire encode queue has completed.\r
87         /// </summary>\r
88         public event EventHandler QueueCompleted;\r
89 \r
90         /// <summary>\r
91         /// Invoke the JobProcessingStarted event\r
92         /// </summary>\r
93         /// <param name="e">\r
94         /// The QueueProgressEventArgs.\r
95         /// </param>\r
96         private void InvokeJobProcessingStarted(QueueProgressEventArgs e)\r
97         {\r
98             QueueProgressStatus handler = this.JobProcessingStarted;\r
99             if (handler != null)\r
100             {\r
101                 handler(this, e);\r
102             }\r
103         }\r
104 \r
105         /// <summary>\r
106         /// Invoke the QueuePaused event\r
107         /// </summary>\r
108         /// <param name="e">\r
109         /// The EventArgs.\r
110         /// </param>\r
111         private void InvokeQueuePaused(EventArgs e)\r
112         {\r
113             EventHandler handler = this.QueuePaused;\r
114             if (handler != null)\r
115             {\r
116                 handler(this, e);\r
117             }\r
118         }\r
119 \r
120         /// <summary>\r
121         /// Invoke the QueueCompleted event.\r
122         /// </summary>\r
123         /// <param name="e">\r
124         /// The EventArgs.\r
125         /// </param>\r
126         private void InvokeQueueCompleted(EventArgs e)\r
127         {\r
128             this.IsProcessing = false;\r
129 \r
130             EventHandler handler = this.QueueCompleted;\r
131             if (handler != null)\r
132             {\r
133                 handler(this, e);\r
134             }\r
135         }\r
136 \r
137         #endregion\r
138 \r
139         #region Properties\r
140 \r
141         /// <summary>\r
142         /// Gets a value indicating whether IsProcessing.\r
143         /// </summary>\r
144         public bool IsProcessing { get; private set; }\r
145 \r
146         /// <summary>\r
147         /// Gets the IEncodeService instance.\r
148         /// </summary>\r
149         public IEncode EncodeService { get; private set; }\r
150 \r
151         /// <summary>\r
152         /// Gets the IQueueManager instance.\r
153         /// </summary>\r
154         public IQueueManager QueueManager { get; private set; }\r
155 \r
156         #endregion\r
157 \r
158         /// <summary>\r
159         /// Starts encoding the first job in the queue and continues encoding until all jobs\r
160         /// have been encoded.\r
161         /// </summary>\r
162         public void Start()\r
163         {\r
164             if (IsProcessing)\r
165             {\r
166                 throw new Exception("Already Processing the Queue");\r
167             }\r
168 \r
169             IsProcessing = true;\r
170             this.EncodeService.EncodeCompleted += this.EncodeServiceEncodeCompleted;\r
171             this.ProcessNextJob();\r
172         }\r
173 \r
174         /// <summary>\r
175         /// Requests a pause of the encode queue.\r
176         /// </summary>\r
177         public void Pause()\r
178         {\r
179             this.EncodeService.EncodeCompleted -= this.EncodeServiceEncodeCompleted;\r
180             this.InvokeQueuePaused(EventArgs.Empty);\r
181             this.IsProcessing = false;\r
182         }\r
183 \r
184         /// <summary>\r
185         /// After an encode is complete, move onto the next job.\r
186         /// </summary>\r
187         /// <param name="sender">\r
188         /// The sender.\r
189         /// </param>\r
190         /// <param name="e">\r
191         /// The EncodeCompletedEventArgs.\r
192         /// </param>\r
193         private void EncodeServiceEncodeCompleted(object sender, EncodeCompletedEventArgs e)\r
194         {\r
195             // Growl\r
196             if (Init.GrowlEncode)\r
197                 GrowlCommunicator.Notify("Encode Completed",\r
198                                          "Put down that cocktail...\nyour Handbrake encode is done.");\r
199 \r
200             if (!e.Successful)\r
201             {\r
202                 this.Pause();\r
203                 MessageBox.Show(e.Exception + e.ErrorInformation);\r
204             }\r
205 \r
206             // Handling Log Data \r
207             this.EncodeService.ProcessLogs(this.QueueManager.LastProcessedJob.Destination);\r
208 \r
209             // Move onto the next job.\r
210             this.ProcessNextJob();\r
211         }\r
212 \r
213         /// <summary>\r
214         /// Run through all the jobs on the queue.\r
215         /// </summary>\r
216         private void ProcessNextJob()\r
217         {\r
218             if (this.EncodeService.IsEncoding || !this.IsProcessing)\r
219             {\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
222                 return;\r
223             }\r
224 \r
225             QueueTask job = this.QueueManager.GetNextJobForProcessing();\r
226             if (job != null)\r
227             {\r
228                 this.EncodeService.Start(job, true);\r
229                 this.InvokeJobProcessingStarted(new QueueProgressEventArgs(job));\r
230             }\r
231             else\r
232             {\r
233                 // No more jobs to process, so unsubscribe the event\r
234                 this.EncodeService.EncodeCompleted -= this.EncodeServiceEncodeCompleted;\r
235 \r
236                 // Fire the event to tell connected services.\r
237                 this.InvokeQueueCompleted(EventArgs.Empty);\r
238 \r
239                 // Run the After encode completeion work\r
240                 Finish();\r
241             }\r
242         }\r
243 \r
244         /// <summary>\r
245         /// Perform an action after an encode. e.g a shutdown, standby, restart etc.\r
246         /// </summary>\r
247         private static void Finish()\r
248         {\r
249             // Growl\r
250             if (Init.GrowlQueue)\r
251                 GrowlCommunicator.Notify("Queue Completed", "Put down that cocktail...\nyour Handbrake queue is done.");\r
252 \r
253             // Do something whent he encode ends.\r
254             switch (Init.CompletionOption)\r
255             {\r
256                 case "Shutdown":\r
257                     Process.Start("Shutdown", "-s -t 60");\r
258                     break;\r
259                 case "Log off":\r
260                     Win32.ExitWindowsEx(0, 0);\r
261                     break;\r
262                 case "Suspend":\r
263                     Application.SetSuspendState(PowerState.Suspend, true, true);\r
264                     break;\r
265                 case "Hibernate":\r
266                     Application.SetSuspendState(PowerState.Hibernate, true, true);\r
267                     break;\r
268                 case "Lock System":\r
269                     Win32.LockWorkStation();\r
270                     break;\r
271                 case "Quit HandBrake":\r
272                     Application.Exit();\r
273                     break;\r
274                 default:\r
275                     break;\r
276             }\r
277         }\r
278     }\r
279 }