diff options
author | sr55 <[email protected]> | 2012-10-27 19:54:23 +0000 |
---|---|---|
committer | sr55 <[email protected]> | 2012-10-27 19:54:23 +0000 |
commit | ecc63f962529a9d85f619097e6658595997e33b3 (patch) | |
tree | 87ad6a8fccb0359855633e7b3311df7aed89de0f /win/CS/HandBrake.ApplicationServices/Utilities | |
parent | 8b7552139a158d00391d074d4e37d2e7c79961e2 (diff) |
WinGui: Initial work to overhaul the plist import code.
git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@5039 b64f7644-9d1e-0410-96f1-a4d463321fa5
Diffstat (limited to 'win/CS/HandBrake.ApplicationServices/Utilities')
-rw-r--r-- | win/CS/HandBrake.ApplicationServices/Utilities/PList.cs | 156 | ||||
-rw-r--r-- | win/CS/HandBrake.ApplicationServices/Utilities/PlistUtility.cs | 450 |
2 files changed, 156 insertions, 450 deletions
diff --git a/win/CS/HandBrake.ApplicationServices/Utilities/PList.cs b/win/CS/HandBrake.ApplicationServices/Utilities/PList.cs new file mode 100644 index 000000000..48212b519 --- /dev/null +++ b/win/CS/HandBrake.ApplicationServices/Utilities/PList.cs @@ -0,0 +1,156 @@ +// --------------------------------------------------------------------------------------------------------------------
+// <copyright file="PList.cs" company="HandBrake Project (http://handbrake.fr)">
+// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
+// </copyright>
+// <summary>
+// A Helper class to parse plist files.
+// </summary>
+// --------------------------------------------------------------------------------------------------------------------
+
+namespace HandBrake.ApplicationServices.Utilities
+{
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Xml.Linq;
+
+ /// <summary>
+ /// A Helper class to parse plist files.
+ /// </summary>
+ public class PList : Dictionary<string, dynamic>
+ {
+ #region Constructors and Destructors
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="PList"/> class.
+ /// </summary>
+ public PList()
+ {
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="PList"/> class.
+ /// </summary>
+ /// <param name="file">
+ /// The file.
+ /// </param>
+ public PList(string file)
+ {
+ this.Load(file);
+ }
+
+ #endregion
+
+ #region Public Methods
+
+
+ /// <summary>
+ /// Load a plist file.
+ /// </summary>
+ /// <param name="file">
+ /// The file name / path
+ /// </param>
+ /// <returns>
+ /// True if successful, false otherwise.
+ /// </returns>
+ 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<XElement> dictElements = dict.Elements();
+ this.Parse(this, dictElements);
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ #endregion
+
+ #region Methods
+
+ /// <summary>
+ /// Parse a list of elements
+ /// </summary>
+ /// <param name="dict">
+ /// The dict.
+ /// </param>
+ /// <param name="elements">
+ /// The elements.
+ /// </param>
+ private void Parse(PList dict, IEnumerable<XElement> 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);
+ }
+ }
+
+ /// <summary>
+ /// The parse array.
+ /// </summary>
+ /// <param name="elements">
+ /// The elements.
+ /// </param>
+ /// <returns>
+ /// The <see cref="List"/>.
+ /// </returns>
+ private List<dynamic> ParseArray(IEnumerable<XElement> elements)
+ {
+ return elements.Select(e => this.ParseValue(e)).ToList();
+ }
+
+ /// <summary>
+ /// The parse value.
+ /// </summary>
+ /// <param name="val">
+ /// The XElement.
+ /// </param>
+ /// <returns>
+ /// The parsed value object.
+ /// </returns>
+ 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<dynamic> 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/HandBrake.ApplicationServices/Utilities/PlistUtility.cs b/win/CS/HandBrake.ApplicationServices/Utilities/PlistUtility.cs index 797c456f3..bea765e27 100644 --- a/win/CS/HandBrake.ApplicationServices/Utilities/PlistUtility.cs +++ b/win/CS/HandBrake.ApplicationServices/Utilities/PlistUtility.cs @@ -9,16 +9,11 @@ namespace HandBrake.ApplicationServices.Utilities
{
- using System;
- using System.Collections.ObjectModel;
- using System.IO;
using System.Text;
- using System.Windows.Forms;
using System.Xml;
using HandBrake.ApplicationServices.Model;
using HandBrake.ApplicationServices.Model.Encoding;
- using HandBrake.ApplicationServices.Services;
using HandBrake.Interop.Model.Encoding;
/// <summary>
@@ -26,451 +21,6 @@ namespace HandBrake.ApplicationServices.Utilities /// </summary>
public class PlistUtility
{
- #region Import
-
- /// <summary>
- /// Import a Plist Preset
- /// </summary>
- /// <param name="filename">
- /// The filename.
- /// </param>
- /// <returns>
- /// A Preset Object
- /// </returns>
- public static Preset Import(string filename)
- {
- Preset preset = new Preset();
-
- XmlNode root = loadFile(filename);
- if (root == null) return null;
-
- // We'll query a query parser object and use it's public var structures to store all the data.
- // This will allow the preset loader logic to be used instead of writing custom logic just for this file.
- EncodeTask parsed = new EncodeTask();
- bool useFilters = false;
- PresetPictureSettingsMode pictureSettingsMode = PresetPictureSettingsMode.None;
- string qualityMode = string.Empty;
-
- #region Get a List of Audio Track Objects
-
- // This is the main <dict> with all the values
- XmlNode mainDict = root.ChildNodes[2].ChildNodes[0].FirstChild;
- XmlNode audioListDict = null;
- ObservableCollection<AudioTrack> audioTracks = new ObservableCollection<AudioTrack>();
-
- // Look for the AudioList key
- bool found = false;
- foreach (XmlNode node in mainDict.ChildNodes)
- {
- if (found)
- {
- audioListDict = node;
- break;
- }
-
- if (node.InnerText == "AudioList")
- {
- found = true; // We want the next node, as it is the value object.
- }
- }
-
- for (int i = 0; i < audioListDict.ChildNodes.Count; i++)
- {
- XmlNode audioChannel = audioListDict.ChildNodes[i];
- AudioTrack track = new AudioTrack();
-
- for (int subi = 0; subi < audioChannel.ChildNodes.Count; subi += 2)
- {
- // Audio Channel Information is here.
- string key = audioChannel.ChildNodes[subi].InnerText;
- string value = audioChannel.ChildNodes[subi + 1].InnerText;
- switch (key)
- {
- case "AudioBitrate":
- track.Bitrate = int.Parse(value);
- break;
- case "AudioEncoder":
- track.Encoder = Converters.GetAudioEncoder(value.Trim());
- break;
- case "AudioMixdown":
- track.MixDown = Converters.GetAudioMixDown(value.Trim());
- break;
- case "AudioSamplerate":
- track.SampleRate = value == "Auto" ? 0 : double.Parse(value);
- break;
- case "AudioTrack":
- //track.SourceTrack = value;
- break;
- case "AudioTrackDRCSlider":
- track.DRC = double.Parse(value);
- break;
- case "AudioTrackGainSlider":
- track.Gain = int.Parse(value.Replace("0.0", "0"));
- break;
- }
- }
- audioTracks.Add(track);
- parsed.AudioTracks = audioTracks;
- }
- #endregion
-
- #region Parse the reset of the plist keys into local variables
-
- XmlNode presetSettings = root.ChildNodes[2].ChildNodes[0].FirstChild;
-
- // Start from 2 to avoid the audio settings which we don't need.
- for (int i = 0; i < presetSettings.ChildNodes.Count; i += 2)
- {
- string key = presetSettings.ChildNodes[i].InnerText;
- string value = presetSettings.ChildNodes[i + 1].InnerText;
-
- switch (key)
- {
- // Output Settings
- case "FileFormat":
- parsed.OutputFormat = Converters.GetFileFormat(value.Replace("file", string.Empty).Trim());
- break;
- case "Mp4HttpOptimize":
- parsed.OptimizeMP4 = value == "1";
- break;
- case "Mp4LargeFile":
- parsed.LargeFile = value == "1";
- break;
- case "Mp4iPodCompatible":
- parsed.IPod5GSupport = value == "1";
- break;
-
- // Picture Settings
- case "PictureAutoCrop":
- parsed.HasCropping = value != "1";
- break;
- case "PictureTopCrop":
- parsed.Cropping.Top = int.Parse(value);
- break;
- case "PictureBottomCrop":
- parsed.Cropping.Bottom = int.Parse(value);
- break;
- case "PictureLeftCrop":
- parsed.Cropping.Left = int.Parse(value);
- break;
- case "PictureRightCrop":
- parsed.Cropping.Right = int.Parse(value);
- break;
- case "PictureHeight":
- parsed.Height = int.Parse(value);
- break;
- case "PictureWidth":
- parsed.Width = int.Parse(value);
- break;
- case "PictureKeepRatio":
- parsed.KeepDisplayAspect = value == "1";
- break;
- case "PicturePAR":
- switch (value)
- {
- case "0":
- parsed.Anamorphic = Anamorphic.None;
- break;
- default:
- parsed.Anamorphic = Anamorphic.Strict;
- break;
- case "2":
- parsed.Anamorphic = Anamorphic.Loose;
- break;
- case "3":
- parsed.Anamorphic = Anamorphic.Custom;
- break;
- }
- break;
-
- // Filters
- case "PictureDeblock":
- parsed.Deblock = int.Parse(value);
- break;
- case "PictureDecomb":
- parsed.Decomb = Decomb.Off;
- switch (value)
- {
- case "1":
- parsed.Decomb = Decomb.Custom;
- break;
- case "2":
- parsed.Decomb = Decomb.Default;
- break;
- case "3":
- parsed.Decomb = Decomb.Fast;
- break;
- }
- break;
- case "PictureDecombCustom":
- if (value != string.Empty)
- {
- parsed.CustomDecomb = value;
- }
- break;
- case "PictureDecombDeinterlace":
- // Not Used
- break;
- case "PictureDeinterlace":
- switch (value)
- {
- case "0":
- parsed.Deinterlace = Deinterlace.Off;
- break;
- case "1":
- parsed.Deinterlace = Deinterlace.Custom;
- break;
- case "2":
- parsed.Deinterlace = Deinterlace.Fast;
- break;
- case "3":
- parsed.Deinterlace = Deinterlace.Slow;
- break;
- case "4":
- parsed.Deinterlace = Deinterlace.Slower;
- break;
- }
- break;
- case "PictureDeinterlaceCustom":
- if (value != string.Empty)
- {
- parsed.CustomDeinterlace = value;
- }
- break;
- case "PictureDenoise":
- switch (value)
- {
- case "0":
- parsed.Denoise = Denoise.Off;
- break;
- case "1":
- parsed.Denoise = Denoise.Custom;
- break;
- case "2":
- parsed.Denoise = Denoise.Weak;
- break;
- case "3":
- parsed.Denoise = Denoise.Medium;
- break;
- case "4":
- parsed.Denoise = Denoise.Strong;
- break;
- }
-
- break;
- case "PictureDenoiseCustom":
- if (value != string.Empty)
- {
- parsed.CustomDenoise = value;
- }
- break;
- case "PictureDetelecine":
- parsed.Detelecine = Detelecine.Off;
- if (value == "1")
- {
- parsed.Detelecine = Detelecine.Custom;
- }
- if (value == "2")
- {
- parsed.Detelecine = Detelecine.Default;
- }
- break;
- case "PictureDetelecineCustom":
- if (value != string.Empty)
- {
- parsed.CustomDetelecine = value;
- }
- break;
-
- // Video Tab
- case "VideoAvgBitrate":
- if (!string.IsNullOrEmpty(value))
- {
- parsed.VideoBitrate = int.Parse(value);
- }
- break;
- case "VideoEncoder":
- parsed.VideoEncoder = EnumHelper<VideoEncoder>.GetValue(value);
- break;
- case "VideoFramerate":
-
- if (value == "Same as source")
- {
- parsed.Framerate = null;
- }
- else if (!string.IsNullOrEmpty(value))
- {
- parsed.Framerate = double.Parse(value);
- }
- break;
- case "VideoFramerateMode":
- switch (value)
- {
- case "vfr":
- parsed.FramerateMode = FramerateMode.VFR;
- break;
- case "cfr":
- parsed.FramerateMode = FramerateMode.CFR;
- break;
- default:
- parsed.FramerateMode = FramerateMode.PFR;
- break;
- }
- break;
- case "VideoGrayScale":
- parsed.Grayscale = value == "1";
- break;
- case "VideoQualitySlider":
- if (!string.IsNullOrEmpty(value))
- {
- parsed.Quality = double.Parse(value);
- }
- break;
- case "VideoQualityType": // The Type of Quality Mode used
- qualityMode = value;
- break;
- case "VideoTurboTwoPass":
- parsed.TurboFirstPass = value == "1";
- break;
- case "VideoTwoPass":
- parsed.TwoPass = value == "1";
- break;
-
- // Chapter Markers Tab
- case "ChapterMarkers":
- parsed.IncludeChapterMarkers = value == "1";
- break;
-
- // Advanced x264 tab
- case "x264Option":
- parsed.AdvancedEncoderOptions = value;
- break;
-
- // Preset Information
- case "PresetBuildNumber":
- preset.Version = value;
- break;
- case "PresetDescription":
- preset.Description = value;
- break;
- case "PresetName":
- preset.Name = value;
- break;
- case "Type":
- //parsed.Type = value; // TODO find out what this is
- break;
- case "UsesMaxPictureSettings":
- pictureSettingsMode = PresetPictureSettingsMode.SourceMaximum;
- break;
- case "UsesPictureFilters":
- useFilters = value == "1";
- break;
- case "UsesPictureSettings":
- pictureSettingsMode = PresetPictureSettingsMode.Custom;
- break;
-
- // Allowed Passthru
- case "AudioAllowAACPass":
- parsed.AllowedPassthruOptions.AudioAllowAACPass = value == "1";
- break;
- case "AudioAllowAC3Pass":
- parsed.AllowedPassthruOptions.AudioAllowAC3Pass = value == "1";
- break;
- case "AudioAllowDTSHDPass":
- parsed.AllowedPassthruOptions.AudioAllowDTSHDPass = value == "1";
- break;
- case "AudioAllowDTSPass":
- parsed.AllowedPassthruOptions.AudioAllowDTSPass = value == "1";
- break;
- case "AudioAllowMP3Pass":
- parsed.AllowedPassthruOptions.AudioAllowMP3Pass = value == "1";
- break;
- case "AudioEncoderFallback":
- parsed.AllowedPassthruOptions.AudioEncoderFallback = EnumHelper<AudioEncoder>.GetValue(value);
- break;
- }
- }
-
- // Kill any Quality values we don't need.
- switch (qualityMode)
- {
- case "1": // Avg Bitrate
- parsed.VideoEncodeRateType = VideoEncodeRateType.AverageBitrate;
- break;
- case "2": // CQ
- parsed.VideoEncodeRateType = VideoEncodeRateType.ConstantQuality;
- break;
- }
-
- // Default filters if we have to
- if (!useFilters)
- {
- parsed.Detelecine = Detelecine.Off;
- parsed.Denoise = Denoise.Off;
- parsed.Deinterlace = Deinterlace.Off;
- parsed.Decomb = Decomb.Off;
- parsed.Deblock = 0;
- parsed.Grayscale = false;
- }
-
- if (pictureSettingsMode == PresetPictureSettingsMode.SourceMaximum)
- {
- parsed.MaxWidth = parsed.Height;
- parsed.MaxHeight = parsed.Width;
- }
- #endregion
-
-
- preset.Task = parsed;
- preset.PictureSettingsMode = pictureSettingsMode;
- preset.UsePictureFilters = useFilters;
- preset.Category = PresetService.UserPresetCatgoryName;
-
- return preset;
- }
-
- private static XmlNode loadFile(string filename)
- {
- try
- {
- XmlNode root;
-
- if (!File.Exists(filename))
- return null;
-
- string fromfile = string.Empty;
- using (StreamReader sr = File.OpenText(filename))
- {
- int fileChar;
- while ((fileChar = sr.Read()) != -1) fromfile += Convert.ToChar(fileChar);
- }
-
- XmlDocument doc = new XmlDocument();
- doc.LoadXml(fromfile);
-
- root = doc;
- if (!root.HasChildNodes)
- {
- MessageBox.Show(
- "The Preset file you selected appears to be invlaid or from an older version of HandBrake",
- "Error",
- MessageBoxButtons.OK, MessageBoxIcon.Error);
- return null;
- }
-
- return root;
- }
- catch (Exception)
- {
- MessageBox.Show(
- "The Preset file you selected appears to be invlaid or from an older version of HandBrake.\n\n Please note, if you are exporting from the MacGui you may need to rebuild your preset so that it uses the current preset plist format.\n The MacGui does not currently update user presets automatically.",
- "Error",
- MessageBoxButtons.OK, MessageBoxIcon.Error);
- return null;
- }
- }
- #endregion
-
#region Export
/// <summary>
|