Winify – Rev

Subversion Repositories:
Rev:
using System;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using AutoUpdaterDotNET;
using Winify.Connections;
using Winify.Gotify;
using Winify.Properties;

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

        private readonly GotifyConnectionManager _gotifyConnectionManager;

        private AboutForm _aboutForm;

        private Servers _servers;

        private SettingsForm _settingsForm;

        #endregion

        #region Constructors, Destructors and Finalizers

        public Form1()
        {
            InitializeComponent();

            AutoUpdater.Start("http://winify.grimore.org/update/winify.xml");

            // Upgrade settings if required.
            if (!ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).HasFile)
            {
                Settings.Default.Upgrade();
            }

            // Bind to settings changed event.
            Settings.Default.SettingsLoaded += Default_SettingsLoaded;
            Settings.Default.SettingsSaving += Default_SettingsSaving;
            Settings.Default.PropertyChanged += Default_PropertyChanged;

            _gotifyConnectionManager = new GotifyConnectionManager();

            Task.Run(async () =>
            {
                _servers = await LoadServers();

                _gotifyConnectionManager.UpdateServers(_servers);
            });
        }

        /// <summary>
        ///     Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && components != null)
            {
                components.Dispose();
            }

            base.Dispose(disposing);
        }

        #endregion

        #region Event Handlers

        private static void Default_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            Settings.Default.Save();
        }

        private static void Default_SettingsSaving(object sender, CancelEventArgs e)
        {
        }

        private static void Default_SettingsLoaded(object sender, SettingsLoadedEventArgs e)
        {
        }

        private void SettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_settingsForm != null)
            {
                return;
            }

            _settingsForm = new SettingsForm(_servers);
            _settingsForm.Closing += SettingsForm_Closing;
            _settingsForm.ServersUpdated += SettingsForm_ServersUpdated;
            _settingsForm.Show();
        }

        private async void SettingsForm_ServersUpdated(object sender, ServersUpdatedEventArgs e)
        {
            _servers = e.Servers;

            switch (await ServersSerialization.Serialize(e.Servers, Constants.ServersFile))
            {
                case SerializationSuccess _:
                    _gotifyConnectionManager.UpdateServers(e.Servers);
                    break;
                case SerializationFailure serializationFailure:
                    Debug.WriteLine(serializationFailure.Exception.Message);
                    break;
            }
        }

        private void SettingsForm_Closing(object sender, CancelEventArgs e)
        {
            if (_aboutForm == null)
            {
                return;
            }

            _settingsForm.Closing -= SettingsForm_Closing;
            _settingsForm.Dispose();
            _settingsForm = null;
        }

        private void AboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_aboutForm != null)
            {
                return;
            }

            _aboutForm = new AboutForm();
            _aboutForm.Closing += AboutForm_Closing;
            _aboutForm.Show();
        }

        private void AboutForm_Closing(object sender, CancelEventArgs e)
        {
            if (_aboutForm == null)
            {
                return;
            }

            _aboutForm.Closing -= AboutForm_Closing;
            _aboutForm.Dispose();
            _aboutForm = null;
        }

        private void QuitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        #endregion

        #region Private Methods

        private static async Task<Servers> LoadServers()
        {
            if (!Directory.Exists(Constants.UserApplicationDirectory))
            {
                Directory.CreateDirectory(Constants.UserApplicationDirectory);
            }

            var deserializationResult = await ServersSerialization.Deserialize(Constants.ServersFile);

            switch (deserializationResult)
            {
                case SerializationSuccess serializationSuccess:
                    return serializationSuccess.Servers;

                default:
                    return new Servers();
            }
        }

        #endregion
    }
}

Generated by GNU Enscript 1.6.5.90.