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