[7339] | 1 | using System;
|
---|
| 2 | using System.ComponentModel;
|
---|
[7362] | 3 | using System.Security.Cryptography.X509Certificates;
|
---|
[7339] | 4 | using System.Windows.Forms;
|
---|
| 5 |
|
---|
| 6 | namespace HeuristicLab.Clients.Hive.CloudManager.Views {
|
---|
| 7 | public partial class AddCertificate : Form {
|
---|
| 8 | private BackgroundWorker worker = new BackgroundWorker();
|
---|
| 9 | public bool ErrorOccured { get; set; }
|
---|
[7362] | 10 | public string CertificateFile { get; set; }
|
---|
| 11 | public string CertificatePassword { get; set; }
|
---|
[7339] | 12 |
|
---|
| 13 | public AddCertificate() {
|
---|
| 14 | InitializeComponent();
|
---|
[7362] | 15 | ErrorOccured = true;
|
---|
| 16 |
|
---|
[7339] | 17 | worker.DoWork += new DoWorkEventHandler(AddCertificateTask);
|
---|
| 18 | worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkerCompleted);
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | private void btnBrowseCertificateFile_Click(object sender, EventArgs e) {
|
---|
| 22 | OpenFileDialog ofd = new OpenFileDialog();
|
---|
| 23 | ofd.Filter = "All Certificate Files|*.pfx";
|
---|
| 24 | if (ofd.ShowDialog() == DialogResult.OK) {
|
---|
| 25 | tbCertificateFile.Text = ofd.FileName;
|
---|
| 26 | }
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | private void btnCancel_Click(object sender, EventArgs e) {
|
---|
| 30 | this.Close();
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | private void btnOk_Click(object sender, EventArgs e) {
|
---|
[7362] | 34 | CertificateFile = tbCertificateFile.Text;
|
---|
| 35 | CertificatePassword = tbCertificatePassword.Text;
|
---|
| 36 |
|
---|
| 37 | if (CertificateFile.Length == 0) {
|
---|
[7661] | 38 | PluginInfrastructure.ErrorHandling.ShowErrorDialog(new Exception("Certificate file is required"));
|
---|
[7362] | 39 | } else if (CertificatePassword.Length == 0) {
|
---|
[7661] | 40 | PluginInfrastructure.ErrorHandling.ShowErrorDialog(new Exception("Password is required"));
|
---|
[7339] | 41 | } else {
|
---|
| 42 | SetControlsEnabled(false);
|
---|
| 43 | progressBar.Visible = true;
|
---|
[7362] | 44 | var parameters = Tuple.Create<string, string>(CertificateFile, CertificatePassword);
|
---|
| 45 | worker.RunWorkerAsync(parameters);
|
---|
[7339] | 46 | }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | private void SetControlsEnabled(bool isEnabled) {
|
---|
| 50 | tbCertificatePassword.Enabled = isEnabled;
|
---|
| 51 | btnBrowseCertificateFile.Enabled = isEnabled;
|
---|
| 52 | btnCancel.Enabled = isEnabled;
|
---|
| 53 | btnOk.Enabled = isEnabled;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | private void AddCertificateTask(object sender, DoWorkEventArgs e) {
|
---|
[7362] | 57 | Tuple<string, string> parameters = e.Argument as Tuple<string, string>;
|
---|
| 58 | string file = parameters.Item1;
|
---|
| 59 | string pw = parameters.Item2;
|
---|
| 60 |
|
---|
| 61 | X509Certificate cert = new X509Certificate(file, pw);
|
---|
[7339] | 62 | }
|
---|
| 63 |
|
---|
| 64 | private void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
|
---|
| 65 | progressBar.Visible = false;
|
---|
[7362] | 66 |
|
---|
| 67 | if (e.Error == null) {
|
---|
| 68 | ErrorOccured = false;
|
---|
[7339] | 69 | this.Close();
|
---|
| 70 | } else {
|
---|
[7661] | 71 | PluginInfrastructure.ErrorHandling.ShowErrorDialog(e.Error);
|
---|
[7362] | 72 | CertificateFile = string.Empty;
|
---|
| 73 | CertificateFile = string.Empty;
|
---|
| 74 | ErrorOccured = true;
|
---|
[7339] | 75 | this.Show();
|
---|
| 76 | SetControlsEnabled(true);
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | }
|
---|