-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettings.cs
More file actions
130 lines (124 loc) · 6.28 KB
/
Settings.cs
File metadata and controls
130 lines (124 loc) · 6.28 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
using System.Text.Json;
using System.IO;
using System;
namespace ChocoButler
{
public class Settings
{
public int CheckIntervalHours { get; set; } = 1; // Default to 1 hour
public bool ShowNotifications { get; set; } = true;
// The 'Start with Windows' setting is not stored here. It reflects the state of the Windows Startup folder.
public bool PeriodicChecksEnabled { get; set; } = true;
}
public static class SettingsManager
{
private static string GetSettingsPath()
{
string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string settingsDir = Path.Combine(appData, "ChocoButler");
Directory.CreateDirectory(settingsDir);
return Path.Combine(settingsDir, "settings-v2.json");
}
public static Settings Load()
{
string path = GetSettingsPath();
if (File.Exists(path))
{
try
{
string json = File.ReadAllText(path);
return JsonSerializer.Deserialize<Settings>(json) ?? new Settings();
}
catch
{
// If there's an error reading the file, return defaults
return new Settings();
}
}
return new Settings();
}
public static void Save(Settings settings)
{
string path = GetSettingsPath();
string json = JsonSerializer.Serialize(settings, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(path, json);
}
// The 'Start with Windows' setting is not stored in the settings file. It reflects the state of the Windows Startup folder.
public static bool IsStartupShortcutPresent()
{
string? startupFolder = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
if (string.IsNullOrEmpty(startupFolder))
{
System.Windows.Forms.MessageBox.Show("Could not determine the Windows Startup folder.", "ChocoButler", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
return false;
}
string shortcutPath = Path.Combine(startupFolder, "ChocoButler.lnk");
return File.Exists(shortcutPath);
}
public static void SetStartupShortcut(bool enable)
{
try
{
string? startupFolder = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
if (string.IsNullOrEmpty(startupFolder))
{
System.Windows.Forms.MessageBox.Show("Could not determine the Windows Startup folder. Cannot create startup shortcut.", "ChocoButler", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
return;
}
string shortcutPath = Path.Combine(startupFolder, "ChocoButler.lnk");
string? exePath = System.Windows.Forms.Application.ExecutablePath;
if (string.IsNullOrEmpty(exePath))
{
System.Windows.Forms.MessageBox.Show("Could not determine the application executable path. Cannot create startup shortcut.", "ChocoButler", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
return;
}
if (enable)
{
Type? shellType = Type.GetTypeFromProgID("WScript.Shell");
if (shellType == null)
{
System.Windows.Forms.MessageBox.Show("Could not access WScript.Shell COM object. Cannot create startup shortcut.", "ChocoButler", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
return;
}
try
{
dynamic? shell = Activator.CreateInstance(shellType);
if (shell == null)
{
System.Windows.Forms.MessageBox.Show("Failed to create WScript.Shell COM object. Cannot create startup shortcut.", "ChocoButler", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
return;
}
dynamic? shortcut = shell.CreateShortcut(shortcutPath);
if (shortcut == null)
{
System.Windows.Forms.MessageBox.Show("Failed to create shortcut object. Cannot create startup shortcut.", "ChocoButler", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
return;
}
shortcut.TargetPath = exePath;
shortcut.WorkingDirectory = Path.GetDirectoryName(exePath);
shortcut.Save();
Console.WriteLine($"[{DateTime.Now:G}] Created startup shortcut: {shortcutPath}");
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show($"Failed to create startup shortcut: {ex.Message}", "ChocoButler", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
Console.WriteLine($"[{DateTime.Now:G}] Failed to create startup shortcut: {ex.Message}");
}
}
else
{
if (File.Exists(shortcutPath))
{
File.Delete(shortcutPath);
Console.WriteLine($"[{DateTime.Now:G}] Removed startup shortcut: {shortcutPath}");
}
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show($"Error updating startup shortcut: {ex.Message}", "ChocoButler", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
Console.WriteLine($"[{DateTime.Now:G}] Error updating startup shortcut: {ex.Message}");
}
}
}
}