/[xmltv_parser]/trunk/xmltv_parser/main.cs
ViewVC logotype

Contents of /trunk/xmltv_parser/main.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 50 - (show annotations) (download)
Fri Mar 8 06:15:44 2013 UTC (6 years, 7 months ago) by william
File size: 7225 byte(s)

1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Linq;
6 using System.Drawing;
7 using System.Text;
8 using System.Windows.Forms;
9 using libxmltv.Core;
10 using Enterprise.Logging;
11 using libxmltv.Interfaces;
12 using System.IO;
13 using System.Runtime.Serialization.Formatters.Binary;
14 using System.Threading;
15
16 namespace xmltv_parser
17 {
18
19 public partial class main : Form
20 {
21 bool form_closing = false;
22 //private IXMLTVRuntimeInstance xmltv;
23 private bool IsUnix
24 {
25 get { return System.Environment.OSVersion.Platform == PlatformID.Unix; }
26 }
27
28 public main()
29 {
30 InitializeComponent();
31 string log_path = Application.StartupPath;
32 string log_filename = string.Format("{0}.log", typeof(main).Assembly.GetName().Name);
33 gLog.CreateLog(string.Format(@"{0}\{1}", log_path, log_filename), false, LogLevel.kLogLevel_All, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush));
34 }
35
36 StringBuilder log_flusher = new StringBuilder();
37 bool HAVE_CR = false;
38 void Log_OnFlush(object sender, LoggerOnFlushEventArgs e)
39 {
40
41 log_flusher.Append(e.Buffer);
42 if (IsUnix)
43 {
44 if (e.Buffer == System.Environment.NewLine[0])
45 {
46 OnLogFlush(log_flusher.ToString());
47 log_flusher = new StringBuilder();
48 }
49 }
50 else
51 {
52 if (e.Buffer == System.Environment.NewLine[0])
53 {
54 HAVE_CR = true;
55 }
56 if (e.Buffer == System.Environment.NewLine[1] && HAVE_CR)
57 {
58 OnLogFlush(log_flusher.ToString().TrimStart(new char[] { System.Environment.NewLine[1] }));
59 log_flusher = new StringBuilder();
60 HAVE_CR = false;
61 }
62 }
63
64 }
65 //bool txtLog_EnterMouse = false;
66
67 private void txtLog_MouseLeave(object sender, EventArgs e)
68 {
69 //txtLog_EnterMouse = false;
70 }
71 private void txtLog_MouseEnter(object sender, EventArgs e)
72 {
73 //txtLog_EnterMouse = true;
74 }
75 void OnLogFlush(string logmessage)
76 {
77 if (this.IsDisposed) { return; }
78 Console.Write(logmessage);
79 txtStatus.Text = logmessage.Replace(System.Environment.NewLine, "");
80 txtLog.AppendText(logmessage);
81
82 //if (!txtLog_EnterMouse)
83 //{
84 txtLog.SelectionStart = txtLog.Text.Length; //Set the current caret position to the end
85 txtLog.ScrollToCaret(); //Now scroll it automatically
86 //}
87 //Thread.Sleep(100);
88 Application.DoEvents();
89 }
90
91 //List<IXMLTVChannel> Channels;
92 List<IXMLTVProgram> Programs;
93
94 private void main_Load(object sender, EventArgs e)
95 {
96
97 }
98 private void main_Shown(object sender, EventArgs e)
99 {
100
101 }
102
103
104 void LoadXMLTVShcedule(string schedule_xml)
105 {
106 XMLTV.CreateInstance(schedule_xml, new EventHandler<CancelEventArgs>(xmltv_cancelevent));
107 var program_count = XMLTV.GetInstance().Programs.Values.Count;
108 var program_list = XMLTV.GetInstance().Programs.Values.ToList().OrderBy(s => s.Start);
109 //Programs = program_list(0, program_count).ToList();
110 Programs = new List<IXMLTVProgram>(program_list.ToArray());
111 }
112
113 void xmltv_cancelevent(object sender, CancelEventArgs e)
114 {
115 if (form_closing)
116 {
117 e.Cancel = true;
118 }
119 }
120
121 void CreateControls()
122 {
123 foreach (var program in Programs)
124 {
125 ListViewItem li = new ListViewItem(string.Format("{0} {1}", program.Channel.Number, program.Channel.CallSign));
126 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Title));
127 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.SubTitle));
128 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Description));
129 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Start.ToString("yyyy/MM/dd hh:mm tt")));
130 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Stop.ToString("yyyy/MM/dd hh:mm tt")));
131 lstPrograms.Items.Add(li);
132 }
133 }
134
135 private void mnuItemOpenXMLTVFile_Click(object sender, EventArgs e)
136 {
137 //LoadXMLTVShcedule("20130307_continuum_schedule.xml");
138 var result = xmltv_file_chooser.ShowDialog();
139 if (result != DialogResult.OK) return;
140 ClearLocalLog();
141 LoadXMLTVShcedule(xmltv_file_chooser.FileName);
142 CreateControls();
143 }
144
145 private void mnuItemOpenSavedData_Click(object sender, EventArgs e)
146 {
147 var result = xmltv_program_data_loader.ShowDialog();
148 if (result != DialogResult.OK) return;
149 string filename = xmltv_program_data_loader.FileName;
150 IXMLTVRuntimeInstance xmltv = null;
151 bool status = false;
152 xmltv = XMLTV.DeSerialize(filename, out status);
153 if (!status)
154 {
155 MessageBox.Show("Failed to load data - check log", "Failed to load data", MessageBoxButtons.OK, MessageBoxIcon.Error);
156 return;
157 }
158 MessageBox.Show("Successfully loaded data", "Successfully loaded data", MessageBoxButtons.OK, MessageBoxIcon.Information);
159 CreateControls();
160 }
161
162 private void mnuItemSaveData_Click(object sender, EventArgs e)
163 {
164 var result = xmltv_program_data_saver.ShowDialog();
165 if (result != DialogResult.OK) return;
166 string filename = xmltv_program_data_saver.FileName;
167 if (!XMLTV.Serialize(filename))
168 {
169 MessageBox.Show("Failed to save data - check log", "Failed to save data", MessageBoxButtons.OK, MessageBoxIcon.Error);
170 return;
171 }
172 MessageBox.Show("Successfully saved data", "Successfully saved data", MessageBoxButtons.OK, MessageBoxIcon.Information);
173 }
174
175 private void main_FormClosing(object sender, FormClosingEventArgs e)
176 {
177 form_closing = true;
178 }
179
180 private void mnuItemClearLocalLog_Click(object sender, EventArgs e)
181 {
182 ClearLocalLog();
183 }
184 private void ClearLocalLog()
185 {
186 var log_top_entry = txtLog.Lines.FirstOrDefault();
187 txtLog.Clear();
188 if (!string.IsNullOrEmpty(log_top_entry))
189 {
190 txtLog.AppendText(log_top_entry);
191 }
192 }
193
194 }
195 }
196

  ViewVC Help
Powered by ViewVC 1.1.22