OSDN Git Service

translation Controls and EncodeQueue
[handbrake-jp/handbrake-jp.git] / win / C# / EncodeQueue / EncodeAndQueueHandler.cs
1 /*  EncodeAndQueueHandler.cs $\r
2         \r
3            This file is part of the HandBrake source code.\r
4            Homepage: <http://handbrake.fr/>.\r
5            It may be used under the terms of the GNU General Public License. */\r
6 \r
7 using System;\r
8 using System.Collections.Generic;\r
9 using System.Collections.ObjectModel;\r
10 using System.Diagnostics;\r
11 using System.IO;\r
12 using System.Threading;\r
13 using System.Windows.Forms;\r
14 using System.Xml.Serialization;\r
15 using Handbrake.Functions;\r
16 \r
17 namespace Handbrake.EncodeQueue\r
18 {\r
19     public class EncodeAndQueueHandler\r
20     {\r
21         private static XmlSerializer serializer = new XmlSerializer(typeof(List<Job>));\r
22         private List<Job> queue = new List<Job>();\r
23         private int nextJobId;\r
24 \r
25         #region Event Handlers\r
26         /// <summary>\r
27         /// Fires when an encode job has been started.\r
28         /// </summary>\r
29         public event EventHandler NewJobStarted;\r
30 \r
31         /// <summary>\r
32         /// Fires when a pause to the encode queue has been requested.\r
33         /// </summary>\r
34         public event EventHandler QueuePauseRequested;\r
35 \r
36         /// <summary>\r
37         /// Fires when an encode job has been completed.\r
38         /// </summary>\r
39         public event EventHandler CurrentJobCompleted;\r
40 \r
41         /// <summary>\r
42         /// Fires when the entire encode queue has completed.\r
43         /// </summary>\r
44         public event EventHandler QueueCompleted;\r
45         #endregion\r
46 \r
47         #region Queue\r
48         /// <summary>\r
49         /// Gets and removes the next job in the queue.\r
50         /// </summary>\r
51         /// <returns>The job that was removed from the queue.</returns>\r
52         private Job GetNextJob()\r
53         {\r
54             Job job = queue[0];\r
55             LastEncode = job;\r
56             RemoveJob(0); // Remove the item which we are about to pass out.\r
57 \r
58             WriteQueueStateToFile("hb_queue_recovery.xml");\r
59 \r
60             return job;\r
61         }\r
62 \r
63         /// <summary>\r
64         /// Gets the current state of the encode queue.\r
65         /// </summary>\r
66         public ReadOnlyCollection<Job> CurrentQueue\r
67         {\r
68             get { return queue.AsReadOnly(); }\r
69         }\r
70 \r
71         /// <summary>\r
72         /// Gets the number of items in the queue.\r
73         /// </summary>\r
74         public int Count\r
75         {\r
76             get { return queue.Count; }\r
77         }\r
78 \r
79         /// <summary>\r
80         /// Adds an item to the queue.\r
81         /// </summary>\r
82         /// <param name="query">The query that will be passed to the HandBrake CLI.</param>\r
83         /// <param name="source">The location of the source video.</param>\r
84         /// <param name="destination">The location where the encoded video will be.</param>\r
85         /// <param name="customJob"></param>\r
86         public void AddJob(string query, string source, string destination, bool customJob)\r
87         {\r
88             Job newJob = new Job { Id = nextJobId++, Query = query, Source = source, Destination = destination, CustomQuery = customJob };\r
89 \r
90             queue.Add(newJob);\r
91             WriteQueueStateToFile("hb_queue_recovery.xml");\r
92         }\r
93 \r
94         /// <summary>\r
95         /// Removes an item from the queue.\r
96         /// </summary>\r
97         /// <param name="index">The zero-based location of the job in the queue.</param>\r
98         public void RemoveJob(int index)\r
99         {\r
100             queue.RemoveAt(index);\r
101             WriteQueueStateToFile("hb_queue_recovery.xml");\r
102         }\r
103 \r
104         /// <summary>\r
105         /// Moves an item up one position in the queue.\r
106         /// </summary>\r
107         /// <param name="index">The zero-based location of the job in the queue.</param>\r
108         public void MoveUp(int index)\r
109         {\r
110             if (index > 0)\r
111             {\r
112                 Job item = queue[index];\r
113 \r
114                 queue.RemoveAt(index);\r
115                 queue.Insert((index - 1), item);\r
116             }\r
117 \r
118             WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file\r
119         }\r
120 \r
121         /// <summary>\r
122         /// Moves an item down one position in the queue.\r
123         /// </summary>\r
124         /// <param name="index">The zero-based location of the job in the queue.</param>\r
125         public void MoveDown(int index)\r
126         {\r
127             if (index < queue.Count - 1)\r
128             {\r
129                 Job item = queue[index];\r
130 \r
131                 queue.RemoveAt(index);\r
132                 queue.Insert((index + 1), item);\r
133             }\r
134 \r
135             WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file\r
136         }\r
137 \r
138         /// <summary>\r
139         /// Writes the current state of the queue to a file.\r
140         /// </summary>\r
141         /// <param name="file">The location of the file to write the queue to.</param>\r
142         public void WriteQueueStateToFile(string file)\r
143         {\r
144             string tempPath = file == "hb_queue_recovery.xml" ? Path.Combine(Path.GetTempPath(), "hb_queue_recovery.xml") : file;\r
145 \r
146             try\r
147             {\r
148                 using (FileStream strm = new FileStream(tempPath, FileMode.Create, FileAccess.Write))\r
149                 {\r
150                     serializer.Serialize(strm, queue);\r
151                     strm.Close();\r
152                     strm.Dispose();\r
153                 }\r
154             }\r
155             catch (Exception)\r
156             {\r
157                 // Any Errors will be out of diskspace/permissions problems. \r
158                 // Don't report them as they'll annoy the user.\r
159             }\r
160         }\r
161 \r
162         /// <summary>\r
163         /// Writes the current state of the queue in the form of a batch (.bat) file.\r
164         /// </summary>\r
165         /// <param name="file">The location of the file to write the batch file to.</param>\r
166         public void WriteBatchScriptToFile(string file)\r
167         {\r
168             string queries = "";\r
169             foreach (Job queue_item in queue)\r
170             {\r
171                 string q_item = queue_item.Query;\r
172                 string fullQuery = '"' + Application.StartupPath + "\\HandBrakeCLI.exe" + '"' + q_item;\r
173 \r
174                 if (queries == string.Empty)\r
175                     queries = queries + fullQuery;\r
176                 else\r
177                     queries = queries + " && " + fullQuery;\r
178             }\r
179             string strCmdLine = queries;\r
180 \r
181             if (file != "")\r
182             {\r
183                 try\r
184                 {\r
185                     // Create a StreamWriter and open the file, Write the batch file query to the file and \r
186                     // Close the stream\r
187                     using (StreamWriter line = new StreamWriter(file))\r
188                     {\r
189                         line.WriteLine(strCmdLine);\r
190                     }\r
191 \r
192                     MessageBox.Show("バッチスクリプトを保存しました。", "Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);\r
193                 }\r
194                 catch (Exception)\r
195                 {\r
196                     MessageBox.Show("ファイルを保存できませんでした。書き込み可能なフォルダを指定しているか過帰任してください。", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
197                 }\r
198 \r
199             }\r
200         }\r
201 \r
202         /// <summary>\r
203         /// Reads a serialized XML file that represents a queue of encoding jobs.\r
204         /// </summary>\r
205         /// <param name="file">The location of the file to read the queue from.</param>\r
206         public void LoadQueueFromFile(string file)\r
207         {\r
208             string tempPath = file == "hb_queue_recovery.xml" ? Path.Combine(Path.GetTempPath(), "hb_queue_recovery.xml") : file;\r
209 \r
210             if (File.Exists(tempPath))\r
211             {\r
212                 using (FileStream strm = new FileStream(tempPath, FileMode.Open, FileAccess.Read))\r
213                 {\r
214                     if (strm.Length != 0)\r
215                     {\r
216                         List<Job> list = serializer.Deserialize(strm) as List<Job>;\r
217 \r
218                         if (list != null)\r
219                             foreach (Job item in list)\r
220                                 queue.Add(item);\r
221 \r
222                         if (file != "hb_queue_recovery.xml")\r
223                             WriteQueueStateToFile("hb_queue_recovery.xml");\r
224                     }\r
225                 }\r
226             }\r
227         }\r
228 \r
229         /// <summary>\r
230         /// Checks the current queue for an existing instance of the specified destination.\r
231         /// </summary>\r
232         /// <param name="destination">The destination of the encode.</param>\r
233         /// <returns>Whether or not the supplied destination is already in the queue.</returns>\r
234         public bool CheckForDestinationDuplicate(string destination)\r
235         {\r
236             foreach (Job checkItem in queue)\r
237             {\r
238                 if (checkItem.Destination.Contains(destination.Replace("\\\\", "\\")))\r
239                     return true;\r
240             }\r
241 \r
242             return false;\r
243         }\r
244 \r
245         #endregion\r
246 \r
247         #region Encoding\r
248 \r
249         /// <summary>\r
250         /// Gets the last encode that was processed.\r
251         /// </summary>\r
252         /// <returns></returns> \r
253         public Job LastEncode { get; set; }\r
254 \r
255         /// <summary>\r
256         /// Request Pause\r
257         /// </summary>\r
258         public Boolean PauseRequested { get; private set; }\r
259 \r
260         /// <summary>\r
261         /// Starts encoding the first job in the queue and continues encoding until all jobs\r
262         /// have been encoded.\r
263         /// </summary>\r
264         public void StartEncodeQueue()\r
265         {\r
266             if (this.Count != 0)\r
267             {\r
268                 if (PauseRequested)\r
269                     PauseRequested = false;\r
270                 else\r
271                 {\r
272                     PauseRequested = false;\r
273                     try\r
274                     {\r
275                         Thread theQueue = new Thread(startProcess) { IsBackground = true };\r
276                         theQueue.Start();\r
277                     }\r
278                     catch (Exception exc)\r
279                     {\r
280                         MessageBox.Show(exc.ToString());\r
281                     }\r
282                 }\r
283             }\r
284         }\r
285 \r
286         /// <summary>\r
287         /// Requests a pause of the encode queue.\r
288         /// </summary>\r
289         public void RequestPause()\r
290         {\r
291             PauseRequested = true;\r
292 \r
293             if (QueuePauseRequested != null)\r
294                 QueuePauseRequested(this, new EventArgs());\r
295         }\r
296 \r
297         /// <summary>\r
298         /// Stops the current job.\r
299         /// </summary>\r
300         public void EndEncodeJob()\r
301         {\r
302             CloseCLI();\r
303         }\r
304 \r
305         private void startProcess(object state)\r
306         {\r
307             // Run through each item on the queue\r
308             while (this.Count != 0)\r
309             {\r
310                 Job encJob = GetNextJob();\r
311                 string query = encJob.Query;\r
312                 WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file\r
313 \r
314                 RunCli(query);\r
315 \r
316                 if (NewJobStarted != null)\r
317                     NewJobStarted(this, new EventArgs());\r
318 \r
319                 hbProcess.WaitForExit();\r
320 \r
321                 AddCLIQueryToLog(encJob);\r
322                 CopyLog(LastEncode.Destination);\r
323 \r
324                 hbProcess.Close();\r
325                 hbProcess.Dispose();\r
326                 \r
327                 isEncoding = false;\r
328 \r
329                 //Growl\r
330                 if (Properties.Settings.Default.growlEncode)\r
331                     GrowlCommunicator.Notify("変換完了", "Handbrakeの変換作業が完了しました。");\r
332 \r
333                 if (CurrentJobCompleted != null)\r
334                     CurrentJobCompleted(this, new EventArgs());\r
335 \r
336                 while (PauseRequested) // Need to find a better way of doing this.\r
337                 {\r
338                     Thread.Sleep(5000);\r
339                 }\r
340             }\r
341             LastEncode = new Job();\r
342 \r
343             if (QueueCompleted != null)\r
344                 QueueCompleted(this, new EventArgs());\r
345 \r
346             // After the encode is done, we may want to shutdown, suspend etc.\r
347             AfterEncodeAction();\r
348         }\r
349 \r
350         #endregion\r
351 \r
352         #region CLI and Log Handling\r
353         public Process hbProcess { get; set; }\r
354         public int processID { get; set; }\r
355         public IntPtr processHandle { get; set; }\r
356         public String currentQuery { get; set; }\r
357         public Boolean isEncoding { get; set; }\r
358 \r
359         /// <summary>\r
360         /// Execute a HandBrakeCLI process.\r
361         /// </summary>\r
362         /// <param name="query">The CLI Query</param>\r
363         public void RunCli(string query)\r
364         {\r
365             try\r
366             {\r
367                 isEncoding = true;\r
368 \r
369                 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
370                 string logPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs", "last_encode_log.txt");\r
371                 string strCmdLine = String.Format(@" /C """"{0}"" {1} 2>""{2}"" """, handbrakeCLIPath, query, logPath);\r
372                 ProcessStartInfo cliStart = new ProcessStartInfo("CMD.exe", strCmdLine);\r
373 \r
374                 if (Properties.Settings.Default.enocdeStatusInGui)\r
375                 {\r
376                     cliStart.RedirectStandardOutput = true;\r
377                     cliStart.UseShellExecute = false;\r
378                 }\r
379                 if (Properties.Settings.Default.cli_minimized)\r
380                     cliStart.WindowStyle = ProcessWindowStyle.Minimized;\r
381 \r
382                 Process[] before = Process.GetProcesses(); // Get a list of running processes before starting.\r
383                 hbProcess = Process.Start(cliStart);\r
384                 processID = Main.getCliProcess(before); \r
385                 currentQuery = query;\r
386                 if (hbProcess != null)\r
387                     processHandle = hbProcess.MainWindowHandle; // Set the process Handle\r
388 \r
389                 // Set the process Priority\r
390                 Process hbCliProcess = null;\r
391                 if (processID != -1)\r
392                     hbCliProcess = Process.GetProcessById(processID);\r
393 \r
394                 if (hbCliProcess != null)\r
395                     switch (Properties.Settings.Default.processPriority)\r
396                     {\r
397                         case "Realtime":\r
398                             hbCliProcess.PriorityClass = ProcessPriorityClass.RealTime;\r
399                             break;\r
400                         case "High":\r
401                             hbCliProcess.PriorityClass = ProcessPriorityClass.High;\r
402                             break;\r
403                         case "Above Normal":\r
404                             hbCliProcess.PriorityClass = ProcessPriorityClass.AboveNormal;\r
405                             break;\r
406                         case "Normal":\r
407                             hbCliProcess.PriorityClass = ProcessPriorityClass.Normal;\r
408                             break;\r
409                         case "Low":\r
410                             hbCliProcess.PriorityClass = ProcessPriorityClass.Idle;\r
411                             break;\r
412                         default:\r
413                             hbCliProcess.PriorityClass = ProcessPriorityClass.BelowNormal;\r
414                             break;\r
415                     }\r
416             }\r
417             catch (Exception exc)\r
418             {\r
419                 MessageBox.Show("HandBrakeCLI.exeの起動に失敗しました。詳細については履歴ログを確認してください。\n\n   Detailed Error Information: error occured in runCli()\n\n" + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
420             }\r
421         }\r
422 \r
423         /// <summary>\r
424         /// Kill the CLI process\r
425         /// </summary>\r
426         private void CloseCLI()\r
427         {\r
428             hbProcess.Kill();\r
429             isEncoding = false;\r
430         }\r
431 \r
432         /// <summary>\r
433         /// Perform an action after an encode. e.g a shutdown, standby, restart etc.\r
434         /// </summary>\r
435         private void AfterEncodeAction()\r
436         {\r
437             isEncoding = false;\r
438             currentQuery = String.Empty;\r
439 \r
440             //Growl\r
441             if (Properties.Settings.Default.growlQueue)\r
442                 GrowlCommunicator.Notify("変換キューの完了", "Handbrake変換キュー内の変換作業がすべて完了しました。");\r
443 \r
444             // Do something whent he encode ends.\r
445             switch (Properties.Settings.Default.CompletionOption)\r
446             {\r
447                 case "Shutdown":\r
448                     Process.Start("Shutdown", "-s -t 60");\r
449                     break;\r
450                 case "Log Off":\r
451                     Win32.ExitWindowsEx(0, 0);\r
452                     break;\r
453                 case "Suspend":\r
454                     Application.SetSuspendState(PowerState.Suspend, true, true);\r
455                     break;\r
456                 case "Hibernate":\r
457                     Application.SetSuspendState(PowerState.Hibernate, true, true);\r
458                     break;\r
459                 case "Lock System":\r
460                     Win32.LockWorkStation();\r
461                     break;\r
462                 case "Quit HandBrake":\r
463                     Application.Exit();\r
464                     break;\r
465                 default:\r
466                     break;\r
467             }\r
468         }\r
469 \r
470         /// <summar>\r
471         /// Append the CLI query to the start of the log file.\r
472         /// </summary>\r
473         /// <param name="query"></param>\r
474         private static void AddCLIQueryToLog(Job encJob)\r
475         {\r
476             string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
477             string logPath = Path.Combine(logDir, "last_encode_log.txt");\r
478 \r
479             StreamReader reader = new StreamReader(File.Open(logPath, FileMode.Open, FileAccess.Read, FileShare.Read));\r
480             String log = reader.ReadToEnd();\r
481             reader.Close();\r
482 \r
483             StreamWriter writer = new StreamWriter(File.Create(logPath));\r
484 \r
485             writer.Write("### CLI Query: " + encJob.Query + "\n\n");\r
486             writer.Write("### User Query: " + encJob.CustomQuery + "\n\n");\r
487             writer.Write("#########################################\n\n");\r
488             writer.WriteLine(log);\r
489             writer.Flush();\r
490             writer.Close();\r
491         }\r
492 \r
493         /// <summary>\r
494         /// Save a copy of the log to the users desired location or a default location\r
495         /// if this feature is enabled in options.\r
496         /// </summary>\r
497         /// <param name="destination"></param>\r
498         private static void CopyLog(string destination)\r
499         {\r
500             try\r
501             {\r
502                 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
503                 string tempLogFile = Path.Combine(logDir, "last_encode_log.txt");\r
504 \r
505                 string encodeDestinationPath = Path.GetDirectoryName(destination);\r
506                 String destinationFile = Path.GetFileName(destination);\r
507                 string encodeLogFile = destinationFile + " " + DateTime.Now.ToString().Replace("/", "-").Replace(":", "-") + ".txt";\r
508 \r
509                 // Make sure the log directory exists.\r
510                 if (!Directory.Exists(logDir))\r
511                     Directory.CreateDirectory(logDir);\r
512 \r
513                 // Copy the Log to HandBrakes log folder in the users applciation data folder.\r
514                 File.Copy(tempLogFile, Path.Combine(logDir, encodeLogFile));\r
515 \r
516                 // Save a copy of the log file in the same location as the enocde.\r
517                 if (Properties.Settings.Default.saveLogWithVideo)\r
518                     File.Copy(tempLogFile, Path.Combine(encodeDestinationPath, encodeLogFile));\r
519 \r
520                 // Save a copy of the log file to a user specified location\r
521                 if (Directory.Exists(Properties.Settings.Default.saveLogPath))\r
522                     if (Properties.Settings.Default.saveLogPath != String.Empty && Properties.Settings.Default.saveLogToSpecifiedPath)\r
523                         File.Copy(tempLogFile, Path.Combine(Properties.Settings.Default.saveLogPath, encodeLogFile));\r
524             }\r
525             catch (Exception exc)\r
526             {\r
527                 MessageBox.Show("ログファイルのコピー中に問題が発生しました。\nError Information:\n\n" + exc, "Error",\r
528                                 MessageBoxButtons.OK, MessageBoxIcon.Error);\r
529             }\r
530         }\r
531         #endregion\r
532     }\r
533 }