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
133 public void KillScan()
\r
137 if (hbProc != null)
\r
140 catch (Exception ex)
\r
143 "Unable to kill HandBrakeCLI.exe \nYou may need to manually kill HandBrakeCLI.exe using the Windows Task Manager if it does not close automatically within the next few minutes. \n\nError Information: \n" +
\r
144 ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
149 /// Start a scan for a given source path and title
\r
151 /// <param name="sourcePath">Path to the source file</param>
\r
152 /// <param name="title">the title number to look at</param>
\r
153 private void RunScan(object sourcePath, int title)
\r
158 if (this.ScanStared != null)
\r
159 this.ScanStared(this, new EventArgs());
\r
163 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");
\r
164 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
\r
165 "\\HandBrake\\logs";
\r
166 string dvdInfoPath = Path.Combine(logDir, "last_scan_log.txt");
\r
168 // Make we don't pick up a stale last_encode_log.txt (and that we have rights to the file)
\r
169 if (File.Exists(dvdInfoPath))
\r
170 File.Delete(dvdInfoPath);
\r
172 string extraArguments = string.Empty;
\r
173 if (Properties.Settings.Default.noDvdNav)
\r
174 extraArguments = " --no-dvdnav";
\r
177 extraArguments += " --scan ";
\r
179 this.hbProc = new Process
\r
183 FileName = handbrakeCLIPath,
\r
185 String.Format(@" -i ""{0}"" -t{1} {2} -v ", sourcePath, title, extraArguments),
\r
186 RedirectStandardOutput = true,
\r
187 RedirectStandardError = true,
\r
188 UseShellExecute = false,
\r
189 CreateNoWindow = true
\r
194 this.hbProc.Start();
\r
196 this.readData = new Parser(this.hbProc.StandardError.BaseStream);
\r
197 this.readData.OnScanProgress += new ScanProgressEventHandler(this.OnScanProgress);
\r
198 this.thisDvd = DVD.Parse(this.readData);
\r
200 // Write the Buffer out to file.
\r
201 StreamWriter scanLog = new StreamWriter(dvdInfoPath);
\r
202 scanLog.Write(this.readData.Buffer);
\r
206 if (this.ScanCompleted != null)
\r
207 this.ScanCompleted(this, new EventArgs());
\r
208 IsScanning = false;
\r
210 catch (Exception exc)
\r
212 Console.WriteLine("frmMain.cs - scanProcess() " + exc);
\r
217 /// Read the log file
\r
219 private void ReadFile()
\r
223 // 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
224 // we'll need to make a copy of it.
\r
225 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
\r
226 string logFile = Path.Combine(logDir, "last_scan_log.txt");
\r
227 string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");
\r
231 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.
\r
232 if (File.Exists(logFile2))
\r
233 File.Delete(logFile2);
\r
235 // Copy the log file.
\r
236 if (File.Exists(logFile))
\r
237 File.Copy(logFile, logFile2, true);
\r
244 // Start the Reader
\r
245 // Only use text which continues on from the last read line
\r
246 StreamReader sr = new StreamReader(logFile2);
\r
249 while ((line = sr.ReadLine()) != null)
\r
251 if (i > logFilePosition)
\r
253 logBuffer.AppendLine(line);
\r
269 /// Reset the Log Reader
\r
271 private void ResetLogReader()
\r
273 logFilePosition = 0;
\r
274 logBuffer = new StringBuilder();
\r
278 /// Fire an event when the scan process progresses
\r
280 /// <param name="sender">the sender</param>
\r
281 /// <param name="currentTitle">the current title being scanned</param>
\r
282 /// <param name="titleCount">the total number of titles</param>
\r
283 private void OnScanProgress(object sender, int currentTitle, int titleCount)
\r
285 this.scanProgress = string.Format("Processing Title: {0} of {1}", currentTitle, titleCount);
\r
286 if (this.ScanStatusChanged != null)
\r
287 this.ScanStatusChanged(this, new EventArgs());
\r