public override ValidationResult Validate(
            object value, System.Globalization.CultureInfo cultureInfo)
        {
            DataGridEntry x = (value as BindingGroup).Items[0] as DataGridEntry;

            if (!FilterRules.IsValidRange(x.PortFirst, x.PortLast))
            {
                return(new ValidationResult(false, strings.wr_val_err_portrange_inval));
            }
            if (!FilterRules.IsValidAddress(x.Address))
            {
                return(new ValidationResult(false, strings.wr_val_err_address_inval));
            }
            if (!FilterRules.IsValidDescription(x.Description))
            {
                return(new ValidationResult(false, strings.wr_val_err_description_inval));
            }
            string action = FilterRules.Actions.GetCode(x.Action);

            if (!FilterRules.IsValidAction(action, x.KeyId))
            {
                if (action.Equals("encrypt"))
                {
                    return(new ValidationResult(false, strings.wr_val_err_keyid_inval));
                }
                else
                {
                    return(new ValidationResult(false, strings.wr_val_err_keyid_should_be_empty));
                }
            }
            return(ValidationResult.ValidResult);
        }
        private async void btnApply_Click(object sender, RoutedEventArgs e)
        {
            FilterRules fl = new FilterRules();

            foreach (DataGridEntry dge in entries)
            {
                string action = FilterRules.Actions.GetCode(dge.Action);
                if (!fl.Add(dge.Description, dge.Executable, dge.Address,
                            dge.PortFirst, dge.PortLast, action, dge.KeyId))
                {
                    MessageBox.Show(strings.wr_grid_inval,
                                    "SELink", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }

            bool success = false;

            btnApply.IsEnabled = false;
            await Task.Run(() =>
            {
                fl.Save();
                bool busy = !app.ipclock.WaitOne(App.IpcTimeout);
                if (!busy)
                {
                    success = app.ipc.Reload();
                    app.ipclock.Release();
                }
            });

            if (!success)
            {
                SELinkTray.Instance.Error(strings.ipc_error);
            }
            else
            {
                SELinkTray.Instance.Info(strings.wr_apply_ok);
            }
            btnApply.IsEnabled = true;
        }
        private void Window_Initialized(object sender, EventArgs e)
        {
            app = ((App)Application.Current);
            // bind action names as the action cell choices
            Action.ItemsSource = FilterRules.Actions.Names;

            entries          = new ObservableCollection <DataGridEntry>();
            this.DataContext = this;

            // populate the context menu to show on right mouse click on the data grid
            GridMenu = new ContextMenu();
            {
                MenuItem m = new MenuItem();
                m.Header = strings.wr_insert_before;
                m.Click += new RoutedEventHandler(dataGridMenuInsertBefore_Click);
                GridMenu.Items.Add(m);
            }
            {
                MenuItem m = new MenuItem();
                m.Header = strings.wr_insert_after;
                m.Click += new RoutedEventHandler(dataGridMenuInsertAfter_Click);
                GridMenu.Items.Add(m);
            }
            {
                MenuItem m = new MenuItem();
                m.Header = strings.wr_delete;
                m.Click += new RoutedEventHandler(dataGridMenuDelete_Click);
                GridMenu.Items.Add(m);
            }
            dataGrid.ContextMenu = GridMenu;

            // read filter rules from the configuration file
            FilterRules fl = new FilterRules();

            fl.Read();

            // populate the data grid
            foreach (var rule in fl.conf.Rules)
            {
                DataGridEntry dge    = new DataGridEntry();
                string        action = NullToEmpty(rule.Action);
                dge.Action      = FilterRules.Actions.GetName(action);
                dge.Address     = NullToEmpty(rule.Address);
                dge.Description = NullToEmpty(rule.Description);
                dge.Executable  = NullToEmpty(rule.ExePath);
                dge.KeyId       = NullToEmpty(rule.KeyId);
                dge.PortFirst   = NullToEmpty(rule.PortFirst);
                dge.PortLast    = NullToEmpty(rule.PortLast);
                entries.Add(dge);
            }

            // restore form's last state
            this.Top    = Properties.Settings.Default.Location.Top;
            this.Left   = Properties.Settings.Default.Location.Left;
            this.Width  = Properties.Settings.Default.Location.Width;
            this.Height = Properties.Settings.Default.Location.Height;
            if (Properties.Settings.Default.State != WindowState.Minimized)
            {
                this.WindowState = Properties.Settings.Default.State;
            }

            // bind validation rules to the data grid
            FilterValidationRule validationRule = new FilterValidationRule();

            validationRule.ValidationStep = ValidationStep.UpdatedValue;
            dataGrid.RowValidationRules.Add(validationRule);

            dragHelper = new DataGridDragHelper(gridRoot, popupDrag, popupDragText, dataGrid);

            this.Icon = app.icon;
        }