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.  
  8. namespace DNS_Manager
  9. {
  10.     public partial class Settings : Form
  11.     {
  12.         public Settings()
  13.         {
  14.             InitializeComponent();
  15.         }
  16.  
  17.         // BackgroundWorker for verifying key functionality.
  18.  
  19.         private BackgroundWorker Checker = new BackgroundWorker();
  20.  
  21.         private void Settings_Load(object sender, EventArgs e)
  22.         {
  23.             // Enter existing key
  24.  
  25.             this.textBoxKey.Text = Properties.Settings.Default.APIKey;
  26.  
  27.             // Set up Background Worker thread
  28.  
  29.             this.Checker.DoWork += new DoWorkEventHandler(Checker_DoWork);
  30.             this.Checker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Checker_RunWorkerCompleted);
  31.             this.Checker.WorkerSupportsCancellation = true;
  32.         }
  33.  
  34.         private void buttonCancel_Click(object sender, EventArgs e)
  35.         {
  36.             // Cancel background worker thread
  37.  
  38.             if (this.Checker.IsBusy)
  39.             {
  40.                 this.Checker.CancelAsync();
  41.             }
  42.  
  43.             // Close dialog
  44.  
  45.             this.DialogResult = DialogResult.Cancel;
  46.             this.Close();
  47.         }
  48.  
  49.  
  50.         private void buttonSave_Click(object sender, EventArgs e)
  51.         {
  52.             // If key has been entered check it in worker thread, else display error.
  53.  
  54.             if (this.textBoxKey.Text == string.Empty)
  55.             {
  56.                 MessageBox.Show("You must enter an API Key", "DNS Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
  57.             }
  58.             else
  59.             {
  60.                 this.buttonSave.Enabled = false;
  61.                 this.labelKeyCheck.Text = "Checking key...";
  62.                 this.Checker.RunWorkerAsync(this.textBoxKey.Text);
  63.             }
  64.         }
  65.  
  66.         private void Checker_DoWork(object sender, DoWorkEventArgs e)
  67.         {
  68.             // Get API Methods
  69.             string Key = (string)e.Argument;
  70.  
  71.             DreamhostAPI API = new DreamhostAPI(Key);
  72.  
  73.             IEnumerable<string> Methods = API.ListAccessibleMethods();
  74.  
  75.             // Add unavailable methods to list
  76.  
  77.             string[] RequiredMethods = {
  78.                                         "dns-list_records",
  79.                                         "dns-add_record",
  80.                                         "dns-remove_record"
  81.                                        };
  82.  
  83.             List<string> MissingMethods = new List<string>();
  84.  
  85.             foreach (string m in RequiredMethods)
  86.             {
  87.                 if (!Methods.Contains(m))
  88.                 {
  89.                     MissingMethods.Add(m);
  90.                 }
  91.             }
  92.  
  93.             e.Result = MissingMethods;
  94.         }
  95.  
  96.         void Checker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  97.         {
  98.            
  99.  
  100.             // If error occurred
  101.  
  102.             if (e.Error != null)
  103.             {
  104.                 // Ask whether to retry.
  105.  
  106.                 if (MessageBox.Show(e.Error.Message, "DNS Manager", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == DialogResult.Retry)
  107.                 {
  108.                     this.Checker.RunWorkerAsync(this.textBoxKey.Text);
  109.                     return;
  110.                 }
  111.             }
  112.  
  113.             // If missing functions returned
  114.  
  115.             else if (e.Result.GetType() == typeof(List<string>) && ((List<string>)e.Result).Count > 0)
  116.             {
  117.                 string ErrorMessage = "The key does not have access to the following functions:";
  118.  
  119.                 foreach (string m in (List<string>)e.Result)
  120.                 {
  121.                     ErrorMessage += "\n - " + m;
  122.                 }
  123.  
  124.                 MessageBox.Show(ErrorMessage, "DNS Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
  125.             }
  126.  
  127.             // If key valid
  128.  
  129.             else
  130.             {
  131.                 // Save key
  132.  
  133.                 Properties.Settings.Default.APIKey = this.textBoxKey.Text;
  134.                 Properties.Settings.Default.Save();
  135.  
  136.                 // Set dialog result and return.
  137.  
  138.                 this.DialogResult = DialogResult.OK;
  139.                 this.Close();
  140.             }
  141.            
  142.             this.buttonSave.Enabled = true;
  143.             this.labelKeyCheck.Text = string.Empty;
  144.         }
  145.  
  146.  
  147.  
  148.  
  149.     }
  150. }
  151.