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