comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:aca8706f4eec
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Text;
5
6 /*
7 * Copyright (c) 2014 Brad Greco <brad@bgreco.net>
8 */
9 namespace QuickQR
10 {
11 class Settings
12 {
13 public String error;
14 private String configFile;
15 private String configDir;
16
17 public bool autostart = true;
18 public bool background = true;
19 public String hotkey = "";
20
21 public Settings()
22 {
23 String appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
24 configDir = Path.Combine(appData, "QuickQR");
25 configFile = Path.Combine(configDir, "quickqr.ini");
26
27 readConfig();
28 }
29
30 public void setAutostart(bool autostart)
31 {
32 this.autostart = autostart;
33 writeConfig();
34 }
35
36 public void setBackground(bool background)
37 {
38 this.background = background;
39 writeConfig();
40 }
41
42 public void setHotkey(KeyBinding hotkey)
43 {
44 this.hotkey = hotkey.comboString();
45 writeConfig();
46 }
47
48 private void readConfig()
49 {
50 StreamReader reader;
51 try
52 {
53 reader = new StreamReader(configFile);
54 }
55 catch (IOException e)
56 {
57 error = e.Message;
58 return;
59 }
60 String line;
61 while ((line = reader.ReadLine()) != null)
62 {
63 String[] words = line.Split(null, 3);
64 if(words.Length >= 3 && words[1].Equals("="))
65 {
66 if (words[0].Equals("autostart"))
67 autostart = words[2].Equals("true");
68 if (words[0].Equals("background"))
69 background = words[2].Equals("true");
70 if (words[0].Equals("hotkey"))
71 hotkey = words[2];
72 }
73 }
74 reader.Close();
75 }
76
77 private void writeConfig()
78 {
79 StreamWriter writer;
80 try
81 {
82 if (!Directory.Exists(configDir))
83 Directory.CreateDirectory(configDir);
84 writer = new StreamWriter(configFile);
85 }
86 catch (IOException e)
87 {
88 error = e.Message;
89 return;
90 }
91 writer.WriteLine("[QuickQR]");
92 writer.WriteLine("autostart = " + (autostart ? "true" : "false"));
93 writer.WriteLine("background = " + (background ? "true" : "false"));
94 writer.WriteLine("hotkey = " + hotkey);
95 writer.Close();
96 }
97 }
98 }