1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
23 #include <unordered_map>
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"
41 enum PPDValueType
{ eInvocation
, eQuoted
, eSymbol
, eString
, eNo
};
43 struct VCL_DLLPUBLIC PPDValue
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
50 mutable OUString m_aCustomOption
;
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
;
70 value_type m_aOrderedValues
;
71 const PPDValue
* m_pDefaultValue
;
73 PPDValue m_aQueryValue
;
77 enum UIType
{ PickOne
, PickMany
, Boolean
};
78 enum SetupType
{ ExitServer
, Prolog
, DocumentSetup
, PageSetup
, JCLSetup
, AnySetup
};
83 int m_nOrderDependency
;
84 SetupType m_eSetupType
;
86 void eraseValue( const OUString
& rOption
);
88 PPDKey( const OUString
& rKey
);
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
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
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
);
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
) {}
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
155 // some basic attributes
157 bool m_bType42Capable
;
158 sal_uLong m_nLanguageLevel
;
159 rtl_TextEncoding m_aFileEncoding
;
162 // shortcuts to important keys and their default values
164 const PPDValue
* m_pDefaultImageableArea
;
165 const PPDKey
* m_pImageableAreas
;
167 const PPDValue
* m_pDefaultPaperDimension
;
168 const PPDKey
* m_pPaperDimensions
;
170 const PPDValue
* m_pDefaultInputSlot
;
171 const PPDKey
* m_pInputSlots
;
173 const PPDValue
* m_pDefaultResolution
;
174 const PPDKey
* m_pResolutions
;
176 const PPDValue
* m_pDefaultDuplexType
;
177 const PPDKey
* m_pDuplexTypes
;
180 const PPDKey
* m_pFontList
;
183 PPDTranslator
* m_pTranslator
;
185 PPDParser( const OUString
& rFile
);
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
);
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;
237 // returns true if paper found
241 OUString
getDefaultInputSlot() const;
242 int getInputSlots() const
243 { return m_pInputSlots
? m_pInputSlots
->countValues() : 0; }
245 void getDefaultResolution( int& rXRes
, int& rYRes
) const;
247 static void getResolutionFromString( const OUString
&, int&, int& );
250 int getDuplexTypes() const
251 { return m_pDuplexTypes
? m_pDuplexTypes
->countValues() : 0; }
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 );
282 PPDContext( const PPDParser
* pParser
= NULL
);
283 PPDContext( const PPDContext
& rContext
) { operator=( rContext
); }
284 PPDContext
& operator=( const PPDContext
& rContext
);
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
* );
300 char* getStreamableBuffer( sal_uLong
& rBytes
) const;
301 void rebuildFromStreamBuffer( char* pBuffer
, sal_uLong nBytes
);
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;
313 #endif // INCLUDED_VCL_PPDPARSER_HXX
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */