/* Scan.cs $ This file is part of the HandBrake source code. Homepage: . It may be used under the terms of the GNU General Public License. */ namespace Handbrake.Functions { using System; using System.Diagnostics; using System.IO; using System.Threading; using System.Windows.Forms; using Parsing; /// /// Scan a Source /// public class Scan { /// /// The information for this source /// private DVD thisDvd; /// /// The CLI data parser /// private Parser readData; /// /// The Process belonging to the CLI /// private Process hbProc; /// /// The Progress of the scan /// private string scanProgress; /// /// Scan has Started /// public event EventHandler ScanStared; /// /// Scan has completed /// public event EventHandler ScanCompleted; /// /// Scan process has changed to a new title /// public event EventHandler ScanStatusChanged; /// /// Scan a Source Path. /// Title 0: scan all /// /// Path to the file to scan /// int title number. 0 for scan all public void ScanSource(string sourcePath, int title) { Thread t = new Thread(unused => this.RunScan(sourcePath, title)); t.Start(); } /// /// Object containing the information parsed in the scan. /// /// The DVD object containing the scan information public DVD SouceData() { return this.thisDvd; } /// /// Raw log output from HandBrake CLI /// /// The Log Data public string LogData() { return this.readData.Buffer; } /// /// Progress of the scan. /// /// The progress of the scan public string ScanStatus() { return this.scanProgress; } /// /// The Scan Process /// /// The CLI process public Process ScanProcess() { return this.hbProc; } /// /// Start a scan for a given source path and title /// /// Path to the source file /// the title number to look at private void RunScan(object sourcePath, int title) { try { if (this.ScanStared != null) this.ScanStared(this, new EventArgs()); string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe"); string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs"; string dvdInfoPath = Path.Combine(logDir, "last_scan_log.txt"); // Make we don't pick up a stale last_encode_log.txt (and that we have rights to the file) if (File.Exists(dvdInfoPath)) File.Delete(dvdInfoPath); string dvdnav = string.Empty; if (Properties.Settings.Default.noDvdNav) dvdnav = " --no-dvdnav"; this.hbProc = new Process { StartInfo = { FileName = handbrakeCLIPath, Arguments = String.Format(@" -i ""{0}"" -t{1} {2} -v ", sourcePath, title, dvdnav), RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = true } }; this.hbProc.Start(); this.readData = new Parser(this.hbProc.StandardError.BaseStream); this.readData.OnScanProgress += new ScanProgressEventHandler(this.OnScanProgress); this.thisDvd = DVD.Parse(this.readData); // Write the Buffer out to file. StreamWriter scanLog = new StreamWriter(dvdInfoPath); scanLog.Write(this.readData.Buffer); scanLog.Flush(); scanLog.Close(); if (this.ScanCompleted != null) this.ScanCompleted(this, new EventArgs()); } catch (Exception exc) { Console.WriteLine("frmMain.cs - scanProcess() " + exc); } } /// /// Fire an event when the scan process progresses /// /// the sender /// the current title being scanned /// the total number of titles private void OnScanProgress(object sender, int currentTitle, int titleCount) { this.scanProgress = string.Format("Processing Title: {0} of {1}", currentTitle, titleCount); if (this.ScanStatusChanged != null) this.ScanStatusChanged(this, new EventArgs()); } } }