From afcbfbd96a5536171ea640a67f6c14c907744894 Mon Sep 17 00:00:00 2001 From: sr55 Date: Sat, 22 Jan 2011 18:02:19 +0000 Subject: WinGui: - Refactored some more code out of the UI project into the Services Project. - Added support for showing bluray drives to the "Source" dropdown menu git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@3764 b64f7644-9d1e-0410-96f1-a4d463321fa5 --- .../Functions/Converters.cs | 44 +-- .../Functions/EnumHelper.cs | 2 +- .../Functions/Logging.cs | 52 --- .../Functions/Main.cs | 24 -- .../HandBrake.ApplicationServices.csproj | 8 +- .../Model/DriveInformation.cs | 44 +++ .../Model/EncodeTask.cs | 2 +- .../Model/Encoding/Anamorphic.cs | 7 +- .../Model/Encoding/AudioEncoder.cs | 7 +- .../Model/Encoding/Decomb.cs | 7 +- .../Model/Encoding/Deinterlace.cs | 10 +- .../Model/Encoding/Denoise.cs | 10 +- .../Model/Encoding/Detelecine.cs | 10 +- .../Model/Encoding/Mixdown.cs | 7 +- .../Model/Encoding/OutputFormat.cs | 7 +- .../Model/Encoding/PointToPointMode.cs | 7 +- .../Model/Encoding/SubtitleTrack.cs | 2 +- .../Model/Encoding/VideoEncodeMode.cs | 17 + .../Model/Encoding/VideoEncodeRateType.cs | 9 - .../Model/Encoding/VideoEncoder.cs | 10 +- .../Model/QueueTask.cs | 4 +- .../HandBrake.ApplicationServices/Parsing/Title.cs | 10 +- .../Services/Encode.cs | 4 +- .../HandBrake.ApplicationServices/Services/Scan.cs | 355 --------------------- .../Services/ScanService.cs | 354 ++++++++++++++++++++ .../Services/UtilityService.cs | 131 ++++++++ 26 files changed, 640 insertions(+), 504 deletions(-) delete mode 100644 win/C#/HandBrake.ApplicationServices/Functions/Logging.cs delete mode 100644 win/C#/HandBrake.ApplicationServices/Functions/Main.cs create mode 100644 win/C#/HandBrake.ApplicationServices/Model/DriveInformation.cs create mode 100644 win/C#/HandBrake.ApplicationServices/Model/Encoding/VideoEncodeMode.cs delete mode 100644 win/C#/HandBrake.ApplicationServices/Model/Encoding/VideoEncodeRateType.cs delete mode 100644 win/C#/HandBrake.ApplicationServices/Services/Scan.cs create mode 100644 win/C#/HandBrake.ApplicationServices/Services/ScanService.cs create mode 100644 win/C#/HandBrake.ApplicationServices/Services/UtilityService.cs (limited to 'win/C#/HandBrake.ApplicationServices') diff --git a/win/C#/HandBrake.ApplicationServices/Functions/Converters.cs b/win/C#/HandBrake.ApplicationServices/Functions/Converters.cs index c4ba59f14..84df3dac3 100644 --- a/win/C#/HandBrake.ApplicationServices/Functions/Converters.cs +++ b/win/C#/HandBrake.ApplicationServices/Functions/Converters.cs @@ -1,7 +1,11 @@ -namespace HandBrake.ApplicationServices.Functions +/* Converters.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.ApplicationServices.Functions { using System; - using System.Collections.Generic; using System.Text.RegularExpressions; /// @@ -30,41 +34,5 @@ return converted; } - - /// - /// Video Framerate Converter - /// - private static readonly Dictionary vrates = new Dictionary - { - {5, 5400000}, - {10, 2700000}, - {12, 2250000}, - {15, 1800000}, - {23.976, 1126125}, - {24, 1125000}, - {25, 1080000}, - {29.97, 900900} - }; - - /// - /// Convert the desired framerate to the video rate. - /// - /// - /// The framerate. - /// - /// - /// The Video Rate. - /// - /// - /// - public static int FramerateToVrate(double framerate) - { - if (!vrates.ContainsKey(framerate)) - { - throw new ArgumentException("Framerate not recognized.", "framerate"); - } - - return vrates[framerate]; - } } } diff --git a/win/C#/HandBrake.ApplicationServices/Functions/EnumHelper.cs b/win/C#/HandBrake.ApplicationServices/Functions/EnumHelper.cs index 6cf7342a9..0bbc746d7 100644 --- a/win/C#/HandBrake.ApplicationServices/Functions/EnumHelper.cs +++ b/win/C#/HandBrake.ApplicationServices/Functions/EnumHelper.cs @@ -5,9 +5,9 @@ namespace HandBrake.ApplicationServices.Functions { + using System; using System.ComponentModel; using System.Reflection; - using System; /// /// Enum Helpers diff --git a/win/C#/HandBrake.ApplicationServices/Functions/Logging.cs b/win/C#/HandBrake.ApplicationServices/Functions/Logging.cs deleted file mode 100644 index 1bb08cdfb..000000000 --- a/win/C#/HandBrake.ApplicationServices/Functions/Logging.cs +++ /dev/null @@ -1,52 +0,0 @@ -/* Logging.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.ApplicationServices.Functions -{ - using System; - using System.IO; - using System.Text; - using System.Windows.Forms; - - using HandBrake.ApplicationServices.Model; - - /// - /// The System Information. - /// - public class Logging - { - /// - /// Add the CLI Query to the Log File. - /// - /// - /// The Encode Job Object - /// - /// - /// The create cli log header. - /// - public static string CreateCliLogHeader(QueueTask encJob) - { - StringBuilder logHeader = new StringBuilder(); - - logHeader.AppendLine(String.Format("# {0}", Init.HandBrakeGuiVersionString)); - logHeader.AppendLine(String.Format("# Running: {0}", Environment.OSVersion)); - logHeader.AppendLine(String.Format("# CPU: {0}", SystemInfo.GetCpuCount)); - logHeader.AppendLine(String.Format("# Ram: {0} MB", SystemInfo.TotalPhysicalMemory)); - logHeader.AppendLine(String.Format("# Screen: {0}x{1}", SystemInfo.ScreenBounds.Bounds.Width, SystemInfo.ScreenBounds.Bounds.Height)); - logHeader.AppendLine(String.Format("# Temp Dir: {0}", Path.GetTempPath())); - logHeader.AppendLine(String.Format("# Install Dir: {0}", Application.StartupPath)); - logHeader.AppendLine(String.Format("# Data Dir: {0}\n", Application.UserAppDataPath)); - - if (encJob != null) - { - logHeader.AppendLine(String.Format("# CLI Query: {0}", encJob.Query)); - logHeader.AppendLine(String.Format("# User Query: {0}", encJob.CustomQuery)); - } - logHeader.AppendLine("-------------------------------------------"); - - return logHeader.ToString(); - } - } -} \ No newline at end of file diff --git a/win/C#/HandBrake.ApplicationServices/Functions/Main.cs b/win/C#/HandBrake.ApplicationServices/Functions/Main.cs deleted file mode 100644 index 368065dca..000000000 --- a/win/C#/HandBrake.ApplicationServices/Functions/Main.cs +++ /dev/null @@ -1,24 +0,0 @@ -/* Main.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.ApplicationServices.Functions -{ - using System.Diagnostics; - - /// - /// Useful functions which various screens can use. - /// - public static class Main - { - /// - /// Get the Process ID of HandBrakeCLI for the current instance. - /// - /// A list of processes - public static Process[] GetCliProcess() - { - return Process.GetProcessesByName("HandBrakeCLI"); - } - } -} \ No newline at end of file diff --git a/win/C#/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj b/win/C#/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj index 12b5bc624..57c3ca4ce 100644 --- a/win/C#/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj +++ b/win/C#/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj @@ -89,13 +89,12 @@ - - + @@ -110,7 +109,7 @@ - + @@ -133,7 +132,8 @@ - + + diff --git a/win/C#/HandBrake.ApplicationServices/Model/DriveInformation.cs b/win/C#/HandBrake.ApplicationServices/Model/DriveInformation.cs new file mode 100644 index 000000000..65708cee2 --- /dev/null +++ b/win/C#/HandBrake.ApplicationServices/Model/DriveInformation.cs @@ -0,0 +1,44 @@ +/* DriveInformation.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.ApplicationServices.Model +{ + /// + /// Information about a DVD drive + /// + public class DriveInformation + { + /// + /// Gets or sets A Unique ID That represemts this model. + /// + public int Id { get; set; } + + /// + /// Gets or sets The Drive Volume Name + /// + public string VolumeLabel { get; set; } + + /// + /// Gets or sets The Root Directory + /// + public string RootDirectory { get; set; } + + /// + /// Gets or sets a value indicating whether this is a BluRay Drive. + /// + public bool IsBluRay { get; set; } + + /// + /// Returns "Drive" + Id (e.g Drive2) + /// + /// + /// A String that contrains "Drive" and it's ID + /// + public override string ToString() + { + return "Drive" + this.Id; + } + } +} \ No newline at end of file diff --git a/win/C#/HandBrake.ApplicationServices/Model/EncodeTask.cs b/win/C#/HandBrake.ApplicationServices/Model/EncodeTask.cs index b10168aa5..dfc0fbf08 100644 --- a/win/C#/HandBrake.ApplicationServices/Model/EncodeTask.cs +++ b/win/C#/HandBrake.ApplicationServices/Model/EncodeTask.cs @@ -200,7 +200,7 @@ namespace HandBrake.ApplicationServices.Model /// /// Gets or sets VideoEncodeRateType. /// - public VideoEncodeRateType VideoEncodeRateType { get; set; } + public VideoEncodeMode VideoEncodeRateType { get; set; } /// /// Gets or sets Quality. diff --git a/win/C#/HandBrake.ApplicationServices/Model/Encoding/Anamorphic.cs b/win/C#/HandBrake.ApplicationServices/Model/Encoding/Anamorphic.cs index b3b524cf9..c958b777c 100644 --- a/win/C#/HandBrake.ApplicationServices/Model/Encoding/Anamorphic.cs +++ b/win/C#/HandBrake.ApplicationServices/Model/Encoding/Anamorphic.cs @@ -1,4 +1,9 @@ -namespace HandBrake.ApplicationServices.Model.Encoding +/* Anamorphic.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.ApplicationServices.Model.Encoding { using System.ComponentModel; diff --git a/win/C#/HandBrake.ApplicationServices/Model/Encoding/AudioEncoder.cs b/win/C#/HandBrake.ApplicationServices/Model/Encoding/AudioEncoder.cs index d98b120bf..c6f838ed7 100644 --- a/win/C#/HandBrake.ApplicationServices/Model/Encoding/AudioEncoder.cs +++ b/win/C#/HandBrake.ApplicationServices/Model/Encoding/AudioEncoder.cs @@ -1,4 +1,9 @@ -namespace HandBrake.ApplicationServices.Model.Encoding +/* AudioEncoder.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.ApplicationServices.Model.Encoding { using System.ComponentModel; diff --git a/win/C#/HandBrake.ApplicationServices/Model/Encoding/Decomb.cs b/win/C#/HandBrake.ApplicationServices/Model/Encoding/Decomb.cs index c09c5b98e..ae8b8a3d4 100644 --- a/win/C#/HandBrake.ApplicationServices/Model/Encoding/Decomb.cs +++ b/win/C#/HandBrake.ApplicationServices/Model/Encoding/Decomb.cs @@ -1,4 +1,9 @@ -namespace HandBrake.ApplicationServices.Model.Encoding +/* Decomb.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.ApplicationServices.Model.Encoding { /// /// Decomb Mode diff --git a/win/C#/HandBrake.ApplicationServices/Model/Encoding/Deinterlace.cs b/win/C#/HandBrake.ApplicationServices/Model/Encoding/Deinterlace.cs index 308b025a9..154428987 100644 --- a/win/C#/HandBrake.ApplicationServices/Model/Encoding/Deinterlace.cs +++ b/win/C#/HandBrake.ApplicationServices/Model/Encoding/Deinterlace.cs @@ -1,5 +1,13 @@ -namespace HandBrake.ApplicationServices.Model.Encoding +/* Deinterlace.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.ApplicationServices.Model.Encoding { + /// + /// The Deinterlace Filter + /// public enum Deinterlace { Off = 0, diff --git a/win/C#/HandBrake.ApplicationServices/Model/Encoding/Denoise.cs b/win/C#/HandBrake.ApplicationServices/Model/Encoding/Denoise.cs index 6698e775d..fc81e9f20 100644 --- a/win/C#/HandBrake.ApplicationServices/Model/Encoding/Denoise.cs +++ b/win/C#/HandBrake.ApplicationServices/Model/Encoding/Denoise.cs @@ -1,5 +1,13 @@ -namespace HandBrake.ApplicationServices.Model.Encoding +/* Denoise.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.ApplicationServices.Model.Encoding { + /// + /// The Denose Filters + /// public enum Denoise { Off = 0, diff --git a/win/C#/HandBrake.ApplicationServices/Model/Encoding/Detelecine.cs b/win/C#/HandBrake.ApplicationServices/Model/Encoding/Detelecine.cs index 19614971b..09c948dbd 100644 --- a/win/C#/HandBrake.ApplicationServices/Model/Encoding/Detelecine.cs +++ b/win/C#/HandBrake.ApplicationServices/Model/Encoding/Detelecine.cs @@ -1,5 +1,13 @@ -namespace HandBrake.ApplicationServices.Model.Encoding +/* Detelecine.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.ApplicationServices.Model.Encoding { + /// + /// Detelecine Filter + /// public enum Detelecine { Off = 0, diff --git a/win/C#/HandBrake.ApplicationServices/Model/Encoding/Mixdown.cs b/win/C#/HandBrake.ApplicationServices/Model/Encoding/Mixdown.cs index 373ee6b00..12611b454 100644 --- a/win/C#/HandBrake.ApplicationServices/Model/Encoding/Mixdown.cs +++ b/win/C#/HandBrake.ApplicationServices/Model/Encoding/Mixdown.cs @@ -1,4 +1,9 @@ -namespace HandBrake.ApplicationServices.Model.Encoding +/* Mixdown.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.ApplicationServices.Model.Encoding { using System.ComponentModel; diff --git a/win/C#/HandBrake.ApplicationServices/Model/Encoding/OutputFormat.cs b/win/C#/HandBrake.ApplicationServices/Model/Encoding/OutputFormat.cs index c5565914b..fa5c3225f 100644 --- a/win/C#/HandBrake.ApplicationServices/Model/Encoding/OutputFormat.cs +++ b/win/C#/HandBrake.ApplicationServices/Model/Encoding/OutputFormat.cs @@ -1,4 +1,9 @@ -namespace HandBrake.ApplicationServices.Model.Encoding +/* OutputFormat.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.ApplicationServices.Model.Encoding { using System.ComponentModel; diff --git a/win/C#/HandBrake.ApplicationServices/Model/Encoding/PointToPointMode.cs b/win/C#/HandBrake.ApplicationServices/Model/Encoding/PointToPointMode.cs index 8c58aa4cf..dcee4cac4 100644 --- a/win/C#/HandBrake.ApplicationServices/Model/Encoding/PointToPointMode.cs +++ b/win/C#/HandBrake.ApplicationServices/Model/Encoding/PointToPointMode.cs @@ -1,4 +1,9 @@ -namespace HandBrake.ApplicationServices.Model.Encoding +/* PointToPoint.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.ApplicationServices.Model.Encoding { /// /// Point to Point Mode diff --git a/win/C#/HandBrake.ApplicationServices/Model/Encoding/SubtitleTrack.cs b/win/C#/HandBrake.ApplicationServices/Model/Encoding/SubtitleTrack.cs index 5a3635689..f69abed7d 100644 --- a/win/C#/HandBrake.ApplicationServices/Model/Encoding/SubtitleTrack.cs +++ b/win/C#/HandBrake.ApplicationServices/Model/Encoding/SubtitleTrack.cs @@ -1,4 +1,4 @@ -/* Subtitle.cs $ +/* SubtitleTrack.cs $ This file is part of the HandBrake source code. Homepage: . It may be used under the terms of the GNU General Public License. */ diff --git a/win/C#/HandBrake.ApplicationServices/Model/Encoding/VideoEncodeMode.cs b/win/C#/HandBrake.ApplicationServices/Model/Encoding/VideoEncodeMode.cs new file mode 100644 index 000000000..cf42f3ca6 --- /dev/null +++ b/win/C#/HandBrake.ApplicationServices/Model/Encoding/VideoEncodeMode.cs @@ -0,0 +1,17 @@ +/* VideoEncoderMode.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.ApplicationServices.Model.Encoding +{ + /// + /// The Mode of Video Encoding. Bitrate, Filesize or Quality + /// + public enum VideoEncodeMode + { + TargetSize = 0, + AverageBitrate, + ConstantQuality + } +} diff --git a/win/C#/HandBrake.ApplicationServices/Model/Encoding/VideoEncodeRateType.cs b/win/C#/HandBrake.ApplicationServices/Model/Encoding/VideoEncodeRateType.cs deleted file mode 100644 index b128fdbc4..000000000 --- a/win/C#/HandBrake.ApplicationServices/Model/Encoding/VideoEncodeRateType.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace HandBrake.ApplicationServices.Model.Encoding -{ - public enum VideoEncodeRateType - { - TargetSize = 0, - AverageBitrate, - ConstantQuality - } -} diff --git a/win/C#/HandBrake.ApplicationServices/Model/Encoding/VideoEncoder.cs b/win/C#/HandBrake.ApplicationServices/Model/Encoding/VideoEncoder.cs index 4eddbf44b..b5a2556e3 100644 --- a/win/C#/HandBrake.ApplicationServices/Model/Encoding/VideoEncoder.cs +++ b/win/C#/HandBrake.ApplicationServices/Model/Encoding/VideoEncoder.cs @@ -1,7 +1,15 @@ -namespace HandBrake.ApplicationServices.Model.Encoding +/* VideoEncoder.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.ApplicationServices.Model.Encoding { using System.ComponentModel; + /// + /// The Video Encoder + /// public enum VideoEncoder { [Description("H.264 (x264)")] diff --git a/win/C#/HandBrake.ApplicationServices/Model/QueueTask.cs b/win/C#/HandBrake.ApplicationServices/Model/QueueTask.cs index 9f92ec609..83948a1f3 100644 --- a/win/C#/HandBrake.ApplicationServices/Model/QueueTask.cs +++ b/win/C#/HandBrake.ApplicationServices/Model/QueueTask.cs @@ -1,4 +1,4 @@ -/* QueueItem.cs $ +/* QueueTask.cs $ This file is part of the HandBrake source code. Homepage: . It may be used under the terms of the GNU General Public License. */ @@ -6,7 +6,7 @@ namespace HandBrake.ApplicationServices.Model { /// - /// The job. + /// The QueueTask. /// public class QueueTask { diff --git a/win/C#/HandBrake.ApplicationServices/Parsing/Title.cs b/win/C#/HandBrake.ApplicationServices/Parsing/Title.cs index e7266207f..3a9e6a56a 100644 --- a/win/C#/HandBrake.ApplicationServices/Parsing/Title.cs +++ b/win/C#/HandBrake.ApplicationServices/Parsing/Title.cs @@ -30,9 +30,9 @@ namespace HandBrake.ApplicationServices.Parsing /// public Title() { - AudioTracks = new List(); - Chapters = new List(); - Subtitles = new List(); + this.AudioTracks = new List(); + this.Chapters = new List(); + this.Subtitles = new List(); } #region Properties @@ -112,8 +112,8 @@ namespace HandBrake.ApplicationServices.Parsing /// /// Parse the Title Information /// - /// A stingreader of output data - /// A Title + /// A StringReader of output data + /// A Title Object public static Title Parse(StringReader output) { var thisTitle = new Title(); diff --git a/win/C#/HandBrake.ApplicationServices/Services/Encode.cs b/win/C#/HandBrake.ApplicationServices/Services/Encode.cs index bc5220e1e..9d3f7bb2e 100644 --- a/win/C#/HandBrake.ApplicationServices/Services/Encode.cs +++ b/win/C#/HandBrake.ApplicationServices/Services/Encode.cs @@ -446,8 +446,8 @@ namespace HandBrake.ApplicationServices.Services fileWriter = new StreamWriter(logFile) { AutoFlush = true }; - fileWriter.WriteLine(Logging.CreateCliLogHeader(encodeQueueTask)); - logBuffer.AppendLine(Logging.CreateCliLogHeader(encodeQueueTask)); + fileWriter.WriteLine(UtilityService.CreateCliLogHeader(encodeQueueTask)); + logBuffer.AppendLine(UtilityService.CreateCliLogHeader(encodeQueueTask)); } catch (Exception) { diff --git a/win/C#/HandBrake.ApplicationServices/Services/Scan.cs b/win/C#/HandBrake.ApplicationServices/Services/Scan.cs deleted file mode 100644 index d6925b304..000000000 --- a/win/C#/HandBrake.ApplicationServices/Services/Scan.cs +++ /dev/null @@ -1,355 +0,0 @@ -/* 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.ApplicationServices.Services -{ - using System; - using System.Diagnostics; - using System.IO; - using System.Text; - using System.Threading; - using System.Windows.Forms; - - using HandBrake.ApplicationServices.EventArgs; - using HandBrake.ApplicationServices.Functions; - using HandBrake.ApplicationServices.Parsing; - using HandBrake.ApplicationServices.Services.Interfaces; - - /// - /// Scan a Source - /// - public class ScanService : IScan - { - #region Private Variables - - /// - /// A Lock object - /// - private static readonly object locker = new object(); - - /// - /// The CLI data parser - /// - private Parser readData; - - /// - /// The Log Buffer - /// - private StringBuilder logBuffer; - - /// - /// The line number thats been read to in the log file - /// - private int logFilePosition; - - /// - /// The Process belonging to the CLI - /// - private Process hbProc; - - #endregion - - /// - /// Initializes a new instance of the class. - /// - public ScanService() - { - } - - #region Events - - /// - /// Scan has Started - /// - public event EventHandler ScanStared; - - /// - /// Scan has completed - /// - public event ScanCompletedStatus ScanCompleted; - - /// - /// Encode process has progressed - /// - public event ScanProgessStatus ScanStatusChanged; - - #endregion - - #region Public Properties - - /// - /// Gets a value indicating whether IsScanning. - /// - public bool IsScanning { get; private set; } - - /// - /// Gets the Souce Data. - /// - public DVD SouceData { get; private set; } - - /// - /// Gets ActivityLog. - /// - public string ActivityLog - { - get - { - if (IsScanning) - return readData.Buffer.ToString(); - - if (logBuffer == null) - { - ResetLogReader(false); - ReadLastScanFile(); - } - - return logBuffer != null ? logBuffer.ToString() : string.Empty; - } - } - - #endregion - - #region Public Methods - - /// - /// Scan a Source Path. - /// Title 0: scan all - /// - /// Path to the file to scan - /// int title number. 0 for scan all - public void Scan(string sourcePath, int title) - { - Thread t = new Thread(unused => this.ScanSource(sourcePath, title)); - t.Start(); - } - - /// - /// Kill the scan - /// - public void Stop() - { - try - { - // Try to clean things up as best as possible. - if (this.readData != null) - { - this.readData.OnScanProgress -= this.OnScanProgress; - } - - if (hbProc != null && !hbProc.HasExited) - hbProc.Kill(); - } - catch - { - // We don't really need to notify the user of any errors here. - } - } - #endregion - - #region Private Methods - - /// - /// Start a scan for a given source path and title - /// - /// Path to the source file - /// the title number to look at - private void ScanSource(object sourcePath, int title) - { - try - { - IsScanning = true; - if (this.ScanStared != null) - { - this.ScanStared(this, new EventArgs()); - } - - ResetLogReader(true); - - string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe"); - string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + - "\\HandBrake\\logs"; - string dvdInfoPath = Path.Combine( - logDir, - string.Format("last_scan_log{0}.txt", Init.InstanceId == 0 ? string.Empty : Init.InstanceId.ToString())); - - // 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 extraArguments = string.Empty; - if (Init.DisableDvdNav) - { - extraArguments = " --no-dvdnav"; - } - - if (title > 0) - { - extraArguments += " --scan "; - } - - // Quick fix for "F:\\" style paths. Just get rid of the \\ so the CLI doesn't fall over. - // Sould probably clean up the escaping of the strings later. - string source = sourcePath.ToString().EndsWith("\\") ? sourcePath.ToString() : "\"" + sourcePath + "\""; - - this.hbProc = new Process - { - StartInfo = - { - FileName = handbrakeCLIPath, - Arguments = String.Format(@" -i ""{0}"" -t{1} {2} -v ", sourcePath, title, extraArguments), - RedirectStandardOutput = true, - RedirectStandardError = true, - UseShellExecute = false, - CreateNoWindow = true - } - }; - - string command = String.Format(@" -i {0} -t{1} {2} -v ", source, title, extraArguments); - - this.hbProc = new Process - { - StartInfo = - { - FileName = handbrakeCLIPath, - Arguments = command, - RedirectStandardOutput = true, - RedirectStandardError = true, - UseShellExecute = false, - CreateNoWindow = true - } - }; - - // Start the Scan - this.hbProc.Start(); - - this.readData = new Parser(this.hbProc.StandardError.BaseStream); - this.readData.OnScanProgress += this.OnScanProgress; - this.SouceData = DVD.Parse(this.readData); - - // Write the Buffer out to file. - using (StreamWriter scanLog = new StreamWriter(dvdInfoPath)) - { - // Only write the log file to disk if it's less than 100MB. - if (this.readData.Buffer.Length < 100000000) - { - scanLog.WriteLine(Logging.CreateCliLogHeader(null)); - scanLog.Write(this.readData.Buffer); - logBuffer.AppendLine(this.readData.Buffer.ToString()); - } - else - { - throw new Exception( - "The Log file has not been written to disk as it has grown above the 100MB limit. This indicates there was a problem during the scan process."); - } - } - - IsScanning = false; - - if (this.ScanCompleted != null) - { - this.ScanCompleted(this, new ScanCompletedEventArgs(true, null, string.Empty)); - } - } - catch (Exception exc) - { - this.Stop(); - - if (this.ScanCompleted != null) - this.ScanCompleted(this, new ScanCompletedEventArgs(false, exc, "An Error has occured in ScanService.ScanSource()")); - } - } - - /// - /// Read the log file - /// - private void ReadLastScanFile() - { - lock (locker) - { - // 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), - // we'll need to make a copy of it. - string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + - "\\HandBrake\\logs"; - string logFile = Path.Combine(logDir, string.Format("last_scan_log{0}.txt", Init.InstanceId == 0 ? string.Empty : Init.InstanceId.ToString())); - string logFile2 = Path.Combine(logDir, string.Format("tmp_appReadable_log{0}.txt", Init.InstanceId == 0 ? string.Empty : Init.InstanceId.ToString())); - - try - { - // Make sure the application readable log file does not already exist. FileCopy fill fail if it does. - if (File.Exists(logFile2)) - File.Delete(logFile2); - - // Copy the log file. - if (File.Exists(logFile)) - File.Copy(logFile, logFile2, true); - else - { - ResetLogReader(true); - return; - } - - // Start the Reader - // Only use text which continues on from the last read line - StreamReader sr = new StreamReader(logFile2); - string line; - int i = 1; - while ((line = sr.ReadLine()) != null) - { - if (i > logFilePosition) - { - logBuffer.AppendLine(line); - logFilePosition++; - } - i++; - } - sr.Close(); - sr.Dispose(); - } - catch (Exception exc) - { - ResetLogReader(true); - } - } - } - - /// - /// Reset the Log Reader - /// - /// - /// The add Header. - /// - private void ResetLogReader(bool addHeader) - { - logFilePosition = 0; - logBuffer = new StringBuilder(); - if (addHeader) - logBuffer.AppendLine(Logging.CreateCliLogHeader(null)); - } - - /// - /// 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) - { - ScanProgressEventArgs eventArgs = new ScanProgressEventArgs - { - CurrentTitle = currentTitle, - Titles = titleCount - }; - - if (this.ScanStatusChanged != null) - { - this.ScanStatusChanged(this, eventArgs); - } - } - - #endregion - } -} \ No newline at end of file diff --git a/win/C#/HandBrake.ApplicationServices/Services/ScanService.cs b/win/C#/HandBrake.ApplicationServices/Services/ScanService.cs new file mode 100644 index 000000000..9b85740cd --- /dev/null +++ b/win/C#/HandBrake.ApplicationServices/Services/ScanService.cs @@ -0,0 +1,354 @@ +/* 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.ApplicationServices.Services +{ + using System; + using System.Diagnostics; + using System.IO; + using System.Text; + using System.Threading; + using System.Windows.Forms; + + using HandBrake.ApplicationServices.EventArgs; + using HandBrake.ApplicationServices.Parsing; + using HandBrake.ApplicationServices.Services.Interfaces; + + /// + /// Scan a Source + /// + public class ScanService : IScan + { + #region Private Variables + + /// + /// A Lock object + /// + private static readonly object locker = new object(); + + /// + /// The CLI data parser + /// + private Parser readData; + + /// + /// The Log Buffer + /// + private StringBuilder logBuffer; + + /// + /// The line number thats been read to in the log file + /// + private int logFilePosition; + + /// + /// The Process belonging to the CLI + /// + private Process hbProc; + + #endregion + + /// + /// Initializes a new instance of the class. + /// + public ScanService() + { + } + + #region Events + + /// + /// Scan has Started + /// + public event EventHandler ScanStared; + + /// + /// Scan has completed + /// + public event ScanCompletedStatus ScanCompleted; + + /// + /// Encode process has progressed + /// + public event ScanProgessStatus ScanStatusChanged; + + #endregion + + #region Public Properties + + /// + /// Gets a value indicating whether IsScanning. + /// + public bool IsScanning { get; private set; } + + /// + /// Gets the Souce Data. + /// + public DVD SouceData { get; private set; } + + /// + /// Gets ActivityLog. + /// + public string ActivityLog + { + get + { + if (IsScanning) + return readData.Buffer.ToString(); + + if (logBuffer == null) + { + ResetLogReader(false); + ReadLastScanFile(); + } + + return logBuffer != null ? logBuffer.ToString() : string.Empty; + } + } + + #endregion + + #region Public Methods + + /// + /// Scan a Source Path. + /// Title 0: scan all + /// + /// Path to the file to scan + /// int title number. 0 for scan all + public void Scan(string sourcePath, int title) + { + Thread t = new Thread(unused => this.ScanSource(sourcePath, title)); + t.Start(); + } + + /// + /// Kill the scan + /// + public void Stop() + { + try + { + // Try to clean things up as best as possible. + if (this.readData != null) + { + this.readData.OnScanProgress -= this.OnScanProgress; + } + + if (hbProc != null && !hbProc.HasExited) + hbProc.Kill(); + } + catch + { + // We don't really need to notify the user of any errors here. + } + } + #endregion + + #region Private Methods + + /// + /// Start a scan for a given source path and title + /// + /// Path to the source file + /// the title number to look at + private void ScanSource(object sourcePath, int title) + { + try + { + IsScanning = true; + if (this.ScanStared != null) + { + this.ScanStared(this, new EventArgs()); + } + + ResetLogReader(true); + + string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe"); + string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + + "\\HandBrake\\logs"; + string dvdInfoPath = Path.Combine( + logDir, + string.Format("last_scan_log{0}.txt", Init.InstanceId == 0 ? string.Empty : Init.InstanceId.ToString())); + + // 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 extraArguments = string.Empty; + if (Init.DisableDvdNav) + { + extraArguments = " --no-dvdnav"; + } + + if (title > 0) + { + extraArguments += " --scan "; + } + + // Quick fix for "F:\\" style paths. Just get rid of the \\ so the CLI doesn't fall over. + // Sould probably clean up the escaping of the strings later. + string source = sourcePath.ToString().EndsWith("\\") ? sourcePath.ToString() : "\"" + sourcePath + "\""; + + this.hbProc = new Process + { + StartInfo = + { + FileName = handbrakeCLIPath, + Arguments = String.Format(@" -i ""{0}"" -t{1} {2} -v ", sourcePath, title, extraArguments), + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true + } + }; + + string command = String.Format(@" -i {0} -t{1} {2} -v ", source, title, extraArguments); + + this.hbProc = new Process + { + StartInfo = + { + FileName = handbrakeCLIPath, + Arguments = command, + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true + } + }; + + // Start the Scan + this.hbProc.Start(); + + this.readData = new Parser(this.hbProc.StandardError.BaseStream); + this.readData.OnScanProgress += this.OnScanProgress; + this.SouceData = DVD.Parse(this.readData); + + // Write the Buffer out to file. + using (StreamWriter scanLog = new StreamWriter(dvdInfoPath)) + { + // Only write the log file to disk if it's less than 100MB. + if (this.readData.Buffer.Length < 100000000) + { + scanLog.WriteLine(UtilityService.CreateCliLogHeader(null)); + scanLog.Write(this.readData.Buffer); + logBuffer.AppendLine(this.readData.Buffer.ToString()); + } + else + { + throw new Exception( + "The Log file has not been written to disk as it has grown above the 100MB limit. This indicates there was a problem during the scan process."); + } + } + + IsScanning = false; + + if (this.ScanCompleted != null) + { + this.ScanCompleted(this, new ScanCompletedEventArgs(true, null, string.Empty)); + } + } + catch (Exception exc) + { + this.Stop(); + + if (this.ScanCompleted != null) + this.ScanCompleted(this, new ScanCompletedEventArgs(false, exc, "An Error has occured in ScanService.ScanSource()")); + } + } + + /// + /// Read the log file + /// + private void ReadLastScanFile() + { + lock (locker) + { + // 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), + // we'll need to make a copy of it. + string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + + "\\HandBrake\\logs"; + string logFile = Path.Combine(logDir, string.Format("last_scan_log{0}.txt", Init.InstanceId == 0 ? string.Empty : Init.InstanceId.ToString())); + string logFile2 = Path.Combine(logDir, string.Format("tmp_appReadable_log{0}.txt", Init.InstanceId == 0 ? string.Empty : Init.InstanceId.ToString())); + + try + { + // Make sure the application readable log file does not already exist. FileCopy fill fail if it does. + if (File.Exists(logFile2)) + File.Delete(logFile2); + + // Copy the log file. + if (File.Exists(logFile)) + File.Copy(logFile, logFile2, true); + else + { + ResetLogReader(true); + return; + } + + // Start the Reader + // Only use text which continues on from the last read line + StreamReader sr = new StreamReader(logFile2); + string line; + int i = 1; + while ((line = sr.ReadLine()) != null) + { + if (i > logFilePosition) + { + logBuffer.AppendLine(line); + logFilePosition++; + } + i++; + } + sr.Close(); + sr.Dispose(); + } + catch (Exception exc) + { + ResetLogReader(true); + } + } + } + + /// + /// Reset the Log Reader + /// + /// + /// The add Header. + /// + private void ResetLogReader(bool addHeader) + { + logFilePosition = 0; + logBuffer = new StringBuilder(); + if (addHeader) + logBuffer.AppendLine(UtilityService.CreateCliLogHeader(null)); + } + + /// + /// 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) + { + ScanProgressEventArgs eventArgs = new ScanProgressEventArgs + { + CurrentTitle = currentTitle, + Titles = titleCount + }; + + if (this.ScanStatusChanged != null) + { + this.ScanStatusChanged(this, eventArgs); + } + } + + #endregion + } +} \ No newline at end of file diff --git a/win/C#/HandBrake.ApplicationServices/Services/UtilityService.cs b/win/C#/HandBrake.ApplicationServices/Services/UtilityService.cs new file mode 100644 index 000000000..b705cb8e1 --- /dev/null +++ b/win/C#/HandBrake.ApplicationServices/Services/UtilityService.cs @@ -0,0 +1,131 @@ +/* UtilityService.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.ApplicationServices.Services +{ + using System; + using System.Collections.Generic; + using System.Diagnostics; + using System.IO; + using System.Text; + using System.Windows.Forms; + + using HandBrake.ApplicationServices.Functions; + using HandBrake.ApplicationServices.Model; + + /// + /// A Set of Static Utilites + /// + public class UtilityService + { + /// + /// The Default Log Directory + /// + private static readonly string LogDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs"; + + /// + /// Clear all the log files older than 30 Days + /// + /// + /// The Number of Days to Keep + /// + public static void ClearLogFiles(int daysToKeep) + { + if (Directory.Exists(LogDir)) + { + // Get all the log files + DirectoryInfo info = new DirectoryInfo(LogDir); + FileInfo[] logFiles = info.GetFiles("*.txt"); + + // Delete Them + foreach (FileInfo file in logFiles) + { + if (file.LastWriteTime < DateTime.Now.AddDays(-daysToKeep)) + { + if (!file.Name.Contains("last_scan_log.txt") && !file.Name.Contains("last_encode_log.txt")) + { + File.Delete(file.FullName); + } + else if (file.Length > 104857600) + { + File.Delete(file.FullName); + } + } + } + } + } + + /// + /// Get a list of available DVD drives which are ready and contain DVD content. + /// + /// A List of Drives with their details + public static List GetDrives() + { + List drives = new List(); + DriveInfo[] theCollectionOfDrives = DriveInfo.GetDrives(); + int id = 0; + foreach (DriveInfo curDrive in theCollectionOfDrives) + { + if (curDrive.DriveType == DriveType.CDRom && curDrive.IsReady) + { + if (Directory.Exists(curDrive.RootDirectory + "VIDEO_TS") || Directory.Exists(curDrive.RootDirectory + "BDMV")) + { + drives.Add( + new DriveInformation + { + Id = id, + VolumeLabel = curDrive.VolumeLabel, + RootDirectory = curDrive.RootDirectory.ToString() + }); + id++; + } + } + } + + return drives; + } + + /// + /// Get the Process ID of HandBrakeCLI for the current instance. + /// + /// A list of processes + public static Process[] GetCliProcess() + { + return Process.GetProcessesByName("HandBrakeCLI"); + } + + /// + /// Add the CLI Query to the Log File. + /// + /// + /// The Encode Job Object + /// + /// + /// The create cli log header. + /// + public static string CreateCliLogHeader(QueueTask encJob) + { + StringBuilder logHeader = new StringBuilder(); + + logHeader.AppendLine(String.Format("# {0}", Init.HandBrakeGuiVersionString)); + logHeader.AppendLine(String.Format("# Running: {0}", Environment.OSVersion)); + logHeader.AppendLine(String.Format("# CPU: {0}", SystemInfo.GetCpuCount)); + logHeader.AppendLine(String.Format("# Ram: {0} MB", SystemInfo.TotalPhysicalMemory)); + logHeader.AppendLine(String.Format("# Screen: {0}x{1}", SystemInfo.ScreenBounds.Bounds.Width, SystemInfo.ScreenBounds.Bounds.Height)); + logHeader.AppendLine(String.Format("# Temp Dir: {0}", Path.GetTempPath())); + logHeader.AppendLine(String.Format("# Install Dir: {0}", Application.StartupPath)); + logHeader.AppendLine(String.Format("# Data Dir: {0}\n", Application.UserAppDataPath)); + + if (encJob != null) + { + logHeader.AppendLine(String.Format("# CLI Query: {0}", encJob.Query)); + logHeader.AppendLine(String.Format("# User Query: {0}", encJob.CustomQuery)); + } + logHeader.AppendLine("-------------------------------------------"); + + return logHeader.ToString(); + } + } +} -- cgit v1.2.3