OSDN Git Service

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