summaryrefslogtreecommitdiffstats
path: root/win/CS/HandBrake.ApplicationServices/Functions/PlistHelper.cs
blob: 8ce092dade862e31b318ee6b526038b3a771bb15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*  PlistHelper.cs $
    This file is part of the HandBrake source code.
    Homepage: <http://handbrake.fr>.
    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.IO;
    using System.Xml;

    /// <summary>
    /// A Plist Helper
    /// </summary>
    public class PlistHelper
    {
        /// <summary>
        /// Get the Audio Track XML Nodes from a plist preset file
        /// </summary>
        /// <param name="filename">
        /// A Plist preset file
        /// </param>
        /// <returns>
        /// A List of XmlNodes which contain audio tracks
        /// </returns>
        public static List<XmlNode> GetAudioTracks(string filename)
        {
            // Data Store
            List<XmlNode> audioTracks = new List<XmlNode>();

            // 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]; // <dict>
                audioTracks.Add(audioChannel);
            }

            return audioTracks;
        }

        /// <summary>
        /// Get the settings portion of a presets file
        /// </summary>
        /// <param name="filename">
        /// A Plist preset file
        /// </param>
        /// <returns>
        /// A Dictionary of plist key against an XML node containing the setting.
        /// </returns>
        public static Dictionary<string, XmlNode> GetPresetSettings(string filename)
        {
            // Data Store
            Dictionary<string, XmlNode> settings = new Dictionary<string, XmlNode>();

            // 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;
        }

        /// <summary>
        /// Load a Plist Preset file and return it as an XML document
        /// </summary>
        /// <param name="filename">
        /// A Plist preset file
        /// </param>
        /// <returns>
        /// An XML document
        /// </returns>
        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;
            }
        }
    }
}