Skip to content

armgit / nota

Public Imported from GitHub
0

Nota — a free DAW for macOS, Windows and Linux. No subscription, no account — just open it and make music.

2.5 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (c) 2026 Egor Khindikaynen (Nota). See LICENSES/ for license terms.
//
// Single source of truth for Nota's per-user data directory, shared by the
// settings, recovery and logging services. Pushed to native at startup via
// NotaEngine.SetDataDir (nota_set_data_dir); the engine itself never infers
// platform paths — besides this value only NOTA_DATA_DIR (tests) is honoured.
// Platform conventions:
//   macOS   -> ~/Library/Application Support/Nota   (unchanged from the mac-only era)
//   Windows -> %APPDATA%\Nota                       (Roaming AppData)
//   Linux   -> $XDG_CONFIG_HOME/nota or ~/.config/nota (lowercase, XDG style)

namespace Nota.Infrastructure;

public static class NotaPaths
{
    /// <summary>The per-user Nota data root, created if missing.</summary>
    public static string DataDir { get; } = ResolveDataDir();

    /// <summary>User home directory.</summary>
    public static string Home => Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

    /// <summary>Ensures and returns a subfolder of <see cref="DataDir"/> (e.g. "logs", "presets").</summary>
    public static string SubDir(string name)
    {
        var dir = Path.Combine(DataDir, name);
        try { Directory.CreateDirectory(dir); } catch { /* best-effort */ }
        return dir;
    }

    private static string ResolveDataDir()
    {
        string root;
        // NOTA_DATA_DIR (tests) relocates the data root; the native plugin host honours it too.
        if (Environment.GetEnvironmentVariable("NOTA_DATA_DIR") is { Length: > 0 } over)
            root = over;
        else if (OperatingSystem.IsWindows())
        {
            // %APPDATA% == Roaming AppData; on Windows SpecialFolder.ApplicationData maps here.
            root = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Nota");
        }
        else if (OperatingSystem.IsMacOS())
        {
            // Preserve the exact mac-only path so existing installs keep their data.
            root = Path.Combine(Home, "Library", "Application Support", "Nota");
        }
        else
        {
            var xdg = Environment.GetEnvironmentVariable("XDG_CONFIG_HOME");
            var baseDir = string.IsNullOrEmpty(xdg) ? Path.Combine(Home, ".config") : xdg;
            root = Path.Combine(baseDir, "nota");
        }

        try { Directory.CreateDirectory(root); } catch { /* best-effort */ }
        return root;
    }
}