mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-21 09:26:43 +00:00
Print: keep dry runs out of live printer discovery
This commit is contained in:
1 parent
a77d513d74
commit
b4c5f4da0e
2 files changed
+31
-5
No files matched your search
@@ -191,6 +191,7 @@ fn cups_spool(job: &PrintJob) -> std::result::Result<String, SpoolError> {
|
||||
/// `craft-print` (`lpstat`, the Windows spooler) where that is the route; over IPP for a named
|
||||
/// server and where there is no `lp` (a sandbox).
|
||||
fn system_printers(_s: &mut Session, p: &Value) -> Result<Value> {
|
||||
require_live_printer_query(p)?;
|
||||
let named = p.get("printServer").and_then(Value::as_str).filter(|v| !v.is_empty());
|
||||
if named.is_none() && system_spooler().is_some() {
|
||||
let list = craft_print::printers();
|
||||
@@ -854,7 +855,18 @@ fn print_windows(page: &PrintPage, p: &Value, title: &str, copies: u32) -> Resul
|
||||
)
|
||||
}
|
||||
|
||||
fn printers(_: &mut Session, _: &Value) -> Result<Value> {
|
||||
// Driver discovery/settings are external I/O, even when no page is spooled. A dry run must
|
||||
// not block on a disconnected printer or a print server (including in the command panic hunt).
|
||||
fn require_live_printer_query(p: &Value) -> Result<()> {
|
||||
if p.get("dryRun").and_then(Value::as_bool).unwrap_or(false) {
|
||||
Err(other("printer queries are unavailable in a dry run; omit dryRun to query the live printer backend"))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn printers(_: &mut Session, p: &Value) -> Result<Value> {
|
||||
require_live_printer_query(p)?;
|
||||
match craft_print::rgb::printers() {
|
||||
Ok(list) => Ok(json!({"supported": true, "printers": list.iter().map(|p| json!({"name": p.name, "default": p.default})).collect::<Vec<_>>()})),
|
||||
Err(craft_print::rgb::PrintError::Unsupported) => Ok(json!({"supported": false, "printers": []})),
|
||||
@@ -863,6 +875,7 @@ fn printers(_: &mut Session, _: &Value) -> Result<Value> {
|
||||
}
|
||||
|
||||
fn printer_page(_: &mut Session, p: &Value) -> Result<Value> {
|
||||
require_live_printer_query(p)?;
|
||||
// Checked before the printer, so a bad value is the error whether or not a printer answers.
|
||||
let landscape = match p.get("orientation").filter(|v| !v.is_null()) {
|
||||
None => None,
|
||||
@@ -920,7 +933,9 @@ fn print_one_copy(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let mut q = last_print(s).unwrap_or_else(|| json!({}));
|
||||
// A printer remembered from an earlier session may be gone: then the default takes the job,
|
||||
// as the Print dialog falls back (#7410). A list that can't be read decides nothing.
|
||||
if let Ok(listed) = system_printers(s, &q) {
|
||||
if !p.get("dryRun").and_then(Value::as_bool).unwrap_or(false)
|
||||
&& let Ok(listed) = system_printers(s, &q)
|
||||
{
|
||||
let names: Vec<&str> = listed["printers"].as_array().into_iter().flatten().filter_map(|p| p["name"].as_str()).collect();
|
||||
drop_vanished_printer(&mut q, &names);
|
||||
}
|
||||
@@ -1107,7 +1122,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
label: "Printers",
|
||||
menu: &[],
|
||||
shortcut: None,
|
||||
params: r##"{} → {supported, printers: [{name, default}]} (supported: Windows; elsewhere Print spools with lp)"##,
|
||||
params: r##"{"dryRun":bool=false} → {supported, printers: [{name, default}]} (supported: Windows; elsewhere Print spools with lp; dryRun refuses live device queries)"##,
|
||||
enabled: |_| Ok(()),
|
||||
journal: false,
|
||||
run: printers,
|
||||
@@ -1117,7 +1132,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
label: "Printer Page",
|
||||
menu: &[],
|
||||
shortcut: None,
|
||||
params: r##"{"printer":name? (default printer),"printerSettings":hex?,"orientation":"portrait|landscape"? (turn the settings, or the printer's own, that way)} → {printer, paper: [w,h] pt, printable: [x,y,w,h] pt from the top-left, dpi: [x,y], printerSettings: hex (only with orientation: the turned settings)} (Windows)"##,
|
||||
params: r##"{"printer":name? (default printer),"printerSettings":hex?,"orientation":"portrait|landscape"? (turn the settings, or the printer's own, that way),"dryRun":bool=false (refuse live device queries)} → {printer, paper: [w,h] pt, printable: [x,y,w,h] pt from the top-left, dpi: [x,y], printerSettings: hex (only with orientation: the turned settings)} (Windows)"##,
|
||||
enabled: |_| Ok(()),
|
||||
journal: false,
|
||||
run: printer_page,
|
||||
@@ -1127,7 +1142,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
label: "Printers",
|
||||
menu: &[],
|
||||
shortcut: None,
|
||||
params: r##"{} → {printers:[{name, default}], default:name|null} (the system spooler's printers; a name is Print's "printer")"##,
|
||||
params: r##"{"printServer":str?,"dryRun":bool=false} → {printers:[{name, default}], default:name|null} (the system spooler's printers; a name is Print's "printer"; dryRun refuses live device queries)"##,
|
||||
enabled: crate::commands::always,
|
||||
journal: false,
|
||||
run: system_printers,
|
||||
|
||||
@@ -869,3 +869,14 @@ fn windows_pdf_component_is_detected_by_loading_its_dll() {
|
||||
fn the_windows_pdf_check_is_a_no_op_elsewhere() {
|
||||
assert!(require_windows_pdf().is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn printer_queries_refuse_dry_runs_before_contacting_a_backend() {
|
||||
let mut s = Session::new();
|
||||
for command in ["file.printers", "file.print.printers", "file.print.page"] {
|
||||
for params in [json!({"dryRun": true}), json!({"dryRun": true, "printServer": "127.0.0.1:1", "printer": "Synthetic unavailable printer"})] {
|
||||
let error = s.execute(command, params).unwrap_err().to_string();
|
||||
assert!(error.contains("printer queries are unavailable in a dry run"), "{command}: {error}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user