subspace_shell/theme/new_install.rs
1//! A product's theme for a new install, recorded as the saved theme at the
2//! desktop app's first start. Once recorded it is the user's choice like any
3//! other, so a later change of the product's default never moves it, and an
4//! install with no recorded theme keeps showing the product's default.
5
6use std::io;
7use std::path::Path;
8
9use subspace_core::state::State;
10use subspace_product::{ProductContext, ValidatedProduct};
11
12use super::THEME_PREF;
13use super::loader::ProductThemes;
14use crate::product::ProductFiles;
15
16/// Records `theme` as the saved theme when `context`'s product has neither a
17/// config file nor a state file: a new install. Returns whether it recorded.
18/// `product_themes` are the app's own theme documents
19/// (`ProductBuild.product_themes`, `&[]` for none), which `theme` may be a
20/// variant of. An existing install, including one whose files are unreadable
21/// or malformed, is left alone. Nothing is written, and an error returned,
22/// when a new install's product themes fail the checks startup makes, when
23/// `theme` is not a variant of the product's theme catalogue, or when the
24/// files' folder cannot be read, so a new install cannot be told from an
25/// existing one. It first names the data directory after the context's
26/// binary, as `prepare_context` does, and returns that error, writing
27/// nothing, when a data path was read under another name before. Only the
28/// desktop app calls this, before it starts; command-line and headless runs
29/// must not.
30// qual:api (apps call it from their entrypoint, before the desktop starts)
31pub fn record_new_install_theme(
32 context: &ProductContext,
33 theme: &str,
34 product_themes: ProductThemes,
35) -> Result<bool, String> {
36 crate::startup::name_data_directory(context.product.binary_name.as_str())?;
37 let files = ProductFiles::for_context(context);
38 let new = is_new_install(&files).map_err(|error| file_error(&files, &error))?;
39 new.then(|| record(&context.product, &files, theme, product_themes))
40 .transpose()
41 .map(|recorded| recorded.is_some())
42}
43
44/// Saves `theme`, a variant of `product`'s catalogue, into a new state file,
45/// once `documents` pass startup's checks.
46fn record(
47 product: &ValidatedProduct,
48 files: &ProductFiles,
49 theme: &str,
50 documents: ProductThemes,
51) -> Result<(), String> {
52 super::loader::validate_product_themes(product, documents)?;
53 super::loader::require_catalogue_variant(product, "new install theme", theme, documents)?;
54 save_theme(&files.state, theme).map_err(|error| file_error(files, &error))
55}
56
57fn save_theme(file: &Path, theme: &str) -> io::Result<()> {
58 let mut state = State::default();
59 state.set(THEME_PREF, theme);
60 state.save(file)
61}
62
63fn file_error(files: &ProductFiles, error: &io::Error) -> String {
64 format!(
65 "could not record the new install's theme in {}: {error}",
66 files.state.display()
67 )
68}
69
70/// True when neither the config file nor the state file exists. Both are
71/// checked without following links, so a dangling link counts as a file.
72fn is_new_install(files: &ProductFiles) -> io::Result<bool> {
73 Ok(absent(&files.config)? && absent(&files.state)?)
74}
75
76/// Whether nothing is at `path`. An error other than "not found", such as an
77/// unreadable folder, is returned rather than guessed at.
78fn absent(path: &Path) -> io::Result<bool> {
79 match std::fs::symlink_metadata(path) {
80 Ok(_) => Ok(false),
81 Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(true),
82 Err(error) => Err(error),
83 }
84}