/[xmltv_parser]/trunk/xmltv_parser/main.cs
ViewVC logotype

Diff of /trunk/xmltv_parser/main.cs

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

--- trunk/xmltv_parser/main.cs	2013/03/08 04:20:09	47
+++ trunk/xmltv_parser/main.cs	2013/03/13 15:16:59	131
@@ -1,4 +1,7 @@
-´╗┐using System;
+´╗┐//#define DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG // when defined will turn off kLogLevel_Debug messages, in release mode
+//#define DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG // when defined will turn off kLogLevel_VerboseDebug message, in debug mode
+//#define USE_DYNAMIC_SORTING_FILTERING_CONTROLS // when defined will setup dynamic sorting and filtering controls for XMLTV Data
+using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
@@ -12,206 +15,294 @@
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.Threading;
+using System.Diagnostics;
+using System.Xml.Linq;
 
 namespace xmltv_parser
 {
+
     public partial class main : Form
     {
-        bool form_closing = false;
-        private IXMLTVRuntimeInstance xmltv;
-        private bool IsUnix
-        {
-            get { return System.Environment.OSVersion.Platform == PlatformID.Unix; }
-        }
-
         public main()
         {
             InitializeComponent();
+            ListViewSorter Sorter = new ListViewSorter();
+            //lstPrograms.ListViewItemSorter = Sorter;
             string log_path = Application.StartupPath;
             string log_filename = string.Format("{0}.log", typeof(main).Assembly.GetName().Name);
-            gLog.CreateLog(string.Format(@"{0}\{1}", log_path, log_filename), false, LogLevel.kLogLevel_All, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush));
-        }
-
-        StringBuilder log_flusher = new StringBuilder();
-        bool HAVE_CR = false;
-        void Log_OnFlush(object sender, LoggerOnFlushEventArgs e)
+            gLog.CreateLog(string.Format(@"{0}\{1}", log_path, log_filename), false, LogLevel.kLogLevel_All_NoProgress, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush));
+#if DEBUG
+            LogLevel gLevel = gLog.LogLevel;
+#if DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG
+            gLevel &= ~LogLevel.kLogLevel_VerboseDebug;
+#else
+            gLevel |= LogLevel.kLogLevel_VerboseDebug;
+#endif
+            gLevel |= LogLevel.kLogLevel_Debug;
+            gLog.SetLogLevel(gLevel);
+#else
+            LogLevel gLevel = LogLevel.kLogLevel_Default; // set the default log level: Info, Warn, Error, Debug
+            // it is OK for kLogLevel_Debug to be set in Release mode ... must of the chatty messages are from kLogLevel_VerboseDebug
+#if DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG
+            gLevel &= ~LogLevel.kLogLevel_Debug;
+#else
+            gLevel |= LogLevel.kLogLevel_Debug;
+#endif
+            gLevel &= ~LogLevel.kLogLevel_VerboseDebug; // ensure this is not set, ever in release mode         
+            gLog.SetLogLevel(gLevel);
+#endif
+
+
+            gLog.ReportProgressEvent += new EventHandler<ReportProgressEventArgs>(gLog_ReportProgress);
+        }
+        #region main form events
+        private void mnuItemExit_Click(object sender, EventArgs e) { this.Close(); }
+        private void main_Load(object sender, EventArgs e) { }
+        private void main_Shown(object sender, EventArgs e) { }
+        private void mnuItemOpenXMLTVFile_Click(object sender, EventArgs e)
         {
-            
-            log_flusher.Append(e.Buffer);
-            if (IsUnix)
+            try
             {
-                if (e.Buffer == System.Environment.NewLine[0])
-                {
-                    OnLogFlush(log_flusher.ToString());
-                    log_flusher = new StringBuilder();
-                }
+                var result = xmltv_file_chooser.ShowDialog();
+                if (result != DialogResult.OK) return;
+                ClearLocalLog();
+                LoadXMLTVShcedule(xmltv_file_chooser.FileName);
             }
-            else
-            {                
-                if (e.Buffer == System.Environment.NewLine[0])
-                {
-                    HAVE_CR = true;
-                }
-                if (e.Buffer == System.Environment.NewLine[1] && HAVE_CR)
-                {
-                    OnLogFlush(log_flusher.ToString().TrimStart(new char[] { System.Environment.NewLine[1] }));
-                    log_flusher = new StringBuilder();
-                    HAVE_CR = false;
-                }
-            }
-            
+            catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
         }
