Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / include / vcl / ppdparser.hxx
blob79f50a68d3fd515b24e6938534b8181893aa34a9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #ifndef INCLUDED_VCL_PPDPARSER_HXX
20 #define INCLUDED_VCL_PPDPARSER_HXX
22 #include <list>
23 #include <vector>
24 #include <boost/unordered_map.hpp>
26 #include <tools/stream.hxx>
27 #include <rtl/string.hxx>
28 #include <rtl/ustring.hxx>
29 #include <vcl/dllapi.h>
31 #include <com/sun/star/lang/Locale.hpp>
33 #define PRINTER_PPDDIR "driver"
35 namespace psp {
37 class PPDCache;
38 class PPDParser;
39 class PPDTranslator;
41 enum PPDValueType { eInvocation, eQuoted, eSymbol, eString, eNo };
43 struct VCL_DLLPUBLIC PPDValue
45 PPDValueType m_eType;
46 //CustomOption stuff for fdo#43049
47 //see http://www.cups.org/documentation.php/spec-ppd.html#OPTIONS
48 //for full specs, only the basics are implemented here
49 bool m_bCustomOption;
50 mutable OUString m_aCustomOption;
51 OUString m_aOption;
52 OUString m_aValue;
58 * PPDKey - a container for the available options (=values) of a PPD keyword
61 class VCL_DLLPUBLIC PPDKey
63 friend class PPDParser;
65 typedef ::boost::unordered_map< OUString, PPDValue, OUStringHash > hash_type;
66 typedef ::std::vector< PPDValue* > value_type;
68 OUString m_aKey;
69 hash_type m_aValues;
70 value_type m_aOrderedValues;
71 const PPDValue* m_pDefaultValue;
72 bool m_bQueryValue;
73 PPDValue m_aQueryValue;
75 public:
76 enum UIType { PickOne, PickMany, Boolean };
77 enum SetupType { ExitServer, Prolog, DocumentSetup, PageSetup, JCLSetup, AnySetup };
78 private:
80 bool m_bUIOption;
81 UIType m_eUIType;
82 int m_nOrderDependency;
83 SetupType m_eSetupType;
85 void eraseValue( const OUString& rOption );
86 public:
87 PPDKey( const OUString& rKey );
88 ~PPDKey();
90 PPDValue* insertValue(const OUString& rOption, PPDValueType eType, bool bCustomOption = false);
91 int countValues() const
92 { return m_aValues.size(); }
93 // neither getValue will return the query option
94 const PPDValue* getValue( int n ) const;
95 const PPDValue* getValue( const OUString& rOption ) const;
96 const PPDValue* getValueCaseInsensitive( const OUString& rOption ) const;
97 const PPDValue* getDefaultValue() const { return m_pDefaultValue; }
98 const PPDValue* getQueryValue() const { return m_bQueryValue ? &m_aQueryValue : NULL; }
100 const OUString& getKey() const { return m_aKey; }
101 bool isUIKey() const { return m_bUIOption; }
102 UIType getUIType() const { return m_eUIType; }
103 SetupType getSetupType() const { return m_eSetupType; }
104 int getOrderDependency() const { return m_nOrderDependency; }
107 // define a hash for PPDKey
108 struct PPDKeyhash
110 size_t operator()( const PPDKey * pKey) const
111 { return (size_t)pKey; }
117 * PPDParser - parses a PPD file and contains all available keys from it
120 class PPDContext;
121 class CUPSManager;
123 class VCL_DLLPUBLIC PPDParser
125 friend class PPDContext;
126 friend class CUPSManager;
127 friend class PPDCache;
129 typedef ::boost::unordered_map< OUString, PPDKey*, OUStringHash > hash_type;
130 typedef ::std::vector< PPDKey* > value_type;
132 void insertKey( const OUString& rKey, PPDKey* pKey );
133 public:
134 struct PPDConstraint
136 const PPDKey* m_pKey1;
137 const PPDValue* m_pOption1;
138 const PPDKey* m_pKey2;
139 const PPDValue* m_pOption2;
141 PPDConstraint() : m_pKey1( NULL ), m_pOption1( NULL ), m_pKey2( NULL ), m_pOption2( NULL ) {}
143 private:
144 hash_type m_aKeys;
145 value_type m_aOrderedKeys;
146 ::std::list< PPDConstraint > m_aConstraints;
148 // some identifying fields
149 OUString m_aPrinterName;
150 OUString m_aNickName;
151 // the full path of the PPD file
152 OUString m_aFile;
153 // some basic attributes
154 bool m_bColorDevice;
155 bool m_bType42Capable;
156 sal_uLong m_nLanguageLevel;
157 rtl_TextEncoding m_aFileEncoding;
160 // shortcuts to important keys and their default values
161 // imageable area
162 const PPDValue* m_pDefaultImageableArea;
163 const PPDKey* m_pImageableAreas;
164 // paper dimensions
165 const PPDValue* m_pDefaultPaperDimension;
166 const PPDKey* m_pPaperDimensions;
167 // paper trays
168 const PPDValue* m_pDefaultInputSlot;
169 const PPDKey* m_pInputSlots;
170 // resolutions
171 const PPDValue* m_pDefaultResolution;
172 const PPDKey* m_pResolutions;
173 // duplex commands
174 const PPDValue* m_pDefaultDuplexType;
175 const PPDKey* m_pDuplexTypes;
177 // fonts
178 const PPDKey* m_pFontList;
180 // translations
181 PPDTranslator* m_pTranslator;
183 PPDParser( const OUString& rFile );
184 ~PPDParser();
186 void parseOrderDependency(const OString& rLine);
187 void parseOpenUI(const OString& rLine);
188 void parseConstraint(const OString& rLine);
189 void parse( std::list< OString >& rLines );
191 OUString handleTranslation(const OString& i_rString, bool i_bIsGlobalized);
193 static void scanPPDDir( const OUString& rDir );
194 static void initPPDFiles(PPDCache &rPPDCache);
195 static OUString getPPDFile( const OUString& rFile );
196 public:
197 static const PPDParser* getParser( const OUString& rFile );
198 static void freeAll();
200 const OUString& getFilename() const { return m_aFile; }
202 const PPDKey* getKey( int n ) const;
203 const PPDKey* getKey( const OUString& rKey ) const;
204 int getKeys() const { return m_aKeys.size(); }
205 bool hasKey( const PPDKey* ) const;
207 const ::std::list< PPDConstraint >& getConstraints() const { return m_aConstraints; }
209 const OUString& getPrinterName() const
210 { return m_aPrinterName.isEmpty() ? m_aNickName : m_aPrinterName; }
211 const OUString& getNickName() const
212 { return m_aNickName.isEmpty() ? m_aPrinterName : m_aNickName; }
214 bool isColorDevice() const { return m_bColorDevice; }
215 bool isType42Capable() const { return m_bType42Capable; }
216 sal_uLong getLanguageLevel() const { return m_nLanguageLevel; }
218 OUString getDefaultPaperDimension() const;
219 void getDefaultPaperDimension( int& rWidth, int& rHeight ) const
220 { getPaperDimension( getDefaultPaperDimension(), rWidth, rHeight ); }
221 bool getPaperDimension( const OUString& rPaperName,
222 int& rWidth, int& rHeight ) const;
223 // width and height in pt
224 // returns false if paper not found
225 int getPaperDimensions() const
226 { return m_pPaperDimensions ? m_pPaperDimensions->countValues() : 0; }
228 // match the best paper for width and height
229 OUString matchPaper( int nWidth, int nHeight ) const;
231 bool getMargins( const OUString& rPaperName,
232 int &rLeft, int& rRight,
233 int &rUpper, int& rLower ) const;
234 // values in pt
235 // returns true if paper found
237 // values int pt
239 OUString getDefaultInputSlot() const;
240 int getInputSlots() const
241 { return m_pInputSlots ? m_pInputSlots->countValues() : 0; }
243 void getDefaultResolution( int& rXRes, int& rYRes ) const;
244 // values in dpi
245 void getResolutionFromString( const OUString&, int&, int& ) const;
246 // helper function
248 int getDuplexTypes() const
249 { return m_pDuplexTypes ? m_pDuplexTypes->countValues() : 0; }
251 int getFonts() const
252 { return m_pFontList ? m_pFontList->countValues() : 0; }
255 OUString translateKey( const OUString& i_rKey,
256 const com::sun::star::lang::Locale& i_rLocale = com::sun::star::lang::Locale() ) const;
257 OUString translateOption( const OUString& i_rKey,
258 const OUString& i_rOption,
259 const com::sun::star::lang::Locale& i_rLocale = com::sun::star::lang::Locale() ) const;
265 * PPDContext - a class to manage user definable states based on the
266 * contents of a PPDParser.
269 class VCL_DLLPUBLIC PPDContext
271 typedef ::boost::unordered_map< const PPDKey*, const PPDValue*, PPDKeyhash > hash_type;
272 hash_type m_aCurrentValues;
273 const PPDParser* m_pParser;
275 // returns false: check failed, new value is constrained
276 // true: check succeeded, new value can be set
277 bool checkConstraints( const PPDKey*, const PPDValue*, bool bDoReset );
278 bool resetValue( const PPDKey*, bool bDefaultable = false );
279 public:
280 PPDContext( const PPDParser* pParser = NULL );
281 PPDContext( const PPDContext& rContext ) { operator=( rContext ); }
282 PPDContext& operator=( const PPDContext& rContext );
283 ~PPDContext();
285 void setParser( const PPDParser* );
286 const PPDParser* getParser() const { return m_pParser; }
288 const PPDValue* getValue( const PPDKey* ) const;
289 const PPDValue* setValue( const PPDKey*, const PPDValue*, bool bDontCareForConstraints = false );
291 int countValuesModified() const { return m_aCurrentValues.size(); }
292 const PPDKey* getModifiedKey( int n ) const;
294 // public wrapper for the private method
295 bool checkConstraints( const PPDKey*, const PPDValue* );
297 // for printer setup
298 char* getStreamableBuffer( sal_uLong& rBytes ) const;
299 void rebuildFromStreamBuffer( char* pBuffer, sal_uLong nBytes );
301 // convenience
302 int getRenderResolution() const;
304 // width, height in points, paper will contain the name of the selected
305 // paper after the call
306 void getPageSize( OUString& rPaper, int& rWidth, int& rHeight ) const;
309 } // namespace
311 #endif // INCLUDED_VCL_PPDPARSER_HXX
313 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */