mirror of
https://github.com/narrrl/proton-drive-linux.git
synced 2026-11-17 17:55:03 +01:00
feat(gui,core,cli): edit excluded files, open-with rules and the launcher in Preferences (#99)
Preferences gains two pages for the config.json settings that had no place in the app: - Sync: the patterns every synced folder leaves out (own patterns, and each built-in one on or off), and what the conflict sweep may do. - Apps: which program opens which kind of file (open_with), and how the search launcher opens (prompt). Each synced folder kept on this computer gets "Excluded Files…" in its menu for its own .pdfsignore: checked as typed, "Check a path" says which rule decides, and a version changed elsewhere is never written over without asking. `pdfs sync ignore list|add|remove|reset|check` does the same from a terminal, and `--page preferences-sync` (and -apps, -storage) opens Preferences at a page. Every writer of config.json now goes through AppDirs::update_config, which reads, changes and writes under an flock on config.json.lock, so the app, the tray and the daemon no longer undo each other's change (B292). The opener reads its rules on each open, so a new rule applies without a restart. The built-in ignore list also leaves out what Nextcloud, Dropbox and Syncthing leave out: desktop.ini, .directory, trash folders, NFS/FUSE leftovers, partial downloads, Kate swap and office lock files.
This commit is contained in:
1 parent
14b9e0fe64
commit
0df4b3c5f9
40 files changed
+8376
-3303
No files matched your search
@@ -26,6 +26,8 @@ use pages::photo_viewer::*;
|
||||
use pages::photos::*;
|
||||
use pages::places::*;
|
||||
use pages::places_map::*;
|
||||
use pages::preferences_apps::*;
|
||||
use pages::preferences_sync::*;
|
||||
use pages::shared::*;
|
||||
use pages::shared_by_me::*;
|
||||
use pages::status::*;
|
||||
@@ -39,6 +41,7 @@ use widgets::compat::*;
|
||||
use widgets::crumb::*;
|
||||
use widgets::details::*;
|
||||
use widgets::file_list::*;
|
||||
use widgets::ignore_dialog::*;
|
||||
use widgets::menu::*;
|
||||
use widgets::photo_drag::*;
|
||||
use widgets::player::*;
|
||||
@@ -307,6 +310,18 @@ fn sync_view(page: &str) -> Option<&'static str> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The Preferences page a `--page` name opens: "preferences" and
|
||||
/// "preferences-" followed by the page's name.
|
||||
fn preferences_page(page: &str) -> Option<&'static str> {
|
||||
match page {
|
||||
"preferences" | "preferences-general" => Some("general"),
|
||||
"preferences-sync" => Some("sync"),
|
||||
"preferences-apps" => Some("apps"),
|
||||
"preferences-storage" => Some("storage"),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The Photos view a `--page` name opens, and whether Places shows its map:
|
||||
/// "albums", "places" and "places-map". Other names are stack pages.
|
||||
fn photos_view(page: &str) -> Option<(u32, bool)> {
|
||||
@@ -710,6 +725,8 @@ fn build_window(app: &adw::Application) {
|
||||
&main_widgets.purge_button,
|
||||
&main_widgets.mountpoint_button,
|
||||
);
|
||||
wire_sync_prefs(&ui, main_widgets.sync_prefs);
|
||||
wire_apps_prefs(&ui, main_widgets.apps_prefs);
|
||||
wire_sidebar(&ui);
|
||||
wire_browser(&ui);
|
||||
wire_bulk(
|
||||
@@ -868,12 +885,10 @@ fn save_window_state(ui: &Ui, window: &adw::ApplicationWindow) {
|
||||
height,
|
||||
maximized: window.is_maximized(),
|
||||
};
|
||||
let mut config = ui.dirs.load_config();
|
||||
if config.window == state {
|
||||
if ui.dirs.load_config().window == state {
|
||||
return;
|
||||
}
|
||||
config.window = state;
|
||||
if let Err(e) = ui.dirs.save_config(&config) {
|
||||
if let Err(e) = ui.dirs.update_config(|config| config.window = state) {
|
||||
tracing::warn!("cannot save the window size: {e}");
|
||||
}
|
||||
}
|
||||
@@ -1471,7 +1486,10 @@ fn install_launch_actions(ui: &Rc<Ui>, app: &adw::Application, window: &adw::App
|
||||
show_login(&ui_page);
|
||||
return;
|
||||
}
|
||||
if let Some(view) = sync_view(page) {
|
||||
if let Some(name) = preferences_page(page) {
|
||||
ui_page.status.prefs.set_visible_page_name(name);
|
||||
ui_page.status.prefs.present(ui_window(&ui_page).as_ref());
|
||||
} else if let Some(view) = sync_view(page) {
|
||||
ui_page.locations.views.set_visible_child_name(view);
|
||||
ui_page.stack.set_visible_child_name("locations");
|
||||
} else if let Some((view, map)) = photos_view(page) {
|
||||
@@ -2055,6 +2073,15 @@ mod tests {
|
||||
assert_eq!(sync_view("locations"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_page_name_can_open_a_preferences_page() {
|
||||
assert_eq!(preferences_page("preferences"), Some("general"));
|
||||
assert_eq!(preferences_page("preferences-sync"), Some("sync"));
|
||||
assert_eq!(preferences_page("preferences-apps"), Some("apps"));
|
||||
assert_eq!(preferences_page("preferences-storage"), Some("storage"));
|
||||
assert_eq!(preferences_page("sync"), None);
|
||||
}
|
||||
|
||||
/// One file of a shot, for the group-switch tests.
|
||||
fn member(uid: &str, name: Option<&str>, kind: PhotoKind) -> PhotoItem {
|
||||
PhotoItem {
|
||||
|
||||
@@ -1431,9 +1431,11 @@ fn save_icon_size(ui: &Rc<Ui>, size: i32) {
|
||||
let ui_save = ui.clone();
|
||||
let source = glib::timeout_add_local_once(Duration::from_millis(500), move || {
|
||||
ui_save.browser.zoom_save.borrow_mut().take();
|
||||
let mut config = ui_save.dirs.load_config();
|
||||
config.files_view.icon_size = ui_save.browser.view.get().icon_size;
|
||||
if let Err(e) = ui_save.dirs.save_config(&config) {
|
||||
let icon_size = ui_save.browser.view.get().icon_size;
|
||||
if let Err(e) = ui_save
|
||||
.dirs
|
||||
.update_config(|config| config.files_view.icon_size = icon_size)
|
||||
{
|
||||
tracing::warn!("cannot save the icon size: {e}");
|
||||
}
|
||||
});
|
||||
@@ -1464,9 +1466,7 @@ pub(crate) fn set_files_view(ui: &Rc<Ui>, view: FilesView) {
|
||||
return;
|
||||
}
|
||||
apply_files_view(ui, view);
|
||||
let mut config = ui.dirs.load_config();
|
||||
config.files_view = view;
|
||||
if let Err(e) = ui.dirs.save_config(&config) {
|
||||
if let Err(e) = ui.dirs.update_config(|config| config.files_view = view) {
|
||||
toast_error(ui, &gettext("Couldn't save the view"), &e.to_string());
|
||||
}
|
||||
if (old.sort, old.descending, old.folders_first)
|
||||
|
||||
@@ -1498,6 +1498,7 @@ fn folder_mode_label(ondemand: bool) -> String {
|
||||
/// synced folder's menu.
|
||||
fn add_device_items(ui: &Rc<Ui>, menu: &mut ActionMenu, spec: &MountSpec, id: i64) {
|
||||
if spec.mode != MountMode::OnDemand {
|
||||
let ui_ignore = ui.clone();
|
||||
// A paused folder skips its passes, so Sync now would do nothing.
|
||||
if !spec.paused {
|
||||
let ui = ui.clone();
|
||||
@@ -1516,6 +1517,14 @@ fn add_device_items(ui: &Rc<Ui>, menu: &mut ActionMenu, spec: &MountSpec, id: i6
|
||||
menu.item(&gettext("Sync From Scratch…"), move || {
|
||||
confirm_reset_sync_folder(&ui, id, &path)
|
||||
});
|
||||
// Only a synced copy has local files for the rules to leave out.
|
||||
if spec.mode == MountMode::Mirror {
|
||||
let ui = ui_ignore;
|
||||
let root = PathBuf::from(&spec.local_path);
|
||||
menu.item(&gettext("Excluded Files…"), move || {
|
||||
present_ignore_dialog(&ui, id, root.clone())
|
||||
});
|
||||
}
|
||||
menu.section();
|
||||
}
|
||||
let ui = ui.clone();
|
||||
|
||||
@@ -12,6 +12,8 @@ pub(crate) mod photo_viewer;
|
||||
pub(crate) mod photos;
|
||||
pub(crate) mod places;
|
||||
pub(crate) mod places_map;
|
||||
pub(crate) mod preferences_apps;
|
||||
pub(crate) mod preferences_sync;
|
||||
pub(crate) mod shared;
|
||||
pub(crate) mod shared_by_me;
|
||||
pub(crate) mod status;
|
||||
|
||||
@@ -2129,16 +2129,17 @@ fn save_photos_view(ui: &Rc<Ui>) {
|
||||
let ui_save = ui.clone();
|
||||
let source = glib::timeout_add_local_once(Duration::from_millis(500), move || {
|
||||
ui_save.gallery.zoom_save.borrow_mut().take();
|
||||
let mut config = ui_save.dirs.load_config();
|
||||
let view = PhotosView {
|
||||
row_height: Some(ui_save.gallery.row_height.get()),
|
||||
square: ui_save.gallery.square.get(),
|
||||
};
|
||||
if config.photos_view == view {
|
||||
if ui_save.dirs.load_config().photos_view == view {
|
||||
return;
|
||||
}
|
||||
config.photos_view = view;
|
||||
if let Err(e) = ui_save.dirs.save_config(&config) {
|
||||
if let Err(e) = ui_save
|
||||
.dirs
|
||||
.update_config(|config| config.photos_view = view)
|
||||
{
|
||||
tracing::warn!("cannot save the Photos view: {e}");
|
||||
}
|
||||
});
|
||||
|
||||
@@ -0,0 +1,606 @@
|
||||
//! Preferences → Apps: which program opens which file (`open_with`) and how
|
||||
//! the search launcher shows itself (`prompt`).
|
||||
//!
|
||||
//! Neither involves the service. The app and the search launcher read both
|
||||
//! again each time they open something, so a change applies to the next file
|
||||
//! opened or the next search.
|
||||
|
||||
use crate::*;
|
||||
use pdfs_core::menu::{PromptConfig, PromptMode};
|
||||
use pdfs_core::opener::{
|
||||
OpenRule, OpenWith, command_available, format_command, parse_command, resolve_terminal,
|
||||
};
|
||||
|
||||
/// The widgets of the Apps page, built with the rest of the dialog and wired
|
||||
/// once the app's state exists.
|
||||
pub(crate) struct AppsPrefs {
|
||||
pub(crate) page: adw::PreferencesPage,
|
||||
rules: adw::PreferencesGroup,
|
||||
add_rule: gtk4::Button,
|
||||
other_row: adw::EntryRow,
|
||||
terminal_row: adw::EntryRow,
|
||||
mode_row: adw::ComboRow,
|
||||
menu_row: adw::EntryRow,
|
||||
prompt_terminal_row: adw::EntryRow,
|
||||
limit_row: adw::SpinRow,
|
||||
icons_row: adw::SwitchRow,
|
||||
}
|
||||
|
||||
/// The launcher modes in the order the combo row lists them.
|
||||
const MODES: [PromptMode; 3] = [PromptMode::Gtk, PromptMode::Dmenu, PromptMode::Fzf];
|
||||
|
||||
/// The file classes a rule can name, with what the dialog calls them.
|
||||
fn classes() -> [(&'static str, String); 6] {
|
||||
[
|
||||
("@text", gettext("Text")),
|
||||
("@document", gettext("Documents")),
|
||||
("@image", gettext("Images")),
|
||||
("@media", gettext("Audio and Video")),
|
||||
("@dir", gettext("Folders")),
|
||||
("@any", gettext("Everything")),
|
||||
]
|
||||
}
|
||||
|
||||
pub(crate) fn build_apps_prefs() -> AppsPrefs {
|
||||
let add_rule = gtk4::Button::builder()
|
||||
.label(gettext("Add Rule…"))
|
||||
.valign(gtk4::Align::Center)
|
||||
.build();
|
||||
add_rule.add_css_class("flat");
|
||||
let rules = adw::PreferencesGroup::builder()
|
||||
.title(gettext("Open Files With"))
|
||||
.description(gettext(
|
||||
"For files opened from Proton Drive and the search launcher. The first rule that matches a file decides; other files open in their default app.",
|
||||
))
|
||||
.header_suffix(&add_rule)
|
||||
.build();
|
||||
|
||||
let other_row = adw::EntryRow::builder()
|
||||
.title(gettext("Other files (empty: default app)"))
|
||||
.show_apply_button(true)
|
||||
.build();
|
||||
other_row.add_css_class("monospace");
|
||||
let terminal_row = adw::EntryRow::builder()
|
||||
.title(terminal_title())
|
||||
.show_apply_button(true)
|
||||
.build();
|
||||
terminal_row.add_css_class("monospace");
|
||||
let other = adw::PreferencesGroup::new();
|
||||
other.add(&other_row);
|
||||
other.add(&terminal_row);
|
||||
|
||||
let mode_row = adw::ComboRow::builder()
|
||||
.title(gettext("Opens as"))
|
||||
.model(>k4::StringList::new(&[
|
||||
&gettext("Window"),
|
||||
&gettext("Your Launcher"),
|
||||
&gettext("fzf in a Terminal"),
|
||||
]))
|
||||
.build();
|
||||
let menu_row = adw::EntryRow::builder()
|
||||
.title(gettext("Launcher command (empty: find one)"))
|
||||
.show_apply_button(true)
|
||||
.build();
|
||||
menu_row.add_css_class("monospace");
|
||||
let prompt_terminal_row = adw::EntryRow::builder()
|
||||
.title(terminal_title())
|
||||
.show_apply_button(true)
|
||||
.build();
|
||||
prompt_terminal_row.add_css_class("monospace");
|
||||
let limit_row = adw::SpinRow::builder()
|
||||
.title(gettext("Results shown"))
|
||||
.adjustment(>k4::Adjustment::new(
|
||||
pdfs_core::menu::DEFAULT_MENU_LIMIT as f64,
|
||||
5.0,
|
||||
500.0,
|
||||
5.0,
|
||||
25.0,
|
||||
0.0,
|
||||
))
|
||||
.build();
|
||||
let icons_row = adw::SwitchRow::builder()
|
||||
.title(gettext("Show icons"))
|
||||
.subtitle(gettext("When the launcher can show them"))
|
||||
.build();
|
||||
let launcher = adw::PreferencesGroup::builder()
|
||||
.title(gettext("Search Launcher"))
|
||||
.description(gettext(
|
||||
"How “pdfs-prompt” shows search results, for example from a keyboard shortcut. Your launcher can be any program that reads choices like dmenu, such as rofi, wofi, fuzzel or bemenu.",
|
||||
))
|
||||
.build();
|
||||
launcher.add(&mode_row);
|
||||
launcher.add(&menu_row);
|
||||
launcher.add(&prompt_terminal_row);
|
||||
launcher.add(&limit_row);
|
||||
launcher.add(&icons_row);
|
||||
|
||||
let page = adw::PreferencesPage::builder()
|
||||
.title(gettext("Apps"))
|
||||
.name("apps")
|
||||
.icon_name("application-x-executable-symbolic")
|
||||
.build();
|
||||
page.add(&rules);
|
||||
page.add(&other);
|
||||
page.add(&launcher);
|
||||
AppsPrefs {
|
||||
page,
|
||||
rules,
|
||||
add_rule,
|
||||
other_row,
|
||||
terminal_row,
|
||||
mode_row,
|
||||
menu_row,
|
||||
prompt_terminal_row,
|
||||
limit_row,
|
||||
icons_row,
|
||||
}
|
||||
}
|
||||
|
||||
/// "Terminal", naming the one used when the row is left empty.
|
||||
fn terminal_title() -> String {
|
||||
match resolve_terminal() {
|
||||
// Translators: {terminal} is a terminal command line such as "foot -e".
|
||||
Some(found) => gettext_f(
|
||||
"Terminal (empty: {terminal})",
|
||||
&[("terminal", &format_command(&found))],
|
||||
),
|
||||
None => gettext("Terminal"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Shared by every handler of the page.
|
||||
struct Apps {
|
||||
ui: Rc<Ui>,
|
||||
widgets: AppsPrefs,
|
||||
rule_rows: RefCell<Vec<adw::ActionRow>>,
|
||||
}
|
||||
|
||||
/// Fill the page from the config and connect its controls.
|
||||
pub(crate) fn wire_apps_prefs(ui: &Rc<Ui>, widgets: AppsPrefs) {
|
||||
let apps = Rc::new(Apps {
|
||||
ui: ui.clone(),
|
||||
widgets,
|
||||
rule_rows: RefCell::default(),
|
||||
});
|
||||
let config = ui.dirs.load_config();
|
||||
let open_with = config.resolved_open_with();
|
||||
let prompt = config.resolved_prompt();
|
||||
let w = &apps.widgets;
|
||||
|
||||
let suppress = ui.status.settings_suppress.replace(true);
|
||||
paint_rules(&apps, &open_with);
|
||||
w.other_row
|
||||
.set_text(&format_command(open_with.default.as_deref().unwrap_or(&[])));
|
||||
w.terminal_row.set_text(&format_command(
|
||||
open_with.terminal.as_deref().unwrap_or(&[]),
|
||||
));
|
||||
let mode = prompt.resolved_mode();
|
||||
w.mode_row
|
||||
.set_selected(MODES.iter().position(|m| *m == mode).unwrap_or_default() as u32);
|
||||
w.menu_row
|
||||
.set_text(&format_command(prompt.menu.as_deref().unwrap_or(&[])));
|
||||
w.prompt_terminal_row
|
||||
.set_text(&format_command(prompt.terminal.as_deref().unwrap_or(&[])));
|
||||
w.limit_row.set_value(prompt.resolved_menu_limit() as f64);
|
||||
w.icons_row.set_active(prompt.resolved_menu_icons());
|
||||
show_mode_rows(w, mode);
|
||||
ui.status.settings_suppress.set(suppress);
|
||||
|
||||
let a = apps.clone();
|
||||
w.add_rule
|
||||
.connect_clicked(move |_| edit_rule(&a, None, OpenRule::default()));
|
||||
|
||||
// Command lines: checked as typed, saved on apply. Empty means "unset".
|
||||
connect_command_row(&apps, &w.other_row, |config, argv| {
|
||||
open_with_of(config).default = argv;
|
||||
});
|
||||
connect_command_row(&apps, &w.terminal_row, |config, argv| {
|
||||
open_with_of(config).terminal = argv;
|
||||
});
|
||||
connect_command_row(&apps, &w.menu_row, |config, argv| {
|
||||
prompt_of(config).menu = argv;
|
||||
});
|
||||
connect_command_row(&apps, &w.prompt_terminal_row, |config, argv| {
|
||||
prompt_of(config).terminal = argv;
|
||||
});
|
||||
|
||||
let a = apps.clone();
|
||||
w.mode_row.connect_selected_notify(move |row| {
|
||||
if a.ui.status.settings_suppress.get() {
|
||||
return;
|
||||
}
|
||||
let mode = MODES
|
||||
.get(row.selected() as usize)
|
||||
.copied()
|
||||
.unwrap_or_default();
|
||||
show_mode_rows(&a.widgets, mode);
|
||||
save(&a, move |config| {
|
||||
prompt_of(config).mode = (mode != PromptMode::default()).then_some(mode);
|
||||
});
|
||||
});
|
||||
let a = apps.clone();
|
||||
w.limit_row.connect_value_notify(move |row| {
|
||||
if a.ui.status.settings_suppress.get() {
|
||||
return;
|
||||
}
|
||||
let limit = row.value().round() as usize;
|
||||
save(&a, move |config| {
|
||||
prompt_of(config).menu_limit =
|
||||
(limit != pdfs_core::menu::DEFAULT_MENU_LIMIT).then_some(limit);
|
||||
});
|
||||
});
|
||||
let a = apps.clone();
|
||||
w.icons_row.connect_active_notify(move |row| {
|
||||
if a.ui.status.settings_suppress.get() {
|
||||
return;
|
||||
}
|
||||
let on = row.is_active();
|
||||
save(&a, move |config| {
|
||||
prompt_of(config).menu_icons = (!on).then_some(false)
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// Show only the rows the chosen launcher mode uses.
|
||||
fn show_mode_rows(w: &AppsPrefs, mode: PromptMode) {
|
||||
w.menu_row.set_visible(mode == PromptMode::Dmenu);
|
||||
w.icons_row.set_visible(mode == PromptMode::Dmenu);
|
||||
w.prompt_terminal_row.set_visible(mode == PromptMode::Fzf);
|
||||
w.limit_row.set_visible(mode != PromptMode::Gtk);
|
||||
}
|
||||
|
||||
/// The `open_with` section, created when absent.
|
||||
fn open_with_of(config: &mut pdfs_core::config::AppConfig) -> &mut OpenWith {
|
||||
config.open_with.get_or_insert_with(OpenWith::default)
|
||||
}
|
||||
|
||||
/// The `prompt` section, created when absent.
|
||||
fn prompt_of(config: &mut pdfs_core::config::AppConfig) -> &mut PromptConfig {
|
||||
config.prompt.get_or_insert_with(PromptConfig::default)
|
||||
}
|
||||
|
||||
/// Save a change, then drop sections left at their defaults so the file only
|
||||
/// holds what the user chose. Says whether it saved.
|
||||
fn save(apps: &Rc<Apps>, change: impl FnOnce(&mut pdfs_core::config::AppConfig)) -> bool {
|
||||
let saved = apps.ui.dirs.update_config(|config| {
|
||||
change(config);
|
||||
if config.open_with.as_ref() == Some(&OpenWith::default()) {
|
||||
config.open_with = None;
|
||||
}
|
||||
if config.prompt.as_ref() == Some(&PromptConfig::default()) {
|
||||
config.prompt = None;
|
||||
}
|
||||
config.resolved_open_with()
|
||||
});
|
||||
match saved {
|
||||
Ok(open_with) => {
|
||||
paint_rules(apps, &open_with);
|
||||
true
|
||||
}
|
||||
Err(e) => {
|
||||
prefs_toast(
|
||||
&apps.ui,
|
||||
&gettext_f(
|
||||
"{what}: {detail}",
|
||||
&[
|
||||
("what", &gettext("Couldn't save the setting")),
|
||||
("detail", &e.to_string()),
|
||||
],
|
||||
),
|
||||
);
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Check a command line as it is typed, and save it when applied.
|
||||
fn connect_command_row(
|
||||
apps: &Rc<Apps>,
|
||||
row: &adw::EntryRow,
|
||||
store: impl Fn(&mut pdfs_core::config::AppConfig, Option<Vec<String>>) + 'static,
|
||||
) {
|
||||
row.connect_changed(|row| mark_command(row, false));
|
||||
let a = apps.clone();
|
||||
row.connect_apply(move |row| {
|
||||
let Some(argv) = parse_command(&row.text()) else {
|
||||
prefs_toast(&a.ui, &gettext("A quote is not closed in the command"));
|
||||
return;
|
||||
};
|
||||
if !argv.is_empty() && !command_available(&argv) {
|
||||
prefs_toast(
|
||||
&a.ui,
|
||||
// Translators: {program} is a program name such as "nvim".
|
||||
&gettext_f(
|
||||
"“{program}” isn't installed; saved anyway",
|
||||
&[("program", &argv[0])],
|
||||
),
|
||||
);
|
||||
}
|
||||
let argv = (!argv.is_empty()).then_some(argv);
|
||||
save(&a, |config| store(config, argv));
|
||||
mark_command(row, true);
|
||||
});
|
||||
}
|
||||
|
||||
/// Mark a command line that doesn't parse as an error, and, once `applied`,
|
||||
/// one whose program isn't found as a warning.
|
||||
fn mark_command(row: &adw::EntryRow, applied: bool) {
|
||||
row.remove_css_class("error");
|
||||
row.remove_css_class("warning");
|
||||
match parse_command(&row.text()) {
|
||||
None => row.add_css_class("error"),
|
||||
Some(argv) if applied && !argv.is_empty() && !command_available(&argv) => {
|
||||
row.add_css_class("warning")
|
||||
}
|
||||
Some(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// Show the rules, one row each, in the order they are tried.
|
||||
fn paint_rules(apps: &Rc<Apps>, open_with: &OpenWith) {
|
||||
let group = &apps.widgets.rules;
|
||||
for row in apps.rule_rows.borrow_mut().drain(..) {
|
||||
group.remove(&row);
|
||||
}
|
||||
let count = open_with.rules.len();
|
||||
if count == 0 {
|
||||
let row = adw::ActionRow::builder()
|
||||
.title(gettext("No rules: every file opens in its default app"))
|
||||
.build();
|
||||
row.add_css_class("dim-label");
|
||||
group.add(&row);
|
||||
apps.rule_rows.borrow_mut().push(row);
|
||||
return;
|
||||
}
|
||||
let names = classes();
|
||||
for (index, rule) in open_with.rules.iter().enumerate() {
|
||||
let patterns: Vec<String> = rule
|
||||
.patterns
|
||||
.iter()
|
||||
.map(|p| {
|
||||
names
|
||||
.iter()
|
||||
.find(|(class, _)| class == p)
|
||||
.map_or_else(|| p.clone(), |(_, name)| name.clone())
|
||||
})
|
||||
.collect();
|
||||
let command = format_command(&rule.command);
|
||||
let subtitle = if rule.terminal {
|
||||
// Translators: {command} is a command line such as "nvim".
|
||||
gettext_f("{command}, in a terminal", &[("command", &command)])
|
||||
} else {
|
||||
command
|
||||
};
|
||||
let row = adw::ActionRow::builder()
|
||||
.title(patterns.join(", "))
|
||||
.subtitle(subtitle)
|
||||
.use_markup(false)
|
||||
.activatable(true)
|
||||
.build();
|
||||
if !command_available(&rule.command) {
|
||||
let warning = gtk4::Image::builder()
|
||||
.icon_name("dialog-warning-symbolic")
|
||||
// Translators: {program} is a program name such as "nvim".
|
||||
.tooltip_text(gettext_f(
|
||||
"“{program}” isn't installed",
|
||||
&[("program", rule.command.first().map_or("", String::as_str))],
|
||||
))
|
||||
.build();
|
||||
warning.add_css_class("warning");
|
||||
row.add_prefix(&warning);
|
||||
}
|
||||
let up = rule_button("go-up-symbolic", &gettext("Move Up"), index > 0);
|
||||
let down = rule_button("go-down-symbolic", &gettext("Move Down"), index + 1 < count);
|
||||
let remove = rule_button("user-trash-symbolic", &gettext("Remove"), true);
|
||||
let a = apps.clone();
|
||||
up.connect_clicked(move |_| {
|
||||
save(&a, |config| {
|
||||
open_with_of(config).rules.swap(index - 1, index)
|
||||
});
|
||||
});
|
||||
let a = apps.clone();
|
||||
down.connect_clicked(move |_| {
|
||||
save(&a, |config| {
|
||||
open_with_of(config).rules.swap(index, index + 1)
|
||||
});
|
||||
});
|
||||
let a = apps.clone();
|
||||
remove.connect_clicked(move |_| {
|
||||
save(&a, |config| {
|
||||
let rules = &mut open_with_of(config).rules;
|
||||
if index < rules.len() {
|
||||
rules.remove(index);
|
||||
}
|
||||
});
|
||||
});
|
||||
row.add_suffix(&up);
|
||||
row.add_suffix(&down);
|
||||
row.add_suffix(&remove);
|
||||
let a = apps.clone();
|
||||
let rule = rule.clone();
|
||||
row.connect_activated(move |_| edit_rule(&a, Some(index), rule.clone()));
|
||||
group.add(&row);
|
||||
apps.rule_rows.borrow_mut().push(row);
|
||||
}
|
||||
}
|
||||
|
||||
fn rule_button(icon: &str, tooltip: &str, sensitive: bool) -> gtk4::Button {
|
||||
let button = gtk4::Button::builder()
|
||||
.icon_name(icon)
|
||||
.tooltip_text(tooltip)
|
||||
.valign(gtk4::Align::Center)
|
||||
.sensitive(sensitive)
|
||||
.build();
|
||||
button.add_css_class("flat");
|
||||
button
|
||||
}
|
||||
|
||||
/// Split a typed pattern list into patterns, dropping empty ones.
|
||||
fn split_patterns(text: &str) -> Vec<String> {
|
||||
text.split(',')
|
||||
.map(str::trim)
|
||||
.filter(|p| !p.is_empty())
|
||||
.map(str::to_string)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Edit one rule, or a new one when `index` is `None`, in a dialog over the
|
||||
/// Preferences.
|
||||
fn edit_rule(apps: &Rc<Apps>, index: Option<usize>, rule: OpenRule) {
|
||||
let patterns_row = adw::EntryRow::builder()
|
||||
.title(gettext("Files, such as *.md, *.txt"))
|
||||
.text(rule.patterns.join(", "))
|
||||
.build();
|
||||
patterns_row.add_css_class("monospace");
|
||||
let chips = gtk4::FlowBox::builder()
|
||||
.selection_mode(gtk4::SelectionMode::None)
|
||||
.column_spacing(6)
|
||||
.row_spacing(6)
|
||||
.max_children_per_line(6)
|
||||
.margin_top(6)
|
||||
.build();
|
||||
for (class, name) in classes() {
|
||||
let chip = gtk4::Button::builder().label(&name).build();
|
||||
chip.add_css_class("pill");
|
||||
chip.add_css_class("small");
|
||||
let row = patterns_row.clone();
|
||||
chip.connect_clicked(move |_| {
|
||||
let mut list = split_patterns(&row.text());
|
||||
if !list.iter().any(|p| p == class) {
|
||||
list.push(class.to_string());
|
||||
}
|
||||
row.set_text(&list.join(", "));
|
||||
});
|
||||
chips.insert(&chip, -1);
|
||||
}
|
||||
let command_row = adw::EntryRow::builder()
|
||||
.title(gettext("Command, such as nvim or code --wait"))
|
||||
.text(format_command(&rule.command))
|
||||
.build();
|
||||
command_row.add_css_class("monospace");
|
||||
let terminal_row = adw::SwitchRow::builder()
|
||||
.title(gettext("Run in a terminal"))
|
||||
.subtitle(gettext("For programs that run in a terminal, such as vim"))
|
||||
.active(rule.terminal)
|
||||
.build();
|
||||
let hint = gtk4::Label::builder()
|
||||
.wrap(true)
|
||||
.xalign(0.0)
|
||||
.margin_top(6)
|
||||
.build();
|
||||
hint.add_css_class("caption");
|
||||
hint.add_css_class("dim-label");
|
||||
|
||||
let group = adw::PreferencesGroup::new();
|
||||
group.add(&patterns_row);
|
||||
let files = adw::PreferencesGroup::builder()
|
||||
.description(gettext("Or pick kinds of files:"))
|
||||
.build();
|
||||
files.add(&chips);
|
||||
let program = adw::PreferencesGroup::new();
|
||||
program.add(&command_row);
|
||||
program.add(&terminal_row);
|
||||
program.add(&hint);
|
||||
let page = adw::PreferencesPage::new();
|
||||
page.add(&group);
|
||||
page.add(&files);
|
||||
page.add(&program);
|
||||
|
||||
let cancel = gtk4::Button::with_label(&gettext("Cancel"));
|
||||
let save_button = gtk4::Button::with_label(&gettext("Save"));
|
||||
save_button.add_css_class("suggested-action");
|
||||
let header = adw::HeaderBar::builder()
|
||||
.show_start_title_buttons(false)
|
||||
.show_end_title_buttons(false)
|
||||
.build();
|
||||
header.pack_start(&cancel);
|
||||
header.pack_end(&save_button);
|
||||
let view = adw::ToolbarView::new();
|
||||
view.add_top_bar(&header);
|
||||
view.set_content(Some(&page));
|
||||
let dialog = adw::Dialog::builder()
|
||||
.title(if index.is_some() {
|
||||
gettext("Edit Rule")
|
||||
} else {
|
||||
gettext("New Rule")
|
||||
})
|
||||
.content_width(480)
|
||||
.child(&view)
|
||||
.build();
|
||||
|
||||
// Save only a rule that matches something and runs something.
|
||||
let check = {
|
||||
let (patterns_row, command_row, save_button, hint) = (
|
||||
patterns_row.clone(),
|
||||
command_row.clone(),
|
||||
save_button.clone(),
|
||||
hint.clone(),
|
||||
);
|
||||
move || {
|
||||
let patterns = split_patterns(&patterns_row.text());
|
||||
let argv = parse_command(&command_row.text());
|
||||
command_row.remove_css_class("error");
|
||||
hint.set_text("");
|
||||
match &argv {
|
||||
None => {
|
||||
command_row.add_css_class("error");
|
||||
hint.set_text(&gettext("A quote is not closed in the command."));
|
||||
}
|
||||
Some(argv) if !argv.is_empty() && !command_available(argv) => {
|
||||
hint.set_text(&gettext_f(
|
||||
"“{program}” isn't installed.",
|
||||
&[("program", &argv[0])],
|
||||
));
|
||||
}
|
||||
Some(_) => {}
|
||||
}
|
||||
save_button
|
||||
.set_sensitive(!patterns.is_empty() && argv.is_some_and(|argv| !argv.is_empty()));
|
||||
}
|
||||
};
|
||||
check();
|
||||
let c = check.clone();
|
||||
patterns_row.connect_changed(move |_| c());
|
||||
command_row.connect_changed(move |_| check());
|
||||
|
||||
let d = dialog.clone();
|
||||
cancel.connect_clicked(move |_| {
|
||||
d.close();
|
||||
});
|
||||
let (a, d) = (apps.clone(), dialog.clone());
|
||||
save_button.connect_clicked(move |_| {
|
||||
let Some(command) = parse_command(&command_row.text()) else {
|
||||
return;
|
||||
};
|
||||
let rule = OpenRule {
|
||||
patterns: split_patterns(&patterns_row.text()),
|
||||
command,
|
||||
terminal: terminal_row.is_active(),
|
||||
};
|
||||
let saved = save(&a, move |config| {
|
||||
let rules = &mut open_with_of(config).rules;
|
||||
match index {
|
||||
Some(i) if i < rules.len() => rules[i] = rule,
|
||||
_ => rules.push(rule),
|
||||
}
|
||||
});
|
||||
if saved {
|
||||
d.close();
|
||||
}
|
||||
});
|
||||
dialog.present(Some(&apps.ui.status.prefs));
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn typed_patterns_are_split_on_commas() {
|
||||
assert_eq!(
|
||||
split_patterns(" *.md, @text ,, notes-*.txt "),
|
||||
["*.md", "@text", "notes-*.txt"]
|
||||
);
|
||||
assert!(split_patterns(" , ").is_empty());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,347 @@
|
||||
//! Preferences → Sync: the patterns every synced folder leaves out
|
||||
//! (`ignore_patterns`) and what the conflict sweep may do (`conflict_sweep`).
|
||||
//!
|
||||
//! Both live in `config.json`. The daemon reads the patterns again on every
|
||||
//! sync pass, so a change here is followed by a Sync Now and applies at once;
|
||||
//! the sweep mode is read when the service starts, so a change offers a
|
||||
//! restart, as the mountpoint does.
|
||||
|
||||
use crate::*;
|
||||
use pdfs_core::config::SweepMode;
|
||||
use pdfs_core::syncignore::{DEFAULT_IGNORE_PATTERNS, check_pattern};
|
||||
|
||||
/// The widgets of the Sync page, built with the rest of the dialog and wired
|
||||
/// once the app's state exists.
|
||||
pub(crate) struct SyncPrefs {
|
||||
pub(crate) page: adw::PreferencesPage,
|
||||
patterns: adw::PreferencesGroup,
|
||||
add_row: adw::EntryRow,
|
||||
reset: gtk4::Button,
|
||||
sweep_row: adw::ComboRow,
|
||||
/// Whether the built-in patterns are shown, kept across repaints.
|
||||
builtin_expanded: Cell<bool>,
|
||||
}
|
||||
|
||||
/// The sweep modes in the order the combo row lists them.
|
||||
const SWEEP_MODES: [SweepMode; 3] = [SweepMode::Report, SweepMode::Enforce, SweepMode::Off];
|
||||
|
||||
pub(crate) fn build_sync_prefs() -> SyncPrefs {
|
||||
let reset = gtk4::Button::builder()
|
||||
.label(gettext("Reset"))
|
||||
.tooltip_text(gettext("Use the built-in patterns again"))
|
||||
.valign(gtk4::Align::Center)
|
||||
.build();
|
||||
reset.add_css_class("flat");
|
||||
let patterns = adw::PreferencesGroup::builder()
|
||||
.title(gettext("Excluded Files"))
|
||||
.description(gettext(
|
||||
"Files and folders matching these patterns are not synced in any synced folder. Use * for any characters, end a pattern with / to match folders only, and start it with ! to include something again. Excluding never deletes anything from Proton Drive.",
|
||||
))
|
||||
.header_suffix(&reset)
|
||||
.build();
|
||||
let add_row = adw::EntryRow::builder()
|
||||
.title(gettext("Add Pattern"))
|
||||
.show_apply_button(true)
|
||||
.build();
|
||||
add_row.add_css_class("monospace");
|
||||
patterns.add(&add_row);
|
||||
|
||||
let sweep_row = adw::ComboRow::builder()
|
||||
.title(gettext("Identical conflict copies"))
|
||||
.subtitle(gettext(
|
||||
"A copy made when a file changed in two places, found to be the same as the file",
|
||||
))
|
||||
.model(>k4::StringList::new(&[
|
||||
&gettext("Report Only"),
|
||||
&gettext("Move to Trash"),
|
||||
&gettext("Off"),
|
||||
]))
|
||||
.build();
|
||||
let conflicts = adw::PreferencesGroup::builder()
|
||||
.title(gettext("Conflict Copies"))
|
||||
.build();
|
||||
conflicts.add(&sweep_row);
|
||||
|
||||
let page = adw::PreferencesPage::builder()
|
||||
.title(gettext("Sync"))
|
||||
.name("sync")
|
||||
.icon_name("pdfs-sync-symbolic")
|
||||
.build();
|
||||
page.add(&patterns);
|
||||
page.add(&conflicts);
|
||||
SyncPrefs {
|
||||
page,
|
||||
patterns,
|
||||
add_row,
|
||||
reset,
|
||||
sweep_row,
|
||||
builtin_expanded: Cell::new(false),
|
||||
}
|
||||
}
|
||||
|
||||
/// Fill the page from the config and connect its controls.
|
||||
pub(crate) fn wire_sync_prefs(ui: &Rc<Ui>, widgets: SyncPrefs) {
|
||||
let widgets = Rc::new(widgets);
|
||||
let rows: Rc<RefCell<Vec<adw::PreferencesRow>>> = Rc::default();
|
||||
let config = ui.dirs.load_config();
|
||||
paint_patterns(ui, &widgets, &rows, &config);
|
||||
|
||||
let mode = config.conflict_sweep.unwrap_or_default();
|
||||
let suppress = ui.status.settings_suppress.replace(true);
|
||||
widgets.sweep_row.set_selected(
|
||||
SWEEP_MODES
|
||||
.iter()
|
||||
.position(|m| *m == mode)
|
||||
.unwrap_or_default() as u32,
|
||||
);
|
||||
ui.status.settings_suppress.set(suppress);
|
||||
|
||||
widgets.add_row.connect_changed(|row| {
|
||||
let text = row.text();
|
||||
if text.trim().is_empty() || check_pattern(text.trim()).is_ok() {
|
||||
row.remove_css_class("error");
|
||||
} else {
|
||||
row.add_css_class("error");
|
||||
}
|
||||
});
|
||||
let (ui_add, w_add, rows_add) = (ui.clone(), widgets.clone(), rows.clone());
|
||||
widgets.add_row.connect_apply(move |row| {
|
||||
let pattern = row.text().trim().to_string();
|
||||
if pattern.is_empty() {
|
||||
return;
|
||||
}
|
||||
if let Err(e) = check_pattern(&pattern) {
|
||||
tracing::debug!(%pattern, error = %e, "not a valid ignore pattern");
|
||||
prefs_toast(
|
||||
&ui_add,
|
||||
// Translators: {pattern} is what the user typed as a pattern.
|
||||
&gettext_f(
|
||||
"“{pattern}” isn't a valid pattern",
|
||||
&[("pattern", &pattern)],
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
let added = change_patterns(&ui_add, &w_add, &rows_add, |list| {
|
||||
if !list.contains(&pattern) {
|
||||
list.push(pattern.clone());
|
||||
}
|
||||
});
|
||||
if added {
|
||||
row.set_text("");
|
||||
}
|
||||
});
|
||||
|
||||
let (ui_reset, w_reset, rows_reset) = (ui.clone(), widgets.clone(), rows.clone());
|
||||
widgets.reset.connect_clicked(move |_| {
|
||||
let (ui, w, rows) = (ui_reset.clone(), w_reset.clone(), rows_reset.clone());
|
||||
confirm_destructive(
|
||||
&ui_reset.status.prefs,
|
||||
&gettext("Reset Excluded Files?"),
|
||||
&gettext("Your own patterns are removed, and the built-in ones apply again."),
|
||||
&gettext("Reset"),
|
||||
move || {
|
||||
change_patterns(&ui, &w, &rows, |list| {
|
||||
*list = default_patterns();
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
let ui_sweep = ui.clone();
|
||||
widgets.sweep_row.connect_selected_notify(move |row| {
|
||||
if ui_sweep.status.settings_suppress.get() {
|
||||
return;
|
||||
}
|
||||
let Some(mode) = SWEEP_MODES.get(row.selected() as usize).copied() else {
|
||||
return;
|
||||
};
|
||||
let stored = (mode != SweepMode::default()).then_some(mode);
|
||||
match ui_sweep
|
||||
.dirs
|
||||
.update_config(|config| config.conflict_sweep = stored)
|
||||
{
|
||||
Ok(()) => offer_restart(&ui_sweep),
|
||||
Err(e) => prefs_toast(
|
||||
&ui_sweep,
|
||||
&gettext_f(
|
||||
"{what}: {detail}",
|
||||
&[
|
||||
("what", &gettext("Couldn't save the setting")),
|
||||
("detail", &e.to_string()),
|
||||
],
|
||||
),
|
||||
),
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn default_patterns() -> Vec<String> {
|
||||
DEFAULT_IGNORE_PATTERNS
|
||||
.iter()
|
||||
.map(|p| (*p).to_string())
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Apply `edit` to the patterns in effect and save them, then show them and
|
||||
/// have every synced folder sync with them. A list equal to the built-in one
|
||||
/// is saved as "unset", so it follows the built-in list in later releases.
|
||||
/// Says whether it saved.
|
||||
fn change_patterns(
|
||||
ui: &Rc<Ui>,
|
||||
widgets: &Rc<SyncPrefs>,
|
||||
rows: &Rc<RefCell<Vec<adw::PreferencesRow>>>,
|
||||
edit: impl FnOnce(&mut Vec<String>),
|
||||
) -> bool {
|
||||
let saved = ui.dirs.update_config(|config| {
|
||||
let mut list = config.resolved_ignore_patterns();
|
||||
edit(&mut list);
|
||||
config.ignore_patterns = (list != default_patterns()).then_some(list);
|
||||
config.clone()
|
||||
});
|
||||
match saved {
|
||||
Ok(config) => {
|
||||
paint_patterns(ui, widgets, rows, &config);
|
||||
// Best effort: the next pass picks the change up anyway.
|
||||
let _ = spawn_request(ui.dirs.control_socket(), Request::SyncNow { id: None });
|
||||
true
|
||||
}
|
||||
Err(e) => {
|
||||
prefs_toast(
|
||||
ui,
|
||||
&gettext_f(
|
||||
"{what}: {detail}",
|
||||
&[
|
||||
("what", &gettext("Couldn't save the patterns")),
|
||||
("detail", &e.to_string()),
|
||||
],
|
||||
),
|
||||
);
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Show the patterns in effect below the entry row: the user's own one row
|
||||
/// each, then the built-in ones folded into one row, since they are many and
|
||||
/// rarely changed.
|
||||
fn paint_patterns(
|
||||
ui: &Rc<Ui>,
|
||||
widgets: &Rc<SyncPrefs>,
|
||||
rows: &Rc<RefCell<Vec<adw::PreferencesRow>>>,
|
||||
config: &pdfs_core::config::AppConfig,
|
||||
) {
|
||||
for row in rows.borrow_mut().drain(..) {
|
||||
widgets.patterns.remove(&row);
|
||||
}
|
||||
widgets
|
||||
.reset
|
||||
.set_sensitive(config.ignore_patterns.is_some());
|
||||
let list = config.resolved_ignore_patterns();
|
||||
let (builtin, own): (Vec<String>, Vec<String>) = list
|
||||
.into_iter()
|
||||
.partition(|p| DEFAULT_IGNORE_PATTERNS.contains(&p.as_str()));
|
||||
for pattern in own {
|
||||
let row = pattern_row(ui, widgets, rows, pattern);
|
||||
widgets.patterns.add(&row);
|
||||
rows.borrow_mut().push(row.upcast());
|
||||
}
|
||||
// Every built-in pattern, on or off, so one turned off can be turned on
|
||||
// again without losing the user's own.
|
||||
let on = builtin.len();
|
||||
let total = DEFAULT_IGNORE_PATTERNS.len();
|
||||
let expander = adw::ExpanderRow::builder()
|
||||
.title(gettext("Built-in patterns"))
|
||||
// Translators: {on} and {total} are numbers of patterns.
|
||||
.subtitle(gettext_f(
|
||||
"{on} of {total} on: version control, dependency and build folders, and what editors, downloads and systems leave behind",
|
||||
&[("on", &on.to_string()), ("total", &total.to_string())],
|
||||
))
|
||||
.expanded(widgets.builtin_expanded.get())
|
||||
.build();
|
||||
let w = widgets.clone();
|
||||
expander.connect_expanded_notify(move |row| w.builtin_expanded.set(row.is_expanded()));
|
||||
for pattern in DEFAULT_IGNORE_PATTERNS {
|
||||
let row = adw::SwitchRow::builder()
|
||||
.title(format!("<tt>{}</tt>", glib::markup_escape_text(pattern)))
|
||||
.use_markup(true)
|
||||
.active(builtin.iter().any(|p| p == pattern))
|
||||
.build();
|
||||
let (ui, w, r) = (ui.clone(), widgets.clone(), rows.clone());
|
||||
row.connect_active_notify(move |row| {
|
||||
let active = row.is_active();
|
||||
// Repainting replaces this row; let the switch finish first.
|
||||
let (ui, w, r) = (ui.clone(), w.clone(), r.clone());
|
||||
glib::idle_add_local_once(move || {
|
||||
change_patterns(&ui, &w, &r, move |list| {
|
||||
list.retain(|p| p != pattern);
|
||||
if active {
|
||||
list.push((*pattern).to_string());
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
expander.add_row(&row);
|
||||
}
|
||||
widgets.patterns.add(&expander);
|
||||
rows.borrow_mut().push(expander.upcast());
|
||||
}
|
||||
|
||||
/// One pattern, in monospace, with a button that removes it.
|
||||
fn pattern_row(
|
||||
ui: &Rc<Ui>,
|
||||
widgets: &Rc<SyncPrefs>,
|
||||
rows: &Rc<RefCell<Vec<adw::PreferencesRow>>>,
|
||||
pattern: String,
|
||||
) -> adw::ActionRow {
|
||||
let row = adw::ActionRow::builder()
|
||||
.title(format!("<tt>{}</tt>", glib::markup_escape_text(&pattern)))
|
||||
.use_markup(true)
|
||||
.build();
|
||||
let remove = gtk4::Button::builder()
|
||||
.icon_name("user-trash-symbolic")
|
||||
.tooltip_text(gettext("Remove"))
|
||||
.valign(gtk4::Align::Center)
|
||||
.build();
|
||||
remove.add_css_class("flat");
|
||||
let (ui, w, r) = (ui.clone(), widgets.clone(), rows.clone());
|
||||
remove.connect_clicked(move |_| {
|
||||
let pattern = pattern.clone();
|
||||
change_patterns(&ui, &w, &r, move |list| list.retain(|p| *p != pattern));
|
||||
});
|
||||
row.add_suffix(&remove);
|
||||
row
|
||||
}
|
||||
|
||||
/// The sweep mode is read when the service starts; offer that restart.
|
||||
fn offer_restart(ui: &Rc<Ui>) {
|
||||
let confirm = adw::AlertDialog::builder()
|
||||
.heading(gettext("Restart Proton Drive?"))
|
||||
.body(gettext(
|
||||
"The new setting applies after Proton Drive restarts.",
|
||||
))
|
||||
.build();
|
||||
confirm.add_response("later", &gettext("Later"));
|
||||
confirm.add_response("restart", &gettext("Restart Now"));
|
||||
confirm.set_response_appearance("restart", adw::ResponseAppearance::Suggested);
|
||||
confirm.set_default_response(Some("restart"));
|
||||
confirm.set_close_response("later");
|
||||
let ui_restart = ui.clone();
|
||||
confirm.connect_response(None, move |_, resp| {
|
||||
if resp == "restart" {
|
||||
restart_service_then(&ui_restart, |_| {});
|
||||
}
|
||||
});
|
||||
confirm.present(Some(&ui.status.prefs));
|
||||
}
|
||||
|
||||
/// A toast on the Preferences dialog, which covers the window's own toasts.
|
||||
pub(crate) fn prefs_toast(ui: &Rc<Ui>, message: &str) {
|
||||
ui.status.prefs.add_toast(
|
||||
adw::Toast::builder()
|
||||
.title(message)
|
||||
.use_markup(false)
|
||||
.timeout(6)
|
||||
.build(),
|
||||
);
|
||||
}
|
||||
@@ -169,6 +169,9 @@ pub(crate) struct MainWidgets {
|
||||
pub(crate) tray_row: adw::SwitchRow,
|
||||
pub(crate) update_row: adw::SwitchRow,
|
||||
pub(crate) language_row: adw::ComboRow,
|
||||
/// Preferences → Sync and → Apps, wired once the app's state exists.
|
||||
pub(crate) sync_prefs: SyncPrefs,
|
||||
pub(crate) apps_prefs: AppsPrefs,
|
||||
}
|
||||
|
||||
/// Build the two surfaces that replaced the old Settings page:
|
||||
@@ -267,6 +270,7 @@ pub(crate) fn build_main_page() -> MainWidgets {
|
||||
|
||||
let general = adw::PreferencesPage::builder()
|
||||
.title(gettext("General"))
|
||||
.name("general")
|
||||
.icon_name("preferences-system-symbolic")
|
||||
.build();
|
||||
general.add(&startup_group);
|
||||
@@ -335,6 +339,7 @@ pub(crate) fn build_main_page() -> MainWidgets {
|
||||
|
||||
let storage = adw::PreferencesPage::builder()
|
||||
.title(gettext("Storage"))
|
||||
.name("storage")
|
||||
.icon_name("drive-harddisk-symbolic")
|
||||
.build();
|
||||
storage.add(&storage_group);
|
||||
@@ -343,7 +348,11 @@ pub(crate) fn build_main_page() -> MainWidgets {
|
||||
let prefs = adw::PreferencesDialog::builder()
|
||||
.search_enabled(false)
|
||||
.build();
|
||||
let sync_prefs = build_sync_prefs();
|
||||
let apps_prefs = build_apps_prefs();
|
||||
prefs.add(&general);
|
||||
prefs.add(&sync_prefs.page);
|
||||
prefs.add(&apps_prefs.page);
|
||||
prefs.add(&storage);
|
||||
|
||||
// ---- Sidebar footer
|
||||
@@ -463,6 +472,8 @@ pub(crate) fn build_main_page() -> MainWidgets {
|
||||
tray_row,
|
||||
update_row,
|
||||
language_row,
|
||||
sync_prefs,
|
||||
apps_prefs,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -678,9 +689,10 @@ pub(crate) fn wire_settings(
|
||||
let rows = list.model.n_items();
|
||||
list.model.items_changed(0, rows, rows);
|
||||
}
|
||||
let mut config = ui_theme.dirs.load_config();
|
||||
config.proton_theme = Some(on);
|
||||
if let Err(e) = ui_theme.dirs.save_config(&config) {
|
||||
if let Err(e) = ui_theme
|
||||
.dirs
|
||||
.update_config(|config| config.proton_theme = Some(on))
|
||||
{
|
||||
toast_error(
|
||||
&ui_theme,
|
||||
&gettext("Couldn't save the theme"),
|
||||
@@ -700,9 +712,7 @@ pub(crate) fn wire_settings(
|
||||
.gallery
|
||||
.places_map
|
||||
.set_online(on, ui_map.dirs.cache_dir());
|
||||
let mut config = ui_map.dirs.load_config();
|
||||
config.online_map = on;
|
||||
if let Err(e) = ui_map.dirs.save_config(&config) {
|
||||
if let Err(e) = ui_map.dirs.update_config(|config| config.online_map = on) {
|
||||
toast_error(
|
||||
&ui_map,
|
||||
&gettext("Couldn't save the map setting"),
|
||||
@@ -717,9 +727,10 @@ pub(crate) fn wire_settings(
|
||||
if ui_update.status.settings_suppress.get() {
|
||||
return;
|
||||
}
|
||||
let mut config = ui_update.dirs.load_config();
|
||||
config.update_check = row.is_active();
|
||||
if let Err(e) = ui_update.dirs.save_config(&config) {
|
||||
if let Err(e) = ui_update
|
||||
.dirs
|
||||
.update_config(|config| config.update_check = row.is_active())
|
||||
{
|
||||
toast_error(
|
||||
&ui_update,
|
||||
&gettext("Couldn't save the update setting"),
|
||||
@@ -739,9 +750,10 @@ pub(crate) fn wire_settings(
|
||||
return;
|
||||
}
|
||||
let show = row.is_active();
|
||||
let mut config = ui_tray.dirs.load_config();
|
||||
config.tray_hidden = !show;
|
||||
if let Err(e) = ui_tray.dirs.save_config(&config) {
|
||||
if let Err(e) = ui_tray
|
||||
.dirs
|
||||
.update_config(|config| config.tray_hidden = !show)
|
||||
{
|
||||
toast_error(
|
||||
&ui_tray,
|
||||
&gettext("Couldn't save the tray setting"),
|
||||
@@ -763,9 +775,9 @@ pub(crate) fn wire_settings(
|
||||
if ui_language.status.settings_suppress.get() {
|
||||
return;
|
||||
}
|
||||
let mut config = ui_language.dirs.load_config();
|
||||
config.language = language_at(row.selected()).map(str::to_string);
|
||||
if let Err(e) = ui_language.dirs.save_config(&config) {
|
||||
if let Err(e) = ui_language.dirs.update_config(|config| {
|
||||
config.language = language_at(row.selected()).map(str::to_string)
|
||||
}) {
|
||||
toast_error(
|
||||
&ui_language,
|
||||
&gettext("Couldn't save the language"),
|
||||
@@ -864,9 +876,10 @@ fn use_mountpoint(ui: &Rc<Ui>, path: &std::path::Path) {
|
||||
let path_str = path.display().to_string();
|
||||
|
||||
// Persist the choice to config so the next mount uses it.
|
||||
let mut config = ui.dirs.load_config();
|
||||
config.mountpoint = Some(path_str.clone());
|
||||
if let Err(e) = ui.dirs.save_config(&config) {
|
||||
if let Err(e) = ui
|
||||
.dirs
|
||||
.update_config(|config| config.mountpoint = Some(path_str.clone()))
|
||||
{
|
||||
toast_error(ui, &gettext("Couldn't save the folder"), &e.to_string());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -82,10 +82,10 @@ pub(crate) fn start_update_checks(ui: &Rc<Ui>) {
|
||||
match fetch_latest().await {
|
||||
Ok(latest) => {
|
||||
checked = Some(unix_now());
|
||||
let mut config = ui.dirs.load_config();
|
||||
config.update_last_checked = checked;
|
||||
config.update_latest = latest;
|
||||
if let Err(e) = ui.dirs.save_config(&config) {
|
||||
if let Err(e) = ui.dirs.update_config(|config| {
|
||||
config.update_last_checked = checked;
|
||||
config.update_latest = latest;
|
||||
}) {
|
||||
tracing::warn!("update check: cannot save the result: {e}");
|
||||
}
|
||||
show_latest(&ui);
|
||||
|
||||
@@ -0,0 +1,342 @@
|
||||
//! The Excluded Files dialog of one synced folder: its own `.pdfsignore`,
|
||||
//! edited as text, with a check of what the rules do to a path.
|
||||
//!
|
||||
//! The file sits in the synced folder and syncs like any other file, so it can
|
||||
//! change on another computer while it is open here. Saving checks for that
|
||||
//! and asks, rather than writing over the other edit.
|
||||
|
||||
use crate::*;
|
||||
use pdfs_core::syncignore::{FolderRules, IgnoreRules, RuleSource, Verdict, WriteRulesError};
|
||||
|
||||
/// Open the dialog for the synced folder `id` at `root`.
|
||||
pub(crate) fn present_ignore_dialog(ui: &Rc<Ui>, id: i64, root: PathBuf) {
|
||||
let root_display = tilde_path(&root.display().to_string());
|
||||
let intro = gtk4::Label::builder()
|
||||
// Translators: {folder} is a local folder path such as ~/Projects.
|
||||
.label(gettext_f(
|
||||
"Patterns for {folder} only, one per line, on top of the ones for all synced folders. Lines starting with # are comments.",
|
||||
&[("folder", &root_display)],
|
||||
))
|
||||
.wrap(true)
|
||||
.xalign(0.0)
|
||||
.build();
|
||||
let text = gtk4::TextView::builder()
|
||||
.monospace(true)
|
||||
.wrap_mode(gtk4::WrapMode::WordChar)
|
||||
.top_margin(8)
|
||||
.bottom_margin(8)
|
||||
.left_margin(8)
|
||||
.right_margin(8)
|
||||
.sensitive(false)
|
||||
.build();
|
||||
let scroller = gtk4::ScrolledWindow::builder()
|
||||
.child(&text)
|
||||
.min_content_height(180)
|
||||
.vexpand(true)
|
||||
.build();
|
||||
scroller.add_css_class("card");
|
||||
let problems = gtk4::Label::builder()
|
||||
.wrap(true)
|
||||
.xalign(0.0)
|
||||
.visible(false)
|
||||
.build();
|
||||
problems.add_css_class("error");
|
||||
|
||||
let check_row = adw::EntryRow::builder()
|
||||
.title(gettext("Check a path in the folder"))
|
||||
.build();
|
||||
let verdict = gtk4::Label::builder()
|
||||
.wrap(true)
|
||||
.xalign(0.0)
|
||||
.margin_top(6)
|
||||
.build();
|
||||
verdict.add_css_class("dim-label");
|
||||
let check_group = adw::PreferencesGroup::new();
|
||||
check_group.add(&check_row);
|
||||
|
||||
let all_folders = gtk4::Button::builder()
|
||||
.label(gettext("Patterns for All Folders…"))
|
||||
.halign(gtk4::Align::Start)
|
||||
.build();
|
||||
all_folders.add_css_class("flat");
|
||||
|
||||
let body = gtk4::Box::new(gtk4::Orientation::Vertical, 12);
|
||||
body.set_margin_top(12);
|
||||
body.set_margin_bottom(18);
|
||||
body.set_margin_start(18);
|
||||
body.set_margin_end(18);
|
||||
body.append(&intro);
|
||||
body.append(&scroller);
|
||||
body.append(&problems);
|
||||
body.append(&check_group);
|
||||
body.append(&verdict);
|
||||
body.append(&all_folders);
|
||||
|
||||
let cancel = gtk4::Button::with_label(&gettext("Cancel"));
|
||||
let save = gtk4::Button::with_label(&gettext("Save"));
|
||||
save.add_css_class("suggested-action");
|
||||
save.set_sensitive(false);
|
||||
let header = adw::HeaderBar::builder()
|
||||
.show_start_title_buttons(false)
|
||||
.show_end_title_buttons(false)
|
||||
.build();
|
||||
header.pack_start(&cancel);
|
||||
header.pack_end(&save);
|
||||
let toasts = adw::ToastOverlay::new();
|
||||
toasts.set_child(Some(&body));
|
||||
let view = adw::ToolbarView::new();
|
||||
view.add_top_bar(&header);
|
||||
view.set_content(Some(&toasts));
|
||||
let dialog = adw::Dialog::builder()
|
||||
.title(gettext("Excluded Files"))
|
||||
.content_width(560)
|
||||
.content_height(560)
|
||||
.child(&view)
|
||||
.build();
|
||||
|
||||
// What was read, so the save can tell whether the file changed since.
|
||||
let loaded: Rc<RefCell<Option<FolderRules>>> = Rc::default();
|
||||
let globals = Rc::new(ui.dirs.load_config().resolved_ignore_patterns());
|
||||
|
||||
let show_verdict = {
|
||||
let (text, check_row, verdict, root, globals) = (
|
||||
text.clone(),
|
||||
check_row.clone(),
|
||||
verdict.clone(),
|
||||
root.clone(),
|
||||
globals.clone(),
|
||||
);
|
||||
move || {
|
||||
let typed = check_row.text();
|
||||
let typed = typed.trim();
|
||||
if typed.is_empty() {
|
||||
verdict.set_text("");
|
||||
return;
|
||||
}
|
||||
let path = Path::new(typed);
|
||||
let rel = path.strip_prefix(&root).unwrap_or(path);
|
||||
let rel = rel.to_string_lossy();
|
||||
let rel = rel.trim_start_matches('/');
|
||||
let is_dir = typed.ends_with('/') || root.join(rel).is_dir();
|
||||
let rules = IgnoreRules::with_folder_text(&root, &globals, &buffer_text(&text));
|
||||
verdict.set_text(&verdict_text(&rules.explain(rel, is_dir)));
|
||||
}
|
||||
};
|
||||
let on_text = {
|
||||
let (problems, save, show_verdict) = (problems.clone(), save.clone(), show_verdict.clone());
|
||||
move |text: &str| {
|
||||
let bad = pdfs_core::syncignore::check_rules(text);
|
||||
problems.set_visible(!bad.is_empty());
|
||||
if let Some((line, _)) = bad.first() {
|
||||
problems.set_text(&ngettext_f(
|
||||
"Line {line} isn't a valid pattern.",
|
||||
"Line {line} and {more} more aren't valid patterns.",
|
||||
bad.len() as u64,
|
||||
&[
|
||||
("line", &line.to_string()),
|
||||
("more", &(bad.len() - 1).to_string()),
|
||||
],
|
||||
));
|
||||
}
|
||||
save.set_sensitive(bad.is_empty());
|
||||
show_verdict();
|
||||
}
|
||||
};
|
||||
let on_text_changed = on_text.clone();
|
||||
text.buffer()
|
||||
.connect_changed(move |buffer| on_text_changed(&buffer_text_of(buffer)));
|
||||
check_row.connect_changed(move |_| show_verdict());
|
||||
|
||||
let d = dialog.clone();
|
||||
cancel.connect_clicked(move |_| {
|
||||
d.close();
|
||||
});
|
||||
let (ui_all, d) = (ui.clone(), dialog.clone());
|
||||
all_folders.connect_clicked(move |_| {
|
||||
d.close();
|
||||
let prefs = &ui_all.status.prefs;
|
||||
prefs.set_visible_page_name("sync");
|
||||
prefs.present(ui_window(&ui_all).as_ref());
|
||||
});
|
||||
|
||||
// Read off the main thread: the folder may be on a slow disk.
|
||||
let reader = {
|
||||
let (text, loaded, root, toasts) =
|
||||
(text.clone(), loaded.clone(), root.clone(), toasts.clone());
|
||||
move || {
|
||||
let (text, loaded, root, toasts) =
|
||||
(text.clone(), loaded.clone(), root.clone(), toasts.clone());
|
||||
glib::spawn_future_local(async move {
|
||||
let read = gio::spawn_blocking(move || FolderRules::read(&root))
|
||||
.await
|
||||
.unwrap_or_else(|_| Err(std::io::Error::other("the read did not finish")));
|
||||
match read {
|
||||
Ok(rules) => {
|
||||
text.buffer().set_text(rules.text.as_deref().unwrap_or(""));
|
||||
text.set_sensitive(true);
|
||||
*loaded.borrow_mut() = Some(rules);
|
||||
}
|
||||
Err(e) => dialog_toast(
|
||||
&toasts,
|
||||
&gettext_f(
|
||||
"{what}: {detail}",
|
||||
&[
|
||||
("what", &gettext("Couldn't read the patterns")),
|
||||
("detail", &e.to_string()),
|
||||
],
|
||||
),
|
||||
),
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
reader();
|
||||
|
||||
let (ui_save, d) = (ui.clone(), dialog.clone());
|
||||
save.connect_clicked(move |_| {
|
||||
let Some(rules) = loaded.borrow().clone() else {
|
||||
return;
|
||||
};
|
||||
write_rules(
|
||||
&ui_save,
|
||||
&d,
|
||||
&toasts,
|
||||
id,
|
||||
rules,
|
||||
buffer_text(&text),
|
||||
reader.clone(),
|
||||
);
|
||||
});
|
||||
|
||||
dialog.present(ui_window(ui).as_ref());
|
||||
}
|
||||
|
||||
/// Save `body` over `rules`, then close and sync the folder. When the file
|
||||
/// changed since it was read, ask whether to load the other version or to
|
||||
/// replace it; `reload` reads the file into the dialog again.
|
||||
fn write_rules(
|
||||
ui: &Rc<Ui>,
|
||||
dialog: &adw::Dialog,
|
||||
toasts: &adw::ToastOverlay,
|
||||
id: i64,
|
||||
rules: FolderRules,
|
||||
body: String,
|
||||
reload: impl Fn() + Clone + 'static,
|
||||
) {
|
||||
let (ui, dialog, toasts) = (ui.clone(), dialog.clone(), toasts.clone());
|
||||
glib::spawn_future_local(async move {
|
||||
let to_write = rules.clone();
|
||||
let text = body.clone();
|
||||
let written = gio::spawn_blocking(move || to_write.write(&text))
|
||||
.await
|
||||
.unwrap_or_else(|_| {
|
||||
Err(WriteRulesError::Io(std::io::Error::other(
|
||||
"the write did not finish",
|
||||
)))
|
||||
});
|
||||
match written {
|
||||
Ok(()) => {
|
||||
dialog.close();
|
||||
toast(&ui, &gettext("Excluded files saved"));
|
||||
let _ = spawn_request(ui.dirs.control_socket(), Request::SyncNow { id: Some(id) });
|
||||
}
|
||||
Err(WriteRulesError::Changed) => {
|
||||
let ask = adw::AlertDialog::builder()
|
||||
.heading(gettext("Patterns Changed Elsewhere"))
|
||||
.body(gettext(
|
||||
"The patterns of this folder changed while you edited them, for example on another computer. Load the other version, or replace it with yours?",
|
||||
))
|
||||
.build();
|
||||
ask.add_response("cancel", &gettext("Cancel"));
|
||||
ask.add_response("reload", &gettext("Load Other Version"));
|
||||
ask.add_response("replace", &gettext("Replace"));
|
||||
ask.set_response_appearance("replace", adw::ResponseAppearance::Destructive);
|
||||
ask.set_default_response(Some("cancel"));
|
||||
ask.set_close_response("cancel");
|
||||
let (ui, d, t) = (ui.clone(), dialog.clone(), toasts.clone());
|
||||
ask.connect_response(None, move |_, response| match response {
|
||||
"reload" => reload(),
|
||||
"replace" => {
|
||||
// Read the other version only as the base to check
|
||||
// against, then write over it.
|
||||
let (ui, d, t, body, reload) = (
|
||||
ui.clone(),
|
||||
d.clone(),
|
||||
t.clone(),
|
||||
body.clone(),
|
||||
reload.clone(),
|
||||
);
|
||||
let root = rules.path.parent().map(Path::to_path_buf);
|
||||
glib::spawn_future_local(async move {
|
||||
let Some(root) = root else { return };
|
||||
let read = gio::spawn_blocking(move || FolderRules::read(&root))
|
||||
.await
|
||||
.ok()
|
||||
.and_then(Result::ok);
|
||||
if let Some(current) = read {
|
||||
write_rules(&ui, &d, &t, id, current, body, reload);
|
||||
}
|
||||
});
|
||||
}
|
||||
_ => {}
|
||||
});
|
||||
ask.present(Some(&dialog));
|
||||
}
|
||||
Err(WriteRulesError::Io(e)) => dialog_toast(
|
||||
&toasts,
|
||||
&gettext_f(
|
||||
"{what}: {detail}",
|
||||
&[
|
||||
("what", &gettext("Couldn't save the patterns")),
|
||||
("detail", &e.to_string()),
|
||||
],
|
||||
),
|
||||
),
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Say what the rules do to the path being checked.
|
||||
fn verdict_text(verdict: &Verdict) -> String {
|
||||
match verdict {
|
||||
Verdict::Synced => gettext("Synced: no pattern matches it."),
|
||||
// Translators: {pattern} is an ignore pattern such as *.log.
|
||||
Verdict::Excluded {
|
||||
pattern,
|
||||
source: RuleSource::ThisFolder,
|
||||
} => gettext_f(
|
||||
"Not synced: excluded by “{pattern}” above.",
|
||||
&[("pattern", pattern)],
|
||||
),
|
||||
Verdict::Excluded {
|
||||
pattern,
|
||||
source: RuleSource::AllFolders,
|
||||
} => gettext_f(
|
||||
"Not synced: excluded by “{pattern}” in the patterns for all folders.",
|
||||
&[("pattern", pattern)],
|
||||
),
|
||||
Verdict::Included { pattern, .. } => gettext_f(
|
||||
"Synced: “{pattern}” includes it again.",
|
||||
&[("pattern", pattern)],
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
fn buffer_text(view: >k4::TextView) -> String {
|
||||
buffer_text_of(&view.buffer())
|
||||
}
|
||||
|
||||
fn buffer_text_of(buffer: >k4::TextBuffer) -> String {
|
||||
let (start, end) = buffer.bounds();
|
||||
buffer.text(&start, &end, false).to_string()
|
||||
}
|
||||
|
||||
fn dialog_toast(toasts: &adw::ToastOverlay, message: &str) {
|
||||
toasts.add_toast(
|
||||
adw::Toast::builder()
|
||||
.title(message)
|
||||
.use_markup(false)
|
||||
.timeout(6)
|
||||
.build(),
|
||||
);
|
||||
}
|
||||
@@ -2,6 +2,7 @@ pub(crate) mod compat;
|
||||
pub(crate) mod crumb;
|
||||
pub(crate) mod details;
|
||||
pub(crate) mod file_list;
|
||||
pub(crate) mod ignore_dialog;
|
||||
pub(crate) mod menu;
|
||||
pub(crate) mod photo_drag;
|
||||
pub(crate) mod player;
|
||||
|
||||
@@ -398,12 +398,10 @@ fn open_manager(args: &[&str]) {
|
||||
/// Remember that the user hid the icon, so the next login's autostart and the
|
||||
/// app both leave it away, then exit.
|
||||
fn hide_tray() {
|
||||
if let Ok(dirs) = AppDirs::new() {
|
||||
let mut config = dirs.load_config();
|
||||
config.tray_hidden = true;
|
||||
if let Err(e) = dirs.save_config(&config) {
|
||||
tracing::warn!("hide tray icon: {e}");
|
||||
}
|
||||
if let Ok(dirs) = AppDirs::new()
|
||||
&& let Err(e) = dirs.update_config(|config| config.tray_hidden = true)
|
||||
{
|
||||
tracing::warn!("hide tray icon: {e}");
|
||||
}
|
||||
std::process::exit(0);
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ const STOCK: &[&str] = &[
|
||||
// GTK's built-in icons
|
||||
"info-outline-symbolic",
|
||||
// Adwaita
|
||||
"application-x-executable-symbolic",
|
||||
"audio-x-generic-symbolic",
|
||||
"checkbox-checked-symbolic",
|
||||
"checkbox-symbolic",
|
||||
|
||||
Reference in new issue
Block a user