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