/[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 47 - (hide annotations) (download)
Fri Mar 8 04:20:09 2013 UTC (6 years, 5 months ago) by william
File size: 8693 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 46 bool form_closing = false;
21 william 44 private IXMLTVRuntimeInstance xmltv;
22 william 42 private bool IsUnix
23     {
24     get { return System.Environment.OSVersion.Platform == PlatformID.Unix; }
25     }
26    
27 william 13 public main()
28 william 2 {
29     InitializeComponent();
30 william 13 string log_path = Application.StartupPath;
31     string log_filename = string.Format("{0}.log", typeof(main).Assembly.GetName().Name);
32 william 41 gLog.CreateLog(string.Format(@"{0}\{1}", log_path, log_filename), false, LogLevel.kLogLevel_All, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush));
33 william 2 }
34 william 11
35 william 40 StringBuilder log_flusher = new StringBuilder();
36 william 42 bool HAVE_CR = false;
37 william 41 void Log_OnFlush(object sender, LoggerOnFlushEventArgs e)
38 william 40 {
39 william 42
40 william 40 log_flusher.Append(e.Buffer);
41 william 42 if (IsUnix)
42 william 40 {
43 william 42 if (e.Buffer == System.Environment.NewLine[0])
44     {
45     OnLogFlush(log_flusher.ToString());
46     log_flusher = new StringBuilder();
47     }
48 william 40 }
49 william 42 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 william 40 }
64 william 47 //bool txtLog_EnterMouse = false;
65 william 40
66 william 47 private void txtLog_MouseLeave(object sender, EventArgs e)
67     {
68     //txtLog_EnterMouse = false;
69     }
70     private void txtLog_MouseEnter(object sender, EventArgs e)
71     {
72     //txtLog_EnterMouse = true;
73     }
74 william 40 void OnLogFlush(string logmessage)
75     {
76 william 47 if (this.IsDisposed) { return; }
77 william 42 Console.Write(logmessage);
78     txtStatus.Text = logmessage.Replace(System.Environment.NewLine, "");
79 william 47 txtLog.AppendText(logmessage);
80    
81     //if (!txtLog_EnterMouse)
82     //{
83     txtLog.SelectionStart = txtLog.Text.Length; //Set the current caret position to the end
84     txtLog.ScrollToCaret(); //Now scroll it automatically
85     //}
86     //Thread.Sleep(100);
87     Application.DoEvents();
88 william 40 }
89    
90 william 29 //List<IXMLTVChannel> Channels;
91     List<IXMLTVProgram> Programs;
92    
93 william 16 private void main_Load(object sender, EventArgs e)
94 william 11 {
95    
96     }
97 william 16 private void main_Shown(object sender, EventArgs e)
98 william 30 {
99 william 36 //libxmltv.Core.XMLTV
100    
101 william 29 }
102    
103    
104     void LoadXMLTVShcedule(string schedule_xml)
105 william 11 {
106 william 36 //var xmltv_loader = XMLTV.CreateLoader(schedule_xml);
107     //var xmltv_parser = XMLTV.CreateParser(xmltv_loader);
108     //var p = XMLTV.GetParser(xmltv_parser);
109 william 29
110 william 36 ////var channel_count = p.Channels.Values.Count;
111     ////Channels = p.Channels.Values.ToList().GetRange(0, channel_count).ToList();
112     //var program_count = p.Programs.Values.Count;
113     //var program_list = p.Programs.Values.ToList().OrderBy(s => s.Start);
114     ////Programs = program_list(0, program_count).ToList();
115 william 46 //Programs = new List<IXMLTVProgram>(program_list.ToArray());
116     xmltv = XMLTV.CreateInstance(schedule_xml, new EventHandler<CancelEventArgs>(xmltv_cancelevent));
117 william 36 var program_count = xmltv.Programs.Values.Count;
118     var program_list = xmltv.Programs.Values.ToList().OrderBy(s => s.Start);
119 william 29 //Programs = program_list(0, program_count).ToList();
120     Programs = new List<IXMLTVProgram>(program_list.ToArray());
121 william 11 }
122 william 29
123 william 46 void xmltv_cancelevent(object sender, CancelEventArgs e)
124     {
125     if (form_closing)
126     {
127     e.Cancel = true;
128     }
129     }
130    
131 william 29 void CreateControls()
132     {
133     foreach (var program in Programs)
134     {
135     ListViewItem li = new ListViewItem(string.Format("{0} {1}", program.Channel.Number, program.Channel.CallSign));
136     li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Title));
137     li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.SubTitle));
138     li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Description));
139     li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Start.ToString("yyyy/MM/dd hh:mm tt")));
140     li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Stop.ToString("yyyy/MM/dd hh:mm tt")));
141     lstPrograms.Items.Add(li);
142     }
143     }
144 william 30
145     private void mnuItemOpenXMLTVFile_Click(object sender, EventArgs e)
146     {
147 william 47 ClearLocalLog();
148 william 32 //LoadXMLTVShcedule("20130307_continuum_schedule.xml");
149     var result = xmltv_file_chooser.ShowDialog();
150     if (result != DialogResult.OK) return;
151     LoadXMLTVShcedule(xmltv_file_chooser.FileName);
152 william 30 CreateControls();
153     }
154    
155     private void mnuItemOpenSavedData_Click(object sender, EventArgs e)
156     {
157 william 31 var result = xmltv_program_data_loader.ShowDialog();
158     if (result != DialogResult.OK) return;
159     try
160     {
161     using (Stream stream = File.Open(xmltv_program_data_loader.FileName, FileMode.Open))
162     {
163     try
164     {
165     BinaryFormatter bin = new BinaryFormatter();
166     Programs = (List<IXMLTVProgram>)bin.Deserialize(stream);
167     }
168 william 33 catch (Exception ex) { gLog.Log.Error.WriteLine(ex.ToString()); MessageBox.Show(ex.Message, "Failed to load data", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
169 william 31 }
170     }
171 william 33 catch (Exception ex) { gLog.Log.Error.WriteLine(ex.ToString()); MessageBox.Show(ex.Message, "Failed to load data", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
172     MessageBox.Show("Successfully loaded data", "Successfully loaded data", MessageBoxButtons.OK, MessageBoxIcon.Information);
173 william 31 CreateControls();
174 william 30 }
175    
176     private void mnuItemSaveData_Click(object sender, EventArgs e)
177     {
178 william 31 var result = xmltv_program_data_saver.ShowDialog();
179     if (result != DialogResult.OK) return;
180     try
181     {
182     using (Stream stream = File.Open(xmltv_program_data_saver.FileName, FileMode.Create))
183     {
184     try
185     {
186     BinaryFormatter bin = new BinaryFormatter();
187     bin.Serialize(stream, Programs);
188     }
189 william 33 catch (Exception ex) { gLog.Log.Error.WriteLine(ex.ToString()); MessageBox.Show(ex.Message, "Failed to save data", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
190 william 31 }
191     }
192 william 33 catch (Exception ex) { gLog.Log.Error.WriteLine(ex.ToString()); MessageBox.Show(ex.Message, "Failed to save data", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
193     MessageBox.Show("Successfully saved data", "Successfully saved data", MessageBoxButtons.OK, MessageBoxIcon.Information);
194 william 30 }
195 william 44
196     private void main_FormClosing(object sender, FormClosingEventArgs e)
197     {
198 william 46 form_closing = true;
199 william 44 }
200 william 47
201     private void mnuItemClearLocalLog_Click(object sender, EventArgs e)
202     {
203     ClearLocalLog();
204     }
205     private void ClearLocalLog()
206     {
207     var log_top_entry = txtLog.Lines.FirstOrDefault();
208     txtLog.Clear();
209     if (!string.IsNullOrEmpty(log_top_entry))
210     {
211     txtLog.AppendText(log_top_entry);
212     }
213     }
214    
215 william 2 }
216     }
217 william 29

  ViewVC Help
Powered by ViewVC 1.1.22