|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtGui module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "qprinterinfo.h" |
|
43 |
|
44 #include "private/qt_mac_p.h" |
|
45 |
|
46 QT_BEGIN_NAMESPACE |
|
47 |
|
48 #ifndef QT_NO_PRINTER |
|
49 |
|
50 class QPrinterInfoPrivate |
|
51 { |
|
52 Q_DECLARE_PUBLIC(QPrinterInfo) |
|
53 public: |
|
54 ~QPrinterInfoPrivate(); |
|
55 QPrinterInfoPrivate(); |
|
56 QPrinterInfoPrivate(const QString& name); |
|
57 |
|
58 private: |
|
59 QPrinterInfo* q_ptr; |
|
60 |
|
61 QString m_name; |
|
62 bool m_default; |
|
63 bool m_isNull; |
|
64 }; |
|
65 |
|
66 static QPrinterInfoPrivate nullQPrinterInfoPrivate; |
|
67 |
|
68 class QPrinterInfoPrivateDeleter |
|
69 { |
|
70 public: |
|
71 static inline void cleanup(QPrinterInfoPrivate *d) |
|
72 { |
|
73 if (d != &nullQPrinterInfoPrivate) |
|
74 delete d; |
|
75 } |
|
76 }; |
|
77 |
|
78 extern QPrinter::PaperSize qSizeFTopaperSize(const QSizeF& size); |
|
79 |
|
80 ///////////////////////////////////////////////////////////////////////////// |
|
81 ///////////////////////////////////////////////////////////////////////////// |
|
82 |
|
83 QList<QPrinterInfo> QPrinterInfo::availablePrinters() |
|
84 { |
|
85 QList<QPrinterInfo> printers; |
|
86 |
|
87 OSStatus status = noErr; |
|
88 QCFType<CFArrayRef> printerList; |
|
89 status = PMServerCreatePrinterList(kPMServerLocal, &printerList); |
|
90 if (status == noErr) { |
|
91 CFIndex count = CFArrayGetCount(printerList); |
|
92 for (CFIndex i=0; i<count; ++i) { |
|
93 PMPrinter printer = static_cast<PMPrinter>(const_cast<void *>(CFArrayGetValueAtIndex(printerList, i))); |
|
94 QString name = QCFString::toQString(PMPrinterGetName(printer)); |
|
95 printers.append(QPrinterInfo(name)); |
|
96 if (PMPrinterIsDefault(printer)) { |
|
97 printers[i].d_ptr->m_default = true; |
|
98 } |
|
99 } |
|
100 } |
|
101 |
|
102 return printers; |
|
103 } |
|
104 |
|
105 QPrinterInfo QPrinterInfo::defaultPrinter(){ |
|
106 QList<QPrinterInfo> printers = availablePrinters(); |
|
107 for (int c = 0; c < printers.size(); ++c) { |
|
108 if (printers[c].isDefault()) { |
|
109 return printers[c]; |
|
110 } |
|
111 } |
|
112 return QPrinterInfo(); |
|
113 } |
|
114 |
|
115 ///////////////////////////////////////////////////////////////////////////// |
|
116 ///////////////////////////////////////////////////////////////////////////// |
|
117 |
|
118 QPrinterInfo::QPrinterInfo(const QPrinter& prn) |
|
119 : d_ptr(&nullQPrinterInfoPrivate) |
|
120 { |
|
121 QList<QPrinterInfo> list = availablePrinters(); |
|
122 for (int c = 0; c < list.size(); ++c) { |
|
123 if (prn.printerName() == list[c].printerName()) { |
|
124 *this = list[c]; |
|
125 return; |
|
126 } |
|
127 } |
|
128 } |
|
129 |
|
130 QPrinterInfo::~QPrinterInfo() |
|
131 { |
|
132 } |
|
133 |
|
134 QPrinterInfo::QPrinterInfo() |
|
135 : d_ptr(&nullQPrinterInfoPrivate) |
|
136 { |
|
137 } |
|
138 |
|
139 QPrinterInfo::QPrinterInfo(const QString& name) |
|
140 : d_ptr(new QPrinterInfoPrivate(name)) |
|
141 { |
|
142 d_ptr->q_ptr = this; |
|
143 } |
|
144 |
|
145 QPrinterInfo::QPrinterInfo(const QPrinterInfo& src) |
|
146 : d_ptr(&nullQPrinterInfoPrivate) |
|
147 { |
|
148 *this = src; |
|
149 } |
|
150 |
|
151 QPrinterInfo& QPrinterInfo::operator=(const QPrinterInfo& src) |
|
152 { |
|
153 Q_ASSERT(d_ptr); |
|
154 d_ptr.reset(new QPrinterInfoPrivate(*src.d_ptr)); |
|
155 d_ptr->q_ptr = this; |
|
156 return *this; |
|
157 } |
|
158 |
|
159 QString QPrinterInfo::printerName() const |
|
160 { |
|
161 const Q_D(QPrinterInfo); |
|
162 return d->m_name; |
|
163 } |
|
164 |
|
165 bool QPrinterInfo::isNull() const |
|
166 { |
|
167 const Q_D(QPrinterInfo); |
|
168 return d->m_isNull; |
|
169 } |
|
170 |
|
171 bool QPrinterInfo::isDefault() const |
|
172 { |
|
173 const Q_D(QPrinterInfo); |
|
174 return d->m_default; |
|
175 } |
|
176 |
|
177 QList<QPrinter::PaperSize> QPrinterInfo::supportedPaperSizes() const |
|
178 { |
|
179 const Q_D(QPrinterInfo); |
|
180 |
|
181 PMPrinter cfPrn = PMPrinterCreateFromPrinterID(QCFString::toCFStringRef(d->m_name)); |
|
182 |
|
183 if (!cfPrn) return QList<QPrinter::PaperSize>(); |
|
184 |
|
185 CFArrayRef array; |
|
186 OSStatus status = PMPrinterGetPaperList(cfPrn, &array); |
|
187 |
|
188 if (status != 0) { |
|
189 PMRelease(cfPrn); |
|
190 return QList<QPrinter::PaperSize>(); |
|
191 } |
|
192 |
|
193 QList<QPrinter::PaperSize> paperList; |
|
194 int count = CFArrayGetCount(array); |
|
195 for (int c = 0; c < count; c++) { |
|
196 PMPaper paper = static_cast<PMPaper>( |
|
197 const_cast<void*>( |
|
198 CFArrayGetValueAtIndex(array, c))); |
|
199 double width, height; |
|
200 status = PMPaperGetWidth(paper, &width); |
|
201 status |= PMPaperGetHeight(paper, &height); |
|
202 if (status != 0) continue; |
|
203 |
|
204 QSizeF size(width * 0.3527, height * 0.3527); |
|
205 paperList.append(qSizeFTopaperSize(size)); |
|
206 } |
|
207 |
|
208 PMRelease(cfPrn); |
|
209 |
|
210 return paperList; |
|
211 } |
|
212 |
|
213 ///////////////////////////////////////////////////////////////////////////// |
|
214 ///////////////////////////////////////////////////////////////////////////// |
|
215 |
|
216 QPrinterInfoPrivate::QPrinterInfoPrivate() : |
|
217 q_ptr(NULL), |
|
218 m_default(false), |
|
219 m_isNull(true) |
|
220 { |
|
221 } |
|
222 |
|
223 QPrinterInfoPrivate::QPrinterInfoPrivate(const QString& name) : |
|
224 q_ptr(NULL), |
|
225 m_name(name), |
|
226 m_default(false), |
|
227 m_isNull(false) |
|
228 { |
|
229 } |
|
230 |
|
231 QPrinterInfoPrivate::~QPrinterInfoPrivate() |
|
232 { |
|
233 } |
|
234 |
|
235 #endif // QT_NO_PRINTER |
|
236 |
|
237 QT_END_NAMESPACE |