1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
46 #ifndef QT_LINUXBASE // LSB merges everything into cups.h
47 # include <cups/language.h>
49 #include <qtextcodec.h>
53 typedef int (*CupsGetDests)(cups_dest_t **dests);
54 typedef void (*CupsFreeDests)(int num_dests, cups_dest_t *dests);
55 typedef const char* (*CupsGetPPD)(const char *printer);
56 typedef int (*CupsMarkOptions)(ppd_file_t *ppd, int num_options, cups_option_t *options);
57 typedef ppd_file_t* (*PPDOpenFile)(const char *filename);
58 typedef void (*PPDMarkDefaults)(ppd_file_t *ppd);
59 typedef int (*PPDMarkOption)(ppd_file_t *ppd, const char *keyword, const char *option);
60 typedef void (*PPDClose)(ppd_file_t *ppd);
61 typedef int (*PPDMarkOption)(ppd_file_t *ppd, const char *keyword, const char *option);
62 typedef void (*CupsFreeOptions)(int num_options, cups_option_t *options);
63 typedef void (*CupsSetDests)(int num_dests, cups_dest_t *dests);
64 typedef cups_lang_t* (*CupsLangGet)(const char *language);
65 typedef const char* (*CupsLangEncoding)(cups_lang_t *language);
66 typedef int (*CupsAddOption)(const char *name, const char *value, int num_options, cups_option_t **options);
67 typedef int (*CupsTempFd)(char *name, int len);
68 typedef int (*CupsPrintFile)(const char * name, const char * filename, const char * title, int num_options, cups_option_t * options);
70 static bool cupsLoaded = false;
71 static int qt_cups_num_printers = 0;
72 static CupsGetDests _cupsGetDests = 0;
73 static CupsFreeDests _cupsFreeDests = 0;
74 static CupsGetPPD _cupsGetPPD = 0;
75 static PPDOpenFile _ppdOpenFile = 0;
76 static PPDMarkDefaults _ppdMarkDefaults = 0;
77 static PPDClose _ppdClose = 0;
78 static CupsMarkOptions _cupsMarkOptions = 0;
79 static PPDMarkOption _ppdMarkOption = 0;
80 static CupsFreeOptions _cupsFreeOptions = 0;
81 static CupsSetDests _cupsSetDests = 0;
82 static CupsLangGet _cupsLangGet = 0;
83 static CupsLangEncoding _cupsLangEncoding = 0;
84 static CupsAddOption _cupsAddOption = 0;
85 static CupsTempFd _cupsTempFd = 0;
86 static CupsPrintFile _cupsPrintFile = 0;
88 static void resolveCups()
90 QLibrary cupsLib(QLatin1String("cups"), 2);
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");
108 if (_cupsGetDests && _cupsFreeDests) {
109 cups_dest_t *printers;
110 int num_printers = _cupsGetDests(&printers);
112 _cupsFreeDests(num_printers, printers);
113 qt_cups_num_printers = num_printers;
119 // ================ CUPS Support class ========================
121 QCUPSSupport::QCUPSSupport()
132 // getting all available printers
136 prnCount = _cupsGetDests(&printers);
138 for (int i = 0; i < prnCount; ++i) {
139 if (printers[i].is_default) {
140 currPrinterIndex = i;
141 setCurrentPrinter(i);
146 #ifndef QT_NO_TEXTCODEC
147 cups_lang_t *cupsLang = _cupsLangGet(0);
148 codec = QTextCodec::codecForName(_cupsLangEncoding(cupsLang));
150 codec = QTextCodec::codecForLocale();
154 QCUPSSupport::~QCUPSSupport()
159 _cupsFreeDests(prnCount, printers);
162 int QCUPSSupport::availablePrintersCount() const
167 const cups_dest_t* QCUPSSupport::availablePrinters() const
172 const ppd_file_t* QCUPSSupport::currentPPD() const
177 const ppd_file_t* QCUPSSupport::setCurrentPrinter(int index)
179 Q_ASSERT(index >= 0 && index <= prnCount);
180 if (index == prnCount)
183 currPrinterIndex = index;
190 const char *ppdFile = _cupsGetPPD(printers[index].name);
195 currPPD = _ppdOpenFile(ppdFile);
198 // marking default options
199 _ppdMarkDefaults(currPPD);
201 // marking options explicitly set
202 _cupsMarkOptions(currPPD, printers[currPrinterIndex].num_options, printers[currPrinterIndex].options);
204 // getting pointer to page sizes
205 page_sizes = ppdOption("PageSize");
210 int QCUPSSupport::currentPrinterIndex() const
212 return currPrinterIndex;
215 bool QCUPSSupport::isAvailable()
220 return _cupsGetDests &&
233 (qt_cups_num_printers > 0);
236 const ppd_option_t* QCUPSSupport::ppdOption(const char *key) const
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];
249 const cups_option_t* QCUPSSupport::printerOption(const QString &key) const
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];
258 const ppd_option_t* QCUPSSupport::pageSizes() const
263 int QCUPSSupport::markOption(const char* name, const char* value)
265 return _ppdMarkOption(currPPD, name, value);
268 void QCUPSSupport::saveOptions(QList<const ppd_option_t*> options, QList<const char*> markedOptions)
270 int oldOptionCount = printers[currPrinterIndex].num_options;
271 cups_option_t* oldOptions = printers[currPrinterIndex].options;
273 int newOptionCount = 0;
274 cups_option_t* newOptions = 0;
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) {
287 newOptionCount = _cupsAddOption(oldOptions[i].name, oldOptions[i].value, newOptionCount, &newOptions);
291 // we can release old option list
292 _cupsFreeOptions(oldOptionCount, oldOptions);
294 // adding marked options
295 for (int i = 0; i < markedOptions.count(); ++i) {
296 const char* name = markedOptions.at(i);
298 newOptionCount = _cupsAddOption(name, markedOptions.at(i), newOptionCount, &newOptions);
301 // placing the new option list
302 printers[currPrinterIndex].num_options = newOptionCount;
303 printers[currPrinterIndex].options = newOptions;
305 // saving new default values
306 _cupsSetDests(prnCount, printers);
309 QRect QCUPSSupport::paperRect(const char *choice) const
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));
320 QRect QCUPSSupport::pageRect(const char *choice) const
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));
334 QStringList QCUPSSupport::options() const
337 collectMarkedOptions(list);
341 bool QCUPSSupport::printerHasPPD(const char *printerName)
345 const char *ppdFile = _cupsGetPPD(printerName);
348 return (ppdFile != 0);
351 QString QCUPSSupport::unicodeString(const char *s)
353 #ifndef QT_NO_TEXTCODEC
354 return codec->toUnicode(s);
356 return QLatin1String(s);
360 void QCUPSSupport::collectMarkedOptions(QStringList& list, const ppd_group_t* group) const
365 for (int i = 0; i < currPPD->num_groups; ++i) {
366 collectMarkedOptions(list, &currPPD->groups[i]);
367 collectMarkedOptionsHelper(list, &currPPD->groups[i]);
370 for (int i = 0; i < group->num_subgroups; ++i)
371 collectMarkedOptionsHelper(list, &group->subgroups[i]);
375 void QCUPSSupport::collectMarkedOptionsHelper(QStringList& list, const ppd_group_t* group) const
377 for (int i = 0; i < group->num_options; ++i) {
378 for (int j = 0; j < group->options[i].num_choices; ++j) {
379 if (group->options[i].choices[j].marked == 1 && qstrcmp(group->options[i].choices[j].choice, group->options[i].defchoice) != 0)
380 list << QString::fromLocal8Bit(group->options[i].keyword) << QString::fromLocal8Bit(group->options[i].choices[j].choice);
385 QPair<int, QString> QCUPSSupport::tempFd()
388 int fd = _cupsTempFd(filename, 512);
389 return QPair<int, QString>(fd, QString::fromLocal8Bit(filename));
392 // Prints the given file and returns a job id.
393 int QCUPSSupport::printFile(const char * printerName, const char * filename, const char * title,
394 int num_options, cups_option_t * options)
396 return _cupsPrintFile(printerName, filename, title, num_options, options);