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