Branch libreoffice-5-0-4
[LibreOffice.git] / include / vcl / ppdparser.hxx
blob02fb83a730cb8e66e569ae11a8be1d6123246356
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 <unordered_map>
24 #include <vector>
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 std::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;
74 OUString m_aGroup;
76 public:
77 enum UIType { PickOne, PickMany, Boolean };
78 enum SetupType { ExitServer, Prolog, DocumentSetup, PageSetup, JCLSetup, AnySetup };
79 private:
81 bool m_bUIOption;
82 UIType m_eUIType;
83 int m_nOrderDependency;
84 SetupType m_eSetupType;
86 void eraseValue( const OUString& rOption );
87 public:
88 PPDKey( const OUString& rKey );
89 ~PPDKey();
91 PPDValue* insertValue(const OUString& rOption, PPDValueType eType, bool bCustomOption = false);
92 int countValues() const
93 { return m_aValues.size(); }
94 // neither getValue will return the query option
95 const PPDValue* getValue( int n ) const;
96 const PPDValue* getValue( const OUString& rOption ) const;
97 const PPDValue* getValueCaseInsensitive( const OUString& rOption ) const;
98 const PPDValue* getDefaultValue() const { return m_pDefaultValue; }
99 const PPDValue* getQueryValue() const { return m_bQueryValue ? &m_aQueryValue : NULL; }
100 const OUString& getGroup() const { return m_aGroup; }
102 const OUString& getKey() const { return m_aKey; }
103 bool isUIKey() const { return m_bUIOption; }
104 UIType getUIType() const { return m_eUIType; }
105 SetupType getSetupType() const { return m_eSetupType; }
106 int getOrderDependency() const { return m_nOrderDependency; }
109 // define a hash for PPDKey
110 struct PPDKeyhash
112 size_t operator()( const PPDKey * pKey) const
113 { return reinterpret_cast<size_t>(pKey); }
119 * PPDParser - parses a PPD file and contains all available keys from it
122 class PPDContext;
123 class CUPSManager;
125 class VCL_DLLPUBLIC PPDParser
127 friend class PPDContext;
128 friend class CUPSManager;
129 friend class PPDCache;
131 typedef std::unordered_map< OUString, PPDKey*, OUStringHash > hash_type;
132 typedef std::vector< PPDKey* > value_type;
134 void insertKey( const OUString& rKey, PPDKey* pKey );
135 public:
136 struct PPDConstraint
138 const PPDKey* m_pKey1;
139 const PPDValue* m_pOption1;
140 const PPDKey* m_pKey2;
141 const PPDValue* m_pOption2;
143 PPDConstraint() : m_pKey1( NULL ), m_pOption1( NULL ), m_pKey2( NULL ), m_pOption2( NULL ) {}
145 private:
146 hash_type m_aKeys;
147 value_type m_aOrderedKeys;
148 ::std::list< PPDConstraint > m_aConstraints;
150 // some identifying fields
151 OUString m_aPrinterName;
152 OUString m_aNickName;
153 // the full path of the PPD file
154 OUString m_aFile;
155 // some basic attributes
156 bool m_bColorDevice;
157 bool m_bType42Capable;
158 sal_uLong m_nLanguageLevel;
159 rtl_TextEncoding m_aFileEncoding;
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;
179 // fonts
180 const PPDKey* m_pFontList;
182 // translations
183 PPDTranslator* m_pTranslator;
185 PPDParser( const OUString& rFile );
186 ~PPDParser();
188 void parseOrderDependency(const OString& rLine);
189 void parseOpenUI(const OString& rLine, const OString& rPPDGroup);
190 void parseConstraint(const OString& rLine);
191 void parse( std::list< OString >& rLines );
193 OUString handleTranslation(const OString& i_rString, bool i_bIsGlobalized);
195 static void scanPPDDir( const OUString& rDir );
196 static void initPPDFiles(PPDCache &rPPDCache);
197 static OUString getPPDFile( const OUString& rFile );
198 public:
199 static const PPDParser* getParser( const OUString& rFile );
200 static void freeAll();
202 const OUString& getFilename() const { return m_aFile; }
204 const PPDKey* getKey( int n ) const;
205 const PPDKey* getKey( const OUString& rKey ) const;
206 int getKeys() const { return m_aKeys.size(); }
207 bool hasKey( const PPDKey* ) const;
209 const ::std::list< PPDConstraint >& getConstraints() const { return m_aConstraints; }
211 const OUString& getPrinterName() const
212 { return m_aPrinterName.isEmpty() ? m_aNickName : m_aPrinterName; }
213 const OUString& getNickName() const
214 { return m_aNickName.isEmpty() ? m_aPrinterName : m_aNickName; }
216 bool isColorDevice() const { return m_bColorDevice; }
217 bool isType42Capable() const { return m_bType42Capable; }
218 sal_uLong getLanguageLevel() const { return m_nLanguageLevel; }
220 OUString getDefaultPaperDimension() const;
221 void getDefaultPaperDimension( int& rWidth, int& rHeight ) const
222 { getPaperDimension( getDefaultPaperDimension(), rWidth, rHeight ); }
223 bool getPaperDimension( const OUString& rPaperName,
224 int& rWidth, int& rHeight ) const;
225 // width and height in pt
226 // returns false if paper not found
227 int getPaperDimensions() const
228 { return m_pPaperDimensions ? m_pPaperDimensions->countValues() : 0; }
230 // match the best paper for width and height
231 OUString matchPaper( int nWidth, int nHeight ) const;
233 bool getMargins( const OUString& rPaperName,
234 int &rLeft, int& rRight,
235 int &rUpper, int& rLower ) const;
236 // values in pt
237 // returns true if paper found
239 // values int pt
241 OUString getDefaultInputSlot() const;
242 int getInputSlots() const
243 { return m_pInputSlots ? m_pInputSlots->countValues() : 0; }
245 void getDefaultResolution( int& rXRes, int& rYRes ) const;
246 // values in dpi
247 static void getResolutionFromString( const OUString&, int&, int& );
248 // helper function
250 int getDuplexTypes() const
251 { return m_pDuplexTypes ? m_pDuplexTypes->countValues() : 0; }
253 int getFonts() const
254 { return m_pFontList ? m_pFontList->countValues() : 0; }
257 OUString translateKey( const OUString& i_rKey,
258 const com::sun::star::lang::Locale& i_rLocale = com::sun::star::lang::Locale() ) const;
259 OUString translateOption( const OUString& i_rKey,
260 const OUString& i_rOption,
261 const com::sun::star::lang::Locale& i_rLocale = com::sun::star::lang::Locale() ) const;
267 * PPDContext - a class to manage user definable states based on the
268 * contents of a PPDParser.
271 class VCL_DLLPUBLIC PPDContext
273 typedef std::unordered_map< const PPDKey*, const PPDValue*, PPDKeyhash > hash_type;
274 hash_type m_aCurrentValues;
275 const PPDParser* m_pParser;
277 // returns false: check failed, new value is constrained
278 // true: check succeeded, new value can be set
279 bool checkConstraints( const PPDKey*, const PPDValue*, bool bDoReset );
280 bool resetValue( const PPDKey*, bool bDefaultable = false );
281 public:
282 PPDContext( const PPDParser* pParser = NULL );
283 PPDContext( const PPDContext& rContext ) { operator=( rContext ); }
284 PPDContext& operator=( const PPDContext& rContext );
285 ~PPDContext();
287 void setParser( const PPDParser* );
288 const PPDParser* getParser() const { return m_pParser; }
290 const PPDValue* getValue( const PPDKey* ) const;
291 const PPDValue* setValue( const PPDKey*, const PPDValue*, bool bDontCareForConstraints = false );
293 int countValuesModified() const { return m_aCurrentValues.size(); }
294 const PPDKey* getModifiedKey( int n ) const;
296 // public wrapper for the private method
297 bool checkConstraints( const PPDKey*, const PPDValue* );
299 // for printer setup
300 char* getStreamableBuffer( sal_uLong& rBytes ) const;
301 void rebuildFromStreamBuffer( char* pBuffer, sal_uLong nBytes );
303 // convenience
304 int getRenderResolution() const;
306 // width, height in points, paper will contain the name of the selected
307 // paper after the call
308 void getPageSize( OUString& rPaper, int& rWidth, int& rHeight ) const;
311 } // namespace
313 #endif // INCLUDED_VCL_PPDPARSER_HXX
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */