OSDN Git Service

MacGui: Allow up to 320 kbps bitrate for stereo and 768 kbps for 6 channel discrete...
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmActivityWindow.cs
1 /*  frmActivityWindow.cs $\r
2     This file is part of the HandBrake source code.\r
3     Homepage: <http://handbrake.fr>.\r
4     It may be used under the terms of the GNU General Public License. */\r
5 \r
6 namespace Handbrake\r
7 {\r
8     using System;\r
9     using System.ComponentModel;\r
10     using System.Diagnostics;\r
11     using System.IO;\r
12     using System.Text;\r
13     using System.Threading;\r
14     using System.Windows.Forms;\r
15     using Functions;\r
16     using Model;\r
17     using Services;\r
18     using Timer = System.Threading.Timer;\r
19 \r
20     /// <summary>\r
21     /// The Activity Log Window\r
22     /// </summary>\r
23     public partial class frmActivityWindow : Form\r
24     {\r
25         /// <summary>\r
26         /// The current position in the log file\r
27         /// </summary>\r
28         private int position;\r
29 \r
30         /// <summary>\r
31         /// A Timer for this window\r
32         /// </summary>\r
33         private Timer windowTimer;\r
34 \r
35         /// <summary>\r
36         /// The Encode Object\r
37         /// </summary>\r
38         private Encode encode;\r
39 \r
40         /// <summary>\r
41         /// The Scan Object\r
42         /// </summary>\r
43         private Scan scan;\r
44 \r
45         /// <summary>\r
46         /// The Type of log that the window is currently dealing with\r
47         /// </summary>\r
48         private ActivityLogMode mode;\r
49 \r
50         /// <summary>\r
51         /// Initializes a new instance of the <see cref="frmActivityWindow"/> class.\r
52         /// </summary>\r
53         /// <param name="mode">\r
54         /// The mode.\r
55         /// </param>\r
56         /// <param name="encode">\r
57         /// The encode.\r
58         /// </param>\r
59         /// <param name="scan">\r
60         /// The scan.\r
61         /// </param>\r
62         public frmActivityWindow(ActivityLogMode mode, Encode encode, Scan scan)\r
63         {\r
64             InitializeComponent();\r
65 \r
66             this.encode = encode;\r
67             this.scan = scan;\r
68             this.mode = mode;\r
69             this.position = 0;\r
70         }\r
71 \r
72         /// <summary>\r
73         /// A callback function for updating the ui\r
74         /// </summary>\r
75         /// <param name="text">\r
76         /// The text.\r
77         /// </param>\r
78         private delegate void SetTextCallback(StringBuilder text);\r
79 \r
80         /// <summary>\r
81         /// Clear text callback\r
82         /// </summary>\r
83         private delegate void SetTextClearCallback();\r
84 \r
85         // Public\r
86 \r
87         /// <summary>\r
88         /// Set the window to scan mode\r
89         /// </summary>\r
90         /// <param name="setMode">\r
91         /// The set Mode.\r
92         /// </param>\r
93         public void SetMode(ActivityLogMode setMode)\r
94         {\r
95             Reset();\r
96             this.mode = setMode;\r
97             this.Text = mode == ActivityLogMode.Scan ? "Activity Window (Scan Log)" : "Activity Window (Enocde Log)";\r
98         }\r
99 \r
100         // Logging\r
101 \r
102         /// <summary>\r
103         /// On Window load, start a new timer\r
104         /// </summary>\r
105         /// <param name="sender">\r
106         /// The sender.\r
107         /// </param>\r
108         /// <param name="e">\r
109         /// The e.\r
110         /// </param>\r
111         private void NewActivityWindow_Load(object sender, EventArgs e)\r
112         {\r
113             windowTimer = new Timer(new TimerCallback(LogMonitor), null, 1000, 1000);\r
114         }\r
115 \r
116         /// <summary>\r
117         /// Append new text to the window\r
118         /// </summary>\r
119         /// <param name="n">\r
120         /// The n.\r
121         /// </param>\r
122         private void LogMonitor(object n)\r
123         {\r
124             AppendWindowText(GetLog());\r
125         }\r
126 \r
127         /// <summary>\r
128         /// New Code for getting the Activity log from the Services rather than reading a file.\r
129         /// </summary>\r
130         /// <returns>\r
131         /// The StringBuilder containing a log\r
132         /// </returns>\r
133         private StringBuilder GetLog()\r
134         {\r
135             StringBuilder appendText = new StringBuilder();\r
136             \r
137             if (this.mode == ActivityLogMode.Scan)\r
138             {\r
139                 if (scan == null || scan.ActivityLog == string.Empty)\r
140                 {\r
141                     appendText.AppendFormat("Waiting for the log to be generated ...\n");\r
142                     position = 0;\r
143                     ClearWindowText();\r
144                     PrintLogHeader();\r
145                     return appendText;\r
146                 }\r
147 \r
148                 using (StringReader reader = new StringReader(scan.ActivityLog))\r
149                 {\r
150                     LogReader(reader, appendText);\r
151                 }\r
152             }\r
153             else\r
154             {\r
155                 if (encode == null || encode.ActivityLog == string.Empty)\r
156                 {\r
157                     appendText.AppendFormat("Waiting for the log to be generated ...\n");\r
158                     position = 0;\r
159                     ClearWindowText();\r
160                     PrintLogHeader();\r
161                     return appendText;\r
162                 }\r
163 \r
164                 using (StringReader reader = new StringReader(encode.ActivityLog))\r
165                 {\r
166                     LogReader(reader, appendText);\r
167                 }\r
168             }\r
169             return appendText;\r
170         }\r
171 \r
172         /// <summary>\r
173         /// Reads the log data from a Scan or Encode object\r
174         /// </summary>\r
175         /// <param name="reader">\r
176         /// The reader.\r
177         /// </param>\r
178         /// <param name="appendText">\r
179         /// The append text.\r
180         /// </param>\r
181         private void LogReader(StringReader reader, StringBuilder appendText)\r
182         {\r
183             string line;\r
184             int i = 1;\r
185             while ((line = reader.ReadLine()) != null)\r
186             {\r
187                 if (i > position)\r
188                 {\r
189                     appendText.AppendLine(line);\r
190                     position++;\r
191                 }\r
192                 i++;\r
193             }\r
194         }\r
195 \r
196         /// <summary>\r
197         /// Append text to the RTF box\r
198         /// </summary>\r
199         /// <param name="text">\r
200         /// The text.\r
201         /// </param>\r
202         private void AppendWindowText(StringBuilder text)\r
203         {\r
204             try\r
205             {\r
206                 if (IsHandleCreated)\r
207                 {\r
208                     if (rtf_actLog.InvokeRequired)\r
209                     {\r
210                         IAsyncResult invoked = BeginInvoke(new SetTextCallback(AppendWindowText), new object[] { text });\r
211                         EndInvoke(invoked);\r
212                     }\r
213                     else\r
214                         lock (rtf_actLog)\r
215                             rtf_actLog.AppendText(text.ToString());\r
216                 }\r
217             }\r
218             catch (Exception)\r
219             {\r
220                 return;\r
221             }\r
222         }\r
223 \r
224         /// <summary>\r
225         /// Clear the contents of the log window\r
226         /// </summary>\r
227         private void ClearWindowText()\r
228         {\r
229             try\r
230             {\r
231                 if (IsHandleCreated)\r
232                 {\r
233                     if (rtf_actLog.InvokeRequired)\r
234                     {\r
235                         IAsyncResult invoked = BeginInvoke(new SetTextClearCallback(ClearWindowText));\r
236                         EndInvoke(invoked);\r
237                     }\r
238                     else\r
239                         lock (rtf_actLog)\r
240                             rtf_actLog.Clear();\r
241                 }\r
242             }\r
243             catch (Exception)\r
244             {\r
245                 return;\r
246             }\r
247         }\r
248 \r
249         /// <summary>\r
250         /// Display the log header\r
251         /// </summary>\r
252         private void PrintLogHeader()\r
253         {\r
254             try\r
255             {\r
256                 if (IsHandleCreated)\r
257                 {\r
258                     if (rtf_actLog.InvokeRequired)\r
259                     {\r
260                         IAsyncResult invoked = BeginInvoke(new SetTextClearCallback(PrintLogHeader));\r
261                         EndInvoke(invoked);\r
262                     }\r
263                     else\r
264                     {\r
265                         lock (rtf_actLog)\r
266                         {\r
267                             // Print the log header. This function will be re-implimented later. Do not delete.\r
268                             StringBuilder header = new StringBuilder();\r
269 \r
270                             header.Append(String.Format("### Windows GUI {1} {0} \n", Properties.Settings.Default.hb_build, Properties.Settings.Default.hb_version));\r
271                             header.Append(String.Format("### Running: {0} \n###\n", Environment.OSVersion));\r
272                             header.Append(String.Format("### CPU: {0} \n", SystemInfo.GetCpuCount));\r
273                             header.Append(String.Format("### Ram: {0} MB \n", SystemInfo.TotalPhysicalMemory));\r
274                             header.Append(String.Format("### Screen: {0}x{1} \n", SystemInfo.ScreenBounds.Bounds.Width, SystemInfo.ScreenBounds.Bounds.Height));\r
275                             header.Append(String.Format("### Temp Dir: {0} \n", Path.GetTempPath()));\r
276                             header.Append(String.Format("### Install Dir: {0} \n", Application.StartupPath));\r
277                             header.Append(String.Format("### Data Dir: {0} \n", Application.UserAppDataPath));\r
278                             header.Append("#########################################\n\n");\r
279 \r
280                             rtf_actLog.AppendText(header.ToString());\r
281                         }\r
282                     }\r
283                 }\r
284             }\r
285             catch (Exception)\r
286             {\r
287                 return;\r
288             }\r
289         }\r
290 \r
291         /// <summary>\r
292         /// Reset Everything\r
293         /// </summary>\r
294         private void Reset()\r
295         {\r
296             if (windowTimer != null)\r
297                 windowTimer.Dispose();\r
298             position = 0;\r
299             ClearWindowText();\r
300             PrintLogHeader();\r
301             windowTimer = new Timer(new TimerCallback(LogMonitor), null, 1000, 1000);\r
302         }\r
303 \r
304         // Menus and Buttons\r
305 \r
306         /// <summary>\r
307         /// Copy log to clipboard\r
308         /// </summary>\r
309         /// <param name="sender">\r
310         /// The sender.\r
311         /// </param>\r
312         /// <param name="e">\r
313         /// The e.\r
314         /// </param>\r
315         private void MnuCopyLogClick(object sender, EventArgs e)\r
316         {\r
317             Clipboard.SetDataObject(rtf_actLog.SelectedText != string.Empty ? rtf_actLog.SelectedText : rtf_actLog.Text, true);\r
318         }\r
319 \r
320         /// <summary>\r
321         /// Open the log folder\r
322         /// </summary>\r
323         /// <param name="sender">\r
324         /// The sender.\r
325         /// </param>\r
326         /// <param name="e">\r
327         /// The e.\r
328         /// </param>\r
329         private void MnuOpenLogFolderClick(object sender, EventArgs e)\r
330         {\r
331             string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
332             string windir = Environment.GetEnvironmentVariable("WINDIR");\r
333             Process prc = new Process\r
334                               {\r
335                                   StartInfo =\r
336                                       {\r
337                                           FileName = windir + @"\explorer.exe",\r
338                                           Arguments = logDir\r
339                                       }\r
340                               };\r
341             prc.Start();\r
342         }\r
343 \r
344         /// <summary>\r
345         /// Copy the log\r
346         /// </summary>\r
347         /// <param name="sender">\r
348         /// The sender.\r
349         /// </param>\r
350         /// <param name="e">\r
351         /// The e.\r
352         /// </param>\r
353         private void BtnCopyClick(object sender, EventArgs e)\r
354         {\r
355             Clipboard.SetDataObject(rtf_actLog.SelectedText != string.Empty ? rtf_actLog.SelectedText : rtf_actLog.Text, true);\r
356         }\r
357 \r
358         /// <summary>\r
359         /// Set scan mode\r
360         /// </summary>\r
361         /// <param name="sender">\r
362         /// The sender.\r
363         /// </param>\r
364         /// <param name="e">\r
365         /// The e.\r
366         /// </param>\r
367         private void BtnScanLogClick(object sender, EventArgs e)\r
368         {\r
369             SetMode(ActivityLogMode.Scan);\r
370         }\r
371 \r
372         /// <summary>\r
373         /// Set the encode mode\r
374         /// </summary>\r
375         /// <param name="sender">\r
376         /// The sender.\r
377         /// </param>\r
378         /// <param name="e">\r
379         /// The e.\r
380         /// </param>\r
381         private void BtnEncodeLogClick(object sender, EventArgs e)\r
382         {\r
383             SetMode(ActivityLogMode.Encode);\r
384         }\r
385 \r
386         // Overrides\r
387 \r
388         /// <summary>\r
389         /// override onclosing\r
390         /// </summary>\r
391         /// <param name="e">\r
392         /// The e.\r
393         /// </param>\r
394         protected override void OnClosing(CancelEventArgs e)\r
395         {\r
396             windowTimer.Dispose();\r
397             e.Cancel = true;\r
398             this.Dispose();\r
399             base.OnClosing(e);\r
400         }\r
401     }\r
402 }