xref: /trunk/main/comphelper/source/misc/regpathhelper.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
30 
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>
39 
40 using namespace vos;
41 using namespace osl;
42 using namespace rtl;
43 
44 #define PATH_DELEMITTER '/'
45 
46 #define USER_REGISTRY_NAME_ENV      "STAR_USER_REGISTRY"
47 #define SYSTEM_REGISTRY_NAME_ENV    "STAR_REGISTRY"
48 #define REGISTRY_SYSTEM_NAME    "services.rdb"
49 
50 #define REGISTRY_LOCAL_NAME     "user60.rdb"
51 
52 #ifdef SAL_UNX
53 #define CONFIG_PATH_PREFIX      "."
54 #else
55 #define CONFIG_PATH_PREFIX      ""
56 #endif
57 
58 namespace comphelper
59 {
60 
61 /**
62    @return sal_True, if the office is started in a portal
63                      environment.
64            sal_False, if the common office is started
65  */
66 static sal_Bool retrievePortalUserDir( OUString *pDirectory )
67 {
68     OStartupInfo startInfo;
69     sal_uInt32 nArgs = startInfo.getCommandArgCount();
70     sal_Bool bIsPortalUser = sal_False;
71     OUString sArg;
72     while( nArgs > 0 )
73     {
74         if ( !startInfo.getCommandArg(--nArgs, sArg) )
75         {
76             if ( sArg.indexOf(OUString::createFromAscii("-userid")) == 0 )
77             {
78 
79                 bIsPortalUser = sal_True;
80                 sal_Int32 nStart = sArg.lastIndexOf( '[' );
81                 sal_Int32 nEnd   = sArg.lastIndexOf( ']' );
82                 if( -1 == nStart || -1 == nEnd || nEnd < nStart)
83                 {
84                     *pDirectory = OUString();
85                 }
86                 else
87                 {
88                     OUString aEncHome = sArg.copy( nStart + 1 , nEnd - nStart -1 );
89                     *pDirectory = rtl::Uri::decode(aEncHome,
90                                                    rtl_UriDecodeWithCharset,
91                                                    RTL_TEXTENCODING_UTF8);
92                 }
93                 break;
94             }
95         }
96     }
97     return bIsPortalUser;
98 }
99 
100 
101 static OUString getDefaultLocalRegistry()
102 {
103     OUString uBuffer, userRegistryName;
104     OUString portalUserDir;
105 
106     sal_Bool bIsPortalUser = retrievePortalUserDir( &portalUserDir );
107 
108     if ( bIsPortalUser )
109     {
110         if(  portalUserDir.getLength() )
111         {
112             FileBase::getFileURLFromSystemPath( portalUserDir , portalUserDir );
113             userRegistryName = portalUserDir;
114             userRegistryName += OUString( RTL_CONSTASCII_USTRINGPARAM(
115                 "/user/" REGISTRY_LOCAL_NAME ) );
116 
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;
124 
125             sal_Int32 nIndex = 3;
126             sPath += userRegistryName.getToken(2, '/', nIndex);
127             while( nIndex != -1 )
128             {
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 )
135                 {
136                     retRC = Directory::create(sPath);
137                     if ( retRC != FileBase::E_None && retRC != FileBase::E_EXIST)
138                     {
139                         return OUString();
140                     }
141                 }
142             }
143         }
144     }
145     else /* bIsPortalUser */
146     {
147         ::osl::Security aUserSecurity;
148         aUserSecurity.getConfigDir( userRegistryName );
149         userRegistryName += OUString( RTL_CONSTASCII_USTRINGPARAM(
150             "/" CONFIG_PATH_PREFIX REGISTRY_LOCAL_NAME ) );
151     }
152 
153     return userRegistryName;
154 }
155 
156 
157 OUString getPathToUserRegistry()
158 {
159     OUString    userRegistryName;
160     FILE        *f=NULL;
161 
162     // search the environment STAR_USER_REGISTRY
163     OString sBuffer( getenv(USER_REGISTRY_NAME_ENV) );
164     if ( sBuffer.getLength() > 0 )
165     {
166         f = fopen( sBuffer.getStr(), "r" );
167 
168         if (f != NULL)
169         {
170             fclose(f);
171             userRegistryName = OStringToOUString( sBuffer, osl_getThreadTextEncoding() );
172         }
173     }
174 
175     if ( !userRegistryName.getLength() )
176     {
177         userRegistryName = getDefaultLocalRegistry();
178     }
179 
180     return userRegistryName;
181 }
182 
183 OUString getPathToSystemRegistry()
184 {
185     OUString uBuffer;
186     OUString registryBaseName( RTL_CONSTASCII_USTRINGPARAM(REGISTRY_SYSTEM_NAME) );
187     OUString systemRegistryName;
188     FILE     *f=NULL;
189 
190     // search in the directory of the executable
191     OStartupInfo info;
192     if( OStartupInfo::E_None == info.getExecutableFile(uBuffer) )
193     {
194         sal_uInt32  lastIndex = uBuffer.lastIndexOf(PATH_DELEMITTER);
195         if (lastIndex > 0)
196         {
197             uBuffer = uBuffer.copy(0, lastIndex + 1);
198         }
199 
200         uBuffer += registryBaseName;
201 
202         if (!FileBase::getSystemPathFromFileURL(uBuffer, systemRegistryName))
203         {
204             OString tmpStr( OUStringToOString(systemRegistryName, osl_getThreadTextEncoding()) );
205             f = fopen( tmpStr.getStr(), "r" );
206         }
207     }
208 
209     if (f == NULL)
210     {
211         // search the environment STAR_REGISTRY
212         OString tmpStr( getenv(SYSTEM_REGISTRY_NAME_ENV) );
213         if ( tmpStr.getLength() > 0 )
214         {
215             f = fopen(tmpStr.getStr(), "r");
216 
217             if (f != NULL)
218             {
219                 fclose(f);
220                 systemRegistryName = OStringToOUString( tmpStr, osl_getThreadTextEncoding() );
221             } else
222             {
223                 systemRegistryName = OUString();
224             }
225         }
226     } else
227     {
228         fclose(f);
229     }
230 
231     return systemRegistryName;
232 }
233 
234 }
235 
236