/[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 72 - (hide annotations) (download)
Sat Mar 9 09:29:40 2013 UTC (6 years, 9 months ago) by william
File size: 11654 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 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 72 if (this.InvokeRequired)
98     {
99     try
100     {
101     this.Invoke((Action)(delegate { UpdateStatus(logmessage); }));
102     }
103     catch { }
104     return;
105     }
106 william 42 txtStatus.Text = logmessage.Replace(System.Environment.NewLine, "");
107 william 54 }
108     void UpdateLogOutput(string logmessage)
109     {
110 william 72 if (this.InvokeRequired)
111 william 54 {
112 william 56 try
113     {
114 william 72 this.Invoke((Action)(delegate { UpdateLogOutput(logmessage); }));
115 william 56 }
116     catch { }
117 william 54 return;
118     }
119 william 47 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 william 40 }
123    
124 william 29 //List<IXMLTVChannel> Channels;
125     List<IXMLTVProgram> Programs;
126    
127 william 16 private void main_Load(object sender, EventArgs e)
128 william 11 {
129    
130     }
131 william 16 private void main_Shown(object sender, EventArgs e)
132 william 30 {
133 william 52
134 william 29 }
135    
136    
137     void LoadXMLTVShcedule(string schedule_xml)
138 william 11 {
139 william 54 //XMLTV.CreateInstance(schedule_xml, new EventHandler<CancelEventArgs>(xmltv_cancelevent));
140 william 72 XMLTV.CreateInstance(schedule_xml, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
141     //XMLTV.OnInstanceCreated += new EventHandler<EventArgs>(XMLTV_OnInstanceCreated);
142 william 11 }
143 william 29
144 william 54 void XMLTV_OnInstanceCreated(object sender, EventArgs e)
145 william 46 {
146 william 59 ReportProgress(0);
147 william 54 var instance = XMLTV.GetInstance();
148     if (instance != null)
149 william 46 {
150 william 54 var program_count = instance.Programs.Values.Count;
151     var program_list = instance.Programs.Values.ToList().OrderBy(s => s.Start);
152     //Programs = program_list(0, program_count).ToList();
153     Programs = new List<IXMLTVProgram>(program_list.ToArray());
154 william 46 }
155 william 54 CreateControls();
156 william 46 }
157    
158 william 54
159     //void xmltv_cancelevent(object sender, CancelEventArgs e)
160     //{
161     // if (form_closing)
162     // {
163     // e.Cancel = true;
164     // }
165     //}
166    
167 william 29 void CreateControls()
168     {
169 william 54 if (this.InvokeRequired)
170     {
171     this.Invoke((Action)(delegate { CreateControls(); }));
172     return;
173     }
174 william 60 List<ListViewItem> items = new List<ListViewItem>();
175 william 29 foreach (var program in Programs)
176     {
177     ListViewItem li = new ListViewItem(string.Format("{0} {1}", program.Channel.Number, program.Channel.CallSign));
178 william 60 li.Tag = program;
179 william 29 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 william 60 //lstPrograms.Items.Add(li);
185     items.Add(li);
186 william 29 }
187 william 60
188     lstPrograms.Items.AddRange(items.ToArray());
189 william 29 }
190 william 30
191     private void mnuItemOpenXMLTVFile_Click(object sender, EventArgs e)
192 william 52 {
193     try
194     {
195 william 59 lstPrograms.Items.Clear();
196 william 52 //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 william 54 //CreateControls();
202 william 52 }
203     catch (Exception ex)
204     {
205 william 55 gLog.Error.WriteLine(ex.ToString());
206 william 52 }
207 william 30 }
208    
209     private void mnuItemOpenSavedData_Click(object sender, EventArgs e)
210     {
211 william 52 try
212 william 31 {
213 william 52 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 william 56 XMLTV.CreateFromInstance(xmltv, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
220 william 52 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 william 31 }
227 william 52 catch (Exception ex)
228     {
229 william 55 gLog.Error.WriteLine(ex.ToString());
230 william 52 }
231 william 30 }
232    
233     private void mnuItemSaveData_Click(object sender, EventArgs e)
234     {
235 william 52 try
236 william 31 {
237 william 52 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 william 31 }
247 william 52 catch (Exception ex)
248     {
249 william 55 gLog.Error.WriteLine(ex.ToString());
250 william 52 }
251 william 30 }
252 william 44
253     private void main_FormClosing(object sender, FormClosingEventArgs e)
254     {
255 william 54 try
256     {
257     XMLTV.DestroyInstance();
258     }
259     catch { }
260 william 44 }
261 william 47
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 william 56 private void mnuItemExit_Click(object sender, EventArgs e)
277     {
278     this.Close();
279     }
280    
281 william 60 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 william 61
292 william 60 }
293    
294 william 61 private void lstPrograms_ColumnClick(object sender, ColumnClickEventArgs e)
295     {
296 william 62 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 william 61 lstPrograms.Sort();
307     }
308 william 2 }
309     }
310 william 29

  ViewVC Help
Powered by ViewVC 1.1.22