Switch to zbus, implement more stuff

This commit is contained in:
2025-03-21 15:11:43 +01:00
parent 01416ab218
commit 07e842660b
4 changed files with 927 additions and 106 deletions

View File

@@ -0,0 +1,70 @@
// https://wiki.gnome.org/Attic(2f)DraftThumbnailerSpec.html
// https://specifications.freedesktop.org/thumbnail-spec/latest/
use zbus::{fdo::Result, proxy};
#[proxy(
interface = "org.freedesktop.thumbnails.Thumbnailer1",
default_service = "org.freedesktop.thumbnails.Thumbnailer1",
default_path = "/org/freedesktop/thumbnails/Thumbnailer1"
)]
pub trait Thumbnailer {
fn queue(
&self,
paths: Vec<String>,
mime_types: Vec<String>,
flavor: &str,
scheduler: &str,
handle_to_unqueue: u32,
) -> Result<u32>;
fn dequeue(&self, handle: u32) -> Result<()>;
fn get_flavors(&self) -> Result<Vec<String>>;
fn get_schedulers(&self) -> Result<Vec<String>>;
fn get_supported(&self) -> Result<(Vec<String>, Vec<String>)>;
#[zbus(signal)]
fn error(
&self,
handle: u32,
failed_uris: Vec<String>,
error_code: i32,
error_message: &str,
) -> Result<()>;
#[zbus(signal)]
fn finished(&self, handle: u32) -> Result<()>;
#[zbus(signal)]
fn ready(&self, handle: u32, uris: Vec<String>) -> Result<()>;
#[zbus(signal)]
fn started(&self, handle: u32) -> Result<()>;
}
#[proxy(
interface = "org.freedesktop.thumbnails.Cache1",
default_service = "org.freedesktop.thumbnails.Cache1",
default_path = "/org/freedesktop/thumbnails/Cache1"
)]
pub trait ThumbnailerCache {
fn cleanup(&self, base_uris: Vec<String>, since: u32) -> Result<()>;
fn copy(&self, from_uris: Vec<String>, to_uris: Vec<String>) -> Result<()>;
fn delete(&self, uris: Vec<String>) -> Result<()>;
fn move_(&self, from_uris: Vec<String>, to_uris: Vec<String>) -> Result<()>;
}
#[proxy(
interface = "org.freedesktop.thumbnails.Manager1",
default_service = "org.freedesktop.thumbnails.Manager1",
default_path = "/org/freedesktop/thumbnails/Manager1"
)]
pub trait ThumbnailerManager {
fn register(&self, uri_scheme: &str, mimetype: &str) -> Result<()>;
}

View File

@@ -1,73 +1,111 @@
// https://specifications.freedesktop.org/thumbnail-spec/latest/
mod dbus_thumbnailer_api;
use dbus::blocking::{BlockingSender, Connection, Proxy};
use dbus::Message;
use std::path::PathBuf;
use std::time::Duration;
use clap::{CommandFactory, Parser, command};
use clap_complete::{Shell, generate};
use dbus_thumbnailer_api::ThumbnailerProxyBlocking;
use zbus::{blocking::Connection, proxy::CacheProperties};
#[derive(Parser, Debug)]
#[command(bin_name = "thumbctl", version, about)]
struct Args {
#[command(subcommand)]
command: Command,
}
#[derive(Parser, Debug, Clone)]
enum Command {
Status,
GetFlavors,
GetSchedulers,
GetSupportedUriSchemes,
GetSupportedMimeTypes,
Enqueue,
CleanCache,
CopyCache,
DeleteCache,
MoveCache,
#[command(hide = true)]
GenerateCompletions(GenerateCompletionArgs),
}
#[derive(Parser, Debug, Clone)]
struct GenerateCompletionArgs {
#[arg(long, default_value = "bash")]
shell: Shell,
}
fn main() {
let connection: Connection = Connection::new_session().unwrap();
let thumbnailers = list_thumbnailers(&connection);
dbg!(&thumbnailers);
let args = Args::parse();
match args.command {
Command::Status => {
unimplemented!();
}
Command::GetFlavors => {
get_flavors();
}
Command::GetSchedulers => {
get_schedulers();
}
Command::GetSupportedUriSchemes => {
get_supported_uri_schemes();
}
Command::GetSupportedMimeTypes => {
get_supported_mime_types();
}
Command::GenerateCompletions(args) => {
generate(
args.shell,
&mut Args::command(),
"thumbctl",
&mut std::io::stdout(),
);
}
_ => {
unimplemented!();
}
}
}
fn thumbnailer_proxy(connection: &Connection) -> Proxy<&Connection> {
connection.with_proxy(
"org.freedesktop.thumbnails.Thumbnailer1",
"/org/freedesktop/thumbnails/Thumbnailer1",
Duration::from_millis(5000),
)
fn create_thumbnailer_proxy() -> ThumbnailerProxyBlocking<'static> {
let connection = Connection::session().unwrap();
ThumbnailerProxyBlocking::builder(&connection)
.cache_properties(CacheProperties::No)
.build()
.unwrap()
}
fn cache_proxy(connection: &Connection) -> Proxy<&Connection> {
connection.with_proxy(
"org.freedesktop.thumbnails.Cache1",
"/org/freedesktop/thumbnails/Cache1",
Duration::from_millis(5000),
)
fn get_flavors() {
let proxy = create_thumbnailer_proxy();
let flavors = proxy.get_flavors().unwrap();
for flavor in flavors {
println!("{}", flavor);
}
}
fn manager_proxy(connection: &Connection) -> Proxy<&Connection> {
connection.with_proxy(
"org.freedesktop.thumbnails.Manager1",
"/org/freedesktop/thumbnails/Manager1",
Duration::from_millis(5000),
)
fn get_schedulers() {
let proxy = create_thumbnailer_proxy();
let schedulers = proxy.get_schedulers().unwrap();
for scheduler in schedulers {
println!("{}", scheduler);
}
}
// fn list_thumbnailers(connection: &Connection) -> Vec<String> {
// let (names,): (Vec<String>,) = proxy
// .method_call(
// "org.freedesktop.DBus.Properties",
// "Get",
// ("org.freedesktop.thumbnails.Thumbnailer1", "Thumbnailers"),
// )
// .unwrap();
// // names
// vec![]
// }
fn queue_thumbnails(connection: &Connection, paths: Vec<PathBuf>) {
let mut proxy = thumbnailer_proxy(connection);
let (handle,): (u32,) = proxy.method_call(
"org.freedesktop.thumbnails.Thumbnailer1",
"Queue",
(
paths.iter().map(|p| p.to_string_lossy().to_string()).collect::<Vec<String>>(),
vec![], // mime types
"normal", // flavor
"", // scheduler
0, // handle_to_unqueue
),
).unwrap();
// proxy.send_with_reply_and_block(
// Message::new_method_call(
// "org.freedesktop.thumbnails.Thumbnailer1",
// "/org/freedesktop/thumbnails/Thumbnailer1",
// "org.freedesktop.thumbnails.Thumbnailer1",
// "Queue",
// ).unwrap();
// , timeout)
fn get_supported_uri_schemes() {
let proxy = create_thumbnailer_proxy();
let (mut uri_schemes, _) = proxy.get_supported().unwrap();
uri_schemes.sort();
for uri_scheme in uri_schemes {
println!("{}", uri_scheme);
}
}
fn get_supported_mime_types() {
let proxy = create_thumbnailer_proxy();
let (_, mut mime_types) = proxy.get_supported().unwrap();
mime_types.sort();
for mime_type in mime_types {
println!("{}", mime_type);
}
}