| 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; |
|
|
10 |
using clempaul.Dreamhost.ResponseData; |
|
|
11 |
|
|
|
12 |
namespace DNS_Manager |
|
|
13 |
{ |
|
|
14 |
public partial class Listing : Form |
|
|
15 |
{ |
|
|
16 |
public Listing() |
|
|
17 |
{ |
|
|
18 |
InitializeComponent(); |
|
|
19 |
} |
|
|
20 |
|
|
|
21 |
BackgroundWorker GetRecords = new BackgroundWorker(); |
|
|
22 |
BackgroundWorker DeleteRecord = new BackgroundWorker(); |
|
|
23 |
|
|
|
24 |
DreamhostAPI API; |
|
|
25 |
IEnumerable<DNSRecord> DNSRecords; |
|
|
26 |
|
|
|
27 |
private void Listing_Load(object sender, EventArgs e) |
|
|
28 |
{ |
|
|
29 |
// Check API Key |
|
|
30 |
|
|
|
31 |
if (Properties.Settings.Default.APIKey == string.Empty && |
|
|
32 |
(new Settings().ShowDialog() == DialogResult.Cancel)) |
|
|
33 |
{ |
|
|
34 |
Application.Exit(); |
|
|
35 |
} |
|
|
36 |
|
|
|
37 |
this.GetRecords.DoWork += new DoWorkEventHandler(GetRecords_DoWork); |
|
|
38 |
this.GetRecords.RunWorkerCompleted += new RunWorkerCompletedEventHandler(GetRecords_RunWorkerCompleted); |
|
|
39 |
|
|
|
40 |
this.DeleteRecord.DoWork += new DoWorkEventHandler(DeleteRecord_DoWork); |
|
|
41 |
this.DeleteRecord.RunWorkerCompleted += new RunWorkerCompletedEventHandler(DeleteRecord_RunWorkerCompleted); |
|
|
42 |
|
|
|
43 |
this.API = new DreamhostAPI(Properties.Settings.Default.APIKey); |
|
|
44 |
|
|
|
45 |
this.LoadRecords(); |
|
|
46 |
} |
|
|
47 |
|
|
|
48 |
private void LoadRecords() |
|
|
49 |
{ |
|
|
50 |
if (!this.GetRecords.IsBusy) |
|
|
51 |
{ |
|
|
52 |
this.toolStripStatusLabel.Text = "Loading records..."; |
|
|
53 |
this.GetRecords.RunWorkerAsync(); |
|
|
54 |
} |
|
|
55 |
} |
|
|
56 |
|
|
|
57 |
void DeleteRecord_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) |
|
|
58 |
{ |
|
|
59 |
MessageBox.Show("Record Deleted", "DNS Manager", MessageBoxButtons.OK, MessageBoxIcon.Information); |
|
|
60 |
this.LoadRecords(); |
|
|
61 |
} |
|
|
62 |
|
|
|
63 |
void DeleteRecord_DoWork(object sender, DoWorkEventArgs e) |
|
|
64 |
{ |
|
|
65 |
if (e.Argument.GetType() != typeof(DNSRecord)) |
|
|
66 |
{ |
|
|
67 |
throw new Exception("Invalid Argument"); |
|
|
68 |
} |
|
|
69 |
|
|
|
70 |
this.API.DNS.RemoveRecord((DNSRecord)e.Argument); |
|
|
71 |
System.Threading.Thread.Sleep(3000); |
|
|
72 |
} |
|
|
73 |
|
|
|
74 |
void GetRecords_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) |
|
|
75 |
{ |
|
|
76 |
string zone = this.toolStripComboBoxDomains.Text ?? string.Empty; |
|
|
77 |
|
|
|
78 |
this.toolStripComboBoxDomains.Items.Clear(); |
|
|
79 |
|
|
|
80 |
foreach (DNSRecord d in this.DNSRecords) |
|
|
81 |
{ |
|
|
82 |
if (!this.toolStripComboBoxDomains.Items.Contains(d.zone)) |
|
|
83 |
{ |
|
|
84 |
this.toolStripComboBoxDomains.Items.Add(d.zone); |
|
|
85 |
} |
|
|
86 |
} |
|
|
87 |
|
|
|
88 |
this.toolStripStatusLabel.Text = string.Empty; |
|
|
89 |
|
|
|
90 |
this.toolStripComboBoxDomains.Text = zone; |
|
|
91 |
this.SetZone(); |
|
|
92 |
} |
|
|
93 |
|
|
|
94 |
void GetRecords_DoWork(object sender, DoWorkEventArgs e) |
|
|
95 |
{ |
|
|
96 |
this.DNSRecords = this.API.DNS.ListRecords(); |
|
|
97 |
|
|
|
98 |
if (e.Argument != null) |
|
|
99 |
{ |
|
|
100 |
e.Result = true; |
|
|
101 |
} |
|
|
102 |
else |
|
|
103 |
{ |
|
|
104 |
e.Result = false; |
|
|
105 |
} |
|
|
106 |
} |
|
|
107 |
|
|
|
108 |
private void aboutToolStripMenuItem_Click(object sender, EventArgs e) |
|
|
109 |
{ |
|
|
110 |
new About().ShowDialog(); |
|
|
111 |
} |
|
|
112 |
|
|
|
113 |
private void visitWebsiteToolStripMenuItem_Click(object sender, EventArgs e) |
|
|
114 |
{ |
|
|
115 |
System.Diagnostics.Process.Start("http://software.clempaul.me.uk/"); |
|
|
116 |
} |
|
|
117 |
|
|
|
118 |
private void toolStripButtonSettings_Click(object sender, EventArgs e) |
|
|
119 |
{ |
|
|
120 |
new Settings().ShowDialog(); |
|
|
121 |
} |
|
|
122 |
|
|
|
123 |
private void toolStripButtonReload_Click(object sender, EventArgs e) |
|
|
124 |
{ |
|
|
125 |
this.LoadRecords(); |
|
|
126 |
} |
|
|
127 |
|
|
|
128 |
private void toolStripButtonSelectDomain_Click(object sender, EventArgs e) |
|
|
129 |
{ |
|
|
130 |
this.SetZone(); |
|
|
131 |
} |
|
|
132 |
|
|
|
133 |
private void toolStripButton3_Click(object sender, EventArgs e) |
|
|
134 |
{ |
|
|
135 |
if (this.dataGridView.SelectedRows.Count == 1) |
|
|
136 |
{ |
|
|
137 |
DNSRecord record = (DNSRecord)this.dataGridView.SelectedRows[0].DataBoundItem; |
|
|
138 |
|
|
|
139 |
if (!record.editable) |
|
|
140 |
{ |
|
|
141 |
MessageBox.Show("This record is not editable", "DNS Manager", MessageBoxButtons.OK, MessageBoxIcon.Error); |
|
|
142 |
} |
|
|
143 |
else if (MessageBox.Show("Are you sure you want to delete the record for " + record.record + "?", "DNS Manager", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) |
|
|
144 |
{ |
|
|
145 |
this.toolStripStatusLabel.Text = "Deleting record..."; |
|
|
146 |
this.DeleteRecord.RunWorkerAsync(record); |
|
|
147 |
} |
|
|
148 |
} |
|
|
149 |
} |
|
|
150 |
|
|
|
151 |
private void toolStripButtonAdd_Click(object sender, EventArgs e) |
|
|
152 |
{ |
|
|
153 |
if (new AddEdit(this.API).ShowDialog() == DialogResult.OK) |
|
|
154 |
{ |
|
|
155 |
this.LoadRecords(); |
|
|
156 |
} |
|
|
157 |
} |
|
|
158 |
|
|
|
159 |
private void toolStripButton2_Click(object sender, EventArgs e) |
|
|
160 |
{ |
|
|
161 |
if (this.dataGridView.SelectedRows.Count == 1) |
|
|
162 |
{ |
|
|
163 |
DNSRecord record = (DNSRecord)this.dataGridView.SelectedRows[0].DataBoundItem; |
|
|
164 |
|
|
|
165 |
if (!record.editable) |
|
|
166 |
{ |
|
|
167 |
MessageBox.Show("This record is not editable", "DNS Manager", MessageBoxButtons.OK, MessageBoxIcon.Error); |
|
|
168 |
} |
|
|
169 |
else |
|
|
170 |
{ |
|
|
171 |
if (new AddEdit(this.API, record).ShowDialog() == DialogResult.OK) |
|
|
172 |
{ |
|
|
173 |
this.LoadRecords(); |
|
|
174 |
} |
|
|
175 |
} |
|
|
176 |
} |
|
|
177 |
} |
|
|
178 |
|
|
|
179 |
private void SetZone() |
|
|
180 |
{ |
|
|
181 |
string zoneValue = this.toolStripComboBoxDomains.Text ?? string.Empty; |
|
|
182 |
|
|
|
183 |
this.dataGridView.DataSource = (from d in this.DNSRecords |
|
|
184 |
where d.zone == zoneValue |
|
|
185 |
select d).ToList(); |
|
|
186 |
} |
|
|
187 |
} |
|
|
188 |
} |