OSDN Git Service

0445392d003da93267ffa7f8f40c055ec1a51a4e
[handbrake-jp/handbrake-jp-git.git] / win / C# / Functions / Encode.cs
1 /*  CLI.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.Collections.Generic;\r
9 using System.Threading;\r
10 using System.Diagnostics;\r
11 using System.Windows.Forms;\r
12 using System.Globalization;\r
13 using System.IO;\r
14 using System.Runtime.InteropServices;\r
15 \r
16 namespace Handbrake.Functions\r
17 {\r
18     public class Encode\r
19     {\r
20         /// <summary>\r
21         /// CLI output is based on en-US locale,\r
22         /// we use this CultureInfo as IFormatProvider to *.Parse() calls\r
23         /// </summary>\r
24         static readonly public CultureInfo Culture = new CultureInfo("en-US", false);\r
25 \r
26         Process hbProc = new Process();\r
27 \r
28         /// <summary>\r
29         /// Execute a HandBrakeCLI process.\r
30         /// </summary>\r
31         /// <param name="s"></param>\r
32         /// <param name="query">The CLI Query</param>\r
33         public Process runCli(object s, string query)\r
34         {\r
35             try\r
36             {\r
37                 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
38                 string logPath = Path.Combine(Path.GetTempPath(), "hb_encode_log.dat");\r
39 \r
40                 string strCmdLine = String.Format(@" CMD /c """"{0}"" {1} 2>""{2}"" """, handbrakeCLIPath, query, logPath);\r
41 \r
42                 ProcessStartInfo cliStart = new ProcessStartInfo("CMD.exe", strCmdLine);\r
43                 if (Properties.Settings.Default.cli_minimized == "Checked")\r
44                     cliStart.WindowStyle = ProcessWindowStyle.Minimized;\r
45                 hbProc = Process.Start(cliStart);\r
46 \r
47                 // Set the process Priority \r
48                 switch (Properties.Settings.Default.processPriority)\r
49                 {\r
50                     case "Realtime":\r
51                         hbProc.PriorityClass = ProcessPriorityClass.RealTime;\r
52                         break;\r
53                     case "High":\r
54                         hbProc.PriorityClass = ProcessPriorityClass.High;\r
55                         break;\r
56                     case "Above Normal":\r
57                         hbProc.PriorityClass = ProcessPriorityClass.AboveNormal;\r
58                         break;\r
59                     case "Normal":\r
60                         hbProc.PriorityClass = ProcessPriorityClass.Normal;\r
61                         break;\r
62                     case "Low":\r
63                         hbProc.PriorityClass = ProcessPriorityClass.Idle;\r
64                         break;\r
65                     default:\r
66                         hbProc.PriorityClass = ProcessPriorityClass.BelowNormal;\r
67                         break;\r
68                 }\r
69             }\r
70             catch (Exception exc)\r
71             {\r
72                 MessageBox.Show("An error occured in runCli()\n Error Information: \n\n" + exc.ToString());\r
73             }\r
74             return hbProc;\r
75         }\r
76 \r
77         [DllImport("user32.dll")]\r
78         public static extern void LockWorkStation();\r
79         [DllImport("user32.dll")]\r
80         public static extern int ExitWindowsEx(int uFlags, int dwReason);\r
81 \r
82         public void afterEncodeAction()\r
83         {\r
84             // Do something whent he encode ends.\r
85             switch (Properties.Settings.Default.CompletionOption)\r
86             {\r
87                 case "Shutdown":\r
88                     System.Diagnostics.Process.Start("Shutdown", "-s -t 60");\r
89                     break;\r
90                 case "Log Off":\r
91                     ExitWindowsEx(0, 0);\r
92                     break;\r
93                 case "Suspend":\r
94                     Application.SetSuspendState(PowerState.Suspend, true, true);\r
95                     break;\r
96                 case "Hibernate":\r
97                     Application.SetSuspendState(PowerState.Hibernate, true, true);\r
98                     break;\r
99                 case "Lock System":\r
100                     LockWorkStation();\r
101                     break;\r
102                 case "Quit HandBrake":\r
103                     Application.Exit();\r
104                     break;\r
105                 default:\r
106                     break;\r
107             }\r
108         }\r
109 \r
110         /// <summary>\r
111         /// Append the CLI query to the start of the log file.\r
112         /// </summary>\r
113         /// <param name="query"></param>\r
114         public void addCLIQueryToLog(string query)\r
115         {\r
116             string logPath = Path.Combine(Path.GetTempPath(), "hb_encode_log.dat");\r
117 \r
118             StreamReader reader = new StreamReader(File.Open(logPath, FileMode.Open, FileAccess.Read));\r
119             String log = reader.ReadToEnd();\r
120             reader.Close();\r
121 \r
122             StreamWriter writer = new StreamWriter(File.Create(logPath));\r
123 \r
124             writer.Write("### CLI Query: " + query + "\n\n");\r
125             writer.Write("#########################################\n\n");\r
126             writer.WriteLine(log);\r
127             writer.Flush();\r
128             writer.Close();\r
129         }\r
130 \r
131         /// <summary>\r
132         /// Save a copy of the log to the users desired location or a default location\r
133         /// if this feature is enabled in options.\r
134         /// </summary>\r
135         /// <param name="query"></param>\r
136         public void copyLog(string query)\r
137         {\r
138             // The user may wish to do something with the log.\r
139             if (Properties.Settings.Default.saveLog == "Checked")\r
140             {\r
141                 string logPath = Path.Combine(Path.GetTempPath(), "hb_encode_log.dat");\r
142                 Functions.QueryParser parsed = Functions.QueryParser.Parse(query);\r
143 \r
144                 if (Properties.Settings.Default.saveLogWithVideo == "Checked")\r
145                 {\r
146                     string[] destName = parsed.Destination.Split('\\');\r
147                     string destinationFile = "";\r
148                     for (int i = 0; i < destName.Length - 1; i++)\r
149                     {\r
150                         destinationFile += destName[i] + "\\";\r
151                     }\r
152 \r
153                     destinationFile += DateTime.Now.ToString().Replace("/", "-").Replace(":", "-") + " " + destName[destName.Length - 1] + ".txt"; \r
154 \r
155                     File.Copy(logPath, destinationFile);\r
156                 }\r
157                 else if (Properties.Settings.Default.saveLogPath != String.Empty)\r
158                 {\r
159                     string[] destName = parsed.Destination.Split('\\');\r
160                     string dest = destName[destName.Length - 1];\r
161                     string filename = DateTime.Now.ToString().Replace("/", "-").Replace(":", "-") + " " + dest + ".txt";\r
162                     string useDefinedLogPath = Path.Combine(Properties.Settings.Default.saveLogPath, filename);\r
163 \r
164                     File.Copy(logPath, useDefinedLogPath);\r
165                 }\r
166             }\r
167         }\r
168     }\r
169 }\r