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         {
2 clempaul 117             this.DialogResult = DialogResult.OK;
118             this.Close();
119         }
120  
121         void AddRecord_DoWork(object sender, DoWorkEventArgs e)
122         {
123             this.API.DNS.AddRecord((DNSRecord)e.Argument);
124             System.Threading.Thread.Sleep(3000);
125         }
126  
127         void EditRecord_DoWork(object sender, DoWorkEventArgs e)
128         {
129             this.API.DNS.RemoveRecord(this.Record);
130             System.Threading.Thread.Sleep(3000);
131             this.API.DNS.AddRecord((DNSRecord)e.Argument);
132             System.Threading.Thread.Sleep(3000);
133         }
134  
135         private void buttonSave_Click(object sender, EventArgs e)
136         {
137             if (this.textBoxRecord.Text == string.Empty)
138             {
139                 MessageBox.Show("You must enter a record", "DNS Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
140             }
141             else if (this.comboBoxType.Text == string.Empty)
142             {
143                 MessageBox.Show("You must select a type", "DNS Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
144             }
145             else if (this.textBoxValue.Text == string.Empty)
146             {
147                 MessageBox.Show("You must enter a value", "DNS Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
148             }
149             else
150             {
151                 this.buttonSave.Enabled = false;
152                 this.buttonCancel.Enabled = false;
153                 this.textBoxComment.Enabled = false;
154                 this.textBoxValue.Enabled = false;
155                 this.textBoxRecord.Enabled = false;
156                 this.comboBoxType.Enabled = false;
157  
158                 if (this.IsEdit)
159                 {
6 clempaul 160                     this.EditRecord.RunWorkerAsync(this.BuildRecord());
2 clempaul 161                 }
162                 else
163                 {
6 clempaul 164                     this.AddRecord.RunWorkerAsync(this.BuildRecord());
165                 }
166             }
167  
168         }
169  
170         private DNSRecord BuildRecord()
171         {
172             return new DNSRecord
2 clempaul 173                         {
174                             record = this.textBoxRecord.Text,
175                             value = this.textBoxValue.Text,
176                             type = this.comboBoxType.Text,
177                             comment = this.textBoxComment.Text
6 clempaul 178                         };
2 clempaul 179         }
180  
181         private void buttonCancel_Click(object sender, EventArgs e)
182         {
183             this.DialogResult = DialogResult.Cancel;
184             this.Close();
185         }
186     }
187 }