Winify – Rev 15

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

namespace Winify
{
    public partial class SettingsForm : Form
    {
        #region Private Delegates, Events, Enums, Properties, Indexers and Fields

        private readonly Announcements.Announcements _notifications;

        private readonly global::Servers.Servers _servers;

        #endregion

        #region Constructors, Destructors and Finalizers

        private SettingsForm()
        {
            InitializeComponent();
        }

        public SettingsForm(global::Servers.Servers servers, Announcements.Announcements notifications) : this()
        {
            _servers = servers;

            listBox1.DataSource = _servers.Server;
            listBox1.DisplayMember = "Name";
            listBox1.DataBindings.Add(new Binding("Text", _servers.Server, "Name", true,
                DataSourceUpdateMode.OnPropertyChanged));

            _notifications = notifications;
            listBox2.DataSource = _notifications.Announcement;
            listBox2.DisplayMember = "Id";
            listBox2.DataBindings.Add(new Binding("Text", _notifications.Announcement, "Id", true,
                DataSourceUpdateMode.OnPropertyChanged));
        }

        #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 url = serverUrlTextBox.Text;
            var username = serverUsernameTextBox.Text;
            var password = serverPasswordTextBox.Text;

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

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

            var update = _servers.Server.FirstOrDefault(select => select.Name == server.Name);
            switch (update)
            {
                default:
                    _servers.Server.Remove(update);
                    update.Url = server.Url;
                    update.Username = server.Username;
                    update.Password = server.Password;
                    _servers.Server.Add(update);
                    break;
                case null:
                    _servers.Server.Add(server);
                    break;
            }
        }

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

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

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

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

                return;
            }

            var server = (Server) listBox.SelectedItem;

            serverNameTextBox.Text = server.Name;
            serverUrlTextBox.Text = server.Url;
            serverUsernameTextBox.Text = server.Username;
            serverPasswordTextBox.Text = server.Password;
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            listBox2.InvokeIfRequired(listbox =>
            {
                var item = listbox.SelectedItem;

                _notifications.Announcement.Remove((Announcement) item);
            });
        }

        private void Button4_Click(object sender, EventArgs e)
        {
            var appId = appIdTextBox.Text;
            var lingerTime = lingerTimeTextBox.Text;
            var speak = speakTextBox.Text;

            if (string.IsNullOrEmpty(appId) ||
                !int.TryParse(appId, out var numAppId) ||
                string.IsNullOrEmpty(lingerTime) ||
                !int.TryParse(lingerTime, out var numLingerTime) ||
                string.IsNullOrEmpty(speak))
            {
                return;
            }

            var notification = new Announcement(numAppId, numLingerTime, speak);

            var update = _notifications.Announcement.FirstOrDefault(select => select.Id == notification.Id);
            switch (update)
            {
                default:
                    _notifications.Announcement.Remove(update);
                    update.AppId = numAppId;
                    update.LingerTime = numLingerTime;
                    update.Speak = speak;
                    _notifications.Announcement.Add(update);
                    break;
                case null:
                    _notifications.Announcement.Add(notification);
                    break;
            }
        }

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

            if (listBox.SelectedIndex == -1)
            {
                appIdTextBox.Text = string.Empty;
                lingerTimeTextBox.Text = string.Empty;
                speakTextBox.Text = string.Empty;

                return;
            }

            var notification = (Announcement) listBox.SelectedItem;

            appIdTextBox.Text = notification.AppId.ToString();
            lingerTimeTextBox.Text = notification.LingerTime.ToString();
            speakTextBox.Text = notification.Speak;
        }

        #endregion
    }
}

Generated by GNU Enscript 1.6.5.90.