OSDN Git Service

import 0.9.4
[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             if (file == "last_scan_log.txt")\r
39                 SetLogView(true);\r
40             else\r
41                 SetLogView(false);\r
42 \r
43             // Start a new thread which will montior and keep the log window up to date if required/\r
44             try\r
45             {\r
46                 Thread monitor = new Thread(AutoUpdate) { IsBackground = true };\r
47                 monitor.Start();\r
48             }\r
49             catch (Exception exc)\r
50             {\r
51                 MessageBox.Show("startLogThread(): Exception: \n" + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
52             }\r
53         }\r
54 \r
55         /// <summary>\r
56         /// Set the view which the Log window displays.\r
57         /// Scan = true;\r
58         /// Encode = false;\r
59         /// </summary>\r
60         /// <param name="scan">Boolean. Scan = true</param>\r
61         public void SetLogView(Boolean scan)\r
62         {\r
63             // Reset\r
64             _position = 0;\r
65             rtf_actLog.Text = String.Empty;\r
66 \r
67             // Print the log header\r
68             rtf_actLog.AppendText(String.Format("### Windows GUI {1} {0} \n", Properties.Settings.Default.hb_build, Properties.Settings.Default.hb_version));\r
69             rtf_actLog.AppendText(String.Format("### Running: {0} \n###\n", Environment.OSVersion));\r
70             rtf_actLog.AppendText(String.Format("### CPU: {0} \n", getCpuCount()));\r
71             rtf_actLog.AppendText(String.Format("### Ram: {0} MB \n", TotalPhysicalMemory()));\r
72             rtf_actLog.AppendText(String.Format("### Screen: {0}x{1} \n", screenBounds().Bounds.Width, screenBounds().Bounds.Height));\r
73             rtf_actLog.AppendText(String.Format("### Temp Dir: {0} \n", Path.GetTempPath()));\r
74             rtf_actLog.AppendText(String.Format("### Install Dir: {0} \n", Application.StartupPath));\r
75             rtf_actLog.AppendText(String.Format("### Data Dir: {0} \n", Application.UserAppDataPath));\r
76             rtf_actLog.AppendText("#########################################\n\n");\r
77 \r
78             // Seutp the log file\r
79             if (scan)\r
80             {\r
81                 txt_log.Text = "Scan Log";\r
82                 _readFile = "last_scan_log.txt";\r
83             }\r
84             else\r
85             {\r
86                 _readFile = "last_encode_log.txt";\r
87                 txt_log.Text = "Encode Log";\r
88                 if (_encodeQueue.isEncoding)\r
89                     if ((!_encodeQueue.LastEncode.IsEmpty) && _encodeQueue.LastEncode.Query != String.Empty)\r
90                     {\r
91                         rtf_actLog.AppendText("### CLI Query: " + _encodeQueue.LastEncode.Query + "\n");\r
92                         rtf_actLog.AppendText("### Custom Query: " + _encodeQueue.LastEncode.CustomQuery + "\n\n");\r
93                         rtf_actLog.AppendText("#########################################\n\n");\r
94                     }\r
95             }\r
96             _lastUpdate = false;\r
97         }\r
98 \r
99         private void AutoUpdate(object state)\r
100         {\r
101             try\r
102             {\r
103                 _lastUpdate = false;\r
104                 UpdateTextFromThread();\r
105                 while (true)\r
106                 {\r
107                     if (IsHandleCreated)\r
108                     {\r
109                         if (_encodeQueue.isEncoding || _mainWin.isScanning)\r
110                             UpdateTextFromThread();\r
111                         else\r
112                         {\r
113                             // The encode may just have stoped, so, refresh the log one more time before restarting it.\r
114                             if (_lastUpdate == false)\r
115                                 UpdateTextFromThread();\r
116 \r
117                             _lastUpdate = true; // Prevents the log window from being updated when there is no encode going.\r
118                             _position = 0; // There is no encoding, so reset the log position counter to 0 so it can be reused\r
119                         }\r
120                     }\r
121                     Thread.Sleep(1000);\r
122                 }\r
123             }\r
124             catch (Exception exc)\r
125             {\r
126                 MessageBox.Show("AutoUpdate(): Exception: \n" + exc);\r
127             }\r
128         }\r
129         private void UpdateTextFromThread()\r
130         {\r
131             try\r
132             {\r
133                 String info = ReadFile();\r
134                 if (info.Contains("has exited"))\r
135                     info += "\n ############ End of Log ############## \n";\r
136 \r
137                 SetText(info);\r
138             }\r
139             catch (Exception exc)\r
140             {\r
141                 MessageBox.Show("UpdateTextFromThread(): Exception: \n" + exc);\r
142             }\r
143         }\r
144         private void SetText(string 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(SetText), new object[] { text });\r
153                         EndInvoke(invoked);\r
154                     }\r
155                     else\r
156                         rtf_actLog.AppendText(text);\r
157                 }\r
158             }\r
159             catch (Exception exc)\r
160             {\r
161                 MessageBox.Show("SetText(): Exception: \n" + exc);\r
162             }\r
163         }\r
164         private String ReadFile()\r
165         {\r
166             String appendText = String.Empty;\r
167             try\r
168             {\r
169                 // 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
170                 // we'll need to make a copy of it.\r
171                 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
172                 string logFile = Path.Combine(logDir, _readFile);\r
173                 string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");\r
174 \r
175                 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.\r
176                 if (File.Exists(logFile2))\r
177                     File.Delete(logFile2);\r
178 \r
179                 // Copy the log file.\r
180                 if (File.Exists(logFile))\r
181                     File.Copy(logFile, logFile2, true);\r
182                 else\r
183                 {\r
184                     if (fileNotFoundQuickFix)\r
185                         return "";\r
186                     fileNotFoundQuickFix = true;\r
187                     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
188                 }\r
189 \r
190                 StreamReader sr = new StreamReader(logFile2);\r
191                 string line;\r
192                 int i = 1;\r
193                 while ((line = sr.ReadLine()) != null)\r
194                 {\r
195                     if (i > _position)\r
196                     {\r
197                         appendText += line + Environment.NewLine;\r
198                         _position++;\r
199                     }\r
200                     i++;\r
201                 }\r
202                 sr.Close();\r
203                 sr.Dispose();\r
204 \r
205                 return appendText;\r
206             }\r
207             catch (Exception exc)\r
208             {\r
209                 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
210             }\r
211         }\r
212 \r
213         protected override void OnClosing(CancelEventArgs e)\r
214         {\r
215             e.Cancel = true;\r
216             this.Hide();\r
217             base.OnClosing(e);\r
218         }\r
219 \r
220         #region User Interface\r
221         private void mnu_copy_log_Click(object sender, EventArgs e)\r
222         {\r
223             if (rtf_actLog.SelectedText != "")\r
224                 Clipboard.SetDataObject(rtf_actLog.SelectedText, true);\r
225             else\r
226                 Clipboard.SetDataObject(rtf_actLog.Text, true);\r
227         }\r
228         private void mnu_openLogFolder_Click(object sender, EventArgs e)\r
229         {\r
230             string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
231             string windir = Environment.GetEnvironmentVariable("WINDIR");\r
232             System.Diagnostics.Process prc = new System.Diagnostics.Process();\r
233             prc.StartInfo.FileName = windir + @"\explorer.exe";\r
234             prc.StartInfo.Arguments = logDir;\r
235             prc.Start();\r
236         }\r
237         private void btn_copy_Click(object sender, EventArgs e)\r
238         {\r
239             if (rtf_actLog.SelectedText != "")\r
240                 Clipboard.SetDataObject(rtf_actLog.SelectedText, true);\r
241             else\r
242                 Clipboard.SetDataObject(rtf_actLog.Text, true);\r
243         }\r
244         private void btn_scan_log_Click(object sender, EventArgs e)\r
245         {\r
246             SetLogView(true);\r
247         }\r
248         private void btn_encode_log_Click(object sender, EventArgs e)\r
249         {\r
250             SetLogView(false);\r
251         }\r
252         #endregion\r
253 \r
254         #region System Information\r
255         /// <summary>\r
256         /// Returns the total physical ram in a system\r
257         /// </summary>\r
258         /// <returns></returns>\r
259         public uint TotalPhysicalMemory()\r
260         {\r
261             Win32.MEMORYSTATUS memStatus = new Win32.MEMORYSTATUS();\r
262             Win32.GlobalMemoryStatus(ref memStatus);\r
263 \r
264             uint MemoryInfo = memStatus.dwTotalPhys;\r
265             MemoryInfo = MemoryInfo / 1024 / 1024;\r
266 \r
267             return MemoryInfo;\r
268         }\r
269 \r
270         /// <summary>\r
271         /// Get the number of CPU Cores\r
272         /// </summary>\r
273         /// <returns>Object</returns>\r
274         public Object getCpuCount()\r
275         {\r
276             RegistryKey RegKey = Registry.LocalMachine;\r
277             RegKey = RegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");\r
278             return RegKey.GetValue("ProcessorNameString");\r
279         }\r
280 \r
281         /// <summary>\r
282         /// Get the System screen size information.\r
283         /// </summary>\r
284         /// <returns>System.Windows.Forms.Scree</returns>\r
285         public Screen screenBounds()\r
286         {\r
287             return Screen.PrimaryScreen;\r
288         }\r
289         #endregion\r
290 \r
291     }\r
292 }