/[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 07:01:37	52
+++ trunk/libxmltv/Core/XMLTV.cs	2013/03/10 19:44:49	125
@@ -7,6 +7,7 @@
 using System.IO;
 using System.Reflection;
 using System.Globalization;
+using System.Windows.Forms;
 
 namespace libxmltv.Core
 {
@@ -15,51 +16,188 @@
     /// </summary>
     public static class XMLTV
     {
-        static XMLTV() { xmltv_logger.Initialize(); }
+        static XMLTV()
+        {
+            xmltv_logger.Initialize();
+            if (instance == null)
+            {
+                instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>();
+            }
+        }
+
+        #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 Load(Stream stream) { Load(stream, null); }
+        public static void Load(Stream stream, EventHandler<EventArgs> handler)
+        {
+            if (!DeSerialize(stream, 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();
+        }
+        #endregion
+        #region Create/Destroy
+        public static void Create(params object[] args) { CreateInstance(args); }
+        public static void Destroy() { DestroyInstance(); }
+        #endregion
+
+        #region Get Data
+        public static IXMLTVSource GetSource()
+        {
+            var gInstance = GetInstance();
+            var list = gInstance.Source;
+            return list;
+        }
+        public static List<IXMLTVChannel> GetChannels()
+        {
+            var gInstance = GetInstance();
+           var list = gInstance.Channels;
+            return list;
+        }
+        public static List<IXMLTVProgram> GetPrograms()
+        {
+            var gInstance = GetInstance();
+            var list = gInstance.Programs;
+            return list;
+        }
+
+        public static BindingSource CreateBindingSourceForData(object data)
+        {
+            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();
+            source.DataSource = bindable;
+            return source;
+        }
+
+        public static BindingSource CreateFilterFromDataSource(object source, params string[] args)
+        {
+            if (source == null) { throw new ArgumentNullException("source", "cannot be null"); }
+            if (args == null) { throw new ArgumentNullException("args", "cannot be null"); }
+            IDataSourceFilterable filter = (source as IDataSourceFilterable);
+            if (filter == null) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(IDataSourceFilterable).Name)); }
+            object filtered = filter.Filter(source,args);
+            BindingSource bound = new BindingSource();
+            bound.DataSource = filtered;
+            return bound;
+        }
+        public static BindingSource CreateSorterFromDataSource(object source, params string[] args)
+        {
+            if (source == null) { throw new ArgumentNullException("source", "cannot be null"); }
+            if (args == null) { throw new ArgumentNullException("args", "cannot be null"); }
+            IDataSourceSortable sorter = (source as IDataSourceSortable);
+            if (sorter == null) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(IDataSourceSortable).Name)); }
+            object sorted = sorter.Sort(source, args);
+            BindingSource bound = new BindingSource();
+            bound.DataSource = sorted;
+            return bound;
+        }
+        #endregion
+        #endregion
+
 
         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); }
-        //public static void CreateInstance(string xml_file, EventHandler<CancelEventArgs> t) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(xml_file, t); }
-        //public static IXMLTVSerializer<IXMLTVRuntimeInstance> GetSerializer() {return new XMLTVSerializer<IXMLTVRuntimeInstance>(InternalGetInstance()); }
+        internal static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }
+        internal static void CreateInstance(params object[] args) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(args); }
 
-        //private static IXMLTVSerializer<T> CreateSerializer<T>() where T : class { return new XMLTVSerializer<T>(InternalGetInstance() as T); }
+        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
-        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); }
+        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> : IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE
+    internal class XMLTV<INTERFACE, CLASS> : IDestroyInstance, IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE,new()
     {
+        public XMLTV()
+        {
+            instance = new CLASS();
+        }
+        public XMLTV(object raw_instance, EventHandler<EventArgs> handler) 
+        {
+            instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS));
+            if (instance != null)
+            {
+                IRuntimeInstanceLoader<CLASS> loader = (instance as IRuntimeInstanceLoader<CLASS>);
+                if (loader != null)
+                {
+                    SetOnInstanceCreated(handler);
+                    instance = loader.LoadFromInstance(instance);
+                }
+            }
+        }
         public XMLTV(params object[] args)
         {
-            ////instance = new CLASS(xml_file, t); 
-            //instance = null;
-
-            //Type type = typeof(CLASS);
-            //var ctors = type.GetConstructors();
-            //foreach (var ctor in ctors)
-            //{
-            //    var ctor_params = ctor.GetParameters();
-            //    if (ctor_params.Count() == 2)
-            //    {
-            //        if (ctor_params[0].ParameterType == typeof(string))
-            //        {
-            //            if (ctor_params[1].ParameterType == typeof(EventHandler<CancelEventArgs>))
-            //            {
-            //                object o = ctor.Invoke(new object[] { xml_file, t });
-            //                instance = (CLASS)Convert.ChangeType(o, typeof(CLASS));
-            //                break;
-            //            }
-            //        }
-            //    }
-            //    else { continue; }
-            //}
-            BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance;
+            
+            BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
             CultureInfo culture = CultureInfo.CurrentCulture;
             try
             {
@@ -68,7 +206,7 @@
             }
             catch (Exception ex)
             {
-                xmltv_logger.Log.Error.WriteLine(ex.ToString());
+                xmltv_logger.Error.WriteLine(ex.ToString());
 
                 StringBuilder parameter_builder = new StringBuilder();
                 foreach (object arg in args)
@@ -101,6 +239,10 @@
         {
             return (INTERFACE)instance;
         }
+        public void SetInstance(INTERFACE gInstance)
+        {
+            this.instance = gInstance as CLASS;
+        }
         #endregion
 
         #region IXMLTVSerializer<INTERFACE> members
@@ -109,6 +251,34 @@
         public INTERFACE DeSerialize(string file, out bool status) { return CreateSerializer().DeSerialize(file, out status); }
         public INTERFACE DeSerialize(Stream stream, out bool status) { return CreateSerializer().DeSerialize(stream, out status); }
         #endregion
+        public void DestroyInstance()
+        {
+            IDestroyInstance destoyer = (instance as IDestroyInstance);
+            if (destoyer != null)
+            {
+                destoyer.DestroyInstance();
+            }
+            else
+            {
+                xmltv_logger.Error.WriteLine("Unable to call DestroyInstance() on type: '{0}'", instance.GetType().Name);
+            }            
+        }
+        private void SetOnInstanceCreated(EventHandler<EventArgs> event_instance)
+        {
+            ISetCreatedInstanceEvent setter = (instance as ISetCreatedInstanceEvent);
+            if (setter != null) { setter.SetOnInstanceCreated(event_instance); }
+        }
+        private EventHandler<EventArgs> GetOnInstanceCreated()
+        {
+            IGetCreatedInstanceEvent getter = (instance as IGetCreatedInstanceEvent);
+            if (getter != null) { return getter.GetOnInstanceCreated(); }
+            return null;
+        }
+        public EventHandler<EventArgs> OnInstanceCreated
+        {
+            get { return GetOnInstanceCreated(); }
+            set { SetOnInstanceCreated(value); }
+        }
     }
 
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22