-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.cs
More file actions
228 lines (209 loc) · 9.03 KB
/
Copy pathConfig.cs
File metadata and controls
228 lines (209 loc) · 9.03 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace devtime
{
public enum TaskbarColor
{
None,
Green,
Yellow,
Red
}
public enum Hotkey
{
None,
F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12,
MediaPlayPause
}
public static class Config
{
public static uint DatabaseUpdateFrequency { get; set; } = 300;
public static string DatabasePath { get; set; } = "";
public static uint DatabaseExportBufferSize { get; set; } = 30;
public static TaskbarColor StoppedTimerColor { get; set; } = TaskbarColor.None;
public static TaskbarColor RunningTimerColor { get; set; } = TaskbarColor.Green;
public static bool MinimizeToTray { get; set; } = false;
public static Hotkey StartHotkey { get; set; } = Hotkey.None;
public static Hotkey StopHotkey { get; set; } = Hotkey.None;
public static bool DisableDynamicGuiUpdates { get; set; } = true;
public static bool FreeMemoryWhenStopping { get; set; } = false;
public static string? LastSelectedProject { get; set; } = null;
static Config()
{
// Initialize default DatabasePath based on platform
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
DatabasePath = Path.Combine(appData, "devtime", "devtime.db");
}
else
{
DatabasePath = "devtime.db";
}
}
public static string GetDefaultDirectory()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
return Path.Combine(appData, "devtime");
}
else
{
return AppDomain.CurrentDomain.BaseDirectory;
}
}
public static string GetDatabaseAbsolutePath()
{
string path = DatabasePath;
if (string.IsNullOrWhiteSpace(path))
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
path = Path.Combine(appData, "devtime", "devtime.db");
}
else
{
path = "devtime.db";
}
}
if (path.StartsWith("~/"))
{
string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
path = Path.Combine(home, path.Substring(2));
}
if (!Path.IsPathRooted(path))
{
path = Path.Combine(GetDefaultDirectory(), path);
}
return Path.GetFullPath(path);
}
public static string GetConfigFilePath()
{
// INI stored in the same directory as the database
string dbAbsPath = GetDatabaseAbsolutePath();
string? dbDir = Path.GetDirectoryName(dbAbsPath);
if (string.IsNullOrEmpty(dbDir))
{
dbDir = GetDefaultDirectory();
}
return Path.Combine(dbDir, "devtime.ini");
}
public static void Load()
{
// We first try to load config from the default directory to bootstrap finding the DB path
string defaultIni = Path.Combine(GetDefaultDirectory(), "devtime.ini");
string path = defaultIni;
if (!File.Exists(path))
{
// If not found in default directory, check where the DB path resolves to
string dbIni = Path.Combine(Path.GetDirectoryName(GetDatabaseAbsolutePath()) ?? GetDefaultDirectory(), "devtime.ini");
if (File.Exists(dbIni))
{
path = dbIni;
}
else
{
return; // Use defaults
}
}
try
{
var lines = File.ReadAllLines(path);
foreach (var line in lines)
{
var trimmed = line.Trim();
if (trimmed.StartsWith(";") || trimmed.StartsWith("#") || !trimmed.Contains("="))
continue;
int idx = trimmed.IndexOf('=');
string key = trimmed.Substring(0, idx).Trim();
string val = trimmed.Substring(idx + 1).Trim();
switch (key)
{
case nameof(DatabaseUpdateFrequency):
if (uint.TryParse(val, out uint duf)) DatabaseUpdateFrequency = duf;
break;
case nameof(DatabasePath):
DatabasePath = val;
break;
case nameof(DatabaseExportBufferSize):
if (uint.TryParse(val, out uint deb)) DatabaseExportBufferSize = deb;
break;
case nameof(StoppedTimerColor):
if (Enum.TryParse(val, out TaskbarColor stc)) StoppedTimerColor = stc;
break;
case nameof(RunningTimerColor):
if (Enum.TryParse(val, out TaskbarColor rtc)) RunningTimerColor = rtc;
break;
case nameof(MinimizeToTray):
if (bool.TryParse(val, out bool mtt)) MinimizeToTray = mtt;
break;
case nameof(StartHotkey):
if (Enum.TryParse(val, out Hotkey sh)) StartHotkey = sh;
break;
case nameof(StopHotkey):
if (Enum.TryParse(val, out Hotkey sth)) StopHotkey = sth;
break;
case nameof(DisableDynamicGuiUpdates):
if (bool.TryParse(val, out bool ddg)) DisableDynamicGuiUpdates = ddg;
break;
case nameof(FreeMemoryWhenStopping):
if (bool.TryParse(val, out bool fmw)) FreeMemoryWhenStopping = fmw;
break;
case nameof(LastSelectedProject):
LastSelectedProject = string.IsNullOrEmpty(val) || val == "null" ? null : val;
break;
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error loading config: {ex}");
}
}
public static void Save()
{
try
{
string path = GetConfigFilePath();
string? dir = Path.GetDirectoryName(path);
if (!string.IsNullOrEmpty(dir))
{
Directory.CreateDirectory(dir);
}
var sb = new StringBuilder();
sb.AppendLine("; devtime configuration");
sb.AppendLine($"{nameof(DatabaseUpdateFrequency)}={DatabaseUpdateFrequency}");
sb.AppendLine($"{nameof(DatabasePath)}={DatabasePath}");
sb.AppendLine($"{nameof(DatabaseExportBufferSize)}={DatabaseExportBufferSize}");
sb.AppendLine($"{nameof(StoppedTimerColor)}={StoppedTimerColor}");
sb.AppendLine($"{nameof(RunningTimerColor)}={RunningTimerColor}");
sb.AppendLine($"{nameof(MinimizeToTray)}={MinimizeToTray.ToString().ToLower()}");
sb.AppendLine($"{nameof(StartHotkey)}={StartHotkey}");
sb.AppendLine($"{nameof(StopHotkey)}={StopHotkey}");
sb.AppendLine($"{nameof(DisableDynamicGuiUpdates)}={DisableDynamicGuiUpdates.ToString().ToLower()}");
sb.AppendLine($"{nameof(FreeMemoryWhenStopping)}={FreeMemoryWhenStopping.ToString().ToLower()}");
sb.AppendLine($"{nameof(LastSelectedProject)}={(LastSelectedProject ?? "null")}");
File.WriteAllText(path, sb.ToString());
// If default path is different, save there too to help bootstrap
string defaultIni = Path.Combine(GetDefaultDirectory(), "devtime.ini");
if (defaultIni != path)
{
string? defDir = Path.GetDirectoryName(defaultIni);
if (!string.IsNullOrEmpty(defDir))
{
Directory.CreateDirectory(defDir);
}
File.WriteAllText(defaultIni, sb.ToString());
}
}
catch (Exception ex)
{
Console.WriteLine($"Error saving config: {ex}");
}
}
}
}