view Settings.cs @ 0:aca8706f4eec default tip

Initial commit
author Brad Greco <brad@bgreco.net>
date Mon, 13 Oct 2014 21:28:19 -0500
parents
children
line wrap: on
line source

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

/*
 * Copyright (c) 2014 Brad Greco <brad@bgreco.net>
 */
namespace QuickQR
{
    class Settings
    {
        public String error;
        private String configFile;
        private String configDir;

        public bool autostart = true;
        public bool background = true;
        public String hotkey = "";

        public Settings()
        {
            String appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            configDir = Path.Combine(appData, "QuickQR");
            configFile = Path.Combine(configDir, "quickqr.ini");

            readConfig();
        }

        public void setAutostart(bool autostart)
        {
            this.autostart = autostart;
            writeConfig();
        }

        public void setBackground(bool background)
        {
            this.background = background;
            writeConfig();
        }

        public void setHotkey(KeyBinding hotkey)
        {
            this.hotkey = hotkey.comboString();
            writeConfig();
        }

        private void readConfig()
        {
            StreamReader reader;
            try
            {
                reader = new StreamReader(configFile);
            }
            catch (IOException e)
            {
                error = e.Message;
                return;
            }
            String line;
            while ((line = reader.ReadLine()) != null)
            {
                String[] words = line.Split(null, 3);
                if(words.Length >= 3 && words[1].Equals("="))
                {
                    if (words[0].Equals("autostart"))
                        autostart = words[2].Equals("true");
                    if (words[0].Equals("background"))
                        background = words[2].Equals("true");
                    if (words[0].Equals("hotkey"))
                        hotkey = words[2];
                }
            }
            reader.Close();
        }

        private void writeConfig()
        {
            StreamWriter writer;
            try
            {
                if (!Directory.Exists(configDir))
                    Directory.CreateDirectory(configDir);
                writer = new StreamWriter(configFile);
            }
            catch (IOException e)
            {
                error = e.Message;
                return;
            }
            writer.WriteLine("[QuickQR]");
            writer.WriteLine("autostart = " + (autostart ? "true" : "false"));
            writer.WriteLine("background = " + (background ? "true" : "false"));
            writer.WriteLine("hotkey = " + hotkey);
            writer.Close();
        }
    }
}