OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / EncodeQueue / Encode.cs
1 /*  Encode.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.Diagnostics;\r
9 using System.IO;\r
10 using System.Windows.Forms;\r
11 using Handbrake.Functions;\r
12 \r
13 namespace Handbrake.EncodeQueue\r
14 {\r
15     public class Encode\r
16     {\r
17         public Process HbProcess { get; set; }\r
18         public int ProcessID { get; set; }\r
19         public IntPtr ProcessHandle { get; set; }\r
20         public String CurrentQuery { get; set; }\r
21         public Boolean IsEncoding { get; set; }\r
22 \r
23         public event EventHandler EncodeStarted;\r
24         public event EventHandler EncodeEnded;\r
25 \r
26         /// <summary>\r
27         /// Create a preview sample video\r
28         /// </summary>\r
29         /// <param name="query"></param>\r
30         public void CreatePreviewSampe(string query)\r
31         {\r
32             Run(query);\r
33         }\r
34 \r
35         /// <summary>\r
36         /// Execute a HandBrakeCLI process.\r
37         /// </summary>\r
38         /// <param name="query">The CLI Query</param>\r
39         protected void Run(string query)\r
40         {\r
41             try\r
42             {\r
43                 if (EncodeStarted != null)\r
44                     EncodeStarted(this, new EventArgs());\r
45 \r
46                 IsEncoding = true;\r
47 \r
48                 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
49                 string logPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs", "last_encode_log.txt");\r
50                 string strCmdLine = String.Format(@" /C """"{0}"" {1} 2>""{2}"" """, handbrakeCLIPath, query, logPath);\r
51                 ProcessStartInfo cliStart = new ProcessStartInfo("CMD.exe", strCmdLine);\r
52 \r
53                 if (Properties.Settings.Default.enocdeStatusInGui)\r
54                 {\r
55                     cliStart.RedirectStandardOutput = true;\r
56                     cliStart.UseShellExecute = false;\r
57                 }\r
58                 if (Properties.Settings.Default.cli_minimized)\r
59                     cliStart.WindowStyle = ProcessWindowStyle.Minimized;\r
60 \r
61                 Process[] before = Process.GetProcesses(); // Get a list of running processes before starting.\r
62                 HbProcess = Process.Start(cliStart);\r
63                 ProcessID = Main.GetCliProcess(before);\r
64                 CurrentQuery = query;\r
65                 if (HbProcess != null)\r
66                     ProcessHandle = HbProcess.MainWindowHandle; // Set the process Handle\r
67 \r
68                 // Set the process Priority\r
69                 Process hbCliProcess = null;\r
70                 if (ProcessID != -1)\r
71                     hbCliProcess = Process.GetProcessById(ProcessID);\r
72 \r
73                 if (hbCliProcess != null)\r
74                     switch (Properties.Settings.Default.processPriority)\r
75                     {\r
76                         case "Realtime":\r
77                             hbCliProcess.PriorityClass = ProcessPriorityClass.RealTime;\r
78                             break;\r
79                         case "High":\r
80                             hbCliProcess.PriorityClass = ProcessPriorityClass.High;\r
81                             break;\r
82                         case "Above Normal":\r
83                             hbCliProcess.PriorityClass = ProcessPriorityClass.AboveNormal;\r
84                             break;\r
85                         case "Normal":\r
86                             hbCliProcess.PriorityClass = ProcessPriorityClass.Normal;\r
87                             break;\r
88                         case "Low":\r
89                             hbCliProcess.PriorityClass = ProcessPriorityClass.Idle;\r
90                             break;\r
91                         default:\r
92                             hbCliProcess.PriorityClass = ProcessPriorityClass.BelowNormal;\r
93                             break;\r
94                     }\r
95             }\r
96             catch (Exception exc)\r
97             {\r
98                 MessageBox.Show("It would appear that HandBrakeCLI has not started correctly. You should take a look at the Activity log as it may indicate the reason why.\n\n   Detailed Error Information: error occured in runCli()\n\n" + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
99             }\r
100         }\r
101 \r
102         /// <summary>\r
103         /// Kill the CLI process\r
104         /// </summary>\r
105         protected void Stop()\r
106         {\r
107             if (EncodeEnded != null)\r
108                 EncodeEnded(this, new EventArgs());\r
109 \r
110             if (HbProcess != null)\r
111                 HbProcess.Kill();\r
112             IsEncoding = false;\r
113         }\r
114 \r
115         /// <summary>\r
116         /// Perform an action after an encode. e.g a shutdown, standby, restart etc.\r
117         /// </summary>\r
118         protected void Finish()\r
119         {\r
120             IsEncoding = false;\r
121             CurrentQuery = String.Empty;\r
122 \r
123             //Growl\r
124             if (Properties.Settings.Default.growlQueue)\r
125                 GrowlCommunicator.Notify("Queue Completed", "Put down that cocktail...\nyour Handbrake queue is done.");\r
126 \r
127             // Do something whent he encode ends.\r
128             switch (Properties.Settings.Default.CompletionOption)\r
129             {\r
130                 case "Shutdown":\r
131                     Process.Start("Shutdown", "-s -t 60");\r
132                     break;\r
133                 case "Log Off":\r
134                     Win32.ExitWindowsEx(0, 0);\r
135                     break;\r
136                 case "Suspend":\r
137                     Application.SetSuspendState(PowerState.Suspend, true, true);\r
138                     break;\r
139                 case "Hibernate":\r
140                     Application.SetSuspendState(PowerState.Hibernate, true, true);\r
141                     break;\r
142                 case "Lock System":\r
143                     Win32.LockWorkStation();\r
144                     break;\r
145                 case "Quit HandBrake":\r
146                     Application.Exit();\r
147                     break;\r
148                 default:\r
149                     break;\r
150             }\r
151         }\r
152 \r
153         /// <summary>\r
154         /// Add the CLI Query to the Log File.\r
155         /// </summary>\r
156         /// <param name="encJob"></param>\r
157         protected void AddCLIQueryToLog(Job encJob)\r
158         {\r
159             string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
160             string logPath = Path.Combine(logDir, "last_encode_log.txt");\r
161 \r
162             StreamReader reader = new StreamReader(File.Open(logPath, FileMode.Open, FileAccess.Read, FileShare.Read));\r
163             String log = reader.ReadToEnd();\r
164             reader.Close();\r
165 \r
166             StreamWriter writer = new StreamWriter(File.Create(logPath));\r
167 \r
168             writer.Write("### CLI Query: " + encJob.Query + "\n\n");\r
169             writer.Write("### User Query: " + encJob.CustomQuery + "\n\n");\r
170             writer.Write("#########################################\n\n");\r
171             writer.WriteLine(log);\r
172             writer.Flush();\r
173             writer.Close();\r
174         }\r
175 \r
176         /// <summary>\r
177         /// Save a copy of the log to the users desired location or a default location\r
178         /// if this feature is enabled in options.\r
179         /// </summary>\r
180         /// <param name="destination"></param>\r
181         protected void CopyLog(string destination)\r
182         {\r
183             try\r
184             {\r
185                 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
186                 string tempLogFile = Path.Combine(logDir, "last_encode_log.txt");\r
187 \r
188                 string encodeDestinationPath = Path.GetDirectoryName(destination);\r
189                 String destinationFile = Path.GetFileName(destination);\r
190                 string encodeLogFile = destinationFile + " " + DateTime.Now.ToString().Replace("/", "-").Replace(":", "-") + ".txt";\r
191 \r
192                 // Make sure the log directory exists.\r
193                 if (!Directory.Exists(logDir))\r
194                     Directory.CreateDirectory(logDir);\r
195 \r
196                 // Copy the Log to HandBrakes log folder in the users applciation data folder.\r
197                 File.Copy(tempLogFile, Path.Combine(logDir, encodeLogFile));\r
198 \r
199                 // Save a copy of the log file in the same location as the enocde.\r
200                 if (Properties.Settings.Default.saveLogWithVideo)\r
201                     File.Copy(tempLogFile, Path.Combine(encodeDestinationPath, encodeLogFile));\r
202 \r
203                 // Save a copy of the log file to a user specified location\r
204                 if (Directory.Exists(Properties.Settings.Default.saveLogPath))\r
205                     if (Properties.Settings.Default.saveLogPath != String.Empty && Properties.Settings.Default.saveLogToSpecifiedPath)\r
206                         File.Copy(tempLogFile, Path.Combine(Properties.Settings.Default.saveLogPath, encodeLogFile));\r
207             }\r
208             catch (Exception exc)\r
209             {\r
210                 MessageBox.Show("Something went a bit wrong trying to copy your log file.\nError Information:\n\n" + exc, "Error",\r
211                                 MessageBoxButtons.OK, MessageBoxIcon.Error);\r
212             }\r
213         }\r
214     }\r
215 }