OSDN Git Service

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