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