Jump to content

SongRec/Internal formats/preferences.toml

From Fossplant
Revision as of 04:23, 6 May 2026 by Marin (talk | contribs) (Variables)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The preferences.toml file of SongRec is saved, under Linux, at the ~/.config/songrec/preferences.toml path by default, when using an unsandboxed environment.

It uses the TOML format.

It is written through by the src/core/preferences.rs utility file, which calculates the path of the preferences file through src/utils/filesystem_operations.rs.

Save location

The location for the preferences.toml file is calculated through the obtain_preferences_file_path function of src/utils/filesystem_operations.rs.

Relevant code:

use directories::ProjectDirs;
use std::error::Error;
use std::fs::create_dir_all;

const QUALIFIER: &str = "";
const ORGANIZATION: &str = "SongRec";
const APPLICATION: &str = "SongRec";

static PROJECT_DIRS: LazyLock<ProjectDirs> =
    LazyLock::new(|| ProjectDirs::from(QUALIFIER, ORGANIZATION, APPLICATION).unwrap());

pub fn obtain_preferences_file_path() -> Result<PathBuf, Box<dyn Error>> {
    let mut preferences_file_path = obtain_preferences_directory()?;
    preferences_file_path.push("preferences.toml");
    Ok(preferences_file_path)
}

fn obtain_preferences_directory() -> Result<PathBuf, Box<dyn Error>> {
    let preferences_dir = PROJECT_DIRS.preference_dir();
    if !preferences_dir.exists() {
        create_dir_all(preferences_dir)?;
    }
    Ok(preferences_dir.to_path_buf())
}

See:

Variables

The Preferences structure defined in src/core/preferences.rs holds a list of the variables that can be serialized through the serde TOML plug-in:

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(default)]
pub struct Preferences {
    pub enable_notifications: Option<bool>,
    pub enable_systray: Option<bool>,
    pub enable_mpris: Option<bool>, // Legacy, before setting default to true
    pub enable_mpris_v2: Option<bool>,
    pub no_duplicates: Option<bool>,
    pub buffer_size_secs: Option<u64>,
    pub request_interval_secs: Option<u64>, // Legacy, before increasing default from 4 to 10
    pub request_interval_secs_v2: Option<u64>, // before decreasing from 10 to 8
    pub request_interval_secs_v3: Option<u64>,
    pub current_device_name: Option<String>,
    pub website_search_url: Option<String>,
    pub website_search_text: Option<String>,
}

With respective default values:

impl Default for Preferences {
    fn default() -> Self {
        Preferences {
            enable_notifications: Some(true),
            enable_systray: Some(false),
            enable_mpris: None,
            enable_mpris_v2: Some(true),
            no_duplicates: Some(false),
            buffer_size_secs: Some(12),
            request_interval_secs: None,
            request_interval_secs_v2: None,
            request_interval_secs_v3: Some(8),
            current_device_name: None,
            website_search_url: Some("https://www.youtube.com/results?search_query=".to_string()),
            website_search_text: Some(gettext("Search on YouTube".to_string())),
        }
    }
}