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