Skip to content

Instantly share code, notes, and snippets.

@tesfabpel
Last active November 12, 2022 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tesfabpel/7df29806b7265e22f4a37ee856fce466 to your computer and use it in GitHub Desktop.
Save tesfabpel/7df29806b7265e22f4a37ee856fce466 to your computer and use it in GitHub Desktop.
Portal API POC
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