private void AddBtn_Click(object sender, RoutedEventArgs e) { string vendor = ((Button)sender).Name.Substring(4, 2).ToUpper(); AccountConfig ac = new AccountConfig(); AccountOption accOpt = (dynamic)settings.GetType().GetProperty(vendor + "Account").GetValue(settings); ac.chkIsEnabled.IsEnabled = CheckIfActivateEnabled(accOpt); ac.ShowDialog(); if ((bool)ac.DialogResult) { ConnectionParam ao = new ConnectionParam(); ao.AccName = ac.AccName; ao.Host = ac.Host; ao.Port = ac.Port; ao.ClientId = ac.ClientId; ao.IsActivate = ac.IsActivate; ao.IsMulti = ac.IsMulti; ao.Password = ac.UnlockPassword; ao.PwdMD5 = ac.UnlockPwdMD5; accOpt.Accounts.Add(ao); if (accOpt.IsExclusive && ac.IsActivate) { foreach (ConnectionParam cp in accOpt.Accounts) { if (cp.AccName != ao.AccName) { cp.IsActivate = false; } } accOpt.Accounts = new ObservableCollection <ConnectionParam>(accOpt.Accounts); } } }
private void CheckBox_Checked(object sender, RoutedEventArgs e, AccountOption accOpt) { CheckBox chkbox = sender as CheckBox; if (chkbox != null && (bool)chkbox.IsChecked) { MessageBoxResult result = MessageBox.Show("If Exclusive is checked, only the first item with Activate True will be kept.\nAre you sure to continue?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result == MessageBoxResult.Yes) { bool firstFound = false; var cps = accOpt.Accounts.Where(x => x.ReadOnly); ConnectionParam cp = null; if (cps.Count() > 1) { MessageBox.Show("Exclusive cannot be checked since there are more than one using connections.", "Warning", MessageBoxButton.OK, MessageBoxImage.Exclamation); accOpt.IsExclusive = false; return; } else { cp = cps.ToList()[0]; } foreach (var item in accOpt.Accounts) { if (cp != null && item != cp) { item.IsActivate = false; } if (item.IsActivate && cp == null) { if (!firstFound) { firstFound = true; } else { item.IsActivate = false; } } } accOpt.Accounts = new ObservableCollection <ConnectionParam>(accOpt.Accounts); } else { accOpt.IsExclusive = false; } } }
// Remove attribute ReadOnly's value for storing the value private void RemoveRedundant() { foreach (string vendor in settings.Vendors) { AccountOption accOpt = (dynamic)settings.GetType().GetProperty(vendor + "Account").GetValue(settings); foreach (var item in accOpt.Accounts) { item.ReadOnly = false; } } }
private bool CheckIfActivateEnabled(AccountOption accOpt) { if (accOpt != null && accOpt.IsExclusive) { foreach (var item in accOpt.Accounts) { if (item.ReadOnly) { return(false); } } } return(true); }
// if Accounts are exclusive and one is being connected (ReadOnly), IsActivate checkbox should be disabled. private bool CheckIfActivateEnabled(string vendor) { AccountOption accOpt = (dynamic)settings.GetType().GetProperty(vendor + "Account").GetValue(settings); if (accOpt != null && accOpt.IsExclusive) { foreach (var item in accOpt.Accounts) { if (item.ReadOnly) { return(false); } } } return(true); }
public void ReadSettings() { UserPreference = JsonConvert.DeserializeObject <UserPreference>(Properties.Settings.Default["preference"].ToString()); List <IController> all_ctrls = new List <IController>(); if (UserPreference != null) { Type t = typeof(VM); string ns = t.Namespace; foreach (string vendor in UserPreference.Vendors) { AccountOption accOpt = (dynamic)UserPreference.GetType().GetProperty(vendor + "Account").GetValue(UserPreference); foreach (var acc in accOpt.Accounts) { if (acc.IsActivate) { string clsName = ns + "." + vendor + "Controller"; Type type = Type.GetType(clsName); IController ctrl = Activator.CreateInstance(type, this) as IController; ctrl.ConnParam = acc; // if some connection is connected, then remain unchanged IController ic = Controllers.FirstOrDefault(x => x.DisplayName == ctrl.DisplayName); if (ic != null) { ic.ConnParam = acc; all_ctrls.Add(ic); } else { all_ctrls.Add(ctrl); } } } // add new controllers foreach (var ctrl in all_ctrls) { IController ic = Controllers.FirstOrDefault(x => x.DisplayName == ctrl.DisplayName); if (ic == null) { Controllers.Add(ctrl); ctrl.Connected += Ctrl_Connected; ctrl.Disconnected += Ctrl_Disconnected; } } } } }
private void EditAccountConfig(string vendor) { ListView lv = (ListView)this.FindName("lv_" + vendor + "_acc"); if (lv.SelectedIndex != -1) { AccountConfig ac = new AccountConfig(); ac.txtName.IsReadOnly = true; ac.Owner = this; string prop = vendor.ToUpper() + "Account"; AccountOption accOpt = (dynamic)settings.GetType().GetProperty(prop).GetValue(settings); ac.chkIsEnabled.IsEnabled = CheckIfActivateEnabled(accOpt); ObservableCollection <ConnectionParam> aos = accOpt.Accounts; ConnectionParam ao = aos[lv.SelectedIndex]; ac.AccName = ao.AccName; ac.Host = ao.Host; ac.Port = ao.Port; ac.ClientId = ao.ClientId; ac.IsActivate = ao.IsActivate; ac.IsMulti = ao.IsMulti; ac.ShowDialog(); if ((bool)ac.DialogResult) { ao.AccName = ac.AccName; ao.Host = ac.Host; ao.Port = ac.Port; ao.ClientId = ac.ClientId; ao.IsActivate = ac.IsActivate; ao.IsMulti = ac.IsMulti; if (accOpt.IsExclusive && ac.IsActivate) { foreach (ConnectionParam cp in accOpt.Accounts) { if (cp.AccName != ao.AccName) { cp.IsActivate = false; } } } accOpt.Accounts = new ObservableCollection <ConnectionParam>(aos); } } }
private void DeleteAccountConfig(string vendor) { ListView lv = (ListView)this.FindName("lv_" + vendor + "_acc"); //ListItem li = lv.ItemContainerGenerator.ContainerFromIndex(lv.SelectedIndex); if (lv.SelectedIndex != -1) { string prop = vendor.ToUpper() + "Account"; AccountOption accOpt = (dynamic)settings.GetType().GetProperty(prop).GetValue(settings); ObservableCollection <ConnectionParam> aos = accOpt.Accounts; ConnectionParam ao = aos[lv.SelectedIndex]; MessageBoxResult result = MessageBox.Show("Are you sure to delete this configuration?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result == MessageBoxResult.Yes) { aos.RemoveAt(lv.SelectedIndex); } } }