OSDN Git Service

4864df680a7f6e76f0fe1a087c117a72df327acb
[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.Generic;\r
9 using System.ComponentModel;\r
10 using System.Data;\r
11 using System.Drawing;\r
12 using System.Text;\r
13 using System.Windows.Forms;\r
14 using System.IO;\r
15 using System.Threading;\r
16 \r
17 \r
18 namespace Handbrake\r
19 {\r
20     public partial class frmActivityWindow : Form\r
21     {\r
22         /// <summary>\r
23         /// This window should be used to display the RAW output of the handbrake CLI which is produced during an encode.\r
24         /// </summary>\r
25         /// \r
26         Thread monitorFile;\r
27         public frmActivityWindow()\r
28         {\r
29             InitializeComponent();\r
30             this.rtf_actLog.Text = string.Empty;\r
31             monitorFile = new Thread(autoUpdate);\r
32 \r
33         }\r
34         \r
35         private void btn_close_Click(object sender, EventArgs e)\r
36         {\r
37             monitorFile.Abort();\r
38             this.Hide();\r
39         }\r
40 \r
41         private void frmActivityWindow_Load(object sender, EventArgs e)\r
42         {\r
43             this.rtf_actLog.Text = string.Empty;\r
44             rtf_actLog.Text = readFile();\r
45    \r
46             monitorFile.Start();\r
47         }\r
48 \r
49         private void autoUpdate(object state)\r
50         {\r
51             while (true)\r
52             {\r
53                 updateTextFromThread();\r
54                 Thread.Sleep(3000);\r
55             }\r
56         }\r
57         \r
58         private delegate void UpdateUIHandler();\r
59         private void updateTextFromThread()\r
60         {\r
61             if (this.InvokeRequired)\r
62             {\r
63                 this.BeginInvoke(new UpdateUIHandler(updateTextFromThread));\r
64                 return;\r
65             }\r
66             rtf_actLog.Text = readFile();\r
67             this.rtf_actLog.SelectionStart = this.rtf_actLog.Text.Length -1;\r
68             this.rtf_actLog.ScrollToCaret();\r
69 \r
70             if (rtf_actLog.Text.Contains("HandBrake has exited."))\r
71                 monitorFile.Abort();\r
72         }\r
73 \r
74         private string readFile()\r
75         {\r
76             string log = "";\r
77             try\r
78             {\r
79                 // hb_encode_log.dat is the primary log file. Since .NET can't read this file whilst the CLI is outputing to it,\r
80                 // we'll need to make a copy of it.\r
81                 string logFile = Path.Combine(Path.GetTempPath(), "hb_encode_log.dat");\r
82                 string logFile2 = Path.Combine(Path.GetTempPath(), "hb_encode_log_AppReadable.dat");\r
83 \r
84                 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.\r
85                 if (File.Exists(logFile2))\r
86                     File.Delete(logFile2);\r
87 \r
88                 // Copy the log file.\r
89                 File.Copy(logFile, logFile2);\r
90 \r
91                 // Begin processing the log file.\r
92                 StreamReader sr = new StreamReader(logFile2);\r
93                 string line = sr.ReadLine();\r
94                 while (line != null)\r
95                 {\r
96                     log = log + (line + System.Environment.NewLine);\r
97                     line = sr.ReadLine();\r
98                 }\r
99                 sr.Close();\r
100             }\r
101             catch (Exception exc)\r
102             {\r
103                 MessageBox.Show("An Error has occured! \n\n" + exc.ToString(), "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
104             }\r
105 \r
106             return log;\r
107         }\r
108 \r
109         private void btn_copy_Click(object sender, EventArgs e)\r
110         {\r
111             if (rtf_actLog.Text != "")\r
112                 Clipboard.SetText(rtf_actLog.Text, TextDataFormat.Rtf);\r
113         }\r
114 \r
115         private void btn_refresh_Click(object sender, EventArgs e)\r
116         {\r
117             rtf_actLog.Clear();\r
118             rtf_actLog.Text = readFile();\r
119         }\r
120 \r
121 \r
122     }\r
123 }