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

Diff of /trunk/xmltv_parser/main.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/xmltv_parser/main.cs	2013/03/13 15:16:59	131
+++ trunk/xmltv_parser/main.cs	2013/03/17 10:40:09	241
@@ -1,4 +1,4 @@
-//#define DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG // when defined will turn off kLogLevel_Debug messages, in release mode
+#define DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG // when defined will turn off kLogLevel_Debug messages, in release mode
 //#define DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG // when defined will turn off kLogLevel_VerboseDebug message, in debug mode
 //#define USE_DYNAMIC_SORTING_FILTERING_CONTROLS // when defined will setup dynamic sorting and filtering controls for XMLTV Data
 using System;
@@ -23,14 +23,45 @@
 
     public partial class main : Form
     {
+        const long MAX_LOG_FILESIZE_IN_BYTES = 250000; //250kb max
+        void TruncateLogFile(FileInfo log_file)
+        {
+            if (!log_file.Exists)
+            {
+                gLog.CreateLog(log_file.FullName, false, LogLevel.kLogLevel_All_NoProgress, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush));
+            }
+            else
+            {
+                if (log_file.Length > MAX_LOG_FILESIZE_IN_BYTES)
+                {
+                    string match = string.Format("{0}.*", log_file.Name);
+                    var files = Directory.GetFiles(log_file.Directory.FullName, match).ToList();
+                    files.RemoveAt(0);
+                    files.TrimExcess();
+                    int count = files.Count;
+                    if (log_file.Exists)
+                    {
+                        File.Copy(log_file.FullName, string.Format("{0}.{1}", log_file.FullName, count));
+                    }
+                    gLog.CreateLog(log_file.FullName, true, LogLevel.kLogLevel_All_NoProgress, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush));
+                }
+                else
+                {
+                    gLog.CreateLog(log_file.FullName, false, LogLevel.kLogLevel_All_NoProgress, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush));
+                }
+            }
+        }
+
         public main()
         {
             InitializeComponent();
             ListViewSorter Sorter = new ListViewSorter();
             //lstPrograms.ListViewItemSorter = Sorter;
+
             string log_path = Application.StartupPath;
             string log_filename = string.Format("{0}.log", typeof(main).Assembly.GetName().Name);
-            gLog.CreateLog(string.Format(@"{0}\{1}", log_path, log_filename), false, LogLevel.kLogLevel_All_NoProgress, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush));
+            FileInfo log_file = new FileInfo(string.Format(@"{0}\{1}", log_path, log_filename));
+            TruncateLogFile(log_file);            
 #if DEBUG
             LogLevel gLevel = gLog.LogLevel;
 #if DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG
@@ -179,7 +210,20 @@
         #endregion
 
 
-        #region t
+        #region CreateControls
+        void AutoResizeDataGridView()
+        {
+            //dataGrid.AutoResizeRow(2, DataGridViewAutoSizeRowMode.AllCellsExceptHeader);
+        
+            for (int i = 0; i < dataGrid.ColumnCount; i++)
+            {
+                dataGrid.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
+            }
+            //for (int i = 0; i < dataGrid.RowCount; i++)
+            //{
+            //    dataGrid.AutoResizeRow(i, DataGridViewAutoSizeRowMode.AllCellsExceptHeader);
+            //}
+        }
         void CreateControls()
         {
             try
@@ -192,22 +236,23 @@
                 }
                 Type data_type;
                 object datasource = XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out data_type);
-                CreatControlsFromType(data_type);
-                //XMLTV.CreateSorterFromDataSource(ref datasource, "title");
+                CreatControlsFromType(data_type);                
                 dataGrid.DataSource = datasource;
+                AutoResizeDataGridView();
                 flow_datagrid_sort.Enabled = true;
+                btnSort.PerformClick();
             }
             catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
         }
 
