/[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 14:26:48	63
+++ trunk/xmltv_parser/main.cs	2013/03/10 16:09:35	116
@@ -1,4 +1,6 @@
-´╗┐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
+using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
@@ -12,19 +14,14 @@
 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();
@@ -33,7 +30,28 @@
             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_NoProgress, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush));
-            gLog.SetLogLevel(gLog.LogLevel & ~LogLevel.kLogLevel_VerboseDebug);
+#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);
         }
 
@@ -48,7 +66,8 @@
                 catch { }
                 return;
             }
-            progress_status.Value = progress;
+            if (progress_status != null && !this.IsDisposed)
+                progress_status.Value = progress;
         }
         private void gLog_ReportProgress(object sender, ReportProgressEventArgs e) { ReportProgress(e.Progress); }
 
@@ -78,15 +97,24 @@
 
         void UpdateStatus(string logmessage)
         {
+            if (this.InvokeRequired)
+            {
+                try
+                {
+                    this.Invoke((Action)(delegate { UpdateStatus(logmessage); }));
+                }
+                catch { }
+                return;
+            }
             txtStatus.Text = logmessage.Replace(System.Environment.NewLine, "");
         }
         void UpdateLogOutput(string logmessage)
         {
-            if (txtLog.InvokeRequired)
+            if (this.InvokeRequired)
             {
                 try
                 {
-                    txtLog.Invoke((Action)(delegate { UpdateLogOutput(logmessage); }));
+                    this.Invoke((Action)(delegate { UpdateLogOutput(logmessage); }));
                 }
                 catch { }
                 return;
@@ -111,9 +139,7 @@
 
         void LoadXMLTVShcedule(string schedule_xml)
         {
-            //XMLTV.CreateInstance(schedule_xml, new EventHandler<CancelEventArgs>(xmltv_cancelevent));
-            XMLTV.CreateInstance(schedule_xml);
-            XMLTV.OnInstanceCreated += new EventHandler<EventArgs>(XMLTV_OnInstanceCreated);
+            XMLTV.CreateInstance(schedule_xml, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
         }
 
         void XMLTV_OnInstanceCreated(object sender, EventArgs e)
@@ -122,8 +148,8 @@
             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);
+                var program_count = instance.Programs.Count;
+                var program_list = instance.Programs.ToList().OrderBy(s => s.MetaData[XMLTVConstants.Programs.ProgramStart].ToString());
                 //Programs = program_list(0, program_count).ToList();
                 Programs = new List<IXMLTVProgram>(program_list.ToArray());
             }
@@ -149,13 +175,36 @@
             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));
+                IXMLTVChannel Channel = null;
+                var instance = XMLTV.GetInstance();
+                var id = program.MetaData[XMLTVConstants.Programs.ProgramChannelId].ToString();
+                Channel = instance.Channels.Find(m => m.Id == id);
+                //ListViewItem li = new ListViewItem(string.Format("{0} {1}", Channel.Number, Channel.CallSign));
+                if (Channel == null)
+                {
+                    throw new NullReferenceException(string.Format("Could not find any channel with an id of '{0}'", id));
+                }
+                if (Channel.MetaData.Count() == 0) { throw new ArgumentOutOfRangeException(string.Format("No metadata available for channel id '{0}'", Channel.Id)); }
+                var channel_names = Channel.MetaData[XMLTVConstants.Channels.ChannelDisplayName];
+                string channel_name = string.Empty;
+                if (channel_names.Count() == 0) { throw new ArgumentOutOfRangeException(string.Format("No properties named '{0}' found for channel id '{1}'", XMLTVConstants.Channels.ChannelDisplayName, Channel.Id)); }
+                else
+                {
+                    var channame = channel_names.FirstOrDefault();
+                    if (channame == null) { throw new ArgumentNullException("channame", string.Format("Unable to get channel display name for channel id '{0}'", Channel.Id)); }
+                    else
+                    {
+                        if (channame.Value == null) { throw new ArgumentNullException("channame.Value", string.Format("Unable to get channel display name for channel id '{0}'", Channel.Id)); }
+                        else { channel_name = channame.Value.ToString(); }
+                    }
+                }
+                ListViewItem li = new ListViewItem(string.Format("{0}", channel_name));
                 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")));
+                li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.MetaData[XMLTVConstants.Programs.ProgramTitle].ToString()));
+                li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.MetaData[XMLTVConstants.Programs.ProgramSubTitle].ToString()));
+                li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.MetaData[XMLTVConstants.Programs.ProgramDescription].ToString()));
+                li.SubItems.Add(new ListViewItem.ListViewSubItem(li, ((DateTime)program.MetaData[XMLTVConstants.Programs.ProgramStart]).ToString("yyyy/MM/dd hh:mm tt")));
+                li.SubItems.Add(new ListViewItem.ListViewSubItem(li, ((DateTime)program.MetaData[XMLTVConstants.Programs.ProgramStop]).ToString("yyyy/MM/dd hh:mm tt")));
                 //lstPrograms.Items.Add(li);
                 items.Add(li);
             }
@@ -181,6 +230,20 @@
             }
         }
 
+        private void DeserializeDataFromFile(object filename)
+        {
+            if (filename == null) { throw new ArgumentNullException("filename", "cannot be null"); }
+            bool status = false;
+            IXMLTVRuntimeInstance xmltv = XMLTV.DeSerialize(filename.ToString(), out status);
+            XMLTV.CreateFromInstance(xmltv, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
+            if (!status)
+            {
+                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);
+        }
+
         private void mnuItemOpenSavedData_Click(object sender, EventArgs e)
         {
             try
@@ -188,23 +251,23 @@
                 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)
-                {
-                    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);
+                Thread worker = new Thread(new ParameterizedThreadStart(DeserializeDataFromFile)); worker.Start(filename);
             }
             catch (Exception ex)
             {
                 gLog.Error.WriteLine(ex.ToString());
             }
         }
-
+        private void DeserializeDataToFile(object filename)
+        {
+            if (filename == null) { throw new ArgumentNullException("filename", "cannot be null"); }
+            if (!XMLTV.Serialize(filename.ToString()))
+            {
+                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);
+        }
         private void mnuItemSaveData_Click(object sender, EventArgs e)
         {
             try
@@ -212,12 +275,7 @@
                 var result = xmltv_program_data_saver.ShowDialog();
                 if (result != DialogResult.OK) return;
                 string filename = xmltv_program_data_saver.FileName;
-                if (!XMLTV.Serialize(filename))
-                {
-                    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);
+                Thread worker = new Thread(new ParameterizedThreadStart(DeserializeDataToFile)); worker.Start(filename);
             }
             catch (Exception ex)
             {

 

  ViewVC Help
Powered by ViewVC 1.1.22