/src/libreoffice/vcl/unx/generic/fontmanager/helper.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
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/. |
8 | | * |
9 | | * This file incorporates work covered by the following license notice: |
10 | | * |
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 . |
18 | | */ |
19 | | |
20 | | #include <config_folders.h> |
21 | | |
22 | | #include <sys/stat.h> |
23 | | #include <limits.h> |
24 | | #include <osl/file.hxx> |
25 | | #include <osl/process.h> |
26 | | #include <osl/thread.h> |
27 | | #include <rtl/bootstrap.hxx> |
28 | | #include <rtl/ustring.hxx> |
29 | | #include <sal/log.hxx> |
30 | | #include <tools/urlobj.hxx> |
31 | | #include <unx/helper.hxx> |
32 | | |
33 | | #include <tuple> |
34 | | |
35 | | using ::rtl::Bootstrap; |
36 | | |
37 | | namespace psp { |
38 | | |
39 | | OUString getOfficePath( whichOfficePath ePath ) |
40 | 324 | { |
41 | 324 | static const auto aPaths = [] { |
42 | 108 | OUString sRoot, sUser, sConfig; |
43 | 108 | Bootstrap::get(u"BRAND_BASE_DIR"_ustr, sRoot); |
44 | 108 | Bootstrap aBootstrap(sRoot + "/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE("bootstrap")); |
45 | 108 | aBootstrap.getFrom(u"UserInstallation"_ustr, sUser); |
46 | 108 | aBootstrap.getFrom(u"CustomDataUrl"_ustr, sConfig); |
47 | 108 | OUString aUPath = sUser + "/user/psprint"; |
48 | 108 | if (sRoot.startsWith("file://")) |
49 | 108 | { |
50 | 108 | OUString aSysPath; |
51 | 108 | if (osl::FileBase::getSystemPathFromFileURL(sRoot, aSysPath) == osl::FileBase::E_None) |
52 | 108 | sRoot = aSysPath; |
53 | 108 | } |
54 | 108 | if (sUser.startsWith("file://")) |
55 | 0 | { |
56 | 0 | OUString aSysPath; |
57 | 0 | if (osl::FileBase::getSystemPathFromFileURL(sUser, aSysPath) == osl::FileBase::E_None) |
58 | 0 | sUser = aSysPath; |
59 | 0 | } |
60 | 108 | if (sConfig.startsWith("file://")) |
61 | 0 | { |
62 | 0 | OUString aSysPath; |
63 | 0 | if (osl::FileBase::getSystemPathFromFileURL(sConfig, aSysPath) == osl::FileBase::E_None) |
64 | 0 | sConfig = aSysPath; |
65 | 0 | } |
66 | | |
67 | | // ensure user path exists |
68 | 108 | SAL_INFO("vcl.fonts", "Trying to create: " << aUPath); |
69 | 108 | osl::Directory::createPath(aUPath); |
70 | | |
71 | 108 | return std::make_tuple(sRoot, sUser, sConfig); |
72 | 108 | }(); |
73 | 324 | const auto& [aInstallationRootPath, aUserPath, aConfigPath] = aPaths; |
74 | | |
75 | 324 | switch( ePath ) |
76 | 324 | { |
77 | 108 | case whichOfficePath::ConfigPath: return aConfigPath; |
78 | 108 | case whichOfficePath::InstallationRootPath: return aInstallationRootPath; |
79 | 108 | case whichOfficePath::UserPath: return aUserPath; |
80 | 324 | } |
81 | 0 | return OUString(); |
82 | 324 | } |
83 | | |
84 | | static OString getEnvironmentPath( const char* pKey ) |
85 | 0 | { |
86 | 0 | OString aPath; |
87 | |
|
88 | 0 | const char* pValue = getenv( pKey ); |
89 | 0 | if( pValue && *pValue ) |
90 | 0 | { |
91 | 0 | aPath = OString( pValue ); |
92 | 0 | } |
93 | 0 | return aPath; |
94 | 0 | } |
95 | | |
96 | | } // namespace psp |
97 | | |
98 | | void psp::getPrinterPathList( std::vector< OUString >& rPathList, const char* pSubDir ) |
99 | 0 | { |
100 | 0 | rPathList.clear(); |
101 | 0 | rtl_TextEncoding aEncoding = osl_getThreadTextEncoding(); |
102 | |
|
103 | 0 | OUStringBuffer aPathBuffer( 256 ); |
104 | | |
105 | | // append net path |
106 | 0 | aPathBuffer.append( getOfficePath( whichOfficePath::InstallationRootPath ) ); |
107 | 0 | if( !aPathBuffer.isEmpty() ) |
108 | 0 | { |
109 | 0 | aPathBuffer.append( "/" LIBO_SHARE_FOLDER "/psprint" ); |
110 | 0 | if( pSubDir ) |
111 | 0 | { |
112 | 0 | aPathBuffer.append( '/' ); |
113 | 0 | aPathBuffer.appendAscii( pSubDir ); |
114 | 0 | } |
115 | 0 | rPathList.push_back( aPathBuffer.makeStringAndClear() ); |
116 | 0 | } |
117 | | // append user path |
118 | 0 | aPathBuffer.append( getOfficePath( whichOfficePath::UserPath ) ); |
119 | 0 | if( !aPathBuffer.isEmpty() ) |
120 | 0 | { |
121 | 0 | aPathBuffer.append( "/user/psprint" ); |
122 | 0 | if( pSubDir ) |
123 | 0 | { |
124 | 0 | aPathBuffer.append( '/' ); |
125 | 0 | aPathBuffer.appendAscii( pSubDir ); |
126 | 0 | } |
127 | 0 | rPathList.push_back( aPathBuffer.makeStringAndClear() ); |
128 | 0 | } |
129 | |
|
130 | 0 | OString aPath( getEnvironmentPath("SAL_PSPRINT") ); |
131 | 0 | sal_Int32 nIndex = 0; |
132 | 0 | do |
133 | 0 | { |
134 | 0 | OString aDir( aPath.getToken( 0, ':', nIndex ) ); |
135 | 0 | if( aDir.isEmpty() ) |
136 | 0 | continue; |
137 | | |
138 | 0 | if( pSubDir ) |
139 | 0 | { |
140 | 0 | aDir += OString::Concat("/") + pSubDir; |
141 | 0 | } |
142 | 0 | struct stat aStat; |
143 | 0 | if( stat( aDir.getStr(), &aStat ) || ! S_ISDIR( aStat.st_mode ) ) |
144 | 0 | continue; |
145 | | |
146 | 0 | rPathList.push_back( OStringToOUString( aDir, aEncoding ) ); |
147 | 0 | } while( nIndex != -1 ); |
148 | |
|
149 | | #ifdef SYSTEM_PPD_DIR |
150 | | if( pSubDir && rtl_str_compare( pSubDir, PRINTER_PPDDIR ) == 0 ) |
151 | | { |
152 | | rPathList.push_back( OStringToOUString( OString( SYSTEM_PPD_DIR ), RTL_TEXTENCODING_UTF8 ) ); |
153 | | } |
154 | | #endif |
155 | |
|
156 | 0 | if( !rPathList.empty() ) |
157 | 0 | return; |
158 | | |
159 | | // last resort: next to program file (mainly for setup) |
160 | 0 | OUString aExe; |
161 | 0 | if( osl_getExecutableFile( &aExe.pData ) == osl_Process_E_None ) |
162 | 0 | { |
163 | 0 | INetURLObject aDir( aExe ); |
164 | 0 | aDir.removeSegment(); |
165 | 0 | aExe = aDir.GetMainURL( INetURLObject::DecodeMechanism::NONE ); |
166 | 0 | OUString aSysPath; |
167 | 0 | if( osl_getSystemPathFromFileURL( aExe.pData, &aSysPath.pData ) == osl_File_E_None ) |
168 | 0 | { |
169 | 0 | rPathList.push_back( aSysPath ); |
170 | 0 | } |
171 | 0 | } |
172 | 0 | } |
173 | | |
174 | | OUString const & psp::getFontPath() |
175 | 108 | { |
176 | 108 | static OUString aPath; |
177 | | |
178 | 108 | if (aPath.isEmpty()) |
179 | 108 | { |
180 | 108 | OUStringBuffer aPathBuffer( 512 ); |
181 | | |
182 | 108 | OUString aConfigPath( getOfficePath( whichOfficePath::ConfigPath ) ); |
183 | 108 | OUString aInstallationRootPath( getOfficePath( whichOfficePath::InstallationRootPath ) ); |
184 | 108 | OUString aUserPath( getOfficePath( whichOfficePath::UserPath ) ); |
185 | 108 | if (!aInstallationRootPath.isEmpty()) |
186 | 108 | { |
187 | | // internal font resources, required for normal operation, like OpenSymbol |
188 | 108 | aPathBuffer.append(aInstallationRootPath |
189 | 108 | + "/" LIBO_SHARE_RESOURCE_FOLDER "/common/fonts;"); |
190 | 108 | } |
191 | 108 | if( !aConfigPath.isEmpty() ) |
192 | 0 | { |
193 | | // #i53530# Path from CustomDataUrl will completely |
194 | | // replace net share and user paths if the path exists |
195 | 0 | OUString sPath = aConfigPath + "/" LIBO_SHARE_FOLDER "/fonts"; |
196 | | // check existence of config path |
197 | 0 | struct stat aStat; |
198 | 0 | if( 0 != stat( OUStringToOString( sPath, osl_getThreadTextEncoding() ).getStr(), &aStat ) |
199 | 0 | || ! S_ISDIR( aStat.st_mode ) ) |
200 | 0 | aConfigPath.clear(); |
201 | 0 | else |
202 | 0 | { |
203 | 0 | aPathBuffer.append(sPath); |
204 | 0 | } |
205 | 0 | } |
206 | 108 | if( aConfigPath.isEmpty() ) |
207 | 108 | { |
208 | 108 | if( !aInstallationRootPath.isEmpty() ) |
209 | 108 | { |
210 | 108 | aPathBuffer.append( aInstallationRootPath |
211 | 108 | + "/" LIBO_SHARE_FOLDER "/fonts/truetype;"); |
212 | 108 | } |
213 | 108 | if( !aUserPath.isEmpty() ) |
214 | 0 | { |
215 | 0 | aPathBuffer.append( aUserPath + "/user/fonts" ); |
216 | 0 | } |
217 | 108 | } |
218 | | |
219 | 108 | aPath = aPathBuffer.makeStringAndClear(); |
220 | 108 | SAL_INFO("vcl.fonts", "Initializing font path to: " << aPath); |
221 | 108 | } |
222 | 108 | return aPath; |
223 | 108 | } |
224 | | |
225 | | void psp::normPath( OString& rPath ) |
226 | 1.62k | { |
227 | 1.62k | char buf[PATH_MAX]; |
228 | | |
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 | 1.62k | OString aPath = rPath.replaceAll("//"_ostr, "/"_ostr); |
233 | | |
234 | 1.62k | if( aPath.endsWith("/") ) |
235 | 0 | aPath = aPath.copy(0, aPath.getLength()-1); |
236 | | |
237 | 1.62k | if( ( aPath.indexOf("./") != -1 || |
238 | 1.62k | aPath.indexOf( '~' ) != -1 ) |
239 | 0 | && realpath( aPath.getStr(), buf ) ) |
240 | 0 | { |
241 | 0 | rPath = buf; |
242 | 0 | } |
243 | 1.62k | else |
244 | 1.62k | { |
245 | 1.62k | rPath = aPath; |
246 | 1.62k | } |
247 | 1.62k | } |
248 | | |
249 | | void psp::splitPath( OString& rPath, OString& rDir, OString& rBase ) |
250 | 1.29k | { |
251 | 1.29k | normPath( rPath ); |
252 | 1.29k | sal_Int32 nIndex = rPath.lastIndexOf( '/' ); |
253 | 1.29k | if( nIndex > 0 ) |
254 | 1.29k | rDir = rPath.copy( 0, nIndex ); |
255 | 0 | else if( nIndex == 0 ) // root dir |
256 | 0 | rDir = rPath.copy( 0, 1 ); |
257 | 1.29k | if( rPath.getLength() > nIndex+1 ) |
258 | 1.29k | rBase = rPath.copy( nIndex+1 ); |
259 | 1.29k | } |
260 | | |
261 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |