Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qppdprintdevice.cpp
Go to the documentation of this file.
1// Copyright (C) 2014 John Layt <jlayt@kde.org>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
7#include "private/qcups_p.h" // Only needed for PDPK_*
8
9#if QT_CONFIG(mimetype)
10#include <QtCore/QMimeDatabase>
11#endif
12
13#ifndef QT_LINUXBASE // LSB merges everything into cups.h
14#include <cups/language.h>
15#endif
16
18
19// avoid all the warnings about using deprecated API from CUPS (as there is no real replacement)
20QT_WARNING_PUSH
21QT_WARNING_DISABLE_DEPRECATED
22
23QPpdPrintDevice::QPpdPrintDevice(const QString &id)
24 : QPlatformPrintDevice(id),
25 m_cupsDest(0),
26 m_ppd(0)
27{
28 if (!id.isEmpty()) {
29
30 // TODO For now each dest is an individual device
31 const auto parts = QStringView{id}.split(u'/');
32 m_cupsName = parts.at(0).toUtf8();
33 if (parts.size() > 1)
34 m_cupsInstance = parts.at(1).toUtf8();
35
36 // Get the print instance and PPD file
37 m_cupsDest = cupsGetNamedDest(CUPS_HTTP_DEFAULT, m_cupsName, m_cupsInstance.isNull() ? nullptr : m_cupsInstance.constData());
38 if (m_cupsDest) {
39 const char *ppdFile = cupsGetPPD(m_cupsName);
40 if (ppdFile == nullptr) {
41 // For auto-discovered IPP printers without a local CUPS queue, CUPS doesn't have a PPD yet.
42 // Request supported values/capabilities, which could be used to read capabilities (IPP attributes) directly.
43 // But for now, we call it only for the useful side-effect that CUPS creates a temp queue
44 // and a PPD file from the IPP attributes for this queue that can be retrieved then.
45 if (cups_dinfo_t *destInfo = cupsCopyDestInfo(CUPS_HTTP_DEFAULT, m_cupsDest)) {
46 cupsFreeDestInfo(destInfo);
47 ppdFile = cupsGetPPD(m_cupsName);
48 }
49 }
50 if (ppdFile) {
51 m_ppd = ppdOpenFile(ppdFile);
52 unlink(ppdFile);
53 }
54 if (m_ppd) {
55 ppdMarkDefaults(m_ppd);
56 cupsMarkOptions(m_ppd, m_cupsDest->num_options, m_cupsDest->options);
57 ppdLocalize(m_ppd);
58
59 m_minimumPhysicalPageSize = QSize(m_ppd->custom_min[0], m_ppd->custom_min[1]);
60 m_maximumPhysicalPageSize = QSize(m_ppd->custom_max[0], m_ppd->custom_max[1]);
61 m_customMargins = QMarginsF(m_ppd->custom_margins[0], m_ppd->custom_margins[3],
62 m_ppd->custom_margins[2], m_ppd->custom_margins[1]);
63 }
64
65 m_name = printerOption("printer-info");
66 m_location = printerOption("printer-location");
67 m_makeAndModel = printerOption("printer-make-and-model");
68 cups_ptype_e type = printerTypeFlags();
69 m_isRemote = type & CUPS_PRINTER_REMOTE;
70 // Note this is if the hardware does multiple copies, not if Cups can
71 m_supportsMultipleCopies = type & CUPS_PRINTER_COPIES;
72 // Note this is if the hardware does collation, not if Cups can
73 m_supportsCollateCopies = type & CUPS_PRINTER_COLLATE;
74
75 // Custom Page Size support
76 // Cups cups_ptype_e CUPS_PRINTER_VARIABLE
77 // Cups ppd_file_t variable_sizes custom_min custom_max
78 // PPD MaxMediaWidth MaxMediaHeight
79 m_supportsCustomPageSizes = type & CUPS_PRINTER_VARIABLE;
80 }
81 }
82}
83
84QPpdPrintDevice::~QPpdPrintDevice()
85{
86 if (m_ppd)
87 ppdClose(m_ppd);
88 if (m_cupsDest)
89 cupsFreeDests(1, m_cupsDest);
90 m_cupsDest = 0;
91 m_ppd = 0;
92}
93
94bool QPpdPrintDevice::isValid() const
95{
96 return m_cupsDest;
97}
98
99bool QPpdPrintDevice::isDefault() const
100{
101 // There seems to be a bug in cups in which printerTypeFlags
102 // returns CUPS_PRINTER_DEFAULT based only on system values, ignoring user lpoptions
103 // so we can't use that. And also there seems to be a bug in which dests returned
104 // by cupsGetNamedDest don't have is_default set at all so we can't use that either
105 // so go the long route and compare our id against the defaultPrintDeviceId
106 return id() == QCupsPrinterSupport::staticDefaultPrintDeviceId();
107}
108
109QPrint::DeviceState QPpdPrintDevice::state() const
110{
111 // 3 = idle, 4 = printing, 5 = stopped
112 // More details available from printer-state-message and printer-state-reasons
113 int state = printerOption(QStringLiteral("printer-state")).toInt();
114 if (state == 3)
115 return QPrint::Idle;
116 else if (state == 4)
117 return QPrint::Active;
118 else
119 return QPrint::Error;
120}
121
122void QPpdPrintDevice::loadPageSizes() const
123{
124 m_pageSizes.clear();
125 m_printableMargins.clear();
126
127 ppd_option_t *pageSizes = ppdFindOption(m_ppd, "PageSize");
128 if (pageSizes) {
129 for (int i = 0; i < pageSizes->num_choices; ++i) {
130 const ppd_size_t *ppdSize = ppdPageSize(m_ppd, pageSizes->choices[i].choice);
131 if (ppdSize) {
132 // Returned size is in points
133 QString key = QString::fromUtf8(ppdSize->name);
134 QSize size = QSize(qRound(ppdSize->width), qRound(ppdSize->length));
135 QString name = QString::fromUtf8(pageSizes->choices[i].text);
136 if (!size.isEmpty()) {
137 QPageSize ps = createPageSize(key, size, name);
138 if (ps.isValid()) {
139 m_pageSizes.append(ps);
140 m_printableMargins.insert(key, QMarginsF(ppdSize->left, ppdSize->length - ppdSize->top,
141 ppdSize->width - ppdSize->right, ppdSize->bottom));
142 }
143 }
144 }
145 }
146 }
147 m_havePageSizes = true;
148}
149
150QPageSize QPpdPrintDevice::defaultPageSize() const
151{
152 ppd_choice_t *defaultChoice = ppdFindMarkedChoice(m_ppd, "PageSize");
153 if (defaultChoice) {
154 ppd_size_t *ppdSize = ppdPageSize(m_ppd, defaultChoice->choice);
155 if (ppdSize) {
156 // Returned size is in points
157 QString key = QString::fromUtf8(ppdSize->name);
158 QSize size = QSize(qRound(ppdSize->width), qRound(ppdSize->length));
159 QString name = QString::fromUtf8(defaultChoice->text);
160 return createPageSize(key, size, name);
161 }
162 }
163 return QPageSize();
164}
165
166QMarginsF QPpdPrintDevice::printableMargins(const QPageSize &pageSize,
167 QPageLayout::Orientation orientation,
168 int resolution) const
169{
170 Q_UNUSED(orientation);
171 Q_UNUSED(resolution);
172 if (!m_havePageSizes)
173 loadPageSizes();
174 // TODO Orientation?
175 if (m_printableMargins.contains(pageSize.key()))
176 return m_printableMargins.value(pageSize.key());
177 return m_customMargins;
178}
179
180void QPpdPrintDevice::loadResolutions() const
181{
182 m_resolutions.clear();
183
184 // Try load standard PPD options first
185 ppd_option_t *resolutions = ppdFindOption(m_ppd, "Resolution");
186 if (resolutions) {
187 for (int i = 0; i < resolutions->num_choices; ++i) {
188 int res = QPrintUtils::parsePpdResolution(resolutions->choices[i].choice);
189 if (res > 0)
190 m_resolutions.append(res);
191 }
192 }
193 // If no result, try just the default
194 if (m_resolutions.size() == 0) {
195 resolutions = ppdFindOption(m_ppd, "DefaultResolution");
196 if (resolutions) {
197 int res = QPrintUtils::parsePpdResolution(resolutions->choices[0].choice);
198 if (res > 0)
199 m_resolutions.append(res);
200 }
201 }
202 // If still no result, then try HP's custom options
203 if (m_resolutions.size() == 0) {
204 resolutions = ppdFindOption(m_ppd, "HPPrintQuality");
205 if (resolutions) {
206 for (int i = 0; i < resolutions->num_choices; ++i) {
207 int res = QPrintUtils::parsePpdResolution(resolutions->choices[i].choice);
208 if (res > 0)
209 m_resolutions.append(res);
210 }
211 }
212 }
213 if (m_resolutions.size() == 0) {
214 resolutions = ppdFindOption(m_ppd, "DefaultHPPrintQuality");
215 if (resolutions) {
216 int res = QPrintUtils::parsePpdResolution(resolutions->choices[0].choice);
217 if (res > 0)
218 m_resolutions.append(res);
219 }
220 }
221 m_haveResolutions = true;
222}
223
224int QPpdPrintDevice::defaultResolution() const
225{
226 // Try load standard PPD option first
227 ppd_option_t *resolution = ppdFindOption(m_ppd, "DefaultResolution");
228 if (resolution) {
229 int res = QPrintUtils::parsePpdResolution(resolution->choices[0].choice);
230 if (res > 0)
231 return res;
232 }
233 // If no result, then try a marked option
234 ppd_choice_t *defaultChoice = ppdFindMarkedChoice(m_ppd, "Resolution");
235 if (defaultChoice) {
236 int res = QPrintUtils::parsePpdResolution(defaultChoice->choice);
237 if (res > 0)
238 return res;
239 }
240 // If still no result, then try HP's custom options
241 resolution = ppdFindOption(m_ppd, "DefaultHPPrintQuality");
242 if (resolution) {
243 int res = QPrintUtils::parsePpdResolution(resolution->choices[0].choice);
244 if (res > 0)
245 return res;
246 }
247 defaultChoice = ppdFindMarkedChoice(m_ppd, "HPPrintQuality");
248 if (defaultChoice) {
249 int res = QPrintUtils::parsePpdResolution(defaultChoice->choice);
250 if (res > 0)
251 return res;
252 }
253 // Otherwise return a sensible default.
254 // TODO What is sensible? 150? 300?
255 return 72;
256}
257
258void QPpdPrintDevice::loadInputSlots() const
259{
260 // NOTE: Implemented in both CUPS and Mac plugins, please keep in sync
261 // TODO Deal with concatenated names like Tray1Manual or Tray1_Man,
262 // will currently show as CustomInputSlot
263 // TODO Deal with separate ManualFeed key
264 // Try load standard PPD options first
265 m_inputSlots.clear();
266 if (m_ppd) {
267 ppd_option_t *inputSlots = ppdFindOption(m_ppd, "InputSlot");
268 if (inputSlots) {
269 m_inputSlots.reserve(inputSlots->num_choices);
270 for (int i = 0; i < inputSlots->num_choices; ++i)
271 m_inputSlots.append(QPrintUtils::ppdChoiceToInputSlot(inputSlots->choices[i]));
272 }
273 // If no result, try just the default
274 if (m_inputSlots.size() == 0) {
275 inputSlots = ppdFindOption(m_ppd, "DefaultInputSlot");
276 if (inputSlots)
277 m_inputSlots.append(QPrintUtils::ppdChoiceToInputSlot(inputSlots->choices[0]));
278 }
279 }
280 // If still no result, just use Auto
281 if (m_inputSlots.size() == 0)
282 m_inputSlots.append(QPlatformPrintDevice::defaultInputSlot());
283 m_haveInputSlots = true;
284}
285
286QPrint::InputSlot QPpdPrintDevice::defaultInputSlot() const
287{
288 // NOTE: Implemented in both CUPS and Mac plugins, please keep in sync
289 // Try load standard PPD option first
290 if (m_ppd) {
291 ppd_option_t *inputSlot = ppdFindOption(m_ppd, "DefaultInputSlot");
292 if (inputSlot)
293 return QPrintUtils::ppdChoiceToInputSlot(inputSlot->choices[0]);
294 // If no result, then try a marked option
295 ppd_choice_t *defaultChoice = ppdFindMarkedChoice(m_ppd, "InputSlot");
296 if (defaultChoice)
297 return QPrintUtils::ppdChoiceToInputSlot(*defaultChoice);
298 }
299 // Otherwise return Auto
300 return QPlatformPrintDevice::defaultInputSlot();
301}
302
303void QPpdPrintDevice::loadOutputBins() const
304{
305 // NOTE: Implemented in both CUPS and Mac plugins, please keep in sync
306 m_outputBins.clear();
307 if (m_ppd) {
308 ppd_option_t *outputBins = ppdFindOption(m_ppd, "OutputBin");
309 if (outputBins) {
310 m_outputBins.reserve(outputBins->num_choices);
311 for (int i = 0; i < outputBins->num_choices; ++i)
312 m_outputBins.append(QPrintUtils::ppdChoiceToOutputBin(outputBins->choices[i]));
313 }
314 // If no result, try just the default
315 if (m_outputBins.size() == 0) {
316 outputBins = ppdFindOption(m_ppd, "DefaultOutputBin");
317 if (outputBins)
318 m_outputBins.append(QPrintUtils::ppdChoiceToOutputBin(outputBins->choices[0]));
319 }
320 }
321 // If still no result, just use Auto
322 if (m_outputBins.size() == 0)
323 m_outputBins.append(QPlatformPrintDevice::defaultOutputBin());
324 m_haveOutputBins = true;
325}
326
327QPrint::OutputBin QPpdPrintDevice::defaultOutputBin() const
328{
329 // NOTE: Implemented in both CUPS and Mac plugins, please keep in sync
330 // Try load standard PPD option first
331 if (m_ppd) {
332 ppd_option_t *outputBin = ppdFindOption(m_ppd, "DefaultOutputBin");
333 if (outputBin)
334 return QPrintUtils::ppdChoiceToOutputBin(outputBin->choices[0]);
335 // If no result, then try a marked option
336 ppd_choice_t *defaultChoice = ppdFindMarkedChoice(m_ppd, "OutputBin");
337 if (defaultChoice)
338 return QPrintUtils::ppdChoiceToOutputBin(*defaultChoice);
339 }
340 // Otherwise return AutoBin
341 return QPlatformPrintDevice::defaultOutputBin();
342}
343
344void QPpdPrintDevice::loadDuplexModes() const
345{
346 // NOTE: Implemented in both CUPS and Mac plugins, please keep in sync
347 // Try load standard PPD options first
348 m_duplexModes.clear();
349 if (m_ppd) {
350 ppd_option_t *duplexModes = ppdFindOption(m_ppd, "Duplex");
351 if (duplexModes) {
352 m_duplexModes.reserve(duplexModes->num_choices);
353 for (int i = 0; i < duplexModes->num_choices; ++i) {
354 if (ppdInstallableConflict(m_ppd, duplexModes->keyword, duplexModes->choices[i].choice) == 0) {
355 m_duplexModes.append(QPrintUtils::ppdChoiceToDuplexMode(duplexModes->choices[i].choice));
356 }
357 }
358 }
359 // If no result, try just the default
360 if (m_duplexModes.size() == 0) {
361 duplexModes = ppdFindOption(m_ppd, "DefaultDuplex");
362 if (duplexModes && (ppdInstallableConflict(m_ppd, duplexModes->keyword, duplexModes->choices[0].choice) == 0)) {
363 m_duplexModes.append(QPrintUtils::ppdChoiceToDuplexMode(duplexModes->choices[0].choice));
364 }
365 }
366 }
367 // If still no result, or not added in PPD, then add None
368 if (m_duplexModes.size() == 0 || !m_duplexModes.contains(QPrint::DuplexNone))
369 m_duplexModes.append(QPrint::DuplexNone);
370 // If have both modes, then can support DuplexAuto
371 if (m_duplexModes.contains(QPrint::DuplexLongSide) && m_duplexModes.contains(QPrint::DuplexShortSide))
372 m_duplexModes.append(QPrint::DuplexAuto);
373 m_haveDuplexModes = true;
374}
375
376QPrint::DuplexMode QPpdPrintDevice::defaultDuplexMode() const
377{
378 // Try load standard PPD option first
379 if (m_ppd) {
380 ppd_option_t *inputSlot = ppdFindOption(m_ppd, "DefaultDuplex");
381 if (inputSlot)
382 return QPrintUtils::ppdChoiceToDuplexMode(inputSlot->choices[0].choice);
383 // If no result, then try a marked option
384 ppd_choice_t *defaultChoice = ppdFindMarkedChoice(m_ppd, "Duplex");
385 if (defaultChoice)
386 return QPrintUtils::ppdChoiceToDuplexMode(defaultChoice->choice);
387 }
388 // Otherwise return None
389 return QPrint::DuplexNone;
390}
391
392void QPpdPrintDevice::loadColorModes() const
393{
394 // Cups cups_ptype_e CUPS_PRINTER_BW CUPS_PRINTER_COLOR
395 // Cups ppd_file_t color_device
396 // PPD ColorDevice
397 m_colorModes.clear();
398 cups_ptype_e type = printerTypeFlags();
399 if (type & CUPS_PRINTER_BW)
400 m_colorModes.append(QPrint::GrayScale);
401 if (type & CUPS_PRINTER_COLOR)
402 m_colorModes.append(QPrint::Color);
403 m_haveColorModes = true;
404}
405
406QPrint::ColorMode QPpdPrintDevice::defaultColorMode() const
407{
408 // NOTE: Implemented in both CUPS and Mac plugins, please keep in sync
409 // Not a proper option, usually only know if supports color or not, but some
410 // users known to abuse ColorModel to always force GrayScale.
411 if (m_ppd && supportedColorModes().contains(QPrint::Color)) {
412 ppd_option_t *colorModel = ppdFindOption(m_ppd, "DefaultColorModel");
413 if (!colorModel)
414 colorModel = ppdFindOption(m_ppd, "ColorModel");
415 if (!colorModel || qstrcmp(colorModel->defchoice, "Gray") != 0)
416 return QPrint::Color;
417 }
418 return QPrint::GrayScale;
419}
420
421QVariant QPpdPrintDevice::property(QPrintDevice::PrintDevicePropertyKey key,
422 const QVariant &params) const
423{
424 if (key == PDPK_PpdFile)
425 return QVariant::fromValue<ppd_file_t *>(m_ppd);
426 else if (key == PDPK_CupsJobPriority)
427 return printerOption(QStringLiteral("job-priority"));
428 else if (key == PDPK_CupsJobSheets)
429 return printerOption(QStringLiteral("job-sheets"));
430 else if (key == PDPK_CupsJobBilling)
431 return printerOption(QStringLiteral("job-billing"));
432 else if (key == PDPK_CupsJobHoldUntil)
433 return printerOption(QStringLiteral("job-hold-until"));
434 if (key == PDPK_OptionValue) {
435 const auto &values = params.toStringList();
436 if (values.size() == 1) {
437 // try to retrieve value set for the option (e.g. set in ~/.cups/lpoptions) first
438 const QString optionValue = printerOption(values.at(0));
439 if (!optionValue.isEmpty())
440 return optionValue;
441 // fall back to PPD's default value
442 ppd_option_t *option = ppdFindOption(m_ppd, values.at(0).toLatin1());
443 if (option)
444 return QString::fromLatin1(option->defchoice);
445 return QVariant();
446 }
447 }
448
449 return QPlatformPrintDevice::property(key, params);
450}
451
452bool QPpdPrintDevice::setProperty(QPrintDevice::PrintDevicePropertyKey key, const QVariant &value)
453{
454 if (key == PDPK_PpdOption) {
455 const QStringList values = value.toStringList();
456 if (values.size() == 2) {
457 ppdMarkOption(m_ppd, values[0].toLatin1(), values[1].toLatin1());
458 return true;
459 }
460 }
461
462 return QPlatformPrintDevice::setProperty(key, value);
463}
464
465bool QPpdPrintDevice::isFeatureAvailable(QPrintDevice::PrintDevicePropertyKey key, const QVariant &params) const
466{
467 if (key == PDPK_PpdChoiceIsInstallableConflict) {
468 const QStringList values = params.toStringList();
469 if (values.size() == 2)
470 return ppdInstallableConflict(m_ppd, values[0].toLatin1(), values[1].toLatin1());
471 } else if (key == PDPK_PpdCustomOption) {
472 const auto &values = params.toStringList();
473 if (values.size() == 1)
474 return ppdFindCustomOption(m_ppd, values[0].toLatin1()) != nullptr;
475 }
476
477 return QPlatformPrintDevice::isFeatureAvailable(key, params);
478}
479
480#if QT_CONFIG(mimetype)
481void QPpdPrintDevice::loadMimeTypes() const
482{
483 // TODO No CUPS api? Need to manually load CUPS mime.types file?
484 // For now hard-code most common support types
485 QMimeDatabase db;
486 m_mimeTypes.append(db.mimeTypeForName(QStringLiteral("application/pdf")));
487 m_mimeTypes.append(db.mimeTypeForName(QStringLiteral("application/postscript")));
488 m_mimeTypes.append(db.mimeTypeForName(QStringLiteral("image/gif")));
489 m_mimeTypes.append(db.mimeTypeForName(QStringLiteral("image/png")));
490 m_mimeTypes.append(db.mimeTypeForName(QStringLiteral("image/jpeg")));
491 m_mimeTypes.append(db.mimeTypeForName(QStringLiteral("image/tiff")));
492 m_mimeTypes.append(db.mimeTypeForName(QStringLiteral("text/html")));
493 m_mimeTypes.append(db.mimeTypeForName(QStringLiteral("text/plain")));
494 m_haveMimeTypes = true;
495}
496#endif
497
498QString QPpdPrintDevice::printerOption(const QString &key) const
499{
500 return cupsGetOption(key.toUtf8(), m_cupsDest->num_options, m_cupsDest->options);
501}
502
503cups_ptype_e QPpdPrintDevice::printerTypeFlags() const
504{
505 return static_cast<cups_ptype_e>(printerOption("printer-type").toUInt());
506}
507
509
\inmodule QtSql
Combined button and popup list for selecting options.