Qt 4.8
qcups.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
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 
52 
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);
69 
70 static bool cupsLoaded = false;
71 static int qt_cups_num_printers = 0;
77 static PPDClose _ppdClose = 0;
87 
88 static 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 
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  // Update the available printer count
138 
139  for (int i = 0; i < prnCount; ++i) {
140  if (printers[i].is_default) {
141  currPrinterIndex = i;
143  break;
144  }
145  }
146 
147 #ifndef QT_NO_TEXTCODEC
148  cups_lang_t *cupsLang = _cupsLangGet(0);
150  if (!codec)
152 #endif
153 }
154 
156 {
157  if (currPPD)
159  if (prnCount)
161 }
162 
164 {
165  return prnCount;
166 }
167 
168 const cups_dest_t* QCUPSSupport::availablePrinters() const
169 {
170  return printers;
171 }
172 
173 const ppd_file_t* QCUPSSupport::currentPPD() const
174 {
175  return currPPD;
176 }
177 
179 {
180  Q_ASSERT(index >= 0 && index <= prnCount);
181  if (index == prnCount)
182  return 0;
183 
185 
186  if (currPPD)
188  currPPD = 0;
189  page_sizes = 0;
190 
191  const char *ppdFile = _cupsGetPPD(printers[index].name);
192 
193  if (!ppdFile)
194  return 0;
195 
196  currPPD = _ppdOpenFile(ppdFile);
197  unlink(ppdFile);
198 
199  // marking default options
201 
202  // marking options explicitly set
204 
205  // getting pointer to page sizes
206  page_sizes = ppdOption("PageSize");
207 
208  return currPPD;
209 }
210 
212 {
213  return currPrinterIndex;
214 }
215 
217 {
218  if(!cupsLoaded)
219  resolveCups();
220 
221  return _cupsGetDests &&
222  _cupsFreeDests &&
223  _cupsGetPPD &&
224  _ppdOpenFile &&
226  _ppdClose &&
228  _ppdMarkOption &&
230  _cupsSetDests &&
231  _cupsLangGet &&
233  _cupsAddOption &&
234  (qt_cups_num_printers > 0);
235 }
236 
237 const ppd_option_t* QCUPSSupport::ppdOption(const char *key) const
238 {
239  if (currPPD) {
240  for (int gr = 0; gr < currPPD->num_groups; ++gr) {
241  for (int opt = 0; opt < currPPD->groups[gr].num_options; ++opt) {
242  if (qstrcmp(currPPD->groups[gr].options[opt].keyword, key) == 0)
243  return &currPPD->groups[gr].options[opt];
244  }
245  }
246  }
247  return 0;
248 }
249 
250 const cups_option_t* QCUPSSupport::printerOption(const QString &key) const
251 {
252  for (int i = 0; i < printers[currPrinterIndex].num_options; ++i) {
254  return &printers[currPrinterIndex].options[i];
255  }
256  return 0;
257 }
258 
259 const ppd_option_t* QCUPSSupport::pageSizes() const
260 {
261  return page_sizes;
262 }
263 
264 int QCUPSSupport::markOption(const char* name, const char* value)
265 {
266  return _ppdMarkOption(currPPD, name, value);
267 }
268 
270 {
271  int oldOptionCount = printers[currPrinterIndex].num_options;
272  cups_option_t* oldOptions = printers[currPrinterIndex].options;
273 
274  int newOptionCount = 0;
275  cups_option_t* newOptions = 0;
276 
277  // copying old options that are not on the new list
278  for (int i = 0; i < oldOptionCount; ++i) {
279  bool contains = false;
280  for (int j = 0; j < options.count(); ++j) {
281  if (qstrcmp(options.at(j)->keyword, oldOptions[i].name) == 0) {
282  contains = true;
283  break;
284  }
285  }
286 
287  if (!contains) {
288  newOptionCount = _cupsAddOption(oldOptions[i].name, oldOptions[i].value, newOptionCount, &newOptions);
289  }
290  }
291 
292  // we can release old option list
293  _cupsFreeOptions(oldOptionCount, oldOptions);
294 
295  // adding marked options
296  for (int i = 0; i < markedOptions.count(); ++i) {
297  const char* name = markedOptions.at(i);
298  ++i;
299  newOptionCount = _cupsAddOption(name, markedOptions.at(i), newOptionCount, &newOptions);
300  }
301 
302  // placing the new option list
303  printers[currPrinterIndex].num_options = newOptionCount;
304  printers[currPrinterIndex].options = newOptions;
305 
306  // saving new default values
308 }
309 
310 QRect QCUPSSupport::paperRect(const char *choice) const
311 {
312  if (!currPPD)
313  return QRect();
314  for (int i = 0; i < currPPD->num_sizes; ++i) {
315  if (qstrcmp(currPPD->sizes[i].name, choice) == 0)
316  return QRect(0, 0, qRound(currPPD->sizes[i].width), qRound(currPPD->sizes[i].length));
317  }
318  return QRect();
319 }
320 
321 QRect QCUPSSupport::pageRect(const char *choice) const
322 {
323  if (!currPPD)
324  return QRect();
325  for (int i = 0; i < currPPD->num_sizes; ++i) {
326  if (qstrcmp(currPPD->sizes[i].name, choice) == 0)
327  return QRect(qRound(currPPD->sizes[i].left),
328  qRound(currPPD->sizes[i].length - currPPD->sizes[i].top),
329  qRound(currPPD->sizes[i].right - currPPD->sizes[i].left),
330  qRound(currPPD->sizes[i].top - currPPD->sizes[i].bottom));
331  }
332  return QRect();
333 }
334 
336 {
337  QStringList list;
338  collectMarkedOptions(list);
339  return list;
340 }
341 
342 bool QCUPSSupport::printerHasPPD(const char *printerName)
343 {
344  if (!isAvailable())
345  return false;
346  const char *ppdFile = _cupsGetPPD(printerName);
347  if (ppdFile)
348  unlink(ppdFile);
349  return (ppdFile != 0);
350 }
351 
353 {
354 #ifndef QT_NO_TEXTCODEC
355  return codec->toUnicode(s);
356 #else
357  return QLatin1String(s);
358 #endif
359 }
360 
361 void QCUPSSupport::collectMarkedOptions(QStringList& list, const ppd_group_t* group) const
362 {
363  if (group == 0) {
364  if (!currPPD)
365  return;
366  for (int i = 0; i < currPPD->num_groups; ++i) {
367  collectMarkedOptions(list, &currPPD->groups[i]);
368  collectMarkedOptionsHelper(list, &currPPD->groups[i]);
369  }
370  } else {
371  for (int i = 0; i < group->num_subgroups; ++i)
372  collectMarkedOptionsHelper(list, &group->subgroups[i]);
373  }
374 }
375 
376 void QCUPSSupport::collectMarkedOptionsHelper(QStringList& list, const ppd_group_t* group) const
377 {
378  for (int i = 0; i < group->num_options; ++i) {
379  for (int j = 0; j < group->options[i].num_choices; ++j) {
380  if (group->options[i].choices[j].marked == 1 && qstrcmp(group->options[i].choices[j].choice, group->options[i].defchoice) != 0)
381  list << QString::fromLocal8Bit(group->options[i].keyword) << QString::fromLocal8Bit(group->options[i].choices[j].choice);
382  }
383  }
384 }
385 
387 {
388  char filename[512];
389  int fd = _cupsTempFd(filename, 512);
390  return QPair<int, QString>(fd, QString::fromLocal8Bit(filename));
391 }
392 
393 // Prints the given file and returns a job id.
394 int QCUPSSupport::printFile(const char * printerName, const char * filename, const char * title,
395  int num_options, cups_option_t * options)
396 {
397  return _cupsPrintFile(printerName, filename, title, num_options, options);
398 }
399 
401 
402 #endif // QT_NO_CUPS
static QTextCodec * codecForLocale()
Returns a pointer to the codec most suitable for this locale.
const ppd_option_t * ppdOption(const char *key) const
Definition: qcups.cpp:237
const ppd_file_t * currentPPD() const
Definition: qcups.cpp:173
static PPDMarkOption _ppdMarkOption
Definition: qcups.cpp:79
static QString fromLocal8Bit(const char *, int size=-1)
Returns a QString initialized with the first size characters of the 8-bit string str.
Definition: qstring.cpp:4245
int markOption(const char *name, const char *value)
Definition: qcups.cpp:264
QCUPSSupport()
Definition: qcups.cpp:121
static CupsGetPPD _cupsGetPPD
Definition: qcups.cpp:74
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
int(* CupsTempFd)(char *name, int len)
Definition: qcups.cpp:67
int availablePrintersCount() const
Definition: qcups.cpp:163
int(* CupsMarkOptions)(ppd_file_t *ppd, int num_options, cups_option_t *options)
Definition: qcups.cpp:56
void(* CupsSetDests)(int num_dests, cups_dest_t *dests)
Definition: qcups.cpp:63
static CupsLangEncoding _cupsLangEncoding
Definition: qcups.cpp:83
static int qt_cups_num_printers
Definition: qcups.cpp:71
static CupsAddOption _cupsAddOption
Definition: qcups.cpp:84
static CupsSetDests _cupsSetDests
Definition: qcups.cpp:81
const ppd_option_t * page_sizes
Definition: qcups_p.h:109
static void resolveCups()
Definition: qcups.cpp:88
static CupsMarkOptions _cupsMarkOptions
Definition: qcups.cpp:78
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
static CupsPrintFile _cupsPrintFile
Definition: qcups.cpp:86
void * resolve(const char *symbol)
Returns the address of the exported symbol symbol.
Definition: qlibrary.cpp:1155
void collectMarkedOptionsHelper(QStringList &list, const ppd_group_t *group) const
Definition: qcups.cpp:376
The QString class provides a Unicode character string.
Definition: qstring.h:83
QRect pageRect(const char *choice) const
Definition: qcups.cpp:321
QTextCodec * codec
Definition: qcups_p.h:113
const cups_dest_t * availablePrinters() const
Definition: qcups.cpp:168
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
void(* PPDMarkDefaults)(ppd_file_t *ppd)
Definition: qcups.cpp:58
static bool isAvailable()
Definition: qcups.cpp:216
cups_dest_t * printers
Definition: qcups_p.h:108
int(* PPDMarkOption)(ppd_file_t *ppd, const char *keyword, const char *option)
Definition: qcups.cpp:59
QLocale::Language language
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static PPDMarkDefaults _ppdMarkDefaults
Definition: qcups.cpp:76
static CupsFreeDests _cupsFreeDests
Definition: qcups.cpp:73
const ppd_option_t * pageSizes() const
Definition: qcups.cpp:259
ppd_file_t * currPPD
Definition: qcups_p.h:111
int printFile(const char *printerName, const char *filename, const char *title, int num_options, cups_option_t *options)
Definition: qcups.cpp:394
ppd_file_t *(* PPDOpenFile)(const char *filename)
Definition: qcups.cpp:57
QRect paperRect(const char *choice) const
Definition: qcups.cpp:310
QStringList options() const
Definition: qcups.cpp:335
int(* CupsGetDests)(cups_dest_t **dests)
Definition: qcups.cpp:53
const char * name
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
void saveOptions(QList< const ppd_option_t *> options, QList< const char *> markedOptions)
Definition: qcups.cpp:269
static CupsFreeOptions _cupsFreeOptions
Definition: qcups.cpp:80
QPair< int, QString > tempFd()
Definition: qcups.cpp:386
static CupsGetDests _cupsGetDests
Definition: qcups.cpp:72
int currentPrinterIndex() const
Definition: qcups.cpp:211
void(* CupsFreeDests)(int num_dests, cups_dest_t *dests)
Definition: qcups.cpp:54
int(* CupsPrintFile)(const char *name, const char *filename, const char *title, int num_options, cups_option_t *options)
Definition: qcups.cpp:68
const char *(* CupsLangEncoding)(cups_lang_t *language)
Definition: qcups.cpp:65
static bool cupsLoaded
Definition: qcups.cpp:70
const char *(* CupsGetPPD)(const char *printer)
Definition: qcups.cpp:55
void(* PPDClose)(ppd_file_t *ppd)
Definition: qcups.cpp:60
QString toUnicode(const QByteArray &) const
Converts a from the encoding of this codec to Unicode, and returns the result in a QString...
const cups_option_t * printerOption(const QString &key) const
Definition: qcups.cpp:250
void(* CupsFreeOptions)(int num_options, cups_option_t *options)
Definition: qcups.cpp:62
bool load()
Loads the library and returns true if the library was loaded successfully; otherwise returns false...
Definition: qlibrary.cpp:909
const ppd_file_t * setCurrentPrinter(int index)
Definition: qcups.cpp:178
static PPDClose _ppdClose
Definition: qcups.cpp:77
QString unicodeString(const char *s)
Definition: qcups.cpp:352
int key
int(* CupsAddOption)(const char *name, const char *value, int num_options, cups_option_t **options)
Definition: qcups.cpp:66
~QCUPSSupport()
Definition: qcups.cpp:155
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
static QTextCodec * codecForName(const QByteArray &name)
Searches all installed QTextCodec objects and returns the one which best matches name; the match is c...
static PPDOpenFile _ppdOpenFile
Definition: qcups.cpp:75
quint16 index
void collectMarkedOptions(QStringList &list, const ppd_group_t *group=0) const
Definition: qcups.cpp:361
static CupsTempFd _cupsTempFd
Definition: qcups.cpp:85
int qstrcmp(const QByteArray &str1, const char *str2)
Definition: qbytearray.cpp:336
cups_lang_t *(* CupsLangGet)(const char *language)
Definition: qcups.cpp:64
static CupsLangGet _cupsLangGet
Definition: qcups.cpp:82
int currPrinterIndex
Definition: qcups_p.h:110
static bool printerHasPPD(const char *printerName)
Definition: qcups.cpp:342
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
The QLibrary class loads shared libraries at runtime.
Definition: qlibrary.h:62
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
int prnCount
Definition: qcups_p.h:107