/[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 100 - (show annotations) (download)
Sun Mar 10 10:34:07 2013 UTC (7 years, 4 months ago) by william
File size: 13285 byte(s)

1 //#define DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG // when defined will turn off kLogLevel_Debug messages, in release mode
2 //#define DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG // when defined will turn off kLogLevel_VerboseDebug message, in debug mode
3 using System;
4 using System.Collections.Generic;
5 using System.ComponentModel;
6 using System.Data;
7 using System.Linq;
8 using System.Drawing;
9 using System.Text;
10 using System.Windows.Forms;
11 using libxmltv.Core;
12 using Enterprise.Logging;
13 using libxmltv.Interfaces;
14 using System.IO;
15 using System.Runtime.Serialization.Formatters.Binary;
16 using System.Threading;
17 using System.Diagnostics;
18
19 namespace xmltv_parser
20 {
21
22 public partial class main : Form
23 {
24 public main()
25 {
26 InitializeComponent();
27 ListViewSorter Sorter = new ListViewSorter();
28 lstPrograms.ListViewItemSorter = Sorter;
29 string log_path = Application.StartupPath;
30 string log_filename = string.Format("{0}.log", typeof(main).Assembly.GetName().Name);
31 gLog.CreateLog(string.Format(@"{0}\{1}", log_path, log_filename), false, LogLevel.kLogLevel_All_NoProgress, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush));
32 #if DEBUG
33 LogLevel gLevel = gLog.LogLevel;
34 #if DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG
35 gLevel &= ~LogLevel.kLogLevel_VerboseDebug;
36 #else
37 gLevel |= LogLevel.kLogLevel_VerboseDebug;
38 #endif
39 gLevel |= LogLevel.kLogLevel_Debug;
40 gLog.SetLogLevel(gLevel);
41 #else
42 LogLevel gLevel = LogLevel.kLogLevel_Default; // set the default log level: Info, Warn, Error, Debug
43 // it is OK for kLogLevel_Debug to be set in Release mode ... must of the chatty messages are from kLogLevel_VerboseDebug
44 #if DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG
45 gLevel &= ~LogLevel.kLogLevel_Debug;
46 #else
47 gLevel |= LogLevel.kLogLevel_Debug;
48 #endif
49 gLevel &= ~LogLevel.kLogLevel_VerboseDebug; // ensure this is not set, ever in release mode
50 gLog.SetLogLevel(gLevel);
51 #endif
52
53
54 gLog.ReportProgressEvent += new EventHandler<ReportProgressEventArgs>(gLog_ReportProgress);
55 }
56
57 private void ReportProgress(int progress)
58 {
59 if (this.InvokeRequired)
60 {
61 try
62 {
63 this.Invoke((Action)(delegate { ReportProgress(progress); }));
64 }
65 catch { }
66 return;
67 }
68 progress_status.Value = progress;
69 }
70 private void gLog_ReportProgress(object sender, ReportProgressEventArgs e) { ReportProgress(e.Progress); }
71
72 StringBuilder log_flusher = new StringBuilder();
73
74 void Log_OnFlush(object sender, LoggerOnFlushEventArgs e)
75 {
76 OnLogFlush(e.Buffer);
77 }
78 //bool txtLog_EnterMouse = false;
79
80 private void txtLog_MouseLeave(object sender, EventArgs e)
81 {
82 //txtLog_EnterMouse = false;
83 }
84 private void txtLog_MouseEnter(object sender, EventArgs e)
85 {
86 //txtLog_EnterMouse = true;
87 }
88 void OnLogFlush(string logmessage)
89 {
90 if (this.IsDisposed) { return; }
91 UpdateStatus(logmessage);
92 UpdateLogOutput(logmessage);
93 Application.DoEvents();
94 }
95
96 void UpdateStatus(string logmessage)
97 {
98 if (this.InvokeRequired)
99 {
100 try
101 {
102 this.Invoke((Action)(delegate { UpdateStatus(logmessage); }));
103 }
104 catch { }
105 return;
106 }
107 txtStatus.Text = logmessage.Replace(System.Environment.NewLine, "");
108 }
109 void UpdateLogOutput(string logmessage)
110 {
111 if (this.InvokeRequired)
112 {
113 try
114 {
115 this.Invoke((Action)(delegate { UpdateLogOutput(logmessage); }));
116 }
117 catch { }
118 return;
119 }
120 txtLog.AppendText(logmessage);
121 txtLog.SelectionStart = txtLog.Text.Length; //Set the current caret position to the end
122 txtLog.ScrollToCaret(); //Now scroll it automatically
123 }
124
125 //List<IXMLTVChannel> Channels;
126 List<IXMLTVProgram> Programs;
127
128 private void main_Load(object sender, EventArgs e)
129 {
130
131 }
132 private void main_Shown(object sender, EventArgs e)
133 {
134
135 }
136
137
138 void LoadXMLTVShcedule(string schedule_xml)
139 {
140 XMLTV.CreateInstance(schedule_xml, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
141 }
142
143 void XMLTV_OnInstanceCreated(object sender, EventArgs e)
144 {
145 ReportProgress(0);
146 var instance = XMLTV.GetInstance();
147 if (instance != null)
148 {
149 var program_count = instance.Programs.Count;
150 var program_list = instance.Programs.ToList().OrderBy(s => s.MetaData[XMLTVConstants.Programs.ProgramStart].ToString());
151 //Programs = program_list(0, program_count).ToList();
152 Programs = new List<IXMLTVProgram>(program_list.ToArray());
153 }
154 CreateControls();
155 }
156
157
158 //void xmltv_cancelevent(object sender, CancelEventArgs e)
159 //{
160 // if (form_closing)
161 // {
162 // e.Cancel = true;
163 // }
164 //}
165
166 void CreateControls()
167 {
168 if (this.InvokeRequired)
169 {
170 this.Invoke((Action)(delegate { CreateControls(); }));
171 return;
172 }
173 List<ListViewItem> items = new List<ListViewItem>();
174 foreach (var program in Programs)
175 {
176 IXMLTVChannel Channel = null;
177 var instance = XMLTV.GetInstance();
178 var id = program.MetaData[XMLTVConstants.Programs.ProgramChannelId].ToString();
179 Channel = instance.Channels.Find(m => m.Id == id);
180 //ListViewItem li = new ListViewItem(string.Format("{0} {1}", Channel.Number, Channel.CallSign));
181 if (Channel.MetaData.Count() == 0) { throw new ArgumentOutOfRangeException(string.Format("No metadata available for channel id: {0}", Channel.Id)); }
182 var channel_names = Channel.MetaData[XMLTVConstants.Channels.ChannelDisplayName];
183 string channel_name = string.Empty;
184 if (channel_names.Count() == 0) { throw new ArgumentOutOfRangeException(string.Format("No properties named '{0}' found for channel id: {1}", XMLTVConstants.Channels.ChannelDisplayName, Channel.Id)); }
185 else
186 {
187 var channame = channel_names.FirstOrDefault();
188 if (channame == null) { throw new NullReferenceException(string.Format("Unable to get channel display name for channel id: {0}", Channel.Id)); }
189 else
190 {
191 if (channame.Value == null) { throw new NullReferenceException(string.Format("Unable to get channel display name for channel id: {0}", Channel.Id)); }
192 else { channel_name = channame.Value.ToString(); }
193 }
194 }
195 ListViewItem li = new ListViewItem(string.Format("{0}", channel_name));
196 li.Tag = program;
197 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.MetaData[XMLTVConstants.Programs.ProgramTitle].ToString()));
198 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.MetaData[XMLTVConstants.Programs.ProgramSubTitle].ToString()));
199 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.MetaData[XMLTVConstants.Programs.ProgramDescription].ToString()));
200 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, ((DateTime)program.MetaData[XMLTVConstants.Programs.ProgramStart]).ToString("yyyy/MM/dd hh:mm tt")));
201 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, ((DateTime)program.MetaData[XMLTVConstants.Programs.ProgramStop]).ToString("yyyy/MM/dd hh:mm tt")));
202 //lstPrograms.Items.Add(li);
203 items.Add(li);
204 }
205
206 lstPrograms.Items.AddRange(items.ToArray());
207 }
208
209 private void mnuItemOpenXMLTVFile_Click(object sender, EventArgs e)
210 {
211 try
212 {
213 lstPrograms.Items.Clear();
214 //LoadXMLTVShcedule("20130307_continuum_schedule.xml");
215 var result = xmltv_file_chooser.ShowDialog();
216 if (result != DialogResult.OK) return;
217 ClearLocalLog();
218 LoadXMLTVShcedule(xmltv_file_chooser.FileName);
219 //CreateControls();
220 }
221 catch (Exception ex)
222 {
223 gLog.Error.WriteLine(ex.ToString());
224 }
225 }
226
227 private void mnuItemOpenSavedData_Click(object sender, EventArgs e)
228 {
229 try
230 {
231 var result = xmltv_program_data_loader.ShowDialog();
232 if (result != DialogResult.OK) return;
233 string filename = xmltv_program_data_loader.FileName;
234 IXMLTVRuntimeInstance xmltv = null;
235 bool status = false;
236 xmltv = XMLTV.DeSerialize(filename, out status);
237 XMLTV.CreateFromInstance(xmltv, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
238 if (!status)
239 {
240 MessageBox.Show("Failed to load data - check log", "Failed to load data", MessageBoxButtons.OK, MessageBoxIcon.Error);
241 return;
242 }
243 MessageBox.Show("Successfully loaded data", "Successfully loaded data", MessageBoxButtons.OK, MessageBoxIcon.Information);
244 }
245 catch (Exception ex)
246 {
247 gLog.Error.WriteLine(ex.ToString());
248 }
249 }
250
251 private void mnuItemSaveData_Click(object sender, EventArgs e)
252 {
253 try
254 {
255 var result = xmltv_program_data_saver.ShowDialog();
256 if (result != DialogResult.OK) return;
257 string filename = xmltv_program_data_saver.FileName;
258 if (!XMLTV.Serialize(filename))
259 {
260 MessageBox.Show("Failed to save data - check log", "Failed to save data", MessageBoxButtons.OK, MessageBoxIcon.Error);
261 return;
262 }
263 MessageBox.Show("Successfully saved data", "Successfully saved data", MessageBoxButtons.OK, MessageBoxIcon.Information);
264 }
265 catch (Exception ex)
266 {
267 gLog.Error.WriteLine(ex.ToString());
268 }
269 }
270
271 private void main_FormClosing(object sender, FormClosingEventArgs e)
272 {
273 try
274 {
275 XMLTV.DestroyInstance();
276 }
277 catch { }
278 }
279
280 private void mnuItemClearLocalLog_Click(object sender, EventArgs e)
281 {
282 ClearLocalLog();
283 }
284 private void ClearLocalLog()
285 {
286 var log_top_entry = txtLog.Lines.FirstOrDefault();
287 txtLog.Clear();
288 if (!string.IsNullOrEmpty(log_top_entry))
289 {
290 txtLog.AppendText(log_top_entry);
291 }
292 }
293
294 private void mnuItemExit_Click(object sender, EventArgs e)
295 {
296 this.Close();
297 }
298
299 private void lstPrograms_SelectedIndexChanged(object sender, EventArgs e)
300 {
301 IXMLTVProgram program = null;
302 var item = lstPrograms.SelectedItems[0];
303 int selected_index = lstPrograms.Items.IndexOf(item);
304 program = (item.Tag as IXMLTVProgram);
305 gLog.Debug.WriteLine("Selected ListViewItem at index: {0}", selected_index);
306 if (program == null) { return; }
307 txtStatus.Text = program.ToString();
308 //gLog.Debug.WriteLine(program.ToString());
309
310 }
311
312 private void lstPrograms_ColumnClick(object sender, ColumnClickEventArgs e)
313 {
314 if (!(lstPrograms.ListViewItemSorter is ListViewSorter)) return;
315 ListViewSorter Sorter = (ListViewSorter)lstPrograms.ListViewItemSorter;
316 Sorter.LastColumn = Sorter.CurrentColumn;
317 Sorter.CurrentColumn = e.Column;
318 if (Sorter.LastColumn == e.Column)
319 {
320 if (lstPrograms.Sorting == SortOrder.Ascending) { lstPrograms.Sorting = SortOrder.Descending; }
321 else { lstPrograms.Sorting = SortOrder.Ascending; }
322 }
323 else { lstPrograms.Sorting = SortOrder.Descending; }
324 lstPrograms.Sort();
325 }
326 }
327 }
328

  ViewVC Help
Powered by ViewVC 1.1.22