1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qprinterinfo.h"
41#include "qprinterinfo_p.h"
42#include "qprintdevice_p.h"
43
44#ifndef QT_NO_PRINTER
45
46#include <QtCore/qdebug.h>
47
48#include <qpa/qplatformprintplugin.h>
49#include <qpa/qplatformprintersupport.h>
50
51QT_BEGIN_NAMESPACE
52
53Q_GLOBAL_STATIC(QPrinterInfoPrivate, shared_null);
54
55class QPrinterInfoPrivateDeleter
56{
57public:
58 static inline void cleanup(QPrinterInfoPrivate *d)
59 {
60 if (d != shared_null)
61 delete d;
62 }
63};
64
65QPrinterInfoPrivate::QPrinterInfoPrivate(const QString &id)
66{
67 if (!id.isEmpty()) {
68 QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
69 if (ps)
70 m_printDevice = ps->createPrintDevice(id);
71 }
72}
73
74QPrinterInfoPrivate::~QPrinterInfoPrivate()
75{
76}
77
78/*!
79 \class QPrinterInfo
80
81 \brief The QPrinterInfo class gives access to information about
82 existing printers.
83
84 \ingroup printing
85 \inmodule QtPrintSupport
86
87 Use the static functions to generate a list of QPrinterInfo
88 objects. Each QPrinterInfo object in the list represents a single
89 printer and can be queried for name, supported paper sizes, and
90 whether or not it is the default printer.
91
92 \since 4.4
93*/
94
95/*!
96 Constructs an empty QPrinterInfo object.
97
98 \sa isNull()
99*/
100QPrinterInfo::QPrinterInfo()
101 : d_ptr(shared_null)
102{
103}
104
105/*!
106 Constructs a copy of \a other.
107*/
108QPrinterInfo::QPrinterInfo(const QPrinterInfo &other)
109 : d_ptr((other.d_ptr.data() == shared_null) ? shared_null : new QPrinterInfoPrivate(*other.d_ptr))
110{
111}
112
113/*!
114 Constructs a QPrinterInfo object from \a printer.
115*/
116QPrinterInfo::QPrinterInfo(const QPrinter &printer)
117 : d_ptr(shared_null)
118{
119 QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
120 if (ps) {
121 QPrinterInfo pi(printer.printerName());
122 if (pi.d_ptr.data() == shared_null)
123 d_ptr.reset(shared_null);
124 else
125 d_ptr.reset(new QPrinterInfoPrivate(*pi.d_ptr));
126 }
127}
128
129/*!
130 \internal
131*/
132QPrinterInfo::QPrinterInfo(const QString &name)
133 : d_ptr(new QPrinterInfoPrivate(name))
134{
135}
136
137/*!
138 Destroys the QPrinterInfo object. References to the values in the
139 object become invalid.
140*/
141QPrinterInfo::~QPrinterInfo()
142{
143}
144
145/*!
146 Sets the QPrinterInfo object to be equal to \a other.
147*/
148QPrinterInfo &QPrinterInfo::operator=(const QPrinterInfo &other)
149{
150 Q_ASSERT(d_ptr);
151 if (other.d_ptr.data() == shared_null)
152 d_ptr.reset(shared_null);
153 else
154 d_ptr.reset(new QPrinterInfoPrivate(*other.d_ptr));
155 return *this;
156}
157
158/*!
159 Returns the name of the printer.
160
161 This is a unique id to identify the printer and may not be human-readable.
162
163 \sa QPrinterInfo::description()
164 \sa QPrinter::setPrinterName()
165*/
166QString QPrinterInfo::printerName() const
167{
168 const Q_D(QPrinterInfo);
169 return d->m_printDevice.id();
170}
171
172/*!
173 Returns the human-readable description of the printer.
174
175 \since 5.0
176 \sa QPrinterInfo::printerName()
177*/
178QString QPrinterInfo::description() const
179{
180 const Q_D(QPrinterInfo);
181 return d->m_printDevice.name();
182}
183
184/*!
185 Returns the human-readable location of the printer.
186
187 \since 5.0
188*/
189QString QPrinterInfo::location() const
190{
191 const Q_D(QPrinterInfo);
192 return d->m_printDevice.location();
193}
194
195/*!
196 Returns the human-readable make and model of the printer.
197
198 \since 5.0
199*/
200QString QPrinterInfo::makeAndModel() const
201{
202 const Q_D(QPrinterInfo);
203 return d->m_printDevice.makeAndModel();
204}
205
206/*!
207 Returns whether this QPrinterInfo object holds a printer definition.
208
209 An empty QPrinterInfo object could result for example from calling
210 defaultPrinter() when there are no printers on the system.
211*/
212bool QPrinterInfo::isNull() const
213{
214 Q_D(const QPrinterInfo);
215 return d == shared_null || !d->m_printDevice.isValid();
216}
217
218/*!
219 Returns whether this printer is currently the default printer.
220*/
221bool QPrinterInfo::isDefault() const
222{
223 Q_D(const QPrinterInfo);
224 return d->m_printDevice.isDefault();
225}
226
227/*!
228 Returns whether this printer is a remote network printer.
229
230 \since 5.3
231*/
232bool QPrinterInfo::isRemote() const
233{
234 Q_D(const QPrinterInfo);
235 return d->m_printDevice.isRemote();
236}
237
238/*!
239 Returns the current state of this printer.
240
241 This state may not always be accurate, depending on the platform, printer
242 driver, or printer itself.
243
244 \since 5.3
245*/
246QPrinter::PrinterState QPrinterInfo::state() const
247{
248 Q_D(const QPrinterInfo);
249 return QPrinter::PrinterState(d->m_printDevice.state());
250}
251
252/*!
253 Returns a list of Page Sizes supported by this printer.
254
255 \since 5.3
256*/
257
258QList<QPageSize> QPrinterInfo::supportedPageSizes() const
259{
260 Q_D(const QPrinterInfo);
261 return d->m_printDevice.supportedPageSizes();
262}
263
264/*!
265 Returns the current default Page Size for this printer.
266
267 \since 5.3
268*/
269
270QPageSize QPrinterInfo::defaultPageSize() const
271{
272 Q_D(const QPrinterInfo);
273 return d->m_printDevice.defaultPageSize();
274}
275
276/*!
277 Returns whether this printer supports custom page sizes.
278
279 \since 5.3
280*/
281
282bool QPrinterInfo::supportsCustomPageSizes() const
283{
284 Q_D(const QPrinterInfo);
285 return d->m_printDevice.supportsCustomPageSizes();
286}
287
288/*!
289 Returns the minimum physical page size supported by this printer.
290
291 \sa maximumPhysicalPageSize()
292
293 \since 5.3
294*/
295
296QPageSize QPrinterInfo::minimumPhysicalPageSize() const
297{
298 Q_D(const QPrinterInfo);
299 return QPageSize(d->m_printDevice.minimumPhysicalPageSize(), QString(), QPageSize::ExactMatch);
300}
301
302/*!
303 Returns the maximum physical page size supported by this printer.
304
305 \sa minimumPhysicalPageSize()
306
307 \since 5.3
308*/
309
310QPageSize QPrinterInfo::maximumPhysicalPageSize() const
311{
312 Q_D(const QPrinterInfo);
313 return QPageSize(d->m_printDevice.maximumPhysicalPageSize(), QString(), QPageSize::ExactMatch);
314}
315
316/*!
317 Returns a list of resolutions supported by this printer.
318
319 \since 5.3
320*/
321
322QList<int> QPrinterInfo::supportedResolutions() const
323{
324 Q_D(const QPrinterInfo);
325 return d->m_printDevice.supportedResolutions();
326}
327
328/*!
329 Returns the default duplex mode of this printer.
330
331 \since 5.4
332*/
333
334QPrinter::DuplexMode QPrinterInfo::defaultDuplexMode() const
335{
336 Q_D(const QPrinterInfo);
337 return QPrinter::DuplexMode(d->m_printDevice.defaultDuplexMode());
338}
339
340/*!
341 Returns a list of duplex modes supported by this printer.
342
343 \since 5.4
344*/
345
346QList<QPrinter::DuplexMode> QPrinterInfo::supportedDuplexModes() const
347{
348 Q_D(const QPrinterInfo);
349 QList<QPrinter::DuplexMode> list;
350 const auto supportedDuplexModes = d->m_printDevice.supportedDuplexModes();
351 list.reserve(supportedDuplexModes.size());
352 for (QPrint::DuplexMode mode : supportedDuplexModes)
353 list << QPrinter::DuplexMode(mode);
354 return list;
355}
356
357/*!
358 Returns the default color mode of this printer.
359
360 \since 5.13
361*/
362
363QPrinter::ColorMode QPrinterInfo::defaultColorMode() const
364{
365 Q_D(const QPrinterInfo);
366 return QPrinter::ColorMode(d->m_printDevice.defaultColorMode());
367}
368
369/*!
370 Returns the supported color modes of this printer.
371
372 \since 5.13
373*/
374
375QList<QPrinter::ColorMode> QPrinterInfo::supportedColorModes() const
376{
377 Q_D(const QPrinterInfo);
378 QList<QPrinter::ColorMode> list;
379 const auto supportedColorModes = d->m_printDevice.supportedColorModes();
380 list.reserve(supportedColorModes.size());
381 for (QPrint::ColorMode mode : supportedColorModes)
382 list << QPrinter::ColorMode(mode);
383 return list;
384}
385
386/*!
387 Returns a list of all the available Printer Names on this system.
388
389 It is recommended to use this instead of availablePrinters() as
390 it will be faster on most systems.
391
392 Note that the list may become outdated if changes are made on the local
393 system or remote print server. Only instantiate required QPrinterInfo
394 instances when needed, and always check for validity before calling.
395
396 \since 5.3
397*/
398QStringList QPrinterInfo::availablePrinterNames()
399{
400 QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
401 if (ps)
402 return ps->availablePrintDeviceIds();
403 return QStringList();
404}
405
406/*!
407 Returns a list of QPrinterInfo objects for all the available printers
408 on this system.
409
410 It is NOT recommended to use this as creating each printer instance may
411 take a long time, especially if there are remote networked printers, and
412 retained instances may become outdated if changes are made on the local
413 system or remote print server. Use availablePrinterNames() instead and
414 only instantiate printer instances as you need them.
415*/
416QList<QPrinterInfo> QPrinterInfo::availablePrinters()
417{
418 QList<QPrinterInfo> list;
419 QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
420 if (ps) {
421 const QStringList availablePrintDeviceIds = ps->availablePrintDeviceIds();
422 list.reserve(availablePrintDeviceIds.size());
423 for (const QString &id : availablePrintDeviceIds)
424 list.append(QPrinterInfo(id));
425 }
426 return list;
427}
428
429/*!
430 Returns the current default printer name.
431
432 \since 5.3
433*/
434QString QPrinterInfo::defaultPrinterName()
435{
436 QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
437 if (ps)
438 return ps->defaultPrintDeviceId();
439 return QString();
440}
441
442/*!
443 Returns the default printer on the system.
444
445 The return value should be checked using isNull() before being
446 used, in case there is no default printer.
447
448 On some systems it is possible for there to be available printers
449 but none of them set to be the default printer.
450
451 \sa isNull()
452 \sa isDefault()
453 \sa availablePrinters()
454*/
455
456QPrinterInfo QPrinterInfo::defaultPrinter()
457{
458 QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
459 if (ps)
460 return QPrinterInfo(ps->defaultPrintDeviceId());
461 return QPrinterInfo();
462}
463
464/*!
465 Returns the printer \a printerName.
466
467 The return value should be checked using isNull() before being
468 used, in case the named printer does not exist.
469
470 \since 5.0
471 \sa isNull()
472*/
473QPrinterInfo QPrinterInfo::printerInfo(const QString &printerName)
474{
475 return QPrinterInfo(printerName);
476}
477
478# ifndef QT_NO_DEBUG_STREAM
479QDebug operator<<(QDebug debug, const QPrinterInfo &p)
480{
481 QDebugStateSaver saver(debug);
482 debug.nospace();
483 debug << "QPrinterInfo(";
484 if (p.isNull())
485 debug << "null";
486 else
487 p.d_ptr->m_printDevice.format(debug);
488 debug << ')';
489 return debug;
490}
491# endif // !QT_NO_DEBUG_STREAM
492
493QT_END_NAMESPACE
494
495#endif // QT_NO_PRINTER
496