/[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 46 - (show annotations) (download)
Fri Mar 8 04:01:25 2013 UTC (6 years, 7 months ago) by william
File size: 7563 byte(s)
+ add support to cancel parseing when main form is closed

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

  ViewVC Help
Powered by ViewVC 1.1.22