OSDN Git Service

ff865e2ee42864899c8c16e9b6d6cd9fe03ea07a
[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             // Handling Log Data \r
201             this.EncodeService.ProcessLogs(this.QueueManager.LastProcessedJob.Destination);\r
202 \r
203             // Move onto the next job.\r
204             this.ProcessNextJob();\r
205         }\r
206 \r
207         /// <summary>\r
208         /// Run through all the jobs on the queue.\r
209         /// </summary>\r
210         private void ProcessNextJob()\r
211         {\r
212             if (this.EncodeService.IsEncoding)\r
213             {\r
214                 // We don't want to try start a second encode, so just return out. The event will trigger the next encode automatically.\r
215                 return;\r
216             }\r
217 \r
218             QueueTask job = this.QueueManager.GetNextJobForProcessing();\r
219             if (job != null)\r
220             {\r
221                 this.EncodeService.Start(job, true);\r
222                 this.InvokeJobProcessingStarted(new QueueProgressEventArgs(job));\r
223             }\r
224             else\r
225             {\r
226                 // No more jobs to process, so unsubscribe the event\r
227                 this.EncodeService.EncodeCompleted -= this.EncodeServiceEncodeCompleted;\r
228 \r
229                 // Fire the event to tell connected services.\r
230                 this.InvokeQueueCompleted(EventArgs.Empty);\r
231 \r
232                 // Run the After encode completeion work\r
233                 Finish();\r
234             }\r
235         }\r
236 \r
237         /// <summary>\r
238         /// Perform an action after an encode. e.g a shutdown, standby, restart etc.\r
239         /// </summary>\r
240         private static void Finish()\r
241         {\r
242             // Growl\r
243             if (Init.GrowlQueue)\r
244                 GrowlCommunicator.Notify("Queue Completed", "Put down that cocktail...\nyour Handbrake queue is done.");\r
245 \r
246             // Do something whent he encode ends.\r
247             switch (Init.CompletionOption)\r
248             {\r
249                 case "Shutdown":\r
250                     Process.Start("Shutdown", "-s -t 60");\r
251                     break;\r
252                 case "Log off":\r
253                     Win32.ExitWindowsEx(0, 0);\r
254                     break;\r
255                 case "Suspend":\r
256                     Application.SetSuspendState(PowerState.Suspend, true, true);\r
257                     break;\r
258                 case "Hibernate":\r
259                     Application.SetSuspendState(PowerState.Hibernate, true, true);\r
260                     break;\r
261                 case "Lock System":\r
262                     Win32.LockWorkStation();\r
263                     break;\r
264                 case "Quit HandBrake":\r
265                     Application.Exit();\r
266                     break;\r
267                 default:\r
268                     break;\r
269             }\r
270         }\r
271     }\r
272 }