A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.
Rust archive-managerdesktop-appdual-paneexplorer-alternativefile-browserfile-explorerfile-managerfluent-designgpu-acceleratedgpuimacosmicaproductivityquick-lookrustrust-guitabswindowswindows-11windows-explorer
//! Settings › Advanced › Export / Import settings: the settings file made to travel to another PC (issue 432).
//!
//! Two things a plain copy of `settings.toml` got wrong there:
//! - "rustyfiles in Open / Save dialogs" lives in Windows' registry, not in the file, so an export left it out and the other PC kept
//! its own (on). The export writes it as `open_save_dialogs`, the import sets it.
//! - Paths of the user's own profile (`C:\Users\me\Downloads`, the pinned folders above all) named a folder only this PC has. The
//! export writes them as `%USERPROFILE%\Downloads`, and the import puts the profile of the PC it runs on back in.
/// The key the Open / Save dialog choice is written under in an exported file.
pub const DIALOGS_KEY: &str = "open_save_dialogs";
/// Settings that name a program rustyfiles starts, or a file or folder it loads from wherever the value points: a settings file from
/// someone else must not change them without the user saying so (a private security report: an imported file set the Git program,
/// which runs as soon as a Git folder is browsed). [`changed_programs`] finds them in a file; the import asks first.
pub const PROGRAM_KEYS: &[&str] = &["git_path", "ide_path", "terminal_path", "text_editor_path", "open_rules", "toolbar_commands", "app_icon", "theme_folder"];
/// What this PC keeps to itself: the history of searches and of the folders open, the sessions, the folders apps' dialogs start in,
/// the weather city, and Ferry's name and inbox. Left out of an export (a private security report: an exported file shared with others
/// carried them), and kept as they are at an import.
pub const PRIVATE_KEYS: &[&str] = &[
"search_history",
"session_tabs",
"session_panes",
"session_active",
"saved_sessions",
"dialog_folders",
"dock_weather_city",
"ferry_name",
"ferry_inbox",
"recent_cleared_ms",
"window",
];
/// The keys of [`PROGRAM_KEYS`] whose value in `imported` is another than in `current` (missing counts as empty).
pub fn changed_programs(imported: &toml::Table, current: &toml::Table) -> Vec<&'static str> {
PROGRAM_KEYS.iter().copied().filter(|k| imported.get(*k).is_some_and(|v| Some(v) != current.get(*k))).collect()
}
const PROFILE_VAR: &str = "%USERPROFILE%";
/// `value` with a leading `profile` (any letter case, followed by the end or a `\`) written as `%USERPROFILE%`.
fn to_portable(value: &str, profile: &str) -> String {
let p = profile.trim_end_matches('\\');
if p.is_empty() || value.len() < p.len() || !value.is_char_boundary(p.len()) {
return value.to_string();
}
let (head, rest) = value.split_at(p.len());
if head.eq_ignore_ascii_case(p) && (rest.is_empty() || rest.starts_with('\\')) { format!("{PROFILE_VAR}{rest}") } else { value.to_string() }
}
/// `value` with a leading `%USERPROFILE%` (any letter case) replaced by `profile`.
fn from_portable(value: &str, profile: &str) -> String {
let n = PROFILE_VAR.len();
// (the variable as a whole path part: "%USERPROFILE%x" is no profile path)
if value.len() >= n && value.is_char_boundary(n) && value[..n].eq_ignore_ascii_case(PROFILE_VAR) && (value.len() == n || value[n..].starts_with('\\')) {
format!("{}{}", profile.trim_end_matches('\\'), &value[n..])
} else {
value.to_string()
}
}
/// Every string in `v`, at any depth, passed through `f`.
fn map_strings(v: &mut toml::Value, f: &dyn Fn(&str) -> String) {
match v {
toml::Value::String(s) => *s = f(s),
toml::Value::Array(a) => a.iter_mut().for_each(|x| map_strings(x, f)),
toml::Value::Table(t) => t.iter_mut().for_each(|(_, x)| map_strings(x, f)),
_ => {}
}
}
/// The text of an exported settings file: the settings with the profile's paths made portable, and the Open / Save dialog choice.
pub fn export_text(settings: &toml::Table, dialogs_on: bool, profile: &str) -> Result<String, toml::ser::Error> {
let mut t = settings.clone();
for k in PRIVATE_KEYS {
t.remove(*k);
}
let mut v = toml::Value::Table(t);
map_strings(&mut v, &|s| to_portable(s, profile));
let toml::Value::Table(mut t) = v else { unreachable!() };
t.insert(DIALOGS_KEY.into(), toml::Value::Boolean(dialogs_on));
toml::to_string_pretty(&t)
}
/// An exported file read back: the settings text with this PC's profile put in, and the Open / Save dialog choice when the file has
/// one (files exported before 1.0.5 do not).
pub fn import_text(text: &str, profile: &str) -> Result<(String, Option<bool>), toml::de::Error> {
let mut t: toml::Table = toml::from_str(text)?;
let dialogs = t.remove(DIALOGS_KEY).and_then(|v| v.as_bool());
for k in PRIVATE_KEYS {
t.remove(*k);
}
let mut v = toml::Value::Table(t);
map_strings(&mut v, &|s| from_portable(s, profile));
Ok((toml::to_string(&v).unwrap_or_default(), dialogs))
}
/// This PC's profile folder (`%USERPROFILE%`), or nothing to replace when it is not set.
pub fn profile() -> String {
std::env::var("USERPROFILE").unwrap_or_default()
}
/// Puts an imported settings text in place. `take_programs` false keeps this PC's programs ([`PROGRAM_KEYS`]) and leaves the Open /
/// Save dialogs off when they are off. This PC's window, session and private things always stay.
///
/// [`PROGRAM_KEYS`]: crate::settings_transfer::PROGRAM_KEYS
pub fn apply_import(text: String, dialogs: Option<bool>, take_programs: bool, cx: &mut gpui_kit::App) {
let classes = rf_win::dialog_reg::CLASSES;
// the file's Open / Save dialog choice, when it has one (issue 432); switching them on only when the programs were taken
if let Some(want) = dialogs
&& want != rf_win::dialog_reg::is_enabled(classes)
&& (!want || take_programs)
{
let r = if want { crate::dialog_registration(crate::DialogReg::Enable).map(|_| ()) } else { rf_win::dialog_reg::disable(classes) };
if let Err(e) = r {
log::warn!("import: Open / Save dialogs: {e:#}");
}
crate::FILE_DIALOGS.store(rf_win::dialog_reg::is_enabled(classes), std::sync::atomic::Ordering::Relaxed);
}
if let Ok(new) = crate::settings::Settings::parse_lenient_pub(&text) {
crate::settings::update(cx, |s| {
let old = s.clone();
*s = new;
// this machine's window placement, session and private things
s.window = old.window;
s.session_tabs = old.session_tabs.clone();
s.session_panes = old.session_panes.clone();
s.session_active = old.session_active;
s.saved_sessions = old.saved_sessions.clone();
s.search_history = old.search_history.clone();
s.dialog_folders = old.dialog_folders.clone();
s.dock_weather_city = old.dock_weather_city.clone();
s.ferry_name = old.ferry_name.clone();
s.ferry_inbox = old.ferry_inbox.clone();
s.recent_cleared_ms = old.recent_cleared_ms;
if !take_programs {
s.git_path = old.git_path.clone();
s.ide_path = old.ide_path.clone();
s.terminal_path = old.terminal_path.clone();
s.text_editor_path = old.text_editor_path.clone();
s.open_rules = old.open_rules.clone();
s.toolbar_commands = old.toolbar_commands.clone();
s.app_icon = old.app_icon.clone();
s.theme_folder = old.theme_folder.clone();
}
});
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn programs_are_found_and_private_things_neither_leave_nor_arrive() {
let current: toml::Table = toml::from_str("git_path = \"\"\nterminal_path = \"wt.exe\"\n").unwrap();
let imported: toml::Table = toml::from_str("git_path = \"\\\\\\\\host\\\\x.exe\"\nterminal_path = \"wt.exe\"\nshow_hidden = true\n").unwrap();
assert_eq!(changed_programs(&imported, ¤t), ["git_path"]);
let mut s = toml::Table::new();
s.insert("search_history".into(), toml::Value::Array(vec!["secret".into()]));
s.insert("show_hidden".into(), true.into());
let text = export_text(&s, false, r"C:\Users\Me").unwrap();
assert!(!text.contains("secret") && text.contains("show_hidden"));
let (back, _) = import_text("search_history = [\"theirs\"]\nshow_hidden = true\n", r"C:\Users\Me").unwrap();
assert!(!back.contains("theirs"));
assert_eq!(from_portable("%USERPROFILE%x", r"C:\Users\Me"), "%USERPROFILE%x", "only as a whole path part");
assert_eq!(from_portable(r"%USERPROFILE%\Docs", r"C:\Users\Me"), r"C:\Users\Me\Docs");
}
#[test]
fn profile_paths_travel_as_the_variable_and_come_back_as_this_pcs() {
let mut t = toml::Table::new();
t.insert(
"pinned".into(),
toml::Value::Array(vec![
r"C:\Users\Myself\Downloads".into(),
r"c:\users\myself".into(),
r"C:\Users\Myselfie\Desktop".into(),
r"D:\Work".into(),
]),
);
t.insert("theme".into(), "dark".into());
let text = export_text(&t, false, r"C:\Users\Myself").unwrap();
assert!(text.contains(r"%USERPROFILE%\Downloads"), "{text}");
assert!(text.contains(r"C:\Users\Myselfie\Desktop"), "another user's folder is not the profile");
assert!(text.contains(r"D:\Work"));
assert!(text.contains("open_save_dialogs = false"));
let (back, dialogs) = import_text(&text, r"C:\Users\Other\").unwrap();
assert_eq!(dialogs, Some(false));
let t: toml::Table = toml::from_str(&back).unwrap();
let pinned: Vec<&str> = t["pinned"].as_array().unwrap().iter().map(|v| v.as_str().unwrap()).collect();
assert_eq!(pinned, [r"C:\Users\Other\Downloads", r"C:\Users\Other", r"C:\Users\Myselfie\Desktop", r"D:\Work"]);
assert!(!t.contains_key(DIALOGS_KEY), "not a setting of the file");
}
#[test]
fn an_older_export_has_no_dialog_choice() {
let (_, dialogs) = import_text("theme = \"dark\"", r"C:\Users\x").unwrap();
assert_eq!(dialogs, None);
}
}