Rev Author Line No. Line
2 clempaul 1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Linq;
5 using System.Windows.Forms;
6 using clempaul;
7  
8 namespace DNS_Manager
9 {
10     public partial class Settings : Form
11     {
12         public Settings()
13         {
14             InitializeComponent();
15         }
16  
17         // BackgroundWorker for verifying key functionality.
18  
19         private BackgroundWorker Checker = new BackgroundWorker();
20  
21         private void buttonSave_Click(object sender, EventArgs e)
22         {
23             // If key has been entered check it in worker thread, else display error.
24  
25             if (this.textBoxKey.Text == string.Empty)
26             {
27                 MessageBox.Show("You must enter an API Key", "DNS Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
28             }
29             else
30             {
31                 this.buttonSave.Enabled = false;
32                 this.labelKeyCheck.Text = "Checking key...";
33                 this.Checker.RunWorkerAsync(this.textBoxKey.Text);
34             }
35         }
36  
37         void Checker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
38         {
39             this.buttonSave.Enabled = true;
40             this.labelKeyCheck.Text = string.Empty;
41  
42             // If error occurred
43  
44             if (e.Result.GetType() == typeof(bool) && (bool)e.Result == false)
45             {
46                 MessageBox.Show("An unknown error has occurred", "DNS Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
47             }
48  
49             // If missing functions returned
50  
51             else if (e.Result.GetType() == typeof(List<string>) && ((List<string>)e.Result).Count > 0)
52             {
53                 string ErrorMessage = "The key does not have access to the following functions:";
54  
55                 foreach (string m in (List<string>)e.Result)
56                 {
57                     ErrorMessage += "\n - " + m;
58                 }
59  
60                 MessageBox.Show(ErrorMessage, "DNS Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
61             }
62  
63             // If key valid
64  
65             else
66             {
67                 Properties.Settings.Default.APIKey = this.textBoxKey.Text;
68                 Properties.Settings.Default.Save();
69  
70                 this.DialogResult = DialogResult.OK;
71                 this.Close();
72             }
73         }
74  
75         private void Checker_DoWork(object sender, DoWorkEventArgs e)
76         {
77             // Get API Methods
78             string Key = (string)e.Argument;
79  
80             DreamhostAPI API = new DreamhostAPI(Key);
81  
82             IEnumerable<string> Methods;
83  
84             try
85             {
86                 Methods = API.ListAccessibleMethods();
87             }
88             catch
89             {
90  
91                 e.Result = false;
92                 return;
93             }
94  
95             // Add unavailable methods to list
96  
97             string[] RequiredMethods = {
98                                         "domain-list_domains",
99                                         "dns-list_records",
100                                         "dns-add_record",
101                                         "dns-remove_record"
102                                        };
103  
104             List<string> MissingMethods = new List<string>();
105  
106             foreach (string m in RequiredMethods)
107             {
108                 if (!Methods.Contains(m))
109                 {
110                     MissingMethods.Add(m);
111                 }
112             }
113  
114             e.Result = MissingMethods;
115         }
116  
117         private void buttonCancel_Click(object sender, EventArgs e)
118         {
119             // Cancel background worker thread
120  
121             if (this.Checker.IsBusy)
122             {
123                 this.Checker.CancelAsync();
124             }
125  
126             // Close dialog
127  
128             this.DialogResult = DialogResult.Cancel;
129             this.Close();
130         }
131  
132         private void Settings_Load(object sender, EventArgs e)
133         {
134             // Enter existing key
135  
136             this.textBoxKey.Text = Properties.Settings.Default.APIKey;
137  
138             // Set up Background Worker thread
139  
140             this.Checker.DoWork += new DoWorkEventHandler(Checker_DoWork);
141             this.Checker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Checker_RunWorkerCompleted);
142             this.Checker.WorkerSupportsCancellation = true;
143         }
144     }
145 }