OSDN Git Service

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