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

  ViewVC Help
Powered by ViewVC 1.1.22