Files
Reims-Go/internal/datamigrate/clipboard_settings.go
T

176 lines
3.7 KiB
Go

package datamigrate
import (
"math"
"github.com/Nwflower/reims-go/internal/plugin"
"github.com/Nwflower/reims-go/internal/plugin/builtin"
)
const (
clipDirHostToGuest = 1 << 0
clipDirGuestToHost = 1 << 1
clipDirBoth = clipDirHostToGuest | clipDirGuestToHost
)
// migrateClipboardSettings moves legacy per-VM clipboard settings into the
// plugin's app-scoped settings using restrictive-first merge rules and clears
// the obsolete VMSettings map.
func migrateClipboardSettings(dataRoot string) error {
store := plugin.NewStateStore(plugin.NewPluginsLayout(dataRoot).Root)
st, err := store.Load()
if err != nil {
return err
}
if !migrateClipboardSettingsState(st) {
return nil
}
return store.Save(st)
}
func migrateClipboardSettingsState(st *plugin.StateFile) bool {
if st == nil || st.Plugins == nil {
return false
}
rec := st.Plugins[builtin.IDClipboard]
if rec == nil || len(rec.VMSettings) == 0 {
return false
}
app := rec.AppSettings
ensureApp := func() map[string]any {
if app == nil {
app = map[string]any{}
rec.AppSettings = app
}
return app
}
// Booleans: explicit false in either AppSettings or any VMSettings wins.
for _, key := range []string{"enabled", "sync_images", "sync_files"} {
if appVal, ok := app[key].(bool); ok && !appVal {
continue
}
hasFalse := false
hasTrue := false
for _, vmVals := range rec.VMSettings {
v, ok := vmVals[key].(bool)
if !ok {
continue
}
if !v {
hasFalse = true
} else {
hasTrue = true
}
}
if hasFalse {
ensureApp()[key] = false
} else if hasTrue {
if _, exists := app[key]; !exists {
ensureApp()[key] = true
}
}
}
// Direction: take the intersection of allowed directions across AppSettings
// and all VMSettings. If two VMs restrict to disjoint directions, disable
// sync rather than silently re-enabling a blocked direction.
dirMask := clipDirBoth
hasDir := false
if s, ok := app["direction"].(string); ok {
if m, valid := parseClipDirectionMask(s); valid {
dirMask &= m
hasDir = true
}
}
for _, vmVals := range rec.VMSettings {
s, ok := vmVals["direction"].(string)
if !ok {
continue
}
if m, valid := parseClipDirectionMask(s); valid {
dirMask &= m
hasDir = true
}
}
if hasDir {
if dirStr, ok := formatClipDirectionMask(dirMask); ok {
ensureApp()["direction"] = dirStr
} else {
ensureApp()["enabled"] = false
}
}
// Size limits: smallest explicit positive value wins.
for _, key := range []string{"max_image_mb", "max_file_mb"} {
best := 0
hasLimit := false
if v, ok := positiveIntSetting(app[key]); ok {
best = v
hasLimit = true
}
for _, vmVals := range rec.VMSettings {
if v, ok := positiveIntSetting(vmVals[key]); ok {
if !hasLimit || v < best {
best = v
hasLimit = true
}
}
}
if hasLimit {
ensureApp()[key] = best
}
}
rec.VMSettings = nil
return true
}
func parseClipDirectionMask(dir string) (int, bool) {
switch dir {
case "bidirectional":
return clipDirBoth, true
case "host-to-guest":
return clipDirHostToGuest, true
case "guest-to-host":
return clipDirGuestToHost, true
default:
return 0, false
}
}
func formatClipDirectionMask(mask int) (string, bool) {
switch mask {
case clipDirBoth:
return "bidirectional", true
case clipDirHostToGuest:
return "host-to-guest", true
case clipDirGuestToHost:
return "guest-to-host", true
default:
return "", false
}
}
func positiveIntSetting(raw any) (int, bool) {
switch v := raw.(type) {
case int:
if v > 0 {
return v, true
}
case int64:
if v > 0 {
return int(v), true
}
case float64:
if v > 0 && !math.IsNaN(v) && !math.IsInf(v, 0) {
n := int(math.Round(v))
if n > 0 {
return n, true
}
}
}
return 0, false
}