Click here to Skip to main content
15,895,011 members
Articles / Desktop Programming / Win32

QuickAccent - A Tool for Accents and Symbols

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
29 Oct 2014CPOL8 min read 30.7K   545   19  
Use QuickAccent to quickly copy accents and symbols to your clipboard. Also read the article to find out about the essentials when writing System Tray based applications
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace QuickAccent
{
    /// <summary>
    /// The <see cref="ApplicationContext"/> Singleton class.
    /// </summary>
    public sealed class ApplicationContext
    {
        /// <summary>
        /// The Singleton instace. Declared 'static readonly' to enforce
        /// a single instance only and lazy initialisation.
        /// </summary>
        private static readonly ApplicationContext instance = new ApplicationContext();

        /// <summary>
        /// Initializes a new instance of the <see cref="ApplicationContext"/> class.
        /// Declared private to enforce a single instance only.
        /// </summary>
        private ApplicationContext()
        {
        }

        /// <summary>
        /// Loads the settings.
        /// </summary>
        public void LoadSettings()
        {
            //  Do we have a settings file?
            if (File.Exists(GetSettingsPath()))
            {
                //  Try and load it.
                try
                {
                    using (var stream = new FileStream(GetSettingsPath(), FileMode.Open))
                    {
                        //  Create a serializer.
                        var serializer = new XmlSerializer(typeof(Settings));

                        //  Read the settings.
                        Settings = (Settings)serializer.Deserialize(stream);
                        
                    }
                }
                catch (Exception exception)
                {
                    //  Trace the exception.
                    System.Diagnostics.Trace.WriteLine("Exception loading settings file: " + exception);

                    //  Warn the user.
                    MessageBox.Show("Failed to load the settings file.", "Error");
                }
            }
            else
            {
                //  We have no settings file - create the default settings.
                CreateDefaultSettings();
                SaveSettings();
            }
        }

        /// <summary>
        /// Saves the settings.
        /// </summary>
        public void SaveSettings()
        {
            //  Try and open the strem.
            try
            {
                using (var stream = new FileStream(GetSettingsPath(), FileMode.Create))
                {
                    //  Create a serializer.
                    var serializer = new XmlSerializer(typeof(Settings));

                    //  Write the settings.
                    serializer.Serialize(stream, Settings);
                }
            }
            catch (Exception exception)
            {
                //  Trace the exception.
                System.Diagnostics.Trace.WriteLine("Exception saving settings file: " + exception);

                //  Warn the user.
                MessageBox.Show("Failed to save the settings file.", "Error");
            }
        }

        /// <summary>
        /// Applies the windows settings, such as the 'start on logon'.
        /// </summary>
        public void ApplyWindowsSettings()
        {
            //  Create a startup manager.
            var startupManager = new ApplicationAutoStartupManager();
            startupManager.ApplicationPath = Application.ExecutablePath;

            //  Are we set to start on windows startup?
            if (Settings.StartOnWindowsLogon)
                startupManager.AddToStartupFolder();
            else
                startupManager.RemoveFromStartupFolder();
        }

        /// <summary>
        /// Creates the default settings.
        /// </summary>
        private void CreateDefaultSettings()
        {
            Settings = new Settings
                           {
                               Accents = new List<Accent>()
                                             {
                                                 new Accent
                                                     {
                                                         DisplayName = "à",
                                                         AccentValue = "à",
                                                         AltCode = "Alt + 133", 
                                                         ShowInMenu = true, 
                                                         SortCode = 0
                                                     }
                                             },
                                             UseWindowsHotkey = true,
                                             StartOnWindowsLogon = true
                           };
        }

        /// <summary>
        /// Gets the ApplicationContext Singleton Instance.
        /// </summary>
        public static ApplicationContext Instance
        {
            get { return instance; }
        }

        /// <summary>
        /// Gets the settings path.
        /// </summary>
        /// <returns></returns>
        private string GetSettingsPath()
        {
            var executableDirectory = Path.GetDirectoryName(Application.ExecutablePath);
            if (executableDirectory == null)
                throw new InvalidOperationException("Cannot get the executable directory.");
            return Path.Combine(executableDirectory, @"Settings.xml");
        }
        
        /// <summary>
        /// Gets the settings.
        /// </summary>
        public Settings Settings { get; private set; }

        /// <summary>
        /// Gets the display accents.
        /// </summary>
        public IEnumerable<Accent> DisplayAccents
        {
            get { return from accent in Settings.Accents where accent.ShowInMenu orderby accent.SortCode select accent; }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United Kingdom United Kingdom
Follow my blog at www.dwmkerr.com and find out about my charity at www.childrenshomesnepal.org.

Comments and Discussions