19f62ea84SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
39f62ea84SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
49f62ea84SAndrew Rist * or more contributor license agreements. See the NOTICE file
59f62ea84SAndrew Rist * distributed with this work for additional information
69f62ea84SAndrew Rist * regarding copyright ownership. The ASF licenses this file
79f62ea84SAndrew Rist * to you under the Apache License, Version 2.0 (the
89f62ea84SAndrew Rist * "License"); you may not use this file except in compliance
99f62ea84SAndrew Rist * with the License. You may obtain a copy of the License at
109f62ea84SAndrew Rist *
119f62ea84SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
129f62ea84SAndrew Rist *
139f62ea84SAndrew Rist * Unless required by applicable law or agreed to in writing,
149f62ea84SAndrew Rist * software distributed under the License is distributed on an
159f62ea84SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169f62ea84SAndrew Rist * KIND, either express or implied. See the License for the
179f62ea84SAndrew Rist * specific language governing permissions and limitations
189f62ea84SAndrew Rist * under the License.
199f62ea84SAndrew Rist *
209f62ea84SAndrew Rist *************************************************************/
219f62ea84SAndrew Rist
22cdf0e10cSrcweir #include "precompiled_vcl.hxx"
23cdf0e10cSrcweir
24cdf0e10cSrcweir #include "sal/config.h"
25cdf0e10cSrcweir #include <list>
26cdf0e10cSrcweir #include <memory>
27cdf0e10cSrcweir #include <utility>
28cdf0e10cSrcweir #include <vector>
29cdf0e10cSrcweir #include <hash_map>
30cdf0e10cSrcweir #include "com/sun/star/container/XNameAccess.hpp"
31cdf0e10cSrcweir #include "com/sun/star/io/XInputStream.hpp"
32cdf0e10cSrcweir #include "com/sun/star/lang/Locale.hpp"
33cdf0e10cSrcweir #include "com/sun/star/uno/Any.hxx"
34cdf0e10cSrcweir #include "com/sun/star/uno/Exception.hpp"
35cdf0e10cSrcweir #include "com/sun/star/uno/Reference.hxx"
36cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp"
37cdf0e10cSrcweir #include "com/sun/star/uno/Sequence.hxx"
38cdf0e10cSrcweir #include "comphelper/processfactory.hxx"
39cdf0e10cSrcweir #include "osl/file.hxx"
40cdf0e10cSrcweir #include "osl/diagnose.h"
41cdf0e10cSrcweir #include "rtl/bootstrap.hxx"
42cdf0e10cSrcweir #include "rtl/string.h"
43cdf0e10cSrcweir #include "rtl/textenc.h"
44cdf0e10cSrcweir #include "rtl/ustrbuf.hxx"
45cdf0e10cSrcweir #include "rtl/ustring.h"
46cdf0e10cSrcweir #include "rtl/ustring.hxx"
47cdf0e10cSrcweir #include "sal/types.h"
48cdf0e10cSrcweir #include "tools/stream.hxx"
49cdf0e10cSrcweir #include "tools/urlobj.hxx"
50cdf0e10cSrcweir #include "vcl/bitmapex.hxx"
51cdf0e10cSrcweir #include "vcl/pngread.hxx"
52cdf0e10cSrcweir #include "vcl/settings.hxx"
53cdf0e10cSrcweir #include "vcl/svapp.hxx"
54cdf0e10cSrcweir #include "impimagetree.hxx"
5545fd3b9aSArmin Le Grand #include <vcl/dibtools.hxx>
56cdf0e10cSrcweir
57cdf0e10cSrcweir namespace {
58cdf0e10cSrcweir
59cdf0e10cSrcweir namespace css = com::sun::star;
60cdf0e10cSrcweir
createPath(rtl::OUString const & name,sal_Int32 pos,rtl::OUString const & locale)61cdf0e10cSrcweir rtl::OUString createPath(
62cdf0e10cSrcweir rtl::OUString const & name, sal_Int32 pos, rtl::OUString const & locale)
63cdf0e10cSrcweir {
64cdf0e10cSrcweir rtl::OUStringBuffer b(name.copy(0, pos + 1));
65cdf0e10cSrcweir b.append(locale);
66cdf0e10cSrcweir b.append(name.copy(pos));
67cdf0e10cSrcweir return b.makeStringAndClear();
68cdf0e10cSrcweir }
69cdf0e10cSrcweir
wrapStream(css::uno::Reference<css::io::XInputStream> const & stream)70cdf0e10cSrcweir std::auto_ptr< SvStream > wrapStream(
71cdf0e10cSrcweir css::uno::Reference< css::io::XInputStream > const & stream)
72cdf0e10cSrcweir {
73cdf0e10cSrcweir // This could use SvInputStream instead if that did not have a broken
74cdf0e10cSrcweir // SeekPos implementation for an XInputStream that is not also XSeekable
75cdf0e10cSrcweir // (cf. "@@@" at tags/DEV300_m37/svtools/source/misc1/strmadpt.cxx@264807
76cdf0e10cSrcweir // l. 593):
77cdf0e10cSrcweir OSL_ASSERT(stream.is());
78cdf0e10cSrcweir std::auto_ptr< SvStream > s(new SvMemoryStream);
79cdf0e10cSrcweir for (;;) {
80cdf0e10cSrcweir css::uno::Sequence< sal_Int8 > data;
81cdf0e10cSrcweir sal_Int32 const size = 30000;
82cdf0e10cSrcweir sal_Int32 n = stream->readBytes(data, size);
83cdf0e10cSrcweir s->Write(data.getConstArray(), n);
84cdf0e10cSrcweir if (n < size) {
85cdf0e10cSrcweir break;
86cdf0e10cSrcweir }
87cdf0e10cSrcweir }
88cdf0e10cSrcweir s->Seek(0);
89cdf0e10cSrcweir return s;
90cdf0e10cSrcweir }
91cdf0e10cSrcweir
loadFromStream(css::uno::Reference<css::io::XInputStream> const & stream,rtl::OUString const & path,BitmapEx & bitmap)92cdf0e10cSrcweir void loadFromStream(
93cdf0e10cSrcweir css::uno::Reference< css::io::XInputStream > const & stream,
94cdf0e10cSrcweir rtl::OUString const & path, BitmapEx & bitmap)
95cdf0e10cSrcweir {
96cdf0e10cSrcweir std::auto_ptr< SvStream > s(wrapStream(stream));
97cdf0e10cSrcweir if (path.endsWithAsciiL(RTL_CONSTASCII_STRINGPARAM(".png")))
98cdf0e10cSrcweir {
99cdf0e10cSrcweir vcl::PNGReader aPNGReader( *s );
100cdf0e10cSrcweir aPNGReader.SetIgnoreGammaChunk( sal_True );
101cdf0e10cSrcweir bitmap = aPNGReader.Read();
10245fd3b9aSArmin Le Grand }
10345fd3b9aSArmin Le Grand else
10445fd3b9aSArmin Le Grand {
10545fd3b9aSArmin Le Grand ReadDIBBitmapEx(bitmap, *s);
106cdf0e10cSrcweir }
107cdf0e10cSrcweir }
108cdf0e10cSrcweir
109cdf0e10cSrcweir }
110cdf0e10cSrcweir
ImplImageTree()111cdf0e10cSrcweir ImplImageTree::ImplImageTree() {}
112cdf0e10cSrcweir
~ImplImageTree()113cdf0e10cSrcweir ImplImageTree::~ImplImageTree() {}
114cdf0e10cSrcweir
checkStyle(rtl::OUString const & style)115cdf0e10cSrcweir bool ImplImageTree::checkStyle(rtl::OUString const & style)
116cdf0e10cSrcweir {
117cdf0e10cSrcweir bool exists;
118cdf0e10cSrcweir
119cdf0e10cSrcweir // using cache because setStyle is an expensive operation
120cdf0e10cSrcweir // setStyle calls resetZips => closes any opened zip files with icons, cleans the icon cache, ...
121cdf0e10cSrcweir if (checkStyleCacheLookup(style, exists)) {
122cdf0e10cSrcweir return exists;
123cdf0e10cSrcweir }
124cdf0e10cSrcweir
125cdf0e10cSrcweir setStyle(style);
126cdf0e10cSrcweir
127cdf0e10cSrcweir exists = false;
128cdf0e10cSrcweir const rtl::OUString sBrandURLSuffix(RTL_CONSTASCII_USTRINGPARAM("_brand.zip"));
129cdf0e10cSrcweir for (Zips::iterator i(m_zips.begin()); i != m_zips.end() && !exists;) {
130cdf0e10cSrcweir ::rtl::OUString aZipURL = i->first;
131cdf0e10cSrcweir sal_Int32 nFromIndex = aZipURL.getLength() - sBrandURLSuffix.getLength();
132cdf0e10cSrcweir // skip brand-specific icon themes; they are incomplete and thus not useful for this check
133cdf0e10cSrcweir if (nFromIndex < 0 || !aZipURL.match(sBrandURLSuffix, nFromIndex)) {
134cdf0e10cSrcweir osl::File aZip(aZipURL);
135cdf0e10cSrcweir if (aZip.open(OpenFlag_Read) == ::osl::FileBase::E_None) {
136cdf0e10cSrcweir aZip.close();
137cdf0e10cSrcweir exists = true;
138cdf0e10cSrcweir }
139cdf0e10cSrcweir }
140cdf0e10cSrcweir ++i;
141cdf0e10cSrcweir }
142cdf0e10cSrcweir m_checkStyleCache[style] = exists;
143cdf0e10cSrcweir return exists;
144cdf0e10cSrcweir }
145cdf0e10cSrcweir
loadImage(rtl::OUString const & name,rtl::OUString const & style,BitmapEx & bitmap,bool localized)146cdf0e10cSrcweir bool ImplImageTree::loadImage(
147cdf0e10cSrcweir rtl::OUString const & name, rtl::OUString const & style, BitmapEx & bitmap,
148cdf0e10cSrcweir bool localized)
149cdf0e10cSrcweir {
150cdf0e10cSrcweir setStyle(style);
151cdf0e10cSrcweir if (iconCacheLookup(name, localized, bitmap)) {
152cdf0e10cSrcweir return true;
153cdf0e10cSrcweir }
154cdf0e10cSrcweir if (!bitmap.IsEmpty()) {
155cdf0e10cSrcweir bitmap.SetEmpty();
156cdf0e10cSrcweir }
157cdf0e10cSrcweir std::vector< rtl::OUString > paths;
158cdf0e10cSrcweir paths.push_back(name);
159cdf0e10cSrcweir if (localized) {
160cdf0e10cSrcweir sal_Int32 pos = name.lastIndexOf('/');
161cdf0e10cSrcweir if (pos != -1) {
162cdf0e10cSrcweir css::lang::Locale const & loc =
163cdf0e10cSrcweir Application::GetSettings().GetUILocale();
164cdf0e10cSrcweir paths.push_back(createPath(name, pos, loc.Language));
165cdf0e10cSrcweir if (loc.Country.getLength() != 0) {
166cdf0e10cSrcweir rtl::OUStringBuffer b(loc.Language);
167cdf0e10cSrcweir b.append(sal_Unicode('-'));
168cdf0e10cSrcweir b.append(loc.Country);
169cdf0e10cSrcweir rtl::OUString p(createPath(name, pos, b.makeStringAndClear()));
170cdf0e10cSrcweir paths.push_back(p);
171cdf0e10cSrcweir if (loc.Variant.getLength() != 0) {
172cdf0e10cSrcweir b.append(p);
173cdf0e10cSrcweir b.append(sal_Unicode('-'));
174cdf0e10cSrcweir b.append(loc.Variant);
175cdf0e10cSrcweir paths.push_back(
176cdf0e10cSrcweir createPath(name, pos, b.makeStringAndClear()));
177cdf0e10cSrcweir }
178cdf0e10cSrcweir }
179cdf0e10cSrcweir }
180cdf0e10cSrcweir }
181cdf0e10cSrcweir bool found = false;
182cdf0e10cSrcweir try {
183cdf0e10cSrcweir found = find(paths, bitmap);
184cdf0e10cSrcweir } catch (css::uno::RuntimeException &) {
185cdf0e10cSrcweir throw;
186cdf0e10cSrcweir } catch (css::uno::Exception & e) {
187cdf0e10cSrcweir OSL_TRACE(
188cdf0e10cSrcweir "ImplImageTree::loadImage exception \"%s\"",
189cdf0e10cSrcweir rtl::OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8).getStr());
190cdf0e10cSrcweir }
191cdf0e10cSrcweir if (found) {
192cdf0e10cSrcweir m_iconCache[name.intern()] = std::make_pair(localized, bitmap);
193cdf0e10cSrcweir }
194cdf0e10cSrcweir return found;
195cdf0e10cSrcweir }
196cdf0e10cSrcweir
shutDown()197cdf0e10cSrcweir void ImplImageTree::shutDown() {
198cdf0e10cSrcweir m_style = rtl::OUString();
199cdf0e10cSrcweir // for safety; empty m_style means "not initialized"
200cdf0e10cSrcweir m_zips.clear();
201cdf0e10cSrcweir m_iconCache.clear();
202cdf0e10cSrcweir m_checkStyleCache.clear();
203cdf0e10cSrcweir }
204cdf0e10cSrcweir
setStyle(rtl::OUString const & style)205cdf0e10cSrcweir void ImplImageTree::setStyle(rtl::OUString const & style) {
206cdf0e10cSrcweir OSL_ASSERT(style.getLength() != 0); // empty m_style means "not initialized"
207cdf0e10cSrcweir if (style != m_style) {
208cdf0e10cSrcweir m_style = style;
209cdf0e10cSrcweir resetZips();
210cdf0e10cSrcweir m_iconCache.clear();
211cdf0e10cSrcweir }
212cdf0e10cSrcweir }
213cdf0e10cSrcweir
resetZips()214cdf0e10cSrcweir void ImplImageTree::resetZips() {
215cdf0e10cSrcweir m_zips.clear();
216cdf0e10cSrcweir {
217cdf0e10cSrcweir rtl::OUString url(
218*910823aeSJürgen Schmidt RTL_CONSTASCII_USTRINGPARAM("$OOO_BASE_DIR/program/edition/images.zip"));
219cdf0e10cSrcweir rtl::Bootstrap::expandMacros(url);
220cdf0e10cSrcweir INetURLObject u(url);
221cdf0e10cSrcweir OSL_ASSERT(!u.HasError());
222cdf0e10cSrcweir m_zips.push_back(
223cdf0e10cSrcweir std::make_pair(
224cdf0e10cSrcweir u.GetMainURL(INetURLObject::NO_DECODE),
225cdf0e10cSrcweir css::uno::Reference< css::container::XNameAccess >()));
226cdf0e10cSrcweir }
227cdf0e10cSrcweir {
228cdf0e10cSrcweir rtl::OUString url(
229*910823aeSJürgen Schmidt RTL_CONSTASCII_USTRINGPARAM("$OOO_BASE_DIR/share/config"));
230cdf0e10cSrcweir rtl::Bootstrap::expandMacros(url);
231cdf0e10cSrcweir INetURLObject u(url);
232cdf0e10cSrcweir OSL_ASSERT(!u.HasError());
233cdf0e10cSrcweir rtl::OUStringBuffer b;
234cdf0e10cSrcweir b.appendAscii(RTL_CONSTASCII_STRINGPARAM("images_"));
235cdf0e10cSrcweir b.append(m_style);
236cdf0e10cSrcweir b.appendAscii(RTL_CONSTASCII_STRINGPARAM("_brand.zip"));
237cdf0e10cSrcweir bool ok = u.Append(b.makeStringAndClear(), INetURLObject::ENCODE_ALL);
238cdf0e10cSrcweir OSL_ASSERT(ok); (void) ok;
239cdf0e10cSrcweir m_zips.push_back(
240cdf0e10cSrcweir std::make_pair(
241cdf0e10cSrcweir u.GetMainURL(INetURLObject::NO_DECODE),
242cdf0e10cSrcweir css::uno::Reference< css::container::XNameAccess >()));
243cdf0e10cSrcweir }
244cdf0e10cSrcweir {
245cdf0e10cSrcweir rtl::OUString url(
246cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM(
247*910823aeSJürgen Schmidt "$OOO_BASE_DIR/share/config/images_brand.zip"));
248cdf0e10cSrcweir rtl::Bootstrap::expandMacros(url);
249cdf0e10cSrcweir m_zips.push_back(
250cdf0e10cSrcweir std::make_pair(
251cdf0e10cSrcweir url, css::uno::Reference< css::container::XNameAccess >()));
252cdf0e10cSrcweir }
253cdf0e10cSrcweir {
254cdf0e10cSrcweir rtl::OUString url(
255cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM("$OOO_BASE_DIR/share/config"));
256cdf0e10cSrcweir rtl::Bootstrap::expandMacros(url);
257cdf0e10cSrcweir INetURLObject u(url);
258cdf0e10cSrcweir OSL_ASSERT(!u.HasError());
259cdf0e10cSrcweir rtl::OUStringBuffer b;
260cdf0e10cSrcweir b.appendAscii(RTL_CONSTASCII_STRINGPARAM("images_"));
261cdf0e10cSrcweir b.append(m_style);
262cdf0e10cSrcweir b.appendAscii(RTL_CONSTASCII_STRINGPARAM(".zip"));
263cdf0e10cSrcweir bool ok = u.Append(b.makeStringAndClear(), INetURLObject::ENCODE_ALL);
264cdf0e10cSrcweir OSL_ASSERT(ok); (void) ok;
265cdf0e10cSrcweir m_zips.push_back(
266cdf0e10cSrcweir std::make_pair(
267cdf0e10cSrcweir u.GetMainURL(INetURLObject::NO_DECODE),
268cdf0e10cSrcweir css::uno::Reference< css::container::XNameAccess >()));
269cdf0e10cSrcweir }
270cdf0e10cSrcweir if ( m_style.equals(::rtl::OUString::createFromAscii("default")) )
271cdf0e10cSrcweir {
272cdf0e10cSrcweir rtl::OUString url(
273cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM(
274cdf0e10cSrcweir "$OOO_BASE_DIR/share/config/images.zip"));
275cdf0e10cSrcweir rtl::Bootstrap::expandMacros(url);
276cdf0e10cSrcweir m_zips.push_back(
277cdf0e10cSrcweir std::make_pair(
278cdf0e10cSrcweir url, css::uno::Reference< css::container::XNameAccess >()));
279cdf0e10cSrcweir }
280cdf0e10cSrcweir }
281cdf0e10cSrcweir
checkStyleCacheLookup(rtl::OUString const & style,bool & exists)282cdf0e10cSrcweir bool ImplImageTree::checkStyleCacheLookup(
283cdf0e10cSrcweir rtl::OUString const & style, bool &exists)
284cdf0e10cSrcweir {
285cdf0e10cSrcweir CheckStyleCache::iterator i(m_checkStyleCache.find(style));
286cdf0e10cSrcweir if (i != m_checkStyleCache.end()) {
287cdf0e10cSrcweir exists = i->second;
288cdf0e10cSrcweir return true;
289cdf0e10cSrcweir } else {
290cdf0e10cSrcweir return false;
291cdf0e10cSrcweir }
292cdf0e10cSrcweir }
293cdf0e10cSrcweir
iconCacheLookup(rtl::OUString const & name,bool localized,BitmapEx & bitmap)294cdf0e10cSrcweir bool ImplImageTree::iconCacheLookup(
295cdf0e10cSrcweir rtl::OUString const & name, bool localized, BitmapEx & bitmap)
296cdf0e10cSrcweir {
297cdf0e10cSrcweir IconCache::iterator i(m_iconCache.find(name));
298cdf0e10cSrcweir if (i != m_iconCache.end() && i->second.first == localized) {
299cdf0e10cSrcweir bitmap = i->second.second;
300cdf0e10cSrcweir return true;
301cdf0e10cSrcweir } else {
302cdf0e10cSrcweir return false;
303cdf0e10cSrcweir }
304cdf0e10cSrcweir }
305cdf0e10cSrcweir
find(std::vector<rtl::OUString> const & paths,BitmapEx & bitmap)306cdf0e10cSrcweir bool ImplImageTree::find(
307cdf0e10cSrcweir std::vector< rtl::OUString > const & paths, BitmapEx & bitmap)
308cdf0e10cSrcweir {
309cdf0e10cSrcweir for (Zips::iterator i(m_zips.begin()); i != m_zips.end();) {
310cdf0e10cSrcweir if (!i->second.is()) {
311cdf0e10cSrcweir css::uno::Sequence< css::uno::Any > args(1);
312cdf0e10cSrcweir args[0] <<= i->first;
313cdf0e10cSrcweir try {
314cdf0e10cSrcweir i->second.set(
315cdf0e10cSrcweir comphelper::createProcessComponentWithArguments(
316cdf0e10cSrcweir rtl::OUString(
317cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM(
318cdf0e10cSrcweir "com.sun.star.packages.zip.ZipFileAccess")),
319cdf0e10cSrcweir args),
320cdf0e10cSrcweir css::uno::UNO_QUERY_THROW);
321cdf0e10cSrcweir } catch (css::uno::RuntimeException &) {
322cdf0e10cSrcweir throw;
323cdf0e10cSrcweir } catch (css::uno::Exception & e) {
324cdf0e10cSrcweir OSL_TRACE(
325f8a117d3SHerbert Dürr "ImplImageTree::find exception \"%s\" for \"%s\"",
326f8a117d3SHerbert Dürr rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8).getStr(),
327f8a117d3SHerbert Dürr rtl::OUStringToOString( i->first, RTL_TEXTENCODING_UTF8).getStr());
328cdf0e10cSrcweir i = m_zips.erase(i);
329cdf0e10cSrcweir continue;
330cdf0e10cSrcweir }
331cdf0e10cSrcweir }
332cdf0e10cSrcweir for (std::vector< rtl::OUString >::const_reverse_iterator j(
333cdf0e10cSrcweir paths.rbegin());
334cdf0e10cSrcweir j != paths.rend(); ++j)
335cdf0e10cSrcweir {
336cdf0e10cSrcweir if (i->second->hasByName(*j)) {
337cdf0e10cSrcweir css::uno::Reference< css::io::XInputStream > s;
338cdf0e10cSrcweir bool ok = i->second->getByName(*j) >>= s;
339cdf0e10cSrcweir OSL_ASSERT(ok); (void) ok;
340cdf0e10cSrcweir loadFromStream(s, *j, bitmap);
341cdf0e10cSrcweir return true;
342cdf0e10cSrcweir }
343cdf0e10cSrcweir }
344cdf0e10cSrcweir ++i;
345cdf0e10cSrcweir }
346cdf0e10cSrcweir return false;
347cdf0e10cSrcweir }
348