// --------------------------------------------------------------------------------------------------------------------
//
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
//
//
// A Plist Helper
//
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrake.ApplicationServices.Functions
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
///
/// A Plist Helper
///
public class PlistHelper
{
///
/// Get the Audio Track XML Nodes from a plist preset file
///
///
/// A Plist preset file
///
///
/// A List of XmlNodes which contain audio tracks
///
public static List GetAudioTracks(string filename)
{
// Data Store
List audioTracks = new List();
// Load the file and get the Root note, or return if none found
XmlNode root = LoadFile(filename);
if (root == null)
{
return null;
}
// Get the Audio Tracks
XmlNode audioListDict = root.ChildNodes[2].ChildNodes[0].FirstChild.ChildNodes[1];
for (int i = 0; i < audioListDict.ChildNodes.Count; i++)
{
XmlNode audioChannel = audioListDict.ChildNodes[i]; //
audioTracks.Add(audioChannel);
}
return audioTracks;
}
///
/// Get the settings portion of a presets file
///
///
/// A Plist preset file
///
///
/// A Dictionary of plist key against an XML node containing the setting.
///
public static Dictionary GetPresetSettings(string filename)
{
// Data Store
Dictionary settings = new Dictionary();
// Load the file and get the Root note, or return if none found
XmlNode root = LoadFile(filename);
if (root == null)
{
return null;
}
// Get the Preset Settings, starting from the 3rd node (skipping the audio notes we've just prcessed)
XmlNode presetSettings = root.ChildNodes[2].ChildNodes[0].FirstChild;
for (int i = 2; i < presetSettings.ChildNodes.Count; i += 2)
{
string key = presetSettings.ChildNodes[i].InnerText;
XmlNode node = presetSettings.ChildNodes[i + 1];
settings.Add(key, node);
}
return settings;
}
///
/// Load a Plist Preset file and return it as an XML document
///
///
/// A Plist preset file
///
///
/// An XML document
///
private static XmlNode LoadFile(string filename)
{
try
{
if (!File.Exists(filename))
{
return null;
}
StreamReader sr = File.OpenText(filename);
string fromfile = string.Empty;
int fileChar;
while ((fileChar = sr.Read()) != -1)
{
fromfile += Convert.ToChar(fileChar);
}
XmlDocument doc = new XmlDocument();
doc.LoadXml(fromfile);
XmlNode root = doc;
if (!root.HasChildNodes)
{
return null;
}
return root;
}
catch (Exception)
{
return null;
}
}
}
}