|
| 1 | +//! This module contains functions for opening file dialogs using DBus. |
| 2 | +
|
| 3 | +use ashpd::desktop::file_chooser; |
| 4 | +use ashpd::{zbus, WindowIdentifier}; |
| 5 | +use futures::executor::block_on; |
| 6 | +use tracing::warn; |
| 7 | + |
| 8 | +use crate::{FileDialogOptions, FileDialogToken, FileInfo}; |
| 9 | + |
| 10 | +use super::window::IdleHandle; |
| 11 | + |
| 12 | +pub(crate) fn open_file( |
| 13 | + window: u32, |
| 14 | + idle: IdleHandle, |
| 15 | + options: FileDialogOptions, |
| 16 | +) -> FileDialogToken { |
| 17 | + dialog(window, idle, options, true) |
| 18 | +} |
| 19 | + |
| 20 | +pub(crate) fn save_file( |
| 21 | + window: u32, |
| 22 | + idle: IdleHandle, |
| 23 | + options: FileDialogOptions, |
| 24 | +) -> FileDialogToken { |
| 25 | + dialog(window, idle, options, false) |
| 26 | +} |
| 27 | + |
| 28 | +fn dialog( |
| 29 | + window: u32, |
| 30 | + idle: IdleHandle, |
| 31 | + mut options: FileDialogOptions, |
| 32 | + open: bool, |
| 33 | +) -> FileDialogToken { |
| 34 | + let tok = FileDialogToken::next(); |
| 35 | + |
| 36 | + std::thread::spawn(move || { |
| 37 | + if let Err(e) = block_on(async { |
| 38 | + let conn = zbus::Connection::session().await?; |
| 39 | + let proxy = file_chooser::FileChooserProxy::new(&conn).await?; |
| 40 | + let id = WindowIdentifier::from_xid(window as u64); |
| 41 | + let multi = options.multi_selection; |
| 42 | + |
| 43 | + let title_owned = options.title.take(); |
| 44 | + let title = match (open, options.select_directories) { |
| 45 | + (true, true) => "Open Folder", |
| 46 | + (true, false) => "Open File", |
| 47 | + (false, _) => "Save File", |
| 48 | + }; |
| 49 | + let title = title_owned.as_deref().unwrap_or(title); |
| 50 | + let open_result; |
| 51 | + let save_result; |
| 52 | + let uris = if open { |
| 53 | + open_result = proxy.open_file(&id, title, options.into()).await?; |
| 54 | + open_result.uris() |
| 55 | + } else { |
| 56 | + save_result = proxy.save_file(&id, title, options.into()).await?; |
| 57 | + save_result.uris() |
| 58 | + }; |
| 59 | + |
| 60 | + let mut paths = uris.iter().filter_map(|s| { |
| 61 | + s.strip_prefix("file://").or_else(|| { |
| 62 | + warn!("expected path '{}' to start with 'file://'", s); |
| 63 | + None |
| 64 | + }) |
| 65 | + }); |
| 66 | + if multi && open { |
| 67 | + let infos = paths |
| 68 | + .map(|p| FileInfo { |
| 69 | + path: p.into(), |
| 70 | + format: None, |
| 71 | + }) |
| 72 | + .collect(); |
| 73 | + idle.add_idle_callback(move |handler| handler.open_files(tok, infos)); |
| 74 | + } else if !multi { |
| 75 | + if uris.len() > 2 { |
| 76 | + warn!( |
| 77 | + "expected one path (got {}), returning only the first", |
| 78 | + uris.len() |
| 79 | + ); |
| 80 | + } |
| 81 | + let info = paths.next().map(|p| FileInfo { |
| 82 | + path: p.into(), |
| 83 | + format: None, |
| 84 | + }); |
| 85 | + if open { |
| 86 | + idle.add_idle_callback(move |handler| handler.open_file(tok, info)); |
| 87 | + } else { |
| 88 | + idle.add_idle_callback(move |handler| handler.save_as(tok, info)); |
| 89 | + } |
| 90 | + } else { |
| 91 | + warn!("cannot save multiple paths"); |
| 92 | + } |
| 93 | + |
| 94 | + Ok(()) as ashpd::Result<()> |
| 95 | + }) { |
| 96 | + warn!("error while opening file dialog: {}", e); |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + tok |
| 101 | +} |
| 102 | + |
| 103 | +impl From<crate::FileSpec> for file_chooser::FileFilter { |
| 104 | + fn from(spec: crate::FileSpec) -> file_chooser::FileFilter { |
| 105 | + let mut filter = file_chooser::FileFilter::new(spec.name); |
| 106 | + for ext in spec.extensions { |
| 107 | + filter = filter.glob(&format!("*.{}", ext)); |
| 108 | + } |
| 109 | + filter |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +impl From<crate::FileDialogOptions> for file_chooser::OpenFileOptions { |
| 114 | + fn from(opts: crate::FileDialogOptions) -> file_chooser::OpenFileOptions { |
| 115 | + let mut fc = file_chooser::OpenFileOptions::default() |
| 116 | + .modal(true) |
| 117 | + .multiple(opts.multi_selection) |
| 118 | + .directory(opts.select_directories); |
| 119 | + |
| 120 | + if let Some(label) = &opts.button_text { |
| 121 | + fc = fc.accept_label(label); |
| 122 | + } |
| 123 | + |
| 124 | + if let Some(filters) = opts.allowed_types { |
| 125 | + for f in filters { |
| 126 | + fc = fc.add_filter(f.into()); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if let Some(filter) = opts.default_type { |
| 131 | + fc = fc.current_filter(filter.into()); |
| 132 | + } |
| 133 | + |
| 134 | + fc |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +impl From<crate::FileDialogOptions> for file_chooser::SaveFileOptions { |
| 139 | + fn from(opts: crate::FileDialogOptions) -> file_chooser::SaveFileOptions { |
| 140 | + let mut fc = file_chooser::SaveFileOptions::default().modal(true); |
| 141 | + |
| 142 | + if let Some(name) = &opts.default_name { |
| 143 | + fc = fc.current_name(name); |
| 144 | + } |
| 145 | + |
| 146 | + if let Some(label) = &opts.button_text { |
| 147 | + fc = fc.accept_label(label); |
| 148 | + } |
| 149 | + |
| 150 | + if let Some(filters) = opts.allowed_types { |
| 151 | + for f in filters { |
| 152 | + fc = fc.add_filter(f.into()); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + if let Some(filter) = opts.default_type { |
| 157 | + fc = fc.current_filter(filter.into()); |
| 158 | + } |
| 159 | + |
| 160 | + if let Some(dir) = &opts.starting_directory { |
| 161 | + fc = fc.current_folder(dir); |
| 162 | + } |
| 163 | + |
| 164 | + fc |
| 165 | + } |
| 166 | +} |
0 commit comments