update dev300-m58
[ooovba.git] / comphelper / source / misc / regpathhelper.cxx
blobadd0793c3955ebf9dad9cc50dff823772e68e784
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: regpathhelper.cxx,v $
10 * $Revision: 1.17 $
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"
34 #include <stdio.h>
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>
40 #include <rtl/uri.h>
41 #include <rtl/uri.hxx>
43 using namespace vos;
44 using namespace osl;
45 using namespace rtl;
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"
55 #ifdef SAL_UNX
56 #define CONFIG_PATH_PREFIX "."
57 #else
58 #define CONFIG_PATH_PREFIX ""
59 #endif
61 namespace comphelper
64 /**
65 @return sal_True, if the office is started in a portal
66 environment.
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;
74 OUString sArg;
75 while( nArgs > 0 )
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();
89 else
91 OUString aEncHome = sArg.copy( nStart + 1 , nEnd - nStart -1 );
92 *pDirectory = rtl::Uri::decode(aEncHome,
93 rtl_UriDecodeWithCharset,
94 RTL_TEXTENCODING_UTF8);
96 break;
100 return bIsPortalUser;
104 static OUString getDefaultLocalRegistry()
106 OUString uBuffer, userRegistryName;
107 OUString portalUserDir;
109 sal_Bool bIsPortalUser = retrievePortalUserDir( &portalUserDir );
111 if ( bIsPortalUser )
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 )
132 sPath += sSeparator;
133 sPath += userRegistryName.getToken(0, '/', nIndex);
134 if( nIndex == -1 )
135 break;
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)
142 return OUString();
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;
163 FILE *f=NULL;
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" );
171 if (f != NULL)
173 fclose(f);
174 userRegistryName = OStringToOUString( sBuffer, osl_getThreadTextEncoding() );
178 if ( !userRegistryName.getLength() )
180 userRegistryName = getDefaultLocalRegistry();
183 return userRegistryName;
186 OUString getPathToSystemRegistry()
188 OUString uBuffer;
189 OUString registryBaseName( RTL_CONSTASCII_USTRINGPARAM(REGISTRY_SYSTEM_NAME) );
190 OUString systemRegistryName;
191 FILE *f=NULL;
193 // search in the directory of the executable
194 OStartupInfo info;
195 if( OStartupInfo::E_None == info.getExecutableFile(uBuffer) )
197 sal_uInt32 lastIndex = uBuffer.lastIndexOf(PATH_DELEMITTER);
198 if (lastIndex > 0)
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" );
212 if (f == NULL)
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");
220 if (f != NULL)
222 fclose(f);
223 systemRegistryName = OStringToOUString( tmpStr, osl_getThreadTextEncoding() );
224 } else
226 systemRegistryName = OUString();
229 } else
231 fclose(f);
234 return systemRegistryName;