1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
32 #include <osl/file.hxx>
33 #include <osl/security.hxx>
34 #include <osl/thread.h>
35 #include <vos/process.hxx>
36 #include <rtl/textenc.h>
38 #include <rtl/uri.hxx>
44 #define PATH_DELEMITTER '/'
46 #define USER_REGISTRY_NAME_ENV "STAR_USER_REGISTRY"
47 #define SYSTEM_REGISTRY_NAME_ENV "STAR_REGISTRY"
48 #define REGISTRY_SYSTEM_NAME "services.rdb"
50 #define REGISTRY_LOCAL_NAME "user60.rdb"
53 #define CONFIG_PATH_PREFIX "."
55 #define CONFIG_PATH_PREFIX ""
62 @return sal_True, if the office is started in a portal
64 sal_False, if the common office is started
66 static sal_Bool
retrievePortalUserDir( OUString
*pDirectory
)
68 OStartupInfo startInfo
;
69 sal_uInt32 nArgs
= startInfo
.getCommandArgCount();
70 sal_Bool bIsPortalUser
= sal_False
;
74 if ( !startInfo
.getCommandArg(--nArgs
, sArg
) )
76 if ( sArg
.indexOf(OUString::createFromAscii("-userid")) == 0 )
79 bIsPortalUser
= sal_True
;
80 sal_Int32 nStart
= sArg
.lastIndexOf( '[' );
81 sal_Int32 nEnd
= sArg
.lastIndexOf( ']' );
82 if( -1 == nStart
|| -1 == nEnd
|| nEnd
< nStart
)
84 *pDirectory
= OUString();
88 OUString aEncHome
= sArg
.copy( nStart
+ 1 , nEnd
- nStart
-1 );
89 *pDirectory
= rtl::Uri::decode(aEncHome
,
90 rtl_UriDecodeWithCharset
,
91 RTL_TEXTENCODING_UTF8
);
101 static OUString
getDefaultLocalRegistry()
103 OUString uBuffer
, userRegistryName
;
104 OUString portalUserDir
;
106 sal_Bool bIsPortalUser
= retrievePortalUserDir( &portalUserDir
);
110 if( portalUserDir
.getLength() )
112 FileBase::getFileURLFromSystemPath( portalUserDir
, portalUserDir
);
113 userRegistryName
= portalUserDir
;
114 userRegistryName
+= OUString( RTL_CONSTASCII_USTRINGPARAM(
115 "/user/" REGISTRY_LOCAL_NAME
) );
117 // Directory creation is probably necessary for bootstrapping a new
118 // user in the portal environment (the ucb uses this function).
119 // This should be solved differently, as
120 // no one expects this function to create anything ...
121 OUString
sSeparator(RTL_CONSTASCII_USTRINGPARAM("/"));
122 OUString
sPath(RTL_CONSTASCII_USTRINGPARAM("file://"));
123 FileBase::RC retRC
= FileBase::E_None
;
125 sal_Int32 nIndex
= 3;
126 sPath
+= userRegistryName
.getToken(2, '/', nIndex
);
127 while( nIndex
!= -1 )
130 sPath
+= userRegistryName
.getToken(0, '/', nIndex
);
133 Directory
aDir( sPath
);
134 if( aDir
.open() == FileBase::E_NOENT
)
136 retRC
= Directory::create(sPath
);
137 if ( retRC
!= FileBase::E_None
&& retRC
!= FileBase::E_EXIST
)
145 else /* bIsPortalUser */
147 ::osl::Security aUserSecurity
;
148 aUserSecurity
.getConfigDir( userRegistryName
);
149 userRegistryName
+= OUString( RTL_CONSTASCII_USTRINGPARAM(
150 "/" CONFIG_PATH_PREFIX REGISTRY_LOCAL_NAME
) );
153 return userRegistryName
;
157 OUString
getPathToUserRegistry()
159 OUString userRegistryName
;
162 // search the environment STAR_USER_REGISTRY
163 OString
sBuffer( getenv(USER_REGISTRY_NAME_ENV
) );
164 if ( sBuffer
.getLength() > 0 )
166 f
= fopen( sBuffer
.getStr(), "r" );
171 userRegistryName
= OStringToOUString( sBuffer
, osl_getThreadTextEncoding() );
175 if ( !userRegistryName
.getLength() )
177 userRegistryName
= getDefaultLocalRegistry();
180 return userRegistryName
;
183 OUString
getPathToSystemRegistry()
186 OUString
registryBaseName( RTL_CONSTASCII_USTRINGPARAM(REGISTRY_SYSTEM_NAME
) );
187 OUString systemRegistryName
;
190 // search in the directory of the executable
192 if( OStartupInfo::E_None
== info
.getExecutableFile(uBuffer
) )
194 sal_uInt32 lastIndex
= uBuffer
.lastIndexOf(PATH_DELEMITTER
);
197 uBuffer
= uBuffer
.copy(0, lastIndex
+ 1);
200 uBuffer
+= registryBaseName
;
202 if (!FileBase::getSystemPathFromFileURL(uBuffer
, systemRegistryName
))
204 OString
tmpStr( OUStringToOString(systemRegistryName
, osl_getThreadTextEncoding()) );
205 f
= fopen( tmpStr
.getStr(), "r" );
211 // search the environment STAR_REGISTRY
212 OString
tmpStr( getenv(SYSTEM_REGISTRY_NAME_ENV
) );
213 if ( tmpStr
.getLength() > 0 )
215 f
= fopen(tmpStr
.getStr(), "r");
220 systemRegistryName
= OStringToOUString( tmpStr
, osl_getThreadTextEncoding() );
223 systemRegistryName
= OUString();
231 return systemRegistryName
;