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

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

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

--- trunk/libxmltv/Core/XMLTV.cs	2013/03/08 12:05:33	56
+++ trunk/libxmltv/Core/XMLTV.cs	2013/03/13 14:39:08	130
@@ -7,6 +7,8 @@
 using System.IO;
 using System.Reflection;
 using System.Globalization;
+using System.Windows.Forms;
+using Enterprise.Logging;
 
 namespace libxmltv.Core
 {
@@ -24,33 +26,186 @@
             }
         }
 
-        static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance;
-        public static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }
-        public static void CreateInstance(params object[] args) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(args); }
+        #region public members
+        #region save data
+        public static void Save(string file)
+        {
+            if (!Serialize(file))
+            {
+                MessageBox.Show("Failed to save data - check log", "Failed to save data", MessageBoxButtons.OK, MessageBoxIcon.Error);
+                return;
+            }
+            MessageBox.Show("Successfully saved data", "Successfully saved data", MessageBoxButtons.OK, MessageBoxIcon.Information);
+        }
+        public static void Save(Stream stream)
+        {
+            if (!Serialize(stream))
+            {
+                MessageBox.Show("Failed to save data - check log", "Failed to save data", MessageBoxButtons.OK, MessageBoxIcon.Error);
+                return;
+            }
+            MessageBox.Show("Successfully saved data", "Successfully saved data", MessageBoxButtons.OK, MessageBoxIcon.Information);
+        }
+        #endregion
+        #region load data
+        public static void Load(string file) { Load(file, null); }
+        public static void Load(string file, EventHandler<EventArgs> handler)
+        {
+            if (!DeSerialize(file, handler))
+            {
+                MessageBox.Show("Failed to load data - check log", "Failed to load data", MessageBoxButtons.OK, MessageBoxIcon.Error);
+                return;
+            }
+            MessageBox.Show("Successfully loaded data", "Successfully loaded data", MessageBoxButtons.OK, MessageBoxIcon.Information);
+            OnCreatedInstance();
 
-        public static void CreateFromInstance(object raw_instance, EventHandler<EventArgs> handler) 
-        { 
-            instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(raw_instance,handler);
-            if (OnInstanceCreated != null)
+        }
+        public static void Load(Stream stream) { Load(stream, null); }
+        public static void Load(Stream stream, EventHandler<EventArgs> handler)
+        {
+            if (!DeSerialize(stream, handler))
             {
-                OnInstanceCreated.Invoke(null, new EventArgs());
+                MessageBox.Show("Failed to load data - check log", "Failed to load data", MessageBoxButtons.OK, MessageBoxIcon.Error);
+                return;
             }
+            MessageBox.Show("Successfully loaded data", "Successfully loaded data", MessageBoxButtons.OK, MessageBoxIcon.Information);
+            OnCreatedInstance();
         }
+        #endregion
+        #region Create/Destroy
+        public static void Create(params object[] args) { CreateInstance(args); }
+        public static void Destroy() { DestroyInstance(); }
+        #endregion
 
-        #region IXMLTVSerializer<IXMLTVRuntimeInstance> members
-        public static bool Serialize(string file) { return instance.Serialize(file); }
-        public static bool Serialize(Stream stream) { return instance.Serialize(stream); }
-        public static IXMLTVRuntimeInstance DeSerialize(string file, out bool status) { return instance.DeSerialize(file, out status); }
-        public static IXMLTVRuntimeInstance DeSerialize(Stream stream, out bool status) { return instance.DeSerialize(stream, out status); }
+        #region Get Data
+        /// <summary>
+        /// Gets the source of the XMLTV Data : <see cref="libxmltv.Interfaces.IXMLTVSource"/>
+        /// </summary>
+        /// <returns>returns an instance of the xmltv data source : <see cref="libxmltv.Interfaces.IXMLTVSource"/></returns>
+        public static IXMLTVSource GetSource()
+        {
+            var gInstance = GetInstance();
+            var list = gInstance.Source;
+            return list;
+        }
+        /// <summary>
+        /// Gets the collection of XMLTV Channels : List of <see cref="libxmltv.Interfaces.IXMLTVChannel"/>
+        /// </summary>
+        /// <returns>returns an instance of the xmltv channels : as a list of:  <see cref="libxmltv.Interfaces.IXMLTVChannel"/></returns>
+        public static List<IXMLTVChannel> GetChannels()
+        {
+            var gInstance = GetInstance();
+           var list = gInstance.Channels;
+            return list;
+        }
+        /// <summary>
+        /// Gets the collection of XMLTV Programs : List of <see cref="libxmltv.Interfaces.IXMLTVProgram"/>
+        /// </summary>
+        /// <returns>returns an instance of the xmltv channels : as a list of:  <see cref="libxmltv.Interfaces.IXMLTVProgram"/></returns>
+        public static List<IXMLTVProgram> GetPrograms()
+        {
+            var gInstance = GetInstance();
+            var list = gInstance.Programs;
+            return list;
+        }
+        /// <summary>
+        /// Creates a binding source for a bindable control
+        /// </summary>
+        /// <param name="data">The object data to create a binding source from</param>
+        /// <param name="type">Represents the underlying type of the created binding source data</param>
+        /// <returns>Returns an object representing the created binding source</returns>
+        public static object CreateBindingSourceFromData(object data, out Type type)
+        {
+            if (data == null) { throw new ArgumentNullException("data", "cannot be null"); }
+            //BindingSource source = new BindingSource();
+
+            IDataSourceBindable binder = (data as IDataSourceBindable);
+            if (binder == null) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", data.GetType().Name, typeof(IDataSourceBindable).Name)); }
+            object bindable = binder.CreateBindableDataSource(out type);
+            //source.DataSource = bindable;
+            //return source;
+            return bindable;
+        }
+
+        private static object ConvertSourceType(Type source_type, object source)
+        {
+            //try
+            //{
+            //    IDataConverter converter = (source as IDataConverter);
+            //    if (converter == null) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(IDataConverter).Name)); }
+            //    object t = converter.ConvertObjectData(source);
+            //    if (t != null) { source = t; }
+            //    return t;
+            //}
+            //catch (Exception ex) { gLog.Verbose.Error.WriteLine(ex.ToString()); return null; }
+            return source;
+        }
+
+        public static void CreateFilterFromDataSource(ref object source, params string[] args)
+        {
+            if (source == null) { throw new ArgumentNullException("source", "cannot be null"); }
+            object t = ConvertSourceType(source.GetType(), source);
+            if (t == null) { throw new ArgumentException("source_type", string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, source.GetType().Name)); }
+            if (args == null) { throw new ArgumentNullException("args", "cannot be null"); }
+            source = t;
+            IDataSourceFilterable filter = (source as IDataSourceFilterable);
+            if (filter == null) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(IDataSourceFilterable).Name)); }
+            filter.Filter(ref source, args);            
+        }
+        public static void CreateSorterFromDataSource(ref object source, params string[] args)
+        {
+            if (source == null) { throw new ArgumentNullException("source", "cannot be null"); }
+            object t = ConvertSourceType(source.GetType(), source);
+            if (t == null) { throw new ArgumentException("source_type", string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, source.GetType().Name)); }
+            if (args == null) { throw new ArgumentNullException("args", "cannot be null"); }
+            source = t;
+            IDataSourceSortable sorter = (source as IDataSourceSortable);
+            if (sorter == null) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(IDataSourceSortable).Name)); }
+            sorter.Sort(ref source, args);            
+        }
+        #endregion
         #endregion
 
-        public static void DestroyInstance() { instance.DestroyInstance(); }        
 
-        public static EventHandler<EventArgs> OnInstanceCreated
+        static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance;
+        internal static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }
+        internal static void CreateInstance(params object[] args) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(args); }
+
+        internal static void CreateFromInstance(object raw_instance, EventHandler<EventArgs> handler)
+        {
+            instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(raw_instance, handler);
+            //OnCreatedInstance();
+        }
+
+        internal static void OnCreatedInstance() { if (OnInstanceCreated != null) { OnInstanceCreated.Invoke(null, new EventArgs()); } }
+
+        #region IXMLTVSerializer<IXMLTVRuntimeInstance> members
+        internal static bool Serialize(string file) { return instance.Serialize(file); }
+        internal static bool Serialize(Stream stream) { return instance.Serialize(stream); }
+        internal static bool DeSerialize(string file, EventHandler<EventArgs> handler)
+        {
+            bool status; 
+            var gInstance = instance.DeSerialize(file, out status);            
+            CreateFromInstance(gInstance, handler);
+            return status;
+        }
+        internal static bool DeSerialize(Stream stream, EventHandler<EventArgs> handler)
+        {
+            bool status;
+            var gInstance = instance.DeSerialize(stream, out status);
+            CreateFromInstance(gInstance, handler);
+            return status;
+        }
+        #endregion
+        internal static void DestroyInstance() { instance.DestroyInstance(); }
+
+        internal static EventHandler<EventArgs> OnInstanceCreated
         {
             get { return instance.OnInstanceCreated; }
             set { instance.OnInstanceCreated = value; }
         }
+
+       
     }
 
     internal class XMLTV<INTERFACE, CLASS> : IDestroyInstance, IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE,new()
@@ -117,6 +272,10 @@
         {
             return (INTERFACE)instance;
         }
+        public void SetInstance(INTERFACE gInstance)
+        {
+            this.instance = gInstance as CLASS;
+        }
         #endregion
 
         #region IXMLTVSerializer<INTERFACE> members

 

  ViewVC Help
Powered by ViewVC 1.1.22