OSDN Git Service

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