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

Annotation of /trunk/xmltv_parser/main.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 42 - (hide annotations) (download)
Fri Mar 8 03:01:48 2013 UTC (6 years, 9 months ago) by william
File size: 7118 byte(s)
+ write logmessages to console
+ write logmesages to status strip control
* remove newlines as needed

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

  ViewVC Help
Powered by ViewVC 1.1.22