/[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 128 - (hide annotations) (download)
Wed Mar 13 13:45:05 2013 UTC (7 years, 2 months ago) by william
File size: 8275 byte(s)
+ current wip

1 william 71 //#define DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG // when defined will turn off kLogLevel_Debug messages, in release mode
2 william 69 //#define DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG // when defined will turn off kLogLevel_VerboseDebug message, in debug mode
3     using System;
4 william 2 using System.Collections.Generic;
5     using System.ComponentModel;
6     using System.Data;
7 william 29 using System.Linq;
8 william 2 using System.Drawing;
9     using System.Text;
10     using System.Windows.Forms;
11 william 11 using libxmltv.Core;
12 william 13 using Enterprise.Logging;
13 william 29 using libxmltv.Interfaces;
14 william 31 using System.IO;
15     using System.Runtime.Serialization.Formatters.Binary;
16 william 40 using System.Threading;
17 william 79 using System.Diagnostics;
18 william 110 using System.Xml.Linq;
19 william 2
20     namespace xmltv_parser
21     {
22 william 50
23 william 13 public partial class main : Form
24 william 2 {
25 william 13 public main()
26 william 2 {
27     InitializeComponent();
28 william 62 ListViewSorter Sorter = new ListViewSorter();
29 william 120 //lstPrograms.ListViewItemSorter = Sorter;
30 william 13 string log_path = Application.StartupPath;
31     string log_filename = string.Format("{0}.log", typeof(main).Assembly.GetName().Name);
32 william 59 gLog.CreateLog(string.Format(@"{0}\{1}", log_path, log_filename), false, LogLevel.kLogLevel_All_NoProgress, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush));
33 william 67 #if DEBUG
34     LogLevel gLevel = gLog.LogLevel;
35 william 69 #if DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG
36     gLevel &= ~LogLevel.kLogLevel_VerboseDebug;
37     #else
38     gLevel |= LogLevel.kLogLevel_VerboseDebug;
39     #endif
40     gLevel |= LogLevel.kLogLevel_Debug;
41 william 67 gLog.SetLogLevel(gLevel);
42     #else
43 william 68 LogLevel gLevel = LogLevel.kLogLevel_Default; // set the default log level: Info, Warn, Error, Debug
44     // it is OK for kLogLevel_Debug to be set in Release mode ... must of the chatty messages are from kLogLevel_VerboseDebug
45 william 69 #if DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG
46     gLevel &= ~LogLevel.kLogLevel_Debug;
47     #else
48     gLevel |= LogLevel.kLogLevel_Debug;
49     #endif
50 william 70 gLevel &= ~LogLevel.kLogLevel_VerboseDebug; // ensure this is not set, ever in release mode
51 william 67 gLog.SetLogLevel(gLevel);
52     #endif
53    
54    
55 william 59 gLog.ReportProgressEvent += new EventHandler<ReportProgressEventArgs>(gLog_ReportProgress);
56 william 2 }
57 william 127 #region main form events
58     private void mnuItemExit_Click(object sender, EventArgs e) { this.Close(); }
59     private void main_Load(object sender, EventArgs e) { }
60     private void main_Shown(object sender, EventArgs e) { }
61     private void mnuItemOpenXMLTVFile_Click(object sender, EventArgs e)
62     {
63     try
64     {
65     var result = xmltv_file_chooser.ShowDialog();
66     if (result != DialogResult.OK) return;
67     ClearLocalLog();
68     LoadXMLTVShcedule(xmltv_file_chooser.FileName);
69     }
70     catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
71     }
72     private void mnuItemOpenSavedData_Click(object sender, EventArgs e)
73     {
74     try
75     {
76     var result = xmltv_program_data_loader.ShowDialog();
77     if (result != DialogResult.OK) return;
78     string filename = xmltv_program_data_loader.FileName;
79     Thread worker = new Thread(new ParameterizedThreadStart(DeserializeDataFromFile)); worker.Start(filename);
80     }
81     catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
82     }
83     private void mnuItemSaveData_Click(object sender, EventArgs e)
84     {
85     try
86     {
87     var result = xmltv_program_data_saver.ShowDialog();
88     if (result != DialogResult.OK) return;
89     string filename = xmltv_program_data_saver.FileName;
90     Thread worker = new Thread(new ParameterizedThreadStart(DeserializeDataToFile)); worker.Start(filename);
91     }
92     catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
93     }
94     private void main_FormClosing(object sender, FormClosingEventArgs e) { try { XMLTV.Destroy(); } catch { } }
95     private void mnuItemClearLocalLog_Click(object sender, EventArgs e) { ClearLocalLog(); }
96     #endregion
97 william 11
98 william 59 private void ReportProgress(int progress)
99     {
100 william 84 if (this.InvokeRequired)
101 william 59 {
102 william 127 try { this.Invoke((Action)(delegate { ReportProgress(progress); })); }
103 william 84 catch { }
104     return;
105 william 108 }
106 william 114 if (progress_status != null && !this.IsDisposed)
107 william 108 progress_status.Value = progress;
108 william 59 }
109 william 127
110     #region gLog Events
111 william 59 private void gLog_ReportProgress(object sender, ReportProgressEventArgs e) { ReportProgress(e.Progress); }
112 william 127 #endregion
113     #region XMLT Events
114     void XMLTV_OnInstanceCreated(object sender, EventArgs e) { ReportProgress(0); CreateControls(); }
115     void LoadXMLTVShcedule(string schedule_xml) { XMLTV.Create(schedule_xml, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated)); }
116     #endregion
117 william 59
118 william 127
119     #region Logging Events
120 william 40 StringBuilder log_flusher = new StringBuilder();
121 william 127 void Log_OnFlush(object sender, LoggerOnFlushEventArgs e) { OnLogFlush(e.Buffer); }
122 william 40 void OnLogFlush(string logmessage)
123     {
124 william 47 if (this.IsDisposed) { return; }
125 william 54 UpdateStatus(logmessage);
126     UpdateLogOutput(logmessage);
127     Application.DoEvents();
128     }
129 william 127 private void ClearLocalLog()
130     {
131     var log_top_entry = txtLog.Lines.FirstOrDefault();
132     txtLog.Clear();
133     if (!string.IsNullOrEmpty(log_top_entry)) { txtLog.AppendText(log_top_entry); }
134     }
135 william 54 void UpdateStatus(string logmessage)
136 william 61 {
137 william 127 try
138 william 72 {
139 william 127 if (this.InvokeRequired)
140 william 72 {
141 william 127 try { this.Invoke((Action)(delegate { UpdateStatus(logmessage); })); }
142     catch { }
143     return;
144 william 72 }
145 william 127 txtStatus.Text = logmessage.Replace(System.Environment.NewLine, "");
146 william 72 }
147 william 127 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
148 william 54 }
149     void UpdateLogOutput(string logmessage)
150     {
151 william 127 try
152 william 54 {
153 william 127 if (this.InvokeRequired)
154 william 56 {
155 william 127 try { this.Invoke((Action)(delegate { UpdateLogOutput(logmessage); })); }
156     catch { }
157     return;
158 william 56 }
159 william 127 txtLog.AppendText(logmessage);
160     txtLog.SelectionStart = txtLog.Text.Length; //Set the current caret position to the end
161     txtLog.ScrollToCaret(); //Now scroll it automatically
162 william 54 }
163 william 127 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
164 william 40 }
165 william 127 #endregion
166 william 40
167 william 127 #region Binary Data Read/Write support
168 william 115 private void DeserializeDataFromFile(object filename)
169     {
170 william 118 if (filename == null) { throw new ArgumentNullException("filename", "cannot be null"); }
171     XMLTV.Load(filename.ToString(), new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
172 william 115 }
173 william 116 private void DeserializeDataToFile(object filename)
174     {
175     if (filename == null) { throw new ArgumentNullException("filename", "cannot be null"); }
176 william 118 XMLTV.Save(filename.ToString());
177 william 116 }
178 william 127 #endregion
179 william 44
180 william 127
181     #region t
182     void CreateControls()
183 william 44 {
184 william 54 try
185     {
186 william 127 if (this.InvokeRequired)
187     {
188     try { this.Invoke((Action)(delegate { CreateControls(); })); }
189     catch { }
190     return;
191     }
192 william 128 Type t;
193     object datasource = XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out t);
194     //XMLTV.CreateSorterFromDataSource(ref datasource, "title");
195 william 127 dataGrid.DataSource = datasource;
196 william 54 }
197 william 127 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
198 william 44 }
199 william 127 #endregion
200 william 47
201 william 2 }
202     }
203 william 29

  ViewVC Help
Powered by ViewVC 1.1.22