Winify – Rev 4

Subversion Repositories:
Rev:
using System;
using System.Linq;
using System.Windows.Forms;
using Winify.Connections;
using Winify.Properties;
using Winify.Utilities;

namespace Winify
{
    public partial class SettingsForm : Form
    {
        #region Public Events & Delegates

        public event EventHandler<ServersUpdatedEventArgs> ServersUpdated;

        #endregion

        #region Private Delegates, Events, Enums, Properties, Indexers and Fields

        private readonly Servers _servers;

        #endregion

        #region Constructors, Destructors and Finalizers

        private SettingsForm()
        {
            InitializeComponent();
        }

        public SettingsForm(Servers servers) : this()
        {
            _servers = servers;
        }

        #endregion

        #region Event Handlers

        private void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            Settings.Default.LaunchOnBoot = ((CheckBox) sender).Checked;

            Miscellaneous.LaunchOnBootSet(Settings.Default.LaunchOnBoot);
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            var name = serverNameTextBox.Text;
            var host = serverHostTextBox.Text;
            var port = serverPortTextBox.Text;
            var username = serverUsernameTextBox.Text;
            var password = serverPasswordTextBox.Text;

            if (string.IsNullOrEmpty(name) ||
                string.IsNullOrEmpty(host) ||
                string.IsNullOrEmpty(port) ||
                string.IsNullOrEmpty(username) ||
                string.IsNullOrEmpty(password))
            {
                return;
            }

            var server = new Server(name, host, port, username, password);

            if (_servers.Server.Any(servers => servers.Name == server.Name))
            {
                return;
            }

            listBox1.InvokeIfRequired(listBox => { listBox.Items.Add(server); });

            _servers.Server.Add(server);

            ServersUpdated?.Invoke(this, new ServersUpdatedEventArgs(_servers));
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            listBox1.InvokeIfRequired(listBox =>
            {
                var item = listBox.SelectedItem;

                listBox.Items.Remove(item);

                _servers.Server.Remove((Server) item);

                ServersUpdated?.Invoke(this, new ServersUpdatedEventArgs(_servers));
            });
        }

        private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var listBox = (ListBox) sender;

            if (listBox.SelectedIndex == -1)
            {
                serverNameTextBox.Text = string.Empty;
                serverHostTextBox.Text = string.Empty;
                serverPortTextBox.Text = string.Empty;
                serverUsernameTextBox.Text = string.Empty;
                serverPasswordTextBox.Text = string.Empty;

                return;
            }

            var server = (Server) listBox.SelectedItem;

            serverNameTextBox.Text = server.Name;
            serverHostTextBox.Text = server.Host;
            serverPortTextBox.Text = server.Port;
            serverUsernameTextBox.Text = server.Username;
            serverPasswordTextBox.Text = server.Password;
        }

        private void SettingsForm_Shown(object sender, EventArgs e)
        {
            foreach (var server in _servers.Server)
            {
                listBox1.InvokeIfRequired(listBox => { listBox.Items.Add(server); });
            }
        }

        #endregion
    }
}

Generated by GNU Enscript 1.6.5.90.