-        
+
         void CreatControlsFromType(Type data_type)
         {
 #if USE_DYNAMIC_SORTING_FILTERING_CONTROLS
               _CreatControlsFromType(data_type);
 #else
-            gLog.Warn.WriteLine("Dynamic creation of sorting and filtering controls has not been enabled.");
-            gLog.Warn.WriteLine("To enable: #define USE_DYNAMIC_SORTING_FILTERING_CONTROLS");
+            gLog.Verbose.Warn.WriteLine("Dynamic creation of sorting and filtering controls has not been enabled.");
+            gLog.Verbose.Warn.WriteLine("To enable: #define USE_DYNAMIC_SORTING_FILTERING_CONTROLS");
 #endif
         }
         [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
@@ -226,30 +271,123 @@
         void CreateDefaultControls(Type data_type)
         {
             // no default controls
-            gLog.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
+            gLog.Verbose.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
         }
         [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
         void CreateSourceDefinitionControls(Type data_type)
         {
             // no controls needed
-            gLog.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
+            gLog.Verbose.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
         }
         [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
         void CreateChannelDefinitionControls(Type data_type)
         {
-            gLog.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
+            gLog.Verbose.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
         }
         [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
         void CreateProgramDefinitionControls(Type data_type)
         {
-            gLog.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
+            gLog.Verbose.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
         }
-        #endregion     
+        #endregion
 
+        #region Program Entry Display        
+        private int CurrentRowIndex = -1;
+        void DisplaySelectedProgramEntry(DataGridViewRow row)
+        {
+            int RowIndex = row.Index; // this is the index in the list
+
+            //if (CurrentRowIndex == -1) { AlreadyDisplayedProgramEntryForIndex = false; CurrentRowIndex = RowIndex; }
+            //if (CurrentRowIndex == RowIndex && AlreadyDisplayedProgramEntryForIndex) { 
+            bool DoDisplay = false;
+            if (CurrentRowIndex == -1)
+            {
+                DoDisplay = true;
+            }
+            if (CurrentRowIndex == RowIndex)
+            {
+                DoDisplay = false;
+            }
+            else
+            {
+                DoDisplay = true;
+            }
 
-        #region Column Sorting Support
-        private bool Descending = false;
-        private void ToggleDescedning() { Descending = Descending ? false : true; }
+            if (DoDisplay)
+            {
+                 CurrentRowIndex = RowIndex;
+                List<IProgramDefinition> list = (dataGrid.DataSource as List<IProgramDefinition>);
+                if (list == null)
+                {
+                    gLog.Warn.WriteLine("Unable to display program entry for rowindex: {0}", RowIndex);
+                   
+                    return;
+                }
+                try
+                {
+                    IProgramDefinition program = list[RowIndex];
+                    ProgramEntryControl c = new ProgramEntryControl(program);
+                    c.Dock = DockStyle.Fill;
+                    split_bottom.Panel2.Controls.Clear();
+                    split_bottom.Panel2.Controls.Add(c);                    
+                }
+                catch (Exception ex)
+                {
+                    gLog.Error.WriteLine(ex.ToString());
+                }
+            }
+        }
+        #endregion
+        #region Datasource support / events
+        private void dataGrid_SelectionChanged(object sender, EventArgs e)
+        {
+            try
+            {
+                DataGridView dgv = (DataGridView)sender;
+                //var row = dgv.SelectedRows[0]; // the row that was selected
+                //var cell = dgv.SelectedCells[0];
+                DataGridViewRow row = null;
+                string data = string.Empty;
+                if (dgv.SelectedCells.Count > 0)
+                {
+                    var cell = dgv.SelectedCells[0];
+                    row = dgv.Rows[cell.RowIndex];
+                }
+                if (dgv.SelectedRows.Count > 0)
+                {
+                    row = dgv.SelectedRows[0];
+                }
+
+                if (row != null)
+                {
+                    RadioButton button = GetSelectedFilteringControl();
+                    int index = GetColumnIndexForRadioButton(button);
+                    data = row.Cells[index].Value.ToString();
+                    txtFilterText.Text = data;
+                    DisplaySelectedProgramEntry(row);
+                }
+            }
+            catch (Exception ex)
+            {
+                gLog.Verbose.Error.WriteLine(ex.ToString());
+            }
+        }
+        private int GetColumnIndexForRadioButton(RadioButton radio) { return GetColumnIndex(radio.Tag as string); }
+        private int GetColumnIndex(string columnname)
+        {
+            int index =0;
+            Type t = typeof(IProgramDefinition);
+
+            var props = t.GetProperties();
+            foreach (var prop in props)
+            {
+                if (prop.Name.ToLower() == columnname.ToLower()) { break; }
+                index++;
+            }
+            return index;
+        }
+        #endregion
+        #region Reset event
         private void btnResetSort_Click(object sender, EventArgs e)
         {
             Type data_type;
@@ -257,49 +395,135 @@
             dataGrid.DataSource = datasource;
             ToggleDescedning();
         }
-        private void btnSortChannelName_Click(object sender, EventArgs e)
+        #endregion
+        #region Filtering events
+        private void FilterByColumn(RadioButton button)
         {
-            object data = dataGrid.DataSource;
-            XMLTV.CreateSorterFromDataSource(ref data, Descending, "channelname");
+            Type data_type;
+            string column = (button.Tag as string);
+            object data = XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out data_type);
+            XMLTV.CreateFilterFromDataSource(ref data, column, txtFilterText.Text);
             dataGrid.DataSource = data;
-            ToggleDescedning();
         }
-
-        private void btnSortStart_Click(object sender, EventArgs e)
+        private RadioButton GetSelectedFilteringControl()
         {
-            object data = dataGrid.DataSource;
-            XMLTV.CreateSorterFromDataSource(ref data, Descending, "start");
-            dataGrid.DataSource = data;
-            ToggleDescedning();
+            bool control_selected = false;
+            RadioButton button = new RadioButton();
+            foreach (var c in grpFilter_flow.Controls)
+            {
+                button = (c as RadioButton);
+                if (button != null) { if (button.Checked) { control_selected = true; break; } }
+                else { continue; }
+            }
+            if (!control_selected)
+            {
+                gLog.Verbose.Warn.WriteLine("Unkown filter value used.  Was not: channelname, title, subtitle, description, start, or stop.");
+                //throw new InvalidOperationException("No filtering control is currently selected.");
+                return button;
+            }
+            else { return button; }
         }
-        private void btnSortStop_Click(object sender, EventArgs e)
+        private void UpdateFilterText(RadioButton radio, DataGridView dgv)
         {
-            object data = dataGrid.DataSource;
-            XMLTV.CreateSorterFromDataSource(ref data, Descending, "stop");
-            dataGrid.DataSource = data;
-            ToggleDescedning();
+            if (radio.Checked)
+            {
+                int cellindex = GetColumnIndexForRadioButton(radio);
+                string t = radio.Tag as string;
+                DataGridViewRow row = null;
+                string data = string.Empty;
+                if (dgv.SelectedCells.Count > 0)
+                {
+                    var cell = dgv.SelectedCells[0];
+                    row = dgv.Rows[cell.RowIndex];
+                }
+                if (dgv.SelectedRows.Count > 0)
+                {
+                    row = dgv.SelectedRows[0];
+                }
+                data = row.Cells[cellindex].Value.ToString();
+                txtFilterText.Text = data;
+            }
         }
-        private void btnSortTitle_Click(object sender, EventArgs e)
+        private void btnFilter_Click(object sender, EventArgs e)
         {
-            object data = dataGrid.DataSource;
-            XMLTV.CreateSorterFromDataSource(ref data, Descending, "title");
-            dataGrid.DataSource = data;
-            ToggleDescedning();
+            RadioButton button = GetSelectedFilteringControl();
+            FilterByColumn(button);
         }
-        private void btnSortSubTitle_Click(object sender, EventArgs e)
+        private void radio_filter_channelnumber_CheckedChanged(object sender, EventArgs e)
         {
-            object data = dataGrid.DataSource;
-            XMLTV.CreateSorterFromDataSource(ref data, Descending, "subtitle");
-            dataGrid.DataSource = data;
-            ToggleDescedning();
+            RadioButton radio = (sender as RadioButton);
+            UpdateFilterText(radio, dataGrid);
+        }
+        private void radio_filter_title_CheckedChanged(object sender, EventArgs e)
+        {
+            RadioButton radio = (sender as RadioButton);
+            UpdateFilterText(radio, dataGrid);
+        }
+        private void radio_filter_subtitle_CheckedChanged(object sender, EventArgs e)
+        {
+            RadioButton radio = (sender as RadioButton);
+            UpdateFilterText(radio, dataGrid);
+        }
+        private void radio_filter_description_CheckedChanged(object sender, EventArgs e)
+        {
+            RadioButton radio = (sender as RadioButton);
+            UpdateFilterText(radio, dataGrid);
+        }
+        private void radio_filter_channelname_CheckedChanged(object sender, EventArgs e)
+        {
+            RadioButton radio = (sender as RadioButton);
+            UpdateFilterText(radio, dataGrid);
+        }
+        private void radio_filter_start_CheckedChanged(object sender, EventArgs e)
+        {
+            RadioButton radio = (sender as RadioButton);
+            UpdateFilterText(radio, dataGrid);
+        }
+        private void radio_filter_stop_CheckedChanged(object sender, EventArgs e)
+        {
+            RadioButton radio = (sender as RadioButton);
+            UpdateFilterText(radio, dataGrid);
         }
-        private void btnSortDescription_Click(object sender, EventArgs e)
+        private void radio_filter_rating_CheckedChanged(object sender, EventArgs e)
         {
+            RadioButton radio = (sender as RadioButton);
+            UpdateFilterText(radio, dataGrid);
+        }
+        #endregion
+        #region Sorting events
+        private bool Descending = false;
+        private void ToggleDescedning() { Descending = Descending ? false : true; }
+        private void SortByColumn(RadioButton button)
+        {
+            string column = (button.Tag as string);
             object data = dataGrid.DataSource;
-            XMLTV.CreateSorterFromDataSource(ref data, Descending, "description");
+            XMLTV.CreateSorterFromDataSource(ref data, Descending, column);
             dataGrid.DataSource = data;
             ToggleDescedning();
         }
+        private RadioButton GetSelectedSortControl()
+        {
+            bool control_selected = false;
+            RadioButton button = new RadioButton();
+            foreach (var c in grpSort_flow.Controls)
+            {
+                button = (c as RadioButton);
+                if (button != null) { if (button.Checked) { control_selected = true; break; } }
+                else { continue; }
+            }
+            if (!control_selected)
+            {
+                gLog.Warn.WriteLine("Unkown sort value used.  Was not: channelname, title, subtitle, description, stop, or start.");
+                //throw new InvalidOperationException("No sorting control is currently selected.");
+                return button;
+            }
+            else { return button; }
+        }
+        private void btnSort_Click(object sender, EventArgs e)
+        {
+            RadioButton button = GetSelectedSortControl();
+            SortByColumn(button);
+        }
         #endregion
 
        

 

  ViewVC Help
Powered by ViewVC 1.1.22