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-07-29 21:02:21 +03:00
|
|
|
|
2020-08-05 14:26:13 +03:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2020-08-06 07:28:01 +03:00
|
|
|
nsPrinterCUPS::nsPrinterCUPS(const nsCUPSShim& aShim, cups_dest_t* aPrinter,
|
|
|
|
const nsAString& aDisplayName)
|
|
|
|
: mDisplayName(aDisplayName), mShim(aShim) {
|
2020-07-29 21:02:21 +03:00
|
|
|
MOZ_ASSERT(aPrinter);
|
|
|
|
MOZ_ASSERT(mShim.IsInitialized());
|
2020-08-06 03:12:07 +03:00
|
|
|
DebugOnly<const int> numCopied = aShim.cupsCopyDest(aPrinter, 0, &mPrinter);
|
2020-07-29 21:02:21 +03:00
|
|
|
MOZ_ASSERT(numCopied == 1);
|
2020-08-06 03:12:07 +03:00
|
|
|
mPrinterInfo = aShim.cupsCopyDestInfo(CUPS_HTTP_DEFAULT, mPrinter);
|
2020-07-29 21:02:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsPrinterCUPS::~nsPrinterCUPS() {
|
|
|
|
if (mPrinterInfo) {
|
2020-08-06 03:12:07 +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 03:12:07 +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-04 15:06:37 +03:00
|
|
|
// static
|
2020-08-06 07:28:01 +03:00
|
|
|
already_AddRefed<nsPrinterCUPS> nsPrinterCUPS::Create(
|
|
|
|
const nsCUPSShim& aShim, cups_dest_t* aPrinter,
|
|
|
|
const nsAString& aDisplayName) {
|
|
|
|
return do_AddRef(new nsPrinterCUPS(aShim, aPrinter, aDisplayName));
|
2020-08-04 15:06:37 +03:00
|
|
|
}
|
2020-07-29 21:02:21 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrinterCUPS::GetName(nsAString& aName) {
|
2020-08-06 04:42:13 +03:00
|
|
|
if (mDisplayName.IsEmpty()) {
|
|
|
|
aName = NS_ConvertUTF8toUTF16(mPrinter->name);
|
|
|
|
} else {
|
|
|
|
aName = mDisplayName;
|
|
|
|
}
|
2020-07-29 21:02:21 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
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-04 15:06:37 +03:00
|
|
|
bool nsPrinterCUPS::SupportsColor() const { return false; }
|
2020-08-03 14:45:35 +03:00
|
|
|
|
2020-07-29 21:02:21 +03:00
|
|
|
bool nsPrinterCUPS::Supports(const char* option, const char* value) const {
|
|
|
|
MOZ_ASSERT(mPrinterInfo);
|
2020-08-06 03:12:07 +03:00
|
|
|
return mShim.cupsCheckDestSupported(CUPS_HTTP_DEFAULT, mPrinter, mPrinterInfo,
|
|
|
|
option, value);
|
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 03:12:07 +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 03:12:07 +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) {
|
|
|
|
cups_size_t info;
|
2020-08-06 03:12:07 +03:00
|
|
|
int getInfoSucceded =
|
|
|
|
mShim.cupsGetDestMediaByIndex(CUPS_HTTP_DEFAULT, mPrinter, mPrinterInfo,
|
|
|
|
i, CUPS_MEDIA_FLAGS_DEFAULT, &info);
|
2020-08-05 04:24:49 +03:00
|
|
|
|
|
|
|
if (!getInfoSucceded) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// localizedName is owned by mPrinterInfo.
|
|
|
|
// https://www.cups.org/doc/cupspm.html#cupsLocalizeDestMedia
|
2020-08-06 03:12:07 +03:00
|
|
|
const char* localizedName = mShim.cupsLocalizeDestMedia(
|
2020-08-05 04:24:49 +03:00
|
|
|
connection, mPrinter, mPrinterInfo, CUPS_MEDIA_FLAGS_DEFAULT, &info);
|
|
|
|
|
|
|
|
if (!localizedName) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX Do we actually have the guarantee that this is utf-8?
|
|
|
|
NS_ConvertUTF8toUTF16 name(localizedName);
|
|
|
|
const double kPointsPerHundredthMillimeter = 0.0283465;
|
|
|
|
|
2020-08-05 14:26:13 +03:00
|
|
|
paperList.AppendElement(PaperInfo{
|
2020-08-05 04:24:49 +03:00
|
|
|
std::move(name),
|
2020-08-05 14:26:13 +03:00
|
|
|
{info.width * kPointsPerHundredthMillimeter,
|
|
|
|
info.length * kPointsPerHundredthMillimeter},
|
|
|
|
Some(MarginDouble{info.top * kPointsPerHundredthMillimeter,
|
|
|
|
info.right * kPointsPerHundredthMillimeter,
|
|
|
|
info.bottom * kPointsPerHundredthMillimeter,
|
|
|
|
info.left * kPointsPerHundredthMillimeter}),
|
2020-08-05 04:24:49 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-06 03:12:07 +03:00
|
|
|
mShim.httpClose(connection);
|
2020-08-05 04:24:49 +03:00
|
|
|
return paperList;
|
|
|
|
}
|