Replace 'i < len-1 && func(i+1)' by 'i+1 < len && func(i+1)'
[profile/ivi/qtbase.git] / src / gui / painting / qcups.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 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 ** 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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
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
51 QT_BEGIN_NAMESPACE
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;
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;
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
121 QCUPSSupport::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
154 QCUPSSupport::~QCUPSSupport()
155 {
156      if (currPPD)
157         _ppdClose(currPPD);
158      if (prnCount)
159          _cupsFreeDests(prnCount, printers);
160 }
161
162 int QCUPSSupport::availablePrintersCount() const
163 {
164     return prnCount;
165 }
166
167 const cups_dest_t* QCUPSSupport::availablePrinters() const
168 {
169     return printers;
170 }
171
172 const ppd_file_t* QCUPSSupport::currentPPD() const
173 {
174     return currPPD;
175 }
176
177 const 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
210 int QCUPSSupport::currentPrinterIndex() const
211 {
212     return currPrinterIndex;
213 }
214
215 bool 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
236 const 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
249 const 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
258 const ppd_option_t* QCUPSSupport::pageSizes() const
259 {
260     return page_sizes;
261 }
262
263 int QCUPSSupport::markOption(const char* name, const char* value)
264 {
265     return _ppdMarkOption(currPPD, name, value);
266 }
267
268 void 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
309 QRect 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
320 QRect 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
334 QStringList QCUPSSupport::options() const
335 {
336     QStringList list;
337     collectMarkedOptions(list);
338     return list;
339 }
340
341 bool QCUPSSupport::printerHasPPD(const char *printerName)
342 {
343     if (!isAvailable())
344         return false;
345     const char *ppdFile = _cupsGetPPD(printerName);
346     if (ppdFile)
347         unlink(ppdFile);
348     return (ppdFile != 0);
349 }
350
351 QString QCUPSSupport::unicodeString(const char *s)
352 {
353 #ifndef QT_NO_TEXTCODEC
354     return codec->toUnicode(s);
355 #else
356     return QLatin1String(s);
357 #endif
358 }
359
360 void QCUPSSupport::collectMarkedOptions(QStringList& list, const ppd_group_t* group) const
361 {
362     if (group == 0) {
363         if (!currPPD)
364             return;
365         for (int i = 0; i < currPPD->num_groups; ++i) {
366             collectMarkedOptions(list, &currPPD->groups[i]);
367             collectMarkedOptionsHelper(list, &currPPD->groups[i]);
368         }
369     } else {
370         for (int i = 0; i < group->num_subgroups; ++i)
371             collectMarkedOptionsHelper(list, &group->subgroups[i]);
372     }
373 }
374
375 void QCUPSSupport::collectMarkedOptionsHelper(QStringList& list, const ppd_group_t* group) const
376 {
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);
381         }
382     }
383 }
384
385 QPair<int, QString> QCUPSSupport::tempFd()
386 {
387     char filename[512];
388     int fd = _cupsTempFd(filename, 512);
389     return QPair<int, QString>(fd, QString::fromLocal8Bit(filename));
390 }
391
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)
395 {
396     return _cupsPrintFile(printerName, filename, title, num_options, options);
397 }
398
399 QT_END_NAMESPACE
400
401 #endif // QT_NO_CUPS