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.ComponentModel;\r
9 using System.Text;\r
10 using System.Windows.Forms;\r
11 using System.IO;\r
12 using System.Threading;\r
13 using Handbrake.Functions;\r
14 using Microsoft.Win32;\r
15 \r
16 \r
17 namespace Handbrake\r
18 {\r
19     public partial class frmActivityWindow : Form\r
20     {\r
21         private delegate void setTextCallback(StringBuilder text);\r
22         private delegate void setTextClearCallback();\r
23         private static int _position;\r
24         private static string _lastMode;\r
25         private static string _currentMode;\r
26         private Thread _monitor;\r
27         private Boolean _kilLThread;\r
28 \r
29         public frmActivityWindow(string mode)\r
30         {\r
31             _kilLThread = false;\r
32             _position = 0;\r
33             if (mode == "scan")\r
34                 SetScanMode();\r
35             else\r
36                 SetEncodeMode();\r
37 \r
38             InitializeComponent();\r
39         }\r
40         private void NewActivityWindow_Load(object sender, EventArgs e)\r
41         {\r
42             _monitor = new Thread(LogMonitor);\r
43             \r
44             try\r
45             {\r
46                 _monitor.Start();\r
47             }\r
48             catch (Exception exc)\r
49             {\r
50                 MessageBox.Show("Unable to monitor HandBrakes log files. Please report this error. If the problem presists, try rebooting your computer.\n\n Debug Informaton:\n" + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
51             }\r
52         }\r
53 \r
54         private void LogMonitor()\r
55         {\r
56             while (true)\r
57             {\r
58                 if (!IsHandleCreated || _kilLThread) // break out the thread if the window has been disposed.\r
59                     break;\r
60 \r
61                 // Perform a reset if require.\r
62                 // If we have switched to a different log file, we want to start from the beginning.\r
63                 if (SetLogFile != _lastMode)\r
64                     Reset();\r
65 \r
66                 // Perform the window update\r
67                 switch (SetLogFile)\r
68                 {\r
69                     case "last_scan_log.txt":\r
70                         AppendWindowText(ReadFile("last_scan_log.txt"));\r
71                         _lastMode = "last_scan_log.txt";\r
72                         break;\r
73                     case "last_encode_log.txt":\r
74                         AppendWindowText(ReadFile("last_encode_log.txt"));\r
75                         _lastMode = "last_encode_log.txt";\r
76                         break;\r
77                 }\r
78 \r
79                 try\r
80                 {\r
81                     Thread.Sleep(1000);\r
82                 }\r
83                 catch (ThreadInterruptedException)\r
84                 {\r
85                     // Do Nothnig.\r
86                 }\r
87 \r
88             }\r
89         }\r
90         private StringBuilder ReadFile(string file)\r
91         {\r
92             StringBuilder appendText = new StringBuilder();\r
93 \r
94             // 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
95             // we'll need to make a copy of it.\r
96             string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
97             string logFile = Path.Combine(logDir, file);\r
98             string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");\r
99 \r
100             try\r
101             {\r
102                 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.\r
103                 if (File.Exists(logFile2))\r
104                     File.Delete(logFile2);\r
105 \r
106                 // Copy the log file.\r
107                 if (File.Exists(logFile))\r
108                     File.Copy(logFile, logFile2, true);\r
109                 else\r
110                 {\r
111                     appendText.AppendFormat("Waiting for the log file to be generated ...\n");\r
112                     _position = 0;\r
113                     ClearWindowText();\r
114                     PrintLogHeader();\r
115                     return appendText;\r
116                 }\r
117 \r
118                 // Start the Reader\r
119                 // Only use text which continues on from the last read line\r
120                 StreamReader sr = new StreamReader(logFile2);\r
121                 string line;\r
122                 int i = 1;\r
123                 while ((line = sr.ReadLine()) != null)\r
124                 {\r
125                     if (i > _position)\r
126                     {\r
127                         appendText.AppendLine(line);\r
128                         _position++;\r
129                     }\r
130                     i++;\r
131                 }\r
132                 sr.Close();\r
133                 sr.Dispose();\r
134 \r
135             }\r
136             catch (Exception exc)\r
137             {\r
138                 appendText.AppendFormat("\n The Log file could not be read. You may need to restart HandBrake! " + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
139                 _position = 0;\r
140                 ClearWindowText();\r
141             }\r
142             return appendText;\r
143         }\r
144         private void AppendWindowText(StringBuilder text)\r
145         {\r
146             try\r
147             {\r
148                 if (IsHandleCreated)\r
149                 {\r
150                     if (rtf_actLog.InvokeRequired)\r
151                     {\r
152                         IAsyncResult invoked = BeginInvoke(new setTextCallback(AppendWindowText), new object[] { text });\r
153                         EndInvoke(invoked);\r
154                     }\r
155                     else\r
156                         rtf_actLog.AppendText(text.ToString());\r
157                 }\r
158             } catch(ThreadInterruptedException)\r
159             {\r
160                 // Do Nothing\r
161             }\r
162             catch (Exception exc)\r
163             {\r
164                 MessageBox.Show("Unless you are having problems, you can probably ignore this error. It would not hurt to report this error!\n\nSetWindowText(): Exception: \n" + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);\r
165             }\r
166         }\r
167         private void ClearWindowText()\r
168         {\r
169             try\r
170             {\r
171                 if (IsHandleCreated)\r
172                 {\r
173                     if (rtf_actLog.InvokeRequired)\r
174                     {\r
175                         IAsyncResult invoked = BeginInvoke(new setTextClearCallback(ClearWindowText));\r
176                         EndInvoke(invoked);\r
177                     }\r
178                     else\r
179                         rtf_actLog.Clear();\r
180                 }\r
181             }\r
182             catch (Exception exc)\r
183             {\r
184                 MessageBox.Show("Unless you are having problems, you can probably ignore this error. It would not hurt to report this error!\n\nClearWindowText(): Exception: \n" + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);\r
185             }\r
186         }\r
187         private void PrintLogHeader()\r
188         {\r
189             try\r
190             {\r
191                 if (IsHandleCreated)\r
192                 {\r
193                     if (rtf_actLog.InvokeRequired)\r
194                     {\r
195                         IAsyncResult invoked = BeginInvoke(new setTextClearCallback(PrintLogHeader));\r
196                         EndInvoke(invoked);\r
197                     }\r
198                     else\r
199                     {\r
200                         // Print the log header. This function will be re-implimented later. Do not delete.\r
201                         rtf_actLog.AppendText(String.Format("### Windows GUI {1} {0} \n", Properties.Settings.Default.hb_build, Properties.Settings.Default.hb_version));\r
202                         rtf_actLog.AppendText(String.Format("### Running: {0} \n###\n", Environment.OSVersion));\r
203                         rtf_actLog.AppendText(String.Format("### CPU: {0} \n", getCpuCount()));\r
204                         rtf_actLog.AppendText(String.Format("### Ram: {0} MB \n", TotalPhysicalMemory()));\r
205                         rtf_actLog.AppendText(String.Format("### Screen: {0}x{1} \n", screenBounds().Bounds.Width, screenBounds().Bounds.Height));\r
206                         rtf_actLog.AppendText(String.Format("### Temp Dir: {0} \n", Path.GetTempPath()));\r
207                         rtf_actLog.AppendText(String.Format("### Install Dir: {0} \n", Application.StartupPath));\r
208                         rtf_actLog.AppendText(String.Format("### Data Dir: {0} \n", Application.UserAppDataPath));\r
209                         rtf_actLog.AppendText("#########################################\n\n");\r
210                     }\r
211                 }\r
212             }\r
213             catch (Exception exc)\r
214             {\r
215                 MessageBox.Show("Unless you are having problems, you can probably ignore this error. It would not hurt to report this error!\n\nPrintLogHeader(): Exception: \n" + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);\r
216             }\r
217 \r
218         }\r
219         private void Reset()\r
220         {\r
221             _position = 0;\r
222             ClearWindowText();\r
223             PrintLogHeader();\r
224         }\r
225 \r
226         #region Public\r
227 \r
228         public string SetLogFile\r
229         {\r
230             get { return string.IsNullOrEmpty(_currentMode) ? "" : _currentMode; }\r
231             set { _currentMode = value; }\r
232         }\r
233         public void SetScanMode()\r
234         {\r
235             Reset();\r
236             SetLogFile = "last_scan_log.txt";\r
237             this.Text = "Activity Window (Scan Log)";\r
238         }\r
239         public void SetEncodeMode()\r
240         {\r
241             Reset();\r
242             SetLogFile = "last_encode_log.txt";\r
243             this.Text = "Activity Window (Enocde Log)";\r
244         }\r
245 \r
246         #endregion\r
247 \r
248         #region User Interface\r
249         private void mnu_copy_log_Click(object sender, EventArgs e)\r
250         {\r
251             if (rtf_actLog.SelectedText != "")\r
252                 Clipboard.SetDataObject(rtf_actLog.SelectedText, true);\r
253             else\r
254                 Clipboard.SetDataObject(rtf_actLog.Text, true);\r
255         }\r
256         private void mnu_openLogFolder_Click(object sender, EventArgs e)\r
257         {\r
258             string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
259             string windir = Environment.GetEnvironmentVariable("WINDIR");\r
260             System.Diagnostics.Process prc = new System.Diagnostics.Process();\r
261             prc.StartInfo.FileName = windir + @"\explorer.exe";\r
262             prc.StartInfo.Arguments = logDir;\r
263             prc.Start();\r
264         }\r
265         private void btn_copy_Click(object sender, EventArgs e)\r
266         {\r
267             if (rtf_actLog.SelectedText != "")\r
268                 Clipboard.SetDataObject(rtf_actLog.SelectedText, true);\r
269             else\r
270                 Clipboard.SetDataObject(rtf_actLog.Text, true);\r
271         }\r
272         private void btn_scan_log_Click(object sender, EventArgs e)\r
273         {\r
274             SetScanMode();\r
275         }\r
276         private void btn_encode_log_Click(object sender, EventArgs e)\r
277         {\r
278             SetEncodeMode();\r
279         }\r
280         #endregion\r
281 \r
282         #region System Information\r
283         /// <summary>\r
284         /// Returns the total physical ram in a system\r
285         /// </summary>\r
286         /// <returns></returns>\r
287         public uint TotalPhysicalMemory()\r
288         {\r
289             Win32.MEMORYSTATUS memStatus = new Win32.MEMORYSTATUS();\r
290             Win32.GlobalMemoryStatus(ref memStatus);\r
291 \r
292             uint MemoryInfo = memStatus.dwTotalPhys;\r
293             MemoryInfo = MemoryInfo / 1024 / 1024;\r
294 \r
295             return MemoryInfo;\r
296         }\r
297 \r
298         /// <summary>\r
299         /// Get the number of CPU Cores\r
300         /// </summary>\r
301         /// <returns>Object</returns>\r
302         public Object getCpuCount()\r
303         {\r
304             RegistryKey RegKey = Registry.LocalMachine;\r
305             RegKey = RegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");\r
306             return RegKey.GetValue("ProcessorNameString");\r
307         }\r
308 \r
309         /// <summary>\r
310         /// Get the System screen size information.\r
311         /// </summary>\r
312         /// <returns>System.Windows.Forms.Scree</returns>\r
313         public Screen screenBounds()\r
314         {\r
315             return Screen.PrimaryScreen;\r
316         }\r
317         #endregion\r
318 \r
319         protected override void OnClosing(CancelEventArgs e)\r
320         {\r
321             _kilLThread = true;\r
322             _monitor.Interrupt();\r
323             _monitor.Join();\r
324             e.Cancel = true;\r
325             this.Dispose();\r
326             base.OnClosing(e);\r
327         }\r
328     }\r
329 }