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 43cdf0e10cSrcweir * if an error occured 44cdf0e10cSrcweir */ 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 85cdf0e10cSrcweir * if an error occured 86cdf0e10cSrcweir */ 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 */ 1003889e2e9SJürgen Schmidt path = getPathFromRegistryKey( HKEY_LOCAL_MACHINE, SUBKEYNAME64 ); 1013889e2e9SJürgen Schmidt } 1023889e2e9SJürgen Schmidt else if ( path == NULL ) 103cdf0e10cSrcweir { 104cdf0e10cSrcweir /* read the key's default value from HKEY_LOCAL_MACHINE */ 105cdf0e10cSrcweir path = getPathFromRegistryKey( HKEY_LOCAL_MACHINE, SUBKEYNAME ); 106cdf0e10cSrcweir } 1073889e2e9SJürgen Schmidt else 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> 120*2e9bc605SJü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 128cdf0e10cSrcweir * if an error occured 129cdf0e10cSrcweir */ 130cdf0e10cSrcweir static char* platformSpecific() 131cdf0e10cSrcweir { 132cdf0e10cSrcweir const int SEPARATOR = '/'; 133cdf0e10cSrcweir const char* PATHSEPARATOR = ":"; 134cdf0e10cSrcweir const char* PATHVARNAME = "PATH"; 135cdf0e10cSrcweir const char* APPENDIX = "/soffice"; 136cdf0e10cSrcweir 137cdf0e10cSrcweir char* path = NULL; 138cdf0e10cSrcweir char* env = NULL; 139cdf0e10cSrcweir char* str = NULL; 140cdf0e10cSrcweir char* dir = NULL; 141cdf0e10cSrcweir char* file = NULL; 142cdf0e10cSrcweir char* resolved = NULL; 143cdf0e10cSrcweir char* sep = NULL; 144cdf0e10cSrcweir 145cdf0e10cSrcweir char buffer[PATH_MAX]; 146cdf0e10cSrcweir int pos; 147cdf0e10cSrcweir 148*2e9bc605SJürgen Schmidt #ifdef MACOSX 149*2e9bc605SJürgen Schmidt /* On MacOS we have no soffice link under /usr/bin but the default office location is known 150*2e9bc605SJürgen Schmidt and we check this only 151*2e9bc605SJürgen Schmidt */ 152*2e9bc605SJürgen Schmidt const char* MACDEFAULTOFFICEPATH = "/Applications/OpenOffice.app/Contents/MacOS"; 153*2e9bc605SJürgen Schmidt const char* MACDEFAULTSOFFICE = "/Applications/OpenOffice.app/Contents/MacOS/soffice"; 154*2e9bc605SJürgen Schmidt 155*2e9bc605SJürgen Schmidt if ( !access( MACDEFAULTSOFFICE, F_OK ) ) 156*2e9bc605SJürgen Schmidt { 157*2e9bc605SJürgen Schmidt path = (char*) malloc( MACDEFAULTOFFICEPATH + 1 ); 158*2e9bc605SJürgen Schmidt strcpy( path, MACDEFAULTOFFICEPATH); 159*2e9bc605SJürgen Schmidt } 160*2e9bc605SJürgen Schmidt return path; 161*2e9bc605SJürgen Schmidt #else 162*2e9bc605SJürgen Schmidt /* get the value of the PATH environment variable */ 163cdf0e10cSrcweir env = getenv( PATHVARNAME ); 164cdf0e10cSrcweir str = (char*) malloc( strlen( env ) + 1 ); 165cdf0e10cSrcweir strcpy( str, env ); 166cdf0e10cSrcweir 167cdf0e10cSrcweir /* get the tokens separated by ':' */ 168cdf0e10cSrcweir dir = strtok( str, PATHSEPARATOR ); 169cdf0e10cSrcweir 170cdf0e10cSrcweir while ( dir ) 171cdf0e10cSrcweir { 172cdf0e10cSrcweir /* construct soffice file path */ 173cdf0e10cSrcweir file = (char*) malloc( strlen( dir ) + strlen( APPENDIX ) + 1 ); 174cdf0e10cSrcweir strcpy( file, dir ); 175cdf0e10cSrcweir strcat( file, APPENDIX ); 176cdf0e10cSrcweir 177cdf0e10cSrcweir /* check existence of soffice file */ 178cdf0e10cSrcweir if ( !access( file, F_OK ) ) 179cdf0e10cSrcweir { 180cdf0e10cSrcweir /* resolve symbolic link */ 181cdf0e10cSrcweir resolved = realpath( file, buffer ); 182cdf0e10cSrcweir 183cdf0e10cSrcweir if ( resolved != NULL ) 184cdf0e10cSrcweir { 185cdf0e10cSrcweir /* get path to program directory */ 186cdf0e10cSrcweir sep = strrchr( resolved, SEPARATOR ); 187cdf0e10cSrcweir 188cdf0e10cSrcweir if ( sep != NULL ) 189cdf0e10cSrcweir { 190cdf0e10cSrcweir pos = sep - resolved; 191cdf0e10cSrcweir path = (char*) malloc( pos + 1 ); 192cdf0e10cSrcweir strncpy( path, resolved, pos ); 193cdf0e10cSrcweir path[ pos ] = '\0'; 194cdf0e10cSrcweir free( file ); 195cdf0e10cSrcweir break; 196cdf0e10cSrcweir } 197cdf0e10cSrcweir } 198cdf0e10cSrcweir } 199cdf0e10cSrcweir 200cdf0e10cSrcweir dir = strtok( NULL, PATHSEPARATOR ); 201cdf0e10cSrcweir free( file ); 202cdf0e10cSrcweir } 203cdf0e10cSrcweir 204cdf0e10cSrcweir free( str ); 205cdf0e10cSrcweir 206cdf0e10cSrcweir return path; 207*2e9bc605SJürgen Schmidt #endif 208cdf0e10cSrcweir } 209cdf0e10cSrcweir 210cdf0e10cSrcweir #endif 211cdf0e10cSrcweir 212cdf0e10cSrcweir char const* cppuhelper_detail_findSofficePath() 213cdf0e10cSrcweir { 214cdf0e10cSrcweir const char* UNOPATHVARNAME = "UNO_PATH"; 215cdf0e10cSrcweir 216cdf0e10cSrcweir char* path = NULL; 217cdf0e10cSrcweir 218cdf0e10cSrcweir /* get the installation path from the UNO_PATH environment variable */ 219cdf0e10cSrcweir path = getenv( UNOPATHVARNAME ); 220cdf0e10cSrcweir 221cdf0e10cSrcweir if ( path == NULL || strlen( path ) == 0 ) 222cdf0e10cSrcweir { 223cdf0e10cSrcweir path = platformSpecific(); 224cdf0e10cSrcweir } 225cdf0e10cSrcweir 226cdf0e10cSrcweir return path; 227cdf0e10cSrcweir } 228