OSDN Git Service

6ec70d7dce3bc017a4f82d72e15eb63287187b2d
[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 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 \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 \r
39             mainWindow = fm;\r
40             queueWindow = fq;\r
41 \r
42             this.rtf_actLog.Text = string.Empty;\r
43 \r
44             read_file = file;\r
45 \r
46             string logFile = Path.Combine(Path.GetTempPath(), read_file);\r
47             if (File.Exists(logFile))\r
48             {\r
49                 if (read_file == "dvdinfo.dat") // No need to refresh the window if we are viwing dvdinfo.dat\r
50                     updateTextFromThread();\r
51                 else // however, we should refresh when reading the encode log file.\r
52                 {\r
53                     monitorFile = new Thread(autoUpdate);\r
54                     monitorFile.Start();\r
55                 }\r
56             }\r
57             else\r
58                 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
59         }\r
60 \r
61         // Update the Activity window every 5 seconds with the latest log data.\r
62         private void autoUpdate(object state)\r
63         {\r
64             updateTextFromThread();\r
65             while (true)\r
66             {\r
67                 if ((mainWindow.isEncoding() == true) || (queueWindow.isEncoding() == true))\r
68                     updateTextFromThread();\r
69                 Thread.Sleep(5000);\r
70             }\r
71         }\r
72 \r
73         private delegate void UpdateUIHandler();\r
74         private void updateTextFromThread()\r
75         {\r
76             try\r
77             {\r
78                 if (this.InvokeRequired)\r
79                 {\r
80                     this.BeginInvoke(new UpdateUIHandler(updateTextFromThread));\r
81                     return;\r
82                 }\r
83                 rtf_actLog.Text = readFile();\r
84                 this.rtf_actLog.SelectionStart = this.rtf_actLog.Text.Length - 1;\r
85                 this.rtf_actLog.ScrollToCaret();\r
86             }\r
87             catch (Exception exc)\r
88             {\r
89                 MessageBox.Show(exc.ToString());\r
90             }\r
91         }\r
92 \r
93         private string readFile()\r
94         {\r
95             string log = "";\r
96             try\r
97             {\r
98                 // hb_encode_log.dat is the primary log file. Since .NET can't read this file whilst the CLI is outputing to it,\r
99                 // we'll need to make a copy of it.\r
100                 string logFile = Path.Combine(Path.GetTempPath(), read_file);\r
101                 string logFile2 = Path.Combine(Path.GetTempPath(), "hb_encode_log_AppReadable.dat");\r
102 \r
103 \r
104                 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.\r
105                 if (File.Exists(logFile2))\r
106                     File.Delete(logFile2);\r
107 \r
108                 // Copy the log file.\r
109                 File.Copy(logFile, logFile2);\r
110 \r
111                 // Begin processing the log file.\r
112                 StreamReader sr = new StreamReader(logFile2);\r
113                 string line = sr.ReadLine();\r
114                 while (line != null)\r
115                 {\r
116                     log = log + (line + System.Environment.NewLine);\r
117                     line = sr.ReadLine();\r
118                 }\r
119                 sr.Close();\r
120                 sr.Dispose();\r
121             }\r
122             catch (Exception exc)\r
123             {\r
124                 MessageBox.Show("An Error has occured! \n\n" + exc.ToString(), "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
125             }\r
126 \r
127             return log;\r
128         }\r
129 \r
130         protected override void OnClosing(CancelEventArgs e)\r
131         {\r
132             if (monitorFile != null)\r
133                 monitorFile.Abort();\r
134             e.Cancel = true;\r
135             this.Hide();\r
136             base.OnClosing(e);\r
137         }\r
138 \r
139     }\r
140 }