1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
26 #include "vcl/helper.hxx"
27 #include "vcl/ppdparser.hxx"
28 #include "tools/string.hxx"
29 #include "tools/urlobj.hxx"
30 #include "osl/file.hxx"
31 #include "osl/process.h"
32 #include "rtl/bootstrap.hxx"
34 using ::rtl::Bootstrap
;
35 using ::rtl::OUString
;
36 using ::rtl::OUStringBuffer
;
38 using ::rtl::OStringToOUString
;
39 using ::rtl::OUStringToOString
;
43 OUString
getOfficePath( enum whichOfficePath ePath
)
45 static OUString aInstallationRootPath
;
46 static OUString aUserPath
;
47 static OUString aConfigPath
;
48 static OUString aEmpty
;
49 static bool bOnce
= false;
55 Bootstrap::get( OUString( RTL_CONSTASCII_USTRINGPARAM( "BRAND_BASE_DIR" ) ), aInstallationRootPath
);
56 aIni
= aInstallationRootPath
+ OUString( RTL_CONSTASCII_USTRINGPARAM( "/program/" SAL_CONFIGFILE( "bootstrap" ) ) );
57 Bootstrap
aBootstrap( aIni
);
58 aBootstrap
.getFrom( OUString( RTL_CONSTASCII_USTRINGPARAM( "CustomDataUrl" ) ), aConfigPath
);
59 aBootstrap
.getFrom( OUString( RTL_CONSTASCII_USTRINGPARAM( "UserInstallation" ) ), aUserPath
);
60 OUString aUPath
= aUserPath
;
62 if( ! aConfigPath
.compareToAscii( "file://", 7 ) )
65 if( osl_getSystemPathFromFileURL( aConfigPath
.pData
, &aSysPath
.pData
) == osl_File_E_None
)
66 aConfigPath
= aSysPath
;
68 if( ! aInstallationRootPath
.compareToAscii( "file://", 7 ) )
71 if( osl_getSystemPathFromFileURL( aInstallationRootPath
.pData
, &aSysPath
.pData
) == osl_File_E_None
)
72 aInstallationRootPath
= aSysPath
;
74 if( ! aUserPath
.compareToAscii( "file://", 7 ) )
77 if( osl_getSystemPathFromFileURL( aUserPath
.pData
, &aSysPath
.pData
) == osl_File_E_None
)
80 // ensure user path exists
81 aUPath
+= OUString( RTL_CONSTASCII_USTRINGPARAM( "/user/psprint" ) );
82 #if OSL_DEBUG_LEVEL > 1
85 osl_createDirectoryPath( aUPath
.pData
, NULL
, NULL
);
86 #if OSL_DEBUG_LEVEL > 1
87 fprintf( stderr
, "try to create \"%s\" = %d\n", OUStringToOString( aUPath
, RTL_TEXTENCODING_UTF8
).getStr(), eErr
);
93 case ConfigPath
: return aConfigPath
;
94 case InstallationRootPath
: return aInstallationRootPath
;
95 case UserPath
: return aUserPath
;
100 static OString
getEnvironmentPath( const char* pKey
)
104 const char* pValue
= getenv( pKey
);
105 if( pValue
&& *pValue
)
107 aPath
= OString( pValue
);
114 void psp::getPrinterPathList( std::list
< OUString
>& rPathList
, const char* pSubDir
)
117 rtl_TextEncoding aEncoding
= osl_getThreadTextEncoding();
119 OUStringBuffer
aPathBuffer( 256 );
122 aPathBuffer
.append( getOfficePath( psp::InstallationRootPath
) );
123 if( aPathBuffer
.getLength() )
125 aPathBuffer
.appendAscii( "/share/psprint" );
128 aPathBuffer
.append( sal_Unicode('/') );
129 aPathBuffer
.appendAscii( pSubDir
);
131 rPathList
.push_back( aPathBuffer
.makeStringAndClear() );
134 aPathBuffer
.append( getOfficePath( psp::UserPath
) );
135 if( aPathBuffer
.getLength() )
137 aPathBuffer
.appendAscii( "/user/psprint" );
140 aPathBuffer
.append( sal_Unicode('/') );
141 aPathBuffer
.appendAscii( pSubDir
);
143 rPathList
.push_back( aPathBuffer
.makeStringAndClear() );
146 OString
aPath( getEnvironmentPath("SAL_PSPRINT") );
147 sal_Int32 nIndex
= 0;
150 OString
aDir( aPath
.getToken( 0, ':', nIndex
) );
160 if( stat( aDir
.getStr(), &aStat
) || ! S_ISDIR( aStat
.st_mode
) )
163 rPathList
.push_back( OStringToOUString( aDir
, aEncoding
) );
164 } while( nIndex
!= -1 );
166 #ifdef SYSTEM_PPD_DIR
167 if( pSubDir
&& rtl_str_compare( pSubDir
, PRINTER_PPDDIR
) == 0 )
169 rPathList
.push_back( rtl::OStringToOUString( rtl::OString( SYSTEM_PPD_DIR
), RTL_TEXTENCODING_UTF8
) );
173 if( rPathList
.empty() )
175 // last resort: next to program file (mainly for setup)
177 if( osl_getExecutableFile( &aExe
.pData
) == osl_Process_E_None
)
179 INetURLObject
aDir( aExe
);
180 aDir
.removeSegment();
181 aExe
= aDir
.GetMainURL( INetURLObject::NO_DECODE
);
183 if( osl_getSystemPathFromFileURL( aExe
.pData
, &aSysPath
.pData
) == osl_File_E_None
)
185 rPathList
.push_back( aSysPath
);
191 OUString
psp::getFontPath()
193 static OUString aPath
;
197 OUStringBuffer
aPathBuffer( 512 );
199 OUString
aConfigPath( getOfficePath( psp::ConfigPath
) );
200 OUString
aInstallationRootPath( getOfficePath( psp::InstallationRootPath
) );
201 OUString
aUserPath( getOfficePath( psp::UserPath
) );
202 if( !aConfigPath
.isEmpty() )
204 // #i53530# Path from CustomDataUrl will completely
205 // replace net and user paths if the path exists
206 aPathBuffer
.append(aConfigPath
);
207 aPathBuffer
.appendAscii("/share/fonts");
208 // check existance of config path
210 if( 0 != stat( OUStringToOString( aPathBuffer
.makeStringAndClear(), osl_getThreadTextEncoding() ).getStr(), &aStat
)
211 || ! S_ISDIR( aStat
.st_mode
) )
212 aConfigPath
= OUString();
215 aPathBuffer
.append(aConfigPath
);
216 aPathBuffer
.appendAscii("/share/fonts");
219 if( aConfigPath
.isEmpty() )
221 if( !aInstallationRootPath
.isEmpty() )
223 aPathBuffer
.append( aInstallationRootPath
);
224 aPathBuffer
.appendAscii( "/share/fonts/truetype;");
225 aPathBuffer
.append( aInstallationRootPath
);
226 aPathBuffer
.appendAscii( "/share/fonts/type1;" );
228 if( !aUserPath
.isEmpty() )
230 aPathBuffer
.append( aUserPath
);
231 aPathBuffer
.appendAscii( "/user/fonts" );
235 aPath
= aPathBuffer
.makeStringAndClear();
236 #if OSL_DEBUG_LEVEL > 1
237 fprintf( stderr
, "initializing font path to \"%s\"\n", OUStringToOString( aPath
, RTL_TEXTENCODING_ISO_8859_1
).getStr() );
243 bool psp::convertPfbToPfa( ::osl::File
& rInFile
, ::osl::File
& rOutFile
)
245 static unsigned char hexDigits
[] =
247 '0', '1', '2', '3', '4', '5', '6', '7',
248 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
251 bool bSuccess
= true;
253 unsigned char buffer
[256];
255 sal_uInt64 nOrgPos
= 0;
256 rInFile
.getPos( nOrgPos
);
258 while( bSuccess
&& ! bEof
)
260 // read leading bytes
261 bEof
= ! rInFile
.read( buffer
, 6, nRead
) && nRead
== 6 ? false : true;
262 unsigned int nType
= buffer
[ 1 ];
263 unsigned int nBytesToRead
= buffer
[2] | buffer
[3] << 8 | buffer
[4] << 16 | buffer
[5] << 24;
264 if( buffer
[0] != 0x80 ) // test for pfb m_agic number
266 // this migt be a pfa font already
267 if( ! rInFile
.read( buffer
+6, 9, nRead
) && nRead
== 9 &&
268 ( ! std::strncmp( (char*)buffer
, "%!FontType1-", 12 ) ||
269 ! std::strncmp( (char*)buffer
, "%!PS-AdobeFont-", 15 ) ) )
271 sal_uInt64 nWrite
= 0;
272 if( rOutFile
.write( buffer
, 15, nWrite
) || nWrite
!= 15 )
275 ! rInFile
.read( buffer
, sizeof( buffer
), nRead
) &&
278 if( rOutFile
.write( buffer
, nRead
, nWrite
) ||
287 else if( nType
== 1 || nType
== 2 )
289 unsigned char* pBuffer
= new unsigned char[ nBytesToRead
+1 ];
291 if( ! rInFile
.read( pBuffer
, nBytesToRead
, nRead
) && nRead
== nBytesToRead
)
295 // ascii data, convert dos lineends( \r\n ) and
296 // m_ac lineends( \r ) to \n
297 unsigned char * pWriteBuffer
= new unsigned char[ nBytesToRead
];
298 unsigned int nBytesToWrite
= 0;
299 for( unsigned int i
= 0; i
< nBytesToRead
; i
++ )
301 if( pBuffer
[i
] != '\r' )
302 pWriteBuffer
[ nBytesToWrite
++ ] = pBuffer
[i
];
303 else if( pBuffer
[ i
+1 ] == '\n' )
306 pWriteBuffer
[ nBytesToWrite
++ ] = '\n';
309 pWriteBuffer
[ nBytesToWrite
++ ] = '\n';
311 if( rOutFile
.write( pWriteBuffer
, nBytesToWrite
, nRead
) || nRead
!= nBytesToWrite
)
314 delete [] pWriteBuffer
;
319 unsigned int nBuffer
= 0;
320 for( unsigned int i
= 0; i
< nBytesToRead
&& bSuccess
; i
++ )
322 buffer
[ nBuffer
++ ] = hexDigits
[ pBuffer
[ i
] >> 4 ];
323 buffer
[ nBuffer
++ ] = hexDigits
[ pBuffer
[ i
] & 15 ];
326 buffer
[ nBuffer
++ ] = '\n';
327 if( rOutFile
.write( buffer
, nBuffer
, nRead
) || nRead
!= nBuffer
)
332 if( nBuffer
> 0 && bSuccess
)
334 buffer
[ nBuffer
++ ] = '\n';
335 if( rOutFile
.write( buffer
, nBuffer
, nRead
) || nRead
!= nBuffer
)
345 else if( nType
== 3 )
354 void psp::normPath( OString
& rPath
)
358 // double slashes and slash at end are probably
359 // removed by realpath anyway, but since this runs
360 // on many different platforms let's play it safe
361 rtl::OString aPath
= rPath
.replaceAll("//", "/");
363 if( !aPath
.isEmpty() && aPath
[aPath
.getLength()-1] == '/' )
364 aPath
= aPath
.copy(0, aPath
.getLength()-1);
366 if( ( aPath
.indexOfL(RTL_CONSTASCII_STRINGPARAM("./")) != -1 ||
367 aPath
.indexOf( '~' ) != -1 )
368 && realpath( aPath
.getStr(), buf
) )
378 void psp::splitPath( OString
& rPath
, OString
& rDir
, OString
& rBase
)
381 sal_Int32 nIndex
= rPath
.lastIndexOf( '/' );
383 rDir
= rPath
.copy( 0, nIndex
);
384 else if( nIndex
== 0 ) // root dir
385 rDir
= rPath
.copy( 0, 1 );
386 if( rPath
.getLength() > nIndex
+1 )
387 rBase
= rPath
.copy( nIndex
+1 );
391 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */