/[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 68 - (hide annotations) (download)
Sat Mar 9 01:28:31 2013 UTC (6 years, 5 months ago) by william
File size: 10837 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22