source: trunk/src/gui/painting/qprinterinfo_unix.cpp@ 427

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

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

File size: 37.9 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
42#include "qprinterinfo.h"
43
44#include <qfile.h>
45#include <qfileinfo.h>
46#include <qdir.h>
47#include <qprintdialog.h>
48#include <qlibrary.h>
49#include <qtextstream.h>
50
51#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
52# include <private/qcups_p.h>
53# include <cups/cups.h>
54# include <private/qpdf_p.h>
55#endif
56
57#include <private/qprinterinfo_unix_p.h>
58
59QT_BEGIN_NAMESPACE
60
61#ifndef QT_NO_PRINTER
62
63class QPrinterInfoPrivate
64{
65Q_DECLARE_PUBLIC(QPrinterInfo)
66public:
67 QPrinterInfoPrivate();
68 QPrinterInfoPrivate(const QString& name);
69 ~QPrinterInfoPrivate();
70
71 static QPrinter::PaperSize string2PaperSize(const QString& str);
72 static QString pageSize2String(QPrinter::PaperSize size);
73
74private:
75 QString m_name;
76 bool m_isNull;
77 bool m_default;
78 QList<QPrinter::PaperSize> m_paperSizes;
79
80 QPrinterInfo* q_ptr;
81};
82
83static QPrinterInfoPrivate nullQPrinterInfoPrivate;
84
85/////////////////////////////////////////////////////////////////////////////
86/////////////////////////////////////////////////////////////////////////////
87
88void qt_perhapsAddPrinter(QList<QPrinterDescription> *printers, const QString &name,
89 QString host, QString comment,
90 QStringList aliases)
91{
92 for (int i = 0; i < printers->size(); ++i)
93 if (printers->at(i).samePrinter(name))
94 return;
95
96#ifndef QT_NO_PRINTDIALOG
97 if (host.isEmpty())
98 host = QPrintDialog::tr("locally connected");
99#endif
100 printers->append(QPrinterDescription(name.simplified(), host.simplified(), comment.simplified(), aliases));
101}
102
103void qt_parsePrinterDesc(QString printerDesc, QList<QPrinterDescription> *printers)
104{
105 if (printerDesc.length() < 1)
106 return;
107
108 printerDesc = printerDesc.simplified();
109 int i = printerDesc.indexOf(QLatin1Char(':'));
110 QString printerName, printerComment, printerHost;
111 QStringList aliases;
112
113 if (i >= 0) {
114 // have ':' want '|'
115 int j = printerDesc.indexOf(QLatin1Char('|'));
116 if (j > 0 && j < i) {
117 printerName = printerDesc.left(j);
118 aliases = printerDesc.mid(j + 1, i - j - 1).split(QLatin1Char('|'));
119#ifndef QT_NO_PRINTDIALOG
120 // try extracting a comment from the aliases
121 printerComment = QPrintDialog::tr("Aliases: %1")
122 .arg(aliases.join(QLatin1String(", ")));
123#endif
124 } else {
125 printerName = printerDesc.left(i);
126 }
127 // look for lprng pseudo all printers entry
128 i = printerDesc.indexOf(QRegExp(QLatin1String(": *all *=")));
129 if (i >= 0)
130 printerName = QString();
131 // look for signs of this being a remote printer
132 i = printerDesc.indexOf(QRegExp(QLatin1String(": *rm *=")));
133 if (i >= 0) {
134 // point k at the end of remote host name
135 while (printerDesc[i] != QLatin1Char('='))
136 i++;
137 while (printerDesc[i] == QLatin1Char('=') || printerDesc[i].isSpace())
138 i++;
139 j = i;
140 while (j < (int)printerDesc.length() && printerDesc[j] != QLatin1Char(':'))
141 j++;
142
143 // and stuff that into the string
144 printerHost = printerDesc.mid(i, j - i);
145 }
146 }
147 if (printerName.length())
148 qt_perhapsAddPrinter(printers, printerName, printerHost, printerComment,
149 aliases);
150}
151
152int qt_parsePrintcap(QList<QPrinterDescription> *printers, const QString& fileName)
153{
154 QFile printcap(fileName);
155 if (!printcap.open(QIODevice::ReadOnly))
156 return NotFound;
157
158 char *line_ascii = new char[1025];
159 line_ascii[1024] = '\0';
160
161 QString printerDesc;
162 bool atEnd = false;
163
164 while (!atEnd) {
165 if (printcap.atEnd() || printcap.readLine(line_ascii, 1024) <= 0)
166 atEnd = true;
167 QString line = QString::fromLocal8Bit(line_ascii);
168 line = line.trimmed();
169 if (line.length() >= 1 && line[int(line.length()) - 1] == QLatin1Char('\\'))
170 line.chop(1);
171 if (line[0] == QLatin1Char('#')) {
172 if (!atEnd)
173 continue;
174 } else if (line[0] == QLatin1Char('|') || line[0] == QLatin1Char(':')
175 || line.isEmpty()) {
176 printerDesc += line;
177 if (!atEnd)
178 continue;
179 }
180
181 qt_parsePrinterDesc(printerDesc, printers);
182
183 // add the first line of the new printer definition
184 printerDesc = line;
185 }
186 delete[] line_ascii;
187 return Success;
188}
189
190/*!
191 \internal
192
193 Checks $HOME/.printers for a line matching '_default <name>' (where
194 <name> does not contain any white space). The first such match
195 results in <name> being returned.
196 If no lines match then an empty string is returned.
197*/
198QString qt_getDefaultFromHomePrinters()
199{
200 QFile file(QDir::homePath() + QLatin1String("/.printers"));
201 if (!file.open(QIODevice::ReadOnly))
202 return QString();
203 QString all(QLatin1String(file.readAll()));
204 QStringList words = all.split(QRegExp(QLatin1String("\\W+")), QString::SkipEmptyParts);
205 const int i = words.indexOf(QLatin1String("_default"));
206 if (i != -1 && i < words.size() - 1)
207 return words.at(i + 1);
208 return QString();
209}
210
211// solaris, not 2.6
212void qt_parseEtcLpPrinters(QList<QPrinterDescription> *printers)
213{
214 QDir lp(QLatin1String("/etc/lp/printers"));
215 QFileInfoList dirs = lp.entryInfoList();
216 if (dirs.isEmpty())
217 return;
218
219 QString tmp;
220 for (int i = 0; i < dirs.size(); ++i) {
221 QFileInfo printer = dirs.at(i);
222 if (printer.isDir()) {
223 tmp.sprintf("/etc/lp/printers/%s/configuration",
224 printer.fileName().toAscii().data());
225 QFile configuration(tmp);
226 char *line = new char[1025];
227 QString remote(QLatin1String("Remote:"));
228 QString contentType(QLatin1String("Content types:"));
229 QString printerHost;
230 bool canPrintPostscript = false;
231 if (configuration.open(QIODevice::ReadOnly)) {
232 while (!configuration.atEnd() &&
233 configuration.readLine(line, 1024) > 0) {
234 if (QString::fromLatin1(line).startsWith(remote)) {
235 const char *p = line;
236 while (*p != ':')
237 p++;
238 p++;
239 while (isspace((uchar) *p))
240 p++;
241 printerHost = QString::fromLocal8Bit(p);
242 printerHost = printerHost.simplified();
243 } else if (QString::fromLatin1(line).startsWith(contentType)) {
244 char *p = line;