/[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 70 - (hide annotations) (download)
Sat Mar 9 01:42:46 2013 UTC (6 years, 6 months ago) by william
File size: 11337 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22