source: trunk/src/gui/painting/qcups.cpp@ 494

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

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

File size: 12.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#include <qdebug.h>
42#include "qcups_p.h"
43
44#ifndef QT_NO_CUPS
45
46#ifndef QT_LINUXBASE // LSB merges everything into cups.h
47# include <cups/language.h>
48#endif
49#include <qtextcodec.h>
50
51QT_BEGIN_NAMESPACE
52
53typedef int (*CupsGetDests)(cups_dest_t **dests);
54typedef void (*CupsFreeDests)(int num_dests, cups_dest_t *dests);
55typedef const char* (*CupsGetPPD)(const char *printer);
56typedef int (*CupsMarkOptions)(ppd_file_t *ppd, int num_options, cups_option_t *options);
57typedef ppd_file_t* (*PPDOpenFile)(const char *filename);
58typedef void (*PPDMarkDefaults)(ppd_file_t *ppd);
59typedef int (*PPDMarkOption)(ppd_file_t *ppd, const char *keyword, const char *option);
60typedef void (*PPDClose)(ppd_file_t *ppd);
61typedef int (*PPDMarkOption)(ppd_file_t *ppd, const char *keyword, const char *option);
62typedef void (*CupsFreeOptions)(int num_options, cups_option_t *options);
63typedef void (*CupsSetDests)(int num_dests, cups_dest_t *dests);
64typedef cups_lang_t* (*CupsLangGet)(const char *language);
65typedef const char* (*CupsLangEncoding)(cups_lang_t *language);
66typedef int (*CupsAddOption)(const char *name, const char *value, int num_options, cups_option_t **options);
67typedef int (*CupsTempFd)(char *name, int len);
68typedef int (*CupsPrintFile)(const char * name, const char * filename, const char * title, int num_options, cups_option_t * options);
69
70static bool cupsLoaded = false;
71static int qt_cups_num_printers = 0;
72static CupsGetDests _cupsGetDests = 0;
73static CupsFreeDests _cupsFreeDests = 0;
74static CupsGetPPD _cupsGetPPD = 0;
75static PPDOpenFile _ppdOpenFile = 0;
76static PPDMarkDefaults _ppdMarkDefaults = 0;
77static PPDClose _ppdClose = 0;
78static CupsMarkOptions _cupsMarkOptions = 0;
79static PPDMarkOption _ppdMarkOption = 0;
80static CupsFreeOptions _cupsFreeOptions = 0;
81static CupsSetDests _cupsSetDests = 0;
82static CupsLangGet _cupsLangGet = 0;
83static CupsLangEncoding _cupsLangEncoding = 0;
84static CupsAddOption _cupsAddOption = 0;
85static CupsTempFd _cupsTempFd = 0;
86static CupsPrintFile _cupsPrintFile = 0;
87
88static void resolveCups()
89{
90 QLibrary cupsLib(QLatin1String("cups"), 2);
91 if(cupsLib.load()) {
92 _cupsGetDests = (CupsGetDests) cupsLib.resolve("cupsGetDests");
93 _cupsFreeDests = (CupsFreeDests) cupsLib.resolve("cupsFreeDests");
94 _cupsGetPPD = (CupsGetPPD) cupsLib.resolve("cupsGetPPD");
95 _cupsLangGet = (CupsLangGet) cupsLib.resolve("cupsLangGet");
96 _cupsLangEncoding = (CupsLangEncoding) cupsLib.resolve("cupsLangEncoding");
97 _ppdOpenFile = (PPDOpenFile) cupsLib.resolve("ppdOpenFile");
98 _ppdMarkDefaults = (PPDMarkDefaults) cupsLib.resolve("ppdMarkDefaults");
99 _ppdClose = (PPDClose) cupsLib.resolve("ppdClose");
100 _cupsMarkOptions = (CupsMarkOptions) cupsLib.resolve("cupsMarkOptions");
101 _ppdMarkOption = (PPDMarkOption) cupsLib.resolve("ppdMarkOption");
102 _cupsFreeOptions = (CupsFreeOptions) cupsLib.resolve("cupsFreeOptions");
103 _cupsSetDests = (CupsSetDests) cupsLib.resolve("cupsSetDests");
104 _cupsAddOption = (CupsAddOption) cupsLib.resolve("cupsAddOption");
105 _cupsTempFd = (CupsTempFd) cupsLib.resolve("cupsTempFd");
106 _cupsPrintFile = (CupsPrintFile) cupsLib.resolve("cupsPrintFile");
107
108 if (_cupsGetDests && _cupsFreeDests) {
109 cups_dest_t *printers;
110 int num_printers = _cupsGetDests(&printers);
111 if (num_printers)
112 _cupsFreeDests(num_printers, printers);
113 qt_cups_num_printers = num_printers;
114 }
115 }
116 cupsLoaded = true;
117}
118
119// ================ CUPS Support class ========================
120
121QCUPSSupport::QCUPSSupport()
122 :
123 prnCount(0),
124 printers(0),
125 page_sizes(0),
126 currPrinterIndex(0),
127 currPPD(0)
128{
129 if (!cupsLoaded)
130 resolveCups();
131
132 // getting all available printers
133 if (!isAvailable())
134 return;
135
136 prnCount = _cupsGetDests(&printers);
137
138 for (int i = 0; i < prnCount; ++i) {
139 if (printers[i].is_default) {
140 currPrinterIndex = i;
141 setCurrentPrinter(i);
142 break;
143 }
144 }
145
146#ifndef QT_NO_TEXTCODEC
147 cups_lang_t *cupsLang = _cupsLangGet(0);
148 codec = QTextCodec::codecForName(_cupsLangEncoding(cupsLang));
149 if (!codec)
150 codec = QTextCodec::codecForLocale();
151#endif
152}
153
154QCUPSSupport::~QCUPSSupport()
155{
156 if (currPPD)
157 _ppdClose(currPPD);
158 if (prnCount)
159 _cupsFreeDests(prnCount, printers);
160}
161
162int QCUPSSupport::availablePrintersCount() const
163{
164 return prnCount;
165}
166
167const cups_dest_t* QCUPSSupport::availablePrinters() const
168{
169 return printers;
170}
171
172const ppd_file_t* QCUPSSupport::currentPPD() const
173{
174 return currPPD;
175}
176
177const ppd_file_t* QCUPSSupport::setCurrentPrinter(int index)
178{
179 Q_ASSERT(index >= 0 && index <= prnCount);
180 if (index == prnCount)
181 return 0;
182
183 currPrinterIndex = index;
184
185 if (currPPD)
186 _ppdClose(currPPD);
187 currPPD = 0;
188 page_sizes = 0;
189
190 const char *ppdFile = _cupsGetPPD(printers[index].name);
191
192 if (!ppdFile)
193 return 0;
194
195 currPPD = _ppdOpenFile(ppdFile);
196 unlink(ppdFile);
197
198 // marking default options
199 _ppdMarkDefaults(currPPD);
200
201 // marking options explicitly set
202 _cupsMarkOptions(currPPD, printers[currPrinterIndex].num_options, printers[currPrinterIndex].options);
203
204 // getting pointer to page sizes
205 page_sizes = ppdOption("PageSize");
206
207 return currPPD;
208}
209
210int QCUPSSupport::currentPrinterIndex() const
211{
212 return currPrinterIndex;
213}
214
215bool QCUPSSupport::isAvailable()
216{
217 if(!cupsLoaded)
218 resolveCups();
219
220 return _cupsGetDests &&
221 _cupsFreeDests &&
222 _cupsGetPPD &&
223 _ppdOpenFile &&
224 _ppdMarkDefaults &&
225 _ppdClose &&
226 _cupsMarkOptions &&
227 _ppdMarkOption &&
228 _cupsFreeOptions &&
229 _cupsSetDests &&
230 _cupsLangGet &&
231 _cupsLangEncoding &&
232 _cupsAddOption &&
233 (qt_cups_num_printers > 0);
234}
235
236const ppd_option_t* QCUPSSupport::ppdOption(const char *key) const
237{
238 if (currPPD) {
239 for (int gr = 0; gr < currPPD->num_groups; ++gr) {
240 for (int opt = 0; opt < currPPD->groups[gr].num_options; ++opt) {
241 if (qstrcmp(currPPD->groups[gr].options[opt].keyword, key) == 0)
242 return &currPPD->groups[gr].options[opt];
243 }
244 }
245 }
246 return 0;
247}
248
249const cups_option_t* QCUPSSupport::printerOption(const QString &key) const
250{
251 for (int i = 0; i < printers[currPrinterIndex].num_options; ++i) {
252 if (QLatin1String(printers[currPrinterIndex].options[i].name) == key)
253 return &printers[currPrinterIndex].options[i];
254 }
255 return 0;
256}
257
258const ppd_option_t* QCUPSSupport::pageSizes() const
259{
260 return page_sizes;
261}
262
263int QCUPSSupport::markOption(const char* name, const char* value)
264{
265 return _ppdMarkOption(currPPD, name, value);
266}
267
268void QCUPSSupport::saveOptions(QList<const ppd_option_t*> options, QList<const char*> markedOptions)
269{
270 int oldOptionCount = printers[currPrinterIndex].num_options;
271 cups_option_t* oldOptions = printers[currPrinterIndex].options;
272
273 int newOptionCount = 0;
274 cups_option_t* newOptions = 0;
275
276 // copying old options that are not on the new list
277 for (int i = 0; i < oldOptionCount; ++i) {
278 bool contains = false;
279 for (int j = 0; j < options.count(); ++j) {
280 if (qstrcmp(options.at(j)->keyword, oldOptions[i].name) == 0) {
281 contains = true;
282 break;
283 }
284 }
285
286 if (!contains) {
287 newOptionCount = _cupsAddOption(oldOptions[i].name, oldOptions[i].value, newOptionCount, &newOptions);
288 }
289 }
290
291 // we can release old option list
292 _cupsFreeOptions(oldOptionCount, oldOptions);
293
294 // adding marked options
295 for (int i = 0; i < markedOptions.count(); ++i) {
296 const char* name = markedOptions.at(i);
297 ++i;
298 newOptionCount = _cupsAddOption(name, markedOptions.at(i), newOptionCount, &newOptions);
299 }
300
301 // placing the new option list
302 printers[currPrinterIndex].num_options = newOptionCount;
303 printers[currPrinterIndex].options = newOptions;
304
305 // saving new default values
306 _cupsSetDests(prnCount, printers);
307}
308
309QRect QCUPSSupport::paperRect(const char *choice) const
310{
311 if (!currPPD)
312 return QRect();
313 for (int i = 0; i < currPPD->num_sizes; ++i) {
314 if (qstrcmp(currPPD->sizes[i].name, choice) == 0)
315 return QRect(0, 0, qRound(currPPD->sizes[i].width), qRound(currPPD->sizes[i].length));
316 }
317 return QRect();
318}
319
320QRect QCUPSSupport::pageRect(const char *choice) const
321{
322 if (!currPPD)
323 return QRect();
324 for (int i = 0; i < currPPD->num_sizes; ++i) {
325 if (qstrcmp(currPPD->sizes[i].name, choice) == 0)
326 return QRect(qRound(currPPD->sizes[i].left),
327 qRound(currPPD->sizes[i].length - currPPD->sizes[i].top),
328 qRound(currPPD->sizes[i].right - currPPD->sizes[i].left),
329 qRound(currPPD->sizes[i].top - currPPD->sizes[i].bottom));
330 }
331 return QRect();
332}
333
334QStringList QCUPSSupport::options() const
335{
336 QStringList list;
337 collectMarkedOptions(list);
338 return list;
339}
340
341bool QCUPSSupport::printerHasPPD(const char *printerName)
342{
343 if (!isAvailable())
344 return false;
345 return _cupsGetPPD(printerName) != 0;
346}
347
348QString QCUPSSupport::unicodeString(const char *s)
349{
350#ifndef QT_NO_TEXTCODEC
351 return codec->toUnicode(s);
352#else
353 return QLatin1String(s);
354#endif
355}
356
357void QCUPSSupport::collectMarkedOptions(QStringList& list, const ppd_group_t* group) const
358{
359 if (group == 0) {
360 if (!currPPD)
361 return;
362 for (int i = 0; i < currPPD->num_groups; ++i) {
363 collectMarkedOptions(list, &currPPD->groups[i]);
364 collectMarkedOptionsHelper(list, &currPPD->groups[i]);
365 }
366 } else {
367 for (int i = 0; i < group->num_subgroups; ++i)
368 collectMarkedOptionsHelper(list, &group->subgroups[i]);
369 }
370}
371
372void QCUPSSupport::collectMarkedOptionsHelper(QStringList& list, const ppd_group_t* group) const
373{
374 for (int i = 0; i < group->num_options; ++i) {
375 for (int j = 0; j < group->options[i].num_choices; ++j) {
376 if (group->options[i].choices[j].marked == 1 && qstrcmp(group->options[i].choices[j].choice, group->options[i].defchoice) != 0)
377 list << QString::fromLocal8Bit(group->options[i].keyword) << QString::fromLocal8Bit(group->options[i].choices[j].choice);
378 }
379 }
380}
381
382QPair<int, QString> QCUPSSupport::tempFd()
383{
384 char filename[512];
385 int fd = _cupsTempFd(filename, 512);
386 return QPair<int, QString>(fd, QString::fromLocal8Bit(filename));
387}
388
389// Prints the given file and returns a job id.
390int QCUPSSupport::printFile(const char * printerName, const char * filename, const char * title,
391 int num_options, cups_option_t * options)
392{
393 return _cupsPrintFile(printerName, filename, title, num_options, options);
394}
395
396QT_END_NAMESPACE
397
398#endif // QT_NO_CUPS
Note: See TracBrowser for help on using the repository browser.