/[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 79 - (hide annotations) (download)
Sat Mar 9 11:40:15 2013 UTC (6 years, 9 months ago) by william
File size: 11824 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22