/[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

revision 52 by william, Fri Mar 8 07:01:37 2013 UTC revision 125 by william, Sun Mar 10 19:44:49 2013 UTC
# Line 7  using System.ComponentModel; Line 7  using System.ComponentModel;
7  using System.IO;  using System.IO;
8  using System.Reflection;  using System.Reflection;
9  using System.Globalization;  using System.Globalization;
10    using System.Windows.Forms;
11    
12  namespace libxmltv.Core  namespace libxmltv.Core
13  {  {
# Line 15  namespace libxmltv.Core Line 16  namespace libxmltv.Core
16      /// </summary>      /// </summary>
17      public static class XMLTV      public static class XMLTV
18      {      {
19          static XMLTV() { xmltv_logger.Initialize(); }          static XMLTV()
20            {
21                xmltv_logger.Initialize();
22                if (instance == null)
23                {
24                    instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>();
25                }
26            }
27    
28            #region public members
29            #region save data
30            public static void Save(string file)
31            {
32                if (!Serialize(file))
33                {
34                    MessageBox.Show("Failed to save data - check log", "Failed to save data", MessageBoxButtons.OK, MessageBoxIcon.Error);
35                    return;
36                }
37                MessageBox.Show("Successfully saved data", "Successfully saved data", MessageBoxButtons.OK, MessageBoxIcon.Information);
38            }
39            public static void Save(Stream stream)
40            {
41                if (!Serialize(stream))
42                {
43                    MessageBox.Show("Failed to save data - check log", "Failed to save data", MessageBoxButtons.OK, MessageBoxIcon.Error);
44                    return;
45                }
46                MessageBox.Show("Successfully saved data", "Successfully saved data", MessageBoxButtons.OK, MessageBoxIcon.Information);
47            }
48            #endregion
49            #region load data
50            public static void Load(string file) { Load(file, null); }
51            public static void Load(string file, EventHandler<EventArgs> handler)
52            {
53                if (!DeSerialize(file, handler))
54                {
55                    MessageBox.Show("Failed to load data - check log", "Failed to load data", MessageBoxButtons.OK, MessageBoxIcon.Error);
56                    return;
57                }
58                MessageBox.Show("Successfully loaded data", "Successfully loaded data", MessageBoxButtons.OK, MessageBoxIcon.Information);
59                OnCreatedInstance();
60    
61            }
62            public static void Load(Stream stream) { Load(stream, null); }
63            public static void Load(Stream stream, EventHandler<EventArgs> handler)
64            {
65                if (!DeSerialize(stream, handler))
66                {
67                    MessageBox.Show("Failed to load data - check log", "Failed to load data", MessageBoxButtons.OK, MessageBoxIcon.Error);
68                    return;
69                }
70                MessageBox.Show("Successfully loaded data", "Successfully loaded data", MessageBoxButtons.OK, MessageBoxIcon.Information);
71                OnCreatedInstance();
72            }
73            #endregion
74            #region Create/Destroy
75            public static void Create(params object[] args) { CreateInstance(args); }
76            public static void Destroy() { DestroyInstance(); }
77            #endregion
78    
79            #region Get Data
80            public static IXMLTVSource GetSource()
81            {
82                var gInstance = GetInstance();
83                var list = gInstance.Source;
84                return list;
85            }
86            public static List<IXMLTVChannel> GetChannels()
87            {
88                var gInstance = GetInstance();
89               var list = gInstance.Channels;
90                return list;
91            }
92            public static List<IXMLTVProgram> GetPrograms()
93            {
94                var gInstance = GetInstance();
95                var list = gInstance.Programs;
96                return list;
97            }
98    
99            public static BindingSource CreateBindingSourceForData(object data)
100            {
101                if (data == null) { throw new ArgumentNullException("data", "cannot be null"); }
102                BindingSource source = new BindingSource();
103    
104                IDataSourceBindable binder = (data as IDataSourceBindable);
105                if (binder == null) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", data.GetType().Name, typeof(IDataSourceBindable).Name)); }
106                object bindable = binder.CreateBindableDataSource();
107                source.DataSource = bindable;
108                return source;
109            }
110    
111            public static BindingSource CreateFilterFromDataSource(object source, params string[] args)
112            {
113                if (source == null) { throw new ArgumentNullException("source", "cannot be null"); }
114                if (args == null) { throw new ArgumentNullException("args", "cannot be null"); }
115                IDataSourceFilterable filter = (source as IDataSourceFilterable);
116                if (filter == null) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(IDataSourceFilterable).Name)); }
117                object filtered = filter.Filter(source,args);
118                BindingSource bound = new BindingSource();
119                bound.DataSource = filtered;
120                return bound;
121            }
122            public static BindingSource CreateSorterFromDataSource(object source, params string[] args)
123            {
124                if (source == null) { throw new ArgumentNullException("source", "cannot be null"); }
125                if (args == null) { throw new ArgumentNullException("args", "cannot be null"); }
126                IDataSourceSortable sorter = (source as IDataSourceSortable);
127                if (sorter == null) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(IDataSourceSortable).Name)); }
128                object sorted = sorter.Sort(source, args);
129                BindingSource bound = new BindingSource();
130                bound.DataSource = sorted;
131                return bound;
132            }
133            #endregion
134            #endregion
135    
136    
137          static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance;          static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance;
138          public static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }          internal static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }
139          public static void CreateInstance(params object[] args) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(args); }          internal 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()); }  
140    
141          //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)
142            {
143                instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(raw_instance, handler);
144                //OnCreatedInstance();
145            }
146    
147            internal static void OnCreatedInstance() { if (OnInstanceCreated != null) { OnInstanceCreated.Invoke(null, new EventArgs()); } }
148    
149          #region IXMLTVSerializer<IXMLTVRuntimeInstance> members          #region IXMLTVSerializer<IXMLTVRuntimeInstance> members
150          public static bool Serialize(string file) { return instance.Serialize(file); }          internal static bool Serialize(string file) { return instance.Serialize(file); }
151          public static bool Serialize(Stream stream) { return instance.Serialize(stream); }          internal static bool Serialize(Stream stream) { return instance.Serialize(stream); }
152          public static IXMLTVRuntimeInstance DeSerialize(string file, out bool status) { return instance.DeSerialize(file, out status); }          internal static bool DeSerialize(string file, EventHandler<EventArgs> handler)
153          public static IXMLTVRuntimeInstance DeSerialize(Stream stream, out bool status) { return instance.DeSerialize(stream, out status); }          {
154                bool status;
155                var gInstance = instance.DeSerialize(file, out status);            
156                CreateFromInstance(gInstance, handler);
157                return status;
158            }
159            internal static bool DeSerialize(Stream stream, EventHandler<EventArgs> handler)
160            {
161                bool status;
162                var gInstance = instance.DeSerialize(stream, out status);
163                CreateFromInstance(gInstance, handler);
164                return status;
165            }
166          #endregion          #endregion
167            internal static void DestroyInstance() { instance.DestroyInstance(); }
168    
169            internal static EventHandler<EventArgs> OnInstanceCreated
170            {
171                get { return instance.OnInstanceCreated; }
172                set { instance.OnInstanceCreated = value; }
173            }
174    
175          
176      }      }
177    
178      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()
179      {      {
180            public XMLTV()
181            {
182                instance = new CLASS();
183            }
184            public XMLTV(object raw_instance, EventHandler<EventArgs> handler)
185            {
186                instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS));
187                if (instance != null)
188                {
189                    IRuntimeInstanceLoader<CLASS> loader = (instance as IRuntimeInstanceLoader<CLASS>);
190                    if (loader != null)
191                    {
192                        SetOnInstanceCreated(handler);
193                        instance = loader.LoadFromInstance(instance);
194                    }
195                }
196            }
197          public XMLTV(params object[] args)          public XMLTV(params object[] args)
198          {          {
199              ////instance = new CLASS(xml_file, t);              
200              //instance = null;              BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
   
             //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;  
201              CultureInfo culture = CultureInfo.CurrentCulture;              CultureInfo culture = CultureInfo.CurrentCulture;
202              try              try
203              {              {
# Line 68  namespace libxmltv.Core Line 206  namespace libxmltv.Core
206              }              }
207              catch (Exception ex)              catch (Exception ex)
208              {              {
209                  xmltv_logger.Log.Error.WriteLine(ex.ToString());                  xmltv_logger.Error.WriteLine(ex.ToString());
210    
211                  StringBuilder parameter_builder = new StringBuilder();                  StringBuilder parameter_builder = new StringBuilder();
212                  foreach (object arg in args)                  foreach (object arg in args)
# Line 101  namespace libxmltv.Core Line 239  namespace libxmltv.Core
239          {          {
240              return (INTERFACE)instance;              return (INTERFACE)instance;
241          }          }
242            public void SetInstance(INTERFACE gInstance)
243            {
244                this.instance = gInstance as CLASS;
245            }
246          #endregion          #endregion
247    
248          #region IXMLTVSerializer<INTERFACE> members          #region IXMLTVSerializer<INTERFACE> members
# Line 109  namespace libxmltv.Core Line 251  namespace libxmltv.Core
251          public INTERFACE DeSerialize(string file, out bool status) { return CreateSerializer().DeSerialize(file, out status); }          public INTERFACE DeSerialize(string file, out bool status) { return CreateSerializer().DeSerialize(file, out status); }
252          public INTERFACE DeSerialize(Stream stream, out bool status) { return CreateSerializer().DeSerialize(stream, out status); }          public INTERFACE DeSerialize(Stream stream, out bool status) { return CreateSerializer().DeSerialize(stream, out status); }
253          #endregion          #endregion
254            public void DestroyInstance()
255            {
256                IDestroyInstance destoyer = (instance as IDestroyInstance);
257                if (destoyer != null)
258                {
259                    destoyer.DestroyInstance();
260                }
261                else
262                {
263                    xmltv_logger.Error.WriteLine("Unable to call DestroyInstance() on type: '{0}'", instance.GetType().Name);
264                }            
265            }
266            private void SetOnInstanceCreated(EventHandler<EventArgs> event_instance)
267            {
268                ISetCreatedInstanceEvent setter = (instance as ISetCreatedInstanceEvent);
269                if (setter != null) { setter.SetOnInstanceCreated(event_instance); }
270            }
271            private EventHandler<EventArgs> GetOnInstanceCreated()
272            {
273                IGetCreatedInstanceEvent getter = (instance as IGetCreatedInstanceEvent);
274                if (getter != null) { return getter.GetOnInstanceCreated(); }
275                return null;
276            }
277            public EventHandler<EventArgs> OnInstanceCreated
278            {
279                get { return GetOnInstanceCreated(); }
280                set { SetOnInstanceCreated(value); }
281            }
282      }      }
283    
284  }  }

Legend:
Removed from v.52  
changed lines
  Added in v.125

  ViewVC Help
Powered by ViewVC 1.1.22