1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: helper.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_vcl.hxx"
39 #include "vcl/helper.hxx"
40 #include "vcl/ppdparser.hxx"
41 #include "tools/string.hxx"
42 #include "tools/urlobj.hxx"
43 #include "osl/file.hxx"
44 #include "osl/process.h"
45 #include "rtl/bootstrap.hxx"
51 OUString
getOfficePath( enum whichOfficePath ePath
)
53 static OUString aNetPath
;
54 static OUString aUserPath
;
55 static OUString aConfigPath
;
56 static OUString aEmpty
;
57 static bool bOnce
= false;
63 Bootstrap::get( OUString( RTL_CONSTASCII_USTRINGPARAM( "BRAND_BASE_DIR" ) ), aIni
);
64 aIni
+= OUString( RTL_CONSTASCII_USTRINGPARAM( "/program/" SAL_CONFIGFILE( "bootstrap" ) ) );
65 Bootstrap
aBootstrap( aIni
);
66 aBootstrap
.getFrom( OUString( RTL_CONSTASCII_USTRINGPARAM( "CustomDataUrl" ) ), aConfigPath
);
67 aBootstrap
.getFrom( OUString( RTL_CONSTASCII_USTRINGPARAM( "BaseInstallation" ) ), aNetPath
);
68 aBootstrap
.getFrom( OUString( RTL_CONSTASCII_USTRINGPARAM( "UserInstallation" ) ), aUserPath
);
69 OUString aUPath
= aUserPath
;
71 if( ! aConfigPath
.compareToAscii( "file://", 7 ) )
74 if( osl_getSystemPathFromFileURL( aConfigPath
.pData
, &aSysPath
.pData
) == osl_File_E_None
)
75 aConfigPath
= aSysPath
;
77 if( ! aNetPath
.compareToAscii( "file://", 7 ) )
80 if( osl_getSystemPathFromFileURL( aNetPath
.pData
, &aSysPath
.pData
) == osl_File_E_None
)
83 if( ! aUserPath
.compareToAscii( "file://", 7 ) )
86 if( osl_getSystemPathFromFileURL( aUserPath
.pData
, &aSysPath
.pData
) == osl_File_E_None
)
89 // ensure user path exists
90 aUPath
+= OUString( RTL_CONSTASCII_USTRINGPARAM( "/user/psprint" ) );
91 #if OSL_DEBUG_LEVEL > 1
94 osl_createDirectoryPath( aUPath
.pData
, NULL
, NULL
);
95 #if OSL_DEBUG_LEVEL > 1
96 fprintf( stderr
, "try to create \"%s\" = %d\n", OUStringToOString( aUPath
, RTL_TEXTENCODING_UTF8
).getStr(), eErr
);
102 case ConfigPath
: return aConfigPath
;
103 case NetPath
: return aNetPath
;
104 case UserPath
: return aUserPath
;
109 static OString
getEnvironmentPath( const char* pKey
)
113 const char* pValue
= getenv( pKey
);
114 if( pValue
&& *pValue
)
116 aPath
= OString( pValue
);
123 void psp::getPrinterPathList( std::list
< OUString
>& rPathList
, const char* pSubDir
)
126 rtl_TextEncoding aEncoding
= osl_getThreadTextEncoding();
128 OUStringBuffer
aPathBuffer( 256 );
131 aPathBuffer
.append( getOfficePath( psp::NetPath
) );
132 if( aPathBuffer
.getLength() )
134 aPathBuffer
.appendAscii( "/share/psprint" );
137 aPathBuffer
.append( sal_Unicode('/') );
138 aPathBuffer
.appendAscii( pSubDir
);
140 rPathList
.push_back( aPathBuffer
.makeStringAndClear() );
143 aPathBuffer
.append( getOfficePath( psp::UserPath
) );
144 if( aPathBuffer
.getLength() )
146 aPathBuffer
.appendAscii( "/user/psprint" );
149 aPathBuffer
.append( sal_Unicode('/') );
150 aPathBuffer
.appendAscii( pSubDir
);
152 rPathList
.push_back( aPathBuffer
.makeStringAndClear() );
155 OString
aPath( getEnvironmentPath("SAL_PSPRINT") );
156 sal_Int32 nIndex
= 0;
159 OString
aDir( aPath
.getToken( 0, ':', nIndex
) );
160 if( ! aDir
.getLength() )
169 if( stat( aDir
.getStr(), &aStat
) || ! S_ISDIR( aStat
.st_mode
) )
172 rPathList
.push_back( OStringToOUString( aDir
, aEncoding
) );
173 } while( nIndex
!= -1 );
175 #ifdef SYSTEM_PPD_DIR
176 if( pSubDir
&& rtl_str_compare( pSubDir
, PRINTER_PPDDIR
) == 0 )
178 rPathList
.push_back( rtl::OStringToOUString( rtl::OString( SYSTEM_PPD_DIR
), RTL_TEXTENCODING_UTF8
) );
182 if( rPathList
.empty() )
184 // last resort: next to program file (mainly for setup)
186 if( osl_getExecutableFile( &aExe
.pData
) == osl_Process_E_None
)
188 INetURLObject
aDir( aExe
);
189 aDir
.removeSegment();
190 aExe
= aDir
.GetMainURL( INetURLObject::NO_DECODE
);
192 if( osl_getSystemPathFromFileURL( aExe
.pData
, &aSysPath
.pData
) == osl_File_E_None
)
194 rPathList
.push_back( aSysPath
);
200 OUString
psp::getFontPath()
202 static OUString aPath
;
204 if( ! aPath
.getLength() )
206 OUStringBuffer
aPathBuffer( 512 );
208 OUString
aConfigPath( getOfficePath( psp::ConfigPath
) );
209 OUString
aNetPath( getOfficePath( psp::NetPath
) );
210 OUString
aUserPath( getOfficePath( psp::UserPath
) );
211 if( aConfigPath
.getLength() )
213 // #i53530# Path from CustomDataUrl will completely
214 // replace net and user paths if the path exists
215 aPathBuffer
.append(aConfigPath
);
216 aPathBuffer
.appendAscii("/share/fonts");
217 // check existance of config path
219 if( 0 != stat( OUStringToOString( aPathBuffer
.makeStringAndClear(), osl_getThreadTextEncoding() ).getStr(), &aStat
)
220 || ! S_ISDIR( aStat
.st_mode
) )
221 aConfigPath
= OUString();
224 aPathBuffer
.append(aConfigPath
);
225 aPathBuffer
.appendAscii("/share/fonts");
228 if( aConfigPath
.getLength() == 0 )
230 if( aNetPath
.getLength() )
232 aPathBuffer
.append( aNetPath
);
233 aPathBuffer
.appendAscii( "/share/fonts/truetype;");
234 aPathBuffer
.append( aNetPath
);
235 aPathBuffer
.appendAscii( "/share/fonts/type1;" );
237 if( aUserPath
.getLength() )
239 aPathBuffer
.append( aUserPath
);
240 aPathBuffer
.appendAscii( "/user/fonts" );
243 OString
aEnvPath( getEnvironmentPath( "SAL_FONTPATH_PRIVATE" ) );
244 if( aEnvPath
.getLength() )
246 aPathBuffer
.append( sal_Unicode(';') );
247 aPathBuffer
.append( OStringToOUString( aEnvPath
, osl_getThreadTextEncoding() ) );
250 aPath
= aPathBuffer
.makeStringAndClear();
251 #if OSL_DEBUG_LEVEL > 1
252 fprintf( stderr
, "initializing font path to \"%s\"\n", OUStringToOString( aPath
, RTL_TEXTENCODING_ISO_8859_1
).getStr() );
258 bool psp::convertPfbToPfa( ::osl::File
& rInFile
, ::osl::File
& rOutFile
)
260 static unsigned char hexDigits
[] =
262 '0', '1', '2', '3', '4', '5', '6', '7',
263 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
266 bool bSuccess
= true;
268 unsigned char buffer
[256];
270 sal_uInt64 nOrgPos
= 0;
271 rInFile
.getPos( nOrgPos
);
273 while( bSuccess
&& ! bEof
)
275 // read leading bytes
276 bEof
= ! rInFile
.read( buffer
, 6, nRead
) && nRead
== 6 ? false : true;
277 unsigned int nType
= buffer
[ 1 ];
278 unsigned int nBytesToRead
= buffer
[2] | buffer
[3] << 8 | buffer
[4] << 16 | buffer
[5] << 24;
279 if( buffer
[0] != 0x80 ) // test for pfb m_agic number
281 // this migt be a pfa font already
282 sal_uInt64 nWrite
= 0;
283 if( ! rInFile
.read( buffer
+6, 9, nRead
) && nRead
== 9 &&
284 ( ! std::strncmp( (char*)buffer
, "%!FontType1-", 12 ) ||
285 ! std::strncmp( (char*)buffer
, "%!PS-AdobeFont-", 15 ) ) )
287 if( rOutFile
.write( buffer
, 15, nWrite
) || nWrite
!= 15 )
290 ! rInFile
.read( buffer
, sizeof( buffer
), nRead
) &&
293 if( rOutFile
.write( buffer
, nRead
, nWrite
) ||
302 else if( nType
== 1 || nType
== 2 )
304 unsigned char* pBuffer
= new unsigned char[ nBytesToRead
+1 ];
306 if( ! rInFile
.read( pBuffer
, nBytesToRead
, nRead
) && nRead
== nBytesToRead
)
310 // ascii data, convert dos lineends( \r\n ) and
311 // m_ac lineends( \r ) to \n
312 unsigned char * pWriteBuffer
= new unsigned char[ nBytesToRead
];
313 unsigned int nBytesToWrite
= 0;
314 for( unsigned int i
= 0; i
< nBytesToRead
; i
++ )
316 if( pBuffer
[i
] != '\r' )
317 pWriteBuffer
[ nBytesToWrite
++ ] = pBuffer
[i
];
318 else if( pBuffer
[ i
+1 ] == '\n' )
321 pWriteBuffer
[ nBytesToWrite
++ ] = '\n';
324 pWriteBuffer
[ nBytesToWrite
++ ] = '\n';
326 if( rOutFile
.write( pWriteBuffer
, nBytesToWrite
, nRead
) || nRead
!= nBytesToWrite
)
329 delete [] pWriteBuffer
;
334 unsigned int nBuffer
= 0;
335 for( unsigned int i
= 0; i
< nBytesToRead
&& bSuccess
; i
++ )
337 buffer
[ nBuffer
++ ] = hexDigits
[ pBuffer
[ i
] >> 4 ];
338 buffer
[ nBuffer
++ ] = hexDigits
[ pBuffer
[ i
] & 15 ];
341 buffer
[ nBuffer
++ ] = '\n';
342 if( rOutFile
.write( buffer
, nBuffer
, nRead
) || nRead
!= nBuffer
)
347 if( nBuffer
> 0 && bSuccess
)
349 buffer
[ nBuffer
++ ] = '\n';
350 if( rOutFile
.write( buffer
, nBuffer
, nRead
) || nRead
!= nBuffer
)
360 else if( nType
== 3 )
369 void psp::normPath( OString
& rPath
)
373 ByteString
aPath( rPath
);
375 // double slashes and slash at end are probably
376 // removed by realpath anyway, but since this runs
377 // on many different platforms let's play it safe
378 while( aPath
.SearchAndReplace( "//", "/" ) != STRING_NOTFOUND
)
380 if( aPath
.Len() > 0 && aPath
.GetChar( aPath
.Len()-1 ) == '/' )
381 aPath
.Erase( aPath
.Len()-1 );
383 if( ( aPath
.Search( "./" ) != STRING_NOTFOUND
||
384 aPath
.Search( "~" ) != STRING_NOTFOUND
)
385 && realpath( aPath
.GetBuffer(), buf
) )
395 void psp::splitPath( OString
& rPath
, OString
& rDir
, OString
& rBase
)
398 sal_Int32 nIndex
= rPath
.lastIndexOf( '/' );
400 rDir
= rPath
.copy( 0, nIndex
);
401 else if( nIndex
== 0 ) // root dir
402 rDir
= rPath
.copy( 0, 1 );
403 if( rPath
.getLength() > nIndex
+1 )
404 rBase
= rPath
.copy( nIndex
+1 );