OSDN Git Service

f4f6b0442fb08c5a2ca1ea1e0f1c164aa1d34323
[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         String read_file;\r
26         Thread monitor;\r
27         frmMain mainWindow;\r
28         frmQueue queueWindow;\r
29         int position = 0;  // Position in the arraylist reached by the current log output in the rtf box.\r
30 \r
31         /// <summary>\r
32         /// This window should be used to display the RAW output of the handbrake CLI which is produced during an encode.\r
33         /// </summary>\r
34         /// \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             // System Information\r
46             Functions.SystemInfo info = new Functions.SystemInfo();\r
47 \r
48             // Add a header to the log file indicating that it's from the Windows GUI and display the windows version\r
49             rtf_actLog.AppendText("### Windows GUI \n");\r
50             rtf_actLog.AppendText(String.Format("### Running: {0} \n###\n", Environment.OSVersion.ToString()));\r
51             rtf_actLog.AppendText(String.Format("### CPU: {0} \n", info.getCpuCount()));\r
52             rtf_actLog.AppendText(String.Format("### Ram: {0} MB \n", info.TotalPhysicalMemory()));\r
53             rtf_actLog.AppendText(String.Format("### Screen: {0}x{1} \n", info.screenBounds().Bounds.Width, info.screenBounds().Bounds.Height));\r
54             rtf_actLog.AppendText(String.Format("### Temp Dir: {0} \n", Path.GetTempPath()));\r
55             rtf_actLog.AppendText(String.Format("### Install Dir: {0} \n", Application.StartupPath));\r
56             rtf_actLog.AppendText(String.Format("### Data Dir: {0} \n", Application.UserAppDataPath));\r
57             rtf_actLog.AppendText("#########################################\n\n");\r
58 \r
59             string logFile = Path.Combine(Path.GetTempPath(), read_file);\r
60             if (File.Exists(logFile))\r
61             {\r
62                 // Start a new thread to run the autoUpdate process\r
63                 monitor = new Thread(autoUpdate);\r
64                 monitor.IsBackground = true;\r
65                 monitor.Start();\r
66             }\r
67             else\r
68                 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
69         \r
70             // When the window closes, we want to abort the monitor thread.\r
71             this.Disposed += new EventHandler(forceQuit);\r
72         }\r
73 \r
74         private void forceQuit(object sender, EventArgs e)\r
75         {\r
76             if (monitor != null)\r
77                 monitor.Abort();\r
78 \r
79             this.Close();\r
80         }\r
81 \r
82         // Update the Activity window every 5 seconds with the latest log data.\r
83         private void autoUpdate(object state)\r
84         {\r
85             Boolean lastUpdate = false;\r
86             updateTextFromThread();\r
87             while (true)\r
88             {\r
89                 if ((mainWindow.isEncoding() == true) || (queueWindow.isEncoding() == true))\r
90                     updateTextFromThread();\r
91                 else\r
92                 {\r
93                     // The encode may just have stoped, so, refresh the log one more time before restarting it.\r
94                     if (lastUpdate == false)\r
95                         updateTextFromThread();\r
96 \r
97                     lastUpdate = true; // Prevents the log window from being updated when there is no encode going.\r
98                     position = 0; // There is no encoding, so reset the log position counter to 0 so it can be reused\r
99                 }\r
100                 Thread.Sleep(5000);\r
101             }\r
102         }\r
103 \r
104         private void updateTextFromThread()\r
105         {\r
106             string text = "";\r
107             List<string> data = readFile();\r
108             int count = data.Count;\r
109 \r
110             while (position < count)\r
111             {\r
112                 text = data[position].ToString();\r
113                 if (data[position].ToString().Contains("has exited"))\r
114                     text = "\n ############ End of Encode ############## \n";\r
115                 position++;\r
116 \r
117                 SetText(text);\r
118             }\r
119         }\r
120         delegate void SetTextCallback(string text);\r
121         private void SetText(string text)\r
122         {\r
123             // InvokeRequired required compares the thread ID of the\r
124             // calling thread to the thread ID of the creating thread.\r
125             // If these threads are different, it returns true.\r
126             if (this.rtf_actLog.InvokeRequired)\r
127             {\r
128                 SetTextCallback d = new SetTextCallback(SetText);\r
129                 this.Invoke(d, new object[] { text });\r
130             }\r
131             else\r
132             {\r
133                 this.rtf_actLog.AppendText(text);\r
134             }\r
135         }\r
136 \r
137         private List<string> readFile()\r
138         {\r
139             // Ok, the task here is to, Get an arraylist of log data.\r
140             // And update some global varibles which are pointers to the last displayed log line.\r
141             List<string> logData = new List<string>();\r
142 \r
143             try\r
144             {\r
145                 // 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
146                 // we'll need to make a copy of it.\r
147                 string logFile = Path.Combine(Path.GetTempPath(), read_file);\r
148                 string logFile2 = Path.Combine(Path.GetTempPath(), "hb_encode_log_AppReadable.dat");\r
149 \r
150                 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.\r
151                 if (File.Exists(logFile2))\r
152                     File.Delete(logFile2);\r
153 \r
154                 // Copy the log file.\r
155                 File.Copy(logFile, logFile2);\r
156 \r
157                 // Open the copied log file for reading\r
158                 StreamReader sr = new StreamReader(logFile2);\r
159                 string line = sr.ReadLine();\r
160                 while (line != null)\r
161                 {\r
162                     if (line.Trim() != "")\r
163                         logData.Add(line + System.Environment.NewLine);\r
164 \r
165                     line = sr.ReadLine();\r
166                 }\r
167                 sr.Close();\r
168                 sr.Dispose();\r
169 \r
170                 return logData;\r
171             }\r
172             catch (Exception exc)\r
173             {\r
174                 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
175             }\r
176             return null;\r
177         }\r
178 \r
179     }\r
180 }