--- trunk/libxmltv/Core/XMLTV.cs 2013/03/08 10:19:32 55 +++ trunk/libxmltv/Core/XMLTV.cs 2013/03/10 20:19:33 126 @@ -7,6 +7,7 @@ using System.IO; using System.Reflection; using System.Globalization; +using System.Windows.Forms; namespace libxmltv.Core { @@ -15,35 +16,179 @@ /// public static class XMLTV { - static XMLTV() { xmltv_logger.Initialize(); } + static XMLTV() + { + xmltv_logger.Initialize(); + if (instance == null) + { + instance = new XMLTV(); + } + } + + #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 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 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 GetChannels() + { + var gInstance = GetInstance(); + var list = gInstance.Channels; + return list; + } + public static List GetPrograms() + { + var gInstance = GetInstance(); + var list = gInstance.Programs; + return list; + } + + public static object CreateDataSourceFromObject(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; + return bindable; + } + + public static void CreateFilterFromDataSource(ref 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)); } + 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"); } + 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)); } + sorter.Sort(ref source, args); + } + #endregion + #endregion + static IXMLTV instance; - public static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); } - public static void CreateInstance(params object[] args) { instance = new XMLTV(args); } - //public static void CreateInstance(string xml_file, EventHandler t) { instance = new XMLTV(xml_file, t); } - //public static IXMLTVSerializer GetSerializer() {return new XMLTVSerializer(InternalGetInstance()); } + internal static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); } + internal static void CreateInstance(params object[] args) { instance = new XMLTV(args); } - //private static IXMLTVSerializer CreateSerializer() where T : class { return new XMLTVSerializer(InternalGetInstance() as T); } + internal static void CreateFromInstance(object raw_instance, EventHandler handler) + { + instance = new XMLTV(raw_instance, handler); + //OnCreatedInstance(); + } + + internal static void OnCreatedInstance() { if (OnInstanceCreated != null) { OnInstanceCreated.Invoke(null, new EventArgs()); } } #region IXMLTVSerializer 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 handler) + { + bool status; + var gInstance = instance.DeSerialize(file, out status); + CreateFromInstance(gInstance, handler); + return status; + } + internal static bool DeSerialize(Stream stream, EventHandler handler) + { + bool status; + var gInstance = instance.DeSerialize(stream, out status); + CreateFromInstance(gInstance, handler); + return status; + } #endregion + internal static void DestroyInstance() { instance.DestroyInstance(); } - public static void DestroyInstance() { instance.DestroyInstance(); } - - public static EventHandler OnInstanceCreated + internal static EventHandler OnInstanceCreated { get { return instance.OnInstanceCreated; } set { instance.OnInstanceCreated = value; } } + + } - internal class XMLTV : IDestroyInstance, IXMLTV where CLASS : class,INTERFACE + internal class XMLTV : IDestroyInstance, IXMLTV where CLASS : class,INTERFACE,new() { - + public XMLTV() + { + instance = new CLASS(); + } + public XMLTV(object raw_instance, EventHandler handler) + { + instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS)); + if (instance != null) + { + IRuntimeInstanceLoader loader = (instance as IRuntimeInstanceLoader); + if (loader != null) + { + SetOnInstanceCreated(handler); + instance = loader.LoadFromInstance(instance); + } + } + } public XMLTV(params object[] args) { @@ -89,6 +234,10 @@ { return (INTERFACE)instance; } + public void SetInstance(INTERFACE gInstance) + { + this.instance = gInstance as CLASS; + } #endregion #region IXMLTVSerializer members