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

  ViewVC Help
Powered by ViewVC 1.1.22