CupsPrintServiceLookup.java 8.1 KB

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