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