/[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 03:01:48	42
+++ trunk/xmltv_parser/main.cs	2013/03/08 14:24:17	62
@@ -15,8 +15,11 @@
 
 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; }
@@ -25,46 +28,73 @@
         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));
+            gLog.CreateLog(string.Format(@"{0}\{1}", log_path, log_filename), false, LogLevel.kLogLevel_All_NoProgress, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush));
+            gLog.SetLogLevel(gLog.LogLevel & ~LogLevel.kLogLevel_VerboseDebug);
+            gLog.ReportProgressEvent += new EventHandler<ReportProgressEventArgs>(gLog_ReportProgress);
         }
 
-        StringBuilder log_flusher = new StringBuilder();
-        bool HAVE_CR = false;
-        void Log_OnFlush(object sender, LoggerOnFlushEventArgs e)
+        private void ReportProgress(int progress)
         {
-            
-            log_flusher.Append(e.Buffer);
-            if (IsUnix)
+            if (this.InvokeRequired)
             {
-                if (e.Buffer == System.Environment.NewLine[0])
-                {
-                    OnLogFlush(log_flusher.ToString());
-                    log_flusher = new StringBuilder();
-                }
-            }
-            else
-            {                
-                if (e.Buffer == System.Environment.NewLine[0])
+                try
                 {
-                    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;
+                    this.Invoke((Action)(delegate { ReportProgress(progress); }));
                 }
+                catch { }
+                return;
             }
-            
+            progress_status.Value = progress;
         }
+        private void gLog_ReportProgress(object sender, ReportProgressEventArgs e) { ReportProgress(e.Progress); }
 
+        StringBuilder log_flusher = new StringBuilder();
+
+        void Log_OnFlush(object sender, LoggerOnFlushEventArgs e)
+        {
+            OnLogFlush(e.Buffer);
+        }
+        //bool txtLog_EnterMouse = false;
+
+        private void txtLog_MouseLeave(object sender, EventArgs e)
+        {
+            //txtLog_EnterMouse = false;
+        }
+        private void txtLog_MouseEnter(object sender, EventArgs e)
+        {
+            //txtLog_EnterMouse = true;
+        }
         void OnLogFlush(string logmessage)
         {
-            Console.Write(logmessage);        
+            if (this.IsDisposed) { return; }
+            UpdateStatus(logmessage);
+            UpdateLogOutput(logmessage);
+            Application.DoEvents();
+        }
+
+        void UpdateStatus(string logmessage)
+        {
             txtStatus.Text = logmessage.Replace(System.Environment.NewLine, "");
-            Thread.Sleep(100);
+        }
+        void UpdateLogOutput(string logmessage)
+        {
+            if (txtLog.InvokeRequired)
+            {
+                try
+                {
+                    txtLog.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
         }
 
         //List<IXMLTVChannel> Channels;
@@ -76,93 +106,180 @@
         }
         private void main_Shown(object sender, EventArgs e)
         {
-            //libxmltv.Core.XMLTV
-            
+
         }
 
 
         void LoadXMLTVShcedule(string schedule_xml)
         {
-            //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());
-
-            IXMLTVRuntimeInstance xmltv = XMLTV.CreateInstance(schedule_xml);
-            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());
+            //XMLTV.CreateInstance(schedule_xml, new EventHandler<CancelEventArgs>(xmltv_cancelevent));
+            XMLTV.CreateInstance(schedule_xml);
+            XMLTV.OnInstanceCreated += new EventHandler<EventArgs>(XMLTV_OnInstanceCreated);
         }
 
+        void XMLTV_OnInstanceCreated(object sender, EventArgs e)
+        {
+            ReportProgress(0);
+            var instance = XMLTV.GetInstance();
+            if (instance != null)
+            {
+                var program_count = instance.Programs.Values.Count;
+                var program_list = instance.Programs.Values.ToList().OrderBy(s => s.Start);
+                //Programs = program_list(0, program_count).ToList();
+                Programs = new List<IXMLTVProgram>(program_list.ToArray());
+            }
+            CreateControls();
+        }
+
+
+        //void xmltv_cancelevent(object sender, CancelEventArgs e)
+        //{
+        //    if (form_closing)
+        //    {
+        //        e.Cancel = true;
+        //    }
+        //}
+
         void CreateControls()
         {
+            if (this.InvokeRequired)
+            {
+                this.Invoke((Action)(delegate { CreateControls(); }));
+                return;
+            }
+            List<ListViewItem> items = new List<ListViewItem>();
             foreach (var program in Programs)
             {
                 ListViewItem li = new ListViewItem(string.Format("{0} {1}", program.Channel.Number, program.Channel.CallSign));
+                li.Tag = program;
                 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);
+                //lstPrograms.Items.Add(li);
+                items.Add(li);
             }
