// -------------------------------------------------------------------------------------------------------------------- // // This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. // // // A Base Class for the Encode Services. // // -------------------------------------------------------------------------------------------------------------------- namespace HandBrakeWPF.Services.Encode { using System; using System.Diagnostics; using System.Globalization; using System.IO; using HandBrake.ApplicationServices.Model; using HandBrake.ApplicationServices.Services.Logging; using HandBrake.ApplicationServices.Services.Logging.Interfaces; using EncodeCompletedEventArgs = HandBrakeWPF.Services.Encode.EventArgs.EncodeCompletedEventArgs; using EncodeCompletedStatus = HandBrakeWPF.Services.Encode.Interfaces.EncodeCompletedStatus; using EncodeProgessStatus = HandBrakeWPF.Services.Encode.Interfaces.EncodeProgessStatus; using EncodeProgressEventArgs = HandBrakeWPF.Services.Encode.EventArgs.EncodeProgressEventArgs; using EncodeTask = HandBrakeWPF.Services.Encode.Model.EncodeTask; using GeneralApplicationException = HandBrakeWPF.Exceptions.GeneralApplicationException; /// /// A Base Class for the Encode Services. /// public class EncodeBase { /// /// Initializes a new instance of the class. /// public EncodeBase() { } #region Events /// /// Fires when a new QueueTask starts /// public event EventHandler EncodeStarted; /// /// Fires when a QueueTask finishes. /// public event EncodeCompletedStatus EncodeCompleted; /// /// Encode process has progressed /// public event EncodeProgessStatus EncodeStatusChanged; #endregion #region Properties /// /// Gets or sets a value indicating whether IsEncoding. /// public bool IsEncoding { get; protected set; } #endregion #region Invoke Events /// /// Invoke the Encode Status Changed Event. /// /// /// The EncodeProgressEventArgs. /// public void InvokeEncodeStatusChanged(EncodeProgressEventArgs e) { EncodeProgessStatus handler = this.EncodeStatusChanged; if (handler != null) { handler(this, e); } } /// /// Invoke the Encode Completed Event /// /// /// The EncodeCompletedEventArgs. /// public void InvokeEncodeCompleted(EncodeCompletedEventArgs e) { EncodeCompletedStatus handler = this.EncodeCompleted; if (handler != null) { handler(this, e); } } /// /// Invoke the Encode Started Event /// /// /// The EventArgs. /// public void InvokeEncodeStarted(System.EventArgs e) { EventHandler handler = this.EncodeStarted; if (handler != null) { handler(this, e); } } #endregion #region Methods /// /// Save a copy of the log to the users desired location or a default location /// if this feature is enabled in options. /// /// /// The Destination File Path /// /// /// The is Preview. /// /// /// The configuration. /// public void ProcessLogs(string destination, bool isPreview, HBConfiguration configuration) { try { string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs"; string encodeDestinationPath = Path.GetDirectoryName(destination); string destinationFile = Path.GetFileName(destination); string encodeLogFile = destinationFile + " " + DateTime.Now.ToString(CultureInfo.InvariantCulture).Replace("/", "-").Replace(":", "-") + ".txt"; ILog log = LogService.GetLogger(); string logContent = log.ActivityLog; // Make sure the log directory exists. if (!Directory.Exists(logDir)) { Directory.CreateDirectory(logDir); } // Copy the Log to HandBrakes log folder in the users applciation data folder. this.WriteFile(logContent, Path.Combine(logDir, encodeLogFile)); // Save a copy of the log file in the same location as the enocde. if (configuration.SaveLogWithVideo) { this.WriteFile(logContent, Path.Combine(encodeDestinationPath, encodeLogFile)); } // Save a copy of the log file to a user specified location if (Directory.Exists(configuration.SaveLogCopyDirectory) && configuration.SaveLogToCopyDirectory) { this.WriteFile(logContent, Path.Combine(configuration.SaveLogCopyDirectory, encodeLogFile)); } } catch (Exception exc) { Debug.WriteLine(exc); // This exception doesn't warrent user interaction, but it should be logged } } /// /// The write file. /// /// /// The content. /// /// /// The file name. /// private void WriteFile(string content, string fileName) { try { using (StreamWriter fileWriter = new StreamWriter(fileName) { AutoFlush = true }) { fileWriter.Write(content); } } catch (Exception exc) { Debug.WriteLine(exc); } } /// /// Verify the Encode Destination path exists and if not, create it. /// /// /// The task. /// /// /// If the creation fails, an exception is thrown. /// protected void VerifyEncodeDestinationPath(EncodeTask task) { // Make sure the path exists, attempt to create it if it doesn't try { string path = Directory.GetParent(task.Destination).ToString(); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } } catch (Exception exc) { throw new GeneralApplicationException( "Unable to create directory for the encoded output.", "Please verify that you have a valid path.", exc); } } #endregion } }