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

Last change on this file was 659, checked in by Dmitry A. Kuminov, 16 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 (qt-info@nokia.com)
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 qt-info@nokia.com.
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;
271 } else if (str == QLatin1String("A0")) {
272 return QPrinter::A0;
273 } else if (str == QLatin1String("A1")) {
274 return QPrinter::A1;
275 } else if (str == QLatin1String("A2")) {
276 return QPrinter::A2;
277 } else if (str == QLatin1String("A3")) {
278 return QPrinter::A3;
279 } else if (str == QLatin1String("A5")) {
280 return QPrinter::A5;
281 } else if (str == QLatin1String("A6")) {
282 return QPrinter::A6;
283 } else if (str == QLatin1String("A7")) {
284 return QPrinter::A7;
285 } else if (str == QLatin1String("A8")) {
286 return QPrinter::A8;
287 } else if (str == QLatin1String("A9")) {
288 return QPrinter::A9;
289 } else if (str == QLatin1String("B0")) {
290 return QPrinter::B0;
291 } else if (str == QLatin1String("B1")) {
292 return QPrinter::B1;
293 } else if (str == QLatin1String("B10")) {
294 return QPrinter::B10;
295 } else if (str == QLatin1String("B2")) {
296 return QPrinter::B2;
297 } else if (str == QLatin1String("B3")) {
298 return QPrinter::B3;
299 } else if (str == QLatin1String("B4")) {
300 return QPrinter::B4;
301 } else if (str == QLatin1String("B6")) {
302 return QPrinter::B6;
303 } else if (str == QLatin1String("B7")) {
304 return QPrinter::B7;
305 } else if (str == QLatin1String("B8")) {
306 return QPrinter::B8;
307 } else if (str == QLatin1String("B9")) {
308 return QPrinter::B9;
309 } else if (str == QLatin1String("C5E")) {
310 return QPrinter::C5E;
311 } else if (str == QLatin1String("Comm10E")) {
312 return QPrinter::Comm10E;
313 } else if (str == QLatin1String("DLE")) {
314 return QPrinter::DLE;
315 } else if (str == QLatin1String("Folio")) {
316 return QPrinter::Folio;
317 } else if (str == QLatin1String("Ledger")) {
318 return QPrinter::Ledger;
319 } else if (str == QLatin1String("Tabloid")) {
320 return QPrinter::Tabloid;
321 } else {
322 return QPrinter::Custom;
323 }
324}
325
326QString QPrinterInfoPrivate::pageSize2String(QPrinter::PaperSize size)
327{
328 switch (size) {
329 case QPrinter::A4:
330 return QLatin1String("A4");
331 case QPrinter::B5:
332 return QLatin1String("B5");
333 case QPrinter::Letter:
334 return QLatin1String("Letter");
335 case QPrinter::Legal:
336 return QLatin1String("Legal");
337 case QPrinter::Executive:
338 return QLatin1String("Executive");
339 case QPrinter::A0:
340 return QLatin1String("A0");
341 case QPrinter::A1:
342 return QLatin1String("A1");
343 case QPrinter::A2:
344 return QLatin1String("A2");
345 case QPrinter::A3:
346 return QLatin1String("A3");
347 case QPrinter::A5:
348 return QLatin1String("A5");
349 case QPrinter::A6:
350 return QLatin1String("A6");
351 case QPrinter::A7:
352 return QLatin1String("A7");
353 case QPrinter::A8:
354 return QLatin1String("A8");
355 case QPrinter::A9:
356 return QLatin1String("A9");
357 case QPrinter::B0:
358 return QLatin1String("B0");
359 case QPrinter::B1:
360 return QLatin1String("B1");
361 case QPrinter::B10:
362 return QLatin1String("B10");
363 case QPrinter::B2:
364 return QLatin1String("B2");
365 case QPrinter::B3:
366 return QLatin1String("B3");
367 case QPrinter::B4:
368 return QLatin1String("B4");
369 case QPrinter::B6:
370 return QLatin1String("B6");
371 case QPrinter::B7:
372 return QLatin1String("B7");
373 case QPrinter::B8:
374 return QLatin1String("B8");
375 case QPrinter::B9:
376 return QLatin1String("B9");
377 case QPrinter::C5E:
378 return QLatin1String("C5E");
379 case QPrinter::Comm10E:
380 return QLatin1String("Comm10E");
381 case QPrinter::DLE:
382 return QLatin1String("DLE");
383 case QPrinter::Folio:
384 return QLatin1String("Folio");
385 case QPrinter::Ledger:
386 return QLatin1String("Ledger");
387 case QPrinter::Tabloid:
388 return QLatin1String("Tabloid");
389 default:
390 return QLatin1String("Custom");
391 }
392}
393
394#endif // QT_NO_PRINTER
395
396QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.