| 1 |
use std::{ |
| 2 |
ffi::{CStr, CString, OsStr}, |
| 3 |
os::unix::ffi::OsStrExt, |
| 4 |
path::PathBuf, |
| 5 |
}; |
| 6 |
|
| 7 |
mod ffi; |
| 8 |
|
| 9 |
mod file_dialog; |
| 10 |
|
| 11 |
mod libdbus; |
| 12 |
use libdbus::*; |
| 13 |
|
| 14 |
pub use file_dialog::{FileFilter, FilePath, HandleToken, OpenFileOptions, SaveFileOptions}; |
| 15 |
|
| 16 |
pub fn uris_to_paths(uris: Vec<CString>) -> Vec<PathBuf> { |
| 17 |
uris.into_iter() |
| 18 |
.filter_map(|uri| { |
| 19 |
let bytes: Vec<u8> = percent_encoding::percent_decode(uri.as_bytes()).collect(); |
| 20 |
let Some(path) = bytes.strip_prefix(b"file://") else { |
| 21 |
log::error!("Ignoring uri: {bytes:?} lacks `file://`"); |
| 22 |
return None; |
| 23 |
}; |
| 24 |
Some(PathBuf::from(OsStr::from_bytes(path))) |
| 25 |
}) |
| 26 |
.collect() |
| 27 |
} |
| 28 |
|
| 29 |
pub fn open_file(opts: OpenFileOptions) -> Option<Vec<CString>> { |
| 30 |
let mut conn = Connection::new()?; |
| 31 |
|
| 32 |
let handle_path = generate_response_path(&mut conn, &opts.handle_token); |
| 33 |
register_response_listener(&mut conn, &handle_path); |
| 34 |
|
| 35 |
let reply = conn.send_and_block(&Message::open_file(opts)); |
| 36 |
|
| 37 |
if conn.err().is_err() { |
| 38 |
log::error!("OpenFile failed: {}", conn.err()); |
| 39 |
return None; |
| 40 |
} |
| 41 |
|
| 42 |
let reply = reply?; |
| 43 |
|
| 44 |
let mut iter = MessageIter::from_msg(&reply); |
| 45 |
|
| 46 |
let Some(got_handle_path) = iter.get_object_path() else { |
| 47 |
log::error!("Response object path is missing"); |
| 48 |
return None; |
| 49 |
}; |
| 50 |
|
| 51 |
if handle_path != got_handle_path { |
| 52 |
log::debug!("Detected ancient version of xdg portal, attempting fallback"); |
| 53 |
register_response_listener(&mut conn, &got_handle_path); |
| 54 |
} |
| 55 |
|
| 56 |
wait_for_response(&mut conn, &handle_path) |
| 57 |
} |
| 58 |
|
| 59 |
pub fn save_file(opts: SaveFileOptions) -> Option<Vec<CString>> { |
| 60 |
let mut conn = Connection::new()?; |
| 61 |
|
| 62 |
let handle_path = generate_response_path(&mut conn, &opts.handle_token); |
| 63 |
register_response_listener(&mut conn, &handle_path); |
| 64 |
|
| 65 |
let reply = conn.send_and_block(&Message::save_file(opts)); |
| 66 |
|
| 67 |
if conn.err().is_err() { |
| 68 |
log::error!("OpenFile failed: {}", conn.err()); |
| 69 |
return None; |
| 70 |
} |
| 71 |
|
| 72 |
let reply = reply?; |
| 73 |
|
| 74 |
let mut iter = MessageIter::from_msg(&reply); |
| 75 |
|
| 76 |
let Some(got_handle_path) = iter.get_object_path() else { |
| 77 |
log::error!("Response object path is missing"); |
| 78 |
return None; |
| 79 |
}; |
| 80 |
|
| 81 |
if handle_path != got_handle_path { |
| 82 |
log::debug!("Detected ancient version of xdg portal, attempting fallback"); |
| 83 |
register_response_listener(&mut conn, &got_handle_path); |
| 84 |
} |
| 85 |
|
| 86 |
wait_for_response(&mut conn, &handle_path) |
| 87 |
} |
| 88 |
|
| 89 |
fn generate_response_path(conn: &mut Connection, handle_token: &HandleToken) -> CString { |
| 90 |
let unique_name = conn.get_unique_name(); |
| 91 |
let unique_name = unique_name.to_str().unwrap(); |
| 92 |
let unique_identifier = unique_name.trim_start_matches(':').replace('.', "_"); |
| 93 |
let handle_token = handle_token.0.to_str().unwrap(); |
| 94 |
CString::new(format!( |
| 95 |
"/org/freedesktop/portal/desktop/request/{unique_identifier}/{handle_token}" |
| 96 |
)) |
| 97 |
.unwrap() |
| 98 |
} |
| 99 |
|
| 100 |
fn register_response_listener(conn: &mut Connection, handle_path: &CStr) { |
| 101 |
conn.add_match( |
| 102 |
&CString::new( |
| 103 |
[ |
| 104 |
"type='signal'", |
| 105 |
"sender='org.freedesktop.portal.Desktop'", |
| 106 |
&format!("path='{}'", handle_path.to_str().unwrap()), |
| 107 |
"interface='org.freedesktop.portal.Request'", |
| 108 |
"member='Response'", |
| 109 |
] |
| 110 |
.join(","), |
| 111 |
) |
| 112 |
.unwrap(), |
| 113 |
); |
| 114 |
|
| 115 |
if conn.err().is_err() { |
| 116 |
log::error!("Failed to add match rule: {}", conn.err()); |
| 117 |
} |
| 118 |
|
| 119 |
conn.flush(); |
| 120 |
} |
| 121 |
|
| 122 |
#[derive(Debug)] |
| 123 |
enum ResponseCode { |
| 124 |
Success = 0, |
| 125 |
// Cancelled = 1, |
| 126 |
// Other = 2, |
| 127 |
} |
| 128 |
|
| 129 |
fn wait_for_response(conn: &mut Connection, handle_path: &CStr) -> Option<Vec<CString>> { |
| 130 |
loop { |
| 131 |
conn.read_write(-1); |
| 132 |
while let Some(signal) = conn.pop_message() { |
| 133 |
if signal.is_signal(c"org.freedesktop.portal.Request", c"Response") { |
| 134 |
let Some(path) = signal.get_path() else { |
| 135 |
log::error!("Response signal is missing a path"); |
| 136 |
return None; |
| 137 |
}; |
| 138 |
|
| 139 |
if path == handle_path { |
| 140 |
return parse_response(&signal); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
fn parse_response(msg: &Message) -> Option<Vec<CString>> { |
| 148 |
let mut iter = MessageIter::from_msg(msg); |
| 149 |
|
| 150 |
let Some(response_code) = iter.get_u32() else { |
| 151 |
log::error!("Response code missing"); |
| 152 |
return None; |
| 153 |
}; |
| 154 |
if response_code != ResponseCode::Success as u32 { |
| 155 |
return Some(vec![]); |
| 156 |
} |
| 157 |
|
| 158 |
if !iter.next() { |
| 159 |
log::error!("Body of the response is empty"); |
| 160 |
return None; |
| 161 |
} |
| 162 |
|
| 163 |
if iter.get_arg_type() != ffi::DBUS_TYPE_ARRAY { |
| 164 |
log::error!("Body of the response is not an array"); |
| 165 |
return None; |
| 166 |
} |
| 167 |
|
| 168 |
let mut dict_iter = iter.iter_recurse(); |
| 169 |
|
| 170 |
while dict_iter.get_arg_type() == ffi::DBUS_TYPE_DICT_ENTRY { |
| 171 |
let mut entry_iter = dict_iter.iter_recurse(); |
| 172 |
|
| 173 |
let Some(key) = entry_iter.get_string() else { |
| 174 |
log::error!("Wrong type of a dict key"); |
| 175 |
continue; |
| 176 |
}; |
| 177 |
|
| 178 |
entry_iter.next(); |
| 179 |
if key.as_c_str() == c"uris" { |
| 180 |
if entry_iter.get_arg_type() == ffi::DBUS_TYPE_VARIANT { |
| 181 |
let mut var_iter = entry_iter.iter_recurse(); |
| 182 |
return Some(var_iter.get_string_array()); |
| 183 |
} else { |
| 184 |
log::error!( |
| 185 |
"Response.uris type {} != VARIANT", |
| 186 |
entry_iter.get_arg_type() |
| 187 |
); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
dict_iter.next(); |
| 192 |
} |
| 193 |
|
| 194 |
log::error!("Response.uris was not found"); |
| 195 |
None |
| 196 |
} |