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.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             mainWindow = fm;\r
41             queueWindow = fq;\r
42             read_file = file;\r
43             position = 0;\r
44 \r
45             // Print the Log header in the Rich text box.\r
46             displayLogHeader();\r
47 \r
48             // Start a new thread which will montior and keep the log window up to date if required/\r
49             startLogThread(read_file);\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 \r
57             // When the window closes, we want to abort the monitor thread.\r
58             this.Disposed += new EventHandler(forceQuit);\r
59         }\r
60 \r
61         /// <summary>\r
62         /// Displays the Log header\r
63         /// </summary>\r
64         private void displayLogHeader()\r
65         {\r
66             // System Information\r
67             Functions.SystemInfo info = new Functions.SystemInfo();\r
68 \r
69             // Add a header to the log file indicating that it's from the Windows GUI and display the windows version\r
70             rtf_actLog.AppendText(String.Format("### Windows GUI {1} {0} \n", Properties.Settings.Default.hb_build, Properties.Settings.Default.hb_version));\r
71             rtf_actLog.AppendText(String.Format("### Running: {0} \n###\n", Environment.OSVersion.ToString()));\r
72             rtf_actLog.AppendText(String.Format("### CPU: {0} \n", info.getCpuCount()));\r
73             rtf_actLog.AppendText(String.Format("### Ram: {0} MB \n", info.TotalPhysicalMemory()));\r
74             rtf_actLog.AppendText(String.Format("### Screen: {0}x{1} \n", info.screenBounds().Bounds.Width, info.screenBounds().Bounds.Height));\r
75             rtf_actLog.AppendText(String.Format("### Temp Dir: {0} \n", Path.GetTempPath()));\r
76             rtf_actLog.AppendText(String.Format("### Install Dir: {0} \n", Application.StartupPath));\r
77             rtf_actLog.AppendText(String.Format("### Data Dir: {0} \n", Application.UserAppDataPath));\r
78             rtf_actLog.AppendText("#########################################\n\n");\r
79         }\r
80 \r
81         /// <summary>\r
82         /// Starts a new thread which runs the autoUpdate function.\r
83         /// </summary>\r
84         /// <param name="file"> File which will be used to populate the Rich text box.</param>\r
85         private void startLogThread(string file)\r
86         {\r
87             string logFile = Path.Combine(Path.GetTempPath(), file);\r
88             if (File.Exists(logFile))\r
89             {\r
90                 // Start a new thread to run the autoUpdate process\r
91                 monitor = new Thread(autoUpdate);\r
92                 monitor.IsBackground = true;\r
93                 monitor.Start();\r
94             }\r
95             else\r
96                 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
97         }\r
98 \r
99         /// <summary>\r
100         /// Change the log file to be displayed to hb_encode_log.dat\r
101         /// </summary>\r
102         /// <param name="sender"></param>\r
103         /// <param name="e"></param>\r
104         private void btn_scan_log_Click(object sender, EventArgs e)\r
105         {\r
106             if (monitor != null)\r
107                 monitor.Abort();\r
108 \r
109             rtf_actLog.Clear();\r
110             read_file = "dvdinfo.dat";\r
111             displayLogHeader();\r
112             startLogThread(read_file);\r
113             txt_log.Text = "Scan Log";\r
114         }\r
115 \r
116         /// <summary>\r
117         /// Change the log file to be displayed to dvdinfo.dat\r
118         /// </summary>\r
119         /// <param name="sender"></param>\r
120         /// <param name="e"></param>\r
121         private void btn_encode_log_Click(object sender, EventArgs e)\r
122         {\r
123             if (monitor != null)\r
124                 monitor.Abort();\r
125 \r
126             rtf_actLog.Clear();\r
127             read_file = "hb_encode_log.dat";\r
128             position = 0;\r
129             displayLogHeader();\r
130             startLogThread(read_file);\r
131             txt_log.Text = "Encode Log";\r
132         }\r
133 \r
134         /// <summary>\r
135         /// Copy to Clipboard\r
136         /// </summary>\r
137         /// <param name="sender"></param>\r
138         /// <param name="e"></param>\r
139         private void btn_copy_Click(object sender, EventArgs e)\r
140         {\r
141             Clipboard.SetDataObject(rtf_actLog.Text, true);\r
142         }\r
143 \r
144         /// <summary>\r
145         /// Updates the log window with any new data which is in the log file.\r
146         /// This is done every 5 seconds.\r
147         /// </summary>\r
148         /// <param name="state"></param>\r
149         private void autoUpdate(object state)\r
150         {\r
151             Boolean lastUpdate = false;\r
152             updateTextFromThread();\r
153             while (true)\r
154             {\r
155                 if ((mainWindow.isEncoding() == true) || (queueWindow.isEncoding() == true))\r
156                     updateTextFromThread();\r
157                 else\r
158                 {\r
159                     // The encode may just have stoped, so, refresh the log one more time before restarting it.\r
160                     if (lastUpdate == false)\r
161                         updateTextFromThread();\r
162 \r
163                     lastUpdate = true; // Prevents the log window from being updated when there is no encode going.\r
164                     position = 0; // There is no encoding, so reset the log position counter to 0 so it can be reused\r
165                 }\r
166                 Thread.Sleep(5000);\r
167             }\r
168         }\r
169 \r
170         /// <summary>\r
171         /// Finds any new text in the log file and calls a funciton to display this new text.\r
172         /// </summary>\r
173         private void updateTextFromThread()\r
174         {\r
175             string text = "";\r
176             List<string> data = readFile();\r
177             int count = data.Count;\r
178 \r
179             while (position < count)\r
180             {\r
181                 text = data[position].ToString();\r
182                 if (data[position].ToString().Contains("has exited"))\r
183                     text = "\n ############ End of Log ############## \n";\r
184                 position++;\r
185 \r
186                 SetText(text);\r
187             }\r
188         }\r
189 \r
190         /// <summary>\r
191         /// Updates the rich text box with anything in the string text.\r
192         /// </summary>\r
193         /// <param name="text"></param>\r
194         private void SetText(string text)\r
195         {\r
196             // InvokeRequired required compares the thread ID of the\r
197             // calling thread to the thread ID of the creating thread.\r
198             // If these threads are different, it returns true.\r
199             if (this.rtf_actLog.InvokeRequired)\r
200             {\r
201                 SetTextCallback d = new SetTextCallback(SetText);\r
202                 this.Invoke(d, new object[] { text });\r
203             }\r
204             else\r
205             {\r
206                 this.rtf_actLog.AppendText(text);\r
207             }\r
208         }\r
209 \r
210         /// <summary>\r
211         /// Read the log file, and store the data in a List.\r
212         /// </summary>\r
213         /// <returns></returns>\r
214         private List<string> readFile()\r
215         {\r
216             // Ok, the task here is to, Get an arraylist of log data.\r
217             // And update some global varibles which are pointers to the last displayed log line.\r
218             List<string> logData = new List<string>();\r
219 \r
220             try\r
221             {\r
222                 // 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
223                 // we'll need to make a copy of it.\r
224                 string logFile = Path.Combine(Path.GetTempPath(), read_file);\r
225                 string logFile2 = Path.Combine(Path.GetTempPath(), "hb_encode_log_AppReadable.dat");\r
226 \r
227                 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.\r
228                 if (File.Exists(logFile2))\r
229                     File.Delete(logFile2);\r
230 \r
231                 // Copy the log file.\r
232                 File.Copy(logFile, logFile2);\r
233 \r
234                 // Open the copied log file for reading\r
235                 StreamReader sr = new StreamReader(logFile2);\r
236                 string line = sr.ReadLine();\r
237                 while (line != null)\r
238                 {\r
239                     if (line.Trim() != "")\r
240                         logData.Add(line + System.Environment.NewLine);\r
241 \r
242                     line = sr.ReadLine();\r
243                 }\r
244                 sr.Close();\r
245                 sr.Dispose();\r
246 \r
247                 return logData;\r
248             }\r
249             catch (Exception exc)\r
250             {\r
251                 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
252             }\r
253             return null;\r
254         }\r
255 \r
256         /// <summary>\r
257         /// Kills the montior thead when the window is disposed of.\r
258         /// </summary>\r
259         /// <param name="sender"></param>\r
260         /// <param name="e"></param>\r
261         private void forceQuit(object sender, EventArgs e)\r
262         {\r
263             if (monitor != null)\r
264                 monitor.Abort();\r
265 \r
266             this.Close();\r
267         }\r
268     }\r
269 }