-        //bool txtLog_EnterMouse = false;
-
-        private void txtLog_MouseLeave(object sender, EventArgs e)
+        private void mnuItemOpenSavedData_Click(object sender, EventArgs e)
         {
-            //txtLog_EnterMouse = false;
+            try
+            {
+                var result = xmltv_program_data_loader.ShowDialog();
+                if (result != DialogResult.OK) return;
+                string filename = xmltv_program_data_loader.FileName;
+                Thread worker = new Thread(new ParameterizedThreadStart(DeserializeDataFromFile)); worker.Start(filename);
+            }
+            catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
         }
-        private void txtLog_MouseEnter(object sender, EventArgs e)
+        private void mnuItemSaveData_Click(object sender, EventArgs e)
         {
-            //txtLog_EnterMouse = true;
+            try
+            {
+                var result = xmltv_program_data_saver.ShowDialog();
+                if (result != DialogResult.OK) return;
+                string filename = xmltv_program_data_saver.FileName;
+                Thread worker = new Thread(new ParameterizedThreadStart(DeserializeDataToFile)); worker.Start(filename);
+            }
+            catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
         }
-        void OnLogFlush(string logmessage)
+        private void main_FormClosing(object sender, FormClosingEventArgs e) { try { XMLTV.Destroy(); } catch { } }
+        private void mnuItemClearLocalLog_Click(object sender, EventArgs e) { ClearLocalLog(); }
+        #endregion
+
+        private void ReportProgress(int progress)
         {
-            if (this.IsDisposed) { return; }
-            Console.Write(logmessage);        
-            txtStatus.Text = logmessage.Replace(System.Environment.NewLine, "");
-            txtLog.AppendText(logmessage);
-
-            //if (!txtLog_EnterMouse)
-            //{
-            txtLog.SelectionStart = txtLog.Text.Length; //Set the current caret position to the end
-            txtLog.ScrollToCaret(); //Now scroll it automatically
-            //}
-            //Thread.Sleep(100);
-            Application.DoEvents();
+            if (this.InvokeRequired)
+            {
+                try { this.Invoke((Action)(delegate { ReportProgress(progress); })); }
+                catch { }
+                return;
+            }
+            if (progress_status != null && !this.IsDisposed)
+                progress_status.Value = progress;
         }
 
-        //List<IXMLTVChannel> Channels;
-        List<IXMLTVProgram> Programs;
+        #region gLog Events
+        private void gLog_ReportProgress(object sender, ReportProgressEventArgs e) { ReportProgress(e.Progress); }
+        #endregion
+        #region XMLT Events
+        void XMLTV_OnInstanceCreated(object sender, EventArgs e) { ReportProgress(0); CreateControls(); }
+        void LoadXMLTVShcedule(string schedule_xml) { XMLTV.Create(schedule_xml, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated)); }
+        #endregion
 
-        private void main_Load(object sender, EventArgs e)
-        {
 
-        }
-        private void main_Shown(object sender, EventArgs e)
+        #region Logging Events
+        StringBuilder log_flusher = new StringBuilder();
+        void Log_OnFlush(object sender, LoggerOnFlushEventArgs e) { OnLogFlush(e.Buffer); }
+        void OnLogFlush(string logmessage)
         {
-            //libxmltv.Core.XMLTV
-            
+            if (this.IsDisposed) { return; }
+            UpdateStatus(logmessage);
+            UpdateLogOutput(logmessage);
+            Application.DoEvents();
         }
-
-
-        void LoadXMLTVShcedule(string schedule_xml)
+        private void ClearLocalLog()
         {
-            //var xmltv_loader = XMLTV.CreateLoader(schedule_xml);
-            //var xmltv_parser = XMLTV.CreateParser(xmltv_loader);
-            //var p = XMLTV.GetParser(xmltv_parser);
-
-            ////var channel_count = p.Channels.Values.Count;
-            ////Channels = p.Channels.Values.ToList().GetRange(0, channel_count).ToList();
-            //var program_count = p.Programs.Values.Count;
-            //var program_list = p.Programs.Values.ToList().OrderBy(s => s.Start);
-            ////Programs = program_list(0, program_count).ToList();
-            //Programs = new List<IXMLTVProgram>(program_list.ToArray());            
-            xmltv = XMLTV.CreateInstance(schedule_xml, new EventHandler<CancelEventArgs>(xmltv_cancelevent));
-            var program_count = xmltv.Programs.Values.Count;
-            var program_list = xmltv.Programs.Values.ToList().OrderBy(s => s.Start);
-            //Programs = program_list(0, program_count).ToList();
-            Programs = new List<IXMLTVProgram>(program_list.ToArray());
+            var log_top_entry = txtLog.Lines.FirstOrDefault();
+            txtLog.Clear();
+            if (!string.IsNullOrEmpty(log_top_entry)) { txtLog.AppendText(log_top_entry); }
         }
-
-        void xmltv_cancelevent(object sender, CancelEventArgs e)
+        void UpdateStatus(string logmessage)
         {
-            if (form_closing)
+            try
             {
-                e.Cancel = true;
+                if (this.InvokeRequired)
+                {
+                    try { this.Invoke((Action)(delegate { UpdateStatus(logmessage); })); }
+                    catch { }
+                    return;
+                }
+                txtStatus.Text = logmessage.Replace(System.Environment.NewLine, "");
             }
+            catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
         }
-
-        void CreateControls()
+        void UpdateLogOutput(string logmessage)
         {
-            foreach (var program in Programs)
+            try
             {
-                ListViewItem li = new ListViewItem(string.Format("{0} {1}", program.Channel.Number, program.Channel.CallSign));
-                li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Title));
-                li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.SubTitle));
-                li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Description));
-                li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Start.ToString("yyyy/MM/dd hh:mm tt")));
-                li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Stop.ToString("yyyy/MM/dd hh:mm tt")));
-                lstPrograms.Items.Add(li);
+                if (this.InvokeRequired)
+                {
+                    try { this.Invoke((Action)(delegate { UpdateLogOutput(logmessage); })); }
+                    catch { }
+                    return;
+                }
+                txtLog.AppendText(logmessage);
+                txtLog.SelectionStart = txtLog.Text.Length; //Set the current caret position to the end
+                txtLog.ScrollToCaret(); //Now scroll it automatically
             }
+            catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
         }
+        #endregion
 
-        private void mnuItemOpenXMLTVFile_Click(object sender, EventArgs e)
+        #region Binary Data Read/Write support
+        private void DeserializeDataFromFile(object filename)
+        {
+            if (filename == null) { throw new ArgumentNullException("filename", "cannot be null"); }
+            XMLTV.Load(filename.ToString(), new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
+        }
+        private void DeserializeDataToFile(object filename)
         {
-            ClearLocalLog();
-            //LoadXMLTVShcedule("20130307_continuum_schedule.xml");
-            var result = xmltv_file_chooser.ShowDialog();
-            if (result != DialogResult.OK) return;
-            LoadXMLTVShcedule(xmltv_file_chooser.FileName);
-            CreateControls();
+            if (filename == null) { throw new ArgumentNullException("filename", "cannot be null"); }
+            XMLTV.Save(filename.ToString());
         }
+        #endregion
 
-        private void mnuItemOpenSavedData_Click(object sender, EventArgs e)
+
+        #region t
+        void CreateControls()
         {
-            var result = xmltv_program_data_loader.ShowDialog();
-            if (result != DialogResult.OK) return;
             try
             {
-                using (Stream stream = File.Open(xmltv_program_data_loader.FileName, FileMode.Open))
+                if (this.InvokeRequired)
                 {
-                    try
-                    {
-                        BinaryFormatter bin = new BinaryFormatter();
-                        Programs = (List<IXMLTVProgram>)bin.Deserialize(stream);
-                    }
-                    catch (Exception ex) { gLog.Log.Error.WriteLine(ex.ToString()); MessageBox.Show(ex.Message, "Failed to load data", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
+                    try { this.Invoke((Action)(delegate { CreateControls(); })); }
+                    catch { }
+                    return;
                 }
+                Type data_type;
+                object datasource = XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out data_type);
+                CreatControlsFromType(data_type);
+                //XMLTV.CreateSorterFromDataSource(ref datasource, "title");
+                dataGrid.DataSource = datasource;
+                flow_datagrid_sort.Enabled = true;
             }
-            catch (Exception ex) { gLog.Log.Error.WriteLine(ex.ToString()); MessageBox.Show(ex.Message, "Failed to load data", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
-            MessageBox.Show("Successfully loaded data", "Successfully loaded data", MessageBoxButtons.OK, MessageBoxIcon.Information); 
-            CreateControls();
+            catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
         }
 
-        private void mnuItemSaveData_Click(object sender, EventArgs e)
+        
+        void CreatControlsFromType(Type data_type)
         {
-            var result = xmltv_program_data_saver.ShowDialog();
-            if (result != DialogResult.OK) return;
-            try
+#if USE_DYNAMIC_SORTING_FILTERING_CONTROLS
+              _CreatControlsFromType(data_type);
+#else
+            gLog.Warn.WriteLine("Dynamic creation of sorting and filtering controls has not been enabled.");
+            gLog.Warn.WriteLine("To enable: #define USE_DYNAMIC_SORTING_FILTERING_CONTROLS");
+#endif
+        }
+        [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
+        void _CreatControlsFromType(Type data_type)
+        {
+            string strType = data_type.Name;
+            switch (strType)
             {
-                using (Stream stream = File.Open(xmltv_program_data_saver.FileName, FileMode.Create))
-                {
-                    try
-                    {
-                        BinaryFormatter bin = new BinaryFormatter();
-                        bin.Serialize(stream, Programs);
-                    }
-                    catch (Exception ex) { gLog.Log.Error.WriteLine(ex.ToString()); MessageBox.Show(ex.Message, "Failed to save data", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
-                }
+                case "IChannelDefintionList": CreateChannelDefinitionControls(data_type); break;
+                case "IProgramDefinitionList": CreateProgramDefinitionControls(data_type); break;
+                case "IXMLTVSource": CreateSourceDefinitionControls(data_type); break;
+                default: CreateDefaultControls(data_type); break;
             }
-            catch (Exception ex) { gLog.Log.Error.WriteLine(ex.ToString()); MessageBox.Show(ex.Message, "Failed to save data", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
-            MessageBox.Show("Successfully saved data", "Successfully saved data", MessageBoxButtons.OK, MessageBoxIcon.Information); 
         }
+        [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
+        void CreateDefaultControls(Type data_type)
+        {
+            // no default controls
+            gLog.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
+        }
+        [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
+        void CreateSourceDefinitionControls(Type data_type)
+        {
+            // no controls needed
+            gLog.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
+        }
+        [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
+        void CreateChannelDefinitionControls(Type data_type)
+        {
+            gLog.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
+        }
+        [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
+        void CreateProgramDefinitionControls(Type data_type)
+        {
+            gLog.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
+        }
+        #endregion     
+
 
-        private void main_FormClosing(object sender, FormClosingEventArgs e)
+        #region Column Sorting Support
+        private bool Descending = false;
+        private void ToggleDescedning() { Descending = Descending ? false : true; }
+        private void btnResetSort_Click(object sender, EventArgs e)
         {
-            form_closing = true;
+            Type data_type;
+            object datasource = XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out data_type);
+            dataGrid.DataSource = datasource;
+            ToggleDescedning();
+        }
+        private void btnSortChannelName_Click(object sender, EventArgs e)
+        {
+            object data = dataGrid.DataSource;
+            XMLTV.CreateSorterFromDataSource(ref data, Descending, "channelname");
+            dataGrid.DataSource = data;
+            ToggleDescedning();
         }
 
-        private void mnuItemClearLocalLog_Click(object sender, EventArgs e)
+        private void btnSortStart_Click(object sender, EventArgs e)
         {
-            ClearLocalLog();
+            object data = dataGrid.DataSource;
+            XMLTV.CreateSorterFromDataSource(ref data, Descending, "start");
+            dataGrid.DataSource = data;
+            ToggleDescedning();
         }
-        private void ClearLocalLog()
+        private void btnSortStop_Click(object sender, EventArgs e)
         {
-            var log_top_entry = txtLog.Lines.FirstOrDefault();
-            txtLog.Clear();
-            if (!string.IsNullOrEmpty(log_top_entry))
-            {
-                txtLog.AppendText(log_top_entry);
-            }
+            object data = dataGrid.DataSource;
+            XMLTV.CreateSorterFromDataSource(ref data, Descending, "stop");
+            dataGrid.DataSource = data;
+            ToggleDescedning();
+        }
+        private void btnSortTitle_Click(object sender, EventArgs e)
+        {
+            object data = dataGrid.DataSource;
+            XMLTV.CreateSorterFromDataSource(ref data, Descending, "title");
+            dataGrid.DataSource = data;
+            ToggleDescedning();
+        }
+        private void btnSortSubTitle_Click(object sender, EventArgs e)
+        {
+            object data = dataGrid.DataSource;
+            XMLTV.CreateSorterFromDataSource(ref data, Descending, "subtitle");
+            dataGrid.DataSource = data;
+            ToggleDescedning();
+        }
+        private void btnSortDescription_Click(object sender, EventArgs e)
+        {
+            object data = dataGrid.DataSource;
+            XMLTV.CreateSorterFromDataSource(ref data, Descending, "description");
+            dataGrid.DataSource = data;
+            ToggleDescedning();
         }
+        #endregion
 
+       
     }
 }
 

 

  ViewVC Help
Powered by ViewVC 1.1.22