1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #ifndef __FRAMEWORK_SERVICES_SUBSTPATHVARS_HXX_ 29*cdf0e10cSrcweir #define __FRAMEWORK_SERVICES_SUBSTPATHVARS_HXX_ 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir /** Attention: stl headers must(!) be included at first. Otherwhise it can make trouble 32*cdf0e10cSrcweir with solaris headers ... 33*cdf0e10cSrcweir */ 34*cdf0e10cSrcweir #include <vector> 35*cdf0e10cSrcweir #include <list> 36*cdf0e10cSrcweir #include <hash_map> 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 39*cdf0e10cSrcweir // my own includes 40*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 41*cdf0e10cSrcweir #include <threadhelp/threadhelpbase.hxx> 42*cdf0e10cSrcweir #include <macros/generic.hxx> 43*cdf0e10cSrcweir #include <macros/xinterface.hxx> 44*cdf0e10cSrcweir #include <macros/xtypeprovider.hxx> 45*cdf0e10cSrcweir #include <macros/xserviceinfo.hxx> 46*cdf0e10cSrcweir #include <stdtypes.h> 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 49*cdf0e10cSrcweir // interface includes 50*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 51*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp> 52*cdf0e10cSrcweir #include <com/sun/star/lang/XTypeProvider.hpp> 53*cdf0e10cSrcweir #include <com/sun/star/container/NoSuchElementException.hpp> 54*cdf0e10cSrcweir #include <com/sun/star/util/XStringSubstitution.hpp> 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 57*cdf0e10cSrcweir // other includes 58*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 59*cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx> 60*cdf0e10cSrcweir #include <rtl/ustring.hxx> 61*cdf0e10cSrcweir #include <unotools/configitem.hxx> 62*cdf0e10cSrcweir #include <tools/link.hxx> 63*cdf0e10cSrcweir #include <i18npool/lang.h> 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir namespace framework 66*cdf0e10cSrcweir { 67*cdf0e10cSrcweir 68*cdf0e10cSrcweir // Must be zero value based 69*cdf0e10cSrcweir enum EnvironmentType 70*cdf0e10cSrcweir { 71*cdf0e10cSrcweir ET_HOST = 0 , 72*cdf0e10cSrcweir ET_YPDOMAIN , 73*cdf0e10cSrcweir ET_DNSDOMAIN , 74*cdf0e10cSrcweir ET_NTDOMAIN , 75*cdf0e10cSrcweir ET_OS , 76*cdf0e10cSrcweir ET_UNKNOWN , 77*cdf0e10cSrcweir ET_COUNT 78*cdf0e10cSrcweir }; 79*cdf0e10cSrcweir 80*cdf0e10cSrcweir // Must be zero value based 81*cdf0e10cSrcweir enum OperatingSystem 82*cdf0e10cSrcweir { 83*cdf0e10cSrcweir OS_WINDOWS = 0, 84*cdf0e10cSrcweir OS_UNIX , 85*cdf0e10cSrcweir OS_SOLARIS , 86*cdf0e10cSrcweir OS_LINUX , 87*cdf0e10cSrcweir OS_UNKNOWN , 88*cdf0e10cSrcweir OS_COUNT 89*cdf0e10cSrcweir }; 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir struct SubstituteRule 92*cdf0e10cSrcweir { 93*cdf0e10cSrcweir SubstituteRule() {} 94*cdf0e10cSrcweir SubstituteRule( const rtl::OUString& aVarName, 95*cdf0e10cSrcweir const rtl::OUString& aValue, 96*cdf0e10cSrcweir const com::sun::star::uno::Any& aVal, 97*cdf0e10cSrcweir EnvironmentType aType ) : 98*cdf0e10cSrcweir aSubstVariable( aVarName ), aSubstValue( aValue ), aEnvValue( aVal ), aEnvType( aType ) {} 99*cdf0e10cSrcweir 100*cdf0e10cSrcweir rtl::OUString aSubstVariable; 101*cdf0e10cSrcweir rtl::OUString aSubstValue; 102*cdf0e10cSrcweir com::sun::star::uno::Any aEnvValue; 103*cdf0e10cSrcweir EnvironmentType aEnvType; 104*cdf0e10cSrcweir }; 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir struct SubstitutePathNotify 107*cdf0e10cSrcweir { 108*cdf0e10cSrcweir SubstitutePathNotify() {}; 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir const com::sun::star::uno::Sequence<rtl::OUString> aPropertyNames; 111*cdf0e10cSrcweir }; 112*cdf0e10cSrcweir 113*cdf0e10cSrcweir class SubstituteVariables : public ::std::hash_map< ::rtl::OUString, 114*cdf0e10cSrcweir SubstituteRule, 115*cdf0e10cSrcweir OUStringHashCode, 116*cdf0e10cSrcweir ::std::equal_to< ::rtl::OUString > > 117*cdf0e10cSrcweir { 118*cdf0e10cSrcweir public: 119*cdf0e10cSrcweir inline void free() 120*cdf0e10cSrcweir { 121*cdf0e10cSrcweir SubstituteVariables().swap( *this ); 122*cdf0e10cSrcweir } 123*cdf0e10cSrcweir }; 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir typedef std::vector< SubstituteRule > SubstituteRuleVector; 126*cdf0e10cSrcweir class SubstitutePathVariables_Impl : public utl::ConfigItem 127*cdf0e10cSrcweir { 128*cdf0e10cSrcweir public: 129*cdf0e10cSrcweir SubstitutePathVariables_Impl( const Link& aNotifyLink ); 130*cdf0e10cSrcweir virtual ~SubstitutePathVariables_Impl(); 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir static OperatingSystem GetOperatingSystemFromString( const rtl::OUString& ); 133*cdf0e10cSrcweir static EnvironmentType GetEnvTypeFromString( const rtl::OUString& ); 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir void GetSharePointsRules( SubstituteVariables& aSubstVarMap ); 136*cdf0e10cSrcweir 137*cdf0e10cSrcweir /** is called from the ConfigManager before application ends or from the 138*cdf0e10cSrcweir PropertyChangeListener if the sub tree broadcasts changes. */ 139*cdf0e10cSrcweir virtual void Notify( const com::sun::star::uno::Sequence< rtl::OUString >& aPropertyNames ); 140*cdf0e10cSrcweir virtual void Commit(); 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir private: 143*cdf0e10cSrcweir // Wrapper methods for low-level functions 144*cdf0e10cSrcweir OperatingSystem GetOperatingSystem(); 145*cdf0e10cSrcweir const rtl::OUString& GetYPDomainName(); 146*cdf0e10cSrcweir const rtl::OUString& GetDNSDomainName(); 147*cdf0e10cSrcweir const rtl::OUString& GetNTDomainName(); 148*cdf0e10cSrcweir const rtl::OUString& GetHostName(); 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir bool FilterRuleSet( const SubstituteRuleVector& aRuleSet, SubstituteRule& aActiveRule ); 151*cdf0e10cSrcweir 152*cdf0e10cSrcweir void ReadSharePointsFromConfiguration( com::sun::star::uno::Sequence< rtl::OUString >& aSharePointsSeq ); 153*cdf0e10cSrcweir void ReadSharePointRuleSetFromConfiguration( const rtl::OUString& aSharePointName, 154*cdf0e10cSrcweir const rtl::OUString& aSharePointNodeName, 155*cdf0e10cSrcweir SubstituteRuleVector& aRuleSet ); 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir // Stored values for domains and host 158*cdf0e10cSrcweir bool m_bYPDomainRetrieved; 159*cdf0e10cSrcweir rtl::OUString m_aYPDomain; 160*cdf0e10cSrcweir bool m_bDNSDomainRetrieved; 161*cdf0e10cSrcweir rtl::OUString m_aDNSDomain; 162*cdf0e10cSrcweir bool m_bNTDomainRetrieved; 163*cdf0e10cSrcweir rtl::OUString m_aNTDomain; 164*cdf0e10cSrcweir bool m_bHostRetrieved; 165*cdf0e10cSrcweir rtl::OUString m_aHost; 166*cdf0e10cSrcweir bool m_bOSRetrieved; 167*cdf0e10cSrcweir OperatingSystem m_eOSType; 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir Link m_aListenerNotify; 170*cdf0e10cSrcweir const rtl::OUString m_aSharePointsNodeName; 171*cdf0e10cSrcweir const rtl::OUString m_aDirPropertyName; 172*cdf0e10cSrcweir const rtl::OUString m_aEnvPropertyName; 173*cdf0e10cSrcweir const rtl::OUString m_aLevelSep; 174*cdf0e10cSrcweir }; 175*cdf0e10cSrcweir 176*cdf0e10cSrcweir enum PreDefVariable 177*cdf0e10cSrcweir { 178*cdf0e10cSrcweir PREDEFVAR_INST, 179*cdf0e10cSrcweir PREDEFVAR_PROG, 180*cdf0e10cSrcweir PREDEFVAR_USER, 181*cdf0e10cSrcweir PREDEFVAR_WORK, 182*cdf0e10cSrcweir PREDEFVAR_HOME, 183*cdf0e10cSrcweir PREDEFVAR_TEMP, 184*cdf0e10cSrcweir PREDEFVAR_PATH, 185*cdf0e10cSrcweir PREDEFVAR_LANG, 186*cdf0e10cSrcweir PREDEFVAR_LANGID, 187*cdf0e10cSrcweir PREDEFVAR_VLANG, 188*cdf0e10cSrcweir PREDEFVAR_INSTPATH, 189*cdf0e10cSrcweir PREDEFVAR_PROGPATH, 190*cdf0e10cSrcweir PREDEFVAR_USERPATH, 191*cdf0e10cSrcweir PREDEFVAR_INSTURL, 192*cdf0e10cSrcweir PREDEFVAR_PROGURL, 193*cdf0e10cSrcweir PREDEFVAR_USERURL, 194*cdf0e10cSrcweir PREDEFVAR_WORKDIRURL, 195*cdf0e10cSrcweir // --> PB 2004-10-27 #i32656# - new variable of hierachy service 196*cdf0e10cSrcweir PREDEFVAR_BASEINSTURL, 197*cdf0e10cSrcweir PREDEFVAR_USERDATAURL, 198*cdf0e10cSrcweir // <-- 199*cdf0e10cSrcweir PREDEFVAR_BRANDBASEURL, 200*cdf0e10cSrcweir PREDEFVAR_COUNT 201*cdf0e10cSrcweir }; 202*cdf0e10cSrcweir 203*cdf0e10cSrcweir struct PredefinedPathVariables 204*cdf0e10cSrcweir { 205*cdf0e10cSrcweir // Predefined variables supported by substitute variables 206*cdf0e10cSrcweir LanguageType m_eLanguageType; // Lanuage type of Office 207*cdf0e10cSrcweir rtl::OUString m_FixedVar[ PREDEFVAR_COUNT ]; // Variable value access by PreDefVariable 208*cdf0e10cSrcweir rtl::OUString m_FixedVarNames[ PREDEFVAR_COUNT ]; // Variable name access by PreDefVariable 209*cdf0e10cSrcweir }; 210*cdf0e10cSrcweir 211*cdf0e10cSrcweir struct ReSubstFixedVarOrder 212*cdf0e10cSrcweir { 213*cdf0e10cSrcweir sal_Int32 nVarValueLength; 214*cdf0e10cSrcweir PreDefVariable eVariable; 215*cdf0e10cSrcweir 216*cdf0e10cSrcweir bool operator< ( const ReSubstFixedVarOrder& aFixedVarOrder ) const 217*cdf0e10cSrcweir { 218*cdf0e10cSrcweir // Reverse operator< to have high to low ordering 219*cdf0e10cSrcweir return ( nVarValueLength > aFixedVarOrder.nVarValueLength ); 220*cdf0e10cSrcweir } 221*cdf0e10cSrcweir }; 222*cdf0e10cSrcweir 223*cdf0e10cSrcweir struct ReSubstUserVarOrder 224*cdf0e10cSrcweir { 225*cdf0e10cSrcweir sal_Int32 nVarValueLength; 226*cdf0e10cSrcweir rtl::OUString aVarName; 227*cdf0e10cSrcweir 228*cdf0e10cSrcweir bool operator< ( const ReSubstUserVarOrder& aUserVarOrder ) const 229*cdf0e10cSrcweir { 230*cdf0e10cSrcweir // Reverse operator< to have high to low ordering 231*cdf0e10cSrcweir return ( nVarValueLength > aUserVarOrder.nVarValueLength ); 232*cdf0e10cSrcweir } 233*cdf0e10cSrcweir }; 234*cdf0e10cSrcweir 235*cdf0e10cSrcweir typedef std::list< ReSubstFixedVarOrder > ReSubstFixedVarOrderVector; 236*cdf0e10cSrcweir typedef std::list< ReSubstUserVarOrder > ReSubstUserVarOrderVector; 237*cdf0e10cSrcweir 238*cdf0e10cSrcweir class SubstitutePathVariables : private ThreadHelpBase, // Struct for right initalization of mutex member! Must be first of baseclasses. 239*cdf0e10cSrcweir public ::cppu::WeakImplHelper2< ::com::sun::star::util::XStringSubstitution, css::lang::XServiceInfo > 240*cdf0e10cSrcweir { 241*cdf0e10cSrcweir friend class SubstitutePathVariables_Impl; 242*cdf0e10cSrcweir 243*cdf0e10cSrcweir public: 244*cdf0e10cSrcweir SubstitutePathVariables( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceManager ); 245*cdf0e10cSrcweir virtual ~SubstitutePathVariables(); 246*cdf0e10cSrcweir 247*cdf0e10cSrcweir // XInterface, XTypeProvider, XServiceInfo 248*cdf0e10cSrcweir DECLARE_XSERVICEINFO 249*cdf0e10cSrcweir 250*cdf0e10cSrcweir // XStringSubstitution 251*cdf0e10cSrcweir virtual rtl::OUString SAL_CALL substituteVariables( const ::rtl::OUString& aText, sal_Bool bSubstRequired ) 252*cdf0e10cSrcweir throw (::com::sun::star::container::NoSuchElementException, ::com::sun::star::uno::RuntimeException); 253*cdf0e10cSrcweir virtual rtl::OUString SAL_CALL reSubstituteVariables( const ::rtl::OUString& aText ) 254*cdf0e10cSrcweir throw (::com::sun::star::uno::RuntimeException); 255*cdf0e10cSrcweir virtual ::rtl::OUString SAL_CALL getSubstituteVariableValue( const ::rtl::OUString& variable ) 256*cdf0e10cSrcweir throw (::com::sun::star::container::NoSuchElementException, ::com::sun::star::uno::RuntimeException); 257*cdf0e10cSrcweir 258*cdf0e10cSrcweir protected: 259*cdf0e10cSrcweir DECL_LINK( implts_ConfigurationNotify, SubstitutePathNotify* ); 260*cdf0e10cSrcweir 261*cdf0e10cSrcweir void SetPredefinedPathVariables( PredefinedPathVariables& ); 262*cdf0e10cSrcweir rtl::OUString ConvertOSLtoUCBURL( const rtl::OUString& aOSLCompliantURL ) const; 263*cdf0e10cSrcweir 264*cdf0e10cSrcweir // Special case (transient) values can change during runtime! 265*cdf0e10cSrcweir // Don't store them in the pre defined struct 266*cdf0e10cSrcweir rtl::OUString GetWorkPath() const; 267*cdf0e10cSrcweir rtl::OUString GetWorkVariableValue() const; 268*cdf0e10cSrcweir rtl::OUString GetPathVariableValue() const; 269*cdf0e10cSrcweir 270*cdf0e10cSrcweir rtl::OUString GetHomeVariableValue() const; 271*cdf0e10cSrcweir 272*cdf0e10cSrcweir // XStringSubstitution implementation methods 273*cdf0e10cSrcweir rtl::OUString impl_substituteVariable( const ::rtl::OUString& aText, bool bSustRequired ) 274*cdf0e10cSrcweir throw (::com::sun::star::container::NoSuchElementException, ::com::sun::star::uno::RuntimeException); 275*cdf0e10cSrcweir rtl::OUString impl_reSubstituteVariables( const ::rtl::OUString& aText ) 276*cdf0e10cSrcweir throw (::com::sun::star::uno::RuntimeException); 277*cdf0e10cSrcweir ::rtl::OUString impl_getSubstituteVariableValue( const ::rtl::OUString& variable ) 278*cdf0e10cSrcweir throw (::com::sun::star::container::NoSuchElementException, ::com::sun::star::uno::RuntimeException); 279*cdf0e10cSrcweir 280*cdf0e10cSrcweir private: 281*cdf0e10cSrcweir class VarNameToIndexMap : public std::hash_map< ::rtl::OUString, 282*cdf0e10cSrcweir PreDefVariable, 283*cdf0e10cSrcweir OUStringHashCode, 284*cdf0e10cSrcweir ::std::equal_to< ::rtl::OUString > > 285*cdf0e10cSrcweir { 286*cdf0e10cSrcweir inline void free() 287*cdf0e10cSrcweir { 288*cdf0e10cSrcweir VarNameToIndexMap().swap( *this ); 289*cdf0e10cSrcweir } 290*cdf0e10cSrcweir }; 291*cdf0e10cSrcweir 292*cdf0e10cSrcweir // heavy used string 293*cdf0e10cSrcweir const rtl::OUString m_aVarStart; 294*cdf0e10cSrcweir const rtl::OUString m_aVarEnd; 295*cdf0e10cSrcweir 296*cdf0e10cSrcweir VarNameToIndexMap m_aPreDefVarMap; // Mapping from pre-def variable names to enum for array access 297*cdf0e10cSrcweir SubstituteVariables m_aSubstVarMap; // Active rule set map indexed by variable name! 298*cdf0e10cSrcweir PredefinedPathVariables m_aPreDefVars; // All predefined variables 299*cdf0e10cSrcweir SubstitutePathVariables_Impl m_aImpl; // Implementation class that access the configuration 300*cdf0e10cSrcweir ReSubstFixedVarOrderVector m_aReSubstFixedVarOrder; // To speed up resubstitution fixed variables (order for lookup) 301*cdf0e10cSrcweir ReSubstUserVarOrderVector m_aReSubstUserVarOrder; // To speed up resubstitution user variables 302*cdf0e10cSrcweir com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory > m_xServiceManager; 303*cdf0e10cSrcweir }; 304*cdf0e10cSrcweir 305*cdf0e10cSrcweir } 306*cdf0e10cSrcweir 307*cdf0e10cSrcweir #endif // __FRAMEWORK_SERVICES_SUBSTPATHVARS_HXX_ 308