--- trunk/libxmltv/Core/DataSourceBindable.cs 2013/03/10 18:19:12 120 +++ trunk/libxmltv/Core/DataSourceBindable.cs 2013/03/14 13:08:20 141 @@ -3,37 +3,309 @@ using System.Linq; using System.Text; using libxmltv.Interfaces; +using System.Collections; +using Enterprise.Logging; namespace libxmltv.Core { - public class ChannelList : List, IDataSourceBindable + public class ChannelList : List, IDataSourceBindable//, IDataConverter { + private class ChannelDefintionList : List, IDataSourceSortable, IDataSourceFilterable, IChannelDefintionList + { + public ChannelDefintionList() { } + public ChannelDefintionList(List collection) { collection.ForEach(s => this.Add(s)); } + #region IDataSourceSortable members + public void Sort(ref object source, bool descending, params string[] args) + { + } + #endregion + #region IDataSourceFilterable members + public void Filter(ref object source, params string[] args) + { + } + #endregion + } + private class ChannelDefintion : IChannelDefintion + { + public ChannelDefintion() + { + ChannelId = string.Empty; + ChannelName = string.Empty; + } + public string ChannelId { get; internal set; } + public string ChannelName { get; internal set; } + + public override string ToString() + { + StringBuilder builder = new StringBuilder(); + Type t = typeof(ChannelDefintion); + var props = t.GetProperties(); + foreach (var prop in props) + { + string name = prop.Name; + object value = prop.GetValue(this, null); + builder.AppendFormat("\t{0}: '{1}'", name, value == null ? "null" : value.ToString()); + } + return builder.ToString(); + } + + } + //static private List known_columns; + //static ChannelList() + //{ + // known_columns = new List(); + // known_columns.Add("Id"); + // known_columns.Add("Name"); + //} public ChannelList() { } - public object CreateBindableDataSource() + //public object ConvertObjectData(object source) { return this.ConvertData(source); } + //public IChannelDefintionList ConvertData(object source) + //{ + // //object t = source; + // if (source.GetType() != typeof(ChannelDefintionList)) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(ChannelDefintionList).Name)); } + // //if (type != typeof(IChannelDefintionList)) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", type.Name, typeof(IChannelDefintionList).Name)); } + // IChannelDefintionList t = (source as IChannelDefintionList); + // return t; + //} + /// + /// + /// + /// + /// + public object CreateBindableDataSource(out Type type) { + type = typeof(IChannelDefintionList); object bindable = new object(); + ChannelDefintionList list = new ChannelDefintionList(); + + foreach (var t in this) + { + try + { + ChannelDefintion definition = new ChannelDefintion(); + definition.ChannelId = t.Id; + definition.ChannelName = t.MetaData[XMLTVConstants.Channels.ChannelDisplayName].FirstOrDefault().Value.ToString(); + xmltv_logger.Verbose.Debug.WriteLine(definition.ToString()); + list.Add(definition); + } + catch (Exception ex) { throw ex; } + } + list = new ChannelDefintionList(list.OrderBy(s => s.ChannelName).ToList()); + bindable = list; return bindable; } + } - public class ProgramList : List, IDataSourceBindable + public class ProgramList : List, IDataSourceBindable//, IDataConverter { - public ProgramList() { } + private class ProgramDefinitionList : List, IDataSourceSortable, IDataSourceFilterable, IProgramDefinitionList + { + public ProgramDefinitionList() { } + public ProgramDefinitionList(List collection) { collection.ForEach(s => this.Add(s)); } + #region IDataSourceSortable members + public void Sort(ref object source, bool descending, params string[] args) + { + IProgramDefinitionList list = null; + IOrderedEnumerable ordered = null; + try + { + list = (ProgramDefinitionList)source; + var col = args.First().ToString().ToLower(); + switch (col) + { + case "channelnumber": ordered = descending ? list.OrderByDescending(s => s.ChannelNumber) : list.OrderBy(s => s.ChannelNumber); break; + case "channelname": ordered = descending ? list.OrderByDescending(s => s.ChannelName) : list.OrderBy(s => s.ChannelName); break; + case "start": ordered = descending ? list.OrderByDescending(s => s.Start) : list.OrderBy(s => s.Start); break; + case "stop": ordered = descending ? list.OrderByDescending(s => s.Stop) : list.OrderBy(s => s.Stop); break; + case "description": ordered = descending ? list.OrderByDescending(s => s.Description) : list.OrderBy(s => s.Description); break; + case "title": ordered = descending ? list.OrderByDescending(s => s.Title) : list.OrderBy(s => s.Title); break; + case "subtitle": ordered = descending ? list.OrderByDescending(s => s.SubTitle) : list.OrderBy(s => s.SubTitle); break; + } + //bool first = true; + foreach (var arg in args.Skip(1)) + { + col = arg.ToLower(); + switch (col) + { + case "channelnumber": ordered = descending ? ordered.ThenByDescending(s => s.ChannelNumber) : ordered.ThenBy(s => s.ChannelNumber); break; + case "channelname": ordered = descending ? list.OrderByDescending(s => s.ChannelName) : list.OrderBy(s => s.ChannelName); break; + case "start": ordered = descending ? ordered.ThenByDescending(s => s.Start) : ordered.ThenBy(s => s.Start); break; + case "stop": ordered = descending ? ordered.ThenByDescending(s => s.Stop) : ordered.ThenBy(s => s.Stop); break; + case "description": ordered = descending ? ordered.ThenByDescending(s => s.Description) : ordered.ThenBy(s => s.Description); break; + case "title": ordered = descending ? ordered.ThenByDescending(s => s.Title) : ordered.ThenBy(s => s.Title); break; + case "subtitle": ordered = descending ? ordered.ThenByDescending(s => s.SubTitle) : ordered.ThenBy(s => s.SubTitle); break; + } + } + list = new ProgramDefinitionList(ordered.ToList()); + } + catch (Exception ex) { throw ex; } + if (list != null) { source = list; } + } + #endregion + #region IDataSourceFilterable members + public void Filter(ref object source, params string[] args) + { + IProgramDefinitionList list = null; + List ordered = null; + try + { + list = (ProgramDefinitionList)source; + //var col = args.First().ToString().ToLower(); + //var data = args.().ToString().ToLower(); + + if (args.Count() != 2) { throw new ArgumentOutOfRangeException("args", "Excpected 2 aguments: columnname and filtertext"); } + + string columnname = args[0].ToLower(); + string filtertext = args[1].ToLower(); + + switch (columnname) + { + case "channelnumber": ordered = list.ToList().FindAll(s => s.ChannelNumber == Convert.ToInt32(filtertext)); break; + case "channelname": ordered = list.ToList().FindAll(s => s.ChannelName.ToLower().Contains(filtertext)); break; + case "start": + case "stop": + throw new ArgumentException("columnname", string.Format("Filtering for column: '{0}' has not been implemented", columnname)); + case "description": ordered = list.ToList().FindAll(s => s.Description.ToLower().Contains(filtertext)); break; + case "title": ordered = list.ToList().FindAll(s => s.Title.ToLower().Contains(filtertext)); break; + case "subtitle": ordered = list.ToList().FindAll(s => s.SubTitle.ToLower().Contains(filtertext)); break; + } + list = new ProgramDefinitionList(ordered.ToList()); + if (list.Count == 0) + { + list = (ProgramDefinitionList)source; + gLog.Warn.WriteLine("Filering by: column='{0}' with filer='{1}' returned no data",columnname, filtertext); + } + } + catch (Exception ex) { throw ex; } + if (list != null) { source = list; } + } + #endregion + } + private class ProgramDefintion : IProgramDefinition + { + public ProgramDefintion() + { + //ChannelId = string.Empty; + ChannelNumber = 0; + ChannelName = string.Empty; + Start = new DateTime().ToDateTimeString(); + Stop = new DateTime().ToDateTimeString(); + Description = string.Empty; + Title = string.Empty; + SubTitle = string.Empty; + } + //public string ChannelId { get; internal set; } + public int ChannelNumber { get; internal set; } + public string ChannelName { get; internal set; } + public string Start { get; internal set; } + public string Stop { get; internal set; } + public string Title { get; internal set; } + public string SubTitle { get; internal set; } + public string Description { get; internal set; } - public object CreateBindableDataSource() + public override string ToString() + { + StringBuilder builder = new StringBuilder(); + Type t = typeof(ProgramDefintion); + var props = t.GetProperties(); + foreach (var prop in props) + { + string name = prop.Name; + object value = prop.GetValue(this, null); + builder.AppendFormat("\t{0}: '{1}'", name, value == null ? "null" : value.ToString()); + } + return builder.ToString(); + } + + } + public ProgramList() { } + //public object ConvertObjectData(object source) { return this.ConvertData(source); } + //public IProgramDefinitionList ConvertData(object source) + //{ + // if (source.GetType() != typeof(ProgramDefinitionList)) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(ProgramDefinitionList).Name)); } + // //if (type != typeof(IProgramDefinitionList)) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", type.Name, typeof(IProgramDefinitionList).Name)); } + // IProgramDefinitionList t = (source as IProgramDefinitionList); + // return t; + //} + /// + /// + /// + /// + /// + public object CreateBindableDataSource(out Type type) { + type = typeof(IProgramDefinitionList); object bindable = new object(); + ProgramDefinitionList list = new ProgramDefinitionList(); + + foreach (var t in this) + { + try + { + ProgramDefintion definition = new ProgramDefintion(); + string ChannelId = t.MetaData[XMLTVConstants.Programs.ProgramChannelId].ToString(); + //definition.ChannelId = ChannelId; + + var channels = XMLTV.GetChannels(); + if (channels != null) + { + var channel = channels.Find(p => p.Id == ChannelId); + if (channel != null) + { + var channelname = channel.MetaData[XMLTVConstants.Channels.ChannelDisplayName].FirstOrDefault().Value.ToString(); + var split = channelname.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + definition.ChannelNumber = Convert.ToInt32(split.First()); + definition.ChannelName = string.IsNullOrEmpty(split.Last()) ? string.Empty : split.Last().ToString(); + + } + else + { + definition.ChannelNumber = 0; + } + } + else + { + definition.ChannelNumber = 0; + } + definition.Start = ((DateTime)t.MetaData[XMLTVConstants.Programs.ProgramStart]).ToDateTimeString(); + definition.Stop = ((DateTime)t.MetaData[XMLTVConstants.Programs.ProgramStop]).ToDateTimeString(); + definition.Description = t.MetaData[XMLTVConstants.Programs.ProgramDescription].ToString(); + definition.Title = t.MetaData[XMLTVConstants.Programs.ProgramTitle].ToString(); + definition.SubTitle = t.MetaData[XMLTVConstants.Programs.ProgramSubTitle].ToString(); + + xmltv_logger.Verbose.Debug.WriteLine(definition.ToString()); + list.Add(definition); + } + catch (Exception ex) { throw ex; } + } + + list = new ProgramDefinitionList(list.OrderBy(s => s.Start).ToList()); + + bindable = list; return bindable; } } public class ExtraList : List, IDataSourceBindable { - public ExtraList() { } - public object CreateBindableDataSource() + public ExtraList() { } + /// + /// + /// + /// + /// + public object CreateBindableDataSource(out Type type) { - object bindable = new object(); - return bindable; + type = typeof(object); + throw new NotImplementedException("Conversion of Extra MetaData to a bindable datasource has not been implemented."); + //object bindable = new object(); + //return bindable; } + //public object ConvertObjectData(object source) { return source; } + //public object Convert(object source) + //{ + // throw new NotImplementedException("Conversion of Extra MetaData to a bindable datasource has not been implemented."); + //} } }