Winify – Rev

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

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

        private readonly global::Servers.Servers _servers;

        private AboutForm _aboutForm;

        private GotifyConnectionManager _gotifyConnectionManager;

        private NotificationManager _notificationManager;

        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;

            _servers = new global::Servers.Servers();
            _servers.Server.CollectionChanged += Server_CollectionChanged;

            _notificationManager = new NotificationManager(TaskScheduler.FromCurrentSynchronizationContext());

            _gotifyConnectionManager = new GotifyConnectionManager(_servers);
            _gotifyConnectionManager.GotifyNotification += GotifyConnectionManager_GotifyNotification;

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

                foreach (var server in restoredServers.Server)
                {
                    _servers.Server.Add(server);
                }
            });
        }

        /// <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)
            {
                _servers.Server.CollectionChanged -= Server_CollectionChanged;

                Settings.Default.SettingsLoaded -= Default_SettingsLoaded;
                Settings.Default.SettingsSaving -= Default_SettingsSaving;
                Settings.Default.PropertyChanged -= Default_PropertyChanged;

                _gotifyConnectionManager.GotifyNotification -= GotifyConnectionManager_GotifyNotification;
                _gotifyConnectionManager?.Dispose();
                _gotifyConnectionManager = null;

                _notificationManager?.Dispose();
                _notificationManager = null;

                components.Dispose();
            }

            base.Dispose(disposing);
        }

        #endregion

        #region Event Handlers

        private void GotifyConnectionManager_GotifyNotification(object sender, GotifyNotificationEventArgs e)
        {
            _notificationManager.ShowNotification(e);
        }

        private async void Server_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch (await ServersSerialization.Serialize(_servers, Constants.ServersFile))
            {
                case ServersSerializationFailure serializationFailure:
                    Debug.WriteLine(serializationFailure.Exception.Message);
                    break;
            }
        }

        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.Show();
        }

        private void SettingsForm_Closing(object sender, CancelEventArgs e)
        {
            if (_settingsForm == 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();
        }

        private void UpdateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AutoUpdater.Start("http://winify.grimore.org/update/winify.xml");
        }

        #endregion

        #region Private Methods

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

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

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

                default:
                    return new global::Servers.Servers();
            }
        }

        #endregion
    }
}

Generated by GNU Enscript 1.6.5.90.