OSDN Git Service

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