Last active
November 12, 2022 15:25
-
-
Save tesfabpel/7df29806b7265e22f4a37ee856fce466 to your computer and use it in GitHub Desktop.
Portal API POC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
use std::error::Error; | |
use zbus::{Connection, zvariant}; | |
use zbus::zvariant::Value; | |
use zbus::dbus_proxy; | |
use async_std::stream::StreamExt; | |
#[dbus_proxy( | |
interface = "org.freedesktop.portal.FileChooser", | |
default_service = "org.freedesktop.portal.Desktop", | |
default_path = "/org/freedesktop/portal/desktop" | |
)] | |
trait FileChooser { | |
fn open_file( | |
&self, | |
parent_window: &str, | |
title: &str, | |
options: &HashMap<&str, Value<'_>>, | |
) -> zbus::Result<zvariant::OwnedObjectPath>; | |
} | |
#[dbus_proxy( | |
interface = "org.freedesktop.portal.Request", | |
default_service = "org.freedesktop.portal.Desktop", | |
default_path = "/org/freedesktop/portal/desktop" | |
)] | |
trait Request { | |
fn close(&self) -> zbus::Result<()>; | |
#[dbus_proxy(signal)] | |
fn response( | |
&self, | |
response: u32, | |
results: HashMap<&str, Value<'_>>, | |
) -> zbus::Result<()>; | |
} | |
#[async_std::main] | |
async fn main() -> Result<(), Box<dyn Error>> { | |
let connection = Connection::session().await?; | |
let proxy = FileChooserProxy::new(&connection).await?; | |
let req_path: zvariant::OwnedObjectPath = proxy.open_file( | |
"", | |
"Choose a dir", | |
&HashMap::from([ | |
("directory", Value::Bool(true)) | |
]), | |
).await?; | |
println!("{:?}", req_path); | |
let req = RequestProxy::builder(&connection) | |
.path(req_path)? | |
.build() | |
.await?; | |
let mut response_signal = req.receive_response().await?; | |
if let Some(signal) = response_signal.next().await { | |
println!("{:?}", signal.args()); | |
} | |
Ok(()) | |
} | |
// OUTPUT: | |
// OwnedObjectPath(ObjectPath("/org/freedesktop/portal/desktop/request/1_195/t")) | |
// Ok(Response { response: 0, results: {"choices": Array(Array { element_signature: Signature("(ss)"), elements: [], signature: Signature("a(ss)") }), "uris": Array(Array { element_signature: Signature("s"), elements: [Str(Str(Borrowed("file:///mnt/CommonExtraLinuxFiles/OS/Linux/steamdeck")))], signature: Signature("as") })} }) | |
// in the reponse object there is the path I've chosen: "file:///mnt/CommonExtraLinuxFiles/OS/Linux/steamdeck" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment