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
36 static object locker = new object();
\r
39 /// The line number thats been read to in the log file
\r
41 private int logFilePosition;
\r
44 /// The Process belonging to the CLI
\r
46 private Process hbProc;
\r
49 /// The Progress of the scan
\r
51 private string scanProgress;
\r
54 /// Scan has Started
\r
56 public event EventHandler ScanStared;
\r
59 /// Scan has completed
\r
61 public event EventHandler ScanCompleted;
\r
64 /// Scan process has changed to a new title
\r
66 public event EventHandler ScanStatusChanged;
\r
69 /// Gets or sets a value indicating whether IsScanning.
\r
71 public bool IsScanning { get; set; }
\r
74 /// Gets ActivityLog.
\r
76 public string ActivityLog
\r
81 return readData.Buffer;
\r
84 return logBuffer.ToString();
\r
89 /// Scan a Source Path.
\r
90 /// Title 0: scan all
\r
92 /// <param name="sourcePath">Path to the file to scan</param>
\r
93 /// <param name="title">int title number. 0 for scan all</param>
\r
94 public void ScanSource(string sourcePath, int title)
\r
96 Thread t = new Thread(unused => this.RunScan(sourcePath, title));
\r
101 /// Object containing the information parsed in the scan.
\r
103 /// <returns>The DVD object containing the scan information</returns>
\r
104 public DVD SouceData()
\r
106 return this.thisDvd;
\r
110 /// Progress of the scan.
\r
112 /// <returns>The progress of the scan</returns>
\r
113 public string ScanStatus()
\r
115 return this.scanProgress;
\r
119 /// The Scan Process
\r
121 /// <returns>The CLI process</returns>
\r
122 public Process ScanProcess()
\r
124 return this.hbProc;
\r
128 /// Start a scan for a given source path and title
\r
130 /// <param name="sourcePath">Path to the source file</param>
\r
131 /// <param name="title">the title number to look at</param>
\r
132 private void RunScan(object sourcePath, int title)
\r
137 if (this.ScanStared != null)
\r
138 this.ScanStared(this, new EventArgs());
\r
142 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");
\r
143 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
\r
144 string dvdInfoPath = Path.Combine(logDir, "last_scan_log.txt");
\r
146 // Make we don't pick up a stale last_encode_log.txt (and that we have rights to the file)
\r
147 if (File.Exists(dvdInfoPath))
\r
148 File.Delete(dvdInfoPath);
\r
150 string dvdnav = string.Empty;
\r
151 if (Properties.Settings.Default.noDvdNav)
\r
152 dvdnav = " --no-dvdnav";
\r
154 this.hbProc = new Process
\r
158 FileName = handbrakeCLIPath,
\r
160 String.Format(@" -i ""{0}"" -t{1} {2} -v ", sourcePath, title, dvdnav),
\r
161 RedirectStandardOutput = true,
\r
162 RedirectStandardError = true,
\r
163 UseShellExecute = false,
\r
164 CreateNoWindow = true
\r
169 this.hbProc.Start();
\r
171 this.readData = new Parser(this.hbProc.StandardError.BaseStream);
\r
172 this.readData.OnScanProgress += new ScanProgressEventHandler(this.OnScanProgress);
\r
173 this.thisDvd = DVD.Parse(this.readData);
\r
175 // Write the Buffer out to file.
\r
176 StreamWriter scanLog = new StreamWriter(dvdInfoPath);
\r
177 scanLog.Write(this.readData.Buffer);
\r
181 if (this.ScanCompleted != null)
\r
182 this.ScanCompleted(this, new EventArgs());
\r
183 IsScanning = false;
\r
185 catch (Exception exc)
\r
187 Console.WriteLine("frmMain.cs - scanProcess() " + exc);
\r
192 /// Read the log file
\r
194 private void ReadFile()
\r
198 // 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
199 // we'll need to make a copy of it.
\r
200 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
\r
201 string logFile = Path.Combine(logDir, "last_scan_log.txt");
\r
202 string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");
\r
206 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.
\r
207 if (File.Exists(logFile2))
\r
208 File.Delete(logFile2);
\r
210 // Copy the log file.
\r
211 if (File.Exists(logFile))
\r
212 File.Copy(logFile, logFile2, true);
\r
219 // Start the Reader
\r
220 // Only use text which continues on from the last read line
\r
221 StreamReader sr = new StreamReader(logFile2);
\r
224 while ((line = sr.ReadLine()) != null)
\r
226 if (i > logFilePosition)
\r
228 logBuffer.AppendLine(line);
\r
244 /// Reset the Log Reader
\r
246 private void ResetLogReader()
\r
248 logFilePosition = 0;
\r
249 logBuffer = new StringBuilder();
\r
253 /// Fire an event when the scan process progresses
\r
255 /// <param name="sender">the sender</param>
\r
256 /// <param name="currentTitle">the current title being scanned</param>
\r
257 /// <param name="titleCount">the total number of titles</param>
\r
258 private void OnScanProgress(object sender, int currentTitle, int titleCount)
\r
260 this.scanProgress = string.Format("Processing Title: {0} of {1}", currentTitle, titleCount);
\r
261 if (this.ScanStatusChanged != null)
\r
262 this.ScanStatusChanged(this, new EventArgs());
\r