Added NetSparkle for auto updating
[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 enclosureNode = "enclosure";
18         private const String releaseNotesLinkNode = "sparkle:releaseNotesLink";
19         private const String versionAttribute = "sparkle:version";
20         private const String dasSignature = "sparkle:dsaSignature";
21         private const String urlAttribute = "url";
22
23         public NetSparkleAppCast(String castUrl, NetSparkleConfiguration config)
24         {
25             _config     = config;
26             _castUrl    = castUrl;
27         }
28
29         public NetSparkleAppCastItem GetLatestVersion()
30         {
31             NetSparkleAppCastItem latestVersion = null;
32           
33             // build a http web request stream
34             WebRequest request = HttpWebRequest.Create(_castUrl);
35
36             // request the cast and build the stream
37             WebResponse response = request.GetResponse();
38
39             Stream inputstream = response.GetResponseStream();
40
41             NetSparkleAppCastItem currentItem = null;
42
43             XmlTextReader reader = new XmlTextReader(inputstream);
44             while(reader.Read())
45             {
46                 if ( reader.NodeType == XmlNodeType.Element)
47                 {
48                     switch(reader.Name)
49                     {
50                         case itemNode:
51                             {
52                                 currentItem = new NetSparkleAppCastItem();
53                                 break;
54                             }
55                         case releaseNotesLinkNode:
56                             {
57                                 currentItem.ReleaseNotesLink = reader.ReadString();
58                                 currentItem.ReleaseNotesLink = currentItem.ReleaseNotesLink.Trim('\n');
59                                 break;
60                             }                            
61                         case enclosureNode:
62                             {
63                                 currentItem.Version = reader.GetAttribute(versionAttribute);
64                                 currentItem.DownloadLink = reader.GetAttribute(urlAttribute);
65                                 currentItem.DSASignature = reader.GetAttribute(dasSignature);
66
67                                 break;
68                             }
69                     }
70                 }
71                 else if (reader.NodeType == XmlNodeType.EndElement)
72                 {
73                     switch (reader.Name)
74                     {
75                         case itemNode:
76                             {
77                                 if (latestVersion == null)
78                                     latestVersion = currentItem;
79                                 else if (currentItem.CompareTo(latestVersion) > 0 )
80                                 {
81                                         latestVersion = currentItem;
82                                 }
83                                 break;
84                             }                            
85                     }
86                 }                    
87             }
88
89             // add some other attributes
90             latestVersion.AppName = _config.ApplicationName;
91             latestVersion.AppVersionInstalled = _config.InstalledVersion;
92             
93             // go ahead
94             return latestVersion;
95         }
96             
97     }
98 }