update credits
[LibreOffice.git] / include / vcl / ppdparser.hxx
blob80fbeaf4728a42e66ab8989a7daa39c558554b64
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 _PSPRINT_PPDPARSER_HXX_
20 #define _PSPRINT_PPDPARSER_HXX_
22 #include <list>
23 #include <vector>
24 #include <boost/unordered_map.hpp>
26 #include "tools/string.hxx"
27 #include "tools/stream.hxx"
28 #include "rtl/string.hxx"
30 #include "vcl/dllapi.h"
32 #include "com/sun/star/lang/Locale.hpp"
34 #define PRINTER_PPDDIR "driver"
36 namespace psp {
38 class PPDParser;
39 class PPDTranslator;
41 enum PPDValueType { eInvocation, eQuoted, eSymbol, eString, eNo };
43 struct VCL_DLLPUBLIC PPDValue
45 PPDValueType m_eType;
46 String m_aOption;
47 String m_aValue;
50 // ----------------------------------------------------------------------
53 * PPDKey - a container for the available options (=values) of a PPD keyword
56 class VCL_DLLPUBLIC PPDKey
58 friend class PPDParser;
60 typedef ::boost::unordered_map< OUString, PPDValue, OUStringHash > hash_type;
61 typedef ::std::vector< PPDValue* > value_type;
63 String m_aKey;
64 hash_type m_aValues;
65 value_type m_aOrderedValues;
66 const PPDValue* m_pDefaultValue;
67 bool m_bQueryValue;
68 PPDValue m_aQueryValue;
70 public:
71 enum UIType { PickOne, PickMany, Boolean };
72 enum SetupType { ExitServer, Prolog, DocumentSetup, PageSetup, JCLSetup, AnySetup };
73 private:
75 bool m_bUIOption;
76 UIType m_eUIType;
77 int m_nOrderDependency;
78 SetupType m_eSetupType;
80 void eraseValue( const String& rOption );
81 public:
82 PPDKey( const String& rKey );
83 ~PPDKey();
85 PPDValue* insertValue( const String& rOption );
86 int countValues() const
87 { return m_aValues.size(); }
88 // neither getValue will return the query option
89 const PPDValue* getValue( int n ) const;
90 const PPDValue* getValue( const String& rOption ) const;
91 const PPDValue* getValueCaseInsensitive( const String& rOption ) const;
92 const PPDValue* getDefaultValue() const { return m_pDefaultValue; }
93 const PPDValue* getQueryValue() const { return m_bQueryValue ? &m_aQueryValue : NULL; }
95 const String& getKey() const { return m_aKey; }
96 bool isUIKey() const { return m_bUIOption; }
97 UIType getUIType() const { return m_eUIType; }
98 SetupType getSetupType() const { return m_eSetupType; }
99 int getOrderDependency() const { return m_nOrderDependency; }
102 // define a hash for PPDKey
103 struct PPDKeyhash
105 size_t operator()( const PPDKey * pKey) const
106 { return (size_t)pKey; }
109 // ----------------------------------------------------------------------
112 * PPDParser - parses a PPD file and contains all available keys from it
115 class PPDContext;
116 class CUPSManager;
118 class VCL_DLLPUBLIC PPDParser
120 friend class PPDContext;
121 friend class CUPSManager;
122 friend class PPDCache;
124 typedef ::boost::unordered_map< OUString, PPDKey*, OUStringHash > hash_type;
125 typedef ::std::vector< PPDKey* > value_type;
127 void insertKey( const String& rKey, PPDKey* pKey );
128 public:
129 struct PPDConstraint
131 const PPDKey* m_pKey1;
132 const PPDValue* m_pOption1;
133 const PPDKey* m_pKey2;
134 const PPDValue* m_pOption2;
136 PPDConstraint() : m_pKey1( NULL ), m_pOption1( NULL ), m_pKey2( NULL ), m_pOption2( NULL ) {}
138 private:
139 hash_type m_aKeys;
140 value_type m_aOrderedKeys;
141 ::std::list< PPDConstraint > m_aConstraints;
143 // some identifying fields
144 String m_aPrinterName;
145 String m_aNickName;
146 // the full path of the PPD file
147 String m_aFile;
148 // some basic attributes
149 bool m_bColorDevice;
150 bool m_bType42Capable;
151 sal_uLong m_nLanguageLevel;
152 rtl_TextEncoding m_aFileEncoding;
155 // shortcuts to important keys and their default values
156 // imageable area
157 const PPDValue* m_pDefaultImageableArea;
158 const PPDKey* m_pImageableAreas;
159 // paper dimensions
160 const PPDValue* m_pDefaultPaperDimension;
161 const PPDKey* m_pPaperDimensions;
162 // paper trays
163 const PPDValue* m_pDefaultInputSlot;
164 const PPDKey* m_pInputSlots;
165 // resolutions
166 const PPDValue* m_pDefaultResolution;
167 const PPDKey* m_pResolutions;
168 // duplex commands
169 const PPDValue* m_pDefaultDuplexType;
170 const PPDKey* m_pDuplexTypes;
172 // fonts
173 const PPDKey* m_pFontList;
175 // translations
176 PPDTranslator* m_pTranslator;
178 PPDParser( const String& rFile );
179 ~PPDParser();
181 void parseOrderDependency(const OString& rLine);
182 void parseOpenUI(const OString& rLine);
183 void parseConstraint(const OString& rLine);
184 void parse( std::list< OString >& rLines );
186 String handleTranslation(const OString& i_rString, bool i_bIsGlobalized);
188 static void scanPPDDir( const String& rDir );
189 static void initPPDFiles();
190 static String getPPDFile( const String& rFile );
191 public:
192 static const PPDParser* getParser( const String& rFile );
193 static String getPPDPrinterName( const String& rFile );
194 static void freeAll();
195 static void getKnownPPDDrivers( std::list< OUString >& o_rDrivers, bool bRefresh = false );
197 const String& getFilename() const { return m_aFile; }
199 const PPDKey* getKey( int n ) const;
200 const PPDKey* getKey( const String& rKey ) const;
201 int getKeys() const { return m_aKeys.size(); }
202 bool hasKey( const PPDKey* ) const;
204 const ::std::list< PPDConstraint >& getConstraints() const { return m_aConstraints; }
206 const String& getPrinterName() const
207 { return m_aPrinterName.Len() ? m_aPrinterName : m_aNickName; }
208 const String& getNickName() const
209 { return m_aNickName.Len() ? m_aNickName : m_aPrinterName; }
211 bool isColorDevice() const { return m_bColorDevice; }
212 bool isType42Capable() const { return m_bType42Capable; }
213 sal_uLong getLanguageLevel() const { return m_nLanguageLevel; }
215 String getDefaultPaperDimension() const;
216 void getDefaultPaperDimension( int& rWidth, int& rHeight ) const
217 { getPaperDimension( getDefaultPaperDimension(), rWidth, rHeight ); }
218 bool getPaperDimension( const String& rPaperName,
219 int& rWidth, int& rHeight ) const;
220 // width and height in pt
221 // returns false if paper not found
222 int getPaperDimensions() const
223 { return m_pPaperDimensions ? m_pPaperDimensions->countValues() : 0; }
225 // match the best paper for width and height
226 String matchPaper( int nWidth, int nHeight ) const;
228 bool getMargins( const String& rPaperName,
229 int &rLeft, int& rRight,
230 int &rUpper, int& rLower ) const;
231 // values in pt
232 // returns true if paper found
234 // values int pt
236 String getDefaultInputSlot() const;
237 int getInputSlots() const
238 { return m_pInputSlots ? m_pInputSlots->countValues() : 0; }
240 void getDefaultResolution( int& rXRes, int& rYRes ) const;
241 // values in dpi
242 void getResolutionFromString( const String&, int&, int& ) const;
243 // helper function
245 int getDuplexTypes() const
246 { return m_pDuplexTypes ? m_pDuplexTypes->countValues() : 0; }
248 int getFonts() const
249 { return m_pFontList ? m_pFontList->countValues() : 0; }
250 String getFont( int ) const;
253 OUString translateKey( const OUString& i_rKey,
254 const com::sun::star::lang::Locale& i_rLocale = com::sun::star::lang::Locale() ) const;
255 OUString translateOption( const OUString& i_rKey,
256 const OUString& i_rOption,
257 const com::sun::star::lang::Locale& i_rLocale = com::sun::star::lang::Locale() ) const;
260 // ----------------------------------------------------------------------
263 * PPDContext - a class to manage user definable states based on the
264 * contents of a PPDParser.
267 class VCL_DLLPUBLIC PPDContext
269 typedef ::boost::unordered_map< const PPDKey*, const PPDValue*, PPDKeyhash > hash_type;
270 hash_type m_aCurrentValues;
271 const PPDParser* m_pParser;
273 // returns false: check failed, new value is constrained
274 // true: check succeded, new value can be set
275 bool checkConstraints( const PPDKey*, const PPDValue*, bool bDoReset );
276 bool resetValue( const PPDKey*, bool bDefaultable = false );
277 public:
278 PPDContext( const PPDParser* pParser = NULL );
279 PPDContext( const PPDContext& rContext ) { operator=( rContext ); }
280 PPDContext& operator=( const PPDContext& rContext );
281 ~PPDContext();
283 void setParser( const PPDParser* );
284 const PPDParser* getParser() const { return m_pParser; }
286 const PPDValue* getValue( const PPDKey* ) const;
287 const PPDValue* setValue( const PPDKey*, const PPDValue*, bool bDontCareForConstraints = false );
289 int countValuesModified() const { return m_aCurrentValues.size(); }
290 const PPDKey* getModifiedKey( int n ) const;
292 // public wrapper for the private method
293 bool checkConstraints( const PPDKey*, const PPDValue* );
295 // for printer setup
296 char* getStreamableBuffer( sal_uLong& rBytes ) const;
297 void rebuildFromStreamBuffer( char* pBuffer, sal_uLong nBytes );
299 // convenience
300 int getRenderResolution() const;
302 // width, height in points, paper will contain the name of the selected
303 // paper after the call
304 void getPageSize( OUString& rPaper, int& rWidth, int& rHeight ) const;
307 } // namespace
309 #endif // _PSPRINT_PPDPARSER_HXX_
311 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */