private void EditButtonMapping()
    {
      if (mappingsListView.SelectedIndices.Count != 1)
        return;

      ListViewItem item = mappingsListView.SelectedItems[0];

      List<ButtonMapping> currentMappings = GetCurrentButtonMappings();
      if (currentMappings == null)
        return;

      foreach (ButtonMapping test in currentMappings)
      {
        if (item.SubItems[0].Text.Equals(test.KeyCode, StringComparison.Ordinal))
        {
          ButtonMappingForm map = new ButtonMappingForm(test.KeyCode, test.Description, test.Command);

          if (map.ShowDialog(this) == DialogResult.OK)
          {
            item.SubItems[1].Text = map.Description;
            item.SubItems[2].Text = map.Command;

            test.Description = map.Description;
            test.Command = map.Command;
          }

          break;
        }
      }
    }
    private void NewButtonMapping()
    {
      List<ButtonMapping> currentMappings = GetCurrentButtonMappings();
      if (currentMappings == null)
        return;

      GetKeyCodeForm getKeyCode = new GetKeyCodeForm();
      getKeyCode.ShowDialog(this);

      string keyCode = getKeyCode.KeyCode;
      string deviceName = getKeyCode.DeviceName;

      if (String.IsNullOrEmpty(keyCode))
        return;

      ButtonMappingForm map = null;
      ButtonMapping existing = null;

      foreach (ButtonMapping test in currentMappings)
      {
        if (keyCode.Equals(test.KeyCode, StringComparison.Ordinal))
        {
          existing = test;
          map = new ButtonMappingForm(test.KeyCode, test.Description, test.Command);
          break;
        }
      }

      if (map == null)
      {
        string description = String.Empty;

        // TODO: Implement abstract remote button descriptions.
        if (deviceName.Equals("Abstract", StringComparison.OrdinalIgnoreCase))
        {
          switch (keyCode.ToLowerInvariant())
          {
            case "Red":
              description = "Red teletext button";
              break;
          }
        }

        map = new ButtonMappingForm(keyCode, description, String.Empty);
      }

      if (map.ShowDialog(this) == DialogResult.OK)
      {
        if (existing == null) // Create new mapping
        {
          mappingsListView.Items.Add(
            new ListViewItem(
              new string[] { map.KeyCode, map.Description, map.Command }
              ));

          currentMappings.Add(new ButtonMapping(map.KeyCode, map.Description, map.Command));
        }
        else // Replace existing mapping
        {
          for (int index = 0; index < mappingsListView.Items.Count; index++)
          {
            if (mappingsListView.Items[index].SubItems[0].Text.Equals(map.KeyCode, StringComparison.Ordinal))
            {
              mappingsListView.Items[index].SubItems[1].Text = map.Description;
              mappingsListView.Items[index].SubItems[2].Text = map.Command;
            }
          }

          existing.Description = map.Description;
          existing.Command = map.Command;
        }
      }
    }