merge the formfield patch from ooo-build
[ooovba.git] / sd / source / ui / slidesorter / cache / SlsCacheCompactor.cxx
blob28acc7bf7af7453f0d3a125aadc740556a9a6d4b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SlsCacheCompactor.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sd.hxx"
34 #include "SlsCacheCompactor.hxx"
36 #include "SlsBitmapCompressor.hxx"
37 #include "SlsBitmapCache.hxx"
38 #include "SlsCacheCompactor.hxx"
39 #include "SlsCacheConfiguration.hxx"
41 #include <rtl/ustring.hxx>
42 #include <com/sun/star/uno/Any.hxx>
43 #include <set>
45 using namespace ::com::sun::star::uno;
47 // Uncomment the definition of VERBOSE to get some more OSL_TRACE messages.
48 #ifdef DEBUG
49 //#define VERBOSE
50 #endif
52 namespace {
54 /** This is a trivial implementation of the CacheCompactor interface class.
55 It ignores calls to RequestCompaction() and thus will never decrease the
56 total size of off-screen preview bitmaps.
58 class NoCacheCompaction
59 : public ::sd::slidesorter::cache::CacheCompactor
61 public:
62 NoCacheCompaction (
63 ::sd::slidesorter::cache::BitmapCache& rCache,
64 sal_Int32 nMaximalCacheSize)
65 : CacheCompactor(rCache, nMaximalCacheSize)
68 virtual void RequestCompaction (void) { /* Ignored */ };
70 protected:
71 virtual void Run (void) { /* Do nothing */ };
77 /** This implementation of the CacheCompactor interface class uses one of
78 several bitmap compression algorithms to reduce the number of the bytes
79 of the off-screen previews in the bitmap cache. See the documentation
80 of CacheCompactor::Create() for more details on configuration properties
81 that control the choice of compression algorithm.
83 class CacheCompactionByCompression
84 : public ::sd::slidesorter::cache::CacheCompactor
86 public:
87 CacheCompactionByCompression (
88 ::sd::slidesorter::cache::BitmapCache& rCache,
89 sal_Int32 nMaximalCacheSize,
90 const ::boost::shared_ptr< ::sd::slidesorter::cache::BitmapCompressor>& rpCompressor);
92 protected:
93 virtual void Run (void);
95 private:
96 ::boost::shared_ptr< ::sd::slidesorter::cache::BitmapCompressor> mpCompressor;
100 } // end of anonymous namespace
102 namespace sd { namespace slidesorter { namespace cache {
105 ::std::auto_ptr<CacheCompactor> CacheCompactor::Create (
106 BitmapCache& rCache,
107 sal_Int32 nMaximalCacheSize)
109 static const ::rtl::OUString sNone (RTL_CONSTASCII_USTRINGPARAM("None"));
110 static const ::rtl::OUString sCompress (RTL_CONSTASCII_USTRINGPARAM("Compress"));
111 static const ::rtl::OUString sErase (RTL_CONSTASCII_USTRINGPARAM("Erase"));
112 static const ::rtl::OUString sResolution (RTL_CONSTASCII_USTRINGPARAM("ResolutionReduction"));
113 static const ::rtl::OUString sPNGCompression (RTL_CONSTASCII_USTRINGPARAM("PNGCompression"));
115 ::boost::shared_ptr<BitmapCompressor> pCompressor;
116 ::rtl::OUString sCompressionPolicy(sPNGCompression);
117 Any aCompressionPolicy (CacheConfiguration::Instance()->GetValue(
118 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("CompressionPolicy"))));
119 if (aCompressionPolicy.has<rtl::OUString>())
120 aCompressionPolicy >>= sCompressionPolicy;
121 if (sCompressionPolicy == sNone)
122 pCompressor.reset(new NoBitmapCompression());
123 else if (sCompressionPolicy == sErase)
124 pCompressor.reset(new CompressionByDeletion());
125 else if (sCompressionPolicy == sResolution)
126 pCompressor.reset(new ResolutionReduction());
127 else
128 pCompressor.reset(new PngCompression());
130 ::std::auto_ptr<CacheCompactor> pCompactor (NULL);
131 ::rtl::OUString sCompactionPolicy(sCompress);
132 Any aCompactionPolicy (CacheConfiguration::Instance()->GetValue(
133 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("CompactionPolicy"))));
134 if (aCompactionPolicy.has<rtl::OUString>())
135 aCompactionPolicy >>= sCompactionPolicy;
136 if (sCompactionPolicy == sNone)
137 pCompactor.reset(new NoCacheCompaction(rCache,nMaximalCacheSize));
138 else
139 pCompactor.reset(new CacheCompactionByCompression(rCache,nMaximalCacheSize,pCompressor));
141 return pCompactor;
147 void CacheCompactor::RequestCompaction (void)
149 if ( ! mbIsCompactionRunning && ! maCompactionTimer.IsActive())
150 maCompactionTimer.Start();
156 CacheCompactor::CacheCompactor(
157 BitmapCache& rCache,
158 sal_Int32 nMaximalCacheSize)
159 : mrCache(rCache),
160 mnMaximalCacheSize(nMaximalCacheSize),
161 mbIsCompactionRunning(false)
163 maCompactionTimer.SetTimeout(100 /*ms*/);
164 maCompactionTimer.SetTimeoutHdl(LINK(this,CacheCompactor,CompactionCallback));
171 IMPL_LINK(CacheCompactor, CompactionCallback, Timer*, EMPTYARG)
173 mbIsCompactionRunning = true;
177 Run();
179 catch(::com::sun::star::uno::RuntimeException e) { }
180 catch(::com::sun::star::uno::Exception e) { }
182 mbIsCompactionRunning = false;
183 return 1;
189 } } } // end of namespace ::sd::slidesorter::cache
194 namespace {
196 //===== CacheCompactionByCompression ==========================================
198 CacheCompactionByCompression::CacheCompactionByCompression (
199 ::sd::slidesorter::cache::BitmapCache& rCache,
200 sal_Int32 nMaximalCacheSize,
201 const ::boost::shared_ptr< ::sd::slidesorter::cache::BitmapCompressor>& rpCompressor)
202 : CacheCompactor(rCache,nMaximalCacheSize),
203 mpCompressor(rpCompressor)
210 void CacheCompactionByCompression::Run (void)
212 if (mrCache.GetSize() > mnMaximalCacheSize)
214 #ifdef VERBOSE
215 OSL_TRACE ("bitmap cache uses to much space: %d > %d",
216 mrCache.GetSize(), mnMaximalCacheSize);
217 #endif
219 ::std::auto_ptr< ::sd::slidesorter::cache::BitmapCache::CacheIndex> pIndex (
220 mrCache.GetCacheIndex(false,false));
221 ::sd::slidesorter::cache::BitmapCache::CacheIndex::iterator iIndex;
222 for (iIndex=pIndex->begin(); iIndex!=pIndex->end(); ++iIndex)
224 if (*iIndex == NULL)
225 continue;
227 mrCache.Compress(*iIndex, mpCompressor);
228 if (mrCache.GetSize() < mnMaximalCacheSize)
229 break;
231 mrCache.ReCalculateTotalCacheSize();
232 #ifdef VERBOSE
233 OSL_TRACE (" there are now %d bytes occupied", mrCache.GetSize());
234 #endif
239 } // end of anonymous namespace