-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreferences.go
More file actions
89 lines (75 loc) · 2.05 KB
/
preferences.go
File metadata and controls
89 lines (75 loc) · 2.05 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
package main
import (
"encoding/json"
"os"
"path/filepath"
)
type Preferences struct {
LastConfigPath string `json:"lastConfigPath"`
LastIpAddress string `json:"lastIpAddress"`
LastVideoSourceId string `json:"lastVideoSourceId"`
LastVideoSourceLabel string `json:"lastVideoSourceLabel"`
}
func getPreferencesPath() (string, error) {
configDir, err := os.UserConfigDir()
if err != nil {
return "", err
}
return filepath.Join(configDir, "Folje", "preferences.json"), nil
}
func loadPreferences() Preferences {
path, err := getPreferencesPath()
if err != nil {
LogError("Failed to get preferences path: %s", err.Error())
return Preferences{}
}
data, err := os.ReadFile(path)
if err != nil {
// File doesn't exist or can't be read - return empty preferences
return Preferences{}
}
var prefs Preferences
if err := json.Unmarshal(data, &prefs); err != nil {
LogError("Failed to parse preferences: %s (content: %s)", err.Error(), string(data))
return Preferences{}
}
return prefs
}
func savePreferences(prefs Preferences) error {
path, err := getPreferencesPath()
if err != nil {
return err
}
// Create directory if it doesn't exist
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
data, err := json.MarshalIndent(prefs, "", " ")
if err != nil {
return err
}
return os.WriteFile(path, data, 0644)
}
func (a *App) updateLastConfigPath(path string) {
prefs := loadPreferences()
prefs.LastConfigPath = path
if err := savePreferences(prefs); err != nil {
LogError("Failed to save preferences: %s", err.Error())
}
}
func (a *App) updateLastIpAddress(ip string) {
prefs := loadPreferences()
prefs.LastIpAddress = ip
if err := savePreferences(prefs); err != nil {
LogError("Failed to save preferences: %s", err.Error())
}
}
func (a *App) updateLastVideoSource(id, label string) {
prefs := loadPreferences()
prefs.LastVideoSourceId = id
prefs.LastVideoSourceLabel = label
if err := savePreferences(prefs); err != nil {
LogError("Failed to save preferences: %s", err.Error())
}
}