-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettings.cs
More file actions
212 lines (188 loc) · 6.86 KB
/
Copy pathSettings.cs
File metadata and controls
212 lines (188 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using CrystalDock.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace CrystalDock
{
public class Settings
{
private Dictionary<string, Dictionary<string, string>> sections;
public static Settings? settingsInstance = null;
public Settings(string filePath)
{
sections = new Dictionary<string, Dictionary<string, string>>();
LoadSettings(filePath);
settingsInstance = this;
}
public void UpdateEntry(string key, IconInfo iconInfo)
{
if (sections.ContainsKey(key))
{
sections[key] = new Dictionary<string, string>()
{
{ "IconImage", iconInfo.IconImage },
{ "IconImageHover", iconInfo.IconImageHover },
{ "Action", iconInfo.Action },
};
}
}
public void RemoveEntry(string key)
{
if (IsDockLocked) return;
if (sections.TryGetValue(key, out Dictionary<string, string>? v))
{
if (File.Exists(Properties.Resources.IconFolder + v["IconImage"]))
{
File.Delete(Properties.Resources.IconFolder + v["IconImage"]);
File.Delete(Properties.Resources.IconFolder + v["IconImageHover"]);
}
sections.Remove(key);
}
MainWindow._instance?.LoadButtons();
}
public void AddEntry(IconInfo info)
{
sections.Add(NextEntry, info.ToDictionary());
}
public string NextEntry
{
get
{
int count = GetIconEntries().Keys.Where(x => x.StartsWith("Icon")).Count();
while (sections.TryGetValue("Icon" + count.ToString(), out var value)) count++;
return "Icon" + count.ToString();
}
}
public bool IsDockLocked => GetValue<bool>("Global", "PositionLocked");
public void ToggleDockPositionLock()
{
if(IsDockLocked == true)
sections["Global"]["PositionLocked"] = "false";
else
sections["Global"]["PositionLocked"] = "true";
}
private void LoadSettings(string filePath)
{
if(!File.Exists(filePath))
{
File.CreateText(filePath).Close();
List<string> _lines = new List<string>
{
"[Global]",
"PositionLocked=" + Properties.Settings.Default.PositionLocked,
"IconSize=" + Properties.Settings.Default.IconSize,
"IconMargins=" + Properties.Settings.Default.IconMargins
};
File.WriteAllLines(filePath, _lines);
}
string[] lines = File.ReadAllLines(filePath, Encoding.UTF8);
string? currentSection = null;
foreach (string line in lines)
{
if (string.IsNullOrWhiteSpace(line) || line.StartsWith(";"))
continue;
if (line.StartsWith("[") && line.EndsWith("]"))
{
currentSection = line.Substring(1, line.Length - 2);
sections[currentSection] = new Dictionary<string, string>();
}
else if (currentSection != null)
{
string[] parts = line.Split(new char[] { '=' }, 2);
if (parts.Length == 2)
{
string key = parts[0].Trim();
string value = parts[1].Trim().Trim('\"');
sections[currentSection][key] = value;
}
}
}
}
public T? GetValue<T>(string section, string key)
{
if(sections.TryGetValue(section, out var properties) &&
properties.TryGetValue(key, out var value))
{
return (T?)TypeDescriptor.GetConverter(typeof(T))?.ConvertFromString(value);
}
return default(T?);
}
public Dictionary<string, IconInfo> GetIconEntries()
{
Dictionary<string, IconInfo> rtn = new Dictionary<string, IconInfo>();
var iconEntries = sections.Where(x => x.Key != "Global");
foreach(var iconEntry in iconEntries)
{
rtn.Add(iconEntry.Key, new IconInfo
{
IconImage = iconEntry.Value["IconImage"],
IconImageHover = iconEntry.Value["IconImageHover"],
Action = iconEntry.Value["Action"]
});
}
return rtn;
}
public IconInfo? GetIconInfo(string key)
{
if(sections.TryGetValue(key, out var value))
{
return IconInfo.FromDictionary(value);
}
return null;
}
public void SaveSettings()
{
List<string> lines = new List<string>();
foreach (var section in sections)
{
lines.Add($"[{section.Key}]");
foreach (var property in section.Value)
{
if (property.Key == "IconImage" || property.Key == "IconImageHover" || property.Key == "Action")
{
lines.Add($"{property.Key}=\"{property.Value}\"");
}
else
{
lines.Add($"{property.Key}={property.Value}");
}
}
}
File.WriteAllLines(Properties.Resources.SettingsIniFile, lines, Encoding.UTF8);
}
}
public class IconInfo
{
public string IconImage { get; set; } = "";
public string IconImageHover { get; set; } = "";
public string Action { get; set; } = "";
public static IconInfo FromDictionary(Dictionary<string, string> dict)
{
IconInfo rtn = new IconInfo();
foreach(var kvp in dict)
{
switch(kvp.Key)
{
case "IconImage": rtn.IconImage = kvp.Value; break;
case "IconImageHover": rtn.IconImageHover = kvp.Value; break;
case "Action": rtn.Action = kvp.Value; break;
}
}
return rtn;
}
public Dictionary<string, string> ToDictionary()
{
return new Dictionary<string, string>()
{
{ "IconImage", IconImage },
{ "IconImageHover", IconImageHover },
{ "Action", Action }
};
}
}
}