using Titano.Core.IO;
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 portable
/// copy in a OneDrive folder keeps its data in %LOCALAPPDATA%\Titano instead (
/// names that folder to tell the user, the data it kept there before), and a data
/// root still under OneDrive is reported by and refused by .
///
public sealed class AppPaths
{
public const string ProductFolderName = DataLocation.ProductFolderName;
public const string DatabaseFileName = "titano.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%\Titano.
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;
/// The OneDrive sync root a portable copy runs from, whose data is in %LOCALAPPDATA%\Titano 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);
}
/// 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);
}
}