OSDN Git Service

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