/[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 52 - (show annotations) (download)
Fri Mar 8 07:01:37 2013 UTC (6 years, 9 months ago) by william
File size: 7779 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22