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