1 |
769 |
jeremybenn |
/* CupsPrintServiceLookup.java -- Implementation based on CUPS
|
2 |
|
|
Copyright (C) 2006 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package gnu.javax.print;
|
40 |
|
|
|
41 |
|
|
import gnu.javax.print.ipp.IppException;
|
42 |
|
|
|
43 |
|
|
import java.util.ArrayList;
|
44 |
|
|
|
45 |
|
|
import javax.print.DocFlavor;
|
46 |
|
|
import javax.print.MultiDocPrintService;
|
47 |
|
|
import javax.print.PrintService;
|
48 |
|
|
import javax.print.PrintServiceLookup;
|
49 |
|
|
import javax.print.attribute.Attribute;
|
50 |
|
|
import javax.print.attribute.AttributeSet;
|
51 |
|
|
|
52 |
|
|
/**
|
53 |
|
|
* The platform default implementation based on CUPS.
|
54 |
|
|
*
|
55 |
|
|
* @author Wolfgang Baer (WBaer@gmx.de)
|
56 |
|
|
*/
|
57 |
|
|
public class CupsPrintServiceLookup extends PrintServiceLookup
|
58 |
|
|
{
|
59 |
|
|
private CupsServer server;
|
60 |
|
|
|
61 |
|
|
/**
|
62 |
|
|
* Default constructor checking security access.
|
63 |
|
|
*/
|
64 |
|
|
public CupsPrintServiceLookup()
|
65 |
|
|
{
|
66 |
|
|
// security
|
67 |
|
|
SecurityManager sm = System.getSecurityManager();
|
68 |
|
|
if (sm != null)
|
69 |
|
|
sm.checkPrintJobAccess();
|
70 |
|
|
|
71 |
|
|
// use the localhost cups server
|
72 |
|
|
server = new CupsServer(null, null);
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
/**
|
76 |
|
|
* This is the printer marked as default in CUPS.
|
77 |
|
|
*
|
78 |
|
|
* @return The default lookup service or
|
79 |
|
|
* <code>null</code> if there is no default.
|
80 |
|
|
*/
|
81 |
|
|
public PrintService getDefaultPrintService()
|
82 |
|
|
{
|
83 |
|
|
try
|
84 |
|
|
{
|
85 |
|
|
return server.getDefaultPrinter();
|
86 |
|
|
}
|
87 |
|
|
catch (IppException e)
|
88 |
|
|
{
|
89 |
|
|
// if discovery fails treat as if there is none
|
90 |
|
|
return null;
|
91 |
|
|
}
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
/**
|
95 |
|
|
* All printers and printer classes of the CUPS server are checked.
|
96 |
|
|
* If flavors or attributes are null the constraint is not used.
|
97 |
|
|
*
|
98 |
|
|
* @param flavors the document flavors which have to be supported.
|
99 |
|
|
* @param attributes the attributes which have to be supported.
|
100 |
|
|
*
|
101 |
|
|
* @return The multidoc print services of the implementing lookup service
|
102 |
|
|
* for the given parameters, or an array of length 0 if none is available.
|
103 |
|
|
*/
|
104 |
|
|
public MultiDocPrintService[] getMultiDocPrintServices(DocFlavor[] flavors,
|
105 |
|
|
AttributeSet attributes)
|
106 |
|
|
{
|
107 |
|
|
ArrayList result = new ArrayList();
|
108 |
|
|
PrintService[] services = getPrintServices();
|
109 |
|
|
|
110 |
|
|
for (int i=0; i < services.length; i++)
|
111 |
|
|
{
|
112 |
|
|
if (checkMultiDocPrintService(flavors, attributes, services[i]))
|
113 |
|
|
result.add(services[i]);
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
return (MultiDocPrintService[]) result.toArray(
|
117 |
|
|
new MultiDocPrintService[result.size()]);
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
/**
|
121 |
|
|
* These are all printers and printer classes of the CUPS server.
|
122 |
|
|
*
|
123 |
|
|
* @return All known print services regardless of supported features,
|
124 |
|
|
* or an array of length 0 if none is available.
|
125 |
|
|
*/
|
126 |
|
|
public PrintService[] getPrintServices()
|
127 |
|
|
{
|
128 |
|
|
ArrayList result = new ArrayList();
|
129 |
|
|
|
130 |
|
|
try
|
131 |
|
|
{
|
132 |
|
|
result.addAll(server.getAllPrinters());
|
133 |
|
|
result.addAll(server.getAllClasses());
|
134 |
|
|
}
|
135 |
|
|
catch (IppException e)
|
136 |
|
|
{
|
137 |
|
|
// ignore as this method cannot throw exceptions
|
138 |
|
|
// if print service discovery fails - bad luck
|
139 |
|
|
}
|
140 |
|
|
return (PrintService[]) result.toArray(new PrintService[result.size()]);
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
|
144 |
|
|
/**
|
145 |
|
|
* All printers and printer classes of the CUPS server are checked.
|
146 |
|
|
* If flavor or attributes are null the constraint is not used.
|
147 |
|
|
*
|
148 |
|
|
* @param flavor the document flavor which has to be supported.
|
149 |
|
|
* @param attributes the attributes which have to be supported.
|
150 |
|
|
*
|
151 |
|
|
* @return The print services of the implementing lookup service
|
152 |
|
|
* for the given parameters, or an array of length 0 if none is available.
|
153 |
|
|
*/
|
154 |
|
|
public PrintService[] getPrintServices(DocFlavor flavor,
|
155 |
|
|
AttributeSet attributes)
|
156 |
|
|
{
|
157 |
|
|
ArrayList result = new ArrayList();
|
158 |
|
|
PrintService[] services = getPrintServices();
|
159 |
|
|
|
160 |
|
|
for (int i=0; i < services.length; i++)
|
161 |
|
|
{
|
162 |
|
|
if (checkPrintService(flavor, attributes, services[i]))
|
163 |
|
|
result.add(services[i]);
|
164 |
|
|
}
|
165 |
|
|
|
166 |
|
|
return (PrintService[]) result.toArray(new PrintService[result.size()]);
|
167 |
|
|
}
|
168 |
|
|
|
169 |
|
|
/**
|
170 |
|
|
* Checks the given print service - own method so it can be used also
|
171 |
|
|
* to check application registered print services from PrintServiceLookup.
|
172 |
|
|
*
|
173 |
|
|
* @param flavor the document flavor which has to be supported.
|
174 |
|
|
* @param attributes the attributes which have to be supported.
|
175 |
|
|
* @param service the service to check
|
176 |
|
|
*
|
177 |
|
|
* @return <code>true</code> if all constraints match, <code>false</code>
|
178 |
|
|
* otherwise.
|
179 |
|
|
*/
|
180 |
|
|
public boolean checkPrintService(DocFlavor flavor, AttributeSet attributes,
|
181 |
|
|
PrintService service)
|
182 |
|
|
{
|
183 |
|
|
boolean allAttributesSupported = true;
|
184 |
|
|
if (flavor == null || service.isDocFlavorSupported(flavor))
|
185 |
|
|
{
|
186 |
|
|
if (attributes == null || attributes.size() == 0)
|
187 |
|
|
return allAttributesSupported;
|
188 |
|
|
|
189 |
|
|
Attribute[] atts = attributes.toArray();
|
190 |
|
|
for (int i = 0; i < atts.length; i++)
|
191 |
|
|
{
|
192 |
|
|
if (! service.isAttributeCategorySupported(atts[i].getCategory()))
|
193 |
|
|
{
|
194 |
|
|
allAttributesSupported = false;
|
195 |
|
|
break;
|
196 |
|
|
}
|
197 |
|
|
}
|
198 |
|
|
return allAttributesSupported;
|
199 |
|
|
}
|
200 |
|
|
|
201 |
|
|
return false;
|
202 |
|
|
}
|
203 |
|
|
|
204 |
|
|
/**
|
205 |
|
|
* Checks the given print service - own method so it can be used also
|
206 |
|
|
* to check application registered print services from PrintServiceLookup.
|
207 |
|
|
*
|
208 |
|
|
* @param flavors the document flavors which have to be supported.
|
209 |
|
|
* @param attributes the attributes which have to be supported.
|
210 |
|
|
* @param service the service to check
|
211 |
|
|
*
|
212 |
|
|
* @return <code>true</code> if all constraints match, <code>false</code>
|
213 |
|
|
* otherwise.
|
214 |
|
|
*/
|
215 |
|
|
public boolean checkMultiDocPrintService(DocFlavor[] flavors,
|
216 |
|
|
AttributeSet attributes, PrintService service)
|
217 |
|
|
{
|
218 |
|
|
if (service instanceof MultiDocPrintService)
|
219 |
|
|
{
|
220 |
|
|
boolean allFlavorsSupported = true;
|
221 |
|
|
boolean allAttributesSupported = true;
|
222 |
|
|
|
223 |
|
|
if (flavors == null || flavors.length != 0)
|
224 |
|
|
allFlavorsSupported = true;
|
225 |
|
|
else
|
226 |
|
|
{
|
227 |
|
|
for (int k = 0; k < flavors.length; k++)
|
228 |
|
|
{
|
229 |
|
|
if (! service.isDocFlavorSupported(flavors[k]))
|
230 |
|
|
{
|
231 |
|
|
allFlavorsSupported = false;
|
232 |
|
|
break;
|
233 |
|
|
}
|
234 |
|
|
}
|
235 |
|
|
}
|
236 |
|
|
|
237 |
|
|
if (attributes == null || attributes.size() == 0)
|
238 |
|
|
allAttributesSupported = true;
|
239 |
|
|
else
|
240 |
|
|
{
|
241 |
|
|
Attribute[] atts = attributes.toArray();
|
242 |
|
|
for (int j = 0; j < atts.length; j++)
|
243 |
|
|
{
|
244 |
|
|
if (! service.isAttributeCategorySupported(
|
245 |
|
|
atts[j].getCategory()))
|
246 |
|
|
{
|
247 |
|
|
allAttributesSupported = false;
|
248 |
|
|
break;
|
249 |
|
|
}
|
250 |
|
|
}
|
251 |
|
|
}
|
252 |
|
|
|
253 |
|
|
if (allAttributesSupported && allFlavorsSupported)
|
254 |
|
|
return true;
|
255 |
|
|
}
|
256 |
|
|
|
257 |
|
|
return false;
|
258 |
|
|
}
|
259 |
|
|
|
260 |
|
|
}
|