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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_framework.hxx"
26
27 //_________________________________________________________________________________________________________________
28 // my own includes
29 //_________________________________________________________________________________________________________________
30 #include "services/substitutepathvars.hxx"
31 #include <threadhelp/resetableguard.hxx>
32 #include <helper/networkdomain.hxx>
33 #include "services.h"
34
35 //_________________________________________________________________________________________________________________
36 // interface includes
37 //_________________________________________________________________________________________________________________
38 #include <com/sun/star/beans/XPropertySet.hpp>
39
40 //_________________________________________________________________________________________________________________
41 // includes of other projects
42 //_________________________________________________________________________________________________________________
43 #include <unotools/configitem.hxx>
44 #include <unotools/localfilehelper.hxx>
45 #include <unotools/configmgr.hxx>
46
47 #ifndef _UTL_BOOTSTRAP_HXX_
48 #include <unotools/bootstrap.hxx>
49 #endif
50 #include <osl/mutex.hxx>
51 #include <osl/file.hxx>
52 #include <osl/security.hxx>
53 #include <osl/socket.hxx>
54 #include <vos/process.hxx>
55 #include <i18npool/mslangid.hxx>
56 #include <tools/urlobj.hxx>
57 #include <tools/resmgr.hxx>
58 #include <tools/debug.hxx>
59 #include <tools/wldcrd.hxx>
60 #include <rtl/ustrbuf.hxx>
61 #include <rtl/bootstrap.hxx>
62
63 #include <comphelper/configurationhelper.hxx>
64
65 #include <string.h>
66
67 //_________________________________________________________________________________________________________________
68 // Defines
69 //_________________________________________________________________________________________________________________
70 //
71
72 #define STRPOS_NOTFOUND (sal_Int32)-1
73
74 #define ASCII_STR( val ) rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( val ))
75
76 #define SEARCHPATH_DELIMITER ';'
77
78 // Variable start/end characters
79 #define SIGN_STARTVARIABLE ASCII_STR("$(")
80 #define SIGN_ENDVARIABLE ASCII_STR(")")
81
82 // Length of SUBSTITUTE_... to replace it with real values.
83 #define REPLACELENGTH_INST 7
84 #define REPLACELENGTH_PROG 7
85 #define REPLACELENGTH_USER 7
86 #define REPLACELENGTH_WORK 7
87 #define REPLACELENGTH_HOME 7
88 #define REPLACELENGTH_TEMP 7
89 #define REPLACELENGTH_PATH 7
90 #define REPLACELENGTH_INSTPATH 11
91 #define REPLACELENGTH_PROGPATH 11
92 #define REPLACELENGTH_USERPATH 11
93 #define REPLACELENGTH_INSTURL 10
94 #define REPLACELENGTH_PROGURL 10
95 #define REPLACELENGTH_USERURL 10
96 #define REPLACELENGTH_PATH 7
97 #define REPLACELENGTH_LANG 7
98 #define REPLACELENGTH_LANGID 9
99 #define REPLACELENGTH_VLANG 8
100 #define REPLACELENGTH_WORKDIRURL 13
101 // --> PB 2004-10-27 #i32656# - new variable of hierarchy service
102 #define REPLACELENGTH_BASEINSTURL 14
103 #define REPLACELENGTH_USERDATAURL 14
104 // <--
105
106 // Name of the pre defined path variables
107 #define VARIABLE_INST "$(inst)"
108 #define VARIABLE_PROG "$(prog)"
109 #define VARIABLE_USER "$(user)"
110 #define VARIABLE_WORK "$(work)"
111 #define VARIABLE_HOME "$(home)"
112 #define VARIABLE_TEMP "$(temp)"
113 #define VARIABLE_PATH "$(path)"
114 #define VARIABLE_LANG "$(lang)"
115 #define VARIABLE_LANGID "$(langid)"
116 #define VARIABLE_VLANG "$(vlang)"
117 #define VARIABLE_INSTPATH "$(instpath)"
118 #define VARIABLE_PROGPATH "$(progpath)"
119 #define VARIABLE_USERPATH "$(userpath)"
120 #define VARIABLE_INSTURL "$(insturl)"
121 #define VARIABLE_PROGURL "$(progurl)"
122 #define VARIABLE_USERURL "$(userurl)"
123 #define VARIABLE_WORKDIRURL "$(workdirurl)"
124 // --> PB 2004-10-27 #i32656# - new variable of hierarchy service
125 #define VARIABLE_BASEINSTURL "$(baseinsturl)"
126 #define VARIABLE_USERDATAURL "$(userdataurl)"
127 // <--
128 #define VARIABLE_BRANDBASEURL "$(brandbaseurl)"
129
130 using namespace com::sun::star::uno;
131 using namespace com::sun::star::beans;
132 using namespace com::sun::star::util;
133 using namespace com::sun::star::lang;
134 using namespace com::sun::star::container;
135
136 //_________________________________________________________________________________________________________________
137 // Namespace
138 //_________________________________________________________________________________________________________________
139 //
140
141 namespace framework
142 {
143
144 struct FixedVariable
145 {
146 const char* pVarName;
147 PreDefVariable nEnumValue;
148 int nStrLen;
149 bool bAbsPath;
150 };
151
152 struct TableEntry
153 {
154 const char* pOSString;
155 int nStrLen;
156 };
157
158 // Table with valid operating system strings
159 // Name of the os as char* and the length
160 // of the string
161 static TableEntry aOSTable[OS_COUNT] =
162 {
163 { "WINDOWS" , 7 },
164 { "UNIX" , 4 },
165 { "SOLARIS" , 7 },
166 { "LINUX" , 5 },
167 { "" , 0 } // unknown
168 };
169
170 // Table with valid environment variables
171 // Name of the environment type as a char* and
172 // the length of the string.
173 static TableEntry aEnvTable[ET_COUNT] =
174 {
175 { "HOST" , 4 },
176 { "YPDOMAIN" , 8 },
177 { "DNSDOMAIN" , 9 },
178 { "NTDOMAIN" , 8 },
179 { "OS" , 2 },
180 { "" , 0 } // unknown
181 };
182
183 // Priority table for the environment types. Lower numbers define
184 // a higher priority. Equal numbers has the same priority that means
185 // that the first match wins!!
186 static sal_Int16 aEnvPrioTable[ET_COUNT] =
187 {
188 1, // ET_HOST
189 2, // ET_IPDOMAIN
190 2, // ET_DNSDOMAIN
191 2, // ET_NTDOMAIN
192 3, // ET_OS
193 99, // ET_UNKNOWN
194 };
195
196 // Table with all fixed/predefined variables supported.
197 static FixedVariable aFixedVarTable[] =
198 {
199 { VARIABLE_INST, PREDEFVAR_INST, REPLACELENGTH_INST, true },
200 { VARIABLE_PROG, PREDEFVAR_PROG, REPLACELENGTH_PROG, true },
201 { VARIABLE_USER, PREDEFVAR_USER, REPLACELENGTH_USER, true },
202 { VARIABLE_WORK, PREDEFVAR_WORK, REPLACELENGTH_WORK, true }, // Special variable (transient)!
203 { VARIABLE_HOME, PREDEFVAR_HOME, REPLACELENGTH_HOME, true },
204 { VARIABLE_TEMP, PREDEFVAR_TEMP, REPLACELENGTH_TEMP, true },
205 { VARIABLE_PATH, PREDEFVAR_PATH, REPLACELENGTH_PATH, true },
206 { VARIABLE_LANG, PREDEFVAR_LANG, REPLACELENGTH_LANG, false },
207 { VARIABLE_LANGID, PREDEFVAR_LANGID, REPLACELENGTH_LANGID, false },
208 { VARIABLE_VLANG, PREDEFVAR_VLANG, REPLACELENGTH_VLANG, false },
209 { VARIABLE_INSTPATH, PREDEFVAR_INSTPATH, REPLACELENGTH_INSTPATH, true },
210 { VARIABLE_PROGPATH, PREDEFVAR_PROGPATH, REPLACELENGTH_PROGPATH, true },
211 { VARIABLE_USERPATH, PREDEFVAR_USERPATH, REPLACELENGTH_USERPATH, true },
212 { VARIABLE_INSTURL, PREDEFVAR_INSTURL, REPLACELENGTH_INSTURL, true },
213 { VARIABLE_PROGURL, PREDEFVAR_PROGURL, REPLACELENGTH_PROGURL, true },
214 { VARIABLE_USERURL, PREDEFVAR_USERURL, REPLACELENGTH_USERURL, true },
215 { VARIABLE_WORKDIRURL, PREDEFVAR_WORKDIRURL, REPLACELENGTH_WORKDIRURL,true }, // Special variable (transient) and don't use for resubstitution!
216 // --> PB 2004-10-27 #i32656# - new variable of hierarchy service
217 { VARIABLE_BASEINSTURL, PREDEFVAR_BASEINSTURL, REPLACELENGTH_BASEINSTURL,true },
218 { VARIABLE_USERDATAURL, PREDEFVAR_USERDATAURL, REPLACELENGTH_USERDATAURL,true },
219 // <--
220 { VARIABLE_BRANDBASEURL,PREDEFVAR_BRANDBASEURL, RTL_CONSTASCII_LENGTH(VARIABLE_BRANDBASEURL), true }
221 };
222
223 //_________________________________________________________________________________________________________________
224 // Implementation helper classes
225 //_________________________________________________________________________________________________________________
226 //
227
GetOperatingSystemFromString(const rtl::OUString & aOSString)228 OperatingSystem SubstitutePathVariables_Impl::GetOperatingSystemFromString( const rtl::OUString& aOSString )
229 {
230 for ( int i = 0; i < OS_COUNT; i++ )
231 {
232 if ( aOSString.equalsIgnoreAsciiCaseAsciiL( aOSTable[i].pOSString, aOSTable[i].nStrLen ))
233 return (OperatingSystem)i;
234 }
235
236 return OS_UNKNOWN;
237 }
238
GetEnvTypeFromString(const rtl::OUString & aEnvTypeString)239 EnvironmentType SubstitutePathVariables_Impl::GetEnvTypeFromString( const rtl::OUString& aEnvTypeString )
240 {
241 for ( int i = 0; i < ET_COUNT; i++ )
242 {
243 if ( aEnvTypeString.equalsIgnoreAsciiCaseAsciiL( aEnvTable[i].pOSString, aEnvTable[i].nStrLen ))
244 return (EnvironmentType)i;
245 }
246
247 return ET_UNKNOWN;
248 }
249
SubstitutePathVariables_Impl(const Link & aNotifyLink)250 SubstitutePathVariables_Impl::SubstitutePathVariables_Impl( const Link& aNotifyLink ) :
251 utl::ConfigItem( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Office.Substitution" ))),
252 m_bYPDomainRetrieved( false ),
253 m_bDNSDomainRetrieved( false ),
254 m_bNTDomainRetrieved( false ),
255 m_bHostRetrieved( false ),
256 m_bOSRetrieved( false ),
257 m_aListenerNotify( aNotifyLink ),
258 m_aSharePointsNodeName( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "SharePoints" ))),
259 m_aDirPropertyName( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/Directory" ))),
260 m_aEnvPropertyName( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/Environment" ))),
261 m_aLevelSep( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/" )))
262 {
263 // Enable notification mechanism
264 // We need it to get information about changes outside these class on our configuration branch
265 Sequence< rtl::OUString > aNotifySeq( 1 );
266 aNotifySeq[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "SharePoints" ));
267 EnableNotification( aNotifySeq, sal_True );
268 }
269
~SubstitutePathVariables_Impl()270 SubstitutePathVariables_Impl::~SubstitutePathVariables_Impl()
271 {
272 }
273
GetSharePointsRules(SubstituteVariables & aSubstVarMap)274 void SubstitutePathVariables_Impl::GetSharePointsRules( SubstituteVariables& aSubstVarMap )
275 {
276 Sequence< rtl::OUString > aSharePointNames;
277 ReadSharePointsFromConfiguration( aSharePointNames );
278
279 if ( aSharePointNames.getLength() > 0 )
280 {
281 sal_Int32 nSharePoints = 0;
282
283 // Read SharePoints container from configuration
284 while ( nSharePoints < aSharePointNames.getLength() )
285 {
286 rtl::OUString aSharePointNodeName( m_aSharePointsNodeName );
287 aSharePointNodeName += rtl::OUString::createFromAscii( "/" );
288 aSharePointNodeName += aSharePointNames[ nSharePoints ];
289
290 SubstituteRuleVector aRuleSet;
291 ReadSharePointRuleSetFromConfiguration( aSharePointNames[ nSharePoints ], aSharePointNodeName, aRuleSet );
292 if ( !aRuleSet.empty() )
293 {
294 // We have at minimum one rule. Filter the correct rule out of the rule set
295 // and put into our SubstituteVariable map
296 SubstituteRule aActiveRule;
297 if ( FilterRuleSet( aRuleSet, aActiveRule ))
298 {
299 // We have found an active rule
300 aActiveRule.aSubstVariable = aSharePointNames[ nSharePoints ];
301 aSubstVarMap.insert( SubstituteVariables::value_type(
302 aActiveRule.aSubstVariable, aActiveRule ));
303 }
304 }
305 ++nSharePoints;
306 }
307 }
308 }
309
Notify(const com::sun::star::uno::Sequence<rtl::OUString> &)310 void SubstitutePathVariables_Impl::Notify( const com::sun::star::uno::Sequence< rtl::OUString >& /*aPropertyNames*/ )
311 {
312 // NOT implemented yet!
313 }
314
Commit()315 void SubstitutePathVariables_Impl::Commit()
316 {
317 }
318
319
320 //_________________________________________________________________________________________________________________
321 // private methods
322 //_________________________________________________________________________________________________________________
323 //
324
GetOperatingSystem()325 OperatingSystem SubstitutePathVariables_Impl::GetOperatingSystem()
326 {
327 if ( !m_bOSRetrieved )
328 {
329 #ifdef SOLARIS
330 m_eOSType = OS_SOLARIS;
331 #elif defined LINUX
332 m_eOSType = OS_LINUX;
333 #elif defined WIN32
334 m_eOSType = OS_WINDOWS;
335 #elif defined UNIX
336 m_eOSType = OS_UNIX;
337 #else
338 m_eOSType = OS_UNKNOWN;
339 #endif
340 m_bOSRetrieved = sal_True;
341 }
342
343 return m_eOSType;
344 }
345
GetYPDomainName()346 const rtl::OUString& SubstitutePathVariables_Impl::GetYPDomainName()
347 {
348 if ( !m_bYPDomainRetrieved )
349 {
350 m_aYPDomain = NetworkDomain::GetYPDomainName().toAsciiLowerCase();
351 m_bYPDomainRetrieved = sal_True;
352 }
353
354 return m_aYPDomain;
355 }
356
GetDNSDomainName()357 const rtl::OUString& SubstitutePathVariables_Impl::GetDNSDomainName()
358 {
359 if ( !m_bDNSDomainRetrieved )
360 {
361 rtl::OUString aTemp;
362 osl::SocketAddr aSockAddr;
363 oslSocketResult aResult;
364
365 rtl::OUString aHostName = GetHostName();
366 osl::SocketAddr::resolveHostname( aHostName, aSockAddr );
367 aTemp = aSockAddr.getHostname( &aResult );
368
369 // DNS domain name begins after the first "."
370 sal_Int32 nIndex = aTemp.indexOf( '.' );
371 if ( nIndex >= 0 && aTemp.getLength() > nIndex+1 )
372 m_aDNSDomain = aTemp.copy( nIndex+1 ).toAsciiLowerCase();
373 else
374 m_aDNSDomain = rtl::OUString();
375
376 m_bDNSDomainRetrieved = sal_True;
377 }
378
379 return m_aDNSDomain;
380 }
381
GetNTDomainName()382 const rtl::OUString& SubstitutePathVariables_Impl::GetNTDomainName()
383 {
384 if ( !m_bNTDomainRetrieved )
385 {
386 m_aNTDomain = NetworkDomain::GetNTDomainName().toAsciiLowerCase();
387 m_bNTDomainRetrieved = sal_True;
388 }
389
390 return m_aNTDomain;
391 }
392
GetHostName()393 const rtl::OUString& SubstitutePathVariables_Impl::GetHostName()
394 {
395 if ( !m_bHostRetrieved )
396 {
397 rtl::OUString aHostName;
398 oslSocketResult aSocketResult;
399
400 m_aHost = osl::SocketAddr::getLocalHostname( &aSocketResult ).toAsciiLowerCase();
401 }
402
403 return m_aHost;
404 }
405
FilterRuleSet(const SubstituteRuleVector & aRuleSet,SubstituteRule & aActiveRule)406 bool SubstitutePathVariables_Impl::FilterRuleSet( const SubstituteRuleVector& aRuleSet, SubstituteRule& aActiveRule )
407 {
408 bool bResult = sal_False;
409
410 if ( !aRuleSet.empty() )
411 {
412 const sal_uInt32 nCount = aRuleSet.size();
413
414 sal_Int16 nPrioCurrentRule = aEnvPrioTable[ ET_UNKNOWN ];
415 for ( sal_uInt32 nIndex = 0; nIndex < nCount; nIndex++ )
416 {
417 const SubstituteRule& aRule = aRuleSet[nIndex];
418 EnvironmentType eEnvType = aRule.aEnvType;
419
420 // Check if environment type has a higher priority than current one!
421 if ( nPrioCurrentRule > aEnvPrioTable[eEnvType] )
422 {
423 switch ( eEnvType )
424 {
425 case ET_HOST:
426 {
427 rtl::OUString aHost = GetHostName();
428 rtl::OUString aHostStr;
429 aRule.aEnvValue >>= aHostStr;
430 aHostStr = aHostStr.toAsciiLowerCase();
431
432 // Pattern match if domain environment match
433 WildCard aPattern(aHostStr);
434 bool bMatch = aPattern.Matches(aHost);
435 if ( bMatch )
436 {
437 aActiveRule = aRule;
438 bResult = true;
439 nPrioCurrentRule = aEnvPrioTable[eEnvType];
440 }
441 }
442 break;
443
444 case ET_YPDOMAIN:
445 case ET_DNSDOMAIN:
446 case ET_NTDOMAIN:
447 {
448 rtl::OUString aDomain;
449 rtl::OUString aDomainStr;
450 aRule.aEnvValue >>= aDomainStr;
451 aDomainStr = aDomainStr.toAsciiLowerCase();
452
453 // Retrieve the correct domain value
454 if ( eEnvType == ET_YPDOMAIN )
455 aDomain = GetYPDomainName();
456 else if ( eEnvType == ET_DNSDOMAIN )
457 aDomain = GetDNSDomainName();
458 else
459 aDomain = GetNTDomainName();
460
461 // Pattern match if domain environment match
462 WildCard aPattern(aDomainStr);
463 bool bMatch = aPattern.Matches(aDomain);
464 if ( bMatch )
465 {
466 aActiveRule = aRule;
467 bResult = true;
468 nPrioCurrentRule = aEnvPrioTable[eEnvType];
469 }
470 }
471 break;
472
473 case ET_OS:
474 {
475 // No pattern matching for OS type
476 OperatingSystem eOSType = GetOperatingSystem();
477
478 sal_Int16 nValue = 0;
479 aRule.aEnvValue >>= nValue;
480
481 bool bUnix = ( eOSType == OS_LINUX ) || ( eOSType == OS_SOLARIS );
482 OperatingSystem eRuleOSType = (OperatingSystem)nValue;
483
484 // Match if OS identical or rule is set to UNIX and OS is LINUX/SOLARIS!
485 if (( eRuleOSType == eOSType ) || ( eRuleOSType == OS_UNIX && bUnix ))
486 {
487 aActiveRule = aRule;
488 bResult = true;
489 nPrioCurrentRule = aEnvPrioTable[eEnvType];
490 }
491 }
492 break;
493
494 case ET_UNKNOWN: // nothing to do
495 break;
496
497 default:
498 break;
499 }
500 }
501 }
502 }
503
504 return bResult;
505 }
506
ReadSharePointsFromConfiguration(Sequence<rtl::OUString> & aSharePointsSeq)507 void SubstitutePathVariables_Impl::ReadSharePointsFromConfiguration( Sequence< rtl::OUString >& aSharePointsSeq )
508 {
509 //returns all the names of all share point nodes
510 aSharePointsSeq = GetNodeNames( m_aSharePointsNodeName );
511 }
512
ReadSharePointRuleSetFromConfiguration(const rtl::OUString & aSharePointName,const rtl::OUString & aSharePointNodeName,SubstituteRuleVector & rRuleSet)513 void SubstitutePathVariables_Impl::ReadSharePointRuleSetFromConfiguration(
514 const rtl::OUString& aSharePointName,
515 const rtl::OUString& aSharePointNodeName,
516 SubstituteRuleVector& rRuleSet )
517 {
518 Sequence< rtl::OUString > aSharePointMappingsNodeNames = GetNodeNames( aSharePointNodeName, utl::CONFIG_NAME_LOCAL_PATH );
519
520 sal_Int32 nSharePointMapping = 0;
521 while ( nSharePointMapping < aSharePointMappingsNodeNames.getLength() )
522 {
523 rtl::OUString aSharePointMapping( aSharePointNodeName );
524 aSharePointMapping += m_aLevelSep;
525 aSharePointMapping += aSharePointMappingsNodeNames[ nSharePointMapping ];
526
527 // Read SharePointMapping
528 rtl::OUString aDirValue;
529 rtl::OUString aDirProperty( aSharePointMapping );
530 aDirProperty += m_aDirPropertyName;
531
532 // Read only the directory property
533 Sequence< rtl::OUString > aDirPropertySeq( 1 );
534 aDirPropertySeq[0] = aDirProperty;
535
536 Sequence< Any > aValueSeq = GetProperties( aDirPropertySeq );
537 if ( aValueSeq.getLength() == 1 )
538 aValueSeq[0] >>= aDirValue;
539
540 // Read the environment setting
541 rtl::OUString aEnvUsed;
542 rtl::OUString aEnvProperty( aSharePointMapping );
543 aEnvProperty += m_aEnvPropertyName;
544 Sequence< rtl::OUString > aEnvironmentVariable = GetNodeNames( aEnvProperty );
545
546 // Filter the property which has a value set
547 Sequence< rtl::OUString > aEnvUsedPropertySeq( aEnvironmentVariable.getLength() );
548
549 rtl::OUString aEnvUsePropNameTemplate( aEnvProperty );
550 aEnvUsePropNameTemplate += m_aLevelSep;
551
552 for ( sal_Int32 nProperty = 0; nProperty < aEnvironmentVariable.getLength(); nProperty++ )
553 aEnvUsedPropertySeq[nProperty] = rtl::OUString( aEnvUsePropNameTemplate + aEnvironmentVariable[nProperty] );
554
555 Sequence< Any > aEnvUsedValueSeq;
556 aEnvUsedValueSeq = GetProperties( aEnvUsedPropertySeq );
557
558 rtl::OUString aEnvUsedValue;
559 for ( sal_Int32 nIndex = 0; nIndex < aEnvironmentVariable.getLength(); nIndex++ )
560 {
561 if ( aEnvUsedValueSeq[nIndex] >>= aEnvUsedValue )
562 {
563 aEnvUsed = aEnvironmentVariable[nIndex];
564 break;
565 }
566 }
567
568 // Decode the environment and optional the operating system settings
569 Any aEnvValue;
570 EnvironmentType eEnvType = GetEnvTypeFromString( aEnvUsed );
571 if ( eEnvType == ET_OS )
572 {
573 OperatingSystem eOSType = GetOperatingSystemFromString( aEnvUsedValue );
574 aEnvValue <<= (sal_Int16)eOSType;
575 }
576 else
577 aEnvValue <<= aEnvUsedValue;
578
579 // Create rule struct and push it into the rule set
580 SubstituteRule aRule( aSharePointName, aDirValue, aEnvValue, eEnvType );
581 rRuleSet.push_back( aRule );
582
583 ++nSharePointMapping;
584 }
585 }
586
587 //*****************************************************************************************************************
588 // XInterface, XTypeProvider, XServiceInfo
589 //*****************************************************************************************************************
DEFINE_XSERVICEINFO_ONEINSTANCESERVICE(SubstitutePathVariables,::cppu::OWeakObject,SERVICENAME_SUBSTITUTEPATHVARIABLES,IMPLEMENTATIONNAME_SUBSTITUTEPATHVARIABLES)590 DEFINE_XSERVICEINFO_ONEINSTANCESERVICE ( SubstitutePathVariables ,
591 ::cppu::OWeakObject ,
592 SERVICENAME_SUBSTITUTEPATHVARIABLES ,
593 IMPLEMENTATIONNAME_SUBSTITUTEPATHVARIABLES )
594
595 DEFINE_INIT_SERVICE ( SubstitutePathVariables, {} )
596
597
598 SubstitutePathVariables::SubstitutePathVariables( const Reference< XMultiServiceFactory >& xServiceManager ) :
599 ThreadHelpBase(),
600 m_aVarStart( SIGN_STARTVARIABLE ),
601 m_aVarEnd( SIGN_ENDVARIABLE ),
602 m_aImpl( LINK( this, SubstitutePathVariables, implts_ConfigurationNotify )),
603 m_xServiceManager( xServiceManager )
604 {
605 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "SubstitutePathVariables::SubstitutePathVariables" );
606 int i;
607
608 SetPredefinedPathVariables( m_aPreDefVars );
609 m_aImpl.GetSharePointsRules( m_aSubstVarMap );
610
611 // Init the predefined/fixed variable to index hash map
612 for ( i = 0; i < PREDEFVAR_COUNT; i++ )
613 {
614 // Store variable name into struct of predefined/fixed variables
615 m_aPreDefVars.m_FixedVarNames[i] = rtl::OUString::createFromAscii( aFixedVarTable[i].pVarName );
616
617 // Create hash map entry
618 m_aPreDefVarMap.insert( VarNameToIndexMap::value_type(
619 m_aPreDefVars.m_FixedVarNames[i], aFixedVarTable[i].nEnumValue ) );
620 }
621
622 // Sort predefined/fixed variable to path length
623 for ( i = 0; i < PREDEFVAR_COUNT; i++ )
624 {
625 if (( i != PREDEFVAR_WORKDIRURL ) && ( i != PREDEFVAR_PATH ))
626 {
627 // Special path variables, don't include into automatic resubstitution search!
628 // $(workdirurl) is not allowed to resubstitute! This variable is the value of path settings entry
629 // and it could be possible that it will be resubstituted by itself!!
630 // Example: WORK_PATH=c:\test, $(workdirurl)=WORK_PATH => WORK_PATH=$(workdirurl) and this cannot be substituted!
631 ReSubstFixedVarOrder aFixedVar;
632 aFixedVar.eVariable = aFixedVarTable[i].nEnumValue;
633 aFixedVar.nVarValueLength = m_aPreDefVars.m_FixedVar[(sal_Int32)aFixedVar.eVariable].getLength();
634 m_aReSubstFixedVarOrder.push_back( aFixedVar );
635 }
636 }
637 m_aReSubstFixedVarOrder.sort();
638
639 // Sort user variables to path length
640 SubstituteVariables::const_iterator pIter;
641 for ( pIter = m_aSubstVarMap.begin(); pIter != m_aSubstVarMap.end(); pIter++ )
642 {
643 ReSubstUserVarOrder aUserOrderVar;
644 rtl::OUStringBuffer aStrBuffer( pIter->second.aSubstVariable.getLength() );
645 aStrBuffer.append( m_aVarStart );
646 aStrBuffer.append( pIter->second.aSubstVariable );
647 aStrBuffer.append( m_aVarEnd );
648 aUserOrderVar.aVarName = aStrBuffer.makeStringAndClear();
649 aUserOrderVar.nVarValueLength = pIter->second.aSubstVariable.getLength();
650 m_aReSubstUserVarOrder.push_back( aUserOrderVar );
651 }
652 m_aReSubstUserVarOrder.sort();
653 }
654
~SubstitutePathVariables()655 SubstitutePathVariables::~SubstitutePathVariables()
656 {
657 }
658
659 // XStringSubstitution
substituteVariables(const::rtl::OUString & aText,sal_Bool bSubstRequired)660 rtl::OUString SAL_CALL SubstitutePathVariables::substituteVariables( const ::rtl::OUString& aText, sal_Bool bSubstRequired )
661 {
662 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "SubstitutePathVariables::substituteVariables" );
663 ResetableGuard aLock( m_aLock );
664 return impl_substituteVariable( aText, bSubstRequired );
665 }
666
reSubstituteVariables(const::rtl::OUString & aText)667 rtl::OUString SAL_CALL SubstitutePathVariables::reSubstituteVariables( const ::rtl::OUString& aText )
668 {
669 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "SubstitutePathVariables::reSubstituteVariables" );
670 ResetableGuard aLock( m_aLock );
671 return impl_reSubstituteVariables( aText );
672 }
673
getSubstituteVariableValue(const::rtl::OUString & aVariable)674 rtl::OUString SAL_CALL SubstitutePathVariables::getSubstituteVariableValue( const ::rtl::OUString& aVariable )
675 {
676 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "SubstitutePathVariables::getSubstituteVariableValue" );
677 ResetableGuard aLock( m_aLock );
678 return impl_getSubstituteVariableValue( aVariable );
679 }
680
681 //_________________________________________________________________________________________________________________
682 // protected methods
683 //_________________________________________________________________________________________________________________
684 //
685
IMPL_LINK(SubstitutePathVariables,implts_ConfigurationNotify,SubstitutePathNotify *,EMPTYARG)686 IMPL_LINK( SubstitutePathVariables, implts_ConfigurationNotify, SubstitutePathNotify*, EMPTYARG )
687 {
688 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
689 ResetableGuard aLock( m_aLock );
690
691 return 0;
692 }
693
ConvertOSLtoUCBURL(const rtl::OUString & aOSLCompliantURL) const694 rtl::OUString SubstitutePathVariables::ConvertOSLtoUCBURL( const rtl::OUString& aOSLCompliantURL ) const
695 {
696 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "SubstitutePathVariables::ConvertOSLtoUCBURL" );
697 String aResult;
698 rtl::OUString aTemp;
699
700 osl::FileBase::getSystemPathFromFileURL( aOSLCompliantURL, aTemp );
701 utl::LocalFileHelper::ConvertPhysicalNameToURL( aTemp, aResult );
702
703 // Not all OSL URL's can be mapped to UCB URL's!
704 if ( aResult.Len() == 0 )
705 return aOSLCompliantURL;
706 else
707 return rtl::OUString( aResult );
708 }
709
GetWorkPath() const710 rtl::OUString SubstitutePathVariables::GetWorkPath() const
711 {
712 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "SubstitutePathVariables::GetWorkPath" );
713 rtl::OUString aWorkPath;
714 ::comphelper::ConfigurationHelper::readDirectKey(
715 m_xServiceManager,
716 ::rtl::OUString::createFromAscii("org.openoffice.Office.Paths"),
717 ::rtl::OUString::createFromAscii("Paths/Work"),
718 ::rtl::OUString::createFromAscii("WritePath"),
719 ::comphelper::ConfigurationHelper::E_READONLY) >>= aWorkPath;
720 return aWorkPath;
721 }
722
GetWorkVariableValue() const723 rtl::OUString SubstitutePathVariables::GetWorkVariableValue() const
724 {
725 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "SubstitutePathVariables::GetWorkVariableValue" );
726 ::rtl::OUString aWorkPath;
727 ::comphelper::ConfigurationHelper::readDirectKey(
728 m_xServiceManager,
729 ::rtl::OUString::createFromAscii("org.openoffice.Office.Paths"),
730 ::rtl::OUString::createFromAscii("Variables"),
731 ::rtl::OUString::createFromAscii("Work"),
732 ::comphelper::ConfigurationHelper::E_READONLY) >>= aWorkPath;
733
734 // fallback to $HOME in case platform dependend config layer does not return
735 // an usuable work dir value.
736 if (aWorkPath.getLength() < 1)
737 {
738 osl::Security aSecurity;
739 aSecurity.getHomeDir( aWorkPath );
740 }
741 return ConvertOSLtoUCBURL( aWorkPath );
742 }
743
GetHomeVariableValue() const744 rtl::OUString SubstitutePathVariables::GetHomeVariableValue() const
745 {
746 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "SubstitutePathVariables::GetHomeVariableValue" );
747 osl::Security aSecurity;
748 rtl::OUString aHomePath;
749
750 aSecurity.getHomeDir( aHomePath );
751 return ConvertOSLtoUCBURL( aHomePath );
752 }
753
GetPathVariableValue() const754 rtl::OUString SubstitutePathVariables::GetPathVariableValue() const
755 {
756 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "SubstitutePathVariables::GetPathVariableValue" );
757 const int PATH_EXTEND_FACTOR = 120;
758
759 rtl::OUString aRetStr;
760 const char* pEnv = getenv( "PATH" );
761
762 if ( pEnv )
763 {
764 rtl::OUString aTmp;
765 rtl::OUString aPathList( pEnv, strlen( pEnv ), gsl_getSystemTextEncoding() );
766 rtl::OUStringBuffer aPathStrBuffer( aPathList.getLength() * PATH_EXTEND_FACTOR / 100 );
767
768 bool bAppendSep = false;
769 sal_Int32 nToken = 0;
770 do
771 {
772 ::rtl::OUString sToken = aPathList.getToken(0, SAL_PATHSEPARATOR, nToken);
773 if (sToken.getLength())
774 {
775 osl::FileBase::getFileURLFromSystemPath( sToken, aTmp );
776 if ( bAppendSep )
777 aPathStrBuffer.appendAscii( ";" ); // Office uses ';' as path separator
778 aPathStrBuffer.append( aTmp );
779 bAppendSep = true;
780 }
781 }
782 while(nToken>=0);
783
784 aRetStr = aPathStrBuffer.makeStringAndClear();
785 }
786
787 return aRetStr;
788 }
789
impl_substituteVariable(const::rtl::OUString & rText,bool bSubstRequired)790 rtl::OUString SubstitutePathVariables::impl_substituteVariable( const ::rtl::OUString& rText, bool bSubstRequired )
791 {
792 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "SubstitutePathVariables::impl_substituteVariable" );
793 // This is maximal recursive depth supported!
794 const sal_Int32 nMaxRecursiveDepth = 8;
795
796 rtl::OUString aWorkText = rText;
797 rtl::OUString aResult;
798
799 // Use vector with strings to detect endless recursions!
800 std::vector< rtl::OUString > aEndlessRecursiveDetector;
801
802 // Search for first occure of "$(...".
803 sal_Int32 nDepth = 0;
804 sal_Int32 bSubstitutionCompleted = sal_False;
805 sal_Int32 nPosition = aWorkText.indexOf( m_aVarStart ); // = first position of "$(" in string
806 sal_Int32 nLength = 0; // = count of letters from "$(" to ")" in string
807 bool bVarNotSubstituted = false;
808
809 // Have we found any variable like "$(...)"?
810 if ( nPosition != STRPOS_NOTFOUND )
811 {
812 // Yes; Get length of found variable.
813 // If no ")" was found - nLength is set to 0 by default! see before.
814 sal_Int32 nEndPosition = aWorkText.indexOf( m_aVarEnd, nPosition );
815 if ( nEndPosition != STRPOS_NOTFOUND )
816 nLength = nEndPosition - nPosition + 1;
817 }
818
819 // Is there something to replace ?
820 bool bWorkRetrieved = false;
821 bool bWorkDirURLRetrieved = false;
822 while ( !bSubstitutionCompleted && nDepth < nMaxRecursiveDepth )
823 {
824 while ( ( nPosition != STRPOS_NOTFOUND ) && ( nLength > 3 ) ) // "$(" ")"
825 {
826 // YES; Get the next variable for replace.
827 sal_Int32 nReplaceLength = 0;
828 rtl::OUString aReplacement;
829 rtl::OUString aSubString = aWorkText.copy( nPosition, nLength );
830 rtl::OUString aSubVarString;
831
832 // Path variables are not case sensitive!
833 aSubVarString = aSubString.toAsciiLowerCase();
834 VarNameToIndexMap::const_iterator pNTOIIter = m_aPreDefVarMap.find( aSubVarString );
835 if ( pNTOIIter != m_aPreDefVarMap.end() )
836 {
837 // Fixed/Predefined variable found
838 PreDefVariable nIndex = (PreDefVariable)pNTOIIter->second;
839
840 // Determine variable value and length from array/table
841 if ( nIndex == PREDEFVAR_WORK && !bWorkRetrieved )
842 {
843 // Transient value, retrieve it again
844 m_aPreDefVars.m_FixedVar[ (PreDefVariable)nIndex ] = GetWorkVariableValue();
845 bWorkRetrieved = true;
846 }
847 else if ( nIndex == PREDEFVAR_WORKDIRURL && !bWorkDirURLRetrieved )
848 {
849 // Transient value, retrieve it again
850 m_aPreDefVars.m_FixedVar[ (PreDefVariable)nIndex ] = GetWorkPath();
851 bWorkDirURLRetrieved = true;
852 }
853
854 // Check preconditions to substitue path variables.
855 // 1. A path variable can only be substituted if it follows a SEARCHPATH_DELIMITER ';'!
856 // 2. It's located exactly at the start of the string being substituted!
857 if (( aFixedVarTable[ int( nIndex ) ].bAbsPath && (( nPosition == 0 ) || (( nPosition > 0 ) && ( aWorkText[nPosition-1] == ';')))) ||
858 ( !aFixedVarTable[ int( nIndex ) ].bAbsPath ))
859 {
860 aReplacement = m_aPreDefVars.m_FixedVar[ (PreDefVariable)nIndex ];
861 nReplaceLength = nLength;
862 }
863 }
864 else
865 {
866 // Extract the variable name and try to find in the user defined variable set
867 rtl::OUString aVarName = aSubString.copy( 2, nLength-3 );
868 SubstituteVariables::const_iterator pIter = m_aSubstVarMap.find( aVarName );
869 if ( pIter != m_aSubstVarMap.end() )
870 {
871 // Found.
872 aReplacement = pIter->second.aSubstValue;
873 nReplaceLength = nLength;
874 }
875 }
876
877 // Have we found something to replace?
878 if ( nReplaceLength > 0 )
879 {
880 // Yes ... then do it.
881 aWorkText = aWorkText.replaceAt( nPosition, nReplaceLength, aReplacement );
882 }
883 else
884 {
885 // Variable not known
886 bVarNotSubstituted = false;
887 nPosition += nLength;
888 }
889
890 // Step after replaced text! If no text was replaced (unknown variable!),
891 // length of aReplacement is 0 ... and we don't step then.
892 nPosition += aReplacement.getLength();
893
894 // We must control index in string before call something at OUString!
895 // The OUString-implementation don't do it for us :-( but the result is not defined otherwise.
896 if ( nPosition + 1 > aWorkText.getLength() )
897 {
898 // Position is out of range. Break loop!
899 nPosition = STRPOS_NOTFOUND;
900 nLength = 0;
901 }
902 else
903 {
904 // Else; Position is valid. Search for next variable to replace.
905 nPosition = aWorkText.indexOf( m_aVarStart, nPosition );
906 // Have we found any variable like "$(...)"?
907 if ( nPosition != STRPOS_NOTFOUND )
908 {
909 // Yes; Get length of found variable. If no ")" was found - nLength must set to 0!
910 nLength = 0;
911 sal_Int32 nEndPosition = aWorkText.indexOf( m_aVarEnd, nPosition );
912 if ( nEndPosition != STRPOS_NOTFOUND )
913 nLength = nEndPosition - nPosition + 1;
914 }
915 }
916 }
917
918 nPosition = aWorkText.indexOf( m_aVarStart );
919 if ( nPosition == -1 )
920 {
921 bSubstitutionCompleted = sal_True;
922 break; // All variables are substituted
923 }
924 else
925 {
926 // Check for recursion
927 const sal_uInt32 nCount = aEndlessRecursiveDetector.size();
928 for ( sal_uInt32 i=0; i < nCount; i++ )
929 {
930 if ( aEndlessRecursiveDetector[i] == aWorkText )
931 {
932 if ( bVarNotSubstituted )
933 break; // Not all variables could be substituted!
934 else
935 {
936 nDepth = nMaxRecursiveDepth;
937 break; // Recursion detected!
938 }
939 }
940 }
941
942 aEndlessRecursiveDetector.push_back( aWorkText );
943
944 // Initialize values for next
945 sal_Int32 nEndPosition = aWorkText.indexOf( m_aVarEnd, nPosition );
946 if ( nEndPosition != STRPOS_NOTFOUND )
947 nLength = nEndPosition - nPosition + 1;
948 bVarNotSubstituted = sal_False;
949 ++nDepth;
950 }
951 }
952
953 // Fill return value with result
954 if ( bSubstitutionCompleted )
955 {
956 // Substitution successful!
957 aResult = aWorkText;
958 }
959 else
960 {
961 // Substitution not successful!
962 if ( nDepth == nMaxRecursiveDepth )
963 {
964 // recursion depth reached!
965 if ( bSubstRequired )
966 {
967 rtl::OUString aMsg( RTL_CONSTASCII_USTRINGPARAM( "Endless recursion detected. Cannot substitute variables!" ));
968 throw NoSuchElementException( aMsg, (cppu::OWeakObject *)this );
969 }
970 else
971 aResult = rText;
972 }
973 else
974 {
975 // variable in text but unknown!
976 if ( bSubstRequired )
977 {
978 rtl::OUString aMsg( RTL_CONSTASCII_USTRINGPARAM( "Unknown variable found!" ));
979 throw NoSuchElementException( aMsg, (cppu::OWeakObject *)this );
980 }
981 else
982 aResult = aWorkText;
983 }
984 }
985
986 return aResult;
987 }
988
impl_reSubstituteVariables(const::rtl::OUString & rURL)989 rtl::OUString SubstitutePathVariables::impl_reSubstituteVariables( const ::rtl::OUString& rURL )
990 {
991 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "SubstitutePathVariables::impl_reSubstituteVariables" );
992 rtl::OUString aURL;
993
994 INetURLObject aUrl( rURL );
995 if ( !aUrl.HasError() )
996 aURL = aUrl.GetMainURL( INetURLObject::NO_DECODE );
997 else
998 {
999 // Convert a system path to a UCB compliant URL before resubstitution
1000 rtl::OUString aTemp;
1001 if ( osl::FileBase::getFileURLFromSystemPath( rURL, aTemp ) == osl::FileBase::E_None )
1002 {
1003 aTemp = ConvertOSLtoUCBURL( aTemp );
1004 if ( aTemp.getLength() )
1005 aURL = INetURLObject( aTemp ).GetMainURL( INetURLObject::NO_DECODE );
1006 else
1007 return rURL;
1008 }
1009 else
1010 {
1011 // rURL is not a valid URL nor a osl system path. Give up and return error!
1012 return rURL;
1013 }
1014 }
1015
1016 // Due to a recursive definition this code must exchange variables with variables!
1017 bool bResubstitutionCompleted = false;
1018 bool bVariableFound = false;
1019
1020 // Get transient predefined path variable $(work) value before starting resubstitution
1021 m_aPreDefVars.m_FixedVar[ PREDEFVAR_WORK ] = GetWorkVariableValue();
1022
1023 while ( !bResubstitutionCompleted )
1024 {
1025 ReSubstFixedVarOrderVector::const_iterator pIterFixed;
1026 for ( pIterFixed = m_aReSubstFixedVarOrder.begin(); pIterFixed != m_aReSubstFixedVarOrder.end(); pIterFixed++ )
1027 {
1028 rtl::OUString aValue = m_aPreDefVars.m_FixedVar[ (sal_Int32)pIterFixed->eVariable ];
1029 sal_Int32 nPos = aURL.indexOf( aValue );
1030 if ( nPos >= 0 )
1031 {
1032 bool bMatch = true;
1033 if ( pIterFixed->eVariable == PREDEFVAR_LANG ||
1034 pIterFixed->eVariable == PREDEFVAR_LANGID ||
1035 pIterFixed->eVariable == PREDEFVAR_VLANG )
1036 {
1037 // Special path variables as they can occur in the middle of a path. Only match if they
1038 // describe a whole directory and not only a substring of a directory!
1039 const sal_Unicode* pStr = aURL.getStr();
1040
1041 if ( nPos > 0 )
1042 bMatch = ( aURL[ nPos-1 ] == '/' );
1043
1044 if ( bMatch )
1045 {
1046 if ( nPos + aValue.getLength() < aURL.getLength() )
1047 bMatch = ( pStr[ nPos + aValue.getLength() ] == '/' );
1048 }
1049 }
1050
1051 if ( bMatch )
1052 {
1053 rtl::OUStringBuffer aStrBuffer( aURL.getLength() );
1054 aStrBuffer.append( aURL.copy( 0, nPos ) );
1055 aStrBuffer.append( m_aPreDefVars.m_FixedVarNames[ (sal_Int32)pIterFixed->eVariable ] ); // Get the variable name for struct var name array!
1056 aStrBuffer.append( aURL.copy( nPos + aValue.getLength(), ( aURL.getLength() - ( nPos + aValue.getLength() )) ));
1057 aURL = aStrBuffer.makeStringAndClear();
1058 bVariableFound = true; // Resubstitution not finished yet!
1059 break;
1060 }
1061 }
1062 }
1063
1064 // This part can be iteratered more than one time as variables can contain variables again!
1065 ReSubstUserVarOrderVector::const_iterator pIterUser;
1066 for ( pIterUser = m_aReSubstUserVarOrder.begin(); pIterUser != m_aReSubstUserVarOrder.end(); pIterUser++ )
1067 {
1068 rtl::OUString aVarValue = pIterUser->aVarName;
1069 sal_Int32 nPos = aURL.indexOf( aVarValue );
1070 if ( nPos >= 0 )
1071 {
1072 rtl::OUStringBuffer aStrBuffer( aURL.getLength() );
1073 aStrBuffer.append( aURL.copy( 0, nPos ) );
1074 aStrBuffer.append( m_aVarStart );
1075 aStrBuffer.append( aVarValue );
1076 aStrBuffer.append( m_aVarEnd );
1077 aStrBuffer.append( aURL.copy( nPos + aVarValue.getLength(), ( aURL.getLength() - ( nPos + aVarValue.getLength() )) ));
1078 aURL = aStrBuffer.makeStringAndClear();
1079 bVariableFound = true; // Resubstitution not finished yet!
1080 }
1081 }
1082
1083 if ( !bVariableFound )
1084 bResubstitutionCompleted = true;
1085 else
1086 bVariableFound = sal_False; // Next resubstitution
1087 }
1088
1089 return aURL;
1090 }
1091
1092 // This method support both request schemes "$("<varname>")" or "<varname>".
impl_getSubstituteVariableValue(const::rtl::OUString & rVariable)1093 ::rtl::OUString SubstitutePathVariables::impl_getSubstituteVariableValue( const ::rtl::OUString& rVariable )
1094 {
1095 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "SubstitutePathVariables::impl_getSubstituteVariableValue" );
1096 rtl::OUString aVariable;
1097
1098 sal_Int32 nPos = rVariable.indexOf( m_aVarStart );
1099 if ( nPos == -1 )
1100 {
1101 // Prepare variable name before hash map access
1102 rtl::OUStringBuffer aStrBuffer( rVariable.getLength() + m_aVarStart.getLength() + m_aVarEnd.getLength() );
1103 aStrBuffer.append( m_aVarStart );
1104 aStrBuffer.append( rVariable );
1105 aStrBuffer.append( m_aVarEnd );
1106 aVariable = aStrBuffer.makeStringAndClear();
1107 }
1108
1109 VarNameToIndexMap::const_iterator pNTOIIter = m_aPreDefVarMap.find( ( nPos == -1 ) ? aVariable : rVariable );
1110
1111 // Fixed/Predefined variable
1112 if ( pNTOIIter != m_aPreDefVarMap.end() )
1113 {
1114 PreDefVariable nIndex = (PreDefVariable)pNTOIIter->second;
1115 return m_aPreDefVars.m_FixedVar[(sal_Int32)nIndex];
1116 }
1117 else
1118 {
1119 // Prepare variable name before hash map access
1120 if ( nPos >= 0 )
1121 {
1122 if ( rVariable.getLength() > 3 )
1123 aVariable = rVariable.copy( 2, rVariable.getLength() - 3 );
1124 else
1125 {
1126 rtl::OUString aExceptionText( RTL_CONSTASCII_USTRINGPARAM( "Unknown variable!" ));
1127 throw NoSuchElementException();
1128 }
1129 }
1130 else
1131 aVariable = rVariable;
1132
1133 // User defined variable
1134 SubstituteVariables::const_iterator pIter = m_aSubstVarMap.find( aVariable );
1135 if ( pIter != m_aSubstVarMap.end() )
1136 {
1137 // found!
1138 return pIter->second.aSubstValue;
1139 }
1140
1141 rtl::OUString aExceptionText( RTL_CONSTASCII_USTRINGPARAM( "Unknown variable!" ));
1142 throw NoSuchElementException( aExceptionText, (cppu::OWeakObject *)this );
1143 }
1144 }
1145
SetPredefinedPathVariables(PredefinedPathVariables & aPreDefPathVariables)1146 void SubstitutePathVariables::SetPredefinedPathVariables( PredefinedPathVariables& aPreDefPathVariables )
1147 {
1148 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "SubstitutePathVariables::SetPredefinedPathVariables" );
1149 Any aAny;
1150 ::rtl::OUString aOfficePath;
1151 ::rtl::OUString aUserPath;
1152 ::rtl::OUString aTmp;
1153 ::rtl::OUString aTmp2;
1154 String aResult;
1155
1156 // Get inspath and userpath from bootstrap mechanism in every case as file URL
1157 ::utl::Bootstrap::PathStatus aState;
1158 ::rtl::OUString sVal ;
1159
1160 aState = utl::Bootstrap::locateBaseInstallation( sVal );
1161 if( aState==::utl::Bootstrap::PATH_EXISTS ) {
1162 aPreDefPathVariables.m_FixedVar[ PREDEFVAR_INSTPATH ] = ConvertOSLtoUCBURL( sVal );
1163 }
1164 else {
1165 LOG_ERROR( "SubstitutePathVariables::SetPredefinedPathVariables", "Bootstrap code has no value for instpath!");
1166 }
1167
1168 aState = utl::Bootstrap::locateUserData( sVal );
1169 //There can be the valid case that there is no user installation. For example, "unopkg sync"
1170 //is currently (OOo3.4) run as part of the setup. Then no user installation is required.
1171 //Therefore we do not assert here.
1172 if( aState == ::utl::Bootstrap::PATH_EXISTS ) {
1173 aPreDefPathVariables.m_FixedVar[ PREDEFVAR_USERPATH ] = ConvertOSLtoUCBURL( sVal );
1174 }
1175
1176 // Set $(inst), $(instpath), $(insturl)
1177 aPreDefPathVariables.m_FixedVar[ PREDEFVAR_INSTURL ] = aPreDefPathVariables.m_FixedVar[ PREDEFVAR_INSTPATH ];
1178 aPreDefPathVariables.m_FixedVar[ PREDEFVAR_INST ] = aPreDefPathVariables.m_FixedVar[ PREDEFVAR_INSTPATH ];
1179 // --> PB 2004-10-27 #i32656# - new variable of hierarchy service
1180 aPreDefPathVariables.m_FixedVar[ PREDEFVAR_BASEINSTURL ]= aPreDefPathVariables.m_FixedVar[ PREDEFVAR_INSTPATH ];
1181 // <--
1182
1183 // Set $(user), $(userpath), $(userurl)
1184 aPreDefPathVariables.m_FixedVar[ PREDEFVAR_USERURL ] = aPreDefPathVariables.m_FixedVar[ PREDEFVAR_USERPATH ];
1185 aPreDefPathVariables.m_FixedVar[ PREDEFVAR_USER ] = aPreDefPathVariables.m_FixedVar[ PREDEFVAR_USERPATH ];
1186 // --> PB 2004-11-11 #i32656# - new variable of hierarchy service
1187 aPreDefPathVariables.m_FixedVar[ PREDEFVAR_USERDATAURL ]= aPreDefPathVariables.m_FixedVar[ PREDEFVAR_USERPATH ];
1188 // <--
1189
1190 // Detect the program directory
1191 // Set $(prog), $(progpath), $(progurl)
1192 INetURLObject aProgObj(
1193 aPreDefPathVariables.m_FixedVar[ PREDEFVAR_INSTPATH ] );
1194 if ( !aProgObj.HasError() && aProgObj.insertName( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("program")) ) )
1195 {
1196 aPreDefPathVariables.m_FixedVar[ PREDEFVAR_PROGPATH ] = aProgObj.GetMainURL(INetURLObject::NO_DECODE);
1197 aPreDefPathVariables.m_FixedVar[ PREDEFVAR_PROGURL ] = aPreDefPathVariables.m_FixedVar[ PREDEFVAR_PROGPATH ];
1198 aPreDefPathVariables.m_FixedVar[ PREDEFVAR_PROG ] = aPreDefPathVariables.m_FixedVar[ PREDEFVAR_PROGPATH ];
1199 }
1200
1201 // Detect the language type of the current office
1202 aPreDefPathVariables.m_eLanguageType = LANGUAGE_ENGLISH_US;
1203 rtl::OUString aLocaleStr;
1204 if ( utl::ConfigManager::GetConfigManager()->GetDirectConfigProperty( utl::ConfigManager::LOCALE ) >>= aLocaleStr )
1205 aPreDefPathVariables.m_eLanguageType = MsLangId::convertIsoStringToLanguage( aLocaleStr );
1206 else
1207 {
1208 LOG_ERROR( "SubstitutePathVariables::SetPredefinedPathVariables", "Wrong Any type for language!" );
1209 }
1210
1211 // Set $(lang)
1212 aPreDefPathVariables.m_FixedVar[ PREDEFVAR_LANG ] = ConvertOSLtoUCBURL(
1213 rtl::OUString::createFromAscii( ResMgr::GetLang( aPreDefPathVariables.m_eLanguageType, 0 ) ));
1214
1215 // Set $(vlang)
1216 aPreDefPathVariables.m_FixedVar[ PREDEFVAR_VLANG ] = aLocaleStr;
1217
1218 // Set $(langid)
1219 aPreDefPathVariables.m_FixedVar[ PREDEFVAR_LANGID ] = rtl::OUString::valueOf( (sal_Int32)aPreDefPathVariables.m_eLanguageType );
1220
1221 // Set the other pre defined path variables
1222 // Set $(work)
1223 aPreDefPathVariables.m_FixedVar[ PREDEFVAR_WORK ] = GetWorkVariableValue();
1224 aPreDefPathVariables.m_FixedVar[ PREDEFVAR_HOME ] = GetHomeVariableValue();
1225
1226 // Set $(workdirurl) this is the value of the path PATH_WORK which doesn't make sense
1227 // anymore because the path settings service has this value! It can deliver this value more
1228 // quickly than the substitution service!
1229 aPreDefPathVariables.m_FixedVar[ PREDEFVAR_WORKDIRURL ] = GetWorkPath();
1230
1231 // Set $(path) variable
1232 aPreDefPathVariables.m_FixedVar[ PREDEFVAR_PATH ] = GetPathVariableValue();
1233
1234 // Set $(temp)
1235 osl::FileBase::getTempDirURL( aTmp );
1236 aPreDefPathVariables.m_FixedVar[ PREDEFVAR_TEMP ] = ConvertOSLtoUCBURL( aTmp );
1237
1238 aPreDefPathVariables.m_FixedVar[PREDEFVAR_BRANDBASEURL] = rtl::OUString(
1239 RTL_CONSTASCII_USTRINGPARAM("$OOO_BASE_DIR"));
1240 rtl::Bootstrap::expandMacros(
1241 aPreDefPathVariables.m_FixedVar[PREDEFVAR_BRANDBASEURL]);
1242 }
1243
1244 } // namespace framework
1245