1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ***********************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #include "precompiled_vcl.hxx" 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir #include "sal/config.h" 31*cdf0e10cSrcweir 32*cdf0e10cSrcweir #include <list> 33*cdf0e10cSrcweir #include <memory> 34*cdf0e10cSrcweir #include <utility> 35*cdf0e10cSrcweir #include <vector> 36*cdf0e10cSrcweir #include <hash_map> 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir #include "com/sun/star/container/XNameAccess.hpp" 39*cdf0e10cSrcweir #include "com/sun/star/io/XInputStream.hpp" 40*cdf0e10cSrcweir #include "com/sun/star/lang/Locale.hpp" 41*cdf0e10cSrcweir #include "com/sun/star/uno/Any.hxx" 42*cdf0e10cSrcweir #include "com/sun/star/uno/Exception.hpp" 43*cdf0e10cSrcweir #include "com/sun/star/uno/Reference.hxx" 44*cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp" 45*cdf0e10cSrcweir #include "com/sun/star/uno/Sequence.hxx" 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir #include "comphelper/processfactory.hxx" 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir #include "osl/file.hxx" 50*cdf0e10cSrcweir #include "osl/diagnose.h" 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir #include "rtl/bootstrap.hxx" 53*cdf0e10cSrcweir #include "rtl/string.h" 54*cdf0e10cSrcweir #include "rtl/textenc.h" 55*cdf0e10cSrcweir #include "rtl/ustrbuf.hxx" 56*cdf0e10cSrcweir #include "rtl/ustring.h" 57*cdf0e10cSrcweir #include "rtl/ustring.hxx" 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir #include "sal/types.h" 60*cdf0e10cSrcweir 61*cdf0e10cSrcweir #include "tools/stream.hxx" 62*cdf0e10cSrcweir #include "tools/urlobj.hxx" 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir #include "vcl/bitmapex.hxx" 65*cdf0e10cSrcweir #include "vcl/pngread.hxx" 66*cdf0e10cSrcweir #include "vcl/settings.hxx" 67*cdf0e10cSrcweir #include "vcl/svapp.hxx" 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir #include "impimagetree.hxx" 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir namespace { 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir namespace css = com::sun::star; 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir rtl::OUString createPath( 76*cdf0e10cSrcweir rtl::OUString const & name, sal_Int32 pos, rtl::OUString const & locale) 77*cdf0e10cSrcweir { 78*cdf0e10cSrcweir rtl::OUStringBuffer b(name.copy(0, pos + 1)); 79*cdf0e10cSrcweir b.append(locale); 80*cdf0e10cSrcweir b.append(name.copy(pos)); 81*cdf0e10cSrcweir return b.makeStringAndClear(); 82*cdf0e10cSrcweir } 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir std::auto_ptr< SvStream > wrapStream( 85*cdf0e10cSrcweir css::uno::Reference< css::io::XInputStream > const & stream) 86*cdf0e10cSrcweir { 87*cdf0e10cSrcweir // This could use SvInputStream instead if that did not have a broken 88*cdf0e10cSrcweir // SeekPos implementation for an XInputStream that is not also XSeekable 89*cdf0e10cSrcweir // (cf. "@@@" at tags/DEV300_m37/svtools/source/misc1/strmadpt.cxx@264807 90*cdf0e10cSrcweir // l. 593): 91*cdf0e10cSrcweir OSL_ASSERT(stream.is()); 92*cdf0e10cSrcweir std::auto_ptr< SvStream > s(new SvMemoryStream); 93*cdf0e10cSrcweir for (;;) { 94*cdf0e10cSrcweir css::uno::Sequence< sal_Int8 > data; 95*cdf0e10cSrcweir sal_Int32 const size = 30000; 96*cdf0e10cSrcweir sal_Int32 n = stream->readBytes(data, size); 97*cdf0e10cSrcweir s->Write(data.getConstArray(), n); 98*cdf0e10cSrcweir if (n < size) { 99*cdf0e10cSrcweir break; 100*cdf0e10cSrcweir } 101*cdf0e10cSrcweir } 102*cdf0e10cSrcweir s->Seek(0); 103*cdf0e10cSrcweir return s; 104*cdf0e10cSrcweir } 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir void loadFromStream( 107*cdf0e10cSrcweir css::uno::Reference< css::io::XInputStream > const & stream, 108*cdf0e10cSrcweir rtl::OUString const & path, BitmapEx & bitmap) 109*cdf0e10cSrcweir { 110*cdf0e10cSrcweir std::auto_ptr< SvStream > s(wrapStream(stream)); 111*cdf0e10cSrcweir if (path.endsWithAsciiL(RTL_CONSTASCII_STRINGPARAM(".png"))) 112*cdf0e10cSrcweir { 113*cdf0e10cSrcweir vcl::PNGReader aPNGReader( *s ); 114*cdf0e10cSrcweir aPNGReader.SetIgnoreGammaChunk( sal_True ); 115*cdf0e10cSrcweir bitmap = aPNGReader.Read(); 116*cdf0e10cSrcweir } else { 117*cdf0e10cSrcweir *s >> bitmap; 118*cdf0e10cSrcweir } 119*cdf0e10cSrcweir } 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir } 122*cdf0e10cSrcweir 123*cdf0e10cSrcweir ImplImageTree::ImplImageTree() {} 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir ImplImageTree::~ImplImageTree() {} 126*cdf0e10cSrcweir 127*cdf0e10cSrcweir bool ImplImageTree::checkStyle(rtl::OUString const & style) 128*cdf0e10cSrcweir { 129*cdf0e10cSrcweir bool exists; 130*cdf0e10cSrcweir 131*cdf0e10cSrcweir // using cache because setStyle is an expensive operation 132*cdf0e10cSrcweir // setStyle calls resetZips => closes any opened zip files with icons, cleans the icon cache, ... 133*cdf0e10cSrcweir if (checkStyleCacheLookup(style, exists)) { 134*cdf0e10cSrcweir return exists; 135*cdf0e10cSrcweir } 136*cdf0e10cSrcweir 137*cdf0e10cSrcweir setStyle(style); 138*cdf0e10cSrcweir 139*cdf0e10cSrcweir exists = false; 140*cdf0e10cSrcweir const rtl::OUString sBrandURLSuffix(RTL_CONSTASCII_USTRINGPARAM("_brand.zip")); 141*cdf0e10cSrcweir for (Zips::iterator i(m_zips.begin()); i != m_zips.end() && !exists;) { 142*cdf0e10cSrcweir ::rtl::OUString aZipURL = i->first; 143*cdf0e10cSrcweir sal_Int32 nFromIndex = aZipURL.getLength() - sBrandURLSuffix.getLength(); 144*cdf0e10cSrcweir // skip brand-specific icon themes; they are incomplete and thus not useful for this check 145*cdf0e10cSrcweir if (nFromIndex < 0 || !aZipURL.match(sBrandURLSuffix, nFromIndex)) { 146*cdf0e10cSrcweir osl::File aZip(aZipURL); 147*cdf0e10cSrcweir if (aZip.open(OpenFlag_Read) == ::osl::FileBase::E_None) { 148*cdf0e10cSrcweir aZip.close(); 149*cdf0e10cSrcweir exists = true; 150*cdf0e10cSrcweir } 151*cdf0e10cSrcweir } 152*cdf0e10cSrcweir ++i; 153*cdf0e10cSrcweir } 154*cdf0e10cSrcweir m_checkStyleCache[style] = exists; 155*cdf0e10cSrcweir return exists; 156*cdf0e10cSrcweir } 157*cdf0e10cSrcweir 158*cdf0e10cSrcweir bool ImplImageTree::loadImage( 159*cdf0e10cSrcweir rtl::OUString const & name, rtl::OUString const & style, BitmapEx & bitmap, 160*cdf0e10cSrcweir bool localized) 161*cdf0e10cSrcweir { 162*cdf0e10cSrcweir setStyle(style); 163*cdf0e10cSrcweir if (iconCacheLookup(name, localized, bitmap)) { 164*cdf0e10cSrcweir return true; 165*cdf0e10cSrcweir } 166*cdf0e10cSrcweir if (!bitmap.IsEmpty()) { 167*cdf0e10cSrcweir bitmap.SetEmpty(); 168*cdf0e10cSrcweir } 169*cdf0e10cSrcweir std::vector< rtl::OUString > paths; 170*cdf0e10cSrcweir paths.push_back(name); 171*cdf0e10cSrcweir if (localized) { 172*cdf0e10cSrcweir sal_Int32 pos = name.lastIndexOf('/'); 173*cdf0e10cSrcweir if (pos != -1) { 174*cdf0e10cSrcweir css::lang::Locale const & loc = 175*cdf0e10cSrcweir Application::GetSettings().GetUILocale(); 176*cdf0e10cSrcweir paths.push_back(createPath(name, pos, loc.Language)); 177*cdf0e10cSrcweir if (loc.Country.getLength() != 0) { 178*cdf0e10cSrcweir rtl::OUStringBuffer b(loc.Language); 179*cdf0e10cSrcweir b.append(sal_Unicode('-')); 180*cdf0e10cSrcweir b.append(loc.Country); 181*cdf0e10cSrcweir rtl::OUString p(createPath(name, pos, b.makeStringAndClear())); 182*cdf0e10cSrcweir paths.push_back(p); 183*cdf0e10cSrcweir if (loc.Variant.getLength() != 0) { 184*cdf0e10cSrcweir b.append(p); 185*cdf0e10cSrcweir b.append(sal_Unicode('-')); 186*cdf0e10cSrcweir b.append(loc.Variant); 187*cdf0e10cSrcweir paths.push_back( 188*cdf0e10cSrcweir createPath(name, pos, b.makeStringAndClear())); 189*cdf0e10cSrcweir } 190*cdf0e10cSrcweir } 191*cdf0e10cSrcweir } 192*cdf0e10cSrcweir } 193*cdf0e10cSrcweir bool found = false; 194*cdf0e10cSrcweir try { 195*cdf0e10cSrcweir found = find(paths, bitmap); 196*cdf0e10cSrcweir } catch (css::uno::RuntimeException &) { 197*cdf0e10cSrcweir throw; 198*cdf0e10cSrcweir } catch (css::uno::Exception & e) { 199*cdf0e10cSrcweir OSL_TRACE( 200*cdf0e10cSrcweir "ImplImageTree::loadImage exception \"%s\"", 201*cdf0e10cSrcweir rtl::OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8).getStr()); 202*cdf0e10cSrcweir } 203*cdf0e10cSrcweir if (found) { 204*cdf0e10cSrcweir m_iconCache[name.intern()] = std::make_pair(localized, bitmap); 205*cdf0e10cSrcweir } 206*cdf0e10cSrcweir return found; 207*cdf0e10cSrcweir } 208*cdf0e10cSrcweir 209*cdf0e10cSrcweir void ImplImageTree::shutDown() { 210*cdf0e10cSrcweir m_style = rtl::OUString(); 211*cdf0e10cSrcweir // for safety; empty m_style means "not initialized" 212*cdf0e10cSrcweir m_zips.clear(); 213*cdf0e10cSrcweir m_iconCache.clear(); 214*cdf0e10cSrcweir m_checkStyleCache.clear(); 215*cdf0e10cSrcweir } 216*cdf0e10cSrcweir 217*cdf0e10cSrcweir void ImplImageTree::setStyle(rtl::OUString const & style) { 218*cdf0e10cSrcweir OSL_ASSERT(style.getLength() != 0); // empty m_style means "not initialized" 219*cdf0e10cSrcweir if (style != m_style) { 220*cdf0e10cSrcweir m_style = style; 221*cdf0e10cSrcweir resetZips(); 222*cdf0e10cSrcweir m_iconCache.clear(); 223*cdf0e10cSrcweir } 224*cdf0e10cSrcweir } 225*cdf0e10cSrcweir 226*cdf0e10cSrcweir void ImplImageTree::resetZips() { 227*cdf0e10cSrcweir m_zips.clear(); 228*cdf0e10cSrcweir { 229*cdf0e10cSrcweir rtl::OUString url( 230*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM("$BRAND_BASE_DIR/program/edition/images.zip")); 231*cdf0e10cSrcweir rtl::Bootstrap::expandMacros(url); 232*cdf0e10cSrcweir INetURLObject u(url); 233*cdf0e10cSrcweir OSL_ASSERT(!u.HasError()); 234*cdf0e10cSrcweir m_zips.push_back( 235*cdf0e10cSrcweir std::make_pair( 236*cdf0e10cSrcweir u.GetMainURL(INetURLObject::NO_DECODE), 237*cdf0e10cSrcweir css::uno::Reference< css::container::XNameAccess >())); 238*cdf0e10cSrcweir } 239*cdf0e10cSrcweir { 240*cdf0e10cSrcweir rtl::OUString url( 241*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM("$BRAND_BASE_DIR/share/config")); 242*cdf0e10cSrcweir rtl::Bootstrap::expandMacros(url); 243*cdf0e10cSrcweir INetURLObject u(url); 244*cdf0e10cSrcweir OSL_ASSERT(!u.HasError()); 245*cdf0e10cSrcweir rtl::OUStringBuffer b; 246*cdf0e10cSrcweir b.appendAscii(RTL_CONSTASCII_STRINGPARAM("images_")); 247*cdf0e10cSrcweir b.append(m_style); 248*cdf0e10cSrcweir b.appendAscii(RTL_CONSTASCII_STRINGPARAM("_brand.zip")); 249*cdf0e10cSrcweir bool ok = u.Append(b.makeStringAndClear(), INetURLObject::ENCODE_ALL); 250*cdf0e10cSrcweir OSL_ASSERT(ok); (void) ok; 251*cdf0e10cSrcweir m_zips.push_back( 252*cdf0e10cSrcweir std::make_pair( 253*cdf0e10cSrcweir u.GetMainURL(INetURLObject::NO_DECODE), 254*cdf0e10cSrcweir css::uno::Reference< css::container::XNameAccess >())); 255*cdf0e10cSrcweir } 256*cdf0e10cSrcweir { 257*cdf0e10cSrcweir rtl::OUString url( 258*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM( 259*cdf0e10cSrcweir "$BRAND_BASE_DIR/share/config/images_brand.zip")); 260*cdf0e10cSrcweir rtl::Bootstrap::expandMacros(url); 261*cdf0e10cSrcweir m_zips.push_back( 262*cdf0e10cSrcweir std::make_pair( 263*cdf0e10cSrcweir url, css::uno::Reference< css::container::XNameAccess >())); 264*cdf0e10cSrcweir } 265*cdf0e10cSrcweir { 266*cdf0e10cSrcweir rtl::OUString url( 267*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM("$OOO_BASE_DIR/share/config")); 268*cdf0e10cSrcweir rtl::Bootstrap::expandMacros(url); 269*cdf0e10cSrcweir INetURLObject u(url); 270*cdf0e10cSrcweir OSL_ASSERT(!u.HasError()); 271*cdf0e10cSrcweir rtl::OUStringBuffer b; 272*cdf0e10cSrcweir b.appendAscii(RTL_CONSTASCII_STRINGPARAM("images_")); 273*cdf0e10cSrcweir b.append(m_style); 274*cdf0e10cSrcweir b.appendAscii(RTL_CONSTASCII_STRINGPARAM(".zip")); 275*cdf0e10cSrcweir bool ok = u.Append(b.makeStringAndClear(), INetURLObject::ENCODE_ALL); 276*cdf0e10cSrcweir OSL_ASSERT(ok); (void) ok; 277*cdf0e10cSrcweir m_zips.push_back( 278*cdf0e10cSrcweir std::make_pair( 279*cdf0e10cSrcweir u.GetMainURL(INetURLObject::NO_DECODE), 280*cdf0e10cSrcweir css::uno::Reference< css::container::XNameAccess >())); 281*cdf0e10cSrcweir } 282*cdf0e10cSrcweir if ( m_style.equals(::rtl::OUString::createFromAscii("default")) ) 283*cdf0e10cSrcweir { 284*cdf0e10cSrcweir rtl::OUString url( 285*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM( 286*cdf0e10cSrcweir "$OOO_BASE_DIR/share/config/images.zip")); 287*cdf0e10cSrcweir rtl::Bootstrap::expandMacros(url); 288*cdf0e10cSrcweir m_zips.push_back( 289*cdf0e10cSrcweir std::make_pair( 290*cdf0e10cSrcweir url, css::uno::Reference< css::container::XNameAccess >())); 291*cdf0e10cSrcweir } 292*cdf0e10cSrcweir } 293*cdf0e10cSrcweir 294*cdf0e10cSrcweir bool ImplImageTree::checkStyleCacheLookup( 295*cdf0e10cSrcweir rtl::OUString const & style, bool &exists) 296*cdf0e10cSrcweir { 297*cdf0e10cSrcweir CheckStyleCache::iterator i(m_checkStyleCache.find(style)); 298*cdf0e10cSrcweir if (i != m_checkStyleCache.end()) { 299*cdf0e10cSrcweir exists = i->second; 300*cdf0e10cSrcweir return true; 301*cdf0e10cSrcweir } else { 302*cdf0e10cSrcweir return false; 303*cdf0e10cSrcweir } 304*cdf0e10cSrcweir } 305*cdf0e10cSrcweir 306*cdf0e10cSrcweir bool ImplImageTree::iconCacheLookup( 307*cdf0e10cSrcweir rtl::OUString const & name, bool localized, BitmapEx & bitmap) 308*cdf0e10cSrcweir { 309*cdf0e10cSrcweir IconCache::iterator i(m_iconCache.find(name)); 310*cdf0e10cSrcweir if (i != m_iconCache.end() && i->second.first == localized) { 311*cdf0e10cSrcweir bitmap = i->second.second; 312*cdf0e10cSrcweir return true; 313*cdf0e10cSrcweir } else { 314*cdf0e10cSrcweir return false; 315*cdf0e10cSrcweir } 316*cdf0e10cSrcweir } 317*cdf0e10cSrcweir 318*cdf0e10cSrcweir bool ImplImageTree::find( 319*cdf0e10cSrcweir std::vector< rtl::OUString > const & paths, BitmapEx & bitmap) 320*cdf0e10cSrcweir { 321*cdf0e10cSrcweir for (Zips::iterator i(m_zips.begin()); i != m_zips.end();) { 322*cdf0e10cSrcweir if (!i->second.is()) { 323*cdf0e10cSrcweir css::uno::Sequence< css::uno::Any > args(1); 324*cdf0e10cSrcweir args[0] <<= i->first; 325*cdf0e10cSrcweir try { 326*cdf0e10cSrcweir i->second.set( 327*cdf0e10cSrcweir comphelper::createProcessComponentWithArguments( 328*cdf0e10cSrcweir rtl::OUString( 329*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM( 330*cdf0e10cSrcweir "com.sun.star.packages.zip.ZipFileAccess")), 331*cdf0e10cSrcweir args), 332*cdf0e10cSrcweir css::uno::UNO_QUERY_THROW); 333*cdf0e10cSrcweir } catch (css::uno::RuntimeException &) { 334*cdf0e10cSrcweir throw; 335*cdf0e10cSrcweir } catch (css::uno::Exception & e) { 336*cdf0e10cSrcweir OSL_TRACE( 337*cdf0e10cSrcweir "ImplImageTree::find exception \"%s\"", 338*cdf0e10cSrcweir rtl::OUStringToOString( 339*cdf0e10cSrcweir e.Message, RTL_TEXTENCODING_UTF8).getStr()); 340*cdf0e10cSrcweir i = m_zips.erase(i); 341*cdf0e10cSrcweir continue; 342*cdf0e10cSrcweir } 343*cdf0e10cSrcweir } 344*cdf0e10cSrcweir for (std::vector< rtl::OUString >::const_reverse_iterator j( 345*cdf0e10cSrcweir paths.rbegin()); 346*cdf0e10cSrcweir j != paths.rend(); ++j) 347*cdf0e10cSrcweir { 348*cdf0e10cSrcweir if (i->second->hasByName(*j)) { 349*cdf0e10cSrcweir css::uno::Reference< css::io::XInputStream > s; 350*cdf0e10cSrcweir bool ok = i->second->getByName(*j) >>= s; 351*cdf0e10cSrcweir OSL_ASSERT(ok); (void) ok; 352*cdf0e10cSrcweir loadFromStream(s, *j, bitmap); 353*cdf0e10cSrcweir return true; 354*cdf0e10cSrcweir } 355*cdf0e10cSrcweir } 356*cdf0e10cSrcweir ++i; 357*cdf0e10cSrcweir } 358*cdf0e10cSrcweir return false; 359*cdf0e10cSrcweir } 360