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) + "\\HandBrake\\logs";\r
117                 string dvdInfoPath = Path.Combine(logDir, "last_scan_log.txt");\r
118 \r
119                 // Make we don't pick up a stale last_encode_log.txt (and that we have rights to the file)\r
120                 if (File.Exists(dvdInfoPath))\r
121                     File.Delete(dvdInfoPath);\r
122 \r
123                 string dvdnav = string.Empty;\r
124                 if (Properties.Settings.Default.noDvdNav)\r
125                     dvdnav = " --no-dvdnav";\r
126 \r
127                 this.hbProc = new Process\r
128                              {\r
129                                  StartInfo =\r
130                                      {\r
131                                          FileName = handbrakeCLIPath,\r
132                                          Arguments =\r
133                                              String.Format(@" -i ""{0}"" -t{1} {2} -v ", sourcePath, title, dvdnav),\r
134                                          RedirectStandardOutput = true,\r
135                                          RedirectStandardError = true,\r
136                                          UseShellExecute = false,\r
137                                          CreateNoWindow = true\r
138                                      }\r
139                              };\r
140                 this.hbProc.Start();\r
141 \r
142                 this.readData = new Parser(this.hbProc.StandardError.BaseStream);\r
143                 this.readData.OnScanProgress += new ScanProgressEventHandler(this.OnScanProgress);\r
144                 this.thisDvd = DVD.Parse(this.readData);\r
145 \r
146                 // Write the Buffer out to file.\r
147                 StreamWriter scanLog = new StreamWriter(dvdInfoPath);\r
148                 scanLog.Write(this.readData.Buffer);\r
149                 scanLog.Flush();\r
150                 scanLog.Close();\r
151 \r
152                 if (this.ScanCompleted != null)\r
153                     this.ScanCompleted(this, new EventArgs());\r
154             }\r
155             catch (Exception exc)\r
156             {\r
157                 Console.WriteLine("frmMain.cs - scanProcess() " + exc);\r
158             }\r
159         }\r
160 \r
161         /// <summary>\r
162         /// Fire an event when the scan process progresses\r
163         /// </summary>\r
164         /// <param name="sender">the sender</param>\r
165         /// <param name="currentTitle">the current title being scanned</param>\r
166         /// <param name="titleCount">the total number of titles</param>\r
167         private void OnScanProgress(object sender, int currentTitle, int titleCount)\r
168         {\r
169             this.scanProgress = string.Format("Processing Title: {0} of {1}", currentTitle, titleCount);\r
170             if (this.ScanStatusChanged != null)\r
171                 this.ScanStatusChanged(this, new EventArgs());\r
172         }\r
173     }\r
174 }