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
6 namespace Handbrake.Services
\r
9 using System.Diagnostics;
\r
12 using System.Threading;
\r
13 using System.Windows.Forms;
\r
22 /// The information for this source
\r
24 private DVD thisDvd;
\r
27 /// The CLI data parser
\r
29 private Parser readData;
\r
34 private StringBuilder logBuffer;
\r
39 private static object locker = new object();
\r
42 /// The line number thats been read to in the log file
\r
44 private int logFilePosition;
\r
47 /// The Process belonging to the CLI
\r
49 private Process hbProc;
\r
52 /// The Progress of the scan
\r
54 private string scanProgress;
\r
57 /// Scan has Started
\r
59 public event EventHandler ScanStared;
\r
62 /// Scan has completed
\r
64 public event EventHandler ScanCompleted;
\r
67 /// Scan process has changed to a new title
\r
69 public event EventHandler ScanStatusChanged;
\r
72 /// Gets or sets a value indicating whether IsScanning.
\r
74 public bool IsScanning { get; set; }
\r
77 /// Gets ActivityLog.
\r
79 public string ActivityLog
\r
84 return readData.Buffer;
\r
87 return logBuffer.ToString();
\r
92 /// Scan a Source Path.
\r
93 /// Title 0: scan all
\r
95 /// <param name="sourcePath">Path to the file to scan</param>
\r
96 /// <param name="title">int title number. 0 for scan all</param>
\r
97 public void ScanSource(string sourcePath, int title)
\r
99 Thread t = new Thread(unused => this.RunScan(sourcePath, title));
\r
104 /// Object containing the information parsed in the scan.
\r
106 /// <returns>The DVD object containing the scan information</returns>
\r
107 public DVD SouceData()
\r
109 return this.thisDvd;
\r
113 /// Progress of the scan.
\r
115 /// <returns>The progress of the scan</returns>
\r
116 public string ScanStatus()
\r
118 return this.scanProgress;
\r
122 /// The Scan Process
\r
124 /// <returns>The CLI process</returns>
\r
125 public Process ScanProcess()
\r
127 return this.hbProc;
\r
131 /// Start a scan for a given source path and title
\r
133 /// <param name="sourcePath">Path to the source file</param>
\r
134 /// <param name="title">the title number to look at</param>
\r
135 private void RunScan(object sourcePath, int title)
\r
140 if (this.ScanStared != null)
\r
141 this.ScanStared(this, new EventArgs());
\r
145 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");
\r
146 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
\r
147 "\\HandBrake\\logs";
\r
148 string dvdInfoPath = Path.Combine(logDir, "last_scan_log.txt");
\r
150 // Make we don't pick up a stale last_encode_log.txt (and that we have rights to the file)
\r
151 if (File.Exists(dvdInfoPath))
\r
152 File.Delete(dvdInfoPath);
\r
154 string extraArguments = string.Empty;
\r
155 if (Properties.Settings.Default.noDvdNav)
\r
156 extraArguments = " --no-dvdnav";
\r
159 extraArguments += " --scan ";
\r
161 this.hbProc = new Process
\r
165 FileName = handbrakeCLIPath,
\r
167 String.Format(@" -i ""{0}"" -t{1} {2} -v ", sourcePath, title, extraArguments),
\r
168 RedirectStandardOutput = true,
\r
169 RedirectStandardError = true,
\r
170 UseShellExecute = false,
\r
171 CreateNoWindow = true
\r
176 this.hbProc.Start();
\r
178 this.readData = new Parser(this.hbProc.StandardError.BaseStream);
\r
179 this.readData.OnScanProgress += new ScanProgressEventHandler(this.OnScanProgress);
\r
180 this.thisDvd = DVD.Parse(this.readData);
\r
182 // Write the Buffer out to file.
\r
183 StreamWriter scanLog = new StreamWriter(dvdInfoPath);
\r
184 scanLog.Write(this.readData.Buffer);
\r
188 if (this.ScanCompleted != null)
\r
189 this.ScanCompleted(this, new EventArgs());
\r
190 IsScanning = false;
\r
192 catch (Exception exc)
\r
194 Console.WriteLine("frmMain.cs - scanProcess() " + exc);
\r
199 /// Read the log file
\r
201 private void ReadFile()
\r
205 // last_encode_log.txt is the primary log file. Since .NET can't read this file whilst the CLI is outputing to it (Not even in read only mode),
\r
206 // we'll need to make a copy of it.
\r
207 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
\r
208 string logFile = Path.Combine(logDir, "last_scan_log.txt");
\r
209 string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");
\r
213 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.
\r
214 if (File.Exists(logFile2))
\r
215 File.Delete(logFile2);
\r
217 // Copy the log file.
\r
218 if (File.Exists(logFile))
\r
219 File.Copy(logFile, logFile2, true);
\r
226 // Start the Reader
\r
227 // Only use text which continues on from the last read line
\r
228 StreamReader sr = new StreamReader(logFile2);
\r
231 while ((line = sr.ReadLine()) != null)
\r
233 if (i > logFilePosition)
\r
235 logBuffer.AppendLine(line);
\r
251 /// Reset the Log Reader
\r
253 private void ResetLogReader()
\r
255 logFilePosition = 0;
\r
256 logBuffer = new StringBuilder();
\r
260 /// Fire an event when the scan process progresses
\r
262 /// <param name="sender">the sender</param>
\r
263 /// <param name="currentTitle">the current title being scanned</param>
\r
264 /// <param name="titleCount">the total number of titles</param>
\r
265 private void OnScanProgress(object sender, int currentTitle, int titleCount)
\r
267 this.scanProgress = string.Format("Processing Title: {0} of {1}", currentTitle, titleCount);
\r
268 if (this.ScanStatusChanged != null)
\r
269 this.ScanStatusChanged(this, new EventArgs());
\r