CWS-TOOLING: integrate CWS os146
[LibreOffice.git] / comphelper / source / misc / regpathhelper.cxx
blob0ac8f0277236d94b8e150921448f61cd8d9c0565
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"
31 #include <stdio.h>
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>
37 #include <rtl/uri.h>
38 #include <rtl/uri.hxx>
40 using namespace vos;
41 using namespace osl;
42 using namespace rtl;
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"
52 #ifdef SAL_UNX
53 #define CONFIG_PATH_PREFIX "."
54 #else
55 #define CONFIG_PATH_PREFIX ""
56 #endif
58 namespace comphelper
61 /**
62 @return sal_True, if the office is started in a portal
63 environment.
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;
71 OUString sArg;
72 while( nArgs > 0 )
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();
86 else
88 OUString aEncHome = sArg.copy( nStart + 1 , nEnd - nStart -1 );
89 *pDirectory = rtl::Uri::decode(aEncHome,
90 rtl_UriDecodeWithCharset,
91 RTL_TEXTENCODING_UTF8);
93 break;
97 return bIsPortalUser;
101 static OUString getDefaultLocalRegistry()
103 OUString uBuffer, userRegistryName;
104 OUString portalUserDir;
106 sal_Bool bIsPortalUser = retrievePortalUserDir( &portalUserDir );
108 if ( bIsPortalUser )
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 )
129 sPath += sSeparator;
130 sPath += userRegistryName.getToken(0, '/', nIndex);
131 if( nIndex == -1 )
132 break;
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)
139 return OUString();
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;
160 FILE *f=NULL;
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" );
168 if (f != NULL)
170 fclose(f);
171 userRegistryName = OStringToOUString( sBuffer, osl_getThreadTextEncoding() );
175 if ( !userRegistryName.getLength() )
177 userRegistryName = getDefaultLocalRegistry();
180 return userRegistryName;
183 OUString getPathToSystemRegistry()
185 OUString uBuffer;
186 OUString registryBaseName( RTL_CONSTASCII_USTRINGPARAM(REGISTRY_SYSTEM_NAME) );
187 OUString systemRegistryName;
188 FILE *f=NULL;
190 // search in the directory of the executable
191 OStartupInfo info;
192 if( OStartupInfo::E_None == info.getExecutableFile(uBuffer) )
194 sal_uInt32 lastIndex = uBuffer.lastIndexOf(PATH_DELEMITTER);
195 if (lastIndex > 0)
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" );
209 if (f == NULL)
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");
217 if (f != NULL)
219 fclose(f);
220 systemRegistryName = OStringToOUString( tmpStr, osl_getThreadTextEncoding() );
221 } else
223 systemRegistryName = OUString();
226 } else
228 fclose(f);
231 return systemRegistryName;