OSDN Git Service

import 0.9.3
[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.Collections;\r
9 using System.Collections.Generic;\r
10 using System.ComponentModel;\r
11 using System.Data;\r
12 using System.Drawing;\r
13 using System.Text;\r
14 using System.Windows.Forms;\r
15 using System.IO;\r
16 using System.Threading;\r
17 using System.Diagnostics;\r
18 using System.Runtime.InteropServices;\r
19 \r
20 \r
21 namespace Handbrake\r
22 {\r
23     public partial class frmActivityWindow : Form\r
24     {\r
25         delegate void SetTextCallback(string text);\r
26         String read_file;\r
27         Thread monitor;\r
28         frmMain mainWindow;\r
29         frmQueue queueWindow;\r
30         int position = 0;  // Position in the arraylist reached by the current log output in the rtf box.\r
31 \r
32         /// <summary>\r
33         /// This window should be used to display the RAW output of the handbrake CLI which is produced during an encode.\r
34         /// </summary>\r
35         public frmActivityWindow(string file, frmMain fm, frmQueue fq)\r
36         {\r
37             InitializeComponent();\r
38             this.rtf_actLog.Text = string.Empty;\r
39 \r
40             // When the window closes, we want to abort the monitor thread.\r
41             this.Disposed += new EventHandler(forceQuit);\r
42 \r
43             mainWindow = fm;\r
44             queueWindow = fq;\r
45             read_file = file;\r
46             position = 0;\r
47 \r
48             // Print the Log header in the Rich text box.\r
49             displayLogHeader();\r
50 \r
51             if (file == "dvdinfo.dat")\r
52                 txt_log.Text = "Scan Log";\r
53             else if (file == "hb_encode_log.dat")\r
54                 txt_log.Text = "Encode Log";\r
55 \r
56             // Start a new thread which will montior and keep the log window up to date if required/\r
57             startLogThread(read_file);            \r
58         }\r
59 \r
60         /// <summary>\r
61         /// Displays the Log header\r
62         /// </summary>\r
63         private void displayLogHeader()\r
64         {\r
65             // System Information\r
66             Functions.SystemInfo info = new Functions.SystemInfo();\r
67 \r
68             // Add a header to the log file indicating that it's from the Windows GUI and display the windows version\r
69             rtf_actLog.AppendText(String.Format("### Windows GUI {1} {0} \n", Properties.Settings.Default.hb_build, Properties.Settings.Default.hb_version));\r
70             rtf_actLog.AppendText(String.Format("### Running: {0} \n###\n", Environment.OSVersion.ToString()));\r
71             rtf_actLog.AppendText(String.Format("### CPU: {0} \n", info.getCpuCount()));\r
72             rtf_actLog.AppendText(String.Format("### Ram: {0} MB \n", info.TotalPhysicalMemory()));\r
73             rtf_actLog.AppendText(String.Format("### Screen: {0}x{1} \n", info.screenBounds().Bounds.Width, info.screenBounds().Bounds.Height));\r
74             rtf_actLog.AppendText(String.Format("### Temp Dir: {0} \n", Path.GetTempPath()));\r
75             rtf_actLog.AppendText(String.Format("### Install Dir: {0} \n", Application.StartupPath));\r
76             rtf_actLog.AppendText(String.Format("### Data Dir: {0} \n", Application.UserAppDataPath));\r
77             rtf_actLog.AppendText("#########################################\n\n");\r
78         }\r
79 \r
80         /// <summary>\r
81         /// Starts a new thread which runs the autoUpdate function.\r
82         /// </summary>\r
83         /// <param name="file"> File which will be used to populate the Rich text box.</param>\r
84         private void startLogThread(string file)\r
85         {\r
86             try\r
87             {\r
88                 string logFile = Path.Combine(Path.GetTempPath(), file);\r
89                 if (File.Exists(logFile))\r
90                 {\r
91                     // Start a new thread to run the autoUpdate process\r
92                     monitor = new Thread(autoUpdate);\r
93                     monitor.IsBackground = true;\r
94                     monitor.Start();\r
95                 }\r
96                 else\r
97                     rtf_actLog.AppendText("\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
98 \r
99             }\r
100             catch (Exception exc)\r
101             {\r
102                 MessageBox.Show("startLogThread(): Exception: \n" + exc);\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 = "Scan Log";\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 = "Encode Log";\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             try\r
162             {\r
163                 Boolean lastUpdate = false;\r
164                 updateTextFromThread();\r
165                 while (true)\r
166                 {\r
167                     if ((mainWindow.isEncoding() == true) || (queueWindow.isEncoding() == true))\r
168                         updateTextFromThread();\r
169                     else\r
170                     {\r
171                         // The encode may just have stoped, so, refresh the log one more time before restarting it.\r
172                         if (lastUpdate == false)\r
173                             updateTextFromThread();\r
174 \r
175                         lastUpdate = true; // Prevents the log window from being updated when there is no encode going.\r
176                         position = 0; // There is no encoding, so reset the log position counter to 0 so it can be reused\r
177                     }\r
178                     Thread.Sleep(5000);\r
179                 }\r
180             }\r
181             catch (ThreadAbortException)\r
182             {\r
183                 // Do Nothing. This is needed since we run thread.abort(). \r
184                 // Should probably find a better way of making this work at some point.\r
185             }\r
186             catch (Exception exc)\r
187             {\r
188                 MessageBox.Show("autoUpdate(): Exception: \n" + exc);\r
189             }\r
190         }\r
191 \r
192         /// <summary>\r
193         /// Finds any new text in the log file and calls a funciton to display this new text.\r
194         /// </summary>\r
195         private void updateTextFromThread()\r
196         {\r
197             try\r
198             {\r
199                 string text = "";\r
200                 List<string> data = readFile();\r
201                 int count = data.Count;\r
202 \r
203                 while (position < count)\r
204                 {\r
205                     text = data[position].ToString();\r
206                     if (data[position].ToString().Contains("has exited"))\r
207                         text = "\n ############ End of Log ############## \n";\r
208                     position++;\r
209 \r
210                     SetText(text);\r
211                 }\r
212             }\r
213             catch (Exception exc)\r
214             {\r
215                 MessageBox.Show("updateTextFromThread(): Exception: \n" + exc);\r
216             }\r
217         }\r
218 \r
219         /// <summary>\r
220         /// Updates the rich text box with anything in the string text.\r
221         /// </summary>\r
222         /// <param name="text"></param>\r
223         private void SetText(string text)\r
224         {\r
225             try\r
226             {\r
227                 // InvokeRequired required compares the thread ID of the\r
228                 // calling thread to the thread ID of the creating thread.\r
229                 // If these threads are different, it returns true.\r
230                 if (this.IsHandleCreated) // Make sure the windows has a handle before doing anything\r
231                 {\r
232                     if (this.rtf_actLog.InvokeRequired)\r
233                     {\r
234                         SetTextCallback d = new SetTextCallback(SetText);\r
235                         this.Invoke(d, new object[] { text });\r
236                     }\r
237                     else\r
238                         this.rtf_actLog.AppendText(text);\r
239                 }\r
240             }\r
241             catch (Exception exc)\r
242             {\r
243                 MessageBox.Show("SetText(): Exception: \n" + exc);\r
244             }\r
245         }\r
246 \r
247         /// <summary>\r
248         /// Read the log file, and store the data in a List.\r
249         /// </summary>\r
250         /// <returns></returns>\r
251         private List<string> readFile()\r
252         {\r
253             // Ok, the task here is to, Get an arraylist of log data.\r
254             // And update some global varibles which are pointers to the last displayed log line.\r
255             List<string> logData = new List<string>();\r
256 \r
257             try\r
258             {\r
259                 // hb_encode_log.dat 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
260                 // we'll need to make a copy of it.\r
261                 string logFile = Path.Combine(Path.GetTempPath(), read_file);\r
262                 string logFile2 = Path.Combine(Path.GetTempPath(), "hb_encode_log_AppReadable.dat");\r
263 \r
264                 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.\r
265                 if (File.Exists(logFile2))\r
266                     File.Delete(logFile2);\r
267 \r
268                 // Copy the log file.\r
269                 File.Copy(logFile, logFile2);\r
270 \r
271                 // Open the copied log file for reading\r
272                 StreamReader sr = new StreamReader(logFile2);\r
273                 string line = sr.ReadLine();\r
274                 while (line != null)\r
275                 {\r
276                     if (line.Trim() != "")\r
277                         logData.Add(line + System.Environment.NewLine);\r
278 \r
279                     line = sr.ReadLine();\r
280                 }\r
281                 sr.Close();\r
282                 sr.Dispose();\r
283 \r
284                 return logData;\r
285             }\r
286             catch (Exception exc)\r
287             {\r
288                 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.ToString(), "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
289             }\r
290             return null;\r
291         }\r
292 \r
293         /// <summary>\r
294         /// Kills the montior thead when the window is disposed of.\r
295         /// </summary>\r
296         /// <param name="sender"></param>\r
297         /// <param name="e"></param>\r
298         private void forceQuit(object sender, EventArgs e)\r
299         {\r
300             if (monitor != null)\r
301             {\r
302                 while (monitor.IsAlive)\r
303                     monitor.Abort();\r
304             }\r
305 \r
306             this.Close();\r
307         }\r
308 \r
309         private void copyToolStripMenuItem_Click(object sender, EventArgs e)\r
310         {\r
311             if (rtf_actLog.SelectedText != "")\r
312                 Clipboard.SetDataObject(rtf_actLog.SelectedText, true);\r
313             else\r
314                 Clipboard.SetDataObject(rtf_actLog.Text, true);\r
315         }\r
316     }\r
317 }