OSDN Git Service

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