CI / Build and test (push) Successful in 11m36s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 12s
Release / Build, test and publish (push) Successful in 14m56s
CI / PostgreSQL 16 integration (push) Successful in 10m11s
CI / PostgreSQL 17 integration (push) Successful in 10m15s
CI / Docker image (push) Successful in 45s
Release / Server image (push) Successful in 4m55s
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
71 lines
3.5 KiB
C#
71 lines
3.5 KiB
C#
using Chronos.Core.IO;
|
|
|
|
namespace Chronos.Core.Windows.Shell;
|
|
|
|
/// <summary>
|
|
/// Where the desktop programs keep their data (BRIEF §6.2, <see cref="DataLocation"/>): <c>%LOCALAPPDATA%\Chronos</c>, or
|
|
/// <c>.\data</c> next to the executable in portable mode. SQLite corrupts inside synchronised folders, so a portable
|
|
/// copy in a OneDrive folder keeps its data in <c>%LOCALAPPDATA%\Chronos</c> instead (<see cref="PortableSyncRoot"/>
|
|
/// names that folder to tell the user, <see cref="EarlierPortableData"/> the data it kept there before), and a data
|
|
/// root still under OneDrive is reported by <see cref="SyncedFolder"/>.
|
|
/// </summary>
|
|
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;
|
|
}
|
|
|
|
/// <summary>A portable copy, also when its data had to go to <c>%LOCALAPPDATA%\Chronos</c>.</summary>
|
|
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");
|
|
|
|
/// <summary>The OneDrive sync root containing <see cref="DataRoot"/>, or null when the location is safe.</summary>
|
|
public string? SyncedFolder { get; }
|
|
|
|
public bool IsInSyncedFolder => SyncedFolder is not null;
|
|
|
|
/// <summary>The OneDrive sync root a portable copy runs from, whose data is in <c>%LOCALAPPDATA%\Chronos</c> instead; null otherwise.</summary>
|
|
public string? PortableSyncRoot { get; }
|
|
|
|
/// <summary>
|
|
/// The <c>.\data</c> 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.
|
|
/// </summary>
|
|
public string? EarlierPortableData { get; }
|
|
|
|
/// <summary>Paths for this process: the executable's folder decides portable mode.</summary>
|
|
public static AppPaths Resolve() => Resolve(AppContext.BaseDirectory, KnownFolders.LocalAppData);
|
|
|
|
/// <summary>Paths for an executable in <paramref name="applicationDirectory"/>, checked against the user's OneDrive roots.</summary>
|
|
public static AppPaths Resolve(string applicationDirectory, string localAppData) =>
|
|
Resolve(applicationDirectory, localAppData, OneDriveFolders.SyncRoots());
|
|
|
|
/// <summary>Paths for an executable in <paramref name="applicationDirectory"/>, checked against <paramref name="syncRoots"/>.</summary>
|
|
public static AppPaths Resolve(string applicationDirectory, string localAppData, IReadOnlyCollection<string> 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);
|
|
}
|
|
}
|