Skip to main content

CpdbClient

Struct CpdbClient 

Source
pub struct CpdbClient { /* private fields */ }
Available on crate feature zbus-backend only.
Expand description

A connected CPDB client managing proxies to all discovered print backends.

Created via CpdbClient::new(). The client is Clone-able - cloning shares the underlying D-Bus connection.

§Usage

let client = CpdbClient::new().await?;

// Initial population
let printers = client.get_all_printers().await?;

// Fetch capabilities when the user selects a printer
let (options, media) = client.get_printer_details(&printers[0].id, "CUPS").await?;

// Submit a print job
let settings = [("copies", "1"), ("media", "iso_a4_210x297mm")];
let (job_id, fd) = client.print_fd(&printers[0].id, "CUPS", &settings, "My Doc").await?;

Implementations§

Source§

impl CpdbClient

Source

pub async fn new() -> Result<Self>

Connect to the D-Bus session bus and discover all CPDB backends.

  1. Opens a session bus connection.
  2. Calls ListActivatableNames to find org.openprinting.Backend.* services.
  3. Creates a PrintBackendProxy for each discovered backend.

Backends that fail to connect are logged and skipped.

§Errors

Returns CpdbError::DbusError if the session bus itself is unavailable.

Source

pub fn backend_count(&self) -> usize

Returns the number of connected backends.

Source

pub async fn get_all_printers(&self) -> Result<Vec<PrinterSnapshot>>

Fetches all known printers from all connected backends.

This is the initial population method - equivalent to what the C library’s fetchPrinterListFromBackend() does. It calls GetAllPrinters on each backend and unpacks the variant-wrapped printer data into PrinterSnapshots.

Use this to populate the printer list. Use discovery_stream() for live updates after that.

§Errors

Returns errors if a D-Bus call fails. Backends that fail individually are skipped, and printers from working backends are still returned.

Source

pub async fn get_filtered_printers(&self) -> Result<Vec<PrinterSnapshot>>

Like get_all_printers() but returns only printers matching the current filter state.

Call show_remote_printers(false) or show_temporary_printers(false) first to set the filter, then call this to get the filtered list.

Source

pub async fn discovery_stream(&self) -> Result<DiscoveryStream>

Returns a merged stream of DiscoveryEvents from all backends.

The stream emits events as printers are added, removed, or change state. After subscribing to signals, it calls doListing(true) on each backend to trigger initial PrinterAdded emissions.

A background task is automatically spawned to ping the backends periodically, preventing them from timing out. When this stream is dropped, the keep-alive task is cancelled.

§Errors

Returns CpdbError::DbusError if subscribing to D-Bus signals fails.

Source

pub async fn keep_alive_all(&self)

Keeps all backends alive to prevent them from auto-exiting.

CPDB backends automatically exit after a period of inactivity (typically 30 seconds). If you are listening to a discovery_stream(), you should call this method periodically (e.g. every 10 seconds) to ensure the backends stay running and continue emitting discovery signals.

Source

pub async fn get_printer_details( &self, printer_id: &str, backend: &str, ) -> Result<(OptionsCollection, MediaCollection)>

Fetches all options and media for a printer in a single D-Bus call.

This calls the backend’s GetAllOptions method, which returns both the printer’s capabilities (duplex, color mode, etc.) and its supported paper sizes with margin information.

§Arguments
  • printer_id - The printer’s unique ID (from PrinterSnapshot::id).
  • backend - The backend name, e.g. "CUPS" or the full service name "org.openprinting.Backend.CUPS".
§Errors
Source

pub async fn get_translations( &self, printer_id: &str, backend: &str, locale: &str, ) -> Result<HashMap<String, String>>

Fetches localized labels for a printer’s options and choices.

Returns a map of internal name -> human-readable label, e.g. {"sides" -> "Two-Sided", "one-sided" -> "Off", ...}.

§Arguments
  • printer_id - The printer’s unique ID.
  • backend - The backend name.
  • locale - A POSIX locale string, e.g. "en_US" or "de_DE".
Source

pub async fn get_default_printer(&self, backend: &str) -> Result<String>

Returns the default printer ID for a specific backend.

Source

pub async fn print_fd( &self, printer_id: &str, backend: &str, settings: &[(&str, &str)], title: &str, ) -> Result<(String, OwnedFd)>

Submits a print job and returns a writable file descriptor.

The backend creates a CUPS job and returns the write end of a socketpair. The caller writes the document data into fd and closes it when done - the backend reads from the other end and forwards it to the print system.

§Arguments
  • printer_id - The printer’s unique ID.
  • backend - The backend name.
  • settings - Print settings as key-value pairs, e.g. [("copies", "2"), ("media", "iso_a4_210x297mm")].
  • title - The job title shown in the print queue.
§Returns

A tuple of (job_id, fd) where job_id is the CUPS job ID string and fd is the writable end of the socketpair.

Source

pub async fn print_socket( &self, printer_id: &str, backend: &str, settings: &[(&str, &str)], title: &str, ) -> Result<(String, String)>

Submits a print job and returns a Unix domain socket path to write the document to.

The caller must connect to the returned socket path and write the document data, closing the stream when finished.

Source

pub async fn show_remote_printers(&self, visible: bool)

Sets the visibility of remote printers on all connected backends.

When visible is false, printers discovered via DNS-SD / mDNS on remote hosts are hidden from discovery signals.

Source

pub async fn show_temporary_printers(&self, visible: bool)

Sets the visibility of temporary (auto-discovered) printers on all connected backends.

Trait Implementations§

Source§

impl Clone for CpdbClient

Source§

fn clone(&self) -> CpdbClient

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more