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