build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / vcl / source / image / ImplImageTree.cxx
bloba8127a85a0a81825a18f26cbebdb416abafb8183
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 "sal/config.h"
24 #include "com/sun/star/container/XNameAccess.hpp"
25 #include "com/sun/star/io/XInputStream.hpp"
26 #include "com/sun/star/lang/Locale.hpp"
27 #include "com/sun/star/lang/XMultiServiceFactory.hpp"
28 #include "com/sun/star/packages/zip/ZipFileAccess.hpp"
29 #include "com/sun/star/uno/Exception.hpp"
30 #include "com/sun/star/uno/RuntimeException.hpp"
31 #include "com/sun/star/uno/Sequence.hxx"
32 #include "comphelper/processfactory.hxx"
33 #include "osl/file.hxx"
34 #include "osl/diagnose.h"
35 #include "rtl/bootstrap.hxx"
36 #include "rtl/uri.hxx"
38 #include "tools/stream.hxx"
39 #include "tools/urlobj.hxx"
40 #include "implimagetree.hxx"
42 #include <vcl/bitmapex.hxx>
43 #include <vcl/dibtools.hxx>
44 #include <vcl/pngread.hxx>
45 #include <vcl/settings.hxx>
46 #include <vcl/svapp.hxx>
48 #include <vcl/BitmapTools.hxx>
49 #include <vcl/pngwrite.hxx>
51 #include "BitmapProcessor.hxx"
53 bool ImageRequestParameters::convertToDarkTheme()
55 static bool bIconsForDarkTheme = !!getenv("VCL_ICONS_FOR_DARK_THEME");
57 bool bConvertToDarkTheme = false;
58 if (!(meFlags & ImageLoadFlags::IgnoreDarkTheme))
59 bConvertToDarkTheme = bIconsForDarkTheme;
61 return bConvertToDarkTheme;
64 sal_Int32 ImageRequestParameters::scalePercentage()
66 sal_Int32 aScalePercentage = 100;
67 if (!(meFlags & ImageLoadFlags::IgnoreScalingFactor))
68 aScalePercentage = Application::GetDefaultDevice()->GetDPIScalePercentage();
69 return aScalePercentage;
72 namespace
75 OUString convertLcTo32Path(OUString const & rPath)
77 OUString aResult;
78 if (rPath.lastIndexOf('/') != -1)
80 sal_Int32 nCopyFrom = rPath.lastIndexOf('/') + 1;
81 OUString sFile = rPath.copy(nCopyFrom);
82 OUString sDir = rPath.copy(0, rPath.lastIndexOf('/'));
83 if (!sFile.isEmpty() && sFile.startsWith("lc_"))
85 aResult = sDir + "/32/" + sFile.copy(3);
88 return aResult;
91 OUString createPath(OUString const & name, sal_Int32 pos, OUString const & locale)
93 return name.copy(0, pos + 1) + locale + name.copy(pos);
96 OUString getIconThemeFolderUrl()
98 OUString sUrl("$BRAND_BASE_DIR/" LIBO_SHARE_FOLDER "/config/");
99 rtl::Bootstrap::expandMacros(sUrl);
100 return sUrl;
103 OUString getIconCacheUrl(OUString const & sStyle, OUString const & sVariant, OUString const & sName)
105 OUString sUrl("${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE("bootstrap") ":UserInstallation}/cache/");
106 sUrl += sStyle + "/" + sVariant + "/" + sName;
107 rtl::Bootstrap::expandMacros(sUrl);
108 return sUrl;
111 OUString createIconCacheUrl(OUString const & sStyle, OUString const & sVariant, OUString const & sName)
113 OUString sUrl(getIconCacheUrl(sStyle, sVariant, sName));
114 OUString sDir = sUrl.copy(0, sUrl.lastIndexOf('/'));
115 osl::Directory::createPath(sDir);
116 return sUrl;
119 bool urlExists(OUString const & sUrl)
121 osl::File aFile(sUrl);
122 osl::FileBase::RC eRC = aFile.open(osl_File_OpenFlag_Read);
123 if (osl::FileBase::E_None == eRC)
124 return true;
125 return false;
128 OUString getNameNoExtension(OUString const & sName)
130 sal_Int32 nDotPosition = sName.lastIndexOf('.');
131 return sName.copy(0, nDotPosition);
134 std::shared_ptr<SvStream> wrapStream(css::uno::Reference< css::io::XInputStream > const & stream)
136 // This could use SvInputStream instead if that did not have a broken
137 // SeekPos implementation for an XInputStream that is not also XSeekable
138 // (cf. "@@@" at tags/DEV300_m37/svtools/source/misc1/strmadpt.cxx@264807
139 // l. 593):
140 OSL_ASSERT(stream.is());
141 std::shared_ptr<SvStream> s(std::make_shared<SvMemoryStream>());
142 for (;;)
144 sal_Int32 const size = 2048;
145 css::uno::Sequence< sal_Int8 > data(size);
146 sal_Int32 n = stream->readBytes(data, size);
147 s->WriteBytes(data.getConstArray(), n);
148 if (n < size)
149 break;
151 s->Seek(0);
152 return s;
155 void loadImageFromStream(std::shared_ptr<SvStream> const & xStream, OUString const & rPath, ImageRequestParameters& rParameters)
157 bool bConvertToDarkTheme = rParameters.convertToDarkTheme();
158 sal_Int32 aScalePercentage = rParameters.scalePercentage();
160 if (rPath.endsWith(".png"))
162 vcl::PNGReader aPNGReader(*xStream);
163 aPNGReader.SetIgnoreGammaChunk(true);
164 rParameters.mrBitmap = aPNGReader.Read();
166 else if (rPath.endsWith(".svg"))
168 vcl::bitmap::loadFromSvg(*xStream.get(), rPath, rParameters.mrBitmap, aScalePercentage / 100.0);
169 if (bConvertToDarkTheme)
170 rParameters.mrBitmap = BitmapProcessor::createLightImage(rParameters.mrBitmap);
171 return;
173 else
175 ReadDIBBitmapEx(rParameters.mrBitmap, *xStream);
178 if (bConvertToDarkTheme)
179 rParameters.mrBitmap = BitmapProcessor::createLightImage(rParameters.mrBitmap);
181 if (aScalePercentage > 100)
183 double aScaleFactor(aScalePercentage / 100.0);
184 rParameters.mrBitmap.Scale(aScaleFactor, aScaleFactor, BmpScaleFlag::Fast);
188 } // end anonymous namespace
190 ImplImageTree::ImplImageTree()
194 ImplImageTree::~ImplImageTree()
198 std::vector<OUString> ImplImageTree::getPaths(OUString const & name, LanguageTag& rLanguageTag)
200 std::vector<OUString> sPaths;
202 sal_Int32 pos = name.lastIndexOf('/');
203 if (pos != -1)
205 for (OUString& rFallback : rLanguageTag.getFallbackStrings(true))
207 OUString aFallbackName = getNameNoExtension(getRealImageName(createPath(name, pos, rFallback)));
208 sPaths.push_back(aFallbackName + ".png");
209 sPaths.push_back(aFallbackName + ".svg");
213 OUString aRealName = getNameNoExtension(getRealImageName(name));
214 sPaths.push_back(aRealName + ".png");
215 sPaths.push_back(aRealName + ".svg");
217 return sPaths;
220 OUString ImplImageTree::getImageUrl(OUString const & rName, OUString const & rStyle, OUString const & rLang)
222 OUString aStyle(rStyle);
224 while (!aStyle.isEmpty())
228 setStyle(aStyle);
230 if (checkPathAccess())
232 IconSet& rIconSet = getCurrentIconSet();
233 const css::uno::Reference<css::container::XNameAccess>& rNameAccess = rIconSet.maNameAccess;
235 LanguageTag aLanguageTag(rLang);
237 for (OUString& rPath: getPaths(rName, aLanguageTag))
239 if (rNameAccess->hasByName(rPath))
241 return "vnd.sun.star.zip://"
242 + rtl::Uri::encode(rIconSet.maURL, rtl_UriCharClassRegName,
243 rtl_UriEncodeIgnoreEscapes, RTL_TEXTENCODING_UTF8)
244 + "/" + rPath;
249 catch (const css::uno::Exception & e)
251 SAL_INFO("vcl", "exception " << e.Message);
254 aStyle = fallbackStyle(aStyle);
256 return OUString();
259 OUString ImplImageTree::fallbackStyle(const OUString& rsStyle)
261 OUString sResult;
263 if (rsStyle == "galaxy")
264 sResult = "";
265 else if (rsStyle == "industrial" || rsStyle == "tango" || rsStyle == "breeze")
266 sResult = "galaxy";
267 else if (rsStyle == "sifr" || rsStyle == "breeze_dark")
268 sResult = "breeze";
269 else
270 sResult = "tango";
272 return sResult;
275 bool ImplImageTree::loadImage(OUString const & name, OUString const & style, BitmapEx & rBitmap, bool localized, const ImageLoadFlags eFlags)
277 OUString aStyle(style);
278 while (!aStyle.isEmpty())
282 ImageRequestParameters aParameters(name, aStyle, rBitmap, localized, eFlags);
283 if (doLoadImage(aParameters))
284 return true;
286 catch (css::uno::RuntimeException &)
289 aStyle = fallbackStyle(aStyle);
291 return false;
294 bool ImplImageTree::loadDefaultImage(OUString const & style, BitmapEx& bitmap, const ImageLoadFlags eFlags)
296 ImageRequestParameters aParameters("res/grafikde.png", style, bitmap, false, eFlags);
297 return doLoadImage(aParameters);
300 OUString createVariant(ImageRequestParameters& rParameters)
302 bool bConvertToDarkTheme = rParameters.convertToDarkTheme();
303 sal_Int32 aScalePercentage = rParameters.scalePercentage();
305 OUString aVariant;
306 if (aScalePercentage == 100 && !bConvertToDarkTheme)
307 return aVariant;
309 aVariant = OUString::number(aScalePercentage);
311 if (bConvertToDarkTheme)
312 aVariant += "-dark";
314 return aVariant;
317 bool loadDiskCachedVersion(OUString const & sVariant, ImageRequestParameters& rParameters)
319 OUString sUrl(getIconCacheUrl(rParameters.msStyle, sVariant, rParameters.msName));
320 if (!urlExists(sUrl))
321 return false;
322 SvFileStream aFileStream(sUrl, StreamMode::READ);
323 vcl::PNGReader aPNGReader(aFileStream);
324 aPNGReader.SetIgnoreGammaChunk( true );
325 rParameters.mrBitmap = aPNGReader.Read();
326 return true;
329 void cacheBitmapToDisk(OUString const & sVariant, ImageRequestParameters& rParameters)
331 OUString sUrl(createIconCacheUrl(rParameters.msStyle, sVariant, rParameters.msName));
332 vcl::PNGWriter aWriter(rParameters.mrBitmap);
335 SvFileStream aStream(sUrl, StreamMode::WRITE);
336 aWriter.Write(aStream);
337 aStream.Close();
339 catch (...)
343 bool ImplImageTree::doLoadImage(ImageRequestParameters& rParameters)
345 setStyle(rParameters.msStyle);
347 if (iconCacheLookup(rParameters))
348 return true;
350 if (!rParameters.mrBitmap.IsEmpty())
351 rParameters.mrBitmap.SetEmpty();
353 LanguageTag aLanguageTag = Application::GetSettings().GetUILanguageTag();
355 std::vector<OUString> paths = getPaths(rParameters.msName, aLanguageTag);
357 bool bFound = false;
361 bFound = findImage(paths, rParameters);
363 catch (css::uno::RuntimeException&)
365 throw;
367 catch (const css::uno::Exception& e)
369 SAL_INFO("vcl", "ImplImageTree::doLoadImage exception " << e.Message);
372 if (bFound)
374 OUString aVariant = createVariant(rParameters);
375 if (!aVariant.isEmpty())
376 cacheBitmapToDisk(aVariant, rParameters);
377 getCurrentIconSet().maIconCache[rParameters.msName] = std::make_pair(rParameters.mbLocalized, rParameters.mrBitmap);
380 return bFound;
383 void ImplImageTree::shutdown()
385 maCurrentStyle.clear();
386 maIconSets.clear();
389 void ImplImageTree::setStyle(OUString const & style)
391 assert(!style.isEmpty());
392 if (style != maCurrentStyle)
394 maCurrentStyle = style;
395 createStyle();
399 void ImplImageTree::createStyle()
401 if (maIconSets.find(maCurrentStyle) != maIconSets.end())
402 return;
404 OUString sThemeUrl;
406 if (maCurrentStyle != "default")
408 INetURLObject aUrl(getIconThemeFolderUrl());
409 OSL_ASSERT(!aUrl.HasError());
411 bool ok = aUrl.Append("images_" + maCurrentStyle, INetURLObject::EncodeMechanism::All);
412 OSL_ASSERT(ok); (void) ok;
413 sThemeUrl = aUrl.GetMainURL(INetURLObject::DecodeMechanism::NONE) + ".zip";
416 else
417 sThemeUrl += "images";
419 if (!urlExists(sThemeUrl))
420 return;
422 maIconSets[maCurrentStyle] = IconSet(sThemeUrl);
424 loadImageLinks();
427 bool ImplImageTree::iconCacheLookup(ImageRequestParameters& rParameters)
429 IconCache& rIconCache = getCurrentIconSet().maIconCache;
431 IconCache::iterator i(rIconCache.find(getRealImageName(rParameters.msName)));
432 if (i != rIconCache.end() && i->second.first == rParameters.mbLocalized)
434 rParameters.mrBitmap = i->second.second;
435 return true;
438 OUString aVariant = createVariant(rParameters);
439 if (!aVariant.isEmpty() && loadDiskCachedVersion(aVariant, rParameters))
440 return true;
442 return false;
445 bool ImplImageTree::findImage(std::vector<OUString> const & paths, ImageRequestParameters& rParameters)
447 if (!checkPathAccess())
448 return false;
450 const css::uno::Reference<css::container::XNameAccess>& rNameAccess = getCurrentIconSet().maNameAccess;
452 for (const OUString& rPath : paths)
454 if (rNameAccess->hasByName(rPath))
456 css::uno::Reference<css::io::XInputStream> aStream;
457 bool ok = rNameAccess->getByName(rPath) >>= aStream;
458 assert(ok);
459 (void)ok; // prevent unused warning in release build
461 loadImageFromStream(wrapStream(aStream), rPath, rParameters);
462 return true;
465 return false;
468 void ImplImageTree::loadImageLinks()
470 const OUString aLinkFilename("links.txt");
472 if (!checkPathAccess())
473 return;
475 const css::uno::Reference<css::container::XNameAccess> &rNameAccess = getCurrentIconSet().maNameAccess;
477 if (rNameAccess->hasByName(aLinkFilename))
479 css::uno::Reference< css::io::XInputStream > s;
480 bool ok = rNameAccess->getByName(aLinkFilename) >>= s;
481 assert(ok);
482 (void)ok; // prevent unused warning in release build
484 parseLinkFile( wrapStream(s) );
485 return;
489 void ImplImageTree::parseLinkFile(std::shared_ptr<SvStream> const & xStream)
491 OString aLine;
492 OUString aLink, aOriginal;
493 int nLineNo = 0;
494 while (xStream->ReadLine(aLine))
496 ++nLineNo;
497 if ( aLine.isEmpty() )
498 continue;
500 sal_Int32 nIndex = 0;
501 aLink = OStringToOUString( aLine.getToken(0, ' ', nIndex), RTL_TEXTENCODING_UTF8 );
502 aOriginal = OStringToOUString( aLine.getToken(0, ' ', nIndex), RTL_TEXTENCODING_UTF8 );
504 // skip comments, or incomplete entries
505 if (aLink.isEmpty() || aLink[0] == '#' || aOriginal.isEmpty())
507 if (aLink.isEmpty() || aOriginal.isEmpty())
508 SAL_WARN("vcl", "ImplImageTree::parseLinkFile: icon links.txt parse error, incomplete link at line " << nLineNo);
509 continue;
512 getCurrentIconSet().maLinkHash[aLink] = aOriginal;
514 OUString aOriginal32 = convertLcTo32Path(aOriginal);
515 OUString aLink32 = convertLcTo32Path(aLink);
517 if (!aOriginal32.isEmpty() && !aLink32.isEmpty())
518 getCurrentIconSet().maLinkHash[aLink32] = aOriginal32;
522 OUString const & ImplImageTree::getRealImageName(OUString const & name)
524 IconLinkHash &rLinkHash = maIconSets[maCurrentStyle].maLinkHash;
526 IconLinkHash::iterator it(rLinkHash.find(name));
527 if (it == rLinkHash.end())
528 return name;
530 return it->second;
533 bool ImplImageTree::checkPathAccess()
535 IconSet& rIconSet = getCurrentIconSet();
536 css::uno::Reference<css::container::XNameAccess> &rNameAccess = rIconSet.maNameAccess;
537 if (rNameAccess.is())
538 return true;
542 rNameAccess = css::packages::zip::ZipFileAccess::createWithURL(comphelper::getProcessComponentContext(), rIconSet.maURL);
544 catch (const css::uno::RuntimeException &) {
545 throw;
547 catch (const css::uno::Exception & e) {
548 SAL_INFO("vcl", "ImplImageTree::zip file location exception " << e.Message << " for " << rIconSet.maURL);
549 return false;
551 return rNameAccess.is();
554 css::uno::Reference<css::container::XNameAccess> ImplImageTree::getNameAccess()
556 checkPathAccess();
557 return getCurrentIconSet().maNameAccess;
560 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */