source: trunk/src/gui/painting/qprinterinfo_win.cpp@ 350

Last change on this file since 350 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 8.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qprinterinfo.h"
43
44#include <qstringlist.h>
45
46#include <windows.h>
47
48QT_BEGIN_NAMESPACE
49
50#ifndef QT_NO_PRINTER
51
52extern QPrinter::PaperSize mapDevmodePaperSize(int s);
53
54class QPrinterInfoPrivate
55{
56Q_DECLARE_PUBLIC(QPrinterInfo)
57public:
58 ~QPrinterInfoPrivate();
59 QPrinterInfoPrivate();
60 QPrinterInfoPrivate(const QString& name);
61
62private:
63 QString m_name;
64 bool m_default;
65 bool m_isNull;
66
67 QPrinterInfo* q_ptr;
68};
69
70static QPrinterInfoPrivate nullQPrinterInfoPrivate;
71
72/////////////////////////////////////////////////////////////////////////////
73/////////////////////////////////////////////////////////////////////////////
74
75QList<QPrinterInfo> QPrinterInfo::availablePrinters()
76{
77 QList<QPrinterInfo> printers;
78 LPBYTE buffer;
79 DWORD needed = 0;
80 DWORD returned = 0;
81
82 QT_WA({
83 if (!EnumPrintersW(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, NULL,
84 4, 0, 0, &needed, &returned))
85 {
86 buffer = new BYTE[needed];
87 if (!EnumPrintersW(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS , NULL,
88 4, buffer, needed, &needed, &returned))
89 {
90 delete [] buffer;
91 return printers;
92 }
93 PPRINTER_INFO_4 infoList = reinterpret_cast<PPRINTER_INFO_4>(buffer);
94 QPrinterInfo defPrn = defaultPrinter();
95 for (uint i = 0; i < returned; ++i) {
96 printers.append(QPrinterInfo(QString::fromUtf16(reinterpret_cast<const ushort*>(infoList[i].pPrinterName))));
97 if (printers.at(i).printerName() == defPrn.printerName())
98 printers[i].d_ptr->m_default = true;
99 }
100 delete [] buffer;
101 }
102 }, {
103 // In Windows 98 networked printers are served through the local connection
104 if (!EnumPrintersA(PRINTER_ENUM_LOCAL, NULL, 5, 0, 0, &needed, &returned)) {
105 buffer = new BYTE[needed];
106 if (!EnumPrintersA(PRINTER_ENUM_LOCAL, NULL, 5, buffer, needed, &needed, &returned)) {
107 delete [] buffer;
108 return printers;
109 }
110
111 PPRINTER_INFO_5 infoList = reinterpret_cast<PPRINTER_INFO_5>(buffer);
112 QPrinterInfo defPrn = defaultPrinter();
113 for (uint i = 0; i < returned; ++i) {
114 printers.append(QPrinterInfo(QString::fromLocal8Bit(reinterpret_cast<const char*>(infoList[i].pPrinterName))));
115 if (printers.at(i).printerName() == defPrn.printerName())
116 printers[i].d_ptr->m_default = true;
117 }
118 delete [] buffer;
119 }
120 });
121
122 return printers;
123}
124
125QPrinterInfo QPrinterInfo::defaultPrinter()
126{
127 QString noPrinters(QLatin1String("qt_no_printers"));
128 QString output;
129 QT_WA({
130 ushort buffer[256];
131 GetProfileStringW(L"windows", L"device",
132 reinterpret_cast<const wchar_t *>(noPrinters.utf16()),
133 reinterpret_cast<wchar_t *>(buffer), 256);
134 output = QString::fromUtf16(buffer);
135 }, {
136 char buffer[256];
137 GetProfileStringA("windows", "device", noPrinters.toLatin1(), buffer, 256);
138 output = QString::fromLocal8Bit(buffer);
139 });
140
141 // Filter out the name of the printer, which should be everything
142 // before a comma.
143 bool noConfiguredPrinters = (output == noPrinters);
144 QStringList info = output.split(QLatin1Char(','));
145 QString printerName = noConfiguredPrinters ? QString() : info.at(0);
146
147 QPrinterInfo prn(printerName);
148 prn.d_ptr->m_default = true;
149 if (noConfiguredPrinters)
150 prn.d_ptr->m_isNull = true;
151 return prn;
152}
153
154/////////////////////////////////////////////////////////////////////////////
155/////////////////////////////////////////////////////////////////////////////
156
157QPrinterInfo::QPrinterInfo()
158{
159 d_ptr = &nullQPrinterInfoPrivate;
160}
161
162QPrinterInfo::QPrinterInfo(const QString& name)
163{
164 d_ptr = new QPrinterInfoPrivate(name);
165 d_ptr->q_ptr = this;
166}
167
168QPrinterInfo::QPrinterInfo(const QPrinterInfo& src)
169{
170 d_ptr = &nullQPrinterInfoPrivate;
171 *this = src;
172}
173
174QPrinterInfo::QPrinterInfo(const QPrinter& prn)
175{
176 d_ptr = &nullQPrinterInfoPrivate;
177 QList<QPrinterInfo> list = availablePrinters();
178 for (int c = 0; c < list.size(); ++c) {
179 if (prn.printerName() == list[c].printerName()) {
180 *this = list[c];
181 return;
182 }
183 }
184
185 *this = QPrinterInfo();
186}
187
188QPrinterInfo::~QPrinterInfo()
189{
190 if (d_ptr != &nullQPrinterInfoPrivate)
191 delete d_ptr;
192}
193
194QPrinterInfo& QPrinterInfo::operator=(const QPrinterInfo& src)
195{
196 Q_ASSERT(d_ptr);
197 if (d_ptr != &nullQPrinterInfoPrivate)
198 delete d_ptr;
199 d_ptr = new QPrinterInfoPrivate(*src.d_ptr);
200 d_ptr->q_ptr = this;
201 return *this;
202}
203
204QString QPrinterInfo::printerName() const
205{
206 const Q_D(QPrinterInfo);
207 return d->m_name;
208}
209
210bool QPrinterInfo::isNull() const
211{
212 const Q_D(QPrinterInfo);
213 return d->m_isNull;
214}
215
216bool QPrinterInfo::isDefault() const
217{
218 const Q_D(QPrinterInfo);
219 return d->m_default;
220}
221
222QList<QPrinter::PaperSize> QPrinterInfo::supportedPaperSizes() const
223{
224 const Q_D(QPrinterInfo);
225 DWORD size;
226 WORD* papers;
227 QList<QPrinter::PaperSize> paperList;
228
229 QT_WA({
230 size = DeviceCapabilitiesW(reinterpret_cast<const WCHAR*>(d->m_name.utf16()),
231 NULL, DC_PAPERS, NULL, NULL);
232 if ((int)size == -1)
233 return paperList;
234 papers = new WORD[size];
235 size = DeviceCapabilitiesW(reinterpret_cast<const WCHAR*>(d->m_name.utf16()),
236 NULL, DC_PAPERS, reinterpret_cast<WCHAR*>(papers), NULL);
237 }, {
238 size = DeviceCapabilitiesA(d->m_name.toLatin1().data(), NULL, DC_PAPERS, NULL, NULL);
239 if ((int)size == -1)
240 return paperList;
241 papers = new WORD[size];
242 size = DeviceCapabilitiesA(d->m_name.toLatin1().data(), NULL, DC_PAPERS,
243 reinterpret_cast<char*>(papers), NULL);
244 });
245
246 for (int c = 0; c < (int)size; ++c) {
247 paperList.append(mapDevmodePaperSize(papers[c]));
248 }
249
250 delete [] papers;
251
252 return paperList;
253}
254
255/////////////////////////////////////////////////////////////////////////////
256/////////////////////////////////////////////////////////////////////////////
257
258QPrinterInfoPrivate::QPrinterInfoPrivate() :
259 m_default(false),
260 m_isNull(true),
261 q_ptr(NULL)
262{
263}
264
265QPrinterInfoPrivate::QPrinterInfoPrivate(const QString& name) :
266 m_name(name),
267 m_default(false),
268 m_isNull(false),
269 q_ptr(NULL)
270{
271}
272
273QPrinterInfoPrivate::~QPrinterInfoPrivate()
274{
275}
276
277#endif // QT_NO_PRINTER
278
279QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.