OSDN Git Service

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