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