/[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 67 - (show annotations) (download)
Sat Mar 9 01:22:07 2013 UTC (6 years, 6 months ago) by william
File size: 10521 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 public main()
22 {
23 InitializeComponent();
24 ListViewSorter Sorter = new ListViewSorter();
25 lstPrograms.ListViewItemSorter = Sorter;
26 string log_path = Application.StartupPath;
27 string log_filename = string.Format("{0}.log", typeof(main).Assembly.GetName().Name);
28 gLog.CreateLog(string.Format(@"{0}\{1}", log_path, log_filename), false, LogLevel.kLogLevel_All_NoProgress, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush));
29 #if DEBUG
30 LogLevel gLevel = gLog.LogLevel;
31 gLevel |= LogLevel.kLogLevel_VerboseDebug;
32 gLevel |= LogLevel.kLogLevel_Debug;
33 gLog.SetLogLevel(gLevel);
34 #else
35 LogLevel gLevel = LogLevel.kLogLevel_Default;
36 gLog.SetLogLevel(gLevel);
37 #endif
38
39
40 gLog.ReportProgressEvent += new EventHandler<ReportProgressEventArgs>(gLog_ReportProgress);
41 }
42
43 private void ReportProgress(int progress)
44 {
45 if (this.InvokeRequired)
46 {
47 try
48 {
49 this.Invoke((Action)(delegate { ReportProgress(progress); }));
50 }
51 catch { }
52 return;
53 }
54 progress_status.Value = progress;
55 }
56 private void gLog_ReportProgress(object sender, ReportProgressEventArgs e) { ReportProgress(e.Progress); }
57
58 StringBuilder log_flusher = new StringBuilder();
59
60 void Log_OnFlush(object sender, LoggerOnFlushEventArgs e)
61 {
62 OnLogFlush(e.Buffer);
63 }
64 //bool txtLog_EnterMouse = false;
65
66 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 void OnLogFlush(string logmessage)
75 {
76 if (this.IsDisposed) { return; }
77 UpdateStatus(logmessage);
78 UpdateLogOutput(logmessage);
79 Application.DoEvents();
80 }
81
82 void UpdateStatus(string logmessage)
83 {
84 txtStatus.Text = logmessage.Replace(System.Environment.NewLine, "");
85 }
86 void UpdateLogOutput(string logmessage)
87 {
88 if (txtLog.InvokeRequired)
89 {
90 try
91 {
92 txtLog.Invoke((Action)(delegate { UpdateLogOutput(logmessage); }));
93 }
94 catch { }
95 return;
96 }
97 txtLog.AppendText(logmessage);
98 txtLog.SelectionStart = txtLog.Text.Length; //Set the current caret position to the end
99 txtLog.ScrollToCaret(); //Now scroll it automatically
100 }
101
102 //List<IXMLTVChannel> Channels;
103 List<IXMLTVProgram> Programs;
104
105 private void main_Load(object sender, EventArgs e)
106 {
107
108 }
109 private void main_Shown(object sender, EventArgs e)
110 {
111
112 }
113
114
115 void LoadXMLTVShcedule(string schedule_xml)
116 {
117 //XMLTV.CreateInstance(schedule_xml, new EventHandler<CancelEventArgs>(xmltv_cancelevent));
118 XMLTV.CreateInstance(schedule_xml);
119 XMLTV.OnInstanceCreated += new EventHandler<EventArgs>(XMLTV_OnInstanceCreated);
120 }
121
122 void XMLTV_OnInstanceCreated(object sender, EventArgs e)
123 {
124 ReportProgress(0);
125 var instance = XMLTV.GetInstance();
126 if (instance != null)
127 {
128 var program_count = instance.Programs.Values.Count;
129 var program_list = instance.Programs.Values.ToList().OrderBy(s => s.Start);
130 //Programs = program_list(0, program_count).ToList();
131 Programs = new List<IXMLTVProgram>(program_list.ToArray());
132 }
133 CreateControls();
134 }
135
136
137 //void xmltv_cancelevent(object sender, CancelEventArgs e)
138 //{
139 // if (form_closing)
140 // {
141 // e.Cancel = true;
142 // }
143 //}
144
145 void CreateControls()
146 {
147 if (this.InvokeRequired)
148 {
149 this.Invoke((Action)(delegate { CreateControls(); }));
150 return;
151 }
152 List<ListViewItem> items = new List<ListViewItem>();
153 foreach (var program in Programs)
154 {
155 ListViewItem li = new ListViewItem(string.Format("{0} {1}", program.Channel.Number, program.Channel.CallSign));
156 li.Tag = program;
157 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Title));
158 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.SubTitle));
159 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Description));
160 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Start.ToString("yyyy/MM/dd hh:mm tt")));
161 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Stop.ToString("yyyy/MM/dd hh:mm tt")));
162 //lstPrograms.Items.Add(li);
163 items.Add(li);
164 }
165
166 lstPrograms.Items.AddRange(items.ToArray());
167 }
168
169 private void mnuItemOpenXMLTVFile_Click(object sender, EventArgs e)
170 {
171 try
172 {
173 lstPrograms.Items.Clear();
174 //LoadXMLTVShcedule("20130307_continuum_schedule.xml");
175 var result = xmltv_file_chooser.ShowDialog();
176 if (result != DialogResult.OK) return;
177 ClearLocalLog();
178 LoadXMLTVShcedule(xmltv_file_chooser.FileName);
179 //CreateControls();
180 }
181 catch (Exception ex)
182 {
183 gLog.Error.WriteLine(ex.ToString());
184 }
185 }
186
187 private void mnuItemOpenSavedData_Click(object sender, EventArgs e)
188 {
189 try
190 {
191 var result = xmltv_program_data_loader.ShowDialog();
192 if (result != DialogResult.OK) return;
193 string filename = xmltv_program_data_loader.FileName;
194 IXMLTVRuntimeInstance xmltv = null;
195 bool status = false;
196 xmltv = XMLTV.DeSerialize(filename, out status);
197 XMLTV.CreateFromInstance(xmltv, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
198 if (!status)
199 {
200 MessageBox.Show("Failed to load data - check log", "Failed to load data", MessageBoxButtons.OK, MessageBoxIcon.Error);
201 return;
202 }
203 MessageBox.Show("Successfully loaded data", "Successfully loaded data", MessageBoxButtons.OK, MessageBoxIcon.Information);
204 }
205 catch (Exception ex)
206 {
207 gLog.Error.WriteLine(ex.ToString());
208 }
209 }
210
211 private void mnuItemSaveData_Click(object sender, EventArgs e)
212 {
213 try
214 {
215 var result = xmltv_program_data_saver.ShowDialog();
216 if (result != DialogResult.OK) return;
217 string filename = xmltv_program_data_saver.FileName;
218 if (!XMLTV.Serialize(filename))
219 {
220 MessageBox.Show("Failed to save data - check log", "Failed to save data", MessageBoxButtons.OK, MessageBoxIcon.Error);
221 return;
222 }
223 MessageBox.Show("Successfully saved data", "Successfully saved data", MessageBoxButtons.OK, MessageBoxIcon.Information);
224 }
225 catch (Exception ex)
226 {
227 gLog.Error.WriteLine(ex.ToString());
228 }
229 }
230
231 private void main_FormClosing(object sender, FormClosingEventArgs e)
232 {
233 try
234 {
235 XMLTV.DestroyInstance();
236 }
237 catch { }
238 }
239
240 private void mnuItemClearLocalLog_Click(object sender, EventArgs e)
241 {
242 ClearLocalLog();
243 }
244 private void ClearLocalLog()
245 {
246 var log_top_entry = txtLog.Lines.FirstOrDefault();
247 txtLog.Clear();
248 if (!string.IsNullOrEmpty(log_top_entry))
249 {
250 txtLog.AppendText(log_top_entry);
251 }
252 }
253
254 private void mnuItemExit_Click(object sender, EventArgs e)
255 {
256 this.Close();
257 }
258
259 private void lstPrograms_SelectedIndexChanged(object sender, EventArgs e)
260 {
261 IXMLTVProgram program = null;
262 var item = lstPrograms.SelectedItems[0];
263 int selected_index = lstPrograms.Items.IndexOf(item);
264 program = (item.Tag as IXMLTVProgram);
265 gLog.Debug.WriteLine("Selected ListViewItem at index: {0}", selected_index);
266 if (program == null) { return; }
267 txtStatus.Text = program.ToString();
268 //gLog.Debug.WriteLine(program.ToString());
269
270 }
271
272 private void lstPrograms_ColumnClick(object sender, ColumnClickEventArgs e)
273 {
274 if (!(lstPrograms.ListViewItemSorter is ListViewSorter)) return;
275 ListViewSorter Sorter = (ListViewSorter)lstPrograms.ListViewItemSorter;
276 Sorter.LastColumn = Sorter.CurrentColumn;
277 Sorter.CurrentColumn = e.Column;
278 if (Sorter.LastColumn == e.Column)
279 {
280 if (lstPrograms.Sorting == SortOrder.Ascending) { lstPrograms.Sorting = SortOrder.Descending; }
281 else { lstPrograms.Sorting = SortOrder.Ascending; }
282 }
283 else { lstPrograms.Sorting = SortOrder.Descending; }
284 lstPrograms.Sort();
285 }
286 }
287 }
288

  ViewVC Help
Powered by ViewVC 1.1.22