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-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"
|
|
|
|
|
|
|
|
static nsCUPSShim sCupsShim;
|
2020-08-10 01:05:57 +03:00
|
|
|
using PrinterInfo = nsPrinterListBase::PrinterInfo;
|
2020-08-07 15:04:26 +03:00
|
|
|
|
2020-08-10 02:29:59 +03:00
|
|
|
nsTArray<PrinterInfo> nsPrinterListCUPS::Printers() const {
|
2020-08-07 15:07:48 +03:00
|
|
|
if (!sCupsShim.EnsureInitialized()) {
|
2020-08-10 01:05:57 +03:00
|
|
|
return {};
|
2020-08-07 15:04:26 +03:00
|
|
|
}
|
|
|
|
|
2020-08-10 01:05:57 +03:00
|
|
|
nsTArray<PrinterInfo> printerInfoList;
|
|
|
|
|
2020-08-07 17:30:33 +03:00
|
|
|
cups_dest_t* printers = nullptr;
|
|
|
|
auto numPrinters = sCupsShim.cupsGetDests(&printers);
|
2020-08-10 01:05:57 +03:00
|
|
|
printerInfoList.SetCapacity(numPrinters);
|
2020-08-07 15:04:26 +03:00
|
|
|
|
2020-08-07 17:30:33 +03:00
|
|
|
for (auto i : mozilla::IntegerRange(0, numPrinters)) {
|
|
|
|
cups_dest_t* dest = printers + i;
|
2020-08-07 15:04:26 +03:00
|
|
|
|
2020-08-10 01:05:57 +03:00
|
|
|
cups_dest_t* ownedDest = nullptr;
|
|
|
|
mozilla::DebugOnly<const int> numCopied =
|
|
|
|
sCupsShim.cupsCopyDest(dest, 0, &ownedDest);
|
|
|
|
MOZ_ASSERT(numCopied == 1);
|
|
|
|
|
|
|
|
cups_dinfo_t* ownedInfo =
|
|
|
|
sCupsShim.cupsCopyDestInfo(CUPS_HTTP_DEFAULT, ownedDest);
|
|
|
|
|
|
|
|
nsString name;
|
|
|
|
GetDisplayNameForPrinter(*dest, name);
|
2020-08-07 15:04:26 +03:00
|
|
|
|
2020-08-10 01:05:57 +03:00
|
|
|
printerInfoList.AppendElement(
|
|
|
|
PrinterInfo{std::move(name), {ownedDest, ownedInfo}});
|
2020-08-07 15:04:26 +03:00
|
|
|
}
|
|
|
|
|
2020-08-07 17:30:33 +03:00
|
|
|
sCupsShim.cupsFreeDests(numPrinters, printers);
|
2020-08-10 01:05:57 +03:00
|
|
|
return printerInfoList;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<nsIPrinter> nsPrinterListCUPS::CreatePrinter(PrinterInfo aInfo) const {
|
|
|
|
return mozilla::MakeRefPtr<nsPrinterCUPS>(
|
|
|
|
sCupsShim, std::move(aInfo.mName),
|
|
|
|
static_cast<cups_dest_t*>(aInfo.mCupsHandles[0]),
|
|
|
|
static_cast<cups_dinfo_t*>(aInfo.mCupsHandles[1]));
|
2020-08-07 15:04:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrinterListCUPS::GetSystemDefaultPrinterName(nsAString& aName) {
|
2020-08-10 01:05:57 +03:00
|
|
|
aName.Truncate();
|
|
|
|
|
2020-08-07 15:07:48 +03:00
|
|
|
if (!sCupsShim.EnsureInitialized()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
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.
|
|
|
|
const cups_dest_t* dest =
|
|
|
|
sCupsShim.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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|