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