using Chronos.Core.IO; namespace Chronos.Core.Windows.Shell; /// /// Where the desktop programs keep their data (BRIEF ยง6.2, ): %LOCALAPPDATA%\Chronos, or /// .\data next to the executable in portable mode. SQLite corrupts inside synchronised folders, so a portable /// copy in a OneDrive folder keeps its data in %LOCALAPPDATA%\Chronos instead ( /// names that folder to tell the user, the data it kept there before), and a data /// root still under OneDrive is reported by . /// public sealed class AppPaths { public const string ProductFolderName = DataLocation.ProductFolderName; public const string DatabaseFileName = "chronos.db"; public const string SettingsFileName = "settings.json"; private AppPaths(DataLocation location, string? earlierPortableData) { DataRoot = location.Root; IsPortable = location.IsPortable; SyncedFolder = location.SyncedFolder; PortableSyncRoot = location.PortableSyncRoot; EarlierPortableData = earlierPortableData; } /// A portable copy, also when its data had to go to %LOCALAPPDATA%\Chronos. public bool IsPortable { get; } public string DataRoot { get; } public string DatabasePath => Path.Combine(DataRoot, DatabaseFileName); public string LogsDirectory => Path.Combine(DataRoot, "logs"); public string CacheDirectory => Path.Combine(DataRoot, "cache"); /// The OneDrive sync root containing , or null when the location is safe. public string? SyncedFolder { get; } public bool IsInSyncedFolder => SyncedFolder is not null; /// The OneDrive sync root a portable copy runs from, whose data is in %LOCALAPPDATA%\Chronos instead; null otherwise. public string? PortableSyncRoot { get; } /// /// The .\data folder of a portable copy now in a OneDrive folder when it still holds the index or the settings /// of an earlier start: nothing moves or deletes it, and the user is told where it is. Null otherwise. /// public string? EarlierPortableData { get; } /// Paths for this process: the executable's folder decides portable mode. public static AppPaths Resolve() => Resolve(AppContext.BaseDirectory, KnownFolders.LocalAppData); /// Paths for an executable in , checked against the user's OneDrive roots. public static AppPaths Resolve(string applicationDirectory, string localAppData) => Resolve(applicationDirectory, localAppData, OneDriveFolders.SyncRoots()); /// Paths for an executable in , checked against . public static AppPaths Resolve(string applicationDirectory, string localAppData, IReadOnlyCollection syncRoots) { var location = DataLocation.Choose(applicationDirectory, PortableMode.IsEnabled(applicationDirectory), localAppData, syncRoots); var beside = Path.GetFullPath(Path.Combine(applicationDirectory, PortableMode.DataFolderName)); var earlier = location.PortableSyncRoot is not null && (File.Exists(Path.Combine(beside, DatabaseFileName)) || File.Exists(Path.Combine(beside, SettingsFileName))) ? beside : null; return new AppPaths(location, earlier); } }