private void list_SelectedIndexChanged(object sender, EventArgs e)
 {
     selected = list.SelectedItem as DBFormatting;
     if (selected == null)
     {
         enableControls(false);
         return;
     }
     this.checkEnabled.Checked = selected[DBFormatting.cEnabled];
     this.textReplace.Text = selected[DBFormatting.cReplace];
     this.txtWith.Text = selected[DBFormatting.cWith];
     enableControls(true);
 }
 bool isValid(DBFormatting rule)
 {
     return(rule[DBFormatting.cReplace].ToString().Trim() != string.Empty);
 }
        DBFormatting fromInput()
        {
            DBFormatting item = new DBFormatting();
            item[DBFormatting.cEnabled] = this.checkEnabled.Checked;
            item[DBFormatting.cReplace] = this.textReplace.Text;
            item[DBFormatting.cWith] = this.txtWith.Text;

            return item;
        }
 public void LoadFromDB()
 {
     selected = null;
     addToList(DBFormatting.GetAll());
     enableControls(false);
 }
        private void lnkImport_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.IO.StreamReader w = null;
            try
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    w = new System.IO.StreamReader(openFileDialog1.FileName);
                    List<DBFormatting> imports = new List<DBFormatting>();
                    string line;
                    string[] splits = new string[] { "<Enabled>", "<Format>", "<FormatAs>" };
                    while((line = w.ReadLine()) != null)
                    {
                        string[] properties = line.Split(splits, StringSplitOptions.RemoveEmptyEntries);
                        if (properties.Length == 3)
                        {
                            DBFormatting dbf = new DBFormatting();
                            dbf[DBFormatting.cEnabled] = properties[0];
                            dbf[DBFormatting.cReplace] = properties[1];
                            dbf[DBFormatting.cWith] = properties[2];
                            imports.Add(dbf);
                            // now for add each one to the list
                            saveToDBAndReload(dbf, true);
                        }
                        else
                        {
                            MessageBox.Show("Unable to Import: " + line);
                        }
                    }

                    MPTVSeriesLog.Write(imports.Count.ToString() + " Formatting Rules Imported",MPTVSeriesLog.LogLevel.Normal);
                }
            }
            catch (Exception ex)
            {
                MPTVSeriesLog.Write("Error in trying to Export User Formatting Rules: " + ex.Message);
            }
            finally
            {
                if (w != null) w.Close();
            }
        }
 private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
 {
     selected = null;
     enableControls(false);
     enableControls(true);
 }
 bool isValid(DBFormatting rule)
 {
     return rule[DBFormatting.cReplace].ToString().Trim() != string.Empty;
 }
        bool saveToDBAndReload(DBFormatting rule, bool newItem)
        {
            if (isValid(rule))
            {
                if (newItem)
                {
                    DBFormatting.ClearAll();
                    for (int i = 0; i < list.Items.Count; i++)
                    {
                        DBFormatting s = list.Items[i] as DBFormatting;
                        if (s != null)
                        {
                            s[DBFormatting.cIndex] = i;
                            DBFormatting re = new DBFormatting(i);
                            re[DBFormatting.cEnabled] = s[DBFormatting.cEnabled];
                            re[DBFormatting.cReplace] = s[DBFormatting.cReplace];
                            re[DBFormatting.cWith] = s[DBFormatting.cWith];
                            re.Commit();
                        }
                    }
                    rule[DBFormatting.cIndex] = list.Items.Count;
                }

                rule.Commit();
                LoadFromDB();
                return true;
            }
            else return false;
        }
 private void saveChanges()
 {
     if(!isValid(fromInput()))
     {
         MessageBox.Show("You need to create at least one Formatting Rule first!");
         return;
     }
     if (selected == null)
     {
         // new entry
         saveToDBAndReload(fromInput(), true);
         return;
     }
     // existing entry changed
     DBFormatting changed = fromInput();
     
     changed[DBFormatting.cIndex] = selected[DBFormatting.cIndex];
     saveToDBAndReload(changed, false);
     selected = null;
 }