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 .
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
;
75 OUString
convertLcTo32Path(OUString
const & rPath
)
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);
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
);
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
);
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
);
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
)
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
140 OSL_ASSERT(stream
.is());
141 std::shared_ptr
<SvStream
> s(std::make_shared
<SvMemoryStream
>());
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
);
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
);
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('/');
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");
220 OUString
ImplImageTree::getImageUrl(OUString
const & rName
, OUString
const & rStyle
, OUString
const & rLang
)
222 OUString
aStyle(rStyle
);
224 while (!aStyle
.isEmpty())
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
)
249 catch (const css::uno::Exception
& e
)
251 SAL_INFO("vcl", "exception " << e
.Message
);
254 aStyle
= fallbackStyle(aStyle
);
259 OUString
ImplImageTree::fallbackStyle(const OUString
& rsStyle
)
263 if (rsStyle
== "galaxy")
265 else if (rsStyle
== "industrial" || rsStyle
== "tango" || rsStyle
== "breeze")
267 else if (rsStyle
== "sifr" || rsStyle
== "breeze_dark")
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
))
286 catch (css::uno::RuntimeException
&)
289 aStyle
= fallbackStyle(aStyle
);
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();
306 if (aScalePercentage
== 100 && !bConvertToDarkTheme
)
309 aVariant
= OUString::number(aScalePercentage
);
311 if (bConvertToDarkTheme
)
317 bool loadDiskCachedVersion(OUString
const & sVariant
, ImageRequestParameters
& rParameters
)
319 OUString
sUrl(getIconCacheUrl(rParameters
.msStyle
, sVariant
, rParameters
.msName
));
320 if (!urlExists(sUrl
))
322 SvFileStream
aFileStream(sUrl
, StreamMode::READ
);
323 vcl::PNGReader
aPNGReader(aFileStream
);
324 aPNGReader
.SetIgnoreGammaChunk( true );
325 rParameters
.mrBitmap
= aPNGReader
.Read();
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
);
343 bool ImplImageTree::doLoadImage(ImageRequestParameters
& rParameters
)
345 setStyle(rParameters
.msStyle
);
347 if (iconCacheLookup(rParameters
))
350 if (!rParameters
.mrBitmap
.IsEmpty())
351 rParameters
.mrBitmap
.SetEmpty();
353 LanguageTag aLanguageTag
= Application::GetSettings().GetUILanguageTag();
355 std::vector
<OUString
> paths
= getPaths(rParameters
.msName
, aLanguageTag
);
361 bFound
= findImage(paths
, rParameters
);
363 catch (css::uno::RuntimeException
&)
367 catch (const css::uno::Exception
& e
)
369 SAL_INFO("vcl", "ImplImageTree::doLoadImage exception " << e
.Message
);
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
);
383 void ImplImageTree::shutdown()
385 maCurrentStyle
.clear();
389 void ImplImageTree::setStyle(OUString
const & style
)
391 assert(!style
.isEmpty());
392 if (style
!= maCurrentStyle
)
394 maCurrentStyle
= style
;
399 void ImplImageTree::createStyle()
401 if (maIconSets
.find(maCurrentStyle
) != maIconSets
.end())
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";
417 sThemeUrl
+= "images";
419 if (!urlExists(sThemeUrl
))
422 maIconSets
[maCurrentStyle
] = IconSet(sThemeUrl
);
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
;
438 OUString aVariant
= createVariant(rParameters
);
439 if (!aVariant
.isEmpty() && loadDiskCachedVersion(aVariant
, rParameters
))
445 bool ImplImageTree::findImage(std::vector
<OUString
> const & paths
, ImageRequestParameters
& rParameters
)
447 if (!checkPathAccess())
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
;
459 (void)ok
; // prevent unused warning in release build
461 loadImageFromStream(wrapStream(aStream
), rPath
, rParameters
);
468 void ImplImageTree::loadImageLinks()
470 const OUString
aLinkFilename("links.txt");
472 if (!checkPathAccess())
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
;
482 (void)ok
; // prevent unused warning in release build
484 parseLinkFile( wrapStream(s
) );
489 void ImplImageTree::parseLinkFile(std::shared_ptr
<SvStream
> const & xStream
)
492 OUString aLink
, aOriginal
;
494 while (xStream
->ReadLine(aLine
))
497 if ( aLine
.isEmpty() )
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
);
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())
533 bool ImplImageTree::checkPathAccess()
535 IconSet
& rIconSet
= getCurrentIconSet();
536 css::uno::Reference
<css::container::XNameAccess
> &rNameAccess
= rIconSet
.maNameAccess
;
537 if (rNameAccess
.is())
542 rNameAccess
= css::packages::zip::ZipFileAccess::createWithURL(comphelper::getProcessComponentContext(), rIconSet
.maURL
);
544 catch (const css::uno::RuntimeException
&) {
547 catch (const css::uno::Exception
& e
) {
548 SAL_INFO("vcl", "ImplImageTree::zip file location exception " << e
.Message
<< " for " << rIconSet
.maURL
);
551 return rNameAccess
.is();
554 css::uno::Reference
<css::container::XNameAccess
> ImplImageTree::getNameAccess()
557 return getCurrentIconSet().maNameAccess
;
560 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */