/[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/09 13:51:58	86
+++ trunk/xmltv_parser/main.cs	2013/03/10 17:43:20	119
@@ -15,6 +15,7 @@
 using System.Runtime.Serialization.Formatters.Binary;
 using System.Threading;
 using System.Diagnostics;
+using System.Xml.Linq;
 
 namespace xmltv_parser
 {
@@ -65,8 +66,8 @@
                 catch { }
                 return;
             }
-            if (this.IsDisposed) { 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); }
 
@@ -124,7 +125,7 @@
         }
 
         //List<IXMLTVChannel> Channels;
-        List<IXMLTVProgram> Programs;
+        //List<IXMLTVProgram> Programs;
 
         private void main_Load(object sender, EventArgs e)
         {
@@ -138,22 +139,22 @@
 
         void LoadXMLTVShcedule(string schedule_xml)
         {
-            //XMLTV.CreateInstance(schedule_xml, new EventHandler<CancelEventArgs>(xmltv_cancelevent));
-            XMLTV.CreateInstance(schedule_xml, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
-            //XMLTV.OnInstanceCreated += new EventHandler<EventArgs>(XMLTV_OnInstanceCreated);
+            XMLTV.Create(schedule_xml, 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.Count;
-                var program_list = instance.Programs.ToList().OrderBy(s => s.GetProperty(XMLTVConstants.Programs.ProgramStart).ToString());
-                //Programs = program_list(0, program_count).ToList();
-                Programs = new List<IXMLTVProgram>(program_list.ToArray());
-            }
+            //ReportProgress(0);
+            //var instance = XMLTV.GetInstance();
+            //if (instance != null)
+            //{
+            //    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());
+            //}
+            //CreateControls();
+
             CreateControls();
         }
 
@@ -173,25 +174,47 @@
                 this.Invoke((Action)(delegate { CreateControls(); }));
                 return;
             }
-            List<ListViewItem> items = new List<ListViewItem>();
-            foreach (var program in Programs)
-            {
-                IXMLTVChannel Channel = null;
-                var instance = XMLTV.GetInstance();
-                var id = program.GetProperty(XMLTVConstants.Programs.ProgramChannelId).ToString();
-                Channel = instance.Channels.Find(m => m.Id == id);
-                ListViewItem li = new ListViewItem(string.Format("{0} {1}", Channel.Number, Channel.CallSign));
-                li.Tag = program;
-                li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.GetProperty(XMLTVConstants.Programs.ProgramTitle).ToString()));
-                li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.GetProperty(XMLTVConstants.Programs.ProgramSubTitle).ToString()));
-                li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.GetProperty(XMLTVConstants.Programs.ProgramDescription).ToString()));
-                li.SubItems.Add(new ListViewItem.ListViewSubItem(li, ((DateTime)program.GetProperty(XMLTVConstants.Programs.ProgramStart)).ToString("yyyy/MM/dd hh:mm tt")));
-                li.SubItems.Add(new ListViewItem.ListViewSubItem(li, ((DateTime)program.GetProperty(XMLTVConstants.Programs.ProgramStop)).ToString("yyyy/MM/dd hh:mm tt")));
-                //lstPrograms.Items.Add(li);
-                items.Add(li);
-            }
+            //List<ListViewItem> items = new List<ListViewItem>();
+            //foreach (var program in Programs)
+            //{
+            //    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.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);
+            //}
+
+            //lstPrograms.Items.AddRange(items.ToArray());
+
+            
 
-            lstPrograms.Items.AddRange(items.ToArray());
         }
 
         private void mnuItemOpenXMLTVFile_Click(object sender, EventArgs e)
@@ -212,6 +235,12 @@
             }
         }
 
+        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 mnuItemOpenSavedData_Click(object sender, EventArgs e)
         {
             try
@@ -219,23 +248,18 @@
                 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"); }
+            XMLTV.Save(filename.ToString());
+        }
         private void mnuItemSaveData_Click(object sender, EventArgs e)
         {
             try
@@ -243,12 +267,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)
             {
@@ -260,7 +279,7 @@
         {
             try
             {
-                XMLTV.DestroyInstance();
+                XMLTV.Destroy();
             }
             catch { }
         }
@@ -286,30 +305,30 @@
 
         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());
+            //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; }
-            }
-            else { lstPrograms.Sorting = SortOrder.Descending; }
-            lstPrograms.Sort();
+            //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; }
+            //}
+            //else { lstPrograms.Sorting = SortOrder.Descending; }
+            //lstPrograms.Sort();
         }
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22