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