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 |
134 |
using Enterprise.Logging; |
8 |
william |
255 |
using System.ComponentModel; |
9 |
william |
120 |
|
10 |
|
|
namespace libxmltv.Core |
11 |
|
|
{ |
12 |
william |
128 |
public class ChannelList : List<IXMLTVChannel>, IDataSourceBindable//, IDataConverter<IChannelDefintionList> |
13 |
william |
120 |
{ |
14 |
william |
128 |
private class ChannelDefintionList : List<IChannelDefintion>, IDataSourceSortable, IDataSourceFilterable, IChannelDefintionList |
15 |
william |
121 |
{ |
16 |
william |
126 |
public ChannelDefintionList() { } |
17 |
william |
128 |
public ChannelDefintionList(List<IChannelDefintion> collection) { collection.ForEach(s => this.Add(s)); } |
18 |
william |
125 |
#region IDataSourceSortable members |
19 |
william |
131 |
public void Sort(ref object source, bool descending, params string[] args) |
20 |
william |
125 |
{ |
21 |
|
|
} |
22 |
|
|
#endregion |
23 |
|
|
#region IDataSourceFilterable members |
24 |
william |
126 |
public void Filter(ref object source, params string[] args) |
25 |
william |
125 |
{ |
26 |
|
|
} |
27 |
|
|
#endregion |
28 |
william |
254 |
|
29 |
|
|
|
30 |
william |
121 |
} |
31 |
william |
128 |
private class ChannelDefintion : IChannelDefintion |
32 |
william |
126 |
{ |
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 |
william |
141 |
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 |
william |
254 |
|
55 |
william |
255 |
private IXMLTVChannel _RawDataSource; |
56 |
|
|
[Browsable(false)] |
57 |
|
|
public IXMLTVChannel RawDataSource { get { return _RawDataSource; } set { _RawDataSource = value; } } |
58 |
william |
254 |
|
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 |
william |
126 |
|
79 |
|
|
} |
80 |
william |
122 |
//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 |
william |
120 |
public ChannelList() { } |
88 |
|
|
|
89 |
william |
128 |
//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 |
william |
120 |
{ |
105 |
william |
128 |
type = typeof(IChannelDefintionList); |
106 |
william |
120 |
object bindable = new object(); |
107 |
william |
126 |
ChannelDefintionList list = new ChannelDefintionList(); |
108 |
william |
122 |
|
109 |
|
|
foreach (var t in this) |
110 |
|
|
{ |
111 |
|
|
try |
112 |
|
|
{ |
113 |
|
|
ChannelDefintion definition = new ChannelDefintion(); |
114 |
william |
254 |
definition.RawDataSource = t; |
115 |
william |
122 |
definition.ChannelId = t.Id; |
116 |
william |
141 |
definition.ChannelName = t.MetaData[XMLTVConstants.Channels.ChannelDisplayName].FirstOrDefault().Value.ToString(); |
117 |
|
|
xmltv_logger.Verbose.Debug.WriteLine(definition.ToString()); |
118 |
william |
122 |
list.Add(definition); |
119 |
|
|
} |
120 |
|
|
catch (Exception ex) { throw ex; } |
121 |
|
|
} |
122 |
william |
126 |
list = new ChannelDefintionList(list.OrderBy(s => s.ChannelName).ToList()); |
123 |
william |
122 |
bindable = list; |
124 |
william |
120 |
return bindable; |
125 |
|
|
} |
126 |
william |
128 |
|
127 |
william |
120 |
} |
128 |
william |
128 |
public class ProgramList : List<IXMLTVProgram>, IDataSourceBindable//, IDataConverter<IProgramDefinitionList> |
129 |
william |
120 |
{ |
130 |
william |
128 |
private class ProgramDefinitionList : List<IProgramDefinition>, IDataSourceSortable, IDataSourceFilterable, IProgramDefinitionList |
131 |
william |
121 |
{ |
132 |
william |
126 |
public ProgramDefinitionList() { } |
133 |
william |
128 |
public ProgramDefinitionList(List<IProgramDefinition> collection) { collection.ForEach(s => this.Add(s)); } |
134 |
william |
126 |
#region IDataSourceSortable members |
135 |
william |
131 |
public void Sort(ref object source, bool descending, params string[] args) |
136 |
william |
126 |
{ |
137 |
william |
131 |
IProgramDefinitionList list = null; |
138 |
william |
130 |
IOrderedEnumerable<IProgramDefinition> ordered = null; |
139 |
william |
126 |
try |
140 |
|
|
{ |
141 |
|
|
list = (ProgramDefinitionList)source; |
142 |
william |
131 |
var col = args.First().ToString().ToLower(); |
143 |
|
|
switch (col) |
144 |
william |
126 |
{ |
145 |
william |
137 |
case "channelnumber": ordered = descending ? list.OrderByDescending(s => s.ChannelNumber) : list.OrderBy(s => s.ChannelNumber); break; |
146 |
william |
131 |
case "channelname": ordered = descending ? list.OrderByDescending(s => s.ChannelName) : list.OrderBy(s => s.ChannelName); break; |
147 |
william |
252 |
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 |
william |
131 |
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 |
william |
227 |
case "rating": ordered = descending ? list.OrderByDescending(s => s.Rating) : list.OrderBy(s => s.Rating); break; |
153 |
william |
131 |
} |
154 |
|
|
//bool first = true; |
155 |
|
|
foreach (var arg in args.Skip(1)) |
156 |
|
|
{ |
157 |
|
|
col = arg.ToLower(); |
158 |
william |
130 |
switch (col) |
159 |
|
|
{ |
160 |
william |
137 |
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 |
william |
252 |
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 |
william |
131 |
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 |
william |
227 |
case "rating": ordered = descending ? ordered.ThenByDescending(s => s.Rating) : ordered.ThenBy(s => s.Rating); break; |
168 |
william |
130 |
} |
169 |
william |
126 |
} |
170 |
william |
131 |
list = new ProgramDefinitionList(ordered.ToList()); |
171 |
william |
126 |
} |
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 |
william |
132 |
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 |
william |
138 |
case "channelnumber": ordered = list.ToList().FindAll(s => s.ChannelNumber == Convert.ToInt32(filtertext)); break; |
195 |
william |
132 |
case "channelname": ordered = list.ToList().FindAll(s => s.ChannelName.ToLower().Contains(filtertext)); break; |
196 |
william |
142 |
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 |
william |
132 |
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 |
william |
227 |
case "rating": ordered = list.ToList().FindAll(s => s.Rating.ToLower().Contains(filtertext)); break; |
202 |
william |
132 |
} |
203 |
|
|
list = new ProgramDefinitionList(ordered.ToList()); |
204 |
william |
134 |
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 |
william |
132 |
} |
210 |
|
|
catch (Exception ex) { throw ex; } |
211 |
|
|
if (list != null) { source = list; } |
212 |
william |
126 |
} |
213 |
|
|
#endregion |
214 |
william |
254 |
|
215 |
william |
126 |
} |
216 |
william |
181 |
public class ProgramDefintion : IProgramDefinition, IEquatable<IProgramDefinition> |
217 |
william |
126 |
{ |
218 |
william |
122 |
public ProgramDefintion() |
219 |
|
|
{ |
220 |
william |
123 |
//ChannelId = string.Empty; |
221 |
william |
137 |
ChannelNumber = 0; |
222 |
william |
122 |
ChannelName = string.Empty; |
223 |
william |
141 |
Start = new DateTime().ToDateTimeString(); |
224 |
|
|
Stop = new DateTime().ToDateTimeString(); |
225 |
william |
122 |
Description = string.Empty; |
226 |
|
|
Title = string.Empty; |
227 |
|
|
SubTitle = string.Empty; |
228 |
william |
227 |
Rating = string.Empty; |
229 |
william |
122 |
} |
230 |
william |
123 |
//public string ChannelId { get; internal set; } |
231 |
william |
165 |
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 |
william |
227 |
public string Rating { get; set; } |
239 |
william |
141 |
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 |
william |
181 |
|
253 |
william |
255 |
private IXMLTVProgram _RawDataSource; |
254 |
|
|
[Browsable(false)] |
255 |
|
|
public IXMLTVProgram RawDataSource { get { return _RawDataSource; } set { _RawDataSource = value; } } |
256 |
william |
254 |
|
257 |
william |
181 |
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 |
william |
121 |
} |
275 |
william |
120 |
public ProgramList() { } |
276 |
william |
128 |
//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 |
william |
120 |
{ |
291 |
william |
128 |
type = typeof(IProgramDefinitionList); |
292 |
william |
120 |
object bindable = new object(); |
293 |
william |
126 |
ProgramDefinitionList list = new ProgramDefinitionList(); |
294 |
william |
121 |
|
295 |
william |
122 |
foreach (var t in this) |
296 |
|
|
{ |
297 |
|
|
try |
298 |
|
|
{ |
299 |
|
|
ProgramDefintion definition = new ProgramDefintion(); |
300 |
william |
254 |
definition.RawDataSource = t; |
301 |
william |
123 |
string ChannelId = t.MetaData[XMLTVConstants.Programs.ProgramChannelId].ToString(); |
302 |
|
|
//definition.ChannelId = ChannelId; |
303 |
william |
121 |
|
304 |
william |
122 |
var channels = XMLTV.GetChannels(); |
305 |
|
|
if (channels != null) |
306 |
|
|
{ |
307 |
william |
123 |
var channel = channels.Find(p => p.Id == ChannelId); |
308 |
william |
122 |
if (channel != null) |
309 |
|
|
{ |
310 |
william |
137 |
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 |
william |
122 |
} |
316 |
|
|
else |
317 |
|
|
{ |
318 |
william |
137 |
definition.ChannelNumber = 0; |
319 |
william |
122 |
} |
320 |
|
|
} |
321 |
|
|
else |
322 |
|
|
{ |
323 |
william |
137 |
definition.ChannelNumber = 0; |
324 |
william |
122 |
} |
325 |
william |
141 |
definition.Start = ((DateTime)t.MetaData[XMLTVConstants.Programs.ProgramStart]).ToDateTimeString(); |
326 |
|
|
definition.Stop = ((DateTime)t.MetaData[XMLTVConstants.Programs.ProgramStop]).ToDateTimeString(); |
327 |
william |
122 |
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 |
william |
141 |
|
331 |
william |
227 |
|
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 |
william |
141 |
xmltv_logger.Verbose.Debug.WriteLine(definition.ToString()); |
345 |
william |
122 |
list.Add(definition); |
346 |
|
|
} |
347 |
|
|
catch (Exception ex) { throw ex; } |
348 |
|
|
} |
349 |
william |
123 |
|
350 |
william |
252 |
list = new ProgramDefinitionList(list.OrderBy(s => DateTime.Parse(s.Start)).ToList()); |
351 |
william |
123 |
|
352 |
william |
122 |
bindable = list; |
353 |
william |
120 |
return bindable; |
354 |
|
|
} |
355 |
|
|
} |
356 |
|
|
public class ExtraList : List<IExtraMetaData>, IDataSourceBindable |
357 |
|
|
{ |
358 |
william |
121 |
|
359 |
william |
120 |
public ExtraList() { } |
360 |
william |
128 |
/// <summary> |
361 |
|
|
/// |
362 |
|
|
/// </summary> |
363 |
|
|
/// <param name="type"></param> |
364 |
|
|
/// <returns></returns> |
365 |
|
|
public object CreateBindableDataSource(out Type type) |
366 |
william |
120 |
{ |
367 |
william |
128 |
type = typeof(object); |
368 |
william |
124 |
throw new NotImplementedException("Conversion of Extra MetaData to a bindable datasource has not been implemented."); |
369 |
|
|
//object bindable = new object(); |
370 |
|
|
//return bindable; |
371 |
william |
120 |
} |
372 |
william |
128 |
//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 |
william |
120 |
} |
378 |
|
|
} |