2020-07-29 21:02:21 +03:00
|
|
|
/* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 2; -*- */
|
|
|
|
/* 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 "nsPrinterCUPS.h"
|
2020-08-05 04:24:49 +03:00
|
|
|
#include "nsPaper.h"
|
|
|
|
#include "nsPrinterBase.h"
|
2020-08-20 09:11:08 +03:00
|
|
|
#include "nsPrintSettingsImpl.h"
|
2020-07-29 21:02:21 +03:00
|
|
|
|
2020-08-14 23:41:59 +03:00
|
|
|
#include "plstr.h"
|
2020-08-05 14:26:13 +03:00
|
|
|
|
2020-08-20 09:11:08 +03:00
|
|
|
static PaperInfo MakePaperInfo(const char* aName, const cups_size_t& aMedia) {
|
|
|
|
// XXX Do we actually have the guarantee that this is utf-8?
|
|
|
|
NS_ConvertUTF8toUTF16 name(aName ? aName : aMedia.media);
|
|
|
|
const double kPointsPerHundredthMillimeter = 0.0283465;
|
|
|
|
return PaperInfo(
|
|
|
|
name,
|
|
|
|
{aMedia.width * kPointsPerHundredthMillimeter,
|
|
|
|
aMedia.length * kPointsPerHundredthMillimeter},
|
|
|
|
Some(MarginDouble{aMedia.top * kPointsPerHundredthMillimeter,
|
|
|
|
aMedia.right * kPointsPerHundredthMillimeter,
|
|
|
|
aMedia.bottom * kPointsPerHundredthMillimeter,
|
|
|
|
aMedia.left * kPointsPerHundredthMillimeter}));
|
|
|
|
}
|
|
|
|
|
2020-07-29 21:02:21 +03:00
|
|
|
nsPrinterCUPS::~nsPrinterCUPS() {
|
|
|
|
if (mPrinterInfo) {
|
2020-08-06 20:01:21 +03:00
|
|
|
mShim.cupsFreeDestInfo(mPrinterInfo);
|
2020-08-05 04:24:49 +03:00
|
|
|
mPrinterInfo = nullptr;
|
2020-07-29 21:02:21 +03:00
|
|
|
}
|
|
|
|
if (mPrinter) {
|
2020-08-06 20:01:21 +03:00
|
|
|
mShim.cupsFreeDests(1, mPrinter);
|
2020-08-05 04:24:49 +03:00
|
|
|
mPrinter = nullptr;
|
2020-07-29 21:02:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-20 09:11:08 +03:00
|
|
|
PrintSettingsInitializer nsPrinterCUPS::DefaultSettings() const {
|
|
|
|
nsString printerName;
|
|
|
|
GetPrinterName(printerName);
|
|
|
|
|
|
|
|
cups_size_t media;
|
|
|
|
|
|
|
|
bool hasDefaultMedia =
|
|
|
|
mShim.cupsGetDestMediaDefault(CUPS_HTTP_DEFAULT, mPrinter, mPrinterInfo,
|
|
|
|
CUPS_MEDIA_FLAGS_DEFAULT, &media);
|
|
|
|
|
|
|
|
if (!hasDefaultMedia) {
|
|
|
|
Nothing();
|
|
|
|
return PrintSettingsInitializer{
|
|
|
|
std::move(printerName),
|
|
|
|
PaperInfo(),
|
|
|
|
SupportsColor(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* localizedName = nullptr;
|
|
|
|
|
|
|
|
// blocking call
|
|
|
|
http_t* connection = mShim.cupsConnectDest(mPrinter, CUPS_DEST_FLAGS_NONE,
|
|
|
|
/* timeout(ms) */ 5000,
|
|
|
|
/* cancel */ nullptr,
|
|
|
|
/* resource */ nullptr,
|
|
|
|
/* resourcesize */ 0,
|
|
|
|
/* callback */ nullptr,
|
|
|
|
/* user_data */ nullptr);
|
|
|
|
|
|
|
|
if (connection) {
|
|
|
|
localizedName = LocalizeMediaName(*connection, media);
|
|
|
|
mShim.httpClose(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
return PrintSettingsInitializer{
|
|
|
|
std::move(printerName),
|
|
|
|
MakePaperInfo(localizedName, media),
|
|
|
|
SupportsColor(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-07-29 21:02:21 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrinterCUPS::GetName(nsAString& aName) {
|
2020-08-20 09:11:08 +03:00
|
|
|
GetPrinterName(aName);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsPrinterCUPS::GetPrinterName(nsAString& aName) const {
|
2020-08-06 04:42:13 +03:00
|
|
|
if (mDisplayName.IsEmpty()) {
|
2020-08-10 22:22:27 +03:00
|
|
|
aName.Truncate();
|
|
|
|
CopyUTF8toUTF16(MakeStringSpan(mPrinter->name), aName);
|
2020-08-06 04:42:13 +03:00
|
|
|
} else {
|
|
|
|
aName = mDisplayName;
|
|
|
|
}
|
2020-08-20 09:11:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* nsPrinterCUPS::LocalizeMediaName(http_t& aConnection,
|
|
|
|
cups_size_t& aMedia) const {
|
2020-08-22 04:43:33 +03:00
|
|
|
// We want to localize the name on macOS, but not on Linux.
|
|
|
|
#ifdef XP_MACOSX
|
2020-08-20 09:11:08 +03:00
|
|
|
// The returned string is owned by mPrinterInfo.
|
|
|
|
// https://www.cups.org/doc/cupspm.html#cupsLocalizeDestMedia
|
|
|
|
return mShim.cupsLocalizeDestMedia(&aConnection, mPrinter, mPrinterInfo,
|
|
|
|
CUPS_MEDIA_FLAGS_DEFAULT, &aMedia);
|
2020-08-22 04:43:33 +03:00
|
|
|
#else
|
|
|
|
return nullptr;
|
|
|
|
#endif
|
2020-07-29 21:02:21 +03:00
|
|
|
}
|
|
|
|
|
2020-08-04 15:06:37 +03:00
|
|
|
bool nsPrinterCUPS::SupportsDuplex() const {
|
|
|
|
return Supports(CUPS_SIDES, CUPS_SIDES_TWO_SIDED_PORTRAIT);
|
2020-07-29 21:02:21 +03:00
|
|
|
}
|
|
|
|
|
2020-08-06 23:57:43 +03:00
|
|
|
bool nsPrinterCUPS::SupportsColor() const {
|
2020-09-03 02:49:08 +03:00
|
|
|
// CUPS 2.1 (particularly as used in Ubuntu 16) is known to have innaccurate
|
|
|
|
// results for CUPS_PRINT_COLOR_MODE.
|
|
|
|
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1660658#c15
|
|
|
|
if (!IsCUPSVersionAtLeast(2, 2, 0)) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-08-28 22:15:21 +03:00
|
|
|
return Supports(CUPS_PRINT_COLOR_MODE, CUPS_PRINT_COLOR_MODE_COLOR) ||
|
|
|
|
Supports(CUPS_PRINT_COLOR_MODE, CUPS_PRINT_COLOR_MODE_AUTO);
|
2020-08-06 23:57:43 +03:00
|
|
|
}
|
2020-08-03 14:45:35 +03:00
|
|
|
|
2020-08-14 23:41:59 +03:00
|
|
|
bool nsPrinterCUPS::SupportsCollation() const {
|
|
|
|
// We can't depend on cupsGetIntegerOption existing.
|
|
|
|
const char* const value = mShim.cupsGetOption(
|
|
|
|
"printer-type", mPrinter->num_options, mPrinter->options);
|
|
|
|
if (!value) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// If the value is non-numeric, then atoi will return 0, which will still
|
|
|
|
// cause this function to return false.
|
|
|
|
const int type = atoi(value);
|
|
|
|
return type & CUPS_PRINTER_COLLATE;
|
|
|
|
}
|
|
|
|
|
2020-09-03 02:49:08 +03:00
|
|
|
bool nsPrinterCUPS::Supports(const char* aOption, const char* aValue) const {
|
2020-07-29 21:02:21 +03:00
|
|
|
MOZ_ASSERT(mPrinterInfo);
|
2020-08-06 20:01:21 +03:00
|
|
|
return mShim.cupsCheckDestSupported(CUPS_HTTP_DEFAULT, mPrinter, mPrinterInfo,
|
2020-09-03 02:49:08 +03:00
|
|
|
aOption, aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nsPrinterCUPS::IsCUPSVersionAtLeast(uint64_t aCUPSMajor,
|
|
|
|
uint64_t aCUPSMinor,
|
|
|
|
uint64_t aCUPSPatch) const {
|
|
|
|
// Compare major version.
|
|
|
|
if (mCUPSMajor > aCUPSMajor) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (mCUPSMajor < aCUPSMajor) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare minor version.
|
|
|
|
if (mCUPSMinor > aCUPSMinor) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (mCUPSMinor < aCUPSMinor) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare patch.
|
|
|
|
return aCUPSPatch <= mCUPSPatch;
|
2020-07-29 21:02:21 +03:00
|
|
|
}
|
2020-08-05 04:24:49 +03:00
|
|
|
|
2020-08-05 14:26:13 +03:00
|
|
|
nsTArray<PaperInfo> nsPrinterCUPS::PaperList() const {
|
2020-08-05 04:24:49 +03:00
|
|
|
if (!mPrinterInfo) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:01:21 +03:00
|
|
|
const int paperCount = mShim.cupsGetDestMediaCount(
|
2020-08-05 04:24:49 +03:00
|
|
|
CUPS_HTTP_DEFAULT, mPrinter, mPrinterInfo, CUPS_MEDIA_FLAGS_DEFAULT);
|
|
|
|
|
|
|
|
// blocking call
|
2020-08-06 20:01:21 +03:00
|
|
|
http_t* connection = mShim.cupsConnectDest(mPrinter, CUPS_DEST_FLAGS_NONE,
|
|
|
|
/* timeout(ms) */ 5000,
|
|
|
|
/* cancel */ nullptr,
|
|
|
|
/* resource */ nullptr,
|
|
|
|
/* resourcesize */ 0,
|
|
|
|
/* callback */ nullptr,
|
|
|
|
/* user_data */ nullptr);
|
2020-08-05 04:24:49 +03:00
|
|
|
|
|
|
|
if (!connection) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-08-05 14:26:13 +03:00
|
|
|
nsTArray<PaperInfo> paperList;
|
2020-08-05 04:24:49 +03:00
|
|
|
for (int i = 0; i < paperCount; ++i) {
|
2020-08-20 09:11:08 +03:00
|
|
|
cups_size_t media;
|
2020-08-06 20:01:21 +03:00
|
|
|
int getInfoSucceded =
|
|
|
|
mShim.cupsGetDestMediaByIndex(CUPS_HTTP_DEFAULT, mPrinter, mPrinterInfo,
|
2020-08-20 09:11:08 +03:00
|
|
|
i, CUPS_MEDIA_FLAGS_DEFAULT, &media);
|
2020-08-05 04:24:49 +03:00
|
|
|
|
|
|
|
if (!getInfoSucceded) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-20 09:11:08 +03:00
|
|
|
paperList.AppendElement(
|
|
|
|
MakePaperInfo(LocalizeMediaName(*connection, media), media));
|
2020-08-05 04:24:49 +03:00
|
|
|
}
|
|
|
|
|
2020-08-06 20:01:21 +03:00
|
|
|
mShim.httpClose(connection);
|
2020-08-05 04:24:49 +03:00
|
|
|
return paperList;
|
|
|
|
}
|