OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmActivityWindow.cs
1 /*  frmActivityWindow.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.ComponentModel;\r
9 using System.Windows.Forms;\r
10 using System.IO;\r
11 using System.Threading;\r
12 using Handbrake.EncodeQueue;\r
13 using Handbrake.Functions;\r
14 using Microsoft.Win32;\r
15 \r
16 \r
17 namespace Handbrake\r
18 {\r
19     public partial class frmActivityWindow : Form\r
20     {\r
21         private delegate void SetTextCallback(string text);\r
22         private String read_file;\r
23         private Thread monitor;\r
24         private EncodeAndQueueHandler encodeQueue;\r
25         private int position;  // Position in the arraylist reached by the current log output in the rtf box.\r
26         private string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
27         private frmMain mainWin;\r
28         private Boolean lastUpdate;\r
29 \r
30         public frmActivityWindow(string file, EncodeAndQueueHandler eh, frmMain mw)\r
31         {\r
32             InitializeComponent();\r
33 \r
34             encodeQueue = eh;\r
35             mainWin = mw;\r
36 \r
37             if (file == "last_scan_log.txt")\r
38                 setLogView(true);\r
39             else\r
40                 setLogView(false);\r
41 \r
42             // Start a new thread which will montior and keep the log window up to date if required/\r
43             startLogThread(read_file);\r
44         }\r
45 \r
46         /// <summary>\r
47         /// Set the view which the Log window displays.\r
48         /// Scan = true;\r
49         /// Encode = false;\r
50         /// </summary>\r
51         /// <param name="scan">Boolean. Scan = true</param>\r
52         public void setLogView(Boolean scan)\r
53         {\r
54             position = 0;\r
55             rtf_actLog.Text = String.Empty;\r
56             displayLogHeader();\r
57 \r
58             if (scan)\r
59             {\r
60                 txt_log.Text = "Scan Log";\r
61                 read_file = "last_scan_log.txt";\r
62             }\r
63             else\r
64             {\r
65                 read_file = "last_encode_log.txt";\r
66                 txt_log.Text = "Encode Log";\r
67             }\r
68             lastUpdate = false;\r
69         }\r
70         private void displayLogHeader()\r
71         {\r
72             // Add a header to the log file indicating that it's from the Windows GUI and display the windows version\r
73             rtf_actLog.AppendText(String.Format("### Windows GUI {1} {0} \n", Properties.Settings.Default.hb_build, Properties.Settings.Default.hb_version));\r
74             rtf_actLog.AppendText(String.Format("### Running: {0} \n###\n", Environment.OSVersion));\r
75             rtf_actLog.AppendText(String.Format("### CPU: {0} \n", getCpuCount()));\r
76             rtf_actLog.AppendText(String.Format("### Ram: {0} MB \n", TotalPhysicalMemory()));\r
77             rtf_actLog.AppendText(String.Format("### Screen: {0}x{1} \n", screenBounds().Bounds.Width, screenBounds().Bounds.Height));\r
78             rtf_actLog.AppendText(String.Format("### Temp Dir: {0} \n", Path.GetTempPath()));\r
79             rtf_actLog.AppendText(String.Format("### Install Dir: {0} \n", Application.StartupPath));\r
80             rtf_actLog.AppendText(String.Format("### Data Dir: {0} \n", Application.UserAppDataPath));\r
81             rtf_actLog.AppendText("#########################################\n\n");\r
82             if (encodeQueue.isEncoding && encodeQueue.LastEncode.Query != String.Empty)\r
83             {\r
84                 rtf_actLog.AppendText("### CLI Query: " + encodeQueue.LastEncode.Query + "\n\n");\r
85                 rtf_actLog.AppendText("#########################################\n\n");\r
86             }\r
87         }\r
88         private void startLogThread(string file)\r
89         {\r
90             try\r
91             {\r
92                 string logFile = Path.Combine(logDir, file);\r
93                 if (File.Exists(logFile))\r
94                 {\r
95                     monitor = new Thread(autoUpdate) { IsBackground = true };\r
96                     monitor.Start();\r
97                 }\r
98                 else\r
99                     rtf_actLog.AppendText("\n\n\nERROR: The log file could not be found. \nMaybe you cleared your system's tempory folder or maybe you just havn't run an encode yet. \nTried to find the log file in: " + logFile);\r
100 \r
101             }\r
102             catch (Exception exc)\r
103             {\r
104                 MessageBox.Show("startLogThread(): Exception: \n" + exc);\r
105             }\r
106         }\r
107         private void autoUpdate(object state)\r
108         {\r
109             try\r
110             {\r
111                 lastUpdate = false;\r
112                 updateTextFromThread();\r
113                 while (true)\r
114                 {\r
115                     if (IsHandleCreated)\r
116                     {\r
117                         if (encodeQueue.isEncoding || mainWin.isScanning)\r
118                             updateTextFromThread();\r
119                         else\r
120                         {\r
121                             // The encode may just have stoped, so, refresh the log one more time before restarting it.\r
122                             if (lastUpdate == false)\r
123                                 updateTextFromThread();\r
124 \r
125                             lastUpdate = true; // Prevents the log window from being updated when there is no encode going.\r
126                             position = 0; // There is no encoding, so reset the log position counter to 0 so it can be reused\r
127                         }\r
128                     }\r
129                     Thread.Sleep(1000);\r
130                 }\r
131             }\r
132             catch (Exception exc)\r
133             {\r
134                 MessageBox.Show("autoUpdate(): Exception: \n" + exc);\r
135             }\r
136         }\r
137         private void updateTextFromThread()\r
138         {\r
139             try\r
140             {\r
141                 String info = readFile();\r
142                 if (info.Contains("has exited"))\r
143                     info += "\n ############ End of Log ############## \n";\r
144 \r
145                 SetText(info);\r
146             }\r
147             catch (Exception exc)\r
148             {\r
149                 MessageBox.Show("updateTextFromThread(): Exception: \n" + exc);\r
150             }\r
151         }\r
152         private void SetText(string text)\r
153         {\r
154             try\r
155             {\r
156                 if (IsHandleCreated)\r
157                 {\r
158                     if (rtf_actLog.InvokeRequired)\r
159                     {\r
160                         IAsyncResult invoked = BeginInvoke(new SetTextCallback(SetText), new object[] {text});\r
161                         EndInvoke(invoked);\r
162                     } else \r
163                         rtf_actLog.AppendText(text);\r
164                 }\r
165             }\r
166             catch (Exception exc)\r
167             {\r
168                 MessageBox.Show("SetText(): Exception: \n" + exc);\r
169             }\r
170         }\r
171         private String readFile()\r
172         {\r
173             String appendText = String.Empty;\r
174             try\r
175             {\r
176                 // last_encode_log.txt is the primary log file. Since .NET can't read this file whilst the CLI is outputing to it (Not even in read only mode),\r
177                 // we'll need to make a copy of it.\r
178                 string logFile = Path.Combine(logDir, read_file);\r
179                 string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");\r
180 \r
181                 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.\r
182                 if (File.Exists(logFile2))\r
183                     File.Delete(logFile2);\r
184 \r
185                 // Copy the log file.\r
186                 File.Copy(logFile, logFile2);\r
187 \r
188                 // Open the copied log file for reading\r
189                 StreamReader sr = new StreamReader(logFile2);\r
190                 string line;\r
191                 int i = 1;\r
192                 while ((line = sr.ReadLine()) != null)\r
193                 {\r
194                     if (i > position)\r
195                     {\r
196                         appendText += line + Environment.NewLine;\r
197                         position++;\r
198                     }\r
199                     i++;\r
200                 }\r
201                 sr.Close();\r
202                 sr.Dispose();\r
203 \r
204                 return appendText;\r
205             }\r
206             catch (Exception exc)\r
207             {\r
208                 MessageBox.Show("Error in readFile() \n Unable to read the log file.\n You may have to restart HandBrake.\n  Error Information: \n\n" + exc, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
209             }\r
210             return null;\r
211         }\r
212 \r
213         protected override void OnClosing(CancelEventArgs e)\r
214         {\r
215             e.Cancel = true;\r
216             this.Hide();\r
217             base.OnClosing(e);\r
218         }\r
219 \r
220         #region User Interface\r
221         private void mnu_copy_log_Click(object sender, EventArgs e)\r
222         {\r
223             if (rtf_actLog.SelectedText != "")\r
224                 Clipboard.SetDataObject(rtf_actLog.SelectedText, true);\r
225             else\r
226                 Clipboard.SetDataObject(rtf_actLog.Text, true);\r
227         }\r
228         private void mnu_openLogFolder_Click(object sender, EventArgs e)\r
229         {\r
230             string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
231             string windir = Environment.GetEnvironmentVariable("WINDIR");\r
232             System.Diagnostics.Process prc = new System.Diagnostics.Process();\r
233             prc.StartInfo.FileName = windir + @"\explorer.exe";\r
234             prc.StartInfo.Arguments = logDir;\r
235             prc.Start();\r
236         }\r
237         private void btn_copy_Click(object sender, EventArgs e)\r
238         {\r
239             if (rtf_actLog.SelectedText != "")\r
240                 Clipboard.SetDataObject(rtf_actLog.SelectedText, true);\r
241             else\r
242                 Clipboard.SetDataObject(rtf_actLog.Text, true);\r
243         }\r
244         private void btn_scan_log_Click(object sender, EventArgs e)\r
245         {\r
246             setLogView(true);\r
247         }\r
248         private void btn_encode_log_Click(object sender, EventArgs e)\r
249         {\r
250             setLogView(false);\r
251         }\r
252         #endregion\r
253 \r
254         #region System Information\r
255         /// <summary>\r
256         /// Returns the total physical ram in a system\r
257         /// </summary>\r
258         /// <returns></returns>\r
259         public uint TotalPhysicalMemory()\r
260         {\r
261             Win32.MEMORYSTATUS memStatus = new Win32.MEMORYSTATUS();\r
262             Win32.GlobalMemoryStatus(ref memStatus);\r
263 \r
264             uint MemoryInfo = memStatus.dwTotalPhys;\r
265             MemoryInfo = MemoryInfo / 1024 / 1024;\r
266 \r
267             return MemoryInfo;\r
268         }\r
269 \r
270         /// <summary>\r
271         /// Get the number of CPU Cores\r
272         /// </summary>\r
273         /// <returns>Object</returns>\r
274         public Object getCpuCount()\r
275         {\r
276             RegistryKey RegKey = Registry.LocalMachine;\r
277             RegKey = RegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");\r
278             return RegKey.GetValue("ProcessorNameString");\r
279         }\r
280 \r
281         /// <summary>\r
282         /// Get the System screen size information.\r
283         /// </summary>\r
284         /// <returns>System.Windows.Forms.Scree</returns>\r
285         public Screen screenBounds()\r
286         {\r
287             return Screen.PrimaryScreen;\r
288         }\r
289         #endregion\r
290 \r
291     }\r
292 }