using System.Text.Json; namespace OpenNest.Data; /// Where the desktop app persists nests: local .nest files or the shared nest server. public enum NestStorageMode { File, Database, } /// /// Storage-mode toggle and nest-server address, stored at %APPDATA%\OpenNest\storage.json. /// Loading never throws; a missing or corrupt file means File mode so existing installs /// keep the original behavior until the operator opts in. /// public sealed class NestStorageSettings { private static readonly JsonSerializerOptions JsonOptions = new() { WriteIndented = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase, PropertyNameCaseInsensitive = true, }; public NestStorageMode Mode { get; set; } = NestStorageMode.File; /// Base URL of the nest server, e.g. http://barge.lan:8090. No trailing slash required. public string ServerUrl { get; set; } = ""; /// /// Servers this PC has used, each with the operator's note on what it is for, so the /// server pickers can offer them by name. Always holds after a load. /// public List Servers { get; set; } = new(); /// True when Database mode is active and a server URL is configured. public bool IsDatabaseMode => Mode == NestStorageMode.Database && !string.IsNullOrWhiteSpace(ServerUrl); /// The saved server at this address, or null. Addresses differing only by case or a trailing slash match. public SavedNestServer? FindServer(string? url) { var key = NormalizeUrl(url); return Servers.Find(s => string.Equals(s.Url, key, StringComparison.OrdinalIgnoreCase)); } /// /// Adds the server to the saved list, or renames it when already there. A null name keeps /// the saved one; an invalid address is ignored. /// public void RememberServer(string? url, string? name) { if (!TryNormalizeServerUrl(url, out var normalized)) return; var index = Servers.FindIndex(s => string.Equals(s.Url, normalized, StringComparison.OrdinalIgnoreCase)); if (index < 0) Servers.Add(new SavedNestServer((name ?? "").Trim(), normalized)); else if (name != null) Servers[index] = Servers[index] with { Name = name.Trim() }; } /// Removes the server from the saved list; true when it was there. public bool ForgetServer(string? url) { var saved = FindServer(url); return saved != null && Servers.Remove(saved); } /// Validate and normalize a nest server base URL without changing storage mode. public static bool TryNormalizeServerUrl(string? url, out string normalized) { normalized = NormalizeUrl(url); if (Uri.TryCreate(normalized, UriKind.Absolute, out var uri) && (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps)) return true; normalized = ""; return false; } /// %APPDATA%\OpenNest\storage.json. public static string DefaultPath => Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "OpenNest", "storage.json"); public static NestStorageSettings Load(string path) { var settings = SettingsFile.Read(path, JsonOptions) ?? new(); settings.ServerUrl = NormalizeUrl(settings.ServerUrl); settings.Servers = CleanServers(settings.Servers); settings.RememberServer(settings.ServerUrl, null); return settings; } /// Writes camelCase JSON, creating the parent directory and retrying IO collisions. public void Save(string path) { var json = JsonSerializer.Serialize( new NestStorageSettings { Mode = Mode, ServerUrl = NormalizeUrl(ServerUrl), Servers = CleanServers(Servers), }, JsonOptions); SettingsFile.Write(path, json); } /// Normalized entries in their saved order, without invalid addresses or repeats. private static List CleanServers(List? servers) { var clean = new NestStorageSettings(); foreach (var server in servers ?? new()) { if (server != null && clean.FindServer(server.Url) == null) clean.RememberServer(server.Url, server.Name ?? ""); } return clean.Servers; } private static string NormalizeUrl(string? url) => (url ?? "").Trim().TrimEnd('/'); } /// A nest server the operator has used, with their note on what it is for. public sealed record SavedNestServer(string Name, string Url) { /// "Name — address", or the address alone when the server has no name. public override string ToString() => string.IsNullOrWhiteSpace(Name) ? Url : $"{Name} — {Url}"; }