From fe53ec37cbb0da69f4ef0b924303378397d49fc6 Mon Sep 17 00:00:00 2001 From: sr55 Date: Fri, 26 Jun 2015 20:14:25 +0000 Subject: WinGui: Completely replace the plist preset import code with the functionality built into libhb. This now allows the new json preset format to be imported, as well as the legacy plist format. git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@7318 b64f7644-9d1e-0410-96f1-a4d463321fa5 --- .../HandBrake.ApplicationServices.csproj | 2 +- .../Interop/HandBrakePresetService.cs | 22 ++ .../Interop/HbLib/HbFunctions.cs | 6 +- .../Json/Presets/PresetTransportContainer.cs | 41 ++ .../Services/Encode/Factories/EncodeFactory.cs | 4 +- .../Utilities/Converters.cs | 231 ----------- win/CS/HandBrakeWPF/HandBrakeWPF.csproj | 4 +- .../Presets/Factories/JsonPresetFactory.cs | 2 +- .../Services/Presets/Factories/PlistFactory.cs | 19 - .../Presets/Factories/PlistPresetFactory.cs | 440 --------------------- .../Services/Presets/Interfaces/IPresetService.cs | 8 + .../HandBrakeWPF/Services/Presets/PresetService.cs | 94 +++++ win/CS/HandBrakeWPF/Utilities/PList.cs | 155 -------- win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs | 71 +--- 14 files changed, 177 insertions(+), 922 deletions(-) create mode 100644 win/CS/HandBrake.ApplicationServices/Interop/Json/Presets/PresetTransportContainer.cs delete mode 100644 win/CS/HandBrake.ApplicationServices/Utilities/Converters.cs delete mode 100644 win/CS/HandBrakeWPF/Services/Presets/Factories/PlistPresetFactory.cs delete mode 100644 win/CS/HandBrakeWPF/Utilities/PList.cs diff --git a/win/CS/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj b/win/CS/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj index 524729f20..1a080047f 100644 --- a/win/CS/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj +++ b/win/CS/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj @@ -114,6 +114,7 @@ + @@ -193,7 +194,6 @@ - diff --git a/win/CS/HandBrake.ApplicationServices/Interop/HandBrakePresetService.cs b/win/CS/HandBrake.ApplicationServices/Interop/HandBrakePresetService.cs index e2610905d..8d4f34e9e 100644 --- a/win/CS/HandBrake.ApplicationServices/Interop/HandBrakePresetService.cs +++ b/win/CS/HandBrake.ApplicationServices/Interop/HandBrakePresetService.cs @@ -14,6 +14,7 @@ namespace HandBrake.ApplicationServices.Interop using System.Runtime.InteropServices; using HandBrake.ApplicationServices.Interop.HbLib; + using HandBrake.ApplicationServices.Interop.Helpers; using HandBrake.ApplicationServices.Interop.Json.Presets; using HandBrake.ApplicationServices.Services.Logging; using HandBrake.ApplicationServices.Services.Logging.Model; @@ -43,5 +44,26 @@ namespace HandBrake.ApplicationServices.Interop return presetList; } + + /// + /// The get preset from file. + /// + /// + /// The filename. + /// + /// + /// The . + /// + public static PresetTransportContainer GetPresetFromFile(string filename) + { + IntPtr presetStringPointer = HBFunctions.hb_presets_read_file_json(InteropUtilities.ToUtf8PtrFromString(filename)); + string presetJson = Marshal.PtrToStringAnsi(presetStringPointer); + + LogHelper.LogMessage(new LogMessage(presetJson, LogMessageType.libhb, LogLevel.debug)); + + PresetTransportContainer preset = JsonConvert.DeserializeObject(presetJson); + + return preset; + } } } diff --git a/win/CS/HandBrake.ApplicationServices/Interop/HbLib/HbFunctions.cs b/win/CS/HandBrake.ApplicationServices/Interop/HbLib/HbFunctions.cs index 1b68df438..d8d8289f1 100644 --- a/win/CS/HandBrake.ApplicationServices/Interop/HbLib/HbFunctions.cs +++ b/win/CS/HandBrake.ApplicationServices/Interop/HbLib/HbFunctions.cs @@ -440,8 +440,8 @@ namespace HandBrake.ApplicationServices.Interop.HbLib [DllImport("hb.dll", EntryPoint = "hb_presets_builtin_get_json", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr hb_presets_builtin_get_json(); - // hb_value_t * hb_plist_parse_file(const char *filename); - [DllImport("hb.dll", EntryPoint = "hb_plist_parse_file", CallingConvention = CallingConvention.Cdecl)] - public static extern IntPtr hb_plist_parse_file(IntPtr filename); + // char * hb_presets_read_file_json(const char *filename); + [DllImport("hb.dll", EntryPoint = "hb_presets_read_file_json", CallingConvention = CallingConvention.Cdecl)] + public static extern IntPtr hb_presets_read_file_json(IntPtr filename); } } diff --git a/win/CS/HandBrake.ApplicationServices/Interop/Json/Presets/PresetTransportContainer.cs b/win/CS/HandBrake.ApplicationServices/Interop/Json/Presets/PresetTransportContainer.cs new file mode 100644 index 000000000..8f1dd9e78 --- /dev/null +++ b/win/CS/HandBrake.ApplicationServices/Interop/Json/Presets/PresetTransportContainer.cs @@ -0,0 +1,41 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. +// +// +// The preset transport container. +// This is a model for importing the JSON / Plist presets into the GUI. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace HandBrake.ApplicationServices.Interop.Json.Presets +{ + using System.Collections.Generic; + + /// + /// The preset transport container. + /// This is a model for importing the JSON / Plist presets into the GUI. + /// + public class PresetTransportContainer + { + /// + /// Gets or sets the children array. + /// + public List PresetList { get; set; } + + /// + /// Gets or sets the version major. + /// + public string VersionMajor { get; set; } + + /// + /// Gets or sets the version micro. + /// + public string VersionMicro { get; set; } + + /// + /// Gets or sets the version minor. + /// + public string VersionMinor { get; set; } + } +} diff --git a/win/CS/HandBrake.ApplicationServices/Services/Encode/Factories/EncodeFactory.cs b/win/CS/HandBrake.ApplicationServices/Services/Encode/Factories/EncodeFactory.cs index e133492ff..8d93668bf 100644 --- a/win/CS/HandBrake.ApplicationServices/Services/Encode/Factories/EncodeFactory.cs +++ b/win/CS/HandBrake.ApplicationServices/Services/Encode/Factories/EncodeFactory.cs @@ -263,7 +263,7 @@ namespace HandBrake.ApplicationServices.Services.Encode.Factories { Video video = new Video(); - HBVideoEncoder videoEncoder = HandBrakeEncoderHelpers.VideoEncoders.FirstOrDefault(e => e.ShortName == ApplicationServices.Utilities.Converters.GetVideoEncoder(job.VideoEncoder)); + HBVideoEncoder videoEncoder = HandBrakeEncoderHelpers.VideoEncoders.FirstOrDefault(e => e.ShortName == EnumHelper.GetShortName(job.VideoEncoder)); Validate.NotNull(videoEncoder, "Video encoder " + job.VideoEncoder + " not recognized."); if (videoEncoder != null) { @@ -336,7 +336,7 @@ namespace HandBrake.ApplicationServices.Services.Encode.Factories audio.AudioList = new List(); foreach (AudioTrack item in job.AudioTracks) { - HBAudioEncoder encoder = HandBrakeEncoderHelpers.GetAudioEncoder(ApplicationServices.Utilities.Converters.GetCliAudioEncoder(item.Encoder)); + HBAudioEncoder encoder = HandBrakeEncoderHelpers.GetAudioEncoder(EnumHelper.GetShortName(item.Encoder)); Validate.NotNull(encoder, "Unrecognized audio encoder:" + item.Encoder); HBMixdown mixdown = HandBrakeEncoderHelpers.GetMixdown(EnumHelper.GetShortName(item.MixDown)); diff --git a/win/CS/HandBrake.ApplicationServices/Utilities/Converters.cs b/win/CS/HandBrake.ApplicationServices/Utilities/Converters.cs deleted file mode 100644 index dd38fb286..000000000 --- a/win/CS/HandBrake.ApplicationServices/Utilities/Converters.cs +++ /dev/null @@ -1,231 +0,0 @@ -// -------------------------------------------------------------------------------------------------------------------- -// -// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. -// -// -// A class to convert various things to native C# objects -// -// -------------------------------------------------------------------------------------------------------------------- - -namespace HandBrake.ApplicationServices.Utilities -{ - using System; - - using HandBrake.ApplicationServices.Interop.Model.Encoding; - using HandBrake.ApplicationServices.Services.Encode.Model.Models; - - /// - /// A class to convert various things to native C# objects - /// - public class Converters - { - /** - * TODO: - * - Many of these converters can be ditched at a later point. Should be able to model all this within the enums themsevles. - * - **/ - - #region Audio - - /// - /// Get the GUI equiv to a CLI mixdown - /// - /// - /// The Audio Mixdown - /// - /// - /// The GUI representation of the mixdown - /// - [Obsolete("Use EnumHelper instead")] - public static Mixdown GetAudioMixDown(string mixdown) - { - switch (mixdown.Trim()) - { - case "Mono": - return Mixdown.Mono; - case "Stereo": - return Mixdown.Stereo; - case "Dolby Surround": - return Mixdown.DolbySurround; - case "Dolby Pro Logic II": - return Mixdown.DolbyProLogicII; - case "5.1 Channels": - return Mixdown.FivePoint1Channels; - case "6.1 Channels": - return Mixdown.SixPoint1Channels; - case "7.1 Channels": - return Mixdown.SevenPoint1Channels; - case "7.1 (5F/2R/LFE)": - return Mixdown.Five_2_LFE; - case "None": - case "Passthru": - return Mixdown.None; - default: - return Mixdown.Auto; - } - } - - /// - /// Get the GUI equiv to a GUI audio encoder string - /// - /// - /// The Audio Encoder - /// - /// - /// The GUI representation of that audio encoder - /// - [Obsolete("Use EnumHelper instead")] - public static AudioEncoder GetAudioEncoder(string audioEnc) - { - switch (audioEnc) - { - case "AAC (faac)": - case "AAC (ffmpeg)": - case "AAC (avcodec)": - return AudioEncoder.ffaac; - case "AAC (FDK)": - case "AAC (CoreAudio)": - return AudioEncoder.fdkaac; - case "HE-AAC (FDK)": - case "HE-AAC (CoreAudio)": - case "HE-AAC": - return AudioEncoder.fdkheaac; - case "MP3 (lame)": - case "MP3": - return AudioEncoder.Lame; - case "Vorbis (vorbis)": - case "Vorbis": - return AudioEncoder.Vorbis; - case "AC3 (ffmpeg)": - case "AC3": - return AudioEncoder.Ac3; - case "AC3 Passthru": - return AudioEncoder.Ac3Passthrough; - case "DTS Passthru": - return AudioEncoder.DtsPassthrough; - case "DTS-HD Passthru": - return AudioEncoder.DtsHDPassthrough; - case "AAC Passthru": - return AudioEncoder.AacPassthru; - case "MP3 Passthru": - return AudioEncoder.Mp3Passthru; - case "FLAC (ffmpeg)": - case "FLAC 16-bit": - return AudioEncoder.ffflac; - case "FLAC (24-bit)": - case "FLAC 24-bit": - return AudioEncoder.ffflac24; - case "TrueHD Passthru": - return AudioEncoder.TrueHDPassthrough; - case "E-AC3 Passthru": - return AudioEncoder.EAc3Passthrough; - case "FLAC Passthru": - return AudioEncoder.FlacPassthru; - case "Auto Passthru": - return AudioEncoder.Passthrough; - default: - return AudioEncoder.ffaac; - } - } - - /// - /// Get the CLI Audio Encoder name - /// - /// - /// String The GUI Encode name - /// - /// - /// String CLI encoder name - /// - public static string GetCliAudioEncoder(AudioEncoder selectedEncoder) - { - return EnumHelper.GetShortName(selectedEncoder); - } - - #endregion - - #region Video - - /// - /// Get the Video Encoder for a given string - /// - /// - /// The encoder name - /// - /// - /// VideoEncoder enum object - /// - public static string GetVideoEncoder(VideoEncoder encoder) - { - switch (encoder) - { - case VideoEncoder.FFMpeg: - return "mpeg4"; - case VideoEncoder.FFMpeg2: - return "mpeg2"; - case VideoEncoder.X264: - return "x264"; - case VideoEncoder.QuickSync: - return "qsv_h264"; - case VideoEncoder.Theora: - return "theora"; - case VideoEncoder.X265: - return "x265"; - case VideoEncoder.VP8: - return "VP8"; - default: - return "x264"; - } - } - - #endregion - - #region File Format - - /// - /// Get the OutputFormat Enum for a given string - /// - /// - /// OutputFormat as a string - /// - /// - /// An OutputFormat Enum - /// - public static OutputFormat GetFileFormat(string format) - { - switch (format.ToLower()) - { - default: - return OutputFormat.Mp4; - case "m4v": - return OutputFormat.Mp4; - case "mkv": - return OutputFormat.Mkv; - } - } - - /// - /// Get the OutputFormat Enum for a given string - /// - /// - /// OutputFormat as a string - /// - /// - /// An OutputFormat Enum - /// - public static string GetFileFormat(OutputFormat format) - { - switch (format) - { - default: - return "mp4"; - case OutputFormat.Mp4: - return "m4v"; - case OutputFormat.Mkv: - return "mkv"; - } - } - - #endregion - } -} diff --git a/win/CS/HandBrakeWPF/HandBrakeWPF.csproj b/win/CS/HandBrakeWPF/HandBrakeWPF.csproj index 87a26cade..c7ebecb48 100644 --- a/win/CS/HandBrakeWPF/HandBrakeWPF.csproj +++ b/win/CS/HandBrakeWPF/HandBrakeWPF.csproj @@ -171,8 +171,8 @@ True + - @@ -191,8 +191,6 @@ - - diff --git a/win/CS/HandBrakeWPF/Services/Presets/Factories/JsonPresetFactory.cs b/win/CS/HandBrakeWPF/Services/Presets/Factories/JsonPresetFactory.cs index 82c5eb329..a95c9b1ea 100644 --- a/win/CS/HandBrakeWPF/Services/Presets/Factories/JsonPresetFactory.cs +++ b/win/CS/HandBrakeWPF/Services/Presets/Factories/JsonPresetFactory.cs @@ -46,7 +46,7 @@ namespace HandBrakeWPF.Services.Presets.Factories preset.Name = importedPreset.PresetName; preset.Description = importedPreset.PresetDescription; preset.UsePictureFilters = importedPreset.UsesPictureFilters; - preset.UseDeinterlace = importedPreset.PictureDecombDeinterlace; + preset.UseDeinterlace = !importedPreset.PictureDecombDeinterlace; preset.Task = new EncodeTask(); // Step 1, Create the EncodeTask Object that can be loaded into the UI. diff --git a/win/CS/HandBrakeWPF/Services/Presets/Factories/PlistFactory.cs b/win/CS/HandBrakeWPF/Services/Presets/Factories/PlistFactory.cs index eb38c62ad..d50f23be9 100644 --- a/win/CS/HandBrakeWPF/Services/Presets/Factories/PlistFactory.cs +++ b/win/CS/HandBrakeWPF/Services/Presets/Factories/PlistFactory.cs @@ -95,25 +95,6 @@ namespace HandBrakeWPF.Services.Presets.Factories xmlWriter.WriteEndElement(); } - /// - /// The get null bool value. - /// - /// - /// The value. - /// - /// - /// The System.String. - /// - private static string getNullBoolValue(bool? value) - { - if (!value.HasValue) - { - return "1"; - } - - return value.Value ? "1" : "0"; - } - /// /// Add the encode settings to the preset /// diff --git a/win/CS/HandBrakeWPF/Services/Presets/Factories/PlistPresetFactory.cs b/win/CS/HandBrakeWPF/Services/Presets/Factories/PlistPresetFactory.cs deleted file mode 100644 index d58e111eb..000000000 --- a/win/CS/HandBrakeWPF/Services/Presets/Factories/PlistPresetFactory.cs +++ /dev/null @@ -1,440 +0,0 @@ -// -------------------------------------------------------------------------------------------------------------------- -// -// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. -// -// -// A Factory to translate a Plist object into a Preset. -// -// -------------------------------------------------------------------------------------------------------------------- - -namespace HandBrakeWPF.Services.Presets.Factories -{ - using System.Collections.Generic; - using System.Collections.ObjectModel; - using System.ComponentModel; - using System.Globalization; - using System.Linq; - - using HandBrake.ApplicationServices.Services.Encode.Model; - using HandBrake.ApplicationServices.Services.Encode.Model.Models; - using HandBrake.ApplicationServices.Services.Encode.Model.Models.Video; - using HandBrake.ApplicationServices.Utilities; - using HandBrake.ApplicationServices.Interop.Model.Encoding; - - using HandBrakeWPF.Model.Audio; - using HandBrakeWPF.Model.Subtitles; - using HandBrakeWPF.Services.Presets; - using HandBrakeWPF.Services.Presets.Model; - using HandBrakeWPF.Utilities; - - using PresetPictureSettingsMode = HandBrakeWPF.Model.Picture.PresetPictureSettingsMode; - - /// - /// A Factory to translate a Plist object into a Preset. - /// - public class PlistPresetFactory - { - /// - /// The lang list. - /// - private static IDictionary langList; - - /// - /// Initializes static members of the class. - /// - static PlistPresetFactory() - { - IDictionary langMap = LanguageUtilities.MapLanguages(); - langList = (from entry in langMap select entry).ToDictionary(pair => pair.Value, pair => pair.Key); - } - - /// - /// The create preset. - /// - /// - /// The plist. - /// - /// - /// The . - /// - public static Preset CreatePreset(PList plist) - { - Preset preset = new Preset - { - Task = new EncodeTask(), - Category = PresetService.UserPresetCatgoryName, - AudioTrackBehaviours = new AudioBehaviours(), - SubtitleTrackBehaviours = new SubtitleBehaviours() - }; - - // Parse the settings out. - foreach (var item in plist) - { - if (item.Key == "AudioList") - { - List tracks = ParseAudioTracks(item.Value); - preset.Task.AudioTracks = new ObservableCollection(tracks); - } - else - { - ParseSetting(item, preset); - } - } - - // Handle the PictureDecombDeinterlace key - if (preset.UseDeinterlace) - { - preset.Task.Decomb = Decomb.Off; - preset.Task.CustomDecomb = string.Empty; - } - - // Depending on the selected preset options, we may need to change some settings around. - // If the user chose not to use fitlers, remove them. - if (!preset.UsePictureFilters) - { - preset.Task.Detelecine = Detelecine.Off; - preset.Task.Denoise = Denoise.Off; - preset.Task.Deinterlace = Deinterlace.Off; - preset.Task.Decomb = Decomb.Off; - preset.Task.Deblock = 0; - preset.Task.Grayscale = false; - } - - // IF we are using Source Max, Set the Max Width / Height values. - if (preset.PictureSettingsMode == PresetPictureSettingsMode.SourceMaximum) - { - preset.Task.MaxWidth = preset.Task.Height; - preset.Task.MaxHeight = preset.Task.Width; - } - - return preset; - } - - /// - /// Parse a setting and set it in the given preset. - /// - /// - /// The kvp setting pair. - /// - /// - /// The preset object. - /// - private static void ParseSetting(KeyValuePair kvp, Preset preset) - { - switch (kvp.Key) - { - // Output Settings - case "FileFormat": - preset.Task.OutputFormat = Converters.GetFileFormat(kvp.Value.Replace("file", string.Empty).Trim()); - break; - case "Mp4HttpOptimize": - preset.Task.OptimizeMP4 = kvp.Value == 1; - break; - case "Mp4iPodCompatible": - preset.Task.IPod5GSupport = kvp.Value == 1; - break; - - // Picture Settings - case "PictureAutoCrop": - preset.Task.HasCropping = kvp.Value != 1; - break; - case "PictureTopCrop": - preset.Task.Cropping.Top = kvp.Value; - break; - case "PictureBottomCrop": - preset.Task.Cropping.Bottom = kvp.Value; - break; - case "PictureLeftCrop": - preset.Task.Cropping.Left = kvp.Value; - break; - case "PictureRightCrop": - preset.Task.Cropping.Right = kvp.Value; - break; - case "PictureHeight": - preset.Task.Height = kvp.Value == null || kvp.Value == 0 ? null : kvp.Value; - break; - case "PictureWidth": - preset.Task.Width = kvp.Value == null || kvp.Value == 0 ? null : kvp.Value; - break; - case "PictureKeepRatio": - preset.Task.KeepDisplayAspect = kvp.Value == 1; - break; - case "PicturePAR": - preset.Task.Anamorphic = (Anamorphic)kvp.Value; - break; - case "PictureModulus": - preset.Task.Modulus = kvp.Value; - break; - - // Filters - case "PictureDeblock": - preset.Task.Deblock = kvp.Value; - break; - case "PictureDecomb": - preset.Task.Decomb = (Decomb)kvp.Value; - break; - case "PictureDecombCustom": - preset.Task.CustomDecomb = kvp.Value; - break; - case "PictureDecombDeinterlace": - preset.UseDeinterlace = kvp.Value == true; - break; - case "PictureDeinterlace": - preset.Task.Deinterlace = (Deinterlace)kvp.Value; - break; - case "PictureDeinterlaceCustom": - preset.Task.CustomDeinterlace = kvp.Value; - break; - case "PictureDenoise": - preset.Task.Denoise = (Denoise)kvp.Value; - break; - case "DenoisePreset": - preset.Task.DenoisePreset = (DenoisePreset)kvp.Value; // TODO to be confirmed. - break; - case "DenoiseTune": - preset.Task.DenoiseTune = (DenoiseTune)kvp.Value; // TODO to be confirmed. - break; - case "PictureDenoiseCustom": - preset.Task.CustomDenoise = kvp.Value; - break; - case "PictureDetelecine": - preset.Task.Detelecine = (Detelecine)kvp.Value; - break; - case "PictureDetelecineCustom": - preset.Task.CustomDetelecine = kvp.Value; - break; - - // Video Tab - case "VideoAvgBitrate": - if (!string.IsNullOrEmpty(kvp.Value)) - { - preset.Task.VideoBitrate = int.Parse(kvp.Value); - } - break; - case "VideoEncoder": - preset.Task.VideoEncoder = EnumHelper.GetValue(kvp.Value); - break; - case "VideoFramerate": - preset.Task.Framerate = kvp.Value == "Same as source" || string.IsNullOrEmpty(kvp.Value) ? null : double.Parse(kvp.Value, CultureInfo.InvariantCulture); - break; - case "VideoFramerateMode": - string parsedValue = kvp.Value; - switch (parsedValue) - { - case "vfr": - preset.Task.FramerateMode = FramerateMode.VFR; - break; - case "cfr": - preset.Task.FramerateMode = FramerateMode.CFR; - break; - default: - preset.Task.FramerateMode = FramerateMode.PFR; - break; - } - break; - case "VideoGrayScale": - preset.Task.Grayscale = kvp.Value == true; - break; - case "VideoQualitySlider": - preset.Task.Quality = double.Parse(kvp.Value.ToString(), CultureInfo.InvariantCulture); - break; - case "VideoQualityType": // The Type of Quality Mode used - preset.Task.VideoEncodeRateType = (VideoEncodeRateType)kvp.Value; - break; - case "VideoTurboTwoPass": - preset.Task.TurboFirstPass = kvp.Value == 1; - break; - case "VideoTwoPass": - preset.Task.TwoPass = kvp.Value == 1; - break; - - case "VideoOptionExtra": - preset.Task.ExtraAdvancedArguments = kvp.Value; - break; - case "VideoLevel": - preset.Task.VideoLevel = new VideoLevel(kvp.Value, kvp.Value); - break; - case "VideoProfile": - preset.Task.VideoProfile = new VideoProfile(kvp.Value, kvp.Value); - break; - case "VideoPreset": - preset.Task.VideoPreset = new VideoPreset(kvp.Value, kvp.Value); - break; - case "VideoTune": - string[] split = kvp.Value.ToString().Split(','); - foreach (var item in split) - { - preset.Task.VideoTunes.Add(new VideoTune(item, item)); - } - break; - - // Chapter Markers Tab - case "ChapterMarkers": - preset.Task.IncludeChapterMarkers = kvp.Value == true; - break; - - // Advanced tab - case "x264Option": - case "lavcOption": - preset.Task.AdvancedEncoderOptions = kvp.Value; - break; - - // Preset Information - case "PresetBuildNumber": - preset.Version = kvp.Value; - break; - case "PresetDescription": - preset.Description = kvp.Value; - break; - case "PresetName": - preset.Name = kvp.Value; - break; - case "Type": - // preset.Task.Type = kvp.Value; // TODO find out what this is - break; - case "UsesMaxPictureSettings": - // TODO Not sure if this is used now!? - break; - case "UsesPictureFilters": - preset.UsePictureFilters = kvp.Value == 1; - break; - case "UsesPictureSettings": - preset.PictureSettingsMode = (PresetPictureSettingsMode)kvp.Value; - break; - - // Allowed Passthru - case "AudioAllowAACPass": - preset.Task.AllowedPassthruOptions.AudioAllowAACPass = kvp.Value == true; - break; - case "AudioAllowAC3Pass": - preset.Task.AllowedPassthruOptions.AudioAllowAC3Pass = kvp.Value == true; - break; - case "AudioAllowDTSHDPass": - preset.Task.AllowedPassthruOptions.AudioAllowDTSHDPass = kvp.Value == true; - break; - case "AudioAllowDTSPass": - preset.Task.AllowedPassthruOptions.AudioAllowDTSPass = kvp.Value == true; - break; - case "AudioAllowMP3Pass": - preset.Task.AllowedPassthruOptions.AudioAllowMP3Pass = kvp.Value == true; - break; - case "AudioEncoderFallback": - preset.Task.AllowedPassthruOptions.AudioEncoderFallback = EnumHelper.GetValue(kvp.Value); - break; - - // Audio Defaults - case "AudioLanguageList": - preset.AudioTrackBehaviours.SelectedLangauges = new BindingList(ParseLangaugeCodeList(kvp.Value)); - break; - case "AudioSecondaryEncoderMode": - break; - case "AudioTrackSelectionBehavior": - preset.AudioTrackBehaviours.SelectedBehaviour = kvp.Value == "all" - ? AudioBehaviourModes.AllMatching - : kvp.Value == "first" - ? AudioBehaviourModes.FirstMatch - : AudioBehaviourModes.None; - break; - - // Subtitle Defaults - case "SubtitleAddForeignAudioSearch": - preset.SubtitleTrackBehaviours.AddForeignAudioScanTrack = kvp.Value == true; - break; - case "SubtitleAddCC": - preset.SubtitleTrackBehaviours.AddClosedCaptions = kvp.Value == true; - break; - case "SubtitleLanguageList": - preset.SubtitleTrackBehaviours.SelectedLangauges = new BindingList(ParseLangaugeCodeList(kvp.Value)); - break; - case "SubtitleTrackSelectionBehavior": - preset.SubtitleTrackBehaviours.SelectedBehaviour = kvp.Value == "all" - ? SubtitleBehaviourModes.AllMatching - : kvp.Value == "first" - ? SubtitleBehaviourModes.FirstMatch - : SubtitleBehaviourModes.None; - break; - } - } - - /// - /// Parse a number of audio tracks - /// - /// - /// The audio list. - /// - /// - /// The of audio tracks - /// - private static List ParseAudioTracks(IEnumerable audioList) - { - return audioList.Select(item => ParseAudioTrackParameters(item)).Cast().ToList(); - } - - /// - /// The parse langauge code list. - /// - /// - /// The languages. - /// - /// - /// The . - /// - private static IEnumerable ParseLangaugeCodeList(IEnumerable languages) - { - List languageCodesList = new List(); - foreach (var item in languages) - { - string language; - if (langList.TryGetValue(item.ToString(), out language)) - { - languageCodesList.Add(language); - } - } - - return languageCodesList; - } - - /// - /// Parse an audio track's parameters. - /// - /// - /// The audio track params - /// - /// - /// An Object - /// - private static AudioTrack ParseAudioTrackParameters(Dictionary audioTrack) - { - AudioTrack track = new AudioTrack(); - foreach (var item in audioTrack) - { - switch (item.Key) - { - case "AudioBitrate": - track.Bitrate = int.Parse(item.Value); - break; - case "AudioEncoder": - track.Encoder = Converters.GetAudioEncoder(item.Value.Trim()); - break; - case "AudioMixdown": - track.MixDown = Converters.GetAudioMixDown(item.Value.Trim()); - break; - case "AudioSamplerate": - track.SampleRate = item.Value == "Auto" ? 0 : double.Parse(item.Value); - break; - case "AudioTrack": - // track.SourceTrack = value; We don't do anything with this one. - break; - case "AudioTrackDRCSlider": - track.DRC = item.Value; - break; - case "AudioTrackGainSlider": - track.Gain = (int)item.Value; - break; - } - } - - return track; - } - } -} diff --git a/win/CS/HandBrakeWPF/Services/Presets/Interfaces/IPresetService.cs b/win/CS/HandBrakeWPF/Services/Presets/Interfaces/IPresetService.cs index 9035eae70..de76a1af3 100644 --- a/win/CS/HandBrakeWPF/Services/Presets/Interfaces/IPresetService.cs +++ b/win/CS/HandBrakeWPF/Services/Presets/Interfaces/IPresetService.cs @@ -45,6 +45,14 @@ namespace HandBrakeWPF.Services.Presets.Interfaces /// bool Add(Preset preset); + /// + /// The import. + /// + /// + /// The filename. + /// + void Import(string filename); + /// /// Update a preset /// diff --git a/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs b/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs index ec0098358..c6e3c3e4b 100644 --- a/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs +++ b/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs @@ -21,9 +21,14 @@ namespace HandBrakeWPF.Services.Presets using HandBrake.ApplicationServices.Exceptions; using HandBrake.ApplicationServices.Interop; using HandBrake.ApplicationServices.Interop.Json.Presets; + using HandBrake.ApplicationServices.Interop.Model.Encoding; using HandBrake.ApplicationServices.Services.Encode.Model.Models; using HandBrake.ApplicationServices.Utilities; + using HandBrakeWPF.Model.Audio; + using HandBrakeWPF.Model.Picture; + using HandBrakeWPF.Model.Subtitles; + using HandBrakeWPF.Properties; using HandBrakeWPF.Services.Interfaces; using HandBrakeWPF.Services.Presets.Factories; using HandBrakeWPF.Services.Presets.Interfaces; @@ -161,6 +166,95 @@ namespace HandBrakeWPF.Services.Presets return true; } + /// + /// The import. + /// + /// + /// The filename. + /// + public void Import(string filename) + { + // TODO needs a tidy up but will do for now. + if (!string.IsNullOrEmpty(filename)) + { + PresetTransportContainer container = HandBrakePresetService.GetPresetFromFile(filename); + + if (container == null || container.PresetList == null || container.PresetList.Count == 0) + { + this.errorService.ShowError(Resources.Main_PresetImportFailed, Resources.Main_PresetImportFailedSolution, string.Empty); + return; + } + + HBPreset hbPreset = container.PresetList.FirstOrDefault(); + + Preset preset = null; + try + { + preset = JsonPresetFactory.ImportPreset(hbPreset); + preset.Category = UserPresetCatgoryName; + preset.AudioTrackBehaviours = new AudioBehaviours(); + preset.SubtitleTrackBehaviours = new SubtitleBehaviours(); + + // Handle the PictureDecombDeinterlace key + if (preset.UseDeinterlace) + { + preset.Task.Decomb = Decomb.Off; + preset.Task.CustomDecomb = string.Empty; + } + + // Depending on the selected preset options, we may need to change some settings around. + // If the user chose not to use fitlers, remove them. + if (!preset.UsePictureFilters) + { + preset.Task.Detelecine = Detelecine.Off; + preset.Task.Denoise = Denoise.Off; + preset.Task.Deinterlace = Deinterlace.Off; + preset.Task.Decomb = Decomb.Off; + preset.Task.Deblock = 0; + preset.Task.Grayscale = false; + } + + // IF we are using Source Max, Set the Max Width / Height values. + if (preset.PictureSettingsMode == PresetPictureSettingsMode.SourceMaximum) + { + preset.Task.MaxWidth = preset.Task.Height; + preset.Task.MaxHeight = preset.Task.Width; + } + } + catch (Exception exc) + { + this.errorService.ShowError(Resources.Main_PresetImportFailed, Resources.Main_PresetImportFailedSolution, exc); + } + + if (preset == null) + { + this.errorService.ShowError(Resources.Main_PresetImportFailed, Resources.Main_PresetImportFailedSolution, string.Empty); + return; + } + + // TODO Better version checking. + + if (this.CheckIfPresetExists(preset.Name)) + { + if (!CanUpdatePreset(preset.Name)) + { + MessageBox.Show(Resources.Main_PresetErrorBuiltInName, Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error); + return; + } + + MessageBoxResult result = MessageBox.Show(Resources.Main_PresetOverwriteWarning, Resources.Overwrite, MessageBoxButton.YesNo, MessageBoxImage.Warning); + if (result == MessageBoxResult.Yes) + { + Update(preset); + } + } + else + { + Add(preset); + } + } + } + /// /// Update a preset /// diff --git a/win/CS/HandBrakeWPF/Utilities/PList.cs b/win/CS/HandBrakeWPF/Utilities/PList.cs deleted file mode 100644 index 1e3c30e20..000000000 --- a/win/CS/HandBrakeWPF/Utilities/PList.cs +++ /dev/null @@ -1,155 +0,0 @@ -// -------------------------------------------------------------------------------------------------------------------- -// -// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. -// -// -// A Helper class to parse plist files. -// -// -------------------------------------------------------------------------------------------------------------------- - -namespace HandBrakeWPF.Utilities -{ - using System; - using System.Collections.Generic; - using System.Linq; - using System.Xml.Linq; - - /// - /// A Helper class to parse plist files. - /// - public class PList : Dictionary - { - #region Constructors and Destructors - - /// - /// Initializes a new instance of the class. - /// - public PList() - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// The file. - /// - public PList(string file) - { - this.Load(file); - } - - #endregion - - #region Public Methods - - /// - /// Load a plist file. - /// - /// - /// The file name / path - /// - /// - /// True if successful, false otherwise. - /// - public bool Load(string file) - { - this.Clear(); - - XDocument doc = XDocument.Load(file); - XElement plist = doc.Element("plist"); - if (plist != null) - { - XElement array = plist.Element("array"); - if (array != null) - { - XElement dict = array.Element("dict"); - - if (dict != null) - { - IEnumerable dictElements = dict.Elements(); - this.Parse(this, dictElements); - return true; - } - } - } - - return false; - } - - #endregion - - #region Methods - - /// - /// Parse a list of elements - /// - /// - /// The dict. - /// - /// - /// The elements. - /// - private void Parse(PList dict, IEnumerable elements) - { - for (int i = 0; i < elements.Count(); i += 2) - { - XElement key = elements.ElementAt(i); - XElement val = elements.ElementAt(i + 1); - - dict[key.Value] = this.ParseValue(val); - } - } - - /// - /// The parse array. - /// - /// - /// The elements. - /// - /// - /// The . - /// - private List ParseArray(IEnumerable elements) - { - return elements.Select(e => this.ParseValue(e)).ToList(); - } - - /// - /// The parse value. - /// - /// - /// The XElement. - /// - /// - /// The parsed value object. - /// - private dynamic ParseValue(XElement val) - { - switch (val.Name.ToString()) - { - case "string": - return val.Value; - case "integer": - return int.Parse(val.Value); - case "real": - return float.Parse(val.Value); - case "true": - return true; - case "false": - return false; - case "dict": - var plist = new PList(); - this.Parse(plist, val.Elements()); - return plist; - case "array": - List list = this.ParseArray(val.Elements()); - return list; - default: - throw new ArgumentException("This plist file is not supported."); - } - } - - #endregion - } -} \ No newline at end of file diff --git a/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs b/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs index f995b4d2e..60b0c74a5 100644 --- a/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs +++ b/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs @@ -30,6 +30,7 @@ namespace HandBrakeWPF.ViewModels using HandBrake.ApplicationServices.Services.Scan.Model; using HandBrake.ApplicationServices.Utilities; using HandBrake.ApplicationServices.Interop; + using HandBrake.ApplicationServices.Interop.Json.Presets; using HandBrakeWPF.Commands; using HandBrakeWPF.EventArgs; @@ -1813,74 +1814,10 @@ namespace HandBrakeWPF.ViewModels /// public void PresetImport() { - OpenFileDialog dialog = new OpenFileDialog() { Filter = "Plist (*.plist)|*.plist", CheckFileExists = true }; + OpenFileDialog dialog = new OpenFileDialog { Filter = "Preset Files|*.json;*.plist", CheckFileExists = true }; dialog.ShowDialog(); - string filename = dialog.FileName; - - if (!string.IsNullOrEmpty(filename)) - { - PList plist = new PList(filename); - - object build; - plist.TryGetValue("PresetBuildNumber", out build); - - string buildNumber = build as string; - if (buildNumber == null) - { - MessageBox.Show( - Resources.Preset_UnableToImport_Message, - Resources.Preset_UnableToImport_Header, - MessageBoxButton.YesNo, MessageBoxImage.Question); - return; - } - - if (buildNumber != HandBrakeUtils.Build.ToString(CultureInfo.InvariantCulture)) - { - MessageBoxResult result = MessageBox.Show( - Resources.Preset_OldVersion_Message, - Resources.Preset_OldVersion_Header, - MessageBoxButton.YesNo, MessageBoxImage.Question); - - if (result == MessageBoxResult.No) - { - return; - } - } - - Preset preset = null; - try - { - preset = PlistPresetFactory.CreatePreset(plist); - } - catch (Exception exc) - { - this.errorService.ShowError(Resources.Main_PresetImportFailed, Resources.Main_PresetImportFailedSolution, exc); - } - - if (preset != null) - { - if (this.presetService.CheckIfPresetExists(preset.Name)) - { - if (!presetService.CanUpdatePreset(preset.Name)) - { - MessageBox.Show(Resources.Main_PresetErrorBuiltInName, Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error); - return; - } - - MessageBoxResult result = MessageBox.Show(Resources.Main_PresetOverwriteWarning, Resources.Overwrite, MessageBoxButton.YesNo, MessageBoxImage.Warning); - if (result == MessageBoxResult.Yes) - { - presetService.Update(preset); - } - } - else - { - presetService.Add(preset); - } - } - - this.NotifyOfPropertyChange(() => this.Presets); - } + this.presetService.Import(dialog.FileName); + this.NotifyOfPropertyChange(() => this.Presets); } /// -- cgit v1.2.3