2020-08-07 15:04:26 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "nsPrinterListCUPS.h"
|
|
|
|
|
2020-08-07 17:30:33 +03:00
|
|
|
#include "mozilla/IntegerRange.h"
|
2020-12-24 05:49:02 +03:00
|
|
|
#include "mozilla/Maybe.h"
|
2022-01-03 20:20:03 +03:00
|
|
|
#include "mozilla/StaticPrefs_print.h"
|
2020-08-07 15:04:26 +03:00
|
|
|
#include "nsCUPSShim.h"
|
|
|
|
#include "nsPrinterCUPS.h"
|
2020-08-07 17:30:33 +03:00
|
|
|
#include "nsString.h"
|
2020-08-07 15:04:26 +03:00
|
|
|
#include "prenv.h"
|
|
|
|
|
2020-11-04 21:56:13 +03:00
|
|
|
// Use a local static to initialize the CUPS shim lazily, when it's needed.
|
|
|
|
// This is used in order to avoid a global constructor.
|
|
|
|
static nsCUPSShim& CupsShim() {
|
|
|
|
static nsCUPSShim sCupsShim;
|
|
|
|
return sCupsShim;
|
|
|
|
}
|
|
|
|
|
2020-08-10 01:05:57 +03:00
|
|
|
using PrinterInfo = nsPrinterListBase::PrinterInfo;
|
2020-08-07 15:04:26 +03:00
|
|
|
|
2020-08-10 22:22:27 +03:00
|
|
|
/**
|
|
|
|
* Retrieves a human-readable name for the printer from CUPS.
|
|
|
|
* https://www.cups.org/doc/cupspm.html#basic-destination-information
|
|
|
|
*/
|
|
|
|
static void GetDisplayNameForPrinter(const cups_dest_t& aDest,
|
|
|
|
nsAString& aName) {
|
|
|
|
// macOS clients expect prettified printer names
|
|
|
|
// while GTK clients expect non-prettified names.
|
2020-08-20 14:59:46 +03:00
|
|
|
// If you change this, please change NamedPrinter accordingly.
|
2020-08-10 22:22:27 +03:00
|
|
|
#ifdef XP_MACOSX
|
2020-11-04 21:56:13 +03:00
|
|
|
const char* displayName = CupsShim().cupsGetOption(
|
|
|
|
"printer-info", aDest.num_options, aDest.options);
|
2020-08-10 22:22:27 +03:00
|
|
|
if (displayName) {
|
2023-04-01 11:31:12 +03:00
|
|
|
CopyUTF8toUTF16(mozilla::MakeStringSpan(displayName), aName);
|
2020-08-10 22:22:27 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-10-02 02:14:32 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrinterListCUPS::InitPrintSettingsFromPrinter(
|
|
|
|
const nsAString& aPrinterName, nsIPrintSettings* aPrintSettings) {
|
|
|
|
MOZ_ASSERT(aPrintSettings);
|
|
|
|
|
|
|
|
// Set a default file name.
|
|
|
|
nsAutoString filename;
|
|
|
|
nsresult rv = aPrintSettings->GetToFileName(filename);
|
|
|
|
if (NS_FAILED(rv) || filename.IsEmpty()) {
|
|
|
|
const char* path = PR_GetEnv("PWD");
|
|
|
|
if (!path) {
|
|
|
|
path = PR_GetEnv("HOME");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path) {
|
|
|
|
CopyUTF8toUTF16(mozilla::MakeStringSpan(path), filename);
|
|
|
|
filename.AppendLiteral("/mozilla.pdf");
|
|
|
|
} else {
|
|
|
|
filename.AssignLiteral("mozilla.pdf");
|
|
|
|
}
|
|
|
|
|
|
|
|
aPrintSettings->SetToFileName(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
aPrintSettings->SetIsInitializedFromPrinter(true);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-10-15 08:02:46 +03:00
|
|
|
static int CupsDestCallback(void* user_data, unsigned aFlags,
|
|
|
|
cups_dest_t* aDest) {
|
|
|
|
MOZ_ASSERT(user_data);
|
2021-03-29 13:08:44 +03:00
|
|
|
auto* printerInfoList = static_cast<nsTArray<PrinterInfo>*>(user_data);
|
2020-10-15 08:02:46 +03:00
|
|
|
|
|
|
|
cups_dest_t* ownedDest = nullptr;
|
|
|
|
mozilla::DebugOnly<const int> numCopied =
|
2020-11-04 21:56:13 +03:00
|
|
|
CupsShim().cupsCopyDest(aDest, 0, &ownedDest);
|
2020-10-15 08:02:46 +03:00
|
|
|
MOZ_ASSERT(numCopied == 1);
|
|
|
|
|
|
|
|
nsString name;
|
|
|
|
GetDisplayNameForPrinter(*aDest, name);
|
|
|
|
|
|
|
|
printerInfoList->AppendElement(PrinterInfo{std::move(name), ownedDest});
|
|
|
|
|
|
|
|
return aFlags == CUPS_DEST_FLAGS_MORE ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2020-08-10 02:29:59 +03:00
|
|
|
nsTArray<PrinterInfo> nsPrinterListCUPS::Printers() const {
|
2020-11-04 21:56:13 +03:00
|
|
|
if (!CupsShim().InitOkay()) {
|
2020-08-10 01:05:57 +03:00
|
|
|
return {};
|
2020-08-07 15:04:26 +03:00
|
|
|
}
|
|
|
|
|
2022-01-03 20:20:03 +03:00
|
|
|
auto FreeDestsAndClear = [](nsTArray<PrinterInfo>& aArray) {
|
|
|
|
for (auto& info : aArray) {
|
|
|
|
CupsShim().cupsFreeDests(1, static_cast<cups_dest_t*>(info.mCupsHandle));
|
|
|
|
}
|
|
|
|
aArray.Clear();
|
|
|
|
};
|
|
|
|
|
2020-08-10 01:05:57 +03:00
|
|
|
nsTArray<PrinterInfo> printerInfoList;
|
2022-06-22 10:52:59 +03:00
|
|
|
// 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<const int> 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);
|
2022-01-03 20:20:03 +03:00
|
|
|
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):
|
|
|
|
if (CupsShim().cupsEnumDests(
|
|
|
|
CUPS_DEST_FLAGS_NONE,
|
|
|
|
0 /* 0 timeout should be okay when masking CUPS_PRINTER_DISCOVERED */,
|
2020-10-15 08:02:46 +03:00
|
|
|
nullptr /* cancel* */, CUPS_PRINTER_LOCAL,
|
|
|
|
CUPS_PRINTER_FAX | CUPS_PRINTER_SCANNER | CUPS_PRINTER_DISCOVERED,
|
|
|
|
&CupsDestCallback, &printerInfoList)) {
|
2022-01-03 20:20:03 +03:00
|
|
|
return printerInfoList;
|
2020-08-07 15:04:26 +03:00
|
|
|
}
|
2021-12-30 20:04:29 +03:00
|
|
|
|
2022-01-03 20:20:03 +03:00
|
|
|
// Another error occurred. Maybe printerInfoList could be partially
|
|
|
|
// populated, so perhaps we could return it without clearing it in the hope
|
|
|
|
// that there are some usable dests. However, presuambly CUPS doesn't
|
|
|
|
// guarantee that any dests that it added are complete and safe to use when
|
|
|
|
// an error occurs?
|
|
|
|
FreeDestsAndClear(printerInfoList);
|
|
|
|
return {};
|
2020-08-10 01:05:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<nsIPrinter> nsPrinterListCUPS::CreatePrinter(PrinterInfo aInfo) const {
|
|
|
|
return mozilla::MakeRefPtr<nsPrinterCUPS>(
|
2020-11-04 21:56:13 +03:00
|
|
|
mCommonPaperInfo, CupsShim(), std::move(aInfo.mName),
|
2020-09-10 02:56:13 +03:00
|
|
|
static_cast<cups_dest_t*>(aInfo.mCupsHandle));
|
2020-08-07 15:04:26 +03:00
|
|
|
}
|
|
|
|
|
2020-12-24 05:49:02 +03:00
|
|
|
mozilla::Maybe<PrinterInfo> nsPrinterListCUPS::PrinterByName(
|
2020-08-20 14:59:46 +03:00
|
|
|
nsString aPrinterName) const {
|
2020-12-24 05:49:02 +03:00
|
|
|
mozilla::Maybe<PrinterInfo> rv;
|
2020-11-04 21:56:13 +03:00
|
|
|
if (!CupsShim().InitOkay()) {
|
2020-08-20 14:59:46 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Will contain the printer, if found. This must be fully owned, and not a
|
|
|
|
// member of another array of printers.
|
|
|
|
cups_dest_t* printer = nullptr;
|
|
|
|
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
// On OS X the printer name given to this function is the readable/display
|
|
|
|
// name and not the CUPS name, so we iterate over all the printers for now.
|
|
|
|
// See bug 1659807 for one approach to improve perf here.
|
|
|
|
{
|
|
|
|
nsAutoCString printerName;
|
|
|
|
CopyUTF16toUTF8(aPrinterName, printerName);
|
|
|
|
cups_dest_t* printers = nullptr;
|
2020-11-04 21:56:13 +03:00
|
|
|
const auto numPrinters = CupsShim().cupsGetDests(&printers);
|
2020-08-20 14:59:46 +03:00
|
|
|
for (auto i : mozilla::IntegerRange(0, numPrinters)) {
|
2020-11-04 21:56:13 +03:00
|
|
|
const char* const displayName = CupsShim().cupsGetOption(
|
2020-08-20 14:59:46 +03:00
|
|
|
"printer-info", printers[i].num_options, printers[i].options);
|
|
|
|
if (printerName == displayName) {
|
2020-11-04 21:56:13 +03:00
|
|
|
// The second arg to CupsShim().cupsCopyDest is called num_dests, but
|
2020-08-26 00:57:11 +03:00
|
|
|
// it actually copies num_dests + 1 elements.
|
2020-11-04 21:56:13 +03:00
|
|
|
CupsShim().cupsCopyDest(printers + i, 0, &printer);
|
2020-08-20 14:59:46 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-11-04 21:56:13 +03:00
|
|
|
CupsShim().cupsFreeDests(numPrinters, printers);
|
2020-08-20 14:59:46 +03:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
// On GTK, we only ever show the CUPS name of printers, so we can use
|
|
|
|
// cupsGetNamedDest directly.
|
|
|
|
{
|
|
|
|
const auto printerName = NS_ConvertUTF16toUTF8(aPrinterName);
|
2020-11-04 21:56:13 +03:00
|
|
|
printer = CupsShim().cupsGetNamedDest(CUPS_HTTP_DEFAULT, printerName.get(),
|
|
|
|
nullptr);
|
2020-08-20 14:59:46 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (printer) {
|
|
|
|
// Since the printer name had to be passed by-value, we can move the
|
|
|
|
// name from that.
|
2020-09-10 02:56:13 +03:00
|
|
|
rv.emplace(PrinterInfo{std::move(aPrinterName), printer});
|
2020-08-20 14:59:46 +03:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2020-12-24 05:49:02 +03:00
|
|
|
mozilla::Maybe<PrinterInfo> nsPrinterListCUPS::PrinterBySystemName(
|
2020-09-16 00:23:27 +03:00
|
|
|
nsString aPrinterName) const {
|
2020-12-24 05:49:02 +03:00
|
|
|
mozilla::Maybe<PrinterInfo> rv;
|
2020-11-04 21:56:13 +03:00
|
|
|
if (!CupsShim().InitOkay()) {
|
2020-09-16 00:23:27 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto printerName = NS_ConvertUTF16toUTF8(aPrinterName);
|
2020-11-04 21:56:13 +03:00
|
|
|
if (cups_dest_t* const printer = CupsShim().cupsGetNamedDest(
|
2020-09-16 00:23:27 +03:00
|
|
|
CUPS_HTTP_DEFAULT, printerName.get(), nullptr)) {
|
|
|
|
rv.emplace(PrinterInfo{std::move(aPrinterName), printer});
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
2020-10-02 02:14:32 +03:00
|
|
|
|
2020-08-20 14:59:46 +03:00
|
|
|
nsresult nsPrinterListCUPS::SystemDefaultPrinterName(nsAString& aName) const {
|
2020-08-10 01:05:57 +03:00
|
|
|
aName.Truncate();
|
|
|
|
|
2020-11-04 21:56:13 +03:00
|
|
|
if (!CupsShim().InitOkay()) {
|
2021-03-04 19:03:30 +03:00
|
|
|
return NS_OK;
|
2020-08-07 15:04:26 +03:00
|
|
|
}
|
|
|
|
|
2020-08-07 17:30:33 +03:00
|
|
|
// Passing in nullptr for the name will return the default, if any.
|
2020-08-25 00:17:53 +03:00
|
|
|
cups_dest_t* dest =
|
2020-11-04 21:56:13 +03:00
|
|
|
CupsShim().cupsGetNamedDest(CUPS_HTTP_DEFAULT, /* name */ nullptr,
|
|
|
|
/* instance */ nullptr);
|
2020-08-07 15:04:26 +03:00
|
|
|
if (!dest) {
|
2020-08-10 01:05:57 +03:00
|
|
|
return NS_OK;
|
2020-08-07 15:04:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
GetDisplayNameForPrinter(*dest, aName);
|
|
|
|
if (aName.IsEmpty()) {
|
|
|
|
CopyUTF8toUTF16(mozilla::MakeStringSpan(dest->name), aName);
|
|
|
|
}
|
|
|
|
|
2020-11-04 21:56:13 +03:00
|
|
|
CupsShim().cupsFreeDests(1, dest);
|
2020-08-07 15:04:26 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|