Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive.Azure/HeuristicLab.Clients.Hive.CloudManager/3.3/Views/AddCertificate.cs @ 10355

Last change on this file since 10355 was 7661, checked in by spimming, 13 years ago

#1680: Show error dialog (from PluginInfrastructure) instead of message box on exception

File size: 2.8 KB
Line 
1using System;
2using System.ComponentModel;
3using System.Security.Cryptography.X509Certificates;
4using System.Windows.Forms;
5
6namespace HeuristicLab.Clients.Hive.CloudManager.Views {
7  public partial class AddCertificate : Form {
8    private BackgroundWorker worker = new BackgroundWorker();
9    public bool ErrorOccured { get; set; }
10    public string CertificateFile { get; set; }
11    public string CertificatePassword { get; set; }
12
13    public AddCertificate() {
14      InitializeComponent();
15      ErrorOccured = true;
16
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) {
34      CertificateFile = tbCertificateFile.Text;
35      CertificatePassword = tbCertificatePassword.Text;
36
37      if (CertificateFile.Length == 0) {
38        PluginInfrastructure.ErrorHandling.ShowErrorDialog(new Exception("Certificate file is required"));
39      } else if (CertificatePassword.Length == 0) {
40        PluginInfrastructure.ErrorHandling.ShowErrorDialog(new Exception("Password is required"));
41      } else {
42        SetControlsEnabled(false);
43        progressBar.Visible = true;
44        var parameters = Tuple.Create<string, string>(CertificateFile, CertificatePassword);
45        worker.RunWorkerAsync(parameters);
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) {
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);
62    }
63
64    private void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
65      progressBar.Visible = false;
66
67      if (e.Error == null) {
68        ErrorOccured = false;
69        this.Close();
70      } else {
71        PluginInfrastructure.ErrorHandling.ShowErrorDialog(e.Error);
72        CertificateFile = string.Empty;
73        CertificateFile = string.Empty;
74        ErrorOccured = true;
75        this.Show();
76        SetControlsEnabled(true);
77      }
78    }
79  }
80}
Note: See TracBrowser for help on using the repository browser.