Click here to Skip to main content
15,886,823 members
Articles / Programming Languages / C#

LyricsFetcher - The Easiest Way to Find Lyrics for your Songs

Rate me:
Please Sign up or sign in to vote.
4.93/5 (82 votes)
29 Oct 2009GPL325 min read 201.3K   2.4K   184  
An article describing the development of a non-trivial C#/.NET application to fetch lyrics for songs.
/*
 * This class manages persistent user settings between application runs.
 *
 * Author: Phillip Piper
 * Date: 2009-02-10 10:28 PM
 *
 * CHANGE LOG:
 * 2009-03-31  JPP  Added MetaDataListViewState
 * 2009-02-15  JPP  Added ListViewState
 * 2009-02-10  JPP  Initial Version
 */

using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Drawing;

namespace LyricsFetcher
{
    /// <summary>
    /// This class manages persistent user settings between application runs.
    /// </summary>
    public class UserPreferences
    {
        #region Preferences

        /// <summary>
        /// Is the main window maximized?
        /// </summary>
        public bool IsMainWindowMaximized
        {
            get
            {
                bool? isMaximized = this.preferences["IsMainWindowMaximized"] as bool?;
                if (isMaximized.HasValue)
                    return (bool)isMaximized;
                else
                    return false;
            }
            set { this.preferences["IsMainWindowMaximized"] = value; }
        }

        /// <summary>
        /// Which library was selected by the user?
        /// </summary>
        public LibraryApplication LibraryApplication
        {
            get
            {
                LibraryApplication? app = this.preferences["LibraryApplication"] as LibraryApplication?;
                if (app.HasValue)
                    return (LibraryApplication)app;
                else
                    return LibraryApplication.Unknown;
            }
            set { this.preferences["LibraryApplication"] = value; }
        }

        /// <summary>
        /// What is the state of the main listview?
        /// </summary>
        public byte[] ListViewState
        {
            get
            {
                if (this.preferences.ContainsKey("ListViewState"))
                    return (byte[])this.preferences["ListViewState"];
                else
                    return null;
            }
            set { this.preferences["ListViewState"] = value; }
        }

        /// <summary>
        /// Where is the main window located?
        /// </summary>
        public Point MainWindowLocation
        {
            get
            {
                Point? point = this.preferences["MainWindowLocation"] as Point?;
                if (point.HasValue)
                    return (Point)point;
                else
                    return new Point(-1, -1);
            }
            set { this.preferences["MainWindowLocation"] = value; }
        }

        /// <summary>
        /// How big is the main window?
        /// </summary>
        public Size MainWindowSize
        {
            get
            {
                Size? size = this.preferences["MainWindowSize"] as Size?;
                if (size.HasValue)
                    return (Size)size;
                else
                    return new Size(-1, -1);
            }
            set { this.preferences["MainWindowSize"] = value; }
        }

        /// <summary>
        /// What is the state of the metadata listview?
        /// </summary>
        public byte[] MetaDataListViewState {
            get {
                if (this.preferences.ContainsKey("MetaDataListViewState"))
                    return (byte[])this.preferences["MetaDataListViewState"];
                else
                    return null;
            }
            set { this.preferences["MetaDataListViewState"] = value; }
        }

        #endregion

        #region Load and Store

        /// <summary>
        /// Load the user preferences from its normal location
        /// </summary>
        public void Load()
        {
            this.Load(this.GetUserPreferencesPath());
        }

        /// <summary>
        /// Load the user preferences from the given path.
        /// </summary>
        /// <remarks>This discards any previous preferences, even if the load fails</remarks>
        /// <param name="path">The full path name to a file created by Save()</param>
        public void Load(string path)
        {
            this.preferences = new Hashtable();

            // Can't do anything more if the file doesn't exist
            if (!File.Exists(path))
                return;

            BinaryFormatter deserializer = new BinaryFormatter();
            using (FileStream fs = File.OpenRead(path)) {
                try {
                    this.preferences = deserializer.Deserialize(fs) as Hashtable;
                }
                catch (System.Runtime.Serialization.SerializationException ex) {
                    System.Diagnostics.Debug.WriteLine("Load preferences failed");
                    System.Diagnostics.Debug.WriteLine(ex);
                }
            }
        }

        /// <summary>
        /// Save the user preferences to its normal location
        /// </summary>
        public void Save()
        {
            this.Save(this.GetUserPreferencesPath());
        }

        /// <summary>
        /// Save the preferences to the given path
        /// </summary>
        /// <remarks>Any existing saved prefs will be deleted.</remarks>
        /// <param name="path">The full path name of the prefs file</param>
        public void Save(string path)
        {
            // Make the directory for the prefs if it doesn't exist
            if (!File.Exists(Path.GetDirectoryName(path)))
                Directory.CreateDirectory(Path.GetDirectoryName(path));

            // Remove any existing cache
            if (File.Exists(path))
                File.Delete(path);

            // Store the cache as a binary stream
            using (FileStream fs = File.Create(path)) {
                BinaryFormatter serializer = new BinaryFormatter();
                serializer.Serialize(fs, this.preferences);
            }
        }

        #endregion

        private string GetUserPreferencesPath()
        {
            String path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            path = Path.Combine(path, "LyricsFetcher");
            path = Path.Combine(path, "Preferences.bin");
            return path;
        }

        private Hashtable preferences = new Hashtable();
    }
}

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 GNU General Public License (GPLv3)


Written By
Team Leader
Australia Australia
Phillip has been playing with computers since the Apple II was the hottest home computer available. He learned the fine art of C programming and Guru meditation on the Amiga.

C# and Python are his languages of choice. Smalltalk is his mentor for simplicity and beauty. C++ is to programming what drills are to visits to the dentist.

He worked for longer than he cares to remember as Lead Programmer and System Architect of the Objective document management system. (www.objective.com)

He has lived for 10 years in northern Mozambique, teaching in villages.

He has developed high volume trading software, low volume FX trading software, and is currently working for Atlassian on HipChat.

Comments and Discussions