OSDN Git Service

84f9bddd5639fc6446cf6e7f94a5e6d0a38506b2
[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.Windows.Forms;\r
9 using System.IO;\r
10 using System.Threading;\r
11 using Handbrake.EncodeQueue;\r
12 using Handbrake.Functions;\r
13 using Microsoft.Win32;\r
14 \r
15 \r
16 namespace Handbrake\r
17 {\r
18     public partial class frmActivityWindow : Form\r
19     {\r
20         delegate void SetTextCallback(string text);\r
21         String read_file;\r
22         Thread monitor;\r
23         EncodeAndQueueHandler encodeQueue;\r
24         int position;  // Position in the arraylist reached by the current log output in the rtf box.\r
25         string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
26         private frmMain mainWin;\r
27 \r
28         /// <summary>\r
29         /// This window should be used to display the RAW output of the handbrake CLI which is produced during an encode.\r
30         /// </summary>\r
31         public frmActivityWindow(string file, EncodeAndQueueHandler eh, frmMain mw)\r
32         {\r
33             InitializeComponent();\r
34 \r
35             rtf_actLog.Text = string.Empty;\r
36             encodeQueue = eh;\r
37             position = 0;\r
38             mainWin = mw;\r
39 \r
40             // When the window closes, we want to abort the monitor thread.\r
41             this.Disposed += new EventHandler(forceQuit);\r
42 \r
43             // Print the Log header in the Rich text box.\r
44             displayLogHeader();\r
45 \r
46             if (file == "last_scan_log.txt")\r
47                 setLogView(true);\r
48             else\r
49                 setLogView(false);\r
50 \r
51             // Start a new thread which will montior and keep the log window up to date if required/\r
52             startLogThread(read_file);\r
53         }\r
54 \r
55         /// <summary>\r
56         /// Set the file which the log window is viewing.\r
57         /// </summary>\r
58         /// <param name="scan"></param>\r
59         public void setLogView(Boolean scan)\r
60         {\r
61             if (scan)\r
62             {\r
63                 txt_log.Text = "Scan Log";\r
64                 read_file = "last_scan_log.txt";\r
65             }\r
66             else\r
67             {\r
68                 read_file = "last_encode_log.txt";\r
69                 txt_log.Text = "Encode Log";\r
70             }\r
71         }\r
72 \r
73         /// <summary>\r
74         /// Displays the Log header\r
75         /// </summary>\r
76         private void displayLogHeader()\r
77         {\r
78             // Add a header to the log file indicating that it's from the Windows GUI and display the windows version\r
79             rtf_actLog.AppendText(String.Format("### Windows GUI {1} {0} \n", Properties.Settings.Default.hb_build, Properties.Settings.Default.hb_version));\r
80             rtf_actLog.AppendText(String.Format("### Running: {0} \n###\n", Environment.OSVersion));\r
81             rtf_actLog.AppendText(String.Format("### CPU: {0} \n", getCpuCount()));\r
82             rtf_actLog.AppendText(String.Format("### Ram: {0} MB \n", TotalPhysicalMemory()));\r
83             rtf_actLog.AppendText(String.Format("### Screen: {0}x{1} \n", screenBounds().Bounds.Width, screenBounds().Bounds.Height));\r
84             rtf_actLog.AppendText(String.Format("### Temp Dir: {0} \n", Path.GetTempPath()));\r
85             rtf_actLog.AppendText(String.Format("### Install Dir: {0} \n", Application.StartupPath));\r
86             rtf_actLog.AppendText(String.Format("### Data Dir: {0} \n", Application.UserAppDataPath));\r
87             rtf_actLog.AppendText("#########################################\n\n");\r
88             if (encodeQueue.isEncoding && encodeQueue.LastEncode.Query != String.Empty)\r
89             {\r
90                 rtf_actLog.AppendText("### CLI Query: " + encodeQueue.LastEncode.Query + "\n\n");\r
91                 rtf_actLog.AppendText("#########################################\n\n");\r
92             }\r
93         }\r
94 \r
95         /// <summary>\r
96         /// Starts a new thread which runs the autoUpdate function.\r
97         /// </summary>\r
98         /// <param name="file"> File which will be used to populate the Rich text box.</param>\r
99         private void startLogThread(string file)\r
100         {\r
101             try\r
102             {\r
103                 string logFile = Path.Combine(logDir, file);\r
104                 if (File.Exists(logFile))\r
105                 {\r
106                     // Start a new thread to run the autoUpdate process\r
107                     monitor = new Thread(autoUpdate);\r
108                     monitor.IsBackground = true;\r
109                     monitor.Start();\r
110                 }\r
111                 else\r
112                     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
113 \r
114             }\r
115             catch (Exception exc)\r
116             {\r
117                 MessageBox.Show("startLogThread(): Exception: \n" + exc);\r
118             }\r
119         }\r
120 \r
121         /// <summary>\r
122         /// Updates the log window with any new data which is in the log file.\r
123         /// This is done every 5 seconds.\r
124         /// </summary>\r
125         /// <param name="state"></param>\r
126         private void autoUpdate(object state)\r
127         {\r
128             try\r
129             {\r
130                 Boolean lastUpdate = false;\r
131                 updateTextFromThread();\r
132                 while (true)\r
133                 {\r
134                     if (encodeQueue.isEncoding || mainWin.isScanning)\r
135                         updateTextFromThread();\r
136                     else\r
137                     {\r
138                         // The encode may just have stoped, so, refresh the log one more time before restarting it.\r
139                         if (lastUpdate == false)\r
140                             updateTextFromThread();\r
141 \r
142                         lastUpdate = true; // Prevents the log window from being updated when there is no encode going.\r
143                         position = 0; // There is no encoding, so reset the log position counter to 0 so it can be reused\r
144                     }\r
145                     Thread.Sleep(5000);\r
146                 }\r
147             }\r
148             catch (ThreadAbortException)\r
149             {\r
150                 // Do Nothing. This is needed since we run thread.abort(). \r
151                 // Should probably find a better way of making this work at some point.\r
152             }\r
153             catch (Exception exc)\r
154             {\r
155                 MessageBox.Show("autoUpdate(): Exception: \n" + exc);\r
156             }\r
157         }\r
158 \r
159         /// <summary>\r
160         /// Finds any new text in the log file and calls a funciton to display this new text.\r
161         /// </summary>\r
162         private void updateTextFromThread()\r
163         {\r
164             try\r
165             {\r
166                 String info = readFile();\r
167                 if (info.Contains("has exited"))\r
168                     info += "\n ############ End of Log ############## \n";\r
169 \r
170                 SetText(info);\r
171             }\r
172             catch (Exception exc)\r
173             {\r
174                 MessageBox.Show("updateTextFromThread(): Exception: \n" + exc);\r
175             }\r
176         }\r
177 \r
178         /// <summary>\r
179         /// Updates the rich text box with anything in the string text.\r
180         /// </summary>\r
181         /// <param name="text"></param>\r
182         private void SetText(string text)\r
183         {\r
184             try\r
185             {\r
186                 // InvokeRequired required compares the thread ID of the\r
187                 // calling thread to the thread ID of the creating thread.\r
188                 // If these threads are different, it returns true.\r
189                 if (IsHandleCreated) // Make sure the windows has a handle before doing anything\r
190                 {\r
191                     if (rtf_actLog.InvokeRequired)\r
192                     {\r
193                         SetTextCallback d = new SetTextCallback(SetText);\r
194                         Invoke(d, new object[] { text });\r
195                     }\r
196                     else\r
197                         rtf_actLog.AppendText(text);\r
198                 }\r
199             }\r
200             catch (Exception exc)\r
201             {\r
202                 MessageBox.Show("SetText(): Exception: \n" + exc);\r
203             }\r
204         }\r
205 \r
206         /// <summary>\r
207         /// Read the log file, and store the data in a List.\r
208         /// </summary>\r
209         /// <returns></returns>\r
210         private String readFile()\r
211         {\r
212             String appendText = String.Empty;\r
213             try\r
214             {\r
215                 // 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
216                 // we'll need to make a copy of it.\r
217                 string logFile = Path.Combine(logDir, read_file);\r
218                 string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");\r
219 \r
220                 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.\r
221                 if (File.Exists(logFile2))\r
222                     File.Delete(logFile2);\r
223 \r
224                 // Copy the log file.\r
225                 File.Copy(logFile, logFile2);\r
226 \r
227                 // Open the copied log file for reading\r
228                 StreamReader sr = new StreamReader(logFile2);\r
229                 string line;\r
230                 int i = 1;\r
231                 while ((line = sr.ReadLine()) != null)\r
232                 {\r
233                     if (i > position)\r
234                     {\r
235                         appendText += line + Environment.NewLine;\r
236                         position++;\r
237                     }\r
238                     i++;\r
239                 }\r
240                 sr.Close();\r
241                 sr.Dispose();\r
242 \r
243                 return appendText;\r
244             }\r
245             catch (Exception exc)\r
246             {\r
247                 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
248             }\r
249             return null;\r
250         }\r
251 \r
252         /// <summary>\r
253         /// Kills the montior thead when the window is disposed of.\r
254         /// </summary>\r
255         /// <param name="sender"></param>\r
256         /// <param name="e"></param>\r
257         private void forceQuit(object sender, EventArgs e)\r
258         {\r
259             if (monitor != null)\r
260             {\r
261                 while (monitor.IsAlive)\r
262                     monitor.Abort();\r
263             }\r
264 \r
265             this.Close();\r
266         }\r
267 \r
268         #region User Interface\r
269 \r
270         private void mnu_copy_log_Click(object sender, EventArgs e)\r
271         {\r
272             if (rtf_actLog.SelectedText != "")\r
273                 Clipboard.SetDataObject(rtf_actLog.SelectedText, true);\r
274             else\r
275                 Clipboard.SetDataObject(rtf_actLog.Text, true);\r
276         }\r
277         private void mnu_openLogFolder_Click(object sender, EventArgs e)\r
278         {\r
279             string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
280             string windir = Environment.GetEnvironmentVariable("WINDIR");\r
281             System.Diagnostics.Process prc = new System.Diagnostics.Process();\r
282             prc.StartInfo.FileName = windir + @"\explorer.exe";\r
283             prc.StartInfo.Arguments = logDir;\r
284             prc.Start();\r
285         }\r
286         private void btn_copy_Click(object sender, EventArgs e)\r
287         {\r
288             if (rtf_actLog.SelectedText != "")\r
289                 Clipboard.SetDataObject(rtf_actLog.SelectedText, true);\r
290             else\r
291                 Clipboard.SetDataObject(rtf_actLog.Text, true);\r
292         }\r
293         private void btn_scan_log_Click(object sender, EventArgs e)\r
294         {\r
295             // Switch to the scan log.\r
296 \r
297             if (monitor != null)\r
298                 monitor.Abort();\r
299 \r
300             rtf_actLog.Clear();\r
301             read_file = "last_scan_log.txt";\r
302             displayLogHeader();\r
303             startLogThread(read_file);\r
304             txt_log.Text = "Scan Log";\r
305         }\r
306         private void btn_encode_log_Click(object sender, EventArgs e)\r
307         {\r
308             // Switch to the encode log\r
309 \r
310             if (monitor != null)\r
311                 monitor.Abort();\r
312 \r
313             rtf_actLog.Clear();\r
314             read_file = "last_encode_log.txt";\r
315             position = 0;\r
316             displayLogHeader();\r
317             startLogThread(read_file);\r
318             txt_log.Text = "Encode Log";\r
319         }\r
320 \r
321         #endregion\r
322 \r
323         #region System Information\r
324 \r
325 \r
326         /// <summary>\r
327         /// Returns the total physical ram in a system\r
328         /// </summary>\r
329         /// <returns></returns>\r
330         public uint TotalPhysicalMemory()\r
331         {\r
332             Win32.MEMORYSTATUS memStatus = new Win32.MEMORYSTATUS();\r
333             Win32.GlobalMemoryStatus(ref memStatus);\r
334 \r
335             uint MemoryInfo = memStatus.dwTotalPhys;\r
336             MemoryInfo = MemoryInfo / 1024 / 1024;\r
337 \r
338             return MemoryInfo;\r
339         }\r
340 \r
341         /// <summary>\r
342         /// Get the number of CPU Cores\r
343         /// </summary>\r
344         /// <returns>Object</returns>\r
345         public Object getCpuCount()\r
346         {\r
347             RegistryKey RegKey = Registry.LocalMachine;\r
348             RegKey = RegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");\r
349             return RegKey.GetValue("ProcessorNameString");\r
350         }\r
351 \r
352         /// <summary>\r
353         /// Get the System screen size information.\r
354         /// </summary>\r
355         /// <returns>System.Windows.Forms.Scree</returns>\r
356         public Screen screenBounds()\r
357         {\r
358             return Screen.PrimaryScreen;\r
359         }\r
360 \r
361         #endregion\r
362 \r
363     }\r
364 }