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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 132 - (hide annotations) (download)
Wed Mar 13 15:39:18 2013 UTC (6 years, 8 months ago) by william
File size: 13321 byte(s)
XMLTV Program(s):
* filtering by: channel, title, subtitle, and description have been implemented

1 william 120 using System;
2     using System.Collections.Generic;
3     using System.Linq;
4     using System.Text;
5     using libxmltv.Interfaces;
6 william 126 using System.Collections;
7 william 120
8     namespace libxmltv.Core
9     {
10 william 128 public class ChannelList : List<IXMLTVChannel>, IDataSourceBindable//, IDataConverter<IChannelDefintionList>
11 william 120 {
12 william 128 private class ChannelDefintionList : List<IChannelDefintion>, IDataSourceSortable, IDataSourceFilterable, IChannelDefintionList
13 william 121 {
14 william 126 public ChannelDefintionList() { }
15 william 128 public ChannelDefintionList(List<IChannelDefintion> collection) { collection.ForEach(s => this.Add(s)); }
16 william 125 #region IDataSourceSortable members
17 william 131 public void Sort(ref object source, bool descending, params string[] args)
18 william 125 {
19     }
20     #endregion
21     #region IDataSourceFilterable members
22 william 126 public void Filter(ref object source, params string[] args)
23 william 125 {
24     }
25     #endregion
26 william 121 }
27 william 128 private class ChannelDefintion : IChannelDefintion
28 william 126 {
29     public ChannelDefintion()
30     {
31     ChannelId = string.Empty;
32     ChannelName = string.Empty;
33     }
34     public string ChannelId { get; internal set; }
35     public string ChannelName { get; internal set; }
36    
37    
38     }
39 william 122 //static private List<string> known_columns;
40     //static ChannelList()
41     //{
42     // known_columns = new List<string>();
43     // known_columns.Add("Id");
44     // known_columns.Add("Name");
45     //}
46 william 120 public ChannelList() { }
47    
48 william 128 //public object ConvertObjectData(object source) { return this.ConvertData(source); }
49     //public IChannelDefintionList ConvertData(object source)
50     //{
51     // //object t = source;
52     // if (source.GetType() != typeof(ChannelDefintionList)) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(ChannelDefintionList).Name)); }
53     // //if (type != typeof(IChannelDefintionList)) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", type.Name, typeof(IChannelDefintionList).Name)); }
54     // IChannelDefintionList t = (source as IChannelDefintionList);
55     // return t;
56     //}
57     /// <summary>
58     ///
59     /// </summary>
60     /// <param name="type"></param>
61     /// <returns></returns>
62     public object CreateBindableDataSource(out Type type)
63 william 120 {
64 william 128 type = typeof(IChannelDefintionList);
65 william 120 object bindable = new object();
66 william 126 ChannelDefintionList list = new ChannelDefintionList();
67 william 122
68     foreach (var t in this)
69     {
70     try
71     {
72     ChannelDefintion definition = new ChannelDefintion();
73     definition.ChannelId = t.Id;
74 william 123 definition.ChannelName = t.MetaData[XMLTVConstants.Channels.ChannelDisplayName].FirstOrDefault().Value.ToString();
75 william 122 list.Add(definition);
76     }
77     catch (Exception ex) { throw ex; }
78     }
79 william 126 list = new ChannelDefintionList(list.OrderBy(s => s.ChannelName).ToList());
80 william 122 bindable = list;
81 william 120 return bindable;
82     }
83 william 128
84 william 120 }
85 william 128 public class ProgramList : List<IXMLTVProgram>, IDataSourceBindable//, IDataConverter<IProgramDefinitionList>
86 william 120 {
87 william 128 private class ProgramDefinitionList : List<IProgramDefinition>, IDataSourceSortable, IDataSourceFilterable, IProgramDefinitionList
88 william 121 {
89 william 126 public ProgramDefinitionList() { }
90 william 128 public ProgramDefinitionList(List<IProgramDefinition> collection) { collection.ForEach(s => this.Add(s)); }
91 william 126 #region IDataSourceSortable members
92 william 131 public void Sort(ref object source, bool descending, params string[] args)
93 william 126 {
94 william 131 IProgramDefinitionList list = null;
95 william 130 IOrderedEnumerable<IProgramDefinition> ordered = null;
96 william 126 try
97     {
98     list = (ProgramDefinitionList)source;
99 william 131 var col = args.First().ToString().ToLower();
100     switch (col)
101 william 126 {
102 william 131 case "channelname": ordered = descending ? list.OrderByDescending(s => s.ChannelName) : list.OrderBy(s => s.ChannelName); break;
103     case "start": ordered = descending ? list.OrderByDescending(s => s.Start) : list.OrderBy(s => s.Start); break;
104     case "stop": ordered = descending ? list.OrderByDescending(s => s.Stop) : list.OrderBy(s => s.Stop); break;
105     case "description": ordered = descending ? list.OrderByDescending(s => s.Description) : list.OrderBy(s => s.Description); break;
106     case "title": ordered = descending ? list.OrderByDescending(s => s.Title) : list.OrderBy(s => s.Title); break;
107     case "subtitle": ordered = descending ? list.OrderByDescending(s => s.SubTitle) : list.OrderBy(s => s.SubTitle); break;
108     }
109     //bool first = true;
110     foreach (var arg in args.Skip(1))
111     {
112     col = arg.ToLower();
113 william 130 switch (col)
114     {
115 william 131 case "channelname": ordered = descending ? ordered.ThenByDescending(s => s.ChannelName) : ordered.ThenBy(s => s.ChannelName); break;
116     case "start": ordered = descending ? ordered.ThenByDescending(s => s.Start) : ordered.ThenBy(s => s.Start); break;
117     case "stop": ordered = descending ? ordered.ThenByDescending(s => s.Stop) : ordered.ThenBy(s => s.Stop); break;
118     case "description": ordered = descending ? ordered.ThenByDescending(s => s.Description) : ordered.ThenBy(s => s.Description); break;
119     case "title": ordered = descending ? ordered.ThenByDescending(s => s.Title) : ordered.ThenBy(s => s.Title); break;
120     case "subtitle": ordered = descending ? ordered.ThenByDescending(s => s.SubTitle) : ordered.ThenBy(s => s.SubTitle); break;
121 william 130 }
122 william 126 }
123 william 131 list = new ProgramDefinitionList(ordered.ToList());
124 william 126 }
125     catch (Exception ex) { throw ex; }
126     if (list != null) { source = list; }
127     }
128     #endregion
129     #region IDataSourceFilterable members
130     public void Filter(ref object source, params string[] args)
131     {
132 william 132 IProgramDefinitionList list = null;
133     List<IProgramDefinition> ordered = null;
134     try
135     {
136     list = (ProgramDefinitionList)source;
137     //var col = args.First().ToString().ToLower();
138     //var data = args.().ToString().ToLower();
139    
140     if (args.Count() != 2) { throw new ArgumentOutOfRangeException("args", "Excpected 2 aguments: columnname and filtertext"); }
141    
142     string columnname = args[0].ToLower();
143     string filtertext = args[1].ToLower();
144    
145     switch (columnname)
146     {
147     case "channelname": ordered = list.ToList().FindAll(s => s.ChannelName.ToLower().Contains(filtertext)); break;
148     case "start":
149     case "stop":
150     throw new ArgumentException("columnname", string.Format("Filtering for column: '{0}' has not been implemented", columnname));
151     case "description": ordered = list.ToList().FindAll(s => s.Description.ToLower().Contains(filtertext)); break;
152     case "title": ordered = list.ToList().FindAll(s => s.Title.ToLower().Contains(filtertext)); break;
153     case "subtitle": ordered = list.ToList().FindAll(s => s.SubTitle.ToLower().Contains(filtertext)); break;
154     }
155     list = new ProgramDefinitionList(ordered.ToList());
156     }
157     catch (Exception ex) { throw ex; }
158     if (list != null) { source = list; }
159 william 126 }
160     #endregion
161     }
162 william 128 private class ProgramDefintion : IProgramDefinition
163 william 126 {
164 william 122 public ProgramDefintion()
165     {
166 william 123 //ChannelId = string.Empty;
167 william 122 ChannelName = string.Empty;
168     Start = new DateTime();
169     Stop = new DateTime();
170     Description = string.Empty;
171     Title = string.Empty;
172     SubTitle = string.Empty;
173     }
174 william 123 //public string ChannelId { get; internal set; }
175 william 122 public string ChannelName { get; internal set; }
176     public DateTime Start { get; internal set; }
177     public DateTime Stop { get; internal set; }
178     public string Title { get; internal set; }
179     public string SubTitle { get; internal set; }
180 william 125 public string Description { get; internal set; }
181    
182 william 126
183 william 121 }
184 william 120 public ProgramList() { }
185 william 128 //public object ConvertObjectData(object source) { return this.ConvertData(source); }
186     //public IProgramDefinitionList ConvertData(object source)
187     //{
188     // if (source.GetType() != typeof(ProgramDefinitionList)) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(ProgramDefinitionList).Name)); }
189     // //if (type != typeof(IProgramDefinitionList)) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", type.Name, typeof(IProgramDefinitionList).Name)); }
190     // IProgramDefinitionList t = (source as IProgramDefinitionList);
191     // return t;
192     //}
193     /// <summary>
194     ///
195     /// </summary>
196     /// <param name="type"></param>
197     /// <returns></returns>
198     public object CreateBindableDataSource(out Type type)
199 william 120 {
200 william 128 type = typeof(IProgramDefinitionList);
201 william 120 object bindable = new object();
202 william 126 ProgramDefinitionList list = new ProgramDefinitionList();
203 william 121
204 william 122 foreach (var t in this)
205     {
206     try
207     {
208     ProgramDefintion definition = new ProgramDefintion();
209 william 123 string ChannelId = t.MetaData[XMLTVConstants.Programs.ProgramChannelId].ToString();
210     //definition.ChannelId = ChannelId;
211 william 121
212 william 122 var channels = XMLTV.GetChannels();
213     if (channels != null)
214     {
215 william 123 var channel = channels.Find(p => p.Id == ChannelId);
216 william 122 if (channel != null)
217     {
218     definition.ChannelName = channel.MetaData[XMLTVConstants.Channels.ChannelDisplayName].FirstOrDefault().Value.ToString();
219     }
220     else
221     {
222     definition.ChannelName = string.Empty;
223     }
224     }
225     else
226     {
227     definition.ChannelName = string.Empty;
228     }
229     definition.Start = (DateTime)t.MetaData[XMLTVConstants.Programs.ProgramStart];
230     definition.Stop = (DateTime)t.MetaData[XMLTVConstants.Programs.ProgramStop];
231     definition.Description = t.MetaData[XMLTVConstants.Programs.ProgramDescription].ToString();
232     definition.Title = t.MetaData[XMLTVConstants.Programs.ProgramTitle].ToString();
233     definition.SubTitle = t.MetaData[XMLTVConstants.Programs.ProgramSubTitle].ToString();
234     list.Add(definition);
235     }
236     catch (Exception ex) { throw ex; }
237     }
238 william 123
239 william 126 list = new ProgramDefinitionList(list.OrderBy(s => s.Start).ToList());
240 william 123
241 william 122 bindable = list;
242 william 120 return bindable;
243     }
244     }
245     public class ExtraList : List<IExtraMetaData>, IDataSourceBindable
246     {
247 william 121
248 william 120 public ExtraList() { }
249 william 128 /// <summary>
250     ///
251     /// </summary>
252     /// <param name="type"></param>
253     /// <returns></returns>
254     public object CreateBindableDataSource(out Type type)
255 william 120 {
256 william 128 type = typeof(object);
257 william 124 throw new NotImplementedException("Conversion of Extra MetaData to a bindable datasource has not been implemented.");
258     //object bindable = new object();
259     //return bindable;
260 william 120 }
261 william 128 //public object ConvertObjectData(object source) { return source; }
262     //public object Convert(object source)
263     //{
264     // throw new NotImplementedException("Conversion of Extra MetaData to a bindable datasource has not been implemented.");
265     //}
266 william 120 }
267     }

  ViewVC Help
Powered by ViewVC 1.1.22