LibreOffice Module vcl (master)  1
ppdparser.hxx
Go to the documentation of this file.
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 <memory>
23 #include <unordered_map>
24 #include <vector>
25 
26 #include <rtl/string.hxx>
27 #include <rtl/ustring.hxx>
28 #include <tools/solar.h>
29 #include <vcl/dllapi.h>
30 
31 #define PRINTER_PPDDIR "driver"
32 
33 namespace psp {
34 
35 class PPDCache;
36 class PPDTranslator;
37 
39 
41 {
43  //CustomOption stuff for fdo#43049
44  //see http://www.cups.org/documentation.php/spec-ppd.html#OPTIONS
45  //for full specs, only the basics are implemented here
47  mutable OUString m_aCustomOption;
48  OUString m_aOption;
49  OUString m_aValue;
50 };
51 
52 
53 /*
54  * PPDKey - a container for the available options (=values) of a PPD keyword
55  */
56 
58 {
59  friend class PPDParser;
60  friend class CPDManager;
61 
62  typedef std::unordered_map< OUString, PPDValue > hash_type;
63  typedef std::vector< PPDValue* > value_type;
64 
65  OUString const m_aKey;
66  hash_type m_aValues;
67  value_type m_aOrderedValues;
70  OUString m_aGroup;
71 
72 public:
73  enum class SetupType { ExitServer, Prolog, DocumentSetup, PageSetup, JCLSetup, AnySetup };
74 private:
75 
79 
80  void eraseValue( const OUString& rOption );
81 public:
82  PPDKey( const OUString& rKey );
83  ~PPDKey();
84 
85  PPDValue* insertValue(const OUString& rOption, PPDValueType eType, bool bCustomOption = false);
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 OUString& rOption ) const;
91  const PPDValue* getValueCaseInsensitive( const OUString& rOption ) const;
92  const PPDValue* getDefaultValue() const { return m_pDefaultValue; }
93  const OUString& getGroup() const { return m_aGroup; }
94 
95  const OUString& getKey() const { return m_aKey; }
96  bool isUIKey() const { return m_bUIOption; }
97  SetupType getSetupType() const { return m_eSetupType; }
98  int getOrderDependency() const { return m_nOrderDependency; }
99 };
100 
101 // define a hash for PPDKey
103 {
104  size_t operator()( const PPDKey * pKey) const
105  { return reinterpret_cast<size_t>(pKey); }
106 };
107 
108 
109 /*
110  * PPDParser - parses a PPD file and contains all available keys from it
111  */
112 
114 {
115  friend class PPDContext;
116  friend class CUPSManager;
117  friend class CPDManager;
118  friend class PPDCache;
119 
120  typedef std::unordered_map< OUString, std::unique_ptr<PPDKey> > hash_type;
121  typedef std::vector< PPDKey* > value_type;
122 
123  void insertKey( std::unique_ptr<PPDKey> pKey );
124 public:
126  {
127  const PPDKey* m_pKey1;
129  const PPDKey* m_pKey2;
131 
132  PPDConstraint() : m_pKey1( nullptr ), m_pOption1( nullptr ), m_pKey2( nullptr ), m_pOption2( nullptr ) {}
133  };
134 private:
135  hash_type m_aKeys;
136  value_type m_aOrderedKeys;
137  ::std::vector< PPDConstraint > m_aConstraints;
138 
139  // the full path of the PPD file
140  OUString m_aFile;
141  // some basic attributes
145  rtl_TextEncoding m_aFileEncoding;
146 
147 
148  // shortcuts to important keys and their default values
149  // imageable area
151  // paper dimensions
154  // paper trays
156  // resolutions
158 
159  // translations
160  std::unique_ptr<PPDTranslator> m_pTranslator;
161 
162  PPDParser( const OUString& rFile );
163  PPDParser(const OUString& rFile, const std::vector<PPDKey*>& keys);
164 
165  void parseOrderDependency(const OString& rLine);
166  void parseOpenUI(const OString& rLine, const OString& rPPDGroup);
167  void parseConstraint(const OString& rLine);
168  void parse( std::vector< OString >& rLines );
169 
170  OUString handleTranslation(const OString& i_rString, bool i_bIsGlobalized);
171 
172  static void scanPPDDir( const OUString& rDir );
173  static void initPPDFiles(PPDCache &rPPDCache);
174  static OUString getPPDFile( const OUString& rFile );
175 public:
176  ~PPDParser();
177  static const PPDParser* getParser( const OUString& rFile );
178 
179  const PPDKey* getKey( int n ) const;
180  const PPDKey* getKey( const OUString& rKey ) const;
181  int getKeys() const { return m_aKeys.size(); }
182  bool hasKey( const PPDKey* ) const;
183 
184  const ::std::vector< PPDConstraint >& getConstraints() const { return m_aConstraints; }
185 
186  bool isColorDevice() const { return m_bColorDevice; }
187  bool isType42Capable() const { return m_bType42Capable; }
188  sal_uLong getLanguageLevel() const { return m_nLanguageLevel; }
189 
190  OUString getDefaultPaperDimension() const;
191  void getDefaultPaperDimension( int& rWidth, int& rHeight ) const
192  { getPaperDimension( getDefaultPaperDimension(), rWidth, rHeight ); }
193  bool getPaperDimension( const OUString& rPaperName,
194  int& rWidth, int& rHeight ) const;
195  // width and height in pt
196  // returns false if paper not found
197 
198  // match the best paper for width and height
199  OUString matchPaper( int nWidth, int nHeight ) const;
200 
201  bool getMargins( const OUString& rPaperName,
202  int &rLeft, int& rRight,
203  int &rUpper, int& rLower ) const;
204  // values in pt
205  // returns true if paper found
206 
207  // values int pt
208 
209  OUString getDefaultInputSlot() const;
210 
211  void getDefaultResolution( int& rXRes, int& rYRes ) const;
212  // values in dpi
213  static void getResolutionFromString( const OUString&, int&, int& );
214  // helper function
215 
216  OUString translateKey( const OUString& i_rKey ) const;
217  OUString translateOption( const OUString& i_rKey,
218  const OUString& i_rOption ) const;
219 };
220 
221 
222 /*
223  * PPDContext - a class to manage user definable states based on the
224  * contents of a PPDParser.
225  */
226 
228 {
229  typedef std::unordered_map< const PPDKey*, const PPDValue*, PPDKeyhash > hash_type;
230  hash_type m_aCurrentValues;
232 
233  // returns false: check failed, new value is constrained
234  // true: check succeeded, new value can be set
235  bool checkConstraints( const PPDKey*, const PPDValue*, bool bDoReset );
236  bool resetValue( const PPDKey*, bool bDefaultable = false );
237 public:
238  PPDContext();
239  PPDContext( const PPDContext& rContext ) { operator=( rContext ); }
240  PPDContext& operator=( const PPDContext& rContext ) = default;
241  PPDContext& operator=( PPDContext&& rContext );
242 
243  void setParser( const PPDParser* );
244  const PPDParser* getParser() const { return m_pParser; }
245 
246  const PPDValue* getValue( const PPDKey* ) const;
247  const PPDValue* setValue( const PPDKey*, const PPDValue*, bool bDontCareForConstraints = false );
248 
249  int countValuesModified() const { return m_aCurrentValues.size(); }
250  const PPDKey* getModifiedKey( int n ) const;
251 
252  // public wrapper for the private method
253  bool checkConstraints( const PPDKey*, const PPDValue* );
254 
255  // for printer setup
256  char* getStreamableBuffer( sal_uLong& rBytes ) const;
257  void rebuildFromStreamBuffer(const std::vector<char> &rBuffer);
258 
259  // convenience
260  int getRenderResolution() const;
261 
262  // width, height in points, paper will contain the name of the selected
263  // paper after the call
264  void getPageSize( OUString& rPaper, int& rWidth, int& rHeight ) const;
265 };
266 
267 } // namespace
268 
269 #endif // INCLUDED_VCL_PPDPARSER_HXX
270 
271 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
RegError REGISTRY_CALLTYPE setValue(RegKeyHandle hKey, rtl_uString *keyName, RegValueType valueType, RegValue pData, sal_uInt32 valueSize)
OUString m_aFile
Definition: ppdparser.hxx:140
const PPDParser * m_pParser
Definition: ppdparser.hxx:231
OUString m_aOption
Definition: ppdparser.hxx:48
std::vector< PPDValue * > value_type
Definition: ppdparser.hxx:63
bool isColorDevice() const
Definition: ppdparser.hxx:186
const PPDValue * m_pDefaultResolution
Definition: ppdparser.hxx:157
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
const OUString & getKey() const
Definition: ppdparser.hxx:95
sal_uIntPtr sal_uLong
const PPDValue * m_pDefaultValue
Definition: ppdparser.hxx:68
hash_type m_aValues
Definition: ppdparser.hxx:66
hash_type m_aKeys
Definition: ppdparser.hxx:135
void setParser(::connectivity::OSQLParser *)
int getKeys() const
Definition: ppdparser.hxx:181
css::beans::Optional< css::uno::Any > getValue(OUString const &id)
::std::vector< PPDConstraint > m_aConstraints
Definition: ppdparser.hxx:137
size_t operator()(const PPDKey *pKey) const
Definition: ppdparser.hxx:104
const PPDKey * m_pImageableAreas
Definition: ppdparser.hxx:150
void getDefaultPaperDimension(int &rWidth, int &rHeight) const
Definition: ppdparser.hxx:191
const PPDValue * m_pDefaultInputSlot
Definition: ppdparser.hxx:155
std::unordered_map< const PPDKey *, const PPDValue *, PPDKeyhash > hash_type
Definition: ppdparser.hxx:229
int countValuesModified() const
Definition: ppdparser.hxx:249
const OUString & getGroup() const
Definition: ppdparser.hxx:93
const PPDValue * m_pOption2
Definition: ppdparser.hxx:130
const PPDValue * m_pDefaultPaperDimension
Definition: ppdparser.hxx:152
PPDValueType m_eType
Definition: ppdparser.hxx:42
std::vector< PPDKey * > value_type
Definition: ppdparser.hxx:121
OUString const m_aKey
Definition: ppdparser.hxx:65
const PPDValue * getDefaultValue() const
Definition: ppdparser.hxx:92
std::unique_ptr< PPDTranslator > m_pTranslator
Definition: ppdparser.hxx:160
int m_nOrderDependency
Definition: ppdparser.hxx:77
const ::std::vector< PPDConstraint > & getConstraints() const
Definition: ppdparser.hxx:184
SetupType m_eSetupType
Definition: ppdparser.hxx:78
OUString m_aGroup
Definition: ppdparser.hxx:70
bool m_bType42Capable
Definition: ppdparser.hxx:143
OUString m_aCustomOption
Definition: ppdparser.hxx:47
value_type m_aOrderedKeys
Definition: ppdparser.hxx:136
PPDValueType
Definition: ppdparser.hxx:38
const PPDValue * m_pOption1
Definition: ppdparser.hxx:128
PPDContext(const PPDContext &rContext)
Definition: ppdparser.hxx:239
hash_type m_aCurrentValues
Definition: ppdparser.hxx:230
int getOrderDependency() const
Definition: ppdparser.hxx:98
bool m_bCustomOption
Definition: ppdparser.hxx:46
const PPDKey * m_pPaperDimensions
Definition: ppdparser.hxx:153
rtl_TextEncoding m_aFileEncoding
Definition: ppdparser.hxx:145
sal_uLong getLanguageLevel() const
Definition: ppdparser.hxx:188
bool isUIKey() const
Definition: ppdparser.hxx:96
int countValues() const
Definition: ppdparser.hxx:86
std::unordered_map< OUString, PPDValue > hash_type
Definition: ppdparser.hxx:62
OUString m_aValue
Definition: ppdparser.hxx:49
std::unordered_map< OUString, std::unique_ptr< PPDKey > > hash_type
Definition: ppdparser.hxx:120
const PPDParser * getParser() const
Definition: ppdparser.hxx:244
bool m_bUIOption
Definition: ppdparser.hxx:76
value_type m_aOrderedValues
Definition: ppdparser.hxx:67
SetupType getSetupType() const
Definition: ppdparser.hxx:97
bool parse(OUString const &uri, SourceProviderScannerData *data)
sal_uLong m_nLanguageLevel
Definition: ppdparser.hxx:144
bool m_bQueryValue
Definition: ppdparser.hxx:69
bool isType42Capable() const
Definition: ppdparser.hxx:187