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