/[xmltv_parser]/trunk/libxmltv/Core/DataSourceBindable.cs
ViewVC logotype

Contents of /trunk/libxmltv/Core/DataSourceBindable.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 255 - (show annotations) (download)
Sat May 18 02:41:43 2013 UTC (7 years, 2 months ago) by william
File size: 19186 byte(s)
Hide property: RawDataSource by using: [Browsable(false)]

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using libxmltv.Interfaces;
6 using System.Collections;
7 using Enterprise.Logging;
8 using System.ComponentModel;
9
10 namespace libxmltv.Core
11 {
12 public class ChannelList : List<IXMLTVChannel>, IDataSourceBindable//, IDataConverter<IChannelDefintionList>
13 {
14 private class ChannelDefintionList : List<IChannelDefintion>, IDataSourceSortable, IDataSourceFilterable, IChannelDefintionList
15 {
16 public ChannelDefintionList() { }
17 public ChannelDefintionList(List<IChannelDefintion> collection) { collection.ForEach(s => this.Add(s)); }
18 #region IDataSourceSortable members
19 public void Sort(ref object source, bool descending, params string[] args)
20 {
21 }
22 #endregion
23 #region IDataSourceFilterable members
24 public void Filter(ref object source, params string[] args)
25 {
26 }
27 #endregion
28
29
30 }
31 private class ChannelDefintion : IChannelDefintion
32 {
33 public ChannelDefintion()
34 {
35 ChannelId = string.Empty;
36 ChannelName = string.Empty;
37 }
38 public string ChannelId { get; internal set; }
39 public string ChannelName { get; internal set; }
40
41 public override string ToString()
42 {
43 StringBuilder builder = new StringBuilder();
44 Type t = typeof(ChannelDefintion);
45 var props = t.GetProperties();
46 foreach (var prop in props)
47 {
48 string name = prop.Name;
49 object value = prop.GetValue(this, null);
50 builder.AppendFormat("\t{0}: '{1}'", name, value == null ? "null" : value.ToString());
51 }
52 return builder.ToString();
53 }
54
55 private IXMLTVChannel _RawDataSource;
56 [Browsable(false)]
57 public IXMLTVChannel RawDataSource { get { return _RawDataSource; } set { _RawDataSource = value; } }
58
59
60 public bool Equals(IChannelDefintion other)
61 {
62 return this.ToString() == other.ToString();
63 }
64 public override bool Equals(object obj)
65 {
66 if (obj == null) { throw new ArgumentNullException("obj", "Object to compare cannot be null"); }
67 if (obj.GetType().IsAssignableFrom(typeof(IChannelDefintion)))
68 {
69 return this.Equals((IChannelDefintion)obj);
70 }
71 return base.Equals(obj);
72 }
73 public override int GetHashCode()
74 {
75 return this.ToString().GetHashCode();
76 }
77
78
79 }
80 //static private List<string> known_columns;
81 //static ChannelList()
82 //{
83 // known_columns = new List<string>();
84 // known_columns.Add("Id");
85 // known_columns.Add("Name");
86 //}
87 public ChannelList() { }
88
89 //public object ConvertObjectData(object source) { return this.ConvertData(source); }
90 //public IChannelDefintionList ConvertData(object source)
91 //{
92 // //object t = source;
93 // if (source.GetType() != typeof(ChannelDefintionList)) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(ChannelDefintionList).Name)); }
94 // //if (type != typeof(IChannelDefintionList)) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", type.Name, typeof(IChannelDefintionList).Name)); }
95 // IChannelDefintionList t = (source as IChannelDefintionList);
96 // return t;
97 //}
98 /// <summary>
99 ///
100 /// </summary>
101 /// <param name="type"></param>
102 /// <returns></returns>
103 public object CreateBindableDataSource(out Type type)
104 {
105 type = typeof(IChannelDefintionList);
106 object bindable = new object();
107 ChannelDefintionList list = new ChannelDefintionList();
108
109 foreach (var t in this)
110 {
111 try
112 {
113 ChannelDefintion definition = new ChannelDefintion();
114 definition.RawDataSource = t;
115 definition.ChannelId = t.Id;
116 definition.ChannelName = t.MetaData[XMLTVConstants.Channels.ChannelDisplayName].FirstOrDefault().Value.ToString();
117 xmltv_logger.Verbose.Debug.WriteLine(definition.ToString());
118 list.Add(definition);
119 }
120 catch (Exception ex) { throw ex; }
121 }
122 list = new ChannelDefintionList(list.OrderBy(s => s.ChannelName).ToList());
123 bindable = list;
124 return bindable;
125 }
126
127 }
128 public class ProgramList : List<IXMLTVProgram>, IDataSourceBindable//, IDataConverter<IProgramDefinitionList>
129 {
130 private class ProgramDefinitionList : List<IProgramDefinition>, IDataSourceSortable, IDataSourceFilterable, IProgramDefinitionList
131 {
132 public ProgramDefinitionList() { }
133 public ProgramDefinitionList(List<IProgramDefinition> collection) { collection.ForEach(s => this.Add(s)); }
134 #region IDataSourceSortable members
135 public void Sort(ref object source, bool descending, params string[] args)
136 {
137 IProgramDefinitionList list = null;
138 IOrderedEnumerable<IProgramDefinition> ordered = null;
139 try
140 {
141 list = (ProgramDefinitionList)source;
142 var col = args.First().ToString().ToLower();
143 switch (col)
144 {
145 case "channelnumber": ordered = descending ? list.OrderByDescending(s => s.ChannelNumber) : list.OrderBy(s => s.ChannelNumber); break;
146 case "channelname": ordered = descending ? list.OrderByDescending(s => s.ChannelName) : list.OrderBy(s => s.ChannelName); break;
147 case "start": ordered = descending ? list.OrderByDescending(s => DateTime.Parse(s.Start)) : list.OrderBy(s => DateTime.Parse(s.Start)); break;
148 case "stop": ordered = descending ? list.OrderByDescending(s => DateTime.Parse(s.Stop)) : list.OrderBy(s => DateTime.Parse(s.Stop)); break;
149 case "description": ordered = descending ? list.OrderByDescending(s => s.Description) : list.OrderBy(s => s.Description); break;
150 case "title": ordered = descending ? list.OrderByDescending(s => s.Title) : list.OrderBy(s => s.Title); break;
151 case "subtitle": ordered = descending ? list.OrderByDescending(s => s.SubTitle) : list.OrderBy(s => s.SubTitle); break;
152 case "rating": ordered = descending ? list.OrderByDescending(s => s.Rating) : list.OrderBy(s => s.Rating); break;
153 }
154 //bool first = true;
155 foreach (var arg in args.Skip(1))
156 {
157 col = arg.ToLower();
158 switch (col)
159 {
160 case "channelnumber": ordered = descending ? ordered.ThenByDescending(s => s.ChannelNumber) : ordered.ThenBy(s => s.ChannelNumber); break;
161 case "channelname": ordered = descending ? list.OrderByDescending(s => s.ChannelName) : list.OrderBy(s => s.ChannelName); break;
162 case "start": ordered = descending ? ordered.ThenByDescending(s => DateTime.Parse(s.Start)) : ordered.ThenBy(s => DateTime.Parse(s.Start)); break;
163 case "stop": ordered = descending ? ordered.ThenByDescending(s => DateTime.Parse(s.Stop)) : ordered.ThenBy(s => DateTime.Parse(s.Stop)); break;
164 case "description": ordered = descending ? ordered.ThenByDescending(s => s.Description) : ordered.ThenBy(s => s.Description); break;
165 case "title": ordered = descending ? ordered.ThenByDescending(s => s.Title) : ordered.ThenBy(s => s.Title); break;
166 case "subtitle": ordered = descending ? ordered.ThenByDescending(s => s.SubTitle) : ordered.ThenBy(s => s.SubTitle); break;
167 case "rating": ordered = descending ? ordered.ThenByDescending(s => s.Rating) : ordered.ThenBy(s => s.Rating); break;
168 }
169 }
170 list = new ProgramDefinitionList(ordered.ToList());
171 }
172 catch (Exception ex) { throw ex; }
173 if (list != null) { source = list; }
174 }
175 #endregion
176 #region IDataSourceFilterable members
177 public void Filter(ref object source, params string[] args)
178 {
179 IProgramDefinitionList list = null;
180 List<IProgramDefinition> ordered = null;
181 try
182 {
183 list = (ProgramDefinitionList)source;
184 //var col = args.First().ToString().ToLower();
185 //var data = args.().ToString().ToLower();
186
187 if (args.Count() != 2) { throw new ArgumentOutOfRangeException("args", "Excpected 2 aguments: columnname and filtertext"); }
188
189 string columnname = args[0].ToLower();
190 string filtertext = args[1].ToLower();
191
192 switch (columnname)
193 {
194 case "channelnumber": ordered = list.ToList().FindAll(s => s.ChannelNumber == Convert.ToInt32(filtertext)); break;
195 case "channelname": ordered = list.ToList().FindAll(s => s.ChannelName.ToLower().Contains(filtertext)); break;
196 case "start": ordered = list.ToList().FindAll(s => s.Start.ToLower().Contains(filtertext)); break;
197 case "stop": ordered = list.ToList().FindAll(s => s.Stop.ToLower().Contains(filtertext)); break;
198 case "description": ordered = list.ToList().FindAll(s => s.Description.ToLower().Contains(filtertext)); break;
199 case "title": ordered = list.ToList().FindAll(s => s.Title.ToLower().Contains(filtertext)); break;
200 case "subtitle": ordered = list.ToList().FindAll(s => s.SubTitle.ToLower().Contains(filtertext)); break;
201 case "rating": ordered = list.ToList().FindAll(s => s.Rating.ToLower().Contains(filtertext)); break;
202 }
203 list = new ProgramDefinitionList(ordered.ToList());
204 if (list.Count == 0)
205 {
206 list = (ProgramDefinitionList)source;
207 gLog.Warn.WriteLine("Filering by: column='{0}' with filer='{1}' returned no data",columnname, filtertext);
208 }
209 }
210 catch (Exception ex) { throw ex; }
211 if (list != null) { source = list; }
212 }
213 #endregion
214
215 }
216 public class ProgramDefintion : IProgramDefinition, IEquatable<IProgramDefinition>
217 {
218 public ProgramDefintion()
219 {
220 //ChannelId = string.Empty;
221 ChannelNumber = 0;
222 ChannelName = string.Empty;
223 Start = new DateTime().ToDateTimeString();
224 Stop = new DateTime().ToDateTimeString();
225 Description = string.Empty;
226 Title = string.Empty;
227 SubTitle = string.Empty;
228 Rating = string.Empty;
229 }
230 //public string ChannelId { get; internal set; }
231 public int ChannelNumber { get; set; }
232 public string ChannelName { get; set; }
233 public string Start { get; set; }
234 public string Stop { get; set; }
235 public string Title { get; set; }
236 public string SubTitle { get; set; }
237 public string Description { get; set; }
238 public string Rating { get; set; }
239 public override string ToString()
240 {
241 StringBuilder builder = new StringBuilder();
242 Type t = typeof(ProgramDefintion);
243 var props = t.GetProperties();
244 foreach (var prop in props)
245 {
246 string name = prop.Name;
247 object value = prop.GetValue(this, null);
248 builder.AppendFormat("\t{0}: '{1}'", name, value == null ? "null" : value.ToString());
249 }
250 return builder.ToString();
251 }
252
253 private IXMLTVProgram _RawDataSource;
254 [Browsable(false)]
255 public IXMLTVProgram RawDataSource { get { return _RawDataSource; } set { _RawDataSource = value; } }
256
257 public bool Equals(IProgramDefinition other)
258 {
259 return this.ToString() == other.ToString();
260 }
261 public override bool Equals(object obj)
262 {
263 if (obj == null) { throw new ArgumentNullException("obj", "Object to compare cannot be null"); }
264 if (obj.GetType().IsAssignableFrom(typeof(IProgramDefinition)))
265 {
266 return this.Equals((IProgramDefinition)obj);
267 }
268 return base.Equals(obj);
269 }
270 public override int GetHashCode()
271 {
272 return this.ToString().GetHashCode();
273 }
274 }
275 public ProgramList() { }
276 //public object ConvertObjectData(object source) { return this.ConvertData(source); }
277 //public IProgramDefinitionList ConvertData(object source)
278 //{
279 // if (source.GetType() != typeof(ProgramDefinitionList)) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(ProgramDefinitionList).Name)); }
280 // //if (type != typeof(IProgramDefinitionList)) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", type.Name, typeof(IProgramDefinitionList).Name)); }
281 // IProgramDefinitionList t = (source as IProgramDefinitionList);
282 // return t;
283 //}
284 /// <summary>
285 ///
286 /// </summary>
287 /// <param name="type"></param>
288 /// <returns></returns>
289 public object CreateBindableDataSource(out Type type)
290 {
291 type = typeof(IProgramDefinitionList);
292 object bindable = new object();
293 ProgramDefinitionList list = new ProgramDefinitionList();
294
295 foreach (var t in this)
296 {
297 try
298 {
299 ProgramDefintion definition = new ProgramDefintion();
300 definition.RawDataSource = t;
301 string ChannelId = t.MetaData[XMLTVConstants.Programs.ProgramChannelId].ToString();
302 //definition.ChannelId = ChannelId;
303
304 var channels = XMLTV.GetChannels();
305 if (channels != null)
306 {
307 var channel = channels.Find(p => p.Id == ChannelId);
308 if (channel != null)
309 {
310 var channelname = channel.MetaData[XMLTVConstants.Channels.ChannelDisplayName].FirstOrDefault().Value.ToString();
311 var split = channelname.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
312 definition.ChannelNumber = Convert.ToInt32(split.First());
313 definition.ChannelName = string.IsNullOrEmpty(split.Last()) ? string.Empty : split.Last().ToString();
314
315 }
316 else
317 {
318 definition.ChannelNumber = 0;
319 }
320 }
321 else
322 {
323 definition.ChannelNumber = 0;
324 }
325 definition.Start = ((DateTime)t.MetaData[XMLTVConstants.Programs.ProgramStart]).ToDateTimeString();
326 definition.Stop = ((DateTime)t.MetaData[XMLTVConstants.Programs.ProgramStop]).ToDateTimeString();
327 definition.Description = t.MetaData[XMLTVConstants.Programs.ProgramDescription].ToString();
328 definition.Title = t.MetaData[XMLTVConstants.Programs.ProgramTitle].ToString();
329 definition.SubTitle = t.MetaData[XMLTVConstants.Programs.ProgramSubTitle].ToString();
330
331
332 var extras = t.GetExtraMetaData();
333 foreach (var extra in extras)
334 {
335 if (extra.Name.ToLower() == "rating")
336 {
337 var root = extra.AsXElement();
338 var child = root.Descendants("value").FirstOrDefault();
339 if (child == null) { throw new NullReferenceException("Expected rating element to have a child elemented named: value"); }
340 string value = child.Value;
341 definition.Rating = value;
342 }
343 }
344 xmltv_logger.Verbose.Debug.WriteLine(definition.ToString());
345 list.Add(definition);
346 }
347 catch (Exception ex) { throw ex; }
348 }
349
350 list = new ProgramDefinitionList(list.OrderBy(s => DateTime.Parse(s.Start)).ToList());
351
352 bindable = list;
353 return bindable;
354 }
355 }
356 public class ExtraList : List<IExtraMetaData>, IDataSourceBindable
357 {
358
359 public ExtraList() { }
360 /// <summary>
361 ///
362 /// </summary>
363 /// <param name="type"></param>
364 /// <returns></returns>
365 public object CreateBindableDataSource(out Type type)
366 {
367 type = typeof(object);
368 throw new NotImplementedException("Conversion of Extra MetaData to a bindable datasource has not been implemented.");
369 //object bindable = new object();
370 //return bindable;
371 }
372 //public object ConvertObjectData(object source) { return source; }
373 //public object Convert(object source)
374 //{
375 // throw new NotImplementedException("Conversion of Extra MetaData to a bindable datasource has not been implemented.");
376 //}
377 }
378 }

  ViewVC Help
Powered by ViewVC 1.1.22