OSDN Git Service

e23ff0aa55ae244fbb56d155cd06d12b0be4251b
[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 \r
21 namespace Handbrake\r
22 {\r
23     public partial class frmActivityWindow : Form\r
24     {\r
25 \r
26         Thread monitorFile;\r
27         String read_file;\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 \r
33         /// <summary>\r
34         /// This window should be used to display the RAW output of the handbrake CLI which is produced during an encode.\r
35         /// </summary>\r
36         /// \r
37         public frmActivityWindow(string file, frmMain fm, frmQueue fq)\r
38         {\r
39             InitializeComponent();\r
40 \r
41             mainWindow = fm;\r
42             queueWindow = fq;\r
43             read_file = file;\r
44 \r
45             // Reset some varibles\r
46             this.rtf_actLog.Text = string.Empty;\r
47             position = 0;\r
48 \r
49             string logFile = Path.Combine(Path.GetTempPath(), read_file);\r
50             if (File.Exists(logFile))\r
51             {\r
52                 // Add a header to the log file indicating that it's from the Windows GUI and display the windows version\r
53                 rtf_actLog.AppendText("### Windows GUI \n");\r
54                 rtf_actLog.AppendText(String.Format("### Running: {0} \n###\n", Environment.OSVersion.ToString()));\r
55 \r
56                 // Start a new thread to run the autoUpdate process\r
57                 monitorFile = new Thread(autoUpdate);\r
58                 monitorFile.Start();\r
59             }\r
60             else\r
61                 MessageBox.Show("The log file could not be found. Maybe you cleared your system's tempory folder or maybe you just havn't run an encode yet.", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
62 \r
63             // Handle the event of the window being disposed. This is needed to make sure HandBrake exit's cleanly.\r
64             this.Disposed += new EventHandler(forceQuit);\r
65         }\r
66 \r
67         // Ok, so, this function is called when someone closes frmMain but didn't close frmActivitWindow first.\r
68         // When you close frmMain, the activity window gets closed (disposed of) but, this doens't kill the threads that it started.\r
69         // When that thread tries to access the disposed rich text box, it causes an exception.\r
70         // Basically, this function is called when the window is disposed of, to kill the thread and close the window properly.\r
71         // This allows HandBrake to close cleanly.\r
72         private void forceQuit(object sender, EventArgs e)\r
73         {\r
74             if (monitorFile != null)\r
75                 monitorFile.Abort();\r
76 \r
77             this.Close();\r
78         }\r
79 \r
80         // Update the Activity window every 5 seconds with the latest log data.\r
81         private void autoUpdate(object state)\r
82         {\r
83             Boolean lastUpdate = false;\r
84             updateTextFromThread();\r
85             while (true)\r
86             {   \r
87                 if ((mainWindow.isEncoding() == true) || (queueWindow.isEncoding() == true))\r
88                     updateTextFromThread();\r
89                 else\r
90                 {\r
91                     // The encode may just have stoped, so, refresh the log one more time before restarting it.\r
92                     if (lastUpdate == false)\r
93                         updateTextFromThread();\r
94 \r
95                     lastUpdate = true;\r
96                     position = 0;\r
97                 }\r
98                 Thread.Sleep(5000);\r
99             }\r
100         }\r
101 \r
102         private delegate void UpdateUIHandler();\r
103         private void updateTextFromThread()\r
104         {\r
105             try\r
106             {\r
107                 if (this.InvokeRequired)\r
108                 {\r
109                     this.BeginInvoke(new UpdateUIHandler(updateTextFromThread));\r
110                     return;\r
111                 }\r
112                 // Initialize a pointer and get the log data arraylist\r
113                 ArrayList data = readFile();\r
114 \r
115                 while (position < data.Count)\r
116                 {\r
117                     rtf_actLog.AppendText(data[position].ToString());\r
118                     if (data[position].ToString().Contains("has exited"))\r
119                     {\r
120                         rtf_actLog.AppendText("\n ############ End of Encode ############## \n");\r
121                     }\r
122                     position++;\r
123                 }\r
124 \r
125                // this.rtf_actLog.SelectionStart = this.rtf_actLog.Text.Length - 1;\r
126                // this.rtf_actLog.ScrollToCaret();\r
127             }\r
128             catch (Exception exc)\r
129             {\r
130                 MessageBox.Show("An error has occured in: updateTextFromThread(). \n You may have to restart HandBrake. \n  Error Information: \n\n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
131             }\r
132         }\r
133 \r
134         private ArrayList readFile()\r
135         {\r
136             // Ok, the task here is to, Get an arraylist of log data.\r
137             // And update some global varibles which are pointers to the last displayed log line.\r
138             ArrayList logData = new ArrayList();\r
139 \r
140             try\r
141             {\r
142                 // 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
143                 // we'll need to make a copy of it.\r
144                 string logFile = Path.Combine(Path.GetTempPath(), read_file);\r
145                 string logFile2 = Path.Combine(Path.GetTempPath(), "hb_encode_log_AppReadable.dat");\r
146 \r
147                 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.\r
148                 if (File.Exists(logFile2))\r
149                     File.Delete(logFile2);\r
150 \r
151                 // Copy the log file.\r
152                 File.Copy(logFile, logFile2);\r
153 \r
154                 // Open the copied log file for reading\r
155                 StreamReader sr = new StreamReader(logFile2);\r
156                 string line = sr.ReadLine();\r
157                 while (line != null)\r
158                 {\r
159                     if (line.Trim() != "")\r
160                         logData.Add(line + System.Environment.NewLine);\r
161 \r
162                     line = sr.ReadLine();\r
163                 }\r
164                 sr.Close();\r
165                 sr.Dispose();\r
166 \r
167                 return logData;\r
168             }\r
169             catch (Exception exc)\r
170             {\r
171                 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
172             }\r
173             return null;\r
174         }\r
175 \r
176 \r
177         // Ok, We need to make sure the monitor thread is dead when we close the window.\r
178         protected override void OnClosing(CancelEventArgs e)\r
179         {\r
180             if (monitorFile != null)\r
181                 monitorFile.Abort();\r
182             e.Cancel = true;\r
183             this.Hide();\r
184             base.OnClosing(e);\r
185         }\r
186 \r
187     }\r
188 }