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.ApplicationServices.Services
\r
9 using System.Diagnostics;
\r
12 using System.Threading;
\r
13 using System.Windows.Forms;
\r
15 using HandBrake.ApplicationServices.Functions;
\r
16 using HandBrake.ApplicationServices.Parsing;
\r
17 using HandBrake.ApplicationServices.Services.Interfaces;
\r
22 public class ScanService : IScan
\r
24 /* Private Variables */
\r
29 private static readonly object locker = new object();
\r
32 /// The CLI data parser
\r
34 private Parser readData;
\r
39 private StringBuilder logBuffer;
\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
51 /* Event Handlers */
\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
71 /// Gets a value indicating whether IsScanning.
\r
73 public bool IsScanning { get; private set; }
\r
76 /// Gets the Scan Status.
\r
78 public string ScanStatus { get; private set; }
\r
81 /// Gets the Souce Data.
\r
83 public DVD SouceData { get; private set; }
\r
86 /// Gets ActivityLog.
\r
88 public string ActivityLog
\r
93 return readData.Buffer.ToString();
\r
95 if (logBuffer == null)
\r
97 ResetLogReader(false);
\r
98 ReadLastScanFile();
\r
101 return logBuffer != null ? logBuffer.ToString() : string.Empty;
\r
105 /* Public Methods */
\r
108 /// Scan a Source Path.
\r
109 /// Title 0: scan all
\r
111 /// <param name="sourcePath">Path to the file to scan</param>
\r
112 /// <param name="title">int title number. 0 for scan all</param>
\r
113 public void Scan(string sourcePath, int title)
\r
115 Thread t = new Thread(unused => this.ScanSource(sourcePath, title));
\r
126 if (hbProc != null)
\r
129 catch (Exception ex)
\r
131 Main.ShowExceptiowWindow("Unable to kill HandBrakeCLI.exe \n" +
\r
132 "You may need to manually kill HandBrakeCLI.exe using the Windows Task Manager if it does not close automatically" +
\r
133 " within the next few minutes. ", ex.ToString());
\r
137 /* Private Methods */
\r
140 /// Start a scan for a given source path and title
\r
142 /// <param name="sourcePath">Path to the source file</param>
\r
143 /// <param name="title">the title number to look at</param>
\r
144 private void ScanSource(object sourcePath, int title)
\r
149 if (this.ScanStared != null)
\r
150 this.ScanStared(this, new EventArgs());
\r
152 ResetLogReader(true);
\r
154 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");
\r
155 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
\r
156 "\\HandBrake\\logs";
\r
157 string dvdInfoPath = Path.Combine(logDir, string.Format("last_scan_log{0}.txt", Init.InstanceId == 0 ? string.Empty : Init.InstanceId.ToString()));
\r
159 // Make we don't pick up a stale last_encode_log.txt (and that we have rights to the file)
\r
160 if (File.Exists(dvdInfoPath))
\r
161 File.Delete(dvdInfoPath);
\r
163 string extraArguments = string.Empty;
\r
164 if (Init.DisableDvdNav)
\r
165 extraArguments = " --no-dvdnav";
\r
168 extraArguments += " --scan ";
\r
170 this.hbProc = new Process
\r
174 FileName = handbrakeCLIPath,
\r
176 String.Format(@" -i ""{0}"" -t{1} {2} -v ", sourcePath, title,
\r
178 RedirectStandardOutput = true,
\r
179 RedirectStandardError = true,
\r
180 UseShellExecute = false,
\r
181 CreateNoWindow = true
\r
186 this.hbProc.Start();
\r
188 this.readData = new Parser(this.hbProc.StandardError.BaseStream);
\r
189 this.readData.OnScanProgress += new ScanProgressEventHandler(this.OnScanProgress);
\r
190 this.SouceData = DVD.Parse(this.readData);
\r
192 // Write the Buffer out to file.
\r
193 StreamWriter scanLog = new StreamWriter(dvdInfoPath);
\r
194 scanLog.WriteLine(Logging.CreateCliLogHeader(null));
\r
195 scanLog.Write(this.readData.Buffer);
\r
198 logBuffer.AppendLine(this.readData.Buffer.ToString());
\r
200 IsScanning = false;
\r
202 if (this.ScanCompleted != null)
\r
203 this.ScanCompleted(this, new EventArgs());
\r
205 catch (Exception exc)
\r
207 Main.ShowExceptiowWindow("frmMain.cs - scanProcess() Error", exc.ToString());
\r
212 /// Read the log file
\r
214 private void ReadLastScanFile()
\r
218 // 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
219 // we'll need to make a copy of it.
\r
220 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
\r
221 "\\HandBrake\\logs";
\r
222 string logFile = Path.Combine(logDir, string.Format("last_scan_log{0}.txt", Init.InstanceId == 0 ? string.Empty : Init.InstanceId.ToString()));
\r
223 string logFile2 = Path.Combine(logDir, string.Format("tmp_appReadable_log{0}.txt", Init.InstanceId == 0 ? string.Empty : Init.InstanceId.ToString()));
\r
227 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.
\r
228 if (File.Exists(logFile2))
\r
229 File.Delete(logFile2);
\r
231 // Copy the log file.
\r
232 if (File.Exists(logFile))
\r
233 File.Copy(logFile, logFile2, true);
\r
236 ResetLogReader(true);
\r
240 // Start the Reader
\r
241 // Only use text which continues on from the last read line
\r
242 StreamReader sr = new StreamReader(logFile2);
\r
245 while ((line = sr.ReadLine()) != null)
\r
247 if (i > logFilePosition)
\r
249 logBuffer.AppendLine(line);
\r
257 catch (Exception exc)
\r
259 Console.WriteLine(exc.ToString());
\r
260 ResetLogReader(true);
\r
266 /// Reset the Log Reader
\r
268 /// <param name="addHeader">
\r
269 /// The add Header.
\r
271 private void ResetLogReader(bool addHeader)
\r
273 logFilePosition = 0;
\r
274 logBuffer = new StringBuilder();
\r
276 logBuffer.AppendLine(Logging.CreateCliLogHeader(null));
\r
280 /// Fire an event when the scan process progresses
\r
282 /// <param name="sender">the sender</param>
\r
283 /// <param name="currentTitle">the current title being scanned</param>
\r
284 /// <param name="titleCount">the total number of titles</param>
\r
285 private void OnScanProgress(object sender, int currentTitle, int titleCount)
\r
287 this.ScanStatus = string.Format("Processing Title: {0} of {1}", currentTitle, titleCount);
\r
288 if (this.ScanStatusChanged != null)
\r
289 this.ScanStatusChanged(this, new EventArgs());
\r