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

  ViewVC Help
Powered by ViewVC 1.1.22