namespace Titano.Core.Windows.Shell;
///
/// Where the desktop programs keep their data (BRIEF ยง6.2): %LOCALAPPDATA%\Titano, or .\data next to the
/// executable in portable mode. SQLite corrupts inside synchronised folders, so a data root under OneDrive is
/// reported by and refused by .
///
public sealed class AppPaths
{
public const string ProductFolderName = "Titano";
public const string DatabaseFileName = "titano.db";
private AppPaths(string dataRoot, bool isPortable, string? syncedFolder)
{
DataRoot = dataRoot;
IsPortable = isPortable;
SyncedFolder = syncedFolder;
}
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");
public string ThumbnailsDirectory => Path.Combine(DataRoot, "thumbnails");
/// The OneDrive sync root containing , or null when the location is safe.
public string? SyncedFolder { get; }
public bool IsInSyncedFolder => SyncedFolder is not null;
/// 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());
internal static AppPaths Resolve(string applicationDirectory, string localAppData, IReadOnlyList syncRoots)
{
var portable = PortableMode.IsEnabled(applicationDirectory);
var root = Path.GetFullPath(portable
? Path.Combine(applicationDirectory, PortableMode.DataFolderName)
: Path.Combine(localAppData, ProductFolderName));
return new AppPaths(root, portable, OneDriveFolders.Containing(root, syncRoots));
}
/// Creates the data, logs, cache and thumbnails folders.
/// The data root lies inside a OneDrive-synced folder.
public void CreateDirectories()
{
if (SyncedFolder is not null)
throw new InvalidOperationException($"The data folder '{DataRoot}' is inside the OneDrive folder '{SyncedFolder}'; the database would be corrupted by synchronisation.");
Directory.CreateDirectory(DataRoot);
Directory.CreateDirectory(LogsDirectory);
Directory.CreateDirectory(CacheDirectory);
Directory.CreateDirectory(ThumbnailsDirectory);
}
}