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