/[xmltv_parser]/trunk/xmltv_parser/main.cs
ViewVC logotype

Contents of /trunk/xmltv_parser/main.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 40 - (show annotations) (download)
Fri Mar 8 02:40:55 2013 UTC (6 years, 5 months ago) by william
File size: 6370 byte(s)
+ display logmessages in status strip at bottom on main form

1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Linq;
6 using System.Drawing;
7 using System.Text;
8 using System.Windows.Forms;
9 using libxmltv.Core;
10 using Enterprise.Logging;
11 using libxmltv.Interfaces;
12 using System.IO;
13 using System.Runtime.Serialization.Formatters.Binary;
14 using System.Threading;
15
16 namespace xmltv_parser
17 {
18 public partial class main : Form
19 {
20 public main()
21 {
22 InitializeComponent();
23 string log_path = Application.StartupPath;
24 string log_filename = string.Format("{0}.log", typeof(main).Assembly.GetName().Name);
25 gLog.CreateLog(string.Format(@"{0}\{1}", log_path, log_filename), false, LogLevel.kLogLevel_All, new EventHandler<LoggerOnFlushEventArgs>(Logger_OnFlush));
26 }
27
28 StringBuilder log_flusher = new StringBuilder();
29 void Logger_OnFlush(object sender, LoggerOnFlushEventArgs e)
30 {
31 //Console.Write(e.Buffer);
32 log_flusher.Append(e.Buffer);
33 if(e.Buffer == System.Environment.NewLine[0])
34 {
35 OnLogFlush(log_flusher.ToString().TrimEnd(new char[] { System.Environment.NewLine[0] }).TrimStart(new char[]{'\n'}));
36 log_flusher = new StringBuilder();
37 }
38 }
39
40 void OnLogFlush(string logmessage)
41 {
42 txtStatus.Text = logmessage;
43 Thread.Sleep(100);
44 }
45
46 //List<IXMLTVChannel> Channels;
47 List<IXMLTVProgram> Programs;
48
49 private void main_Load(object sender, EventArgs e)
50 {
51
52 }
53 private void main_Shown(object sender, EventArgs e)
54 {
55 //libxmltv.Core.XMLTV
56
57 }
58
59
60 void LoadXMLTVShcedule(string schedule_xml)
61 {
62 //var xmltv_loader = XMLTV.CreateLoader(schedule_xml);
63 //var xmltv_parser = XMLTV.CreateParser(xmltv_loader);
64 //var p = XMLTV.GetParser(xmltv_parser);
65
66 ////var channel_count = p.Channels.Values.Count;
67 ////Channels = p.Channels.Values.ToList().GetRange(0, channel_count).ToList();
68 //var program_count = p.Programs.Values.Count;
69 //var program_list = p.Programs.Values.ToList().OrderBy(s => s.Start);
70 ////Programs = program_list(0, program_count).ToList();
71 //Programs = new List<IXMLTVProgram>(program_list.ToArray());
72
73 IXMLTVRuntimeInstance xmltv = XMLTV.CreateInstance(schedule_xml);
74 var program_count = xmltv.Programs.Values.Count;
75 var program_list = xmltv.Programs.Values.ToList().OrderBy(s => s.Start);
76 //Programs = program_list(0, program_count).ToList();
77 Programs = new List<IXMLTVProgram>(program_list.ToArray());
78 }
79
80 void CreateControls()
81 {
82 foreach (var program in Programs)
83 {
84 ListViewItem li = new ListViewItem(string.Format("{0} {1}", program.Channel.Number, program.Channel.CallSign));
85 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Title));
86 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.SubTitle));
87 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Description));
88 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Start.ToString("yyyy/MM/dd hh:mm tt")));
89 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Stop.ToString("yyyy/MM/dd hh:mm tt")));
90 lstPrograms.Items.Add(li);
91 }
92 }
93
94 private void mnuItemOpenXMLTVFile_Click(object sender, EventArgs e)
95 {
96 //LoadXMLTVShcedule("20130307_continuum_schedule.xml");
97 var result = xmltv_file_chooser.ShowDialog();
98 if (result != DialogResult.OK) return;
99 LoadXMLTVShcedule(xmltv_file_chooser.FileName);
100 CreateControls();
101 }
102
103 private void mnuItemOpenSavedData_Click(object sender, EventArgs e)
104 {
105 var result = xmltv_program_data_loader.ShowDialog();
106 if (result != DialogResult.OK) return;
107 try
108 {
109 using (Stream stream = File.Open(xmltv_program_data_loader.FileName, FileMode.Open))
110 {
111 try
112 {
113 BinaryFormatter bin = new BinaryFormatter();
114 Programs = (List<IXMLTVProgram>)bin.Deserialize(stream);
115 }
116 catch (Exception ex) { gLog.Log.Error.WriteLine(ex.ToString()); MessageBox.Show(ex.Message, "Failed to load data", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
117 }
118 }
119 catch (Exception ex) { gLog.Log.Error.WriteLine(ex.ToString()); MessageBox.Show(ex.Message, "Failed to load data", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
120 MessageBox.Show("Successfully loaded data", "Successfully loaded data", MessageBoxButtons.OK, MessageBoxIcon.Information);
121 CreateControls();
122 }
123
124 private void mnuItemSaveData_Click(object sender, EventArgs e)
125 {
126 var result = xmltv_program_data_saver.ShowDialog();
127 if (result != DialogResult.OK) return;
128 try
129 {
130 using (Stream stream = File.Open(xmltv_program_data_saver.FileName, FileMode.Create))
131 {
132 try
133 {
134 BinaryFormatter bin = new BinaryFormatter();
135 bin.Serialize(stream, Programs);
136 }
137 catch (Exception ex) { gLog.Log.Error.WriteLine(ex.ToString()); MessageBox.Show(ex.Message, "Failed to save data", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
138 }
139 }
140 catch (Exception ex) { gLog.Log.Error.WriteLine(ex.ToString()); MessageBox.Show(ex.Message, "Failed to save data", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
141 MessageBox.Show("Successfully saved data", "Successfully saved data", MessageBoxButtons.OK, MessageBoxIcon.Information);
142 }
143 }
144 }
145

  ViewVC Help
Powered by ViewVC 1.1.22