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)\r
38                 {\r
39                     cliStart.RedirectStandardOutput = true;\r
40                     cliStart.UseShellExecute = false;\r
41                 }\r
42                 if (Properties.Settings.Default.cli_minimized)\r
43                     cliStart.WindowStyle = ProcessWindowStyle.Minimized;\r
44 \r
45                 Process[] before = Process.GetProcesses(); // Get a list of running processes before starting.\r
46                 hbProcess = Process.Start(cliStart);\r
47                 processID = Main.getCliProcess(before);\r
48                 isEncoding = true;\r
49                 currentQuery = query;\r
50                 if (hbProcess != null)\r
51                     processHandle = hbProcess.MainWindowHandle; // Set the process Handle\r
52 \r
53                 // Set the process Priority\r
54                 Process hbCliProcess = null;\r
55                 if (processID != -1)\r
56                     hbCliProcess = Process.GetProcessById(processID);\r
57 \r
58                 if (hbCliProcess != null)\r
59                     switch (Properties.Settings.Default.processPriority)\r
60                     {\r
61                         case "Realtime":\r
62                             hbCliProcess.PriorityClass = ProcessPriorityClass.RealTime;\r
63                             break;\r
64                         case "High":\r
65                             hbCliProcess.PriorityClass = ProcessPriorityClass.High;\r
66                             break;\r
67                         case "Above Normal":\r
68                             hbCliProcess.PriorityClass = ProcessPriorityClass.AboveNormal;\r
69                             break;\r
70                         case "Normal":\r
71                             hbCliProcess.PriorityClass = ProcessPriorityClass.Normal;\r
72                             break;\r
73                         case "Low":\r
74                             hbCliProcess.PriorityClass = ProcessPriorityClass.Idle;\r
75                             break;\r
76                         default:\r
77                             hbCliProcess.PriorityClass = ProcessPriorityClass.BelowNormal;\r
78                             break;\r
79                     }\r
80             }\r
81             catch (Exception exc)\r
82             {\r
83                 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
84             }\r
85 \r
86         }\r
87 \r
88         /// <summary>\r
89         /// Kill the CLI process\r
90         /// </summary>\r
91         public void closeCLI()\r
92         {\r
93             hbProcess.Kill();\r
94         }\r
95 \r
96         /// <summary>\r
97         /// Perform an action after an encode. e.g a shutdown, standby, restart etc.\r
98         /// </summary>\r
99         public void afterEncodeAction()\r
100         {\r
101             isEncoding = false;\r
102             currentQuery = String.Empty;\r
103             // Do something whent he encode ends.\r
104             switch (Properties.Settings.Default.CompletionOption)\r
105             {\r
106                 case "Shutdown":\r
107                     Process.Start("Shutdown", "-s -t 60");\r
108                     break;\r
109                 case "Log Off":\r
110                     Win32.ExitWindowsEx(0, 0);\r
111                     break;\r
112                 case "Suspend":\r
113                     Application.SetSuspendState(PowerState.Suspend, true, true);\r
114                     break;\r
115                 case "Hibernate":\r
116                     Application.SetSuspendState(PowerState.Hibernate, true, true);\r
117                     break;\r
118                 case "Lock System":\r
119                     Win32.LockWorkStation();\r
120                     break;\r
121                 case "Quit HandBrake":\r
122                     Application.Exit();\r
123                     break;\r
124                 default:\r
125                     break;\r
126             }\r
127         }\r
128 \r
129         /// <summar>\r
130         /// Append the CLI query to the start of the log file.\r
131         /// </summary>\r
132         /// <param name="query"></param>\r
133         public void addCLIQueryToLog(string query)\r
134         {\r
135             string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
136             string logPath = Path.Combine(logDir, "last_encode_log.txt");\r
137 \r
138             StreamReader reader = new StreamReader(File.Open(logPath, FileMode.Open, FileAccess.Read, FileShare.Read));\r
139             String log = reader.ReadToEnd();\r
140             reader.Close();\r
141 \r
142             StreamWriter writer = new StreamWriter(File.Create(logPath));\r
143 \r
144             writer.Write("### CLI Query: " + query + "\n\n");\r
145             writer.Write("#########################################\n\n");\r
146             writer.WriteLine(log);\r
147             writer.Flush();\r
148             writer.Close();\r
149         }\r
150 \r
151         /// <summary>\r
152         /// Save a copy of the log to the users desired location or a default location\r
153         /// if this feature is enabled in options.\r
154         /// </summary>\r
155         /// <param name="destination"></param>\r
156         public void copyLog(string destination)\r
157         {\r
158             try\r
159             {\r
160                 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
161                 string tempLogFile = Path.Combine(logDir, "last_encode_log.txt");\r
162 \r
163                 string encodeDestinationPath = Path.GetDirectoryName(destination);\r
164                 String destinationFile = Path.GetFileName(destination);\r
165                 string encodeLogFile = destinationFile + " " + DateTime.Now.ToString().Replace("/", "-").Replace(":", "-") + ".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)\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)\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 }