Rev Author Line No. Line
2 clempaul 1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using clempaul.Dreamhost.ResponseData;
10 using clempaul;
11  
12 namespace DNS_Manager
13 {
14     public partial class AddEdit : Form
15     {
16         private BackgroundWorker AddRecord = new BackgroundWorker();
17         private BackgroundWorker EditRecord = new BackgroundWorker();
18  
19         private DreamhostAPI API;
20         private DNSRecord Record;
21  
22         private bool IsEdit = false;
23  
24         public AddEdit(DreamhostAPI API, DNSRecord Record)
25             : this(API)
26         {
27             this.IsEdit = true;
28             this.Record = Record;
29  
30             this.Text = "Edit Record";
31             this.textBoxRecord.Text = this.Record.record;
32             this.textBoxComment.Text = this.Record.comment;
33             this.textBoxValue.Text = this.Record.value;
34             this.comboBoxType.Text = this.Record.type;
35             this.textBoxRecord.Enabled = false;
36             this.comboBoxType.Enabled = false;
37  
38             this.EditRecord.DoWork += new DoWorkEventHandler(EditRecord_DoWork);
6 clempaul 39             this.EditRecord.RunWorkerCompleted += new RunWorkerCompletedEventHandler(EditRecord_RunWorkerCompleted);
2 clempaul 40         }
41  
42         public AddEdit(DreamhostAPI API)
43         {
44             InitializeComponent();
45  
46             this.API = API;
47  
48             this.AddRecord.DoWork += new DoWorkEventHandler(AddRecord_DoWork);
49             this.AddRecord.RunWorkerCompleted += new RunWorkerCompletedEventHandler(AddRecord_RunWorkerCompleted);
50         }
51  
52         void AddRecord_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
53         {
6 clempaul 54             if (e.Error == null)
55             {
56                 this.DialogResult = DialogResult.OK;
57                 this.Close();
58             }
59             else
60             {
61                 if (e.Error.Message == "CNAME_already_on_record")
62                 {
63                     MessageBox.Show("There can only be one CNAME for this record.", "DNS Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
64                 }
65                 else if (e.Error.Message == "CNAME_must_be_only_record")
66                 {
67                     MessageBox.Show("A CNAME already exists for this record.", "DNS Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
68                 }
69                 else if (e.Error.Message.Contains("invalid_record"))
70                 {
71                     MessageBox.Show("This record is invalid:\n" + e.Error.Message.Replace("invalid_record\t", "").CapitaliseFirstLetter(), "DNS Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
72                 }
73                 else if (e.Error.Message.Contains("invalid_value"))
74                 {
75                     MessageBox.Show("This value is invalid:\n" + e.Error.Message.Replace("invalid_value\t", "").CapitaliseFirstLetter(), "DNS Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
76                 }
77                 else if (e.Error.Message == "no_such_zone")
78                 {
79                     MessageBox.Show("Unable to add record to this zone", "DNS Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
80                 }
81                 else if (e.Error.Message == "record_already_exists_not_editable")
82                 {
83                     MessageBox.Show("This record already exists and is not editable", "DNS Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
84                 }
85                 else if (e.Error.Message == "record_already_exists_remove_first")
86                 {
87                     MessageBox.Show("This record already exists.\nPlease try editing it or removing it first.", "DNS Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
88                 }
89                 else if (e.Error.Message.Contains("internal_error"))
90                 {
91                     if (MessageBox.Show("An internal error has occurred", "DNS Manager", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == DialogResult.Retry)
92                     {
93                         this.AddRecord.RunWorkerAsync(this.BuildRecord());
94                         return;
95                     }
96                 }
97                 else
98                 {
99                     if (MessageBox.Show(e.Error.Message, "DNS Manager", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == DialogResult.Retry)
100                     {
101                         this.AddRecord.RunWorkerAsync(this.BuildRecord());
102                         return;
103                     }
104                 }
105  
106                 this.buttonSave.Enabled = true;
107                 this.buttonCancel.Enabled = true;
108                 this.textBoxComment.Enabled = true;
109                 this.textBoxValue.Enabled = true;
110                 this.textBoxRecord.Enabled = true;
111                 this.comboBoxType.Enabled = true;
112             }
113         }
114  
115         void EditRecord_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
116         {
7 clempaul 117             if (e.Error == null)
118             {
119                 this.DialogResult = DialogResult.OK;
120                 this.Close();
121             }
122             else
123             {
124                 if (e.Error.Message == "internal_error_could_not_destroy_record"
125                     || e.Error.Message == "internal_error_could_not_update_zone")
126                 {
127                     if (MessageBox.Show("An internal error has occurred.", "DNS Manager", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == DialogResult.Retry)
128                     {
129                         this.EditRecord.RunWorkerAsync(this.BuildRecord());
130                         return;
131                     }
132                 }
133                 else if (e.Error.Message.Contains("invalid_value"))
134                 {
135                     MessageBox.Show("This value is invalid:\n" +
136                         e.Error.Message.Replace("invalid_value\t", "").CapitaliseFirstLetter()
137                         + "\nThe previous record has been deleted.", "DNS Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
138  
139                     this.IsEdit = false;
140                     this.textBoxRecord.Enabled = true;
141                     this.comboBoxType.Enabled = true;
142                 }
143  
144                 this.buttonSave.Enabled = true;
145                 this.buttonCancel.Enabled = true;
146                 this.textBoxComment.Enabled = true;
147                 this.textBoxValue.Enabled = true;
148             }
2 clempaul 149         }
150  
151         void AddRecord_DoWork(object sender, DoWorkEventArgs e)
152         {
153             this.API.DNS.AddRecord((DNSRecord)e.Argument);
154             System.Threading.Thread.Sleep(3000);
155         }
156  
157         void EditRecord_DoWork(object sender, DoWorkEventArgs e)
158         {
159             this.API.DNS.RemoveRecord(this.Record);
7 clempaul 160  
161             string[] retryErrors = {
162                                     "CNAME_must_be_only_record",
163                                     "CNAME_already_on_record",
164                                     "record_already_exists_remove_first",
165                                     "internal_error_updating_zone",
166                                     "internal_error_could_not_load_zone",
167                                     "internal_error_could_not_add_record"
168                                    };
169  
170             bool finish = false;
171  
172             while (!finish)
173             {
174                 try
175                 {
176                     this.API.DNS.AddRecord((DNSRecord)e.Argument);
177                     finish = true;
178                 }
179                 catch (Exception x)
180                 {
181                     if (!retryErrors.Contains(x.Message))
182                     {
183                         throw x;
184                     }
185                 }
186             }
187  
2 clempaul 188             System.Threading.Thread.Sleep(3000);
189         }
190  
191         private void buttonSave_Click(object sender, EventArgs e)
192         {
193             if (this.textBoxRecord.Text == string.Empty)
194             {
195                 MessageBox.Show("You must enter a record", "DNS Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
196             }
197             else if (this.comboBoxType.Text == string.Empty)
198             {
199                 MessageBox.Show("You must select a type", "DNS Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
200             }
201             else if (this.textBoxValue.Text == string.Empty)
202             {
203                 MessageBox.Show("You must enter a value", "DNS Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
204             }
205             else
206             {
207                 this.buttonSave.Enabled = false;
208                 this.buttonCancel.Enabled = false;
209                 this.textBoxComment.Enabled = false;
210                 this.textBoxValue.Enabled = false;
211                 this.textBoxRecord.Enabled = false;
212                 this.comboBoxType.Enabled = false;
213  
214                 if (this.IsEdit)
215                 {
6 clempaul 216                     this.EditRecord.RunWorkerAsync(this.BuildRecord());
2 clempaul 217                 }
218                 else
219                 {
6 clempaul 220                     this.AddRecord.RunWorkerAsync(this.BuildRecord());
221                 }
222             }
223  
224         }
225  
226         private DNSRecord BuildRecord()
227         {
228             return new DNSRecord
2 clempaul 229                         {
230                             record = this.textBoxRecord.Text,
231                             value = this.textBoxValue.Text,
232                             type = this.comboBoxType.Text,
233                             comment = this.textBoxComment.Text
6 clempaul 234                         };
2 clempaul 235         }
236  
237         private void buttonCancel_Click(object sender, EventArgs e)
238         {
239             this.DialogResult = DialogResult.Cancel;
240             this.Close();
241         }
242     }
243 }