Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 : #ifndef INCLUDED_VCL_PPDPARSER_HXX
20 : #define INCLUDED_VCL_PPDPARSER_HXX
21 :
22 : #include <list>
23 : #include <unordered_map>
24 : #include <vector>
25 :
26 : #include <tools/stream.hxx>
27 : #include <rtl/string.hxx>
28 : #include <rtl/ustring.hxx>
29 : #include <vcl/dllapi.h>
30 :
31 : #include <com/sun/star/lang/Locale.hpp>
32 :
33 : #define PRINTER_PPDDIR "driver"
34 :
35 : namespace psp {
36 :
37 : class PPDCache;
38 : class PPDParser;
39 : class PPDTranslator;
40 :
41 : enum PPDValueType { eInvocation, eQuoted, eSymbol, eString, eNo };
42 :
43 132720 : struct VCL_DLLPUBLIC PPDValue
44 : {
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;
53 : };
54 :
55 :
56 :
57 : /*
58 : * PPDKey - a container for the available options (=values) of a PPD keyword
59 : */
60 :
61 : class VCL_DLLPUBLIC PPDKey
62 : {
63 : friend class PPDParser;
64 :
65 : typedef std::unordered_map< OUString, PPDValue, OUStringHash > hash_type;
66 : typedef std::vector< PPDValue* > value_type;
67 :
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 :
75 : public:
76 : enum UIType { PickOne, PickMany, Boolean };
77 : enum SetupType { ExitServer, Prolog, DocumentSetup, PageSetup, JCLSetup, AnySetup };
78 : private:
79 :
80 : bool m_bUIOption;
81 : UIType m_eUIType;
82 : int m_nOrderDependency;
83 : SetupType m_eSetupType;
84 :
85 : void eraseValue( const OUString& rOption );
86 : public:
87 : PPDKey( const OUString& rKey );
88 : ~PPDKey();
89 :
90 : PPDValue* insertValue(const OUString& rOption, PPDValueType eType, bool bCustomOption = false);
91 65419 : int countValues() const
92 65419 : { 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 2456 : const PPDValue* getDefaultValue() const { return m_pDefaultValue; }
98 : const PPDValue* getQueryValue() const { return m_bQueryValue ? &m_aQueryValue : NULL; }
99 :
100 3653 : const OUString& getKey() const { return m_aKey; }
101 0 : bool isUIKey() const { return m_bUIOption; }
102 : UIType getUIType() const { return m_eUIType; }
103 0 : SetupType getSetupType() const { return m_eSetupType; }
104 0 : int getOrderDependency() const { return m_nOrderDependency; }
105 : };
106 :
107 : // define a hash for PPDKey
108 : struct PPDKeyhash
109 : {
110 3924 : size_t operator()( const PPDKey * pKey) const
111 3924 : { return reinterpret_cast<size_t>(pKey); }
112 : };
113 :
114 :
115 :
116 : /*
117 : * PPDParser - parses a PPD file and contains all available keys from it
118 : */
119 :
120 : class PPDContext;
121 : class CUPSManager;
122 :
123 : class VCL_DLLPUBLIC PPDParser
124 : {
125 : friend class PPDContext;
126 : friend class CUPSManager;
127 : friend class PPDCache;
128 :
129 : typedef std::unordered_map< OUString, PPDKey*, OUStringHash > hash_type;
130 : typedef std::vector< PPDKey* > value_type;
131 :
132 : void insertKey( const OUString& rKey, PPDKey* pKey );
133 : public:
134 : struct PPDConstraint
135 : {
136 : const PPDKey* m_pKey1;
137 : const PPDValue* m_pOption1;
138 : const PPDKey* m_pKey2;
139 : const PPDValue* m_pOption2;
140 :
141 0 : PPDConstraint() : m_pKey1( NULL ), m_pOption1( NULL ), m_pKey2( NULL ), m_pOption2( NULL ) {}
142 : };
143 : private:
144 : hash_type m_aKeys;
145 : value_type m_aOrderedKeys;
146 : ::std::list< PPDConstraint > m_aConstraints;
147 :
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;
158 :
159 :
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;
176 :
177 : // fonts
178 : const PPDKey* m_pFontList;
179 :
180 : // translations
181 : PPDTranslator* m_pTranslator;
182 :
183 : PPDParser( const OUString& rFile );
184 : ~PPDParser();
185 :
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 );
190 :
191 : OUString handleTranslation(const OString& i_rString, bool i_bIsGlobalized);
192 :
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();
199 :
200 : const OUString& getFilename() const { return m_aFile; }
201 :
202 : const PPDKey* getKey( int n ) const;
203 : const PPDKey* getKey( const OUString& rKey ) const;
204 0 : int getKeys() const { return m_aKeys.size(); }
205 : bool hasKey( const PPDKey* ) const;
206 :
207 106 : const ::std::list< PPDConstraint >& getConstraints() const { return m_aConstraints; }
208 :
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; }
213 :
214 430 : bool isColorDevice() const { return m_bColorDevice; }
215 430 : bool isType42Capable() const { return m_bType42Capable; }
216 430 : sal_uLong getLanguageLevel() const { return m_nLanguageLevel; }
217 :
218 : OUString getDefaultPaperDimension() const;
219 0 : void getDefaultPaperDimension( int& rWidth, int& rHeight ) const
220 0 : { 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; }
227 :
228 : // match the best paper for width and height
229 : OUString matchPaper( int nWidth, int nHeight ) const;
230 :
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
236 :
237 : // values int pt
238 :
239 : OUString getDefaultInputSlot() const;
240 : int getInputSlots() const
241 : { return m_pInputSlots ? m_pInputSlots->countValues() : 0; }
242 :
243 : void getDefaultResolution( int& rXRes, int& rYRes ) const;
244 : // values in dpi
245 : static void getResolutionFromString( const OUString&, int&, int& );
246 : // helper function
247 :
248 : int getDuplexTypes() const
249 : { return m_pDuplexTypes ? m_pDuplexTypes->countValues() : 0; }
250 :
251 : int getFonts() const
252 : { return m_pFontList ? m_pFontList->countValues() : 0; }
253 :
254 :
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;
260 : };
261 :
262 :
263 :
264 : /*
265 : * PPDContext - a class to manage user definable states based on the
266 : * contents of a PPDParser.
267 : */
268 :
269 : class VCL_DLLPUBLIC PPDContext
270 : {
271 : typedef std::unordered_map< const PPDKey*, const PPDValue*, PPDKeyhash > hash_type;
272 : hash_type m_aCurrentValues;
273 : const PPDParser* m_pParser;
274 :
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();
284 :
285 : void setParser( const PPDParser* );
286 811 : const PPDParser* getParser() const { return m_pParser; }
287 :
288 : const PPDValue* getValue( const PPDKey* ) const;
289 : const PPDValue* setValue( const PPDKey*, const PPDValue*, bool bDontCareForConstraints = false );
290 :
291 316 : int countValuesModified() const { return m_aCurrentValues.size(); }
292 : const PPDKey* getModifiedKey( int n ) const;
293 :
294 : // public wrapper for the private method
295 : bool checkConstraints( const PPDKey*, const PPDValue* );
296 :
297 : // for printer setup
298 : char* getStreamableBuffer( sal_uLong& rBytes ) const;
299 : void rebuildFromStreamBuffer( char* pBuffer, sal_uLong nBytes );
300 :
301 : // convenience
302 : int getRenderResolution() const;
303 :
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;
307 : };
308 :
309 : } // namespace
310 :
311 : #endif // INCLUDED_VCL_PPDPARSER_HXX
312 :
313 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|