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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/libxmltv/Core/DataSourceBindable.cs	2013/03/10 19:22:06	122
+++ trunk/libxmltv/Core/DataSourceBindable.cs	2013/03/13 14:39:08	130
@@ -3,20 +3,38 @@
 using System.Linq;
 using System.Text;
 using libxmltv.Interfaces;
+using System.Collections;
 
 namespace libxmltv.Core
 {
-    public class ChannelList : List<IXMLTVChannel>, IDataSourceBindable
+    public class ChannelList : List<IXMLTVChannel>, IDataSourceBindable//, IDataConverter<IChannelDefintionList>
     {
-        private class ChannelDefintion
+        private class ChannelDefintionList : List<IChannelDefintion>, IDataSourceSortable, IDataSourceFilterable, IChannelDefintionList   
+        {
+            public ChannelDefintionList() { }
+            public ChannelDefintionList(List<IChannelDefintion> collection) { collection.ForEach(s => this.Add(s)); }
+            #region IDataSourceSortable members
+            public void Sort(ref object source, 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;
-                Name = string.Empty;
+                ChannelName = string.Empty;
             }
             public string ChannelId { get; internal set; }
-            public string Name { get; internal set; }
+            public string ChannelName { get; internal set; }
+
+            
         }
         //static private List<string> known_columns;
         //static ChannelList()
@@ -27,12 +45,25 @@
         //}
         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;
+        //}
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <param name="type"></param>
+        /// <returns></returns>
+        public object CreateBindableDataSource(out Type type)
         {
+            type = typeof(IChannelDefintionList);
             object bindable = new object();
-            List<ChannelDefintion> list = new List<ChannelDefintion>();
+            ChannelDefintionList list = new ChannelDefintionList();
             
             foreach (var t in this)
             {
@@ -40,22 +71,77 @@
                 {
                     ChannelDefintion definition = new ChannelDefintion();
                     definition.ChannelId = t.Id;
-                    definition.Name = t.MetaData[XMLTVConstants.Channels.ChannelDisplayName].FirstOrDefault().Value.ToString();                    
+                    definition.ChannelName = t.MetaData[XMLTVConstants.Channels.ChannelDisplayName].FirstOrDefault().Value.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<IXMLTVProgram>, IDataSourceBindable
+    public class ProgramList : List<IXMLTVProgram>, IDataSourceBindable//, IDataConverter<IProgramDefinitionList>
     {
-        private class ProgramDefintion
+        private class ProgramDefinitionList : List<IProgramDefinition>, IDataSourceSortable, IDataSourceFilterable, IProgramDefinitionList  
+        {
+            public ProgramDefinitionList() { }
+            public ProgramDefinitionList(List<IProgramDefinition> collection) { collection.ForEach(s => this.Add(s)); }
+            #region IDataSourceSortable members
+            public void Sort(ref object source, params string[] args)
+            {
+                List<IProgramDefinition> list = null;
+                IOrderedEnumerable<IProgramDefinition> ordered = null;
+                try
+                {
+                    list = (ProgramDefinitionList)source;
+                    ordered = list.OrderBy(s => s);
+
+                    foreach (object arg in args)
+                    {
+                        var col = arg.ToString().ToLower();
+                        switch (col)
+                        {
+                            case "channelname": ordered = ordered.ThenBy(s => s.ChannelName); break;
+                            case "start": ordered = ordered.ThenBy(s => s.Start); break;
+                            case "stop": ordered = ordered.ThenBy(s => s.Stop); break;
+                            case "description": ordered = ordered.ThenBy(s => s.Description); break;
+                            case "title": ordered = ordered.ThenBy(s => s.Title); break;
+                            case "subtitle": ordered = ordered.ThenBy(s => s.SubTitle); break;
+                        }
+                    }
+
+                    var k = ordered.ToList();
+                    
+                    //list = new ProgramDefinitionList(ordered.ToList()).ToList();
+                    //var col = args[0].ToLower();
+                    //switch (col)
+                    //{
+                    //    case "channelname": list =new ProgramDefinitionList( list.OrderBy(s => s.ChannelName).ToList()); break;
+                    //    case "start": list = new ProgramDefinitionList( list.OrderBy(s => s.Start).ToList()); break;
+                    //    case "stop": list = new ProgramDefinitionList( list.OrderBy(s => s.Stop).ToList()); break;
+                    //    case "description": list = new ProgramDefinitionList( list.OrderBy(s => s.Description).ToList()); break;
+                    //    case "title": list = new ProgramDefinitionList( list.OrderBy(s => s.Title).ToList()); break;
+                    //    case "subtitle": list = new ProgramDefinitionList(list.OrderBy(s => s.SubTitle).ToList()); break;
+                    //}
+
+                }
+                catch (Exception ex) { throw ex; }
+                if (list != null) { source = list; }
+            }
+            #endregion
+            #region IDataSourceFilterable members
+            public void Filter(ref object source, params string[] args)
+            {
+            }
+            #endregion
+        }
+        private class ProgramDefintion : IProgramDefinition
         {
             public ProgramDefintion()
             {
-                ChannelId = string.Empty;
+                //ChannelId = string.Empty;
                 ChannelName = string.Empty;
                 Start = new DateTime();
                 Stop = new DateTime();
@@ -63,32 +149,48 @@
                 Title = string.Empty;
                 SubTitle = string.Empty;
             }
-            public string ChannelId { get; internal set; }
+            //public string ChannelId { get; internal set; }
             public string ChannelName { get; internal set; }
             public DateTime Start { get; internal set; }
             public DateTime Stop { get; internal set; }
-            public string Description { get; internal set; }
             public string Title { get; internal set; }
             public string SubTitle { get; internal set; }
+            public string Description { get; internal set; }
+
+          
         }
         public ProgramList() { }
-
-        public object CreateBindableDataSource()
+        //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;
+        //}
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <param name="type"></param>
+        /// <returns></returns>
+        public object CreateBindableDataSource(out Type type)
         {
+            type = typeof(IProgramDefinitionList);
             object bindable = new object();
-            List<ProgramDefintion> list = new List<ProgramDefintion>();
+            ProgramDefinitionList list = new ProgramDefinitionList();
 
             foreach (var t in this)
             {
                 try
                 {
                     ProgramDefintion definition = new ProgramDefintion();
-                    definition.ChannelId = t.MetaData[XMLTVConstants.Programs.ProgramChannelId].ToString();
+                    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 == definition.ChannelId);
+                        var channel = channels.Find(p => p.Id == ChannelId);
                         if (channel != null)
                         {
                             definition.ChannelName = channel.MetaData[XMLTVConstants.Channels.ChannelDisplayName].FirstOrDefault().Value.ToString();
@@ -111,6 +213,9 @@
                 }
                 catch (Exception ex) { throw ex; }
             }
+
+            list = new ProgramDefinitionList(list.OrderBy(s => s.Start).ToList());
+
             bindable = list;
             return bindable;
         }
@@ -119,11 +224,22 @@
     {
 
         public ExtraList() { }
-
-        public object CreateBindableDataSource()
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <param name="type"></param>
+        /// <returns></returns>
+        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.");
+        //}
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22