-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Configuration.cs
132 lines (112 loc) · 3.97 KB
/
Configuration.cs
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
using System.Collections.Generic;
using Godot;
using OpenScadGraphEditor.Widgets;
using Serilog;
namespace OpenScadGraphEditor
{
public class Configuration
{
private const string ConfigFolder = "user://openscad_graph_editor";
private const string ConfigPath = ConfigFolder+"/openscad_graph_editor.cfg";
private readonly ConfigFile _configFile = new ConfigFile();
public void Load()
{
if (_configFile.Load(ConfigPath) != Error.Ok)
{
Log.Information("Cannot read config file, starting with clean slate");
_configFile.Clear();
}
}
public void SetEditorScalePercent(int editorScale)
{
_configFile.SetValue("editor", "scale", editorScale);
Save();
}
public int GetEditorScalePercent()
{
return (int) _configFile.GetValue("editor", "scale", 100);
}
public void SetOpenScadPath(string openScadPath)
{
_configFile.SetValue("editor", "open_scad_path", openScadPath);
Save();
}
public string GetOpenScadPath()
{
return (string) _configFile.GetValue("editor", "open_scad_path", "");
}
public void SetNumberOfBackups(int numberOfBackups)
{
_configFile.SetValue("editor", "number_of_backups", numberOfBackups);
Save();
}
public int GetNumberOfBackups()
{
return (int) _configFile.GetValue("editor", "number_of_backups", 5);
}
public string SetDefaultPreamble(string defaultPreamble)
{
_configFile.SetValue("editor", "default_preamble", defaultPreamble);
Save();
return defaultPreamble;
}
public string GetDefaultPreamble()
{
return (string) _configFile.GetValue("editor", "default_preamble", "");
}
private void Save()
{
var directory = new Directory();
directory.MakeDirRecursive(ConfigFolder);
var error = _configFile.Save(ConfigPath);
if (error != Error.Ok)
{
NotificationService.ShowError("Could not save config file. Check log for details.");
Log.Warning("Couldn't save config file to {Path} -> Error {Error}", ConfigPath, error);
}
}
public void AddRecentFile(string filePath)
{
var recentFiles = GetRecentFiles();
var indexOf = recentFiles.IndexOf(filePath);
if (indexOf != -1)
{
// swap with first
recentFiles[indexOf] = recentFiles[0];
recentFiles[0] = filePath;
}
else
{
// add to beginning
recentFiles.Insert(0, filePath);
// remove last if too many
if (recentFiles.Count > 10)
{
recentFiles.RemoveAt(10);
}
}
// now serialize this back into the config file
_configFile.SetValue("recent_files", "count", recentFiles.Count);
for (var i = 0; i < recentFiles.Count; i++)
{
_configFile.SetValue("recent_files", $"file_{i}", recentFiles[i]);
}
Save();
}
public List<string> GetRecentFiles()
{
var result = new List<string>();
var count = (int) _configFile.GetValue("recent_files", "count", 0);
for (var i = 0; i < count; i++)
{
var key = $"file_{i}";
var value = (string) _configFile.GetValue("recent_files", key, "");
if (!value.Empty())
{
result.Add(value);
}
}
return result;
}
}
}