OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / Functions / AppcastReader.cs
index e14d2c1..e6bd64b 100644 (file)
 /*  RssReader.cs $\r
-       \r
-          This file is part of the HandBrake source code.\r
-          Homepage: <http://handbrake.fr>.\r
-          It may be used under the terms of the GNU General Public License. */\r
-\r
-using System;\r
-using System.Collections.Generic;\r
-using System.IO;\r
-using System.Xml;\r
-using System.Text.RegularExpressions;\r
+    This file is part of the HandBrake source code.\r
+    Homepage: <http://handbrake.fr>.\r
+    It may be used under the terms of the GNU General Public License. */\r
 \r
 namespace Handbrake.Functions\r
 {\r
+    using System;\r
+    using System.IO;\r
+    using System.Text.RegularExpressions;\r
+    using System.Xml;\r
+\r
+    /// <summary>\r
+    /// Appcast Reader - Used for parsing HandBrakes update file\r
+    /// </summary>\r
     public class AppcastReader\r
     {\r
-        XmlDocument rssDoc;\r
-        XmlNode nodeRss;\r
-        XmlNode nodeChannel;\r
-        XmlNode nodeItem;\r
-        private string hb_description;\r
-        private string hb_version;\r
-        private string hb_build;\r
-        private string hb_file;\r
+        /// <summary>\r
+        /// Gets Information about an update to HandBrake\r
+        /// </summary>\r
+        public Uri DescriptionUrl { get; private set; }\r
 \r
         /// <summary>\r
-        /// Get the build information from the required appcasts.\r
-        /// This must be run before calling any of the public return functions.\r
+        /// Gets HandBrake's version from the appcast.xml file.\r
         /// </summary>\r
-        public void getInfo()\r
-        {\r
-            Match ver;\r
-            int stable_build, unstable_build = 0;\r
-            string input, unstable_description = "", stable_description, unstable_version = "", stable_version;\r
-            string stable_file, unstable_file = "";\r
+        public string Version { get; private set; }\r
 \r
-            // Check the stable appcast and get the stable build number\r
-            readRss(new XmlTextReader(Properties.Settings.Default.appcast));\r
-            input = nodeItem.InnerXml;\r
-            ver = Regex.Match(input, @"sparkle:version=""([0-9]*)\""");\r
-            stable_build = int.Parse(ver.ToString().Replace("sparkle:version=", "").Replace("\"", ""));\r
-            ver = Regex.Match(input, @"sparkle:shortVersionString=""([0-9].[0-9].[0-9]*)\""");\r
-            stable_version = ver.ToString().Replace("sparkle:shortVersionString=", "").Replace("\"", "");\r
-            stable_description = nodeItem["description"].InnerText;\r
-            stable_file = nodeItem["windows"].InnerText;\r
+        /// <summary>\r
+        /// Gets HandBrake's Build from the appcast.xml file.\r
+        /// </summary>\r
+        public string Build { get; private set; }\r
 \r
-            // If this is a snapshot release, or the user wants to check for snapshot releases\r
-            if (Properties.Settings.Default.checkSnapshot == "Checked" || Properties.Settings.Default.hb_build.ToString().EndsWith("1"))\r
-            {\r
-                // Get the stable build\r
-                readRss(new XmlTextReader(Properties.Settings.Default.appcast_unstable));\r
-                input = nodeItem.InnerXml;\r
-                ver = Regex.Match(input, @"sparkle:version=""([0-9]*)\""");\r
-                unstable_build = int.Parse(ver.ToString().Replace("sparkle:version=", "").Replace("\"", ""));\r
-                ver = Regex.Match(input, @"sparkle:shortVersionString=""([0-9a-zA-Z.]*)\""");\r
-                unstable_version = ver.ToString().Replace("sparkle:shortVersionString=", "").Replace("\"", "");\r
-                unstable_description = nodeItem["description"].InnerText;\r
-                unstable_file = nodeItem["windows"].InnerText;\r
-            }\r
+        /// <summary>\r
+        /// Gets the URL for update file.\r
+        /// </summary>\r
+        public string DownloadFile { get; private set; }\r
 \r
-            // Set the global version information\r
-            if (stable_build >= unstable_build)\r
+        /// <summary>\r
+        /// Get the build information from the required appcasts. Run before accessing the public vars.\r
+        /// </summary>\r
+        /// <param name="input">\r
+        /// The input.\r
+        /// </param>\r
+        public void GetInfo(string input)\r
+        {\r
+            try\r
             {\r
-                hb_description = stable_description;\r
-                hb_version = stable_version;\r
-                hb_build = stable_build.ToString();\r
-                hb_file = stable_file;\r
+                // Get the correct Appcast and set input.\r
+                XmlNode nodeItem = ReadRss(new XmlTextReader(new StringReader(input)));\r
+                string result = nodeItem.InnerXml;\r
+\r
+                // Regular Expressions\r
+                Match ver = Regex.Match(result, @"sparkle:version=""([0-9]*)\""");\r
+                Match verShort = Regex.Match(result, @"sparkle:shortVersionString=""(([svn]*)([0-9.\s]*))\""");\r
+\r
+                this.Build = ver.ToString().Replace("sparkle:version=", string.Empty).Replace("\"", string.Empty);\r
+                this.Version = verShort.ToString().Replace("sparkle:shortVersionString=", string.Empty).Replace("\"", \r
+                                                                                                                string.\r
+                                                                                                                    Empty);\r
+                this.DownloadFile = nodeItem["windows"].InnerText;\r
+                this.DescriptionUrl = new Uri(nodeItem["sparkle:releaseNotesLink"].InnerText);\r
             }\r
-            else\r
+            catch (Exception)\r
             {\r
-                hb_description = unstable_description;\r
-                hb_version = unstable_version;\r
-                hb_build = unstable_build.ToString();\r
-                hb_file = unstable_file;\r
+                return;\r
             }\r
         }\r
 \r
         /// <summary>\r
         /// Read the RSS file.\r
         /// </summary>\r
-        /// <param name="rssReader"></param>\r
-        private void readRss(XmlTextReader rssReader)\r
+        /// <param name="rssReader">\r
+        /// The RSS reader\r
+        /// </param>\r
+        /// <returns>\r
+        /// The read rss.\r
+        /// </returns>\r
+        private static XmlNode ReadRss(XmlReader rssReader)\r
         {\r
-            rssDoc = new XmlDocument();\r
+            XmlNode nodeItem = null;\r
+            XmlNode nodeChannel = null;\r
+            XmlNode nodeRss = null;\r
+\r
+            XmlDocument rssDoc = new XmlDocument();\r
             rssDoc.Load(rssReader);\r
 \r
-            for (int i = 0; i < rssDoc.ChildNodes.Count; i++)\r
+            foreach (XmlNode t in rssDoc.ChildNodes)\r
             {\r
-                if (rssDoc.ChildNodes[i].Name == "rss")\r
-                    nodeRss = rssDoc.ChildNodes[i];\r
+                if (t.Name == "rss")\r
+                {\r
+                    nodeRss = t;\r
+                }\r
             }\r
 \r
-            for (int i = 0; i < nodeRss.ChildNodes.Count; i++)\r
+            if (nodeRss != null)\r
             {\r
-                if (nodeRss.ChildNodes[i].Name == "channel")\r
-                    nodeChannel = nodeRss.ChildNodes[i];\r
+                foreach (XmlNode t in nodeRss.ChildNodes)\r
+                {\r
+                    if (t.Name == "channel")\r
+                    {\r
+                        nodeChannel = t;\r
+                    }\r
+                }\r
             }\r
 \r
-            for (int i = 0; i < nodeChannel.ChildNodes.Count; i++)\r
+            if (nodeChannel != null)\r
             {\r
-                if (nodeChannel.ChildNodes[i].Name == "item")\r
-                    nodeItem = nodeChannel.ChildNodes[i];\r
+                foreach (XmlNode t in nodeChannel.ChildNodes)\r
+                {\r
+                    if (t.Name == "item")\r
+                    {\r
+                        nodeItem = t;\r
+                    }\r
+                }\r
             }\r
-        }\r
 \r
-        /// <summary>\r
-        /// Get Information about an update to HandBrake\r
-        /// </summary>\r
-        /// <returns></returns>\r
-        public string versionInfo()\r
-        {\r
-            return hb_description;\r
-        }\r
-\r
-        /// <summary>\r
-        /// Get HandBrake's version from the appcast.xml file.\r
-        /// </summary>\r
-        /// <returns></returns>\r
-        public string version()\r
-        {\r
-            return hb_version;\r
-        }\r
-\r
-        /// <summary>\r
-        /// Get HandBrake's Build from the appcast.xml file.\r
-        /// </summary>\r
-        /// <returns></returns>\r
-        public string build()\r
-        {\r
-            return hb_build;\r
-        }\r
-\r
-        /// <summary>\r
-        /// Get's the URL for update file.\r
-        /// </summary>\r
-        /// <returns></returns>\r
-        public string downloadFile()\r
-        {\r
-            return hb_file;\r
+            return nodeItem;\r
         }\r
     }\r
 }
\ No newline at end of file