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