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