OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / Functions / Scan.cs
1 /*  Scan.cs $\r
2     This file is part of the HandBrake source code.\r
3     Homepage: <http://handbrake.fr>.\r
4     It may be used under the terms of the GNU General Public License. */\r
5 \r
6 namespace Handbrake.Functions\r
7 {\r
8     using System;\r
9     using System.Diagnostics;\r
10     using System.IO;\r
11     using System.Threading;\r
12     using System.Windows.Forms;\r
13     using Parsing;\r
14 \r
15     /// <summary>\r
16     /// Scan a Source\r
17     /// </summary>\r
18     public class Scan\r
19     {\r
20         /// <summary>\r
21         /// The information for this source\r
22         /// </summary>\r
23         private DVD thisDvd;\r
24 \r
25         /// <summary>\r
26         /// The CLI data parser\r
27         /// </summary>\r
28         private Parser readData;\r
29 \r
30         /// <summary>\r
31         /// The Process belonging to the CLI\r
32         /// </summary>\r
33         private Process hbProc;\r
34 \r
35         /// <summary>\r
36         /// The Progress of the scan\r
37         /// </summary>\r
38         private string scanProgress;\r
39 \r
40         /// <summary>\r
41         /// Scan has Started\r
42         /// </summary>\r
43         public event EventHandler ScanStared;\r
44 \r
45         /// <summary>\r
46         /// Scan has completed\r
47         /// </summary>\r
48         public event EventHandler ScanCompleted;\r
49 \r
50         /// <summary>\r
51         /// Scan process has changed to a new title\r
52         /// </summary>\r
53         public event EventHandler ScanStatusChanged;\r
54 \r
55         /// <summary>\r
56         /// Gets or sets a value indicating whether IsScanning.\r
57         /// </summary>\r
58         public bool IsScanning { get; set; }\r
59 \r
60         /// <summary>\r
61         /// Scan a Source Path.\r
62         /// Title 0: scan all\r
63         /// </summary>\r
64         /// <param name="sourcePath">Path to the file to scan</param>\r
65         /// <param name="title">int title number. 0 for scan all</param>\r
66         public void ScanSource(string sourcePath, int title)\r
67         {\r
68             Thread t = new Thread(unused => this.RunScan(sourcePath, title));\r
69             t.Start();\r
70         }\r
71 \r
72         /// <summary>\r
73         /// Object containing the information parsed in the scan.\r
74         /// </summary>\r
75         /// <returns>The DVD object containing the scan information</returns>\r
76         public DVD SouceData()\r
77         {\r
78             return this.thisDvd;\r
79         }\r
80 \r
81         /// <summary>\r
82         /// Raw log output from HandBrake CLI\r
83         /// </summary>\r
84         /// <returns>The Log Data</returns>\r
85         public string LogData()\r
86         {\r
87             return this.readData.Buffer;\r
88         }\r
89 \r
90         /// <summary>\r
91         /// Progress of the scan.\r
92         /// </summary>\r
93         /// <returns>The progress of the scan</returns>\r
94         public string ScanStatus()\r
95         {\r
96             return this.scanProgress;\r
97         }\r
98 \r
99         /// <summary>\r
100         /// The Scan Process\r
101         /// </summary>\r
102         /// <returns>The CLI process</returns>\r
103         public Process ScanProcess()\r
104         {\r
105             return this.hbProc;\r
106         }\r
107 \r
108         /// <summary>\r
109         /// Start a scan for a given source path and title\r
110         /// </summary>\r
111         /// <param name="sourcePath">Path to the source file</param>\r
112         /// <param name="title">the title number to look at</param>\r
113         private void RunScan(object sourcePath, int title)\r
114         {\r
115             try\r
116             {\r
117                 IsScanning = true;\r
118                 if (this.ScanStared != null)\r
119                     this.ScanStared(this, new EventArgs());\r
120 \r
121                 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
122                 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +\r
123                                 "\\HandBrake\\logs";\r
124                 string dvdInfoPath = Path.Combine(logDir, "last_scan_log.txt");\r
125 \r
126                 // Make we don't pick up a stale last_encode_log.txt (and that we have rights to the file)\r
127                 if (File.Exists(dvdInfoPath))\r
128                     File.Delete(dvdInfoPath);\r
129 \r
130                 string dvdnav = string.Empty;\r
131                 if (Properties.Settings.Default.noDvdNav)\r
132                     dvdnav = " --no-dvdnav";\r
133 \r
134                 this.hbProc = new Process\r
135                                   {\r
136                                       StartInfo =\r
137                                           {\r
138                                               FileName = handbrakeCLIPath, \r
139                                               Arguments =\r
140                                                   String.Format(@" -i ""{0}"" -t{1} {2} -v ", sourcePath, title, dvdnav), \r
141                                               RedirectStandardOutput = true, \r
142                                               RedirectStandardError = true, \r
143                                               UseShellExecute = false, \r
144                                               CreateNoWindow = true\r
145                                           }\r
146                                   };\r
147                 this.hbProc.Start();\r
148 \r
149                 this.readData = new Parser(this.hbProc.StandardError.BaseStream);\r
150                 this.readData.OnScanProgress += new ScanProgressEventHandler(this.OnScanProgress);\r
151                 this.thisDvd = DVD.Parse(this.readData);\r
152 \r
153                 // Write the Buffer out to file.\r
154                 StreamWriter scanLog = new StreamWriter(dvdInfoPath);\r
155                 scanLog.Write(this.readData.Buffer);\r
156                 scanLog.Flush();\r
157                 scanLog.Close();\r
158 \r
159                 if (this.ScanCompleted != null)\r
160                     this.ScanCompleted(this, new EventArgs());\r
161                 IsScanning = false;\r
162             }\r
163             catch (Exception exc)\r
164             {\r
165                 Console.WriteLine("frmMain.cs - scanProcess() " + exc);\r
166             }\r
167         }\r
168 \r
169         /// <summary>\r
170         /// Fire an event when the scan process progresses\r
171         /// </summary>\r
172         /// <param name="sender">the sender</param>\r
173         /// <param name="currentTitle">the current title being scanned</param>\r
174         /// <param name="titleCount">the total number of titles</param>\r
175         private void OnScanProgress(object sender, int currentTitle, int titleCount)\r
176         {\r
177             this.scanProgress = string.Format("Processing Title: {0} of {1}", currentTitle, titleCount);\r
178             if (this.ScanStatusChanged != null)\r
179                 this.ScanStatusChanged(this, new EventArgs());\r
180         }\r
181     }\r
182 }