using System.IO;
namespace Titano.App.Infrastructure;
///
/// Where the client keeps its data (BRIEF ยง6.2): %LOCALAPPDATA%\Titano, or .\data next to the
/// executable when portable.txt exists. Never inside Documents/OneDrive (SQLite corrupts in synced folders).
///
public sealed class AppEnvironment
{
private AppEnvironment(string dataDirectory, bool portable, bool isCapture)
{
DataDirectory = dataDirectory;
IsPortable = portable;
IsCapture = isCapture;
Directory.CreateDirectory(DataDirectory);
Directory.CreateDirectory(LogDirectory);
Directory.CreateDirectory(CacheDirectory);
}
public string DataDirectory { get; }
public bool IsPortable { get; }
/// A throw-away environment for --capture: nothing of the user's data is read or written.
public bool IsCapture { get; }
public string LogDirectory => Path.Combine(DataDirectory, "logs");
public string CacheDirectory => Path.Combine(DataDirectory, "cache");
public string BackupDirectory => Path.Combine(DataDirectory, "backups");
public string DatabasePath => Path.Combine(DataDirectory, "titano.db");
public string SettingsPath => Path.Combine(DataDirectory, "settings.json");
public static AppEnvironment Detect()
{
var exeDir = AppContext.BaseDirectory;
if (File.Exists(Path.Combine(exeDir, "portable.txt")))
return new AppEnvironment(Path.Combine(exeDir, "data"), portable: true, isCapture: false);
var local = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
return new AppEnvironment(Path.Combine(local, "Titano"), portable: false, isCapture: false);
}
/// A fresh temporary data folder for screenshots and smoke tests; delete it with .
public static AppEnvironment ForCapture() =>
new(Path.Combine(Path.GetTempPath(), "titano-capture-" + Guid.CreateVersion7().ToString("N")), portable: false, isCapture: true);
/// Removes the temporary data folder of a capture environment (never a real one).
public void DeleteCaptureData()
{
if (!IsCapture) return;
try
{
Directory.Delete(DataDirectory, recursive: true);
}
catch (IOException)
{
// Files still open (log, database) are left to the OS temp cleanup.
}
catch (UnauthorizedAccessException)
{
}
}
}