Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / vcl / unx / generic / fontmanager / helper.cxx
blob9c1649d547f37013011a169a9d7956c90e6e2720
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <config_folders.h>
22 #include <sys/stat.h>
23 #include <limits.h>
24 #include <osl/process.h>
25 #include <osl/thread.h>
26 #include <rtl/bootstrap.hxx>
27 #include <rtl/ustring.hxx>
28 #include <sal/log.hxx>
29 #include <tools/urlobj.hxx>
30 #include <unx/helper.hxx>
32 using ::rtl::Bootstrap;
34 namespace psp {
36 OUString getOfficePath( whichOfficePath ePath )
38 static OUString aInstallationRootPath;
39 static OUString aUserPath;
40 static OUString aConfigPath;
41 static bool bOnce = false;
43 if( ! bOnce )
45 bOnce = true;
46 OUString aIni;
47 Bootstrap::get( "BRAND_BASE_DIR", aInstallationRootPath );
48 aIni = aInstallationRootPath + "/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE( "bootstrap" );
49 Bootstrap aBootstrap( aIni );
50 aBootstrap.getFrom( "CustomDataUrl", aConfigPath );
51 aBootstrap.getFrom( "UserInstallation", aUserPath );
52 OUString aUPath = aUserPath;
54 if( aConfigPath.startsWith( "file://" ) )
56 OUString aSysPath;
57 if( osl_getSystemPathFromFileURL( aConfigPath.pData, &aSysPath.pData ) == osl_File_E_None )
58 aConfigPath = aSysPath;
60 if( aInstallationRootPath.startsWith( "file://" ) )
62 OUString aSysPath;
63 if( osl_getSystemPathFromFileURL( aInstallationRootPath.pData, &aSysPath.pData ) == osl_File_E_None )
64 aInstallationRootPath = aSysPath;
66 if( aUserPath.startsWith( "file://" ) )
68 OUString aSysPath;
69 if( osl_getSystemPathFromFileURL( aUserPath.pData, &aSysPath.pData ) == osl_File_E_None )
70 aUserPath = aSysPath;
72 // ensure user path exists
73 aUPath += "/user/psprint";
74 SAL_INFO("vcl.fonts", "Trying to create: " << aUPath);
75 osl_createDirectoryPath( aUPath.pData, nullptr, nullptr );
78 switch( ePath )
80 case whichOfficePath::ConfigPath: return aConfigPath;
81 case whichOfficePath::InstallationRootPath: return aInstallationRootPath;
82 case whichOfficePath::UserPath: return aUserPath;
84 return OUString();
87 static OString getEnvironmentPath( const char* pKey )
89 OString aPath;
91 const char* pValue = getenv( pKey );
92 if( pValue && *pValue )
94 aPath = OString( pValue );
96 return aPath;
99 } // namespace psp
101 void psp::getPrinterPathList( std::vector< OUString >& rPathList, const char* pSubDir )
103 rPathList.clear();
104 rtl_TextEncoding aEncoding = osl_getThreadTextEncoding();
106 OUStringBuffer aPathBuffer( 256 );
108 // append net path
109 aPathBuffer.append( getOfficePath( whichOfficePath::InstallationRootPath ) );
110 if( !aPathBuffer.isEmpty() )
112 aPathBuffer.append( "/" LIBO_SHARE_FOLDER "/psprint" );
113 if( pSubDir )
115 aPathBuffer.append( '/' );
116 aPathBuffer.appendAscii( pSubDir );
118 rPathList.push_back( aPathBuffer.makeStringAndClear() );
120 // append user path
121 aPathBuffer.append( getOfficePath( whichOfficePath::UserPath ) );
122 if( !aPathBuffer.isEmpty() )
124 aPathBuffer.append( "/user/psprint" );
125 if( pSubDir )
127 aPathBuffer.append( '/' );
128 aPathBuffer.appendAscii( pSubDir );
130 rPathList.push_back( aPathBuffer.makeStringAndClear() );
133 OString aPath( getEnvironmentPath("SAL_PSPRINT") );
134 sal_Int32 nIndex = 0;
137 OString aDir( aPath.getToken( 0, ':', nIndex ) );
138 if( aDir.isEmpty() )
139 continue;
141 if( pSubDir )
143 aDir += OStringLiteral("/") + pSubDir;
145 struct stat aStat;
146 if( stat( aDir.getStr(), &aStat ) || ! S_ISDIR( aStat.st_mode ) )
147 continue;
149 rPathList.push_back( OStringToOUString( aDir, aEncoding ) );
150 } while( nIndex != -1 );
152 #ifdef SYSTEM_PPD_DIR
153 if( pSubDir && rtl_str_compare( pSubDir, PRINTER_PPDDIR ) == 0 )
155 rPathList.push_back( OStringToOUString( OString( SYSTEM_PPD_DIR ), RTL_TEXTENCODING_UTF8 ) );
157 #endif
159 if( rPathList.empty() )
161 // last resort: next to program file (mainly for setup)
162 OUString aExe;
163 if( osl_getExecutableFile( &aExe.pData ) == osl_Process_E_None )
165 INetURLObject aDir( aExe );
166 aDir.removeSegment();
167 aExe = aDir.GetMainURL( INetURLObject::DecodeMechanism::NONE );
168 OUString aSysPath;
169 if( osl_getSystemPathFromFileURL( aExe.pData, &aSysPath.pData ) == osl_File_E_None )
171 rPathList.push_back( aSysPath );
177 OUString const & psp::getFontPath()
179 static OUString aPath;
181 if (aPath.isEmpty())
183 OUStringBuffer aPathBuffer( 512 );
185 OUString aConfigPath( getOfficePath( whichOfficePath::ConfigPath ) );
186 OUString aInstallationRootPath( getOfficePath( whichOfficePath::InstallationRootPath ) );
187 OUString aUserPath( getOfficePath( whichOfficePath::UserPath ) );
188 if( !aConfigPath.isEmpty() )
190 // #i53530# Path from CustomDataUrl will completely
191 // replace net and user paths if the path exists
192 aPathBuffer.append(aConfigPath);
193 aPathBuffer.append("/" LIBO_SHARE_FOLDER "/fonts");
194 // check existence of config path
195 struct stat aStat;
196 if( 0 != stat( OUStringToOString( aPathBuffer.makeStringAndClear(), osl_getThreadTextEncoding() ).getStr(), &aStat )
197 || ! S_ISDIR( aStat.st_mode ) )
198 aConfigPath.clear();
199 else
201 aPathBuffer.append(aConfigPath);
202 aPathBuffer.append("/" LIBO_SHARE_FOLDER "/fonts");
205 if( aConfigPath.isEmpty() )
207 if( !aInstallationRootPath.isEmpty() )
209 aPathBuffer.append( aInstallationRootPath );
210 aPathBuffer.append( "/" LIBO_SHARE_FOLDER "/fonts/truetype;");
212 if( !aUserPath.isEmpty() )
214 aPathBuffer.append( aUserPath );
215 aPathBuffer.append( "/user/fonts" );
219 aPath = aPathBuffer.makeStringAndClear();
220 SAL_INFO("vcl.fonts", "Initializing font path to: " << aPath);
222 return aPath;
225 void psp::normPath( OString& rPath )
227 char buf[PATH_MAX];
229 // double slashes and slash at end are probably
230 // removed by realpath anyway, but since this runs
231 // on many different platforms let's play it safe
232 OString aPath = rPath.replaceAll("//", "/");
234 if( aPath.endsWith("/") )
235 aPath = aPath.copy(0, aPath.getLength()-1);
237 if( ( aPath.indexOf("./") != -1 ||
238 aPath.indexOf( '~' ) != -1 )
239 && realpath( aPath.getStr(), buf ) )
241 rPath = buf;
243 else
245 rPath = aPath;
249 void psp::splitPath( OString& rPath, OString& rDir, OString& rBase )
251 normPath( rPath );
252 sal_Int32 nIndex = rPath.lastIndexOf( '/' );
253 if( nIndex > 0 )
254 rDir = rPath.copy( 0, nIndex );
255 else if( nIndex == 0 ) // root dir
256 rDir = rPath.copy( 0, 1 );
257 if( rPath.getLength() > nIndex+1 )
258 rBase = rPath.copy( nIndex+1 );
261 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */