OSDN Git Service

merge 0.9.4 to jp
[handbrake-jp/handbrake-jp.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.Windows.Forms;\r
10 using System.IO;\r
11 using System.Threading;\r
12 using Handbrake.EncodeQueue;\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(string text);\r
22         private String _readFile;\r
23         private readonly EncodeAndQueueHandler _encodeQueue;\r
24         private int _position;  // Position in the arraylist reached by the current log output in the rtf box.\r
25         private readonly frmMain _mainWin;\r
26         private Boolean _lastUpdate;\r
27         private Boolean fileNotFoundQuickFix;\r
28 \r
29         public frmActivityWindow(string file, EncodeAndQueueHandler eh, frmMain mw)\r
30         {\r
31             InitializeComponent();\r
32 \r
33             _encodeQueue = eh;\r
34             _mainWin = mw;\r
35 \r
36             fileNotFoundQuickFix = false;\r
37 \r
38 /*\r
39             if (file == "dvdinfo.dat")\r
40                 txt_log.Text = "\83X\83L\83\83\83\93\83\8d\83O";\r
41             else if (file == "hb_encode_log.dat")\r
42                 txt_log.Text = "\83G\83\93\83R\81[\83h\83\8d\83O";\r
43 */\r
44             if (file == "last_scan_log.txt")\r
45                 SetLogView(true);\r
46             else\r
47                 SetLogView(false);\r
48 \r
49             // Start a new thread which will montior and keep the log window up to date if required/\r
50             try\r
51             {\r
52                 Thread monitor = new Thread(AutoUpdate) { IsBackground = true };\r
53                 monitor.Start();\r
54             }\r
55             catch (Exception exc)\r
56             {\r
57                 MessageBox.Show("startLogThread(): Exception: \n" + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
58             }\r
59         }\r
60 \r
61         /// <summary>\r
62         /// Set the view which the Log window displays.\r
63         /// Scan = true;\r
64         /// Encode = false;\r
65         /// </summary>\r
66         /// <param name="scan">Boolean. Scan = true</param>\r
67         public void SetLogView(Boolean scan)\r
68         {\r
69             // Reset\r
70             _position = 0;\r
71             rtf_actLog.Text = String.Empty;\r
72 \r
73             // Print the log header\r
74             rtf_actLog.AppendText(String.Format("### Windows GUI {1} {0} \n", Properties.Settings.Default.hb_build, Properties.Settings.Default.hb_version));\r
75             rtf_actLog.AppendText(String.Format("### Running: {0} \n###\n", Environment.OSVersion));\r
76             rtf_actLog.AppendText(String.Format("### CPU: {0} \n", getCpuCount()));\r
77             rtf_actLog.AppendText(String.Format("### Ram: {0} MB \n", TotalPhysicalMemory()));\r
78             rtf_actLog.AppendText(String.Format("### Screen: {0}x{1} \n", screenBounds().Bounds.Width, screenBounds().Bounds.Height));\r
79             rtf_actLog.AppendText(String.Format("### Temp Dir: {0} \n", Path.GetTempPath()));\r
80             rtf_actLog.AppendText(String.Format("### Install Dir: {0} \n", Application.StartupPath));\r
81             rtf_actLog.AppendText(String.Format("### Data Dir: {0} \n", Application.UserAppDataPath));\r
82             rtf_actLog.AppendText("#########################################\n\n");\r
83 \r
84             // Seutp the log file\r
85             if (scan)\r
86             {\r
87                 txt_log.Text = "Scan Log";\r
88                 _readFile = "last_scan_log.txt";\r
89             }\r
90             else\r
91             {\r
92                 _readFile = "last_encode_log.txt";\r
93                 txt_log.Text = "Encode Log";\r
94                 if (_encodeQueue.isEncoding)\r
95                     if ((!_encodeQueue.LastEncode.IsEmpty) && _encodeQueue.LastEncode.Query != String.Empty)\r
96                     {\r
97                         rtf_actLog.AppendText("### CLI Query: " + _encodeQueue.LastEncode.Query + "\n");\r
98                         rtf_actLog.AppendText("### Custom Query: " + _encodeQueue.LastEncode.CustomQuery + "\n\n");\r
99                         rtf_actLog.AppendText("#########################################\n\n");\r
100                     }\r
101             }\r
102             _lastUpdate = false;\r
103         }\r
104 \r
105 /*\r
106         /// <summary>\r
107         /// Change the log file to be displayed to hb_encode_log.dat\r
108         /// </summary>\r
109         /// <param name="sender"></param>\r
110         /// <param name="e"></param>\r
111         private void btn_scan_log_Click(object sender, EventArgs e)\r
112         {\r
113             if (monitor != null)\r
114                 monitor.Abort();\r
115 \r
116             rtf_actLog.Clear();\r
117             read_file = "dvdinfo.dat";\r
118             displayLogHeader();\r
119             startLogThread(read_file);\r
120             txt_log.Text = "\83X\83L\83\83\83\93\83\8d\83O";\r
121         }\r
122 \r
123         /// <summary>\r
124         /// Change the log file to be displayed to dvdinfo.dat\r
125         /// </summary>\r
126         /// <param name="sender"></param>\r
127         /// <param name="e"></param>\r
128         private void btn_encode_log_Click(object sender, EventArgs e)\r
129         {\r
130             if (monitor != null)\r
131                 monitor.Abort();\r
132 \r
133             rtf_actLog.Clear();\r
134             read_file = "hb_encode_log.dat";\r
135             position = 0;\r
136             displayLogHeader();\r
137             startLogThread(read_file);\r
138             txt_log.Text = "\83G\83\93\83R\81[\83h\83\8d\83O";\r
139         }\r
140 \r
141         /// <summary>\r
142         /// Copy to Clipboard\r
143         /// </summary>\r
144         /// <param name="sender"></param>\r
145         /// <param name="e"></param>\r
146         private void btn_copy_Click(object sender, EventArgs e)\r
147         {\r
148             if (rtf_actLog.SelectedText != "")\r
149                 Clipboard.SetDataObject(rtf_actLog.SelectedText, true);\r
150             else\r
151                 Clipboard.SetDataObject(rtf_actLog.Text, true);\r
152         }\r
153 \r
154         /// <summary>\r
155         /// Updates the log window with any new data which is in the log file.\r
156         /// This is done every 5 seconds.\r
157         /// </summary>\r
158         /// <param name="state"></param>\r
159         private void autoUpdate(object state)\r
160 */\r
161         private void AutoUpdate(object state)\r
162         {\r
163             try\r
164             {\r
165                 _lastUpdate = false;\r
166                 UpdateTextFromThread();\r
167                 while (true)\r
168                 {\r
169                     if (IsHandleCreated)\r
170                     {\r
171                         if (_encodeQueue.isEncoding || _mainWin.isScanning)\r
172                             UpdateTextFromThread();\r
173                         else\r
174                         {\r
175                             // The encode may just have stoped, so, refresh the log one more time before restarting it.\r
176                             if (_lastUpdate == false)\r
177                                 UpdateTextFromThread();\r
178 \r
179                             _lastUpdate = true; // Prevents the log window from being updated when there is no encode going.\r
180                             _position = 0; // There is no encoding, so reset the log position counter to 0 so it can be reused\r
181                         }\r
182                     }\r
183                     Thread.Sleep(1000);\r
184                 }\r
185             }\r
186             catch (Exception exc)\r
187             {\r
188                 MessageBox.Show("AutoUpdate(): Exception: \n" + exc);\r
189             }\r
190         }\r
191         private void UpdateTextFromThread()\r
192         {\r
193             try\r
194             {\r
195                 String info = ReadFile();\r
196                 if (info.Contains("has exited"))\r
197                     info += "\n ############ End of Log ############## \n";\r
198 \r
199                 SetText(info);\r
200             }\r
201             catch (Exception exc)\r
202             {\r
203                 MessageBox.Show("UpdateTextFromThread(): Exception: \n" + exc);\r
204             }\r
205         }\r
206         private void SetText(string text)\r
207         {\r
208             try\r
209             {\r
210                 if (IsHandleCreated)\r
211                 {\r
212                     if (rtf_actLog.InvokeRequired)\r
213                     {\r
214                         IAsyncResult invoked = BeginInvoke(new setTextCallback(SetText), new object[] { text });\r
215                         EndInvoke(invoked);\r
216                     }\r
217                     else\r
218                         rtf_actLog.AppendText(text);\r
219                 }\r
220             }\r
221             catch (Exception exc)\r
222             {\r
223                 MessageBox.Show("SetText(): Exception: \n" + exc);\r
224             }\r
225         }\r
226         private String ReadFile()\r
227         {\r
228             String appendText = String.Empty;\r
229             try\r
230             {\r
231                 // 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
232                 // we'll need to make a copy of it.\r
233                 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
234                 string logFile = Path.Combine(logDir, _readFile);\r
235                 string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");\r
236 \r
237                 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.\r
238                 if (File.Exists(logFile2))\r
239                     File.Delete(logFile2);\r
240 \r
241                 // Copy the log file.\r
242                 if (File.Exists(logFile))\r
243                     File.Copy(logFile, logFile2, true);\r
244                 else\r
245                 {\r
246                     if (fileNotFoundQuickFix)\r
247                         return "";\r
248                     fileNotFoundQuickFix = true;\r
249                     return "\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
250                 }\r
251 \r
252                 StreamReader sr = new StreamReader(logFile2);\r
253                 string line;\r
254                 int i = 1;\r
255                 while ((line = sr.ReadLine()) != null)\r
256                 {\r
257                     if (i > _position)\r
258                     {\r
259                         appendText += line + Environment.NewLine;\r
260                         _position++;\r
261                     }\r
262                     i++;\r
263                 }\r
264                 sr.Close();\r
265                 sr.Dispose();\r
266 \r
267                 return appendText;\r
268             }\r
269             catch (Exception exc)\r
270             {\r
271                 return "Error in ReadFile() \n Unable to read the log file.\n You may have to restart HandBrake. Will try reading the file again in a few seconds... \n  Error Information: \n\n" + exc;\r
272             }\r
273         }\r
274 \r
275         protected override void OnClosing(CancelEventArgs e)\r
276         {\r
277             e.Cancel = true;\r
278             this.Hide();\r
279             base.OnClosing(e);\r
280         }\r
281 \r
282         #region User Interface\r
283         private void mnu_copy_log_Click(object sender, EventArgs e)\r
284         {\r
285             if (rtf_actLog.SelectedText != "")\r
286                 Clipboard.SetDataObject(rtf_actLog.SelectedText, true);\r
287             else\r
288                 Clipboard.SetDataObject(rtf_actLog.Text, true);\r
289         }\r
290         private void mnu_openLogFolder_Click(object sender, EventArgs e)\r
291         {\r
292             string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
293             string windir = Environment.GetEnvironmentVariable("WINDIR");\r
294             System.Diagnostics.Process prc = new System.Diagnostics.Process();\r
295             prc.StartInfo.FileName = windir + @"\explorer.exe";\r
296             prc.StartInfo.Arguments = logDir;\r
297             prc.Start();\r
298         }\r
299         private void btn_copy_Click(object sender, EventArgs e)\r
300         {\r
301             if (rtf_actLog.SelectedText != "")\r
302                 Clipboard.SetDataObject(rtf_actLog.SelectedText, true);\r
303             else\r
304                 Clipboard.SetDataObject(rtf_actLog.Text, true);\r
305         }\r
306         private void btn_scan_log_Click(object sender, EventArgs e)\r
307         {\r
308             SetLogView(true);\r
309         }\r
310         private void btn_encode_log_Click(object sender, EventArgs e)\r
311         {\r
312             SetLogView(false);\r
313         }\r
314         #endregion\r
315 \r
316         #region System Information\r
317         /// <summary>\r
318         /// Returns the total physical ram in a system\r
319         /// </summary>\r
320         /// <returns></returns>\r
321         public uint TotalPhysicalMemory()\r
322         {\r
323             Win32.MEMORYSTATUS memStatus = new Win32.MEMORYSTATUS();\r
324             Win32.GlobalMemoryStatus(ref memStatus);\r
325 \r
326             uint MemoryInfo = memStatus.dwTotalPhys;\r
327             MemoryInfo = MemoryInfo / 1024 / 1024;\r
328 \r
329             return MemoryInfo;\r
330         }\r
331 \r
332         /// <summary>\r
333         /// Get the number of CPU Cores\r
334         /// </summary>\r
335         /// <returns>Object</returns>\r
336         public Object getCpuCount()\r
337         {\r
338             RegistryKey RegKey = Registry.LocalMachine;\r
339             RegKey = RegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");\r
340             return RegKey.GetValue("ProcessorNameString");\r
341         }\r
342 \r
343         /// <summary>\r
344         /// Get the System screen size information.\r
345         /// </summary>\r
346         /// <returns>System.Windows.Forms.Scree</returns>\r
347         public Screen screenBounds()\r
348         {\r
349             return Screen.PrimaryScreen;\r
350         }\r
351         #endregion\r
352 \r
353     }\r
354 }