Added option to disable certificate checking
[pithos-ms-client] / trunk / NetSparkle / NetSparkleAppCast.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.IO;
6 using System.Xml;
7 using System.Net;
8
9 namespace AppLimit.NetSparkle
10 {
11     public class NetSparkleAppCast
12     {
13         private NetSparkleConfiguration _config;
14         private String _castUrl;
15
16         private const String itemNode = "item";
17         private const String descriptionNode = "description";
18         private const String enclosureNode = "enclosure";
19         private const String releaseNotesLinkNode = "sparkle:releaseNotesLink";
20         private const String versionAttribute = "sparkle:version";
21         private const String dasSignature = "sparkle:dsaSignature";
22         private const String urlAttribute = "url";
23
24         public NetSparkleAppCast(String castUrl, NetSparkleConfiguration config)
25         {
26             _config     = config;
27             _castUrl    = castUrl;
28         }
29
30         public NetSparkleAppCastItem GetLatestVersion()
31         {
32             NetSparkleAppCastItem latestVersion = null;
33           
34             // build a http web request stream
35             WebRequest request = HttpWebRequest.Create(_castUrl);
36
37             // request the cast and build the stream
38             WebResponse response = request.GetResponse();
39
40             Stream inputstream = response.GetResponseStream();
41
42             NetSparkleAppCastItem currentItem = null;
43
44             XmlTextReader reader = new XmlTextReader(inputstream);
45             while(reader.Read())
46             {
47                 if ( reader.NodeType == XmlNodeType.Element)
48                 {
49                     switch (reader.Name)
50                     {
51                         case itemNode:
52                             {
53                                 currentItem = new NetSparkleAppCastItem();
54                                 break;
55                             }
56 /*
57                         case descriptionNode:
58                             {
59                                 if (currentItem != null)
60                                 {
61                                     currentItem.Summary = reader.ReadElementContentAsString();
62                                 }
63                                 break;
64                             }
65 */
66                         case releaseNotesLinkNode:
67                             {
68                                 currentItem.ReleaseNotesLink = reader.ReadString();
69                                 currentItem.ReleaseNotesLink = currentItem.ReleaseNotesLink.Trim('\n');
70                                 break;
71                             }
72                         case enclosureNode:
73                             {
74                                 currentItem.Version = reader.GetAttribute(versionAttribute);
75
76                                 var downloadLink = reader.GetAttribute(urlAttribute);
77                                 downloadLink = Environment.Is64BitOperatingSystem
78                                     ? downloadLink.Replace(".x86.", ".x64.")
79                                     : downloadLink.Replace(".x64.", ".x86.") ;
80                                 currentItem.DownloadLink = downloadLink;
81                                 currentItem.DSASignature = reader.GetAttribute(dasSignature);
82
83                                 break;
84                             }
85                     }
86                 }
87                 else if (reader.NodeType == XmlNodeType.EndElement)
88                 {
89                     switch (reader.Name)
90                     {
91                         case itemNode:
92                             {
93                                 if (latestVersion == null)
94                                     latestVersion = currentItem;
95                                 else if (currentItem.CompareTo(latestVersion) > 0 )
96                                 {
97                                         latestVersion = currentItem;
98                                 }
99                                 break;
100                             }                            
101                     }
102                 }                    
103             }
104
105             // add some other attributes
106             latestVersion.AppName = _config.ApplicationName;
107             latestVersion.AppVersionInstalled = _config.InstalledVersion;
108             
109             // go ahead
110             return latestVersion;
111         }
112             
113     }
114 }