source: trunk/src/gui/painting/qprinterinfo_pm.cpp@ 659

Last change on this file since 659 was 659, checked in by Dmitry A. Kuminov, 15 years ago

global: Updated year to 2010 in OS/2-specific headers.

File size: 11.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** Copyright (C) 2010 netlabs.org. OS/2 parts.
8**
9** This file is part of the QtGui module of the Qt Toolkit.
10**
11** $QT_BEGIN_LICENSE:LGPL$
12** Commercial Usage
13** Licensees holding valid Qt Commercial licenses may use this file in
14** accordance with the Qt Commercial License Agreement provided with the
15** Software or, alternatively, in accordance with the terms contained in
16** a written agreement between you and Nokia.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 2.1 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 2.1 requirements
24** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25**
26** In addition, as a special exception, Nokia gives you certain additional
27** rights. These rights are described in the Nokia Qt LGPL Exception
28** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29**
30** GNU General Public License Usage
31** Alternatively, this file may be used under the terms of the GNU
32** General Public License version 3.0 as published by the Free Software
33** Foundation and appearing in the file LICENSE.GPL included in the
34** packaging of this file. Please review the following information to
35** ensure the GNU General Public License version 3.0 requirements will be
36** met: http://www.gnu.org/copyleft/gpl.html.
37**
38** If you have questions regarding the use of this file, please contact
39** Nokia at [email protected].
40** $QT_END_LICENSE$
41**
42****************************************************************************/
43
44#include "qprinterinfo.h"
45
46#if !defined(QT_NO_CUPS)
47# include <private/qcups_p.h>
48# include <cups/cups.h>
49# include <private/qpdf_p.h>
50#endif
51
52QT_BEGIN_NAMESPACE
53
54#ifndef QT_NO_PRINTER
55
56class QPrinterInfoPrivate
57{
58Q_DECLARE_PUBLIC(QPrinterInfo)
59public:
60 QPrinterInfoPrivate();
61 QPrinterInfoPrivate(const QString& name);
62 ~QPrinterInfoPrivate();
63
64 static QPrinter::PaperSize string2PaperSize(const QString& str);
65 static QString pageSize2String(QPrinter::PaperSize size);
66
67private:
68 QString m_name;
69 bool m_isNull;
70 bool m_default;
71 mutable bool m_mustGetPaperSizes;
72 mutable QList<QPrinter::PaperSize> m_paperSizes;
73 int m_cupsPrinterIndex;
74
75 QPrinterInfo* q_ptr;
76};
77
78static QPrinterInfoPrivate nullQPrinterInfoPrivate;
79
80class QPrinterInfoPrivateDeleter
81{
82public:
83 static inline void cleanup(QPrinterInfoPrivate *d)
84 {
85 if (d != &nullQPrinterInfoPrivate)
86 delete d;
87 }
88};
89
90/////////////////////////////////////////////////////////////////////////////
91/////////////////////////////////////////////////////////////////////////////
92
93QList<QPrinterInfo> QPrinterInfo::availablePrinters()
94{
95 QList<QPrinterInfo> list;
96
97#if !defined(QT_NO_CUPS)
98 QCUPSSupport cups;
99 if (QCUPSSupport::isAvailable()) {
100 //const ppd_file_t* cupsPPD = cups.currentPPD();
101 int cupsPrinterCount = cups.availablePrintersCount();
102 const cups_dest_t* cupsPrinters = cups.availablePrinters();
103
104 for (int i = 0; i < cupsPrinterCount; ++i) {
105 QString printerName(QString::fromLocal8Bit(cupsPrinters[i].name));
106 if (cupsPrinters[i].instance)
107 printerName += QLatin1Char('/') + QString::fromLocal8Bit(cupsPrinters[i].instance);
108 list.append(QPrinterInfo(printerName));
109 if (cupsPrinters[i].is_default)
110 list[i].d_ptr->m_default = true;
111 list[i].d_ptr->m_cupsPrinterIndex = i;
112 }
113 }
114#endif
115
116 return list;
117}
118
119QPrinterInfo QPrinterInfo::defaultPrinter()
120{
121 QList<QPrinterInfo> prnList = availablePrinters();
122 for (int i = 0; i < prnList.size(); ++i) {
123 if (prnList[i].isDefault())
124 return prnList[i];
125 }
126 return (prnList.size() > 0) ? prnList[0] : QPrinterInfo();
127}
128
129QPrinterInfo::QPrinterInfo()
130 : d_ptr(&nullQPrinterInfoPrivate)
131{
132}
133
134QPrinterInfo::QPrinterInfo(const QPrinterInfo& src)
135 : d_ptr(&nullQPrinterInfoPrivate)
136{
137 *this = src;
138}
139
140QPrinterInfo::QPrinterInfo(const QPrinter& printer)
141 : d_ptr(new QPrinterInfoPrivate(printer.printerName()))
142{
143
144 Q_D(QPrinterInfo);
145 d->q_ptr = this;
146
147#if !defined(QT_NO_CUPS)
148 QCUPSSupport cups;
149 if (QCUPSSupport::isAvailable()) {
150 int cupsPrinterCount = cups.availablePrintersCount();
151 const cups_dest_t* cupsPrinters = cups.availablePrinters();
152
153 for (int i = 0; i < cupsPrinterCount; ++i) {
154 QString printerName(QString::fromLocal8Bit(cupsPrinters[i].name));
155 if (cupsPrinters[i].instance)
156 printerName += QLatin1Char('/') + QString::fromLocal8Bit(cupsPrinters[i].instance);
157 if (printerName == printer.printerName()) {
158 if (cupsPrinters[i].is_default)
159 d->m_default = true;
160 d->m_cupsPrinterIndex = i;
161 return;
162 }
163 }
164 }
165#endif
166
167 // Printer not found.
168 d_ptr.reset(&nullQPrinterInfoPrivate);
169}
170
171QPrinterInfo::QPrinterInfo(const QString& name)
172 : d_ptr(new QPrinterInfoPrivate(name))
173{
174 d_ptr->q_ptr = this;
175}
176
177QPrinterInfo::~QPrinterInfo()
178{
179}
180
181QPrinterInfo& QPrinterInfo::operator=(const QPrinterInfo& src)
182{
183 Q_ASSERT(d_ptr);
184 d_ptr.reset(new QPrinterInfoPrivate(*src.d_ptr));
185 d_ptr->q_ptr = this;
186 return *this;
187}
188
189QString QPrinterInfo::printerName() const
190{
191 const Q_D(QPrinterInfo);
192 return d->m_name;
193}
194
195bool QPrinterInfo::isNull() const
196{
197 const Q_D(QPrinterInfo);
198 return d->m_isNull;
199}
200
201bool QPrinterInfo::isDefault() const
202{
203 const Q_D(QPrinterInfo);
204 return d->m_default;
205}
206
207QList< QPrinter::PaperSize> QPrinterInfo::supportedPaperSizes() const
208{
209 const Q_D(QPrinterInfo);
210 if (d->m_mustGetPaperSizes) {
211 d->m_mustGetPaperSizes = false;
212
213#if !defined(QT_NO_CUPS)
214 QCUPSSupport cups;
215 if (QCUPSSupport::isAvailable()) {
216 // Find paper sizes from CUPS.
217 cups.setCurrentPrinter(d->m_cupsPrinterIndex);
218 const ppd_option_t* sizes = cups.pageSizes();
219 if (sizes) {
220 for (int j = 0; j < sizes->num_choices; ++j) {
221 d->m_paperSizes.append(
222 QPrinterInfoPrivate::string2PaperSize(
223 QLatin1String(sizes->choices[j].choice)));
224 }
225 }
226 }
227#endif
228
229 }
230 return d->m_paperSizes;
231}
232
233/////////////////////////////////////////////////////////////////////////////
234/////////////////////////////////////////////////////////////////////////////
235
236QPrinterInfoPrivate::QPrinterInfoPrivate()
237{
238 m_isNull = true;
239 m_default = false;
240 m_mustGetPaperSizes = true;
241 m_cupsPrinterIndex = 0;
242 q_ptr = 0;
243}
244
245QPrinterInfoPrivate::QPrinterInfoPrivate(const QString& name)
246{
247 m_name = name;
248 m_isNull = false;
249 m_default = false;
250 m_mustGetPaperSizes = true;
251 m_cupsPrinterIndex = 0;
252 q_ptr = 0;
253}
254
255QPrinterInfoPrivate::~QPrinterInfoPrivate()
256{
257}
258
259QPrinter::PaperSize QPrinterInfoPrivate::string2PaperSize(const QString& str)
260{
261 if (str == QLatin1String("A4")) {
262 return QPrinter::A4;
263 } else if (str == QLatin1String("B5")) {
264 return QPrinter::B5;
265 } else if (str == QLatin1String("Letter")) {
266 return QPrinter::Letter;
267 } else if (str == QLatin1String("Legal")) {
268 return QPrinter::Legal;
269 } else if (str == QLatin1String("Executive")) {
270 return QPrinter::Executive;