xref: /trunk/main/vcl/inc/vcl/ppdparser.hxx (revision 86e1cf34)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef _PSPRINT_PPDPARSER_HXX_
24 #define _PSPRINT_PPDPARSER_HXX_
25 
26 #include <list>
27 #include <vector>
28 #include <hash_map>
29 
30 #include "tools/string.hxx"
31 #include "tools/stream.hxx"
32 
33 #include "vcl/dllapi.h"
34 
35 #include "com/sun/star/lang/Locale.hpp"
36 
37 #define PRINTER_PPDDIR "driver"
38 
39 namespace psp {
40 
41 class PPDParser;
42 class PPDTranslator;
43 
44 enum PPDValueType { eInvocation, eQuoted, eSymbol, eString, eNo };
45 
46 struct VCL_DLLPUBLIC PPDValue
47 {
48     PPDValueType    m_eType;
49     String          m_aOption;
50     String          m_aValue;
51 };
52 
53 // ----------------------------------------------------------------------
54 
55 /*
56  * PPDKey - a container for the available options (=values) of a PPD keyword
57  */
58 
59 class VCL_DLLPUBLIC PPDKey
60 {
61     friend class PPDParser;
62 
63     typedef ::std::hash_map< ::rtl::OUString, PPDValue, ::rtl::OUStringHash > hash_type;
64     typedef ::std::vector< PPDValue* > value_type;
65 
66     String          	m_aKey;
67     hash_type			m_aValues;
68     value_type			m_aOrderedValues;
69     const PPDValue*		m_pDefaultValue;
70     bool            	m_bQueryValue;
71     PPDValue        	m_aQueryValue;
72 
73 public:
74     enum UIType { PickOne, PickMany, Boolean };
75     enum SetupType { ExitServer, Prolog, DocumentSetup, PageSetup, JCLSetup, AnySetup };
76 private:
77 
78     bool				m_bUIOption;
79     UIType				m_eUIType;
80     int					m_nOrderDependency;
81     SetupType			m_eSetupType;
82 
83     void eraseValue( const String& rOption );
84 public:
85     PPDKey( const String& rKey );
86     ~PPDKey();
87 
88     PPDValue*           insertValue( const String& rOption );
countValues() const89     int                 countValues() const
90     { return m_aValues.size(); }
91     // neither getValue will return the query option
92     const PPDValue*         getValue( int n ) const;
93     const PPDValue*         getValue( const String& rOption ) const;
94     const PPDValue*         getValueCaseInsensitive( const String& rOption ) const;
getDefaultValue() const95     const PPDValue*         getDefaultValue() const { return m_pDefaultValue; }
getQueryValue() const96     const PPDValue*     getQueryValue() const { return m_bQueryValue ? &m_aQueryValue : NULL; }
97 
getKey() const98     const String&       getKey() const { return m_aKey; }
isUIKey() const99     bool                isUIKey() const { return m_bUIOption; }
getUIType() const100     UIType              getUIType() const { return m_eUIType; }
getSetupType() const101     SetupType           getSetupType() const { return m_eSetupType; }
getOrderDependency() const102     int                 getOrderDependency() const { return m_nOrderDependency; }
103 };
104 
105 // define a hash for PPDKey
106 struct PPDKeyhash
107 {
operator ()psp::PPDKeyhash108     size_t operator()( const PPDKey * pKey) const
109         { return (size_t)pKey; }
110 };
111 
112 // ----------------------------------------------------------------------
113 
114 /*
115  * PPDParser - parses a PPD file and contains all available keys from it
116  */
117 
118 class PPDContext;
119 class CUPSManager;
120 
121 class VCL_DLLPUBLIC PPDParser
122 {
123     friend class PPDContext;
124     friend class CUPSManager;
125 
126     typedef ::std::hash_map< ::rtl::OUString, PPDKey*, ::rtl::OUStringHash > hash_type;
127     typedef ::std::vector< PPDKey* > value_type;
128 
129     void insertKey( const String& rKey, PPDKey* pKey );
130 public:
131     struct PPDConstraint
132     {
133         const PPDKey*       m_pKey1;
134         const PPDValue*     m_pOption1;
135         const PPDKey*       m_pKey2;
136         const PPDValue*     m_pOption2;
137 
PPDConstraintpsp::PPDParser::PPDConstraint138         PPDConstraint() : m_pKey1( NULL ), m_pOption1( NULL ), m_pKey2( NULL ), m_pOption2( NULL ) {}
139     };
140 private:
141 
142     static ::std::list< PPDParser* >           aAllParsers;
143     static ::std::hash_map< rtl::OUString, rtl::OUString, rtl::OUStringHash >*
144     											pAllPPDFiles;
145 
146     hash_type									m_aKeys;
147     value_type									m_aOrderedKeys;
148     ::std::list< PPDConstraint >				m_aConstraints;
149 
150     // some identifying fields
151     String                          			m_aPrinterName;
152     String                          			m_aNickName;
153     // the full path of the PPD file
154     String                          			m_aFile;
155     // some basic attributes
156     bool                            			m_bColorDevice;
157     bool                            			m_bType42Capable;
158     sal_uLong                           			m_nLanguageLevel;
159     rtl_TextEncoding                            m_aFileEncoding;
160 
161 
162     // shortcuts to important keys and their default values
163     // imageable area
164     const PPDValue*                     		m_pDefaultImageableArea;
165     const PPDKey*                       		m_pImageableAreas;
166     // paper dimensions
167     const PPDValue*                     		m_pDefaultPaperDimension;
168     const PPDKey*                       		m_pPaperDimensions;
169     // paper trays
170     const PPDValue*                     		m_pDefaultInputSlot;
171     const PPDKey*                       		m_pInputSlots;
172     // resolutions
173     const PPDValue*                     		m_pDefaultResolution;
174     const PPDKey*                       		m_pResolutions;
175     // duplex commands
176     const PPDValue*                     		m_pDefaultDuplexType;
177     const PPDKey*                       		m_pDuplexTypes;
178 
179     // fonts
180     const PPDKey*                       		m_pFontList;
181 
182     // translations
183     PPDTranslator*                              m_pTranslator;
184 
185     PPDParser( const String& rFile );
186     ~PPDParser();
187 
188     void parseOrderDependency( const ByteString& rLine );
189     void parseOpenUI( const ByteString& rLine );
190     void parseConstraint( const ByteString& rLine );
191     void parse( std::list< ByteString >& rLines );
192 
193     String handleTranslation( const ByteString& i_rString, bool i_bIsGlobalized );
194 
195     static void scanPPDDir( const String& rDir );
196     static void initPPDFiles();
197     static String getPPDFile( const String& rFile );
198 public:
199     static const PPDParser* getParser( const String& rFile );
200     static String getPPDPrinterName( const String& rFile );
201     static void freeAll();
202     static void getKnownPPDDrivers( std::list< rtl::OUString >& o_rDrivers, bool bRefresh = false );
203 
getFilename() const204     const String&   getFilename() const { return m_aFile; }
205 
206     const PPDKey*   getKey( int n ) const;
207     const PPDKey*   getKey( const String& rKey ) const;
getKeys() const208     int             getKeys() const { return m_aKeys.size(); }
209     bool            hasKey( const PPDKey* ) const;
210 
getConstraints() const211     const ::std::list< PPDConstraint >& getConstraints() const { return m_aConstraints; }
212 
getPrinterName() const213     const String&   getPrinterName() const
214     { return m_aPrinterName.Len() ? m_aPrinterName : m_aNickName; }
getNickName() const215     const String&   getNickName() const
216     { return m_aNickName.Len() ? m_aNickName : m_aPrinterName; }
217 
isColorDevice() const218     bool            isColorDevice() const { return m_bColorDevice; }
isType42Capable() const219     bool            isType42Capable() const { return m_bType42Capable; }
getLanguageLevel() const220     sal_uLong           getLanguageLevel() const { return m_nLanguageLevel; }
221 
222     String          getDefaultPaperDimension() const;
getDefaultPaperDimension(int & rWidth,int & rHeight) const223     void            getDefaultPaperDimension( int& rWidth, int& rHeight ) const
224     { getPaperDimension( getDefaultPaperDimension(), rWidth, rHeight ); }
225     bool getPaperDimension( const String& rPaperName,
226                             int& rWidth, int& rHeight ) const;
227     // width and height in pt
228     // returns false if paper not found
getPaperDimensions() const229     int             getPaperDimensions() const
230     { return m_pPaperDimensions ? m_pPaperDimensions->countValues() : 0; }
231     String          getPaperDimension( int ) const;
232     String          getPaperDimensionCommand( int ) const;
233     String          getPaperDimensionCommand( const String & ) const;
234 
235     // match the best paper for width and height
236     String          matchPaper( int nWidth, int nHeight ) const;
237 
238     bool getMargins( const String& rPaperName,
239                      int &rLeft, int& rRight,
240                      int &rUpper, int& rLower ) const;
241     // values in pt
242     // returns true if paper found
243 
244     // values int pt
245 
246     String          getDefaultInputSlot() const;
getInputSlots() const247     int             getInputSlots() const
248     { return m_pInputSlots ? m_pInputSlots->countValues() : 0; }
249     String          getSlot( int ) const;
250     String          getSlotCommand( int ) const;
251     String          getSlotCommand( const String& ) const;
252 
253     void            getDefaultResolution( int& rXRes, int& rYRes ) const;
254     int             getResolutions() const;
255     void            getResolution( int, int& rXRes, int& rYRes ) const;
256     String          getResolutionCommand( int nXRes, int nYRes ) const;
257     // values in dpi
258     void            getResolutionFromString( const String&, int&, int& ) const;
259     // helper function
260 
261     String          getDefaultDuplexType() const;
getDuplexTypes() const262     int             getDuplexTypes() const
263     { return m_pDuplexTypes ? m_pDuplexTypes->countValues() : 0; }
264     String          getDuplex( int ) const;
265     String          getDuplexCommand( int ) const;
266     String          getDuplexCommand( const String& ) const;
267 
getFonts() const268     int             getFonts() const
269     { return m_pFontList ? m_pFontList->countValues() : 0; }
270     void            getFontAttributes( int,
271                                        String& rEncoding,
272                                        String& rCharset ) const;
273     void            getFontAttributes( const String&,
274                                        String& rEncoding,
275                                        String& rCharset ) const;
276     String          getFont( int ) const;
277 
278 
279     rtl::OUString   translateKey( const rtl::OUString& i_rKey,
280                                   const com::sun::star::lang::Locale& i_rLocale = com::sun::star::lang::Locale() ) const;
281     rtl::OUString   translateOption( const rtl::OUString& i_rKey,
282                                      const rtl::OUString& i_rOption,
283                                      const com::sun::star::lang::Locale& i_rLocale = com::sun::star::lang::Locale() ) const;
284     rtl::OUString   translateValue( const rtl::OUString& i_rKey,
285                                     const rtl::OUString& i_rOption,
286                                     const rtl::OUString& i_rValue,
287                                     const com::sun::star::lang::Locale& i_rLocale = com::sun::star::lang::Locale() ) const;
288 };
289 
290 // ----------------------------------------------------------------------
291 
292 /*
293  * PPDContext - a class to manage user definable states based on the
294  * contents of a PPDParser.
295  */
296 
297 class VCL_DLLPUBLIC PPDContext
298 {
299     typedef ::std::hash_map< const PPDKey*, const PPDValue*, PPDKeyhash > hash_type;
300     hash_type m_aCurrentValues;
301     const PPDParser*                                    m_pParser;
302 
303     // returns false: check failed, new value is constrained
304     //         true:  check succeeded, new value can be set
305     bool checkConstraints( const PPDKey*, const PPDValue*, bool bDoReset );
306     bool resetValue( const PPDKey*, bool bDefaultable = false );
307 public:
308     PPDContext( const PPDParser* pParser = NULL );
PPDContext(const PPDContext & rContext)309     PPDContext( const PPDContext& rContext ) { operator=( rContext ); }
310     PPDContext& operator=( const PPDContext& rContext );
311     ~PPDContext();
312 
313     void setParser( const PPDParser* );
getParser() const314     const PPDParser* getParser() const { return m_pParser; }
315 
316     const PPDValue* getValue( const PPDKey* ) const;
317     const PPDValue* setValue( const PPDKey*, const PPDValue*, bool bDontCareForConstraints = false );
318 
countValuesModified() const319     int countValuesModified() const { return m_aCurrentValues.size(); }
320     const PPDKey* getModifiedKey( int n ) const;
321 
322     // public wrapper for the private method
323     bool checkConstraints( const PPDKey*, const PPDValue* );
324 
325     void getUnconstrainedValues( const PPDKey*, ::std::list< const PPDValue* >& rValues );
326 
327     // for printer setup
328     void*   getStreamableBuffer( sal_uLong& rBytes ) const;
329     void    rebuildFromStreamBuffer( void* pBuffer, sal_uLong nBytes );
330 
331     // convenience
332     int getRenderResolution() const;
333 
334     // width, height in points, paper will contain the name of the selected
335     // paper after the call
336     void getPageSize( String& rPaper, int& rWidth, int& rHeight ) const;
337 };
338 
339 } // namespace
340 
341 #endif // _PSPRINT_PPDPARSER_HXX_
342