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         /// Scan a Source Path.\r
57         /// Title 0: scan all\r
58         /// </summary>\r
59         /// <param name="sourcePath">Path to the file to scan</param>\r
60         /// <param name="title">int title number. 0 for scan all</param>\r
61         public void ScanSource(string sourcePath, int title)\r
62         {\r
63             Thread t = new Thread(unused => this.RunScan(sourcePath, title));\r
64             t.Start();\r
65         }\r
66 \r
67         /// <summary>\r
68         /// Object containing the information parsed in the scan.\r
69         /// </summary>\r
70         /// <returns>The DVD object containing the scan information</returns>\r
71         public DVD SouceData()\r
72         {\r
73             return this.thisDvd;\r
74         }\r
75 \r
76         /// <summary>\r
77         /// Raw log output from HandBrake CLI\r
78         /// </summary>\r
79         /// <returns>The Log Data</returns>\r
80         public string LogData()\r
81         {\r
82             return this.readData.Buffer;\r
83         }\r
84 \r
85         /// <summary>\r
86         /// Progress of the scan.\r
87         /// </summary>\r
88         /// <returns>The progress of the scan</returns>\r
89         public string ScanStatus()\r
90         {\r
91             return this.scanProgress;\r
92         }\r
93 \r
94         /// <summary>\r
95         /// The Scan Process\r
96         /// </summary>\r
97         /// <returns>The CLI process</returns>\r
98         public Process ScanProcess()\r
99         {\r
100             return this.hbProc;\r
101         }\r
102 \r
103         /// <summary>\r
104         /// Start a scan for a given source path and title\r
105         /// </summary>\r
106         /// <param name="sourcePath">Path to the source file</param>\r
107         /// <param name="title">the title number to look at</param>\r
108         private void RunScan(object sourcePath, int title)\r
109         {\r
110             try\r
111             {\r
112                 if (this.ScanStared != null)\r
113                     this.ScanStared(this, new EventArgs());\r
114 \r
115                 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
116                 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +\r
117                                 "\\HandBrake\\logs";\r
118                 string dvdInfoPath = Path.Combine(logDir, "last_scan_log.txt");\r
119 \r
120                 // Make we don't pick up a stale last_encode_log.txt (and that we have rights to the file)\r
121                 if (File.Exists(dvdInfoPath))\r
122                     File.Delete(dvdInfoPath);\r
123 \r
124                 string dvdnav = string.Empty;\r
125                 if (Properties.Settings.Default.noDvdNav)\r
126                     dvdnav = " --no-dvdnav";\r
127 \r
128                 this.hbProc = new Process\r
129                                   {\r
130                                       StartInfo =\r
131                                           {\r
132                                               FileName = handbrakeCLIPath, \r
133                                               Arguments =\r
134                                                   String.Format(@" -i ""{0}"" -t{1} {2} -v ", sourcePath, title, dvdnav), \r
135                                               RedirectStandardOutput = true, \r
136                                               RedirectStandardError = true, \r
137                                               UseShellExecute = false, \r
138                                               CreateNoWindow = true\r
139                                           }\r
140                                   };\r
141                 this.hbProc.Start();\r
142 \r
143                 this.readData = new Parser(this.hbProc.StandardError.BaseStream);\r
144                 this.readData.OnScanProgress += new ScanProgressEventHandler(this.OnScanProgress);\r
145                 this.thisDvd = DVD.Parse(this.readData);\r
146 \r
147                 // Write the Buffer out to file.\r
148                 StreamWriter scanLog = new StreamWriter(dvdInfoPath);\r
149                 scanLog.Write(this.readData.Buffer);\r
150                 scanLog.Flush();\r
151                 scanLog.Close();\r
152 \r
153                 if (this.ScanCompleted != null)\r
154                     this.ScanCompleted(this, new EventArgs());\r
155             }\r
156             catch (Exception exc)\r
157             {\r
158                 Console.WriteLine("frmMain.cs - scanProcess() " + exc);\r
159             }\r
160         }\r
161 \r
162         /// <summary>\r
163         /// Fire an event when the scan process progresses\r
164         /// </summary>\r
165         /// <param name="sender">the sender</param>\r
166         /// <param name="currentTitle">the current title being scanned</param>\r
167         /// <param name="titleCount">the total number of titles</param>\r
168         private void OnScanProgress(object sender, int currentTitle, int titleCount)\r
169         {\r
170             this.scanProgress = string.Format("Processing Title: {0} of {1}", currentTitle, titleCount);\r
171             if (this.ScanStatusChanged != null)\r
172                 this.ScanStatusChanged(this, new EventArgs());\r
173         }\r
174     }\r
175 }