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
8 using System.Diagnostics;
\r
9 using System.Windows.Forms;
\r
11 using System.Runtime.InteropServices;
\r
12 using Handbrake.Functions;
\r
14 namespace Handbrake.EncodeQueue
\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
25 /// Execute a HandBrakeCLI process.
\r
27 /// <param name="query">The CLI Query</param>
\r
28 public EncodeProcess runCli(string query)
\r
30 EncodeProcess currentEncode = new EncodeProcess();
\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
38 ProcessStartInfo cliStart = new ProcessStartInfo("CMD.exe", strCmdLine);
\r
39 if (Properties.Settings.Default.enocdeStatusInGui == "Checked")
\r
41 cliStart.RedirectStandardOutput = true;
\r
42 cliStart.UseShellExecute = false;
\r
45 if (Properties.Settings.Default.cli_minimized == "Checked")
\r
46 cliStart.WindowStyle = ProcessWindowStyle.Minimized;
\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
55 // Set the process Priority
\r
56 Process hbCliProcess = null;
\r
57 if (currentEncode.processID != -1)
\r
58 hbCliProcess = Process.GetProcessById(currentEncode.processID);
\r
60 if (hbCliProcess != null)
\r
61 switch (Properties.Settings.Default.processPriority)
\r
64 hbCliProcess.PriorityClass = ProcessPriorityClass.RealTime;
\r
67 hbCliProcess.PriorityClass = ProcessPriorityClass.High;
\r
69 case "Above Normal":
\r
70 hbCliProcess.PriorityClass = ProcessPriorityClass.AboveNormal;
\r
73 hbCliProcess.PriorityClass = ProcessPriorityClass.Normal;
\r
76 hbCliProcess.PriorityClass = ProcessPriorityClass.Idle;
\r
79 hbCliProcess.PriorityClass = ProcessPriorityClass.BelowNormal;
\r
83 catch (Exception exc)
\r
85 MessageBox.Show("An error occured in runCli()\n Error Information: \n\n" + exc);
\r
88 return currentEncode;
\r
92 /// Kill the CLI process
\r
94 public void closeCLI(EncodeProcess ep)
\r
96 Process[] prs = Process.GetProcesses();
\r
97 foreach (Process process in prs)
\r
99 if (process.Id == ep.processID)
\r
102 if (!process.HasExited)
\r
105 process.WaitForExit();
\r
111 /// Perform an action after an encode. e.g a shutdown, standby, restart etc.
\r
113 public void afterEncodeAction(EncodeProcess ep)
\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
121 Process.Start("Shutdown", "-s -t 60");
\r
124 ExitWindowsEx(0, 0);
\r
127 Application.SetSuspendState(PowerState.Suspend, true, true);
\r
130 Application.SetSuspendState(PowerState.Hibernate, true, true);
\r
132 case "Lock System":
\r
135 case "Quit HandBrake":
\r
136 Application.Exit();
\r
144 /// Append the CLI query to the start of the log file.
\r
146 /// <param name="query"></param>
\r
147 public void addCLIQueryToLog(string query)
\r
149 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
\r
150 string logPath = Path.Combine(logDir, "last_encode_log.txt");
\r
152 StreamReader reader = new StreamReader(File.Open(logPath, FileMode.Open, FileAccess.Read));
\r
153 String log = reader.ReadToEnd();
\r
156 StreamWriter writer = new StreamWriter(File.Create(logPath));
\r
158 writer.Write("### CLI Query: " + query + "\n\n");
\r
159 writer.Write("#########################################\n\n");
\r
160 writer.WriteLine(log);
\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
169 /// <param name="destination"></param>
\r
170 public void copyLog(string destination)
\r
174 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
\r
175 string tempLogFile = Path.Combine(logDir, "last_encode_log.txt");
\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
182 // Make sure the log directory exists.
\r
183 if (!Directory.Exists(logDir))
\r
184 Directory.CreateDirectory(logDir);
\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
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
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
198 catch (Exception exc)
\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