1*0d63794cSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*0d63794cSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*0d63794cSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*0d63794cSAndrew Rist * distributed with this work for additional information 6*0d63794cSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*0d63794cSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*0d63794cSAndrew Rist * "License"); you may not use this file except in compliance 9*0d63794cSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*0d63794cSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*0d63794cSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*0d63794cSAndrew Rist * software distributed under the License is distributed on an 15*0d63794cSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*0d63794cSAndrew Rist * KIND, either express or implied. See the License for the 17*0d63794cSAndrew Rist * specific language governing permissions and limitations 18*0d63794cSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*0d63794cSAndrew Rist *************************************************************/ 21*0d63794cSAndrew Rist 22*0d63794cSAndrew Rist 23cdf0e10cSrcweir #ifndef _PSPRINT_PPDPARSER_HXX_ 24cdf0e10cSrcweir #define _PSPRINT_PPDPARSER_HXX_ 25cdf0e10cSrcweir 26cdf0e10cSrcweir #include <list> 27cdf0e10cSrcweir #include <vector> 28cdf0e10cSrcweir #include <hash_map> 29cdf0e10cSrcweir 30cdf0e10cSrcweir #include "tools/string.hxx" 31cdf0e10cSrcweir #include "tools/stream.hxx" 32cdf0e10cSrcweir 33cdf0e10cSrcweir #include "vcl/dllapi.h" 34cdf0e10cSrcweir 35cdf0e10cSrcweir #include "com/sun/star/lang/Locale.hpp" 36cdf0e10cSrcweir 37cdf0e10cSrcweir #define PRINTER_PPDDIR "driver" 38cdf0e10cSrcweir 39cdf0e10cSrcweir namespace psp { 40cdf0e10cSrcweir 41cdf0e10cSrcweir class PPDParser; 42cdf0e10cSrcweir class PPDTranslator; 43cdf0e10cSrcweir 44cdf0e10cSrcweir enum PPDValueType { eInvocation, eQuoted, eSymbol, eString, eNo }; 45cdf0e10cSrcweir 46cdf0e10cSrcweir struct VCL_DLLPUBLIC PPDValue 47cdf0e10cSrcweir { 48cdf0e10cSrcweir PPDValueType m_eType; 49cdf0e10cSrcweir String m_aOption; 50cdf0e10cSrcweir String m_aValue; 51cdf0e10cSrcweir }; 52cdf0e10cSrcweir 53cdf0e10cSrcweir // ---------------------------------------------------------------------- 54cdf0e10cSrcweir 55cdf0e10cSrcweir /* 56cdf0e10cSrcweir * PPDKey - a container for the available options (=values) of a PPD keyword 57cdf0e10cSrcweir */ 58cdf0e10cSrcweir 59cdf0e10cSrcweir class VCL_DLLPUBLIC PPDKey 60cdf0e10cSrcweir { 61cdf0e10cSrcweir friend class PPDParser; 62cdf0e10cSrcweir 63cdf0e10cSrcweir typedef ::std::hash_map< ::rtl::OUString, PPDValue, ::rtl::OUStringHash > hash_type; 64cdf0e10cSrcweir typedef ::std::vector< PPDValue* > value_type; 65cdf0e10cSrcweir 66cdf0e10cSrcweir String m_aKey; 67cdf0e10cSrcweir hash_type m_aValues; 68cdf0e10cSrcweir value_type m_aOrderedValues; 69cdf0e10cSrcweir const PPDValue* m_pDefaultValue; 70cdf0e10cSrcweir bool m_bQueryValue; 71cdf0e10cSrcweir PPDValue m_aQueryValue; 72cdf0e10cSrcweir 73cdf0e10cSrcweir public: 74cdf0e10cSrcweir enum UIType { PickOne, PickMany, Boolean }; 75cdf0e10cSrcweir enum SetupType { ExitServer, Prolog, DocumentSetup, PageSetup, JCLSetup, AnySetup }; 76cdf0e10cSrcweir private: 77cdf0e10cSrcweir 78cdf0e10cSrcweir bool m_bUIOption; 79cdf0e10cSrcweir UIType m_eUIType; 80cdf0e10cSrcweir int m_nOrderDependency; 81cdf0e10cSrcweir SetupType m_eSetupType; 82cdf0e10cSrcweir 83cdf0e10cSrcweir void eraseValue( const String& rOption ); 84cdf0e10cSrcweir public: 85cdf0e10cSrcweir PPDKey( const String& rKey ); 86cdf0e10cSrcweir ~PPDKey(); 87cdf0e10cSrcweir 88cdf0e10cSrcweir PPDValue* insertValue( const String& rOption ); countValues() const89cdf0e10cSrcweir int countValues() const 90cdf0e10cSrcweir { return m_aValues.size(); } 91cdf0e10cSrcweir // neither getValue will return the query option 92cdf0e10cSrcweir const PPDValue* getValue( int n ) const; 93cdf0e10cSrcweir const PPDValue* getValue( const String& rOption ) const; 94cdf0e10cSrcweir const PPDValue* getValueCaseInsensitive( const String& rOption ) const; getDefaultValue() const95cdf0e10cSrcweir const PPDValue* getDefaultValue() const { return m_pDefaultValue; } getQueryValue() const96cdf0e10cSrcweir const PPDValue* getQueryValue() const { return m_bQueryValue ? &m_aQueryValue : NULL; } 97cdf0e10cSrcweir getKey() const98cdf0e10cSrcweir const String& getKey() const { return m_aKey; } isUIKey() const99cdf0e10cSrcweir bool isUIKey() const { return m_bUIOption; } getUIType() const100cdf0e10cSrcweir UIType getUIType() const { return m_eUIType; } getSetupType() const101cdf0e10cSrcweir SetupType getSetupType() const { return m_eSetupType; } getOrderDependency() const102cdf0e10cSrcweir int getOrderDependency() const { return m_nOrderDependency; } 103cdf0e10cSrcweir }; 104cdf0e10cSrcweir 105cdf0e10cSrcweir // define a hash for PPDKey 106cdf0e10cSrcweir struct PPDKeyhash 107cdf0e10cSrcweir { operator ()psp::PPDKeyhash108cdf0e10cSrcweir size_t operator()( const PPDKey * pKey) const 109cdf0e10cSrcweir { return (size_t)pKey; } 110cdf0e10cSrcweir }; 111cdf0e10cSrcweir 112cdf0e10cSrcweir // ---------------------------------------------------------------------- 113cdf0e10cSrcweir 114cdf0e10cSrcweir /* 115cdf0e10cSrcweir * PPDParser - parses a PPD file and contains all available keys from it 116cdf0e10cSrcweir */ 117cdf0e10cSrcweir 118cdf0e10cSrcweir class PPDContext; 119cdf0e10cSrcweir class CUPSManager; 120cdf0e10cSrcweir 121cdf0e10cSrcweir class VCL_DLLPUBLIC PPDParser 122cdf0e10cSrcweir { 123cdf0e10cSrcweir friend class PPDContext; 124cdf0e10cSrcweir friend class CUPSManager; 125cdf0e10cSrcweir 126cdf0e10cSrcweir typedef ::std::hash_map< ::rtl::OUString, PPDKey*, ::rtl::OUStringHash > hash_type; 127cdf0e10cSrcweir typedef ::std::vector< PPDKey* > value_type; 128cdf0e10cSrcweir 129cdf0e10cSrcweir void insertKey( const String& rKey, PPDKey* pKey ); 130cdf0e10cSrcweir public: 131cdf0e10cSrcweir struct PPDConstraint 132cdf0e10cSrcweir { 133cdf0e10cSrcweir const PPDKey* m_pKey1; 134cdf0e10cSrcweir const PPDValue* m_pOption1; 135cdf0e10cSrcweir const PPDKey* m_pKey2; 136cdf0e10cSrcweir const PPDValue* m_pOption2; 137cdf0e10cSrcweir PPDConstraintpsp::PPDParser::PPDConstraint138cdf0e10cSrcweir PPDConstraint() : m_pKey1( NULL ), m_pOption1( NULL ), m_pKey2( NULL ), m_pOption2( NULL ) {} 139cdf0e10cSrcweir }; 140cdf0e10cSrcweir private: 141cdf0e10cSrcweir 142cdf0e10cSrcweir static ::std::list< PPDParser* > aAllParsers; 143cdf0e10cSrcweir static ::std::hash_map< rtl::OUString, rtl::OUString, rtl::OUStringHash >* 144cdf0e10cSrcweir pAllPPDFiles; 145cdf0e10cSrcweir 146cdf0e10cSrcweir hash_type m_aKeys; 147cdf0e10cSrcweir value_type m_aOrderedKeys; 148cdf0e10cSrcweir ::std::list< PPDConstraint > m_aConstraints; 149cdf0e10cSrcweir 150cdf0e10cSrcweir // some identifying fields 151cdf0e10cSrcweir String m_aPrinterName; 152cdf0e10cSrcweir String m_aNickName; 153cdf0e10cSrcweir // the full path of the PPD file 154cdf0e10cSrcweir String m_aFile; 155cdf0e10cSrcweir // some basic attributes 156cdf0e10cSrcweir bool m_bColorDevice; 157cdf0e10cSrcweir bool m_bType42Capable; 158cdf0e10cSrcweir sal_uLong m_nLanguageLevel; 159cdf0e10cSrcweir rtl_TextEncoding m_aFileEncoding; 160cdf0e10cSrcweir 161cdf0e10cSrcweir 162cdf0e10cSrcweir // shortcuts to important keys and their default values 163cdf0e10cSrcweir // imageable area 164cdf0e10cSrcweir const PPDValue* m_pDefaultImageableArea; 165cdf0e10cSrcweir const PPDKey* m_pImageableAreas; 166cdf0e10cSrcweir // paper dimensions 167cdf0e10cSrcweir const PPDValue* m_pDefaultPaperDimension; 168cdf0e10cSrcweir const PPDKey* m_pPaperDimensions; 169cdf0e10cSrcweir // paper trays 170cdf0e10cSrcweir const PPDValue* m_pDefaultInputSlot; 171cdf0e10cSrcweir const PPDKey* m_pInputSlots; 172cdf0e10cSrcweir // resolutions 173cdf0e10cSrcweir const PPDValue* m_pDefaultResolution; 174cdf0e10cSrcweir const PPDKey* m_pResolutions; 175cdf0e10cSrcweir // duplex commands 176cdf0e10cSrcweir const PPDValue* m_pDefaultDuplexType; 177cdf0e10cSrcweir const PPDKey* m_pDuplexTypes; 178cdf0e10cSrcweir 179cdf0e10cSrcweir // fonts 180cdf0e10cSrcweir const PPDKey* m_pFontList; 181cdf0e10cSrcweir 182cdf0e10cSrcweir // translations 183cdf0e10cSrcweir PPDTranslator* m_pTranslator; 184cdf0e10cSrcweir 185cdf0e10cSrcweir PPDParser( const String& rFile ); 186cdf0e10cSrcweir ~PPDParser(); 187cdf0e10cSrcweir 188cdf0e10cSrcweir void parseOrderDependency( const ByteString& rLine ); 189cdf0e10cSrcweir void parseOpenUI( const ByteString& rLine ); 190cdf0e10cSrcweir void parseConstraint( const ByteString& rLine ); 191cdf0e10cSrcweir void parse( std::list< ByteString >& rLines ); 192cdf0e10cSrcweir 193cdf0e10cSrcweir String handleTranslation( const ByteString& i_rString, bool i_bIsGlobalized ); 194cdf0e10cSrcweir 195cdf0e10cSrcweir static void scanPPDDir( const String& rDir ); 196cdf0e10cSrcweir static void initPPDFiles(); 197cdf0e10cSrcweir static String getPPDFile( const String& rFile ); 198cdf0e10cSrcweir public: 199cdf0e10cSrcweir static const PPDParser* getParser( const String& rFile ); 200cdf0e10cSrcweir static String getPPDPrinterName( const String& rFile ); 201cdf0e10cSrcweir static void freeAll(); 202cdf0e10cSrcweir static void getKnownPPDDrivers( std::list< rtl::OUString >& o_rDrivers, bool bRefresh = false ); 203cdf0e10cSrcweir getFilename() const204cdf0e10cSrcweir const String& getFilename() const { return m_aFile; } 205cdf0e10cSrcweir 206cdf0e10cSrcweir const PPDKey* getKey( int n ) const; 207cdf0e10cSrcweir const PPDKey* getKey( const String& rKey ) const; getKeys() const208cdf0e10cSrcweir int getKeys() const { return m_aKeys.size(); } 209cdf0e10cSrcweir bool hasKey( const PPDKey* ) const; 210cdf0e10cSrcweir getConstraints() const211cdf0e10cSrcweir const ::std::list< PPDConstraint >& getConstraints() const { return m_aConstraints; } 212cdf0e10cSrcweir getPrinterName() const213cdf0e10cSrcweir const String& getPrinterName() const 214cdf0e10cSrcweir { return m_aPrinterName.Len() ? m_aPrinterName : m_aNickName; } getNickName() const215cdf0e10cSrcweir const String& getNickName() const 216cdf0e10cSrcweir { return m_aNickName.Len() ? m_aNickName : m_aPrinterName; } 217cdf0e10cSrcweir isColorDevice() const218cdf0e10cSrcweir bool isColorDevice() const { return m_bColorDevice; } isType42Capable() const219cdf0e10cSrcweir bool isType42Capable() const { return m_bType42Capable; } getLanguageLevel() const220cdf0e10cSrcweir sal_uLong getLanguageLevel() const { return m_nLanguageLevel; } 221cdf0e10cSrcweir 222cdf0e10cSrcweir String getDefaultPaperDimension() const; getDefaultPaperDimension(int & rWidth,int & rHeight) const223cdf0e10cSrcweir void getDefaultPaperDimension( int& rWidth, int& rHeight ) const 224cdf0e10cSrcweir { getPaperDimension( getDefaultPaperDimension(), rWidth, rHeight ); } 225cdf0e10cSrcweir bool getPaperDimension( const String& rPaperName, 226cdf0e10cSrcweir int& rWidth, int& rHeight ) const; 227cdf0e10cSrcweir // width and height in pt 228cdf0e10cSrcweir // returns false if paper not found getPaperDimensions() const229cdf0e10cSrcweir int getPaperDimensions() const 230cdf0e10cSrcweir { return m_pPaperDimensions ? m_pPaperDimensions->countValues() : 0; } 231cdf0e10cSrcweir String getPaperDimension( int ) const; 232cdf0e10cSrcweir String getPaperDimensionCommand( int ) const; 233cdf0e10cSrcweir String getPaperDimensionCommand( const String & ) const; 234cdf0e10cSrcweir 235cdf0e10cSrcweir // match the best paper for width and height 236cdf0e10cSrcweir String matchPaper( int nWidth, int nHeight ) const; 237cdf0e10cSrcweir 238cdf0e10cSrcweir bool getMargins( const String& rPaperName, 239cdf0e10cSrcweir int &rLeft, int& rRight, 240cdf0e10cSrcweir int &rUpper, int& rLower ) const; 241cdf0e10cSrcweir // values in pt 242cdf0e10cSrcweir // returns true if paper found 243cdf0e10cSrcweir 244cdf0e10cSrcweir // values int pt 245cdf0e10cSrcweir 246cdf0e10cSrcweir String getDefaultInputSlot() const; getInputSlots() const247cdf0e10cSrcweir int getInputSlots() const 248cdf0e10cSrcweir { return m_pInputSlots ? m_pInputSlots->countValues() : 0; } 249cdf0e10cSrcweir String getSlot( int ) const; 250cdf0e10cSrcweir String getSlotCommand( int ) const; 251cdf0e10cSrcweir String getSlotCommand( const String& ) const; 252cdf0e10cSrcweir 253cdf0e10cSrcweir void getDefaultResolution( int& rXRes, int& rYRes ) const; 254cdf0e10cSrcweir int getResolutions() const; 255cdf0e10cSrcweir void getResolution( int, int& rXRes, int& rYRes ) const; 256cdf0e10cSrcweir String getResolutionCommand( int nXRes, int nYRes ) const; 257cdf0e10cSrcweir // values in dpi 258cdf0e10cSrcweir void getResolutionFromString( const String&, int&, int& ) const; 259cdf0e10cSrcweir // helper function 260cdf0e10cSrcweir 261cdf0e10cSrcweir String getDefaultDuplexType() const; getDuplexTypes() const262cdf0e10cSrcweir int getDuplexTypes() const 263cdf0e10cSrcweir { return m_pDuplexTypes ? m_pDuplexTypes->countValues() : 0; } 264cdf0e10cSrcweir String getDuplex( int ) const; 265cdf0e10cSrcweir String getDuplexCommand( int ) const; 266cdf0e10cSrcweir String getDuplexCommand( const String& ) const; 267cdf0e10cSrcweir getFonts() const268cdf0e10cSrcweir int getFonts() const 269cdf0e10cSrcweir { return m_pFontList ? m_pFontList->countValues() : 0; } 270cdf0e10cSrcweir void getFontAttributes( int, 271cdf0e10cSrcweir String& rEncoding, 272cdf0e10cSrcweir String& rCharset ) const; 273cdf0e10cSrcweir void getFontAttributes( const String&, 274cdf0e10cSrcweir String& rEncoding, 275cdf0e10cSrcweir String& rCharset ) const; 276cdf0e10cSrcweir String getFont( int ) const; 277cdf0e10cSrcweir 278cdf0e10cSrcweir 279cdf0e10cSrcweir rtl::OUString translateKey( const rtl::OUString& i_rKey, 280cdf0e10cSrcweir const com::sun::star::lang::Locale& i_rLocale = com::sun::star::lang::Locale() ) const; 281cdf0e10cSrcweir rtl::OUString translateOption( const rtl::OUString& i_rKey, 282cdf0e10cSrcweir const rtl::OUString& i_rOption, 283cdf0e10cSrcweir const com::sun::star::lang::Locale& i_rLocale = com::sun::star::lang::Locale() ) const; 284cdf0e10cSrcweir rtl::OUString translateValue( const rtl::OUString& i_rKey, 285cdf0e10cSrcweir const rtl::OUString& i_rOption, 286cdf0e10cSrcweir const rtl::OUString& i_rValue, 287cdf0e10cSrcweir const com::sun::star::lang::Locale& i_rLocale = com::sun::star::lang::Locale() ) const; 288cdf0e10cSrcweir }; 289cdf0e10cSrcweir 290cdf0e10cSrcweir // ---------------------------------------------------------------------- 291cdf0e10cSrcweir 292cdf0e10cSrcweir /* 293cdf0e10cSrcweir * PPDContext - a class to manage user definable states based on the 294cdf0e10cSrcweir * contents of a PPDParser. 295cdf0e10cSrcweir */ 296cdf0e10cSrcweir 297cdf0e10cSrcweir class VCL_DLLPUBLIC PPDContext 298cdf0e10cSrcweir { 299cdf0e10cSrcweir typedef ::std::hash_map< const PPDKey*, const PPDValue*, PPDKeyhash > hash_type; 300cdf0e10cSrcweir hash_type m_aCurrentValues; 301cdf0e10cSrcweir const PPDParser* m_pParser; 302cdf0e10cSrcweir 303cdf0e10cSrcweir // returns false: check failed, new value is constrained 304cdf0e10cSrcweir // true: check succeded, new value can be set 305cdf0e10cSrcweir bool checkConstraints( const PPDKey*, const PPDValue*, bool bDoReset ); 306cdf0e10cSrcweir bool resetValue( const PPDKey*, bool bDefaultable = false ); 307cdf0e10cSrcweir public: 308cdf0e10cSrcweir PPDContext( const PPDParser* pParser = NULL ); PPDContext(const PPDContext & rContext)309cdf0e10cSrcweir PPDContext( const PPDContext& rContext ) { operator=( rContext ); } 310cdf0e10cSrcweir PPDContext& operator=( const PPDContext& rContext ); 311cdf0e10cSrcweir ~PPDContext(); 312cdf0e10cSrcweir 313cdf0e10cSrcweir void setParser( const PPDParser* ); getParser() const314cdf0e10cSrcweir const PPDParser* getParser() const { return m_pParser; } 315cdf0e10cSrcweir 316cdf0e10cSrcweir const PPDValue* getValue( const PPDKey* ) const; 317cdf0e10cSrcweir const PPDValue* setValue( const PPDKey*, const PPDValue*, bool bDontCareForConstraints = false ); 318cdf0e10cSrcweir countValuesModified() const319cdf0e10cSrcweir int countValuesModified() const { return m_aCurrentValues.size(); } 320cdf0e10cSrcweir const PPDKey* getModifiedKey( int n ) const; 321cdf0e10cSrcweir 322cdf0e10cSrcweir // public wrapper for the private method 323cdf0e10cSrcweir bool checkConstraints( const PPDKey*, const PPDValue* ); 324cdf0e10cSrcweir 325cdf0e10cSrcweir void getUnconstrainedValues( const PPDKey*, ::std::list< const PPDValue* >& rValues ); 326cdf0e10cSrcweir 327cdf0e10cSrcweir // for printer setup 328cdf0e10cSrcweir void* getStreamableBuffer( sal_uLong& rBytes ) const; 329cdf0e10cSrcweir void rebuildFromStreamBuffer( void* pBuffer, sal_uLong nBytes ); 330cdf0e10cSrcweir 331cdf0e10cSrcweir // convenience 332cdf0e10cSrcweir int getRenderResolution() const; 333cdf0e10cSrcweir 334cdf0e10cSrcweir // width, height in points, paper will contain the name of the selected 335cdf0e10cSrcweir // paper after the call 336cdf0e10cSrcweir void getPageSize( String& rPaper, int& rWidth, int& rHeight ) const; 337cdf0e10cSrcweir }; 338cdf0e10cSrcweir 339cdf0e10cSrcweir } // namespace 340cdf0e10cSrcweir 341cdf0e10cSrcweir #endif // _PSPRINT_PPDPARSER_HXX_ 342