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