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 |
public partial class main : Form |
19 |
{ |
20 |
bool form_closing = false; |
21 |
private IXMLTVRuntimeInstance xmltv; |
22 |
private bool IsUnix |
23 |
{ |
24 |
get { return System.Environment.OSVersion.Platform == PlatformID.Unix; } |
25 |
} |
26 |
|
27 |
public main() |
28 |
{ |
29 |
InitializeComponent(); |
30 |
string log_path = Application.StartupPath; |
31 |
string log_filename = string.Format("{0}.log", typeof(main).Assembly.GetName().Name); |
32 |
gLog.CreateLog(string.Format(@"{0}\{1}", log_path, log_filename), false, LogLevel.kLogLevel_All, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush)); |
33 |
} |
34 |
|
35 |
StringBuilder log_flusher = new StringBuilder(); |
36 |
bool HAVE_CR = false; |
37 |
void Log_OnFlush(object sender, LoggerOnFlushEventArgs e) |
38 |
{ |
39 |
|
40 |
log_flusher.Append(e.Buffer); |
41 |
if (IsUnix) |
42 |
{ |
43 |
if (e.Buffer == System.Environment.NewLine[0]) |
44 |
{ |
45 |
OnLogFlush(log_flusher.ToString()); |
46 |
log_flusher = new StringBuilder(); |
47 |
} |
48 |
} |
49 |
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 |
} |
64 |
//bool txtLog_EnterMouse = false; |
65 |
|
66 |
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 |
void OnLogFlush(string logmessage) |
75 |
{ |
76 |
if (this.IsDisposed) { return; } |
77 |
Console.Write(logmessage); |
78 |
txtStatus.Text = logmessage.Replace(System.Environment.NewLine, ""); |
79 |
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 |
} |
89 |
|
90 |
//List<IXMLTVChannel> Channels; |
91 |
List<IXMLTVProgram> Programs; |
92 |
|
93 |
private void main_Load(object sender, EventArgs e) |
94 |
{ |
95 |
|
96 |
} |
97 |
private void main_Shown(object sender, EventArgs e) |
98 |
{ |
99 |
//libxmltv.Core.XMLTV |
100 |
|
101 |
} |
102 |
|
103 |
|
104 |
void LoadXMLTVShcedule(string schedule_xml) |
105 |
{ |
106 |
XMLTV.CreateInstance(schedule_xml, new EventHandler<CancelEventArgs>(xmltv_cancelevent)); |
107 |
xmltv = XMLTV.GetInstance(); |
108 |
var program_count = xmltv.Programs.Values.Count; |
109 |
var program_list = xmltv.Programs.Values.ToList().OrderBy(s => s.Start); |
110 |
//Programs = program_list(0, program_count).ToList(); |
111 |
Programs = new List<IXMLTVProgram>(program_list.ToArray()); |
112 |
} |
113 |
|
114 |
void xmltv_cancelevent(object sender, CancelEventArgs e) |
115 |
{ |
116 |
if (form_closing) |
117 |
{ |
118 |
e.Cancel = true; |
119 |
} |
120 |
} |
121 |
|
122 |
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 |
|
136 |
private void mnuItemOpenXMLTVFile_Click(object sender, EventArgs e) |
137 |
{ |
138 |
ClearLocalLog(); |
139 |
//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 |
CreateControls(); |
144 |
} |
145 |
|
146 |
private void mnuItemOpenSavedData_Click(object sender, EventArgs e) |
147 |
{ |
148 |
var result = xmltv_program_data_loader.ShowDialog(); |
149 |
if (result != DialogResult.OK) return; |
150 |
string filename = xmltv_program_data_loader.FileName; |
151 |
if (!XMLTV.GetSerializer().DeSerialize(filename, out xmltv)) |
152 |
{ |
153 |
MessageBox.Show("Failed to load data - check log", "Failed to load data", MessageBoxButtons.OK, MessageBoxIcon.Error); |
154 |
return; |
155 |
} |
156 |
MessageBox.Show("Successfully loaded data", "Successfully loaded data", MessageBoxButtons.OK, MessageBoxIcon.Information); |
157 |
CreateControls(); |
158 |
} |
159 |
|
160 |
private void mnuItemSaveData_Click(object sender, EventArgs e) |
161 |
{ |
162 |
var result = xmltv_program_data_saver.ShowDialog(); |
163 |
if (result != DialogResult.OK) return; |
164 |
string filename = xmltv_program_data_saver.FileName; |
165 |
if (!XMLTV.GetSerializer().Serialize(filename)) |
166 |
{ |
167 |
MessageBox.Show("Failed to save data - check log", "Failed to save data", MessageBoxButtons.OK, MessageBoxIcon.Error); |
168 |
return; |
169 |
} |
170 |
MessageBox.Show("Successfully saved data", "Successfully saved data", MessageBoxButtons.OK, MessageBoxIcon.Information); |
171 |
} |
172 |
|
173 |
private void main_FormClosing(object sender, FormClosingEventArgs e) |
174 |
{ |
175 |
form_closing = true; |
176 |
} |
177 |
|
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 |
} |
193 |
} |
194 |
|