/[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 13:57:11	33
+++ trunk/xmltv_parser/main.cs	2013/03/08 05:20:12	49
@@ -11,17 +11,80 @@
 using libxmltv.Interfaces;
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;
+using System.Threading;
 
 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();
             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;
+                }
+            }
+            
+        }
+        //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)
+        {
+            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();
         }
 
         //List<IXMLTVChannel> Channels;
@@ -33,23 +96,29 @@
         }
         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);
+            XMLTV.CreateInstance(schedule_xml, new EventHandler<CancelEventArgs>(xmltv_cancelevent));
+            xmltv = XMLTV.GetInstance();
+            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 xmltv_cancelevent(object sender, CancelEventArgs e)
+        {
+            if (form_closing)
+            {
+                e.Cancel = true;
+            }
+        }
+
         void CreateControls()
         {
             foreach (var program in Programs)
@@ -66,6 +135,7 @@
 
         private void mnuItemOpenXMLTVFile_Click(object sender, EventArgs e)
         {
+            ClearLocalLog();
             //LoadXMLTVShcedule("20130307_continuum_schedule.xml");
             var result = xmltv_file_chooser.ShowDialog();
             if (result != DialogResult.OK) return;
@@ -77,19 +147,12 @@
         {
             var result = xmltv_program_data_loader.ShowDialog();
             if (result != DialogResult.OK) return;
-            try
+            string filename = xmltv_program_data_loader.FileName;
+            if (!XMLTV.GetSerializer().DeSerialize(filename, out xmltv))
             {
-                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; }
-                }
+                MessageBox.Show("Failed to load data - check log", "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();
         }
@@ -98,21 +161,34 @@
         {
             var result = xmltv_program_data_saver.ShowDialog();
             if (result != DialogResult.OK) return;
-            try
+            string filename = xmltv_program_data_saver.FileName;
+            if (!XMLTV.GetSerializer().Serialize(filename))
             {
-                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; }
-                }
+                MessageBox.Show("Failed to save data - check log", "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)
+        {
+            form_closing = true;
+        }
+
+        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);
+            }
+        }
+
     }
 }
 

 

  ViewVC Help
Powered by ViewVC 1.1.22