/[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/07 09:26:43	16
+++ trunk/xmltv_parser/main.cs	2013/03/08 03:36:44	44
@@ -2,33 +2,177 @@
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
+using System.Linq;
 using System.Drawing;
 using System.Text;
 using System.Windows.Forms;
 using libxmltv.Core;
 using Enterprise.Logging;
+using libxmltv.Interfaces;
+using System.IO;
+using System.Runtime.Serialization.Formatters.Binary;
+using System.Threading;
 
 namespace xmltv_parser
 {
     public partial class main : Form
     {
+        private IXMLTVRuntimeInstance xmltv;
+        private bool IsUnix
+        {
+            get { return System.Environment.OSVersion.Platform == PlatformID.Unix; }
+        }
+
         public main()
         {
             InitializeComponent();
             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);            
+            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)
+        {
+            
+            log_flusher.Append(e.Buffer);
+            if (IsUnix)
+            {
+                if (e.Buffer == System.Environment.NewLine[0])
+                {
+                    OnLogFlush(log_flusher.ToString());
+                    log_flusher = new StringBuilder();
+                }
+            }
+            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;
+                }
+            }
+            
+        }
+
+        void OnLogFlush(string logmessage)
+        {
+            Console.Write(logmessage);        
+            txtStatus.Text = logmessage.Replace(System.Environment.NewLine, "");
+            Thread.Sleep(100);
         }
 
+        //List<IXMLTVChannel> Channels;
+        List<IXMLTVProgram> Programs;
+
         private void main_Load(object sender, EventArgs e)
         {
 
         }
         private void main_Shown(object sender, EventArgs e)
         {
-            gLog.Log.Debug.WriteLine("");
-            var xmltv = XMLTV.CreateLoader("20130307_continuum_schedule.xml");
-            //XMLTV.Test(xmltv);
+            //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());
+
+            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());
+        }
+
+        void CreateControls()
+        {
+            foreach (var program in Programs)
+            {
+                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);
+            }
+        }
+
+        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();
+        }
+
+        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))
+                {
+                    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; }
+                }
+            }
+            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))
+                {
+                    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; }
+                }
+            }
+            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); 
+        }
+
+        private void main_FormClosing(object sender, FormClosingEventArgs e)
+        {
+            if (xmltv != null)
+            {
+                xmltv.Dispose();
+            }
         }
     }
 }
+

 

  ViewVC Help
Powered by ViewVC 1.1.22