a1f836caaf1eb568ae4a114fe9e6d57d46f1117c
[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                                 currentItem.DownloadLink = reader.GetAttribute(urlAttribute);
76                                 currentItem.DSASignature = reader.GetAttribute(dasSignature);
77
78                                 break;
79                             }
80                     }
81                 }
82                 else if (reader.NodeType == XmlNodeType.EndElement)
83                 {
84                     switch (reader.Name)
85                     {
86                         case itemNode:
87                             {
88                                 if (latestVersion == null)
89                                     latestVersion = currentItem;
90                                 else if (currentItem.CompareTo(latestVersion) > 0 )
91                                 {
92                                         latestVersion = currentItem;
93                                 }
94                                 break;
95                             }                            
96                     }
97                 }                    
98             }
99
100             // add some other attributes
101             latestVersion.AppName = _config.ApplicationName;
102             latestVersion.AppVersionInstalled = _config.InstalledVersion;
103             
104             // go ahead
105             return latestVersion;
106         }
107             
108     }
109 }