/[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 44 - (hide annotations) (download)
Fri Mar 8 03:36:44 2013 UTC (6 years, 7 months ago) by william
File size: 7340 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22