summaryrefslogtreecommitdiffstats
path: root/win/C#/Functions/AppcastReader.cs
blob: 861bd0bae7d378877791dd7cb99359ca4c7b95da (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
/*  RssReader.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. */

using System;
using System.Xml;
using System.Text.RegularExpressions;
using System.IO;

namespace Handbrake.Functions
{
    public class AppcastReader
    {
        /// <summary>
        /// Get the build information from the required appcasts. Run before accessing the public vars.
        /// </summary>
        public void getInfo(string input)
        {
            // Get the correct Appcast and set input.
            XmlNode nodeItem = readRss(new XmlTextReader(new StringReader(input)));
            string result = nodeItem.InnerXml;

            // Regular Expressions
            Match ver = Regex.Match(result, @"sparkle:version=""([0-9]*)\""");
            Match verShort = Regex.Match(result, @"sparkle:shortVersionString=""([0-9].[0-9].[0-9]*)\""");

            build = ver.ToString().Replace("sparkle:version=", "").Replace("\"", "");
            version = verShort.ToString().Replace("sparkle:shortVersionString=", "").Replace("\"", "");
            downloadFile = nodeItem["windows"].InnerText;
            descriptionUrl = new Uri(nodeItem["sparkle:releaseNotesLink"].InnerText);
        }

        /// <summary>
        /// Read the RSS file.
        /// </summary>
        /// <param name="rssReader"></param>
        private static XmlNode readRss(XmlReader rssReader)
        {
            XmlNode nodeItem = null;
            XmlNode nodeChannel = null;
            XmlNode nodeRss = null;

            XmlDocument rssDoc = new XmlDocument();
            rssDoc.Load(rssReader);

            for (int i = 0; i < rssDoc.ChildNodes.Count; i++)
            {
                if (rssDoc.ChildNodes[i].Name == "rss")
                    nodeRss = rssDoc.ChildNodes[i];
            }

            if (nodeRss != null)
                for (int i = 0; i < nodeRss.ChildNodes.Count; i++)
                {
                    if (nodeRss.ChildNodes[i].Name == "channel")
                        nodeChannel = nodeRss.ChildNodes[i];
                }

            if (nodeChannel != null)
                for (int i = 0; i < nodeChannel.ChildNodes.Count; i++)
                {
                    if (nodeChannel.ChildNodes[i].Name == "item")
                        nodeItem = nodeChannel.ChildNodes[i];
                }

            return nodeItem;
        }

        /// <summary>
        /// Get Information about an update to HandBrake
        /// </summary>
        public Uri descriptionUrl { get; set; }

        /// <summary>
        /// Get HandBrake's version from the appcast.xml file.
        /// </summary>
        public string version { get; set; }

        /// <summary>
        /// Get HandBrake's Build from the appcast.xml file.
        /// </summary>
        public string build { get; set; }

        /// <summary>
        /// Get's the URL for update file.
        /// </summary>
        public string downloadFile { get; set; }
    }
}