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.Windows.Forms;\r
10 using System.IO;\r
11 using System.Runtime.InteropServices;\r
12 using Handbrake.Functions;\r
13 \r
14 namespace Handbrake.EncodeQueue\r
15 {\r
16     public class Encode\r
17     {\r
18         // DLL Imports\r
19         [DllImport("user32.dll")]\r
20         private static extern void LockWorkStation();\r
21         [DllImport("user32.dll")]\r
22         private static extern int ExitWindowsEx(int uFlags, int dwReason);\r
23  \r
24         /// <summary>\r
25         /// Execute a HandBrakeCLI process.\r
26         /// </summary>\r
27         /// <param name="query">The CLI Query</param>\r
28         public EncodeProcess runCli(string query)\r
29         {\r
30             EncodeProcess currentEncode = new EncodeProcess();\r
31             try\r
32             {\r
33                 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
34                 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
35                 string logPath = Path.Combine(logDir, "last_encode_log.txt");\r
36                 string strCmdLine = String.Format(@" CMD /c """"{0}"" {1} 2>""{2}"" """, handbrakeCLIPath, query, logPath);\r
37 \r
38                 ProcessStartInfo cliStart = new ProcessStartInfo("CMD.exe", strCmdLine);\r
39                 if (Properties.Settings.Default.enocdeStatusInGui == "Checked")\r
40                 {\r
41                     cliStart.RedirectStandardOutput = true;\r
42                     cliStart.UseShellExecute = false;\r
43                 }\r
44                 if (Properties.Settings.Default.cli_minimized == "Checked")\r
45                     cliStart.WindowStyle = ProcessWindowStyle.Minimized;\r
46 \r
47                 Process[] before = Process.GetProcesses(); // Get a list of running processes before starting.\r
48                 currentEncode.hbProcProcess = Process.Start(cliStart);\r
49                 currentEncode.processID = Main.getCliProcess(before);\r
50                 currentEncode.isEncoding = true;\r
51                 currentEncode.currentQuery = query;\r
52                 currentEncode.processHandle = (int)currentEncode.hbProcProcess.MainWindowHandle; // Set the process Handle\r
53 \r
54                 // Set the process Priority\r
55                 Process hbCliProcess = null;\r
56                 if (currentEncode.processID != -1)\r
57                     hbCliProcess = Process.GetProcessById(currentEncode.processID);\r
58 \r
59                 if (hbCliProcess != null)\r
60                     switch (Properties.Settings.Default.processPriority)\r
61                     {\r
62                         case "Realtime":\r
63                             hbCliProcess.PriorityClass = ProcessPriorityClass.RealTime;\r
64                             break;\r
65                         case "High":\r
66                             hbCliProcess.PriorityClass = ProcessPriorityClass.High;\r
67                             break;\r
68                         case "Above Normal":\r
69                             hbCliProcess.PriorityClass = ProcessPriorityClass.AboveNormal;\r
70                             break;\r
71                         case "Normal":\r
72                             hbCliProcess.PriorityClass = ProcessPriorityClass.Normal;\r
73                             break;\r
74                         case "Low":\r
75                             hbCliProcess.PriorityClass = ProcessPriorityClass.Idle;\r
76                             break;\r
77                         default:\r
78                             hbCliProcess.PriorityClass = ProcessPriorityClass.BelowNormal;\r
79                             break;\r
80                     }\r
81             }\r
82             catch (Exception exc)\r
83             {\r
84                 MessageBox.Show("An error occured in runCli()\n Error Information: \n\n" + exc);\r
85             }\r
86 \r
87             return currentEncode;\r
88         }\r
89 \r
90         /// <summary>\r
91         /// Kill the CLI process\r
92         /// </summary>\r
93         public void closeCLI(EncodeProcess ep)\r
94         {\r
95             Process[] prs = Process.GetProcesses();\r
96             foreach (Process process in prs)\r
97             {\r
98                 if (process.Id == ep.processID)\r
99                 {\r
100                     process.Refresh();\r
101                     if (!process.HasExited)\r
102                         process.Kill();\r
103 \r
104                     process.WaitForExit();\r
105                 }\r
106             } \r
107         }\r
108 \r
109         /// <summary>\r
110         /// Perform an action after an encode. e.g a shutdown, standby, restart etc.\r
111         /// </summary>\r
112         public void afterEncodeAction(EncodeProcess ep)\r
113         {\r
114             ep.isEncoding = false;\r
115             ep.currentQuery = String.Empty;\r
116             // Do something whent he encode ends.\r
117             switch (Properties.Settings.Default.CompletionOption)\r
118             {\r
119                 case "Shutdown":\r
120                     Process.Start("Shutdown", "-s -t 60");\r
121                     break;\r
122                 case "Log Off":\r
123                     ExitWindowsEx(0, 0);\r
124                     break;\r
125                 case "Suspend":\r
126                     Application.SetSuspendState(PowerState.Suspend, true, true);\r
127                     break;\r
128                 case "Hibernate":\r
129                     Application.SetSuspendState(PowerState.Hibernate, true, true);\r
130                     break;\r
131                 case "Lock System":\r
132                     LockWorkStation();\r
133                     break;\r
134                 case "Quit HandBrake":\r
135                     Application.Exit();\r
136                     break;\r
137                 default:\r
138                     break;\r
139             }\r
140         }\r
141 \r
142         /// <summary>\r
143         /// Append the CLI query to the start of the log file.\r
144         /// </summary>\r
145         /// <param name="query"></param>\r
146         public void addCLIQueryToLog(string query)\r
147         {\r
148             string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
149             string logPath = Path.Combine(logDir, "last_encode_log.txt");\r
150 \r
151             StreamReader reader = new StreamReader(File.Open(logPath, FileMode.Open, FileAccess.Read));\r
152             String log = reader.ReadToEnd();\r
153             reader.Close();\r
154 \r
155             StreamWriter writer = new StreamWriter(File.Create(logPath));\r
156 \r
157             writer.Write("### CLI Query: " + query + "\n\n");\r
158             writer.Write("#########################################\n\n");\r
159             writer.WriteLine(log);\r
160             writer.Flush();\r
161             writer.Close();\r
162         }\r
163 \r
164         /// <summary>\r
165         /// Save a copy of the log to the users desired location or a default location\r
166         /// if this feature is enabled in options.\r
167         /// </summary>\r
168         /// <param name="destination"></param>\r
169         public void copyLog(string destination)\r
170         {\r
171             try\r
172             {\r
173                 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
174                 string tempLogFile = Path.Combine(logDir, "last_encode_log.txt");\r
175 \r
176                 string encodeDestinationPath = Path.GetDirectoryName(destination);\r
177                 String[] destName = destination.Split('\\');\r
178                 string destinationFile = destName[destName.Length - 1];\r
179                 string encodeLogFile = DateTime.Now.ToString().Replace("/", "-").Replace(":", "-") + " " + destinationFile + ".txt";\r
180 \r
181                 // Make sure the log directory exists.\r
182                 if (!Directory.Exists(logDir))\r
183                     Directory.CreateDirectory(logDir);\r
184 \r
185                 // Copy the Log to HandBrakes log folder in the users applciation data folder.\r
186                 File.Copy(tempLogFile, Path.Combine(logDir, encodeLogFile));\r
187 \r
188                 // Save a copy of the log file in the same location as the enocde.\r
189                 if (Properties.Settings.Default.saveLogWithVideo == "Checked")\r
190                     File.Copy(tempLogFile, Path.Combine(encodeDestinationPath, encodeLogFile));\r
191 \r
192                 // Save a copy of the log file to a user specified location\r
193                 if (Directory.Exists(Properties.Settings.Default.saveLogPath))\r
194                     if (Properties.Settings.Default.saveLogPath != String.Empty && Properties.Settings.Default.saveLogToSpecifiedPath == "Checked")\r
195                         File.Copy(tempLogFile, Path.Combine(Properties.Settings.Default.saveLogPath, encodeLogFile));\r
196             }\r
197             catch (Exception exc)\r
198             {\r
199                 MessageBox.Show("Something went a bit wrong trying to copy your log file.\nError Information:\n\n" + exc, "Error",\r
200                                 MessageBoxButtons.OK, MessageBoxIcon.Error);\r
201             }\r
202         }\r
203 \r
204     }\r
205 }