1 /*
2  * Copyright (c) 2007 Carlos Martín Nieto <carlos@cmartin.tk>
3  *
4  * This file is released under the terms of the GNU GPLv2 or later
5  */
6 
7 using System;
8 using System.IO;
9 using System.Text;
10 using System.Xml.Serialization;
11 
12 namespace Smuxi.Common
13 {
14     [XmlType("feed")]
15     public class AtomFeed
16     {
17         [XmlElement("link")] public AtomLink[] Link = null;
18 
19         [XmlElement("updated")] public DateTime UpdateTime = DateTime.MinValue;
20         [XmlElement("modified")] public DateTime ModifyTime = DateTime.MinValue;
21         [XmlElement("title")] public AtomText Title = null;
22         [XmlElement("subtitle")] public string Subtitle = null;
23         [XmlElement("author")] public AtomAuthor Author = null;
24 
25         [XmlElement("entry")] public AtomEntry[] Entry;
26 
27         private static XmlSerializer ser = new XmlSerializer(typeof(AtomFeed),
28                                                              "http://www.w3.org/2005/Atom");
29 
LoadFromXml(string file)30         public static AtomFeed LoadFromXml(string file)
31         {
32             try {
33                 FileStream fs = new FileStream(file, FileMode.Open);
34                 return (AtomFeed)ser.Deserialize(fs);
35             } catch(FileNotFoundException){
36                 Console.Error.WriteLine("Unable to open file");
37                 return null;
38             }
39         }
40 
Load(StringReader sr)41         public static AtomFeed Load(StringReader sr)
42         {
43             return (AtomFeed)ser.Deserialize(sr);
44         }
45 
Load(Stream stream)46         public static AtomFeed Load(Stream stream)
47         {
48             return (AtomFeed) ser.Deserialize(stream);
49         }
50 
51         public DateTime Modified {
52             get {
53                 if(UpdateTime != DateTime.MinValue){
54                     return UpdateTime;
55                 } else {
56                     return ModifyTime;
57                 }
58             }
59         }
60 
61         public DateTime Updated {
62             get {
63                 return Modified;
64             }
65         }
66 
LinkByType(string type)67         public AtomLink LinkByType(string type)
68         {
69             foreach(AtomLink link in Link){
70                 if(link.Type == type){
71                     return link;
72                 }
73             }
74 
75             return null;
76         }
77     }
78 
79     [XmlType("author")]
80     public class AtomAuthor
81     {
82         [XmlElement("name")] public string Name;
83         [XmlElement("email")] public string Email;
84     }
85 
86     [XmlType("link")]
87     public class AtomLink
88     {
89         [XmlAttribute("href")] public string Url = null;
90         [XmlAttribute("rel")] public string Rel = null;
91         [XmlAttribute("type")] public string Type = null;
92     }
93 
94     [XmlType("entry")]
95     public class AtomEntry
96     {
97         [XmlElement("link")] public AtomLink[] Link = null;
98         [XmlElement("published")] public DateTime Published;
99         [XmlElement("updated")] public DateTime UpdateTime = DateTime.MinValue;
100         [XmlElement("modified")] public DateTime ModifyTime = DateTime.MinValue;
101         [XmlElement("title")] public AtomText Title;
102         [XmlElement("author")] public AtomAuthor Author = null;
103         [XmlElement("id")] public string Id;
104 
105         [XmlElement("content")] public AtomText[] Content;
106         [XmlElement("summary")] public AtomText Summary;
107 
108         public DateTime Modified {
109             get {
110                 if(UpdateTime != DateTime.MinValue){
111                     return UpdateTime;
112                 } else {
113                     return ModifyTime;
114                 }
115             }
116         }
117 
118         public DateTime Updated {
119             get {
120                 return Modified;
121             }
122         }
123 
LinkByType(string type)124         public AtomLink LinkByType(string type)
125         {
126             foreach(AtomLink link in Link){
127                 if(link.Type == type){
128                         return link;
129                 }
130             }
131             return null;
132         }
133 
ContentByType(string type)134         public AtomText ContentByType(string type)
135         {
136             foreach(AtomText text in Content){
137                 if(text.Type == type){
138                     return text;
139                 }
140             }
141 
142             return null;
143         }
144     }
145 
146     public class AtomText
147     {
148         [XmlText] public string Text = null;
149         [XmlAttribute("type")] public string Type = null;
150     }
151 }
152