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