1dffe8aa0SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3dffe8aa0SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4dffe8aa0SAndrew Rist * or more contributor license agreements. See the NOTICE file
5dffe8aa0SAndrew Rist * distributed with this work for additional information
6dffe8aa0SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7dffe8aa0SAndrew Rist * to you under the Apache License, Version 2.0 (the
8dffe8aa0SAndrew Rist * "License"); you may not use this file except in compliance
9dffe8aa0SAndrew Rist * with the License. You may obtain a copy of the License at
10dffe8aa0SAndrew Rist *
11dffe8aa0SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12dffe8aa0SAndrew Rist *
13dffe8aa0SAndrew Rist * Unless required by applicable law or agreed to in writing,
14dffe8aa0SAndrew Rist * software distributed under the License is distributed on an
15dffe8aa0SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16dffe8aa0SAndrew Rist * KIND, either express or implied. See the License for the
17dffe8aa0SAndrew Rist * specific language governing permissions and limitations
18dffe8aa0SAndrew Rist * under the License.
19dffe8aa0SAndrew Rist *
20dffe8aa0SAndrew Rist *************************************************************/
21dffe8aa0SAndrew Rist
22dffe8aa0SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #include "precompiled_cppuhelper.hxx"
25cdf0e10cSrcweir #include "sal/config.h"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <stdlib.h>
28cdf0e10cSrcweir #include <string.h>
29cdf0e10cSrcweir
30cdf0e10cSrcweir #if defined WNT
31cdf0e10cSrcweir
32cdf0e10cSrcweir #define WIN32_LEAN_AND_MEAN
33cdf0e10cSrcweir #include <windows.h>
34cdf0e10cSrcweir
35cdf0e10cSrcweir /*
36cdf0e10cSrcweir * Gets the installation path from the Windows Registry for the specified
37cdf0e10cSrcweir * registry key.
38cdf0e10cSrcweir *
39cdf0e10cSrcweir * @param hroot open handle to predefined root registry key
40cdf0e10cSrcweir * @param subKeyName name of the subkey to open
41cdf0e10cSrcweir *
42cdf0e10cSrcweir * @return the installation path or NULL, if no installation was found or
43*07a3d7f1SPedro Giffuni * if an error occurred
44cdf0e10cSrcweir */
getPathFromRegistryKey(HKEY hroot,const char * subKeyName)45cdf0e10cSrcweir static char* getPathFromRegistryKey( HKEY hroot, const char* subKeyName )
46cdf0e10cSrcweir {
47cdf0e10cSrcweir HKEY hkey;
48cdf0e10cSrcweir DWORD type;
49cdf0e10cSrcweir char* data = NULL;
50cdf0e10cSrcweir DWORD size;
51cdf0e10cSrcweir
52cdf0e10cSrcweir /* open the specified registry key */
53cdf0e10cSrcweir if ( RegOpenKeyEx( hroot, subKeyName, 0, KEY_READ, &hkey ) != ERROR_SUCCESS )
54cdf0e10cSrcweir {
55cdf0e10cSrcweir return NULL;
56cdf0e10cSrcweir }
57cdf0e10cSrcweir
58cdf0e10cSrcweir /* find the type and size of the default value */
59cdf0e10cSrcweir if ( RegQueryValueEx( hkey, NULL, NULL, &type, NULL, &size) != ERROR_SUCCESS )
60cdf0e10cSrcweir {
61cdf0e10cSrcweir RegCloseKey( hkey );
62cdf0e10cSrcweir return NULL;
63cdf0e10cSrcweir }
64cdf0e10cSrcweir
65cdf0e10cSrcweir /* get memory to hold the default value */
66cdf0e10cSrcweir data = (char*) malloc( size );
67cdf0e10cSrcweir
68cdf0e10cSrcweir /* read the default value */
69cdf0e10cSrcweir if ( RegQueryValueEx( hkey, NULL, NULL, &type, (LPBYTE) data, &size ) != ERROR_SUCCESS )
70cdf0e10cSrcweir {
71cdf0e10cSrcweir RegCloseKey( hkey );
72cdf0e10cSrcweir return NULL;
73cdf0e10cSrcweir }
74cdf0e10cSrcweir
75cdf0e10cSrcweir /* release registry key handle */
76cdf0e10cSrcweir RegCloseKey( hkey );
77cdf0e10cSrcweir
78cdf0e10cSrcweir return data;
79cdf0e10cSrcweir }
80cdf0e10cSrcweir
81cdf0e10cSrcweir /*
82cdf0e10cSrcweir * Gets the installation path from the Windows Registry.
83cdf0e10cSrcweir *
84cdf0e10cSrcweir * @return the installation path or NULL, if no installation was found or
85*07a3d7f1SPedro Giffuni * if an error occurred
86cdf0e10cSrcweir */
platformSpecific()87cdf0e10cSrcweir static char* platformSpecific()
88cdf0e10cSrcweir {
89599cc5b4SOliver-Rainer Wittmann const char* SUBKEYNAME = "Software\\OpenOffice\\UNO\\InstallPath";
903889e2e9SJürgen Schmidt const char* SUBKEYNAME64 = "Software\\Wow6432Node\\OpenOffice\\UNO\\InstallPath";
91cdf0e10cSrcweir
92cdf0e10cSrcweir char* path = NULL;
93cdf0e10cSrcweir
94cdf0e10cSrcweir /* read the key's default value from HKEY_CURRENT_USER */
95cdf0e10cSrcweir path = getPathFromRegistryKey( HKEY_CURRENT_USER, SUBKEYNAME );
96cdf0e10cSrcweir
97cdf0e10cSrcweir if ( path == NULL )
983889e2e9SJürgen Schmidt {
993889e2e9SJürgen Schmidt /* read the key's default value from HKEY_LOCAL_USER 64 */
1003a354ae1SJürgen Schmidt path = getPathFromRegistryKey( HKEY_CURRENT_USER, SUBKEYNAME64 );
1013889e2e9SJürgen Schmidt }
1023a354ae1SJürgen Schmidt if ( path == NULL )
103cdf0e10cSrcweir {
104cdf0e10cSrcweir /* read the key's default value from HKEY_LOCAL_MACHINE */
105cdf0e10cSrcweir path = getPathFromRegistryKey( HKEY_LOCAL_MACHINE, SUBKEYNAME );
106cdf0e10cSrcweir }
1073a354ae1SJürgen Schmidt if ( path == NULL )
1083889e2e9SJürgen Schmidt {
1093889e2e9SJürgen Schmidt /* read the key's default value from HKEY_LOCAL_MACHINE 64*/
1103889e2e9SJürgen Schmidt path = getPathFromRegistryKey( HKEY_LOCAL_MACHINE, SUBKEYNAME64 );
1113889e2e9SJürgen Schmidt }
112cdf0e10cSrcweir
113cdf0e10cSrcweir return path;
114cdf0e10cSrcweir }
115cdf0e10cSrcweir
116cdf0e10cSrcweir #else
117cdf0e10cSrcweir
118cdf0e10cSrcweir #include <unistd.h>
119cdf0e10cSrcweir #include <limits.h>
1202e9bc605SJürgen Schmidt #include <stdio.h>
121cdf0e10cSrcweir /*
122cdf0e10cSrcweir * Gets the installation path from the PATH environment variable.
123cdf0e10cSrcweir *
124cdf0e10cSrcweir * <p>An installation is found, if the executable 'soffice' or a symbolic link
125cdf0e10cSrcweir * is in one of the directories listed in the PATH environment variable.</p>
126cdf0e10cSrcweir *
127cdf0e10cSrcweir * @return the installation path or NULL, if no installation was found or
128*07a3d7f1SPedro Giffuni * if an error occurred
129cdf0e10cSrcweir */
platformSpecific()130cdf0e10cSrcweir static char* platformSpecific()
131cdf0e10cSrcweir {
132cdf0e10cSrcweir char* path = NULL;
133cdf0e10cSrcweir
1342e9bc605SJürgen Schmidt #ifdef MACOSX
1352e9bc605SJürgen Schmidt /* On MacOS we have no soffice link under /usr/bin but the default office location is known
1362e9bc605SJürgen Schmidt and we check this only
1372e9bc605SJürgen Schmidt */
1382e9bc605SJürgen Schmidt const char* MACDEFAULTOFFICEPATH = "/Applications/OpenOffice.app/Contents/MacOS";
1392e9bc605SJürgen Schmidt const char* MACDEFAULTSOFFICE = "/Applications/OpenOffice.app/Contents/MacOS/soffice";
1402e9bc605SJürgen Schmidt
1412e9bc605SJürgen Schmidt if ( !access( MACDEFAULTSOFFICE, F_OK ) )
1422e9bc605SJürgen Schmidt {
1438ac661e5SPavel Janík path = (char*) malloc( strlen(MACDEFAULTOFFICEPATH) + 1 );
1442e9bc605SJürgen Schmidt strcpy( path, MACDEFAULTOFFICEPATH);
1452e9bc605SJürgen Schmidt }
1462e9bc605SJürgen Schmidt return path;
1472e9bc605SJürgen Schmidt #else
14889a59a52SPavel Janík const int SEPARATOR = '/';
14989a59a52SPavel Janík const char* PATHSEPARATOR = ":";
15089a59a52SPavel Janík const char* PATHVARNAME = "PATH";
15189a59a52SPavel Janík const char* APPENDIX = "/soffice";
15289a59a52SPavel Janík
15389a59a52SPavel Janík char* env = NULL;
15489a59a52SPavel Janík char* str = NULL;
15589a59a52SPavel Janík char* dir = NULL;
15689a59a52SPavel Janík char* file = NULL;
15789a59a52SPavel Janík char* resolved = NULL;
15889a59a52SPavel Janík char* sep = NULL;
15989a59a52SPavel Janík
16089a59a52SPavel Janík char buffer[PATH_MAX];
16189a59a52SPavel Janík int pos;
16289a59a52SPavel Janík
1632e9bc605SJürgen Schmidt /* get the value of the PATH environment variable */
164cdf0e10cSrcweir env = getenv( PATHVARNAME );
165cdf0e10cSrcweir str = (char*) malloc( strlen( env ) + 1 );
166cdf0e10cSrcweir strcpy( str, env );
167cdf0e10cSrcweir
168cdf0e10cSrcweir /* get the tokens separated by ':' */
169cdf0e10cSrcweir dir = strtok( str, PATHSEPARATOR );
170cdf0e10cSrcweir
171cdf0e10cSrcweir while ( dir )
172cdf0e10cSrcweir {
173cdf0e10cSrcweir /* construct soffice file path */
174cdf0e10cSrcweir file = (char*) malloc( strlen( dir ) + strlen( APPENDIX ) + 1 );
175cdf0e10cSrcweir strcpy( file, dir );
176cdf0e10cSrcweir strcat( file, APPENDIX );
177cdf0e10cSrcweir
178cdf0e10cSrcweir /* check existence of soffice file */
179cdf0e10cSrcweir if ( !access( file, F_OK ) )
180cdf0e10cSrcweir {
181cdf0e10cSrcweir /* resolve symbolic link */
182cdf0e10cSrcweir resolved = realpath( file, buffer );
183cdf0e10cSrcweir
184cdf0e10cSrcweir if ( resolved != NULL )
185cdf0e10cSrcweir {
186cdf0e10cSrcweir /* get path to program directory */
187cdf0e10cSrcweir sep = strrchr( resolved, SEPARATOR );
188cdf0e10cSrcweir
189cdf0e10cSrcweir if ( sep != NULL )
190cdf0e10cSrcweir {
191cdf0e10cSrcweir pos = sep - resolved;
192cdf0e10cSrcweir path = (char*) malloc( pos + 1 );
193cdf0e10cSrcweir strncpy( path, resolved, pos );
194cdf0e10cSrcweir path[ pos ] = '\0';
195cdf0e10cSrcweir free( file );
196cdf0e10cSrcweir break;
197cdf0e10cSrcweir }
198cdf0e10cSrcweir }
199cdf0e10cSrcweir }
200cdf0e10cSrcweir
201cdf0e10cSrcweir dir = strtok( NULL, PATHSEPARATOR );
202cdf0e10cSrcweir free( file );
203cdf0e10cSrcweir }
204cdf0e10cSrcweir
205cdf0e10cSrcweir free( str );
206cdf0e10cSrcweir
207cdf0e10cSrcweir return path;
2082e9bc605SJürgen Schmidt #endif
209cdf0e10cSrcweir }
210cdf0e10cSrcweir
211cdf0e10cSrcweir #endif
212cdf0e10cSrcweir
cppuhelper_detail_findSofficePath()213cdf0e10cSrcweir char const* cppuhelper_detail_findSofficePath()
214cdf0e10cSrcweir {
215cdf0e10cSrcweir const char* UNOPATHVARNAME = "UNO_PATH";
216cdf0e10cSrcweir
217cdf0e10cSrcweir char* path = NULL;
218cdf0e10cSrcweir
219cdf0e10cSrcweir /* get the installation path from the UNO_PATH environment variable */
220cdf0e10cSrcweir path = getenv( UNOPATHVARNAME );
221cdf0e10cSrcweir
222cdf0e10cSrcweir if ( path == NULL || strlen( path ) == 0 )
223cdf0e10cSrcweir {
224cdf0e10cSrcweir path = platformSpecific();
225cdf0e10cSrcweir }
226cdf0e10cSrcweir
227cdf0e10cSrcweir return path;
228cdf0e10cSrcweir }
229