+
+            lstPrograms.Items.AddRange(items.ToArray());
         }
 
         private void mnuItemOpenXMLTVFile_Click(object sender, EventArgs e)
         {
-            //LoadXMLTVShcedule("20130307_continuum_schedule.xml");
-            var result = xmltv_file_chooser.ShowDialog();
-            if (result != DialogResult.OK) return;
-            LoadXMLTVShcedule(xmltv_file_chooser.FileName);
-            CreateControls();
+            try
+            {
+                lstPrograms.Items.Clear();
+                //LoadXMLTVShcedule("20130307_continuum_schedule.xml");
+                var result = xmltv_file_chooser.ShowDialog();
+                if (result != DialogResult.OK) return;
+                ClearLocalLog();
+                LoadXMLTVShcedule(xmltv_file_chooser.FileName);
+                //CreateControls();
+            }
+            catch (Exception ex)
+            {
+                gLog.Error.WriteLine(ex.ToString());
+            }
         }
 
         private void mnuItemOpenSavedData_Click(object sender, EventArgs e)
         {
-            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))
+                var result = xmltv_program_data_loader.ShowDialog();
+                if (result != DialogResult.OK) return;
+                string filename = xmltv_program_data_loader.FileName;
+                IXMLTVRuntimeInstance xmltv = null;
+                bool status = false;
+                xmltv = XMLTV.DeSerialize(filename, out status);
+                XMLTV.CreateFromInstance(xmltv, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
+                if (!status)
                 {
-                    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; }
+                    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);
+            }
+            catch (Exception ex)
+            {
+                gLog.Error.WriteLine(ex.ToString());
             }
-            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();
         }
 
         private void mnuItemSaveData_Click(object sender, EventArgs e)
         {
-            var result = xmltv_program_data_saver.ShowDialog();
-            if (result != DialogResult.OK) return;
             try
             {
-                using (Stream stream = File.Open(xmltv_program_data_saver.FileName, FileMode.Create))
+                var result = xmltv_program_data_saver.ShowDialog();
+                if (result != DialogResult.OK) return;
+                string filename = xmltv_program_data_saver.FileName;
+                if (!XMLTV.Serialize(filename))
                 {
-                    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; }
+                    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);
+            }
+            catch (Exception ex)
+            {
+                gLog.Error.WriteLine(ex.ToString());
+            }
+        }
+
+        private void main_FormClosing(object sender, FormClosingEventArgs e)
+        {
+            try
+            {
+                XMLTV.DestroyInstance();
+            }
+            catch { }
+        }
+
+        private void mnuItemClearLocalLog_Click(object sender, EventArgs e)
+        {
+            ClearLocalLog();
+        }
+        private void ClearLocalLog()
+        {
+            var log_top_entry = txtLog.Lines.FirstOrDefault();
+            txtLog.Clear();
+            if (!string.IsNullOrEmpty(log_top_entry))
+            {
+                txtLog.AppendText(log_top_entry);
+            }
+        }
+
+        private void mnuItemExit_Click(object sender, EventArgs e)
+        {
+            this.Close();
+        }
+
+        private void lstPrograms_SelectedIndexChanged(object sender, EventArgs e)
+        {
+            IXMLTVProgram program = null;
+            var item = lstPrograms.SelectedItems[0];
+            int selected_index = lstPrograms.Items.IndexOf(item);
+            program = (item.Tag as IXMLTVProgram);
+            gLog.Debug.WriteLine("Selected ListViewItem at index: {0}", selected_index);
+            if (program == null) { return; }
+            txtStatus.Text = program.ToString();
+            //gLog.Debug.WriteLine(program.ToString());
+
+        }
+
+        private void lstPrograms_ColumnClick(object sender, ColumnClickEventArgs e)
+        {
+            if (!(lstPrograms.ListViewItemSorter is ListViewSorter)) return;
+            ListViewSorter Sorter = (ListViewSorter)lstPrograms.ListViewItemSorter;
+            Sorter.LastColumn = Sorter.CurrentColumn;
+            Sorter.CurrentColumn = e.Column;
+            if (Sorter.LastColumn == e.Column)
+            {
+                if (lstPrograms.Sorting == SortOrder.Ascending) { lstPrograms.Sorting = SortOrder.Descending; }
+                else { lstPrograms.Sorting = SortOrder.Ascending; }
             }
-            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); 
+            else { lstPrograms.Sorting = SortOrder.Descending; }
+            lstPrograms.Sort();
         }
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22