public ExportKeyCommand(CommandAttribute commandAttribute, ElementViewModel subject, IUIServiceWpf uiService)
     : base(commandAttribute, uiService)
 {
     this.uiService   = uiService;
     this.keyProperty = subject.Properties.OfType <ICryptographicKeyProperty>().FirstOrDefault();
 }
 public ExportKeyCommand(CommandAttribute commandAttribute, ElementViewModel subject, IUIServiceWpf uiService)
     :base(commandAttribute, uiService)
 {
     this.uiService = uiService;
     this.keyProperty = subject.Properties.OfType<ICryptographicKeyProperty>().FirstOrDefault();
 }
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            Debug.Assert(provider != null, "No service provider; we cannot edit the value");
            if (provider != null)
            {
                IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

                Debug.Assert(edSvc != null, "No editor service; we cannot edit the value");
                if (edSvc != null)
                {
                    IWindowsFormsEditorService service = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

                    ProtectedKeySettings keySettings = value as ProtectedKeySettings;
                    if (keySettings == null)
                    {
                        throw new ArgumentException(KeyManagerResources.KeyManagerEditorRequiresKeyInfoProperty);
                    }

                    var bindableProperty = EditorUtility.GetBindableProperty(context);
                    ICryptographicKeyProperty cryptographicKeyContainer = bindableProperty.Property as ICryptographicKeyProperty;
                    CryptographicKeyWizard    dialog = new CryptographicKeyWizard(CryptographicKeyWizardStep.CreateNewKey, cryptographicKeyContainer.KeyCreator);

                    try
                    {
                        var uiService = (IUIServiceWpf)provider.GetService(typeof(IUIServiceWpf));
                        KeyManagerUtilities.ImportProtectedKey(uiService, keySettings);
                        dialog.KeySettings = keySettings;
                    }
                    catch (FileNotFoundException)
                    {
                        //this is handled by the KeyManager utilites;
                        return(value);
                    }
                    catch (IOException ioe)
                    {
                        MessageBox.Show(String.Format(CultureInfo.CurrentCulture, KeyManagerResources.OpenExistingKeyFileFailureErrorMessage, keySettings.FileName, ioe.Message), KeyManagerResources.OpenExistingKeyFileFailureTitle, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                        return(value);
                    }
                    catch (Exception e)
                    {
                        IUIServiceWpf uiService = (IUIServiceWpf)provider.GetService(typeof(IUIServiceWpf));
                        string        unableToLoadKeyMessage      = string.Format(CultureInfo.CurrentCulture, KeyManagerResources.LoadExistingKeyFileFailureErrorMessage, keySettings.FileName, e.Message);
                        var           unableToLoadKeyDialogResult = uiService.ShowMessageWpf(unableToLoadKeyMessage, KeyManagerResources.OpenExistingKeyFileFailureTitle, System.Windows.MessageBoxButton.YesNo);
                        if (unableToLoadKeyDialogResult == System.Windows.MessageBoxResult.No)
                        {
                            return(value);
                        }
                    }

                    DialogResult editorDialogResult = service.ShowDialog(dialog);

                    if (editorDialogResult == DialogResult.Cancel)
                    {
                        return(keySettings);
                    }
                    else
                    {
                        return(dialog.KeySettings);
                    }
                }
            }
            return(value);
        }