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