1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: regpathhelper.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
35 #include <osl/file.hxx>
36 #include <osl/security.hxx>
37 #include <osl/thread.h>
38 #include <vos/process.hxx>
39 #include <rtl/textenc.h>
41 #include <rtl/uri.hxx>
47 #define PATH_DELEMITTER '/'
49 #define USER_REGISTRY_NAME_ENV "STAR_USER_REGISTRY"
50 #define SYSTEM_REGISTRY_NAME_ENV "STAR_REGISTRY"
51 #define REGISTRY_SYSTEM_NAME "services.rdb"
53 #define REGISTRY_LOCAL_NAME "user60.rdb"
56 #define CONFIG_PATH_PREFIX "."
58 #define CONFIG_PATH_PREFIX ""
65 @return sal_True, if the office is started in a portal
67 sal_False, if the common office is started
69 static sal_Bool
retrievePortalUserDir( OUString
*pDirectory
)
71 OStartupInfo startInfo
;
72 sal_uInt32 nArgs
= startInfo
.getCommandArgCount();
73 sal_Bool bIsPortalUser
= sal_False
;
77 if ( !startInfo
.getCommandArg(--nArgs
, sArg
) )
79 if ( sArg
.indexOf(OUString::createFromAscii("-userid")) == 0 )
82 bIsPortalUser
= sal_True
;
83 sal_Int32 nStart
= sArg
.lastIndexOf( '[' );
84 sal_Int32 nEnd
= sArg
.lastIndexOf( ']' );
85 if( -1 == nStart
|| -1 == nEnd
|| nEnd
< nStart
)
87 *pDirectory
= OUString();
91 OUString aEncHome
= sArg
.copy( nStart
+ 1 , nEnd
- nStart
-1 );
92 *pDirectory
= rtl::Uri::decode(aEncHome
,
93 rtl_UriDecodeWithCharset
,
94 RTL_TEXTENCODING_UTF8
);
100 return bIsPortalUser
;
104 static OUString
getDefaultLocalRegistry()
106 OUString uBuffer
, userRegistryName
;
107 OUString portalUserDir
;
109 sal_Bool bIsPortalUser
= retrievePortalUserDir( &portalUserDir
);
113 if( portalUserDir
.getLength() )
115 FileBase::getFileURLFromSystemPath( portalUserDir
, portalUserDir
);
116 userRegistryName
= portalUserDir
;
117 userRegistryName
+= OUString( RTL_CONSTASCII_USTRINGPARAM(
118 "/user/" REGISTRY_LOCAL_NAME
) );
120 // Directory creation is probably necessary for bootstrapping a new
121 // user in the portal environment (the ucb uses this function).
122 // This should be solved differently, as
123 // no one expects this function to create anything ...
124 OUString
sSeparator(RTL_CONSTASCII_USTRINGPARAM("/"));
125 OUString
sPath(RTL_CONSTASCII_USTRINGPARAM("file://"));
126 FileBase::RC retRC
= FileBase::E_None
;
128 sal_Int32 nIndex
= 3;
129 sPath
+= userRegistryName
.getToken(2, '/', nIndex
);
130 while( nIndex
!= -1 )
133 sPath
+= userRegistryName
.getToken(0, '/', nIndex
);
136 Directory
aDir( sPath
);
137 if( aDir
.open() == FileBase::E_NOENT
)
139 retRC
= Directory::create(sPath
);
140 if ( retRC
!= FileBase::E_None
&& retRC
!= FileBase::E_EXIST
)
148 else /* bIsPortalUser */
150 ::osl::Security aUserSecurity
;
151 aUserSecurity
.getConfigDir( userRegistryName
);
152 userRegistryName
+= OUString( RTL_CONSTASCII_USTRINGPARAM(
153 "/" CONFIG_PATH_PREFIX REGISTRY_LOCAL_NAME
) );
156 return userRegistryName
;
160 OUString
getPathToUserRegistry()
162 OUString userRegistryName
;
165 // search the environment STAR_USER_REGISTRY
166 OString
sBuffer( getenv(USER_REGISTRY_NAME_ENV
) );
167 if ( sBuffer
.getLength() > 0 )
169 f
= fopen( sBuffer
.getStr(), "r" );
174 userRegistryName
= OStringToOUString( sBuffer
, osl_getThreadTextEncoding() );
178 if ( !userRegistryName
.getLength() )
180 userRegistryName
= getDefaultLocalRegistry();
183 return userRegistryName
;
186 OUString
getPathToSystemRegistry()
189 OUString
registryBaseName( RTL_CONSTASCII_USTRINGPARAM(REGISTRY_SYSTEM_NAME
) );
190 OUString systemRegistryName
;
193 // search in the directory of the executable
195 if( OStartupInfo::E_None
== info
.getExecutableFile(uBuffer
) )
197 sal_uInt32 lastIndex
= uBuffer
.lastIndexOf(PATH_DELEMITTER
);
200 uBuffer
= uBuffer
.copy(0, lastIndex
+ 1);
203 uBuffer
+= registryBaseName
;
205 if (!FileBase::getSystemPathFromFileURL(uBuffer
, systemRegistryName
))
207 OString
tmpStr( OUStringToOString(systemRegistryName
, osl_getThreadTextEncoding()) );
208 f
= fopen( tmpStr
.getStr(), "r" );
214 // search the environment STAR_REGISTRY
215 OString
tmpStr( getenv(SYSTEM_REGISTRY_NAME_ENV
) );
216 if ( tmpStr
.getLength() > 0 )
218 f
= fopen(tmpStr
.getStr(), "r");
223 systemRegistryName
= OStringToOUString( tmpStr
, osl_getThreadTextEncoding() );
226 systemRegistryName
= OUString();
234 return systemRegistryName
;