From b0dc7110a909458203328659050e6d7678835b3a Mon Sep 17 00:00:00 2001 From: Jan Horak Date: Wed, 22 Jun 2022 07:52:59 +0000 Subject: [PATCH] Bug 1771500 Show all available printers; r=stransky Allow to continue enumeration of the printers for all protocols (ie. ipp and ipps). Following advice given by cups guys to use cupsGetDests2 to avoid duplicates of printers in the list: https://bugzilla.redhat.com/show_bug.cgi?id=1983403#c18 Differential Revision: https://phabricator.services.mozilla.com/D147508 --- widget/nsCUPSShim.h | 1 + widget/nsPrinterListCUPS.cpp | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/widget/nsCUPSShim.h b/widget/nsCUPSShim.h index 29816bc17eba..8ab00a48ad51 100644 --- a/widget/nsCUPSShim.h +++ b/widget/nsCUPSShim.h @@ -56,6 +56,7 @@ class nsCUPSShim { X(Optional::Yes, cupsLocalizeDestMedia) \ X(Optional::No, cupsGetDest) \ X(Optional::No, cupsGetDests) \ + X(Optional::No, cupsGetDests2) \ X(Optional::No, cupsGetNamedDest) \ X(Optional::No, cupsGetOption) \ X(Optional::No, cupsServer) \ diff --git a/widget/nsPrinterListCUPS.cpp b/widget/nsPrinterListCUPS.cpp index 4ebf1390b006..6b215e21f4ef 100644 --- a/widget/nsPrinterListCUPS.cpp +++ b/widget/nsPrinterListCUPS.cpp @@ -98,18 +98,27 @@ nsTArray nsPrinterListCUPS::Printers() const { }; nsTArray printerInfoList; - if (CupsShim().cupsEnumDests( - CUPS_DEST_FLAGS_NONE, - int(StaticPrefs::print_cups_enum_dests_timeout_ms()), - nullptr /* cancel* */, CUPS_PRINTER_LOCAL, - CUPS_PRINTER_FAX | CUPS_PRINTER_SCANNER, &CupsDestCallback, - &printerInfoList)) { + // cupsGetDests2 returns list of found printers without duplicates, unlike + // cupsEnumDests + cups_dest_t* printers = nullptr; + const auto numPrinters = CupsShim().cupsGetDests2(nullptr, &printers); + if (numPrinters > 0) { + for (auto i : mozilla::IntegerRange(0, numPrinters)) { + cups_dest_t* ownedDest = nullptr; + mozilla::DebugOnly numCopied = + CupsShim().cupsCopyDest(printers + i, 0, &ownedDest); + MOZ_ASSERT(numCopied == 1); + + nsString name; + GetDisplayNameForPrinter(*(printers + i), name); + printerInfoList.AppendElement(PrinterInfo{std::move(name), ownedDest}); + } + CupsShim().cupsFreeDests(numPrinters, printers); return printerInfoList; } // An error occurred - retry with CUPS_PRINTER_DISCOVERED masked out (since // it looks like there are a lot of error cases for that in cupsEnumDests): - FreeDestsAndClear(printerInfoList); if (CupsShim().cupsEnumDests( CUPS_DEST_FLAGS_NONE, 0 /* 0 timeout should be okay when masking CUPS_PRINTER_DISCOVERED */,