1*efeef26fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*efeef26fSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*efeef26fSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*efeef26fSAndrew Rist * distributed with this work for additional information
6*efeef26fSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*efeef26fSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*efeef26fSAndrew Rist * "License"); you may not use this file except in compliance
9*efeef26fSAndrew Rist * with the License. You may obtain a copy of the License at
10*efeef26fSAndrew Rist *
11*efeef26fSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*efeef26fSAndrew Rist *
13*efeef26fSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*efeef26fSAndrew Rist * software distributed under the License is distributed on an
15*efeef26fSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*efeef26fSAndrew Rist * KIND, either express or implied. See the License for the
17*efeef26fSAndrew Rist * specific language governing permissions and limitations
18*efeef26fSAndrew Rist * under the License.
19*efeef26fSAndrew Rist *
20*efeef26fSAndrew Rist *************************************************************/
21*efeef26fSAndrew Rist
22*efeef26fSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sw.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir
28cdf0e10cSrcweir #include "swstylemanager.hxx"
29cdf0e10cSrcweir #include <hash_map>
30cdf0e10cSrcweir #include <svl/stylepool.hxx>
31cdf0e10cSrcweir #include <doc.hxx>
32cdf0e10cSrcweir #include <charfmt.hxx>
33cdf0e10cSrcweir #include <docary.hxx>
34cdf0e10cSrcweir #include <swtypes.hxx>
35cdf0e10cSrcweir #include <istyleaccess.hxx>
36cdf0e10cSrcweir
37cdf0e10cSrcweir typedef ::std::hash_map< const ::rtl::OUString,
38cdf0e10cSrcweir StylePool::SfxItemSet_Pointer_t,
39cdf0e10cSrcweir ::rtl::OUStringHash,
40cdf0e10cSrcweir ::std::equal_to< ::rtl::OUString > > SwStyleNameCache;
41cdf0e10cSrcweir
42cdf0e10cSrcweir class SwStyleCache
43cdf0e10cSrcweir {
44cdf0e10cSrcweir SwStyleNameCache mMap;
45cdf0e10cSrcweir public:
SwStyleCache()46cdf0e10cSrcweir SwStyleCache() {}
addStyleName(StylePool::SfxItemSet_Pointer_t pStyle)47cdf0e10cSrcweir void addStyleName( StylePool::SfxItemSet_Pointer_t pStyle )
48cdf0e10cSrcweir { mMap[ StylePool::nameOf(pStyle) ] = pStyle; }
49cdf0e10cSrcweir void addCompletePool( StylePool& rPool );
getByName(const rtl::OUString & rName)50cdf0e10cSrcweir StylePool::SfxItemSet_Pointer_t getByName( const rtl::OUString& rName ) { return mMap[rName]; }
51cdf0e10cSrcweir };
52cdf0e10cSrcweir
addCompletePool(StylePool & rPool)53cdf0e10cSrcweir void SwStyleCache::addCompletePool( StylePool& rPool )
54cdf0e10cSrcweir {
55cdf0e10cSrcweir IStylePoolIteratorAccess *pIter = rPool.createIterator();
56cdf0e10cSrcweir StylePool::SfxItemSet_Pointer_t pStyle = pIter->getNext();
57cdf0e10cSrcweir while( pStyle.get() )
58cdf0e10cSrcweir {
59cdf0e10cSrcweir rtl::OUString aName( StylePool::nameOf(pStyle) );
60cdf0e10cSrcweir mMap[ aName ] = pStyle;
61cdf0e10cSrcweir pStyle = pIter->getNext();
62cdf0e10cSrcweir }
63cdf0e10cSrcweir delete pIter;
64cdf0e10cSrcweir }
65cdf0e10cSrcweir
66cdf0e10cSrcweir class SwStyleManager : public IStyleAccess
67cdf0e10cSrcweir {
68cdf0e10cSrcweir StylePool aAutoCharPool;
69cdf0e10cSrcweir StylePool aAutoParaPool;
70cdf0e10cSrcweir SwStyleCache *mpCharCache;
71cdf0e10cSrcweir SwStyleCache *mpParaCache;
72cdf0e10cSrcweir
73cdf0e10cSrcweir public:
74cdf0e10cSrcweir // --> OD 2008-03-07 #refactorlists#
75cdf0e10cSrcweir // accept empty item set for ignorable paragraph items.
SwStyleManager(SfxItemSet * pIgnorableParagraphItems)76cdf0e10cSrcweir SwStyleManager( SfxItemSet* pIgnorableParagraphItems )
77cdf0e10cSrcweir : aAutoCharPool(),
78cdf0e10cSrcweir aAutoParaPool( pIgnorableParagraphItems ),
79cdf0e10cSrcweir mpCharCache(0),
80cdf0e10cSrcweir mpParaCache(0)
81cdf0e10cSrcweir {}
82cdf0e10cSrcweir // <--
83cdf0e10cSrcweir virtual ~SwStyleManager();
84cdf0e10cSrcweir virtual StylePool::SfxItemSet_Pointer_t getAutomaticStyle( const SfxItemSet& rSet,
85cdf0e10cSrcweir IStyleAccess::SwAutoStyleFamily eFamily );
86cdf0e10cSrcweir virtual StylePool::SfxItemSet_Pointer_t getByName( const rtl::OUString& rName,
87cdf0e10cSrcweir IStyleAccess::SwAutoStyleFamily eFamily );
88cdf0e10cSrcweir virtual void getAllStyles( std::vector<StylePool::SfxItemSet_Pointer_t> &rStyles,
89cdf0e10cSrcweir IStyleAccess::SwAutoStyleFamily eFamily );
90cdf0e10cSrcweir virtual StylePool::SfxItemSet_Pointer_t cacheAutomaticStyle( const SfxItemSet& rSet,
91cdf0e10cSrcweir SwAutoStyleFamily eFamily );
92cdf0e10cSrcweir virtual void clearCaches();
93cdf0e10cSrcweir };
94cdf0e10cSrcweir
createStyleManager(SfxItemSet * pIgnorableParagraphItems)95cdf0e10cSrcweir IStyleAccess *createStyleManager( SfxItemSet* pIgnorableParagraphItems )
96cdf0e10cSrcweir {
97cdf0e10cSrcweir return new SwStyleManager( pIgnorableParagraphItems );
98cdf0e10cSrcweir }
99cdf0e10cSrcweir
~SwStyleManager()100cdf0e10cSrcweir SwStyleManager::~SwStyleManager()
101cdf0e10cSrcweir {
102cdf0e10cSrcweir delete mpCharCache;
103cdf0e10cSrcweir delete mpParaCache;
104cdf0e10cSrcweir }
105cdf0e10cSrcweir
clearCaches()106cdf0e10cSrcweir void SwStyleManager::clearCaches()
107cdf0e10cSrcweir {
108cdf0e10cSrcweir delete mpCharCache;
109cdf0e10cSrcweir mpCharCache = 0;
110cdf0e10cSrcweir delete mpParaCache;
111cdf0e10cSrcweir mpParaCache = 0;
112cdf0e10cSrcweir }
113cdf0e10cSrcweir
getAutomaticStyle(const SfxItemSet & rSet,IStyleAccess::SwAutoStyleFamily eFamily)114cdf0e10cSrcweir StylePool::SfxItemSet_Pointer_t SwStyleManager::getAutomaticStyle( const SfxItemSet& rSet,
115cdf0e10cSrcweir IStyleAccess::SwAutoStyleFamily eFamily )
116cdf0e10cSrcweir {
117cdf0e10cSrcweir StylePool& rAutoPool = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? aAutoCharPool : aAutoParaPool;
118cdf0e10cSrcweir return rAutoPool.insertItemSet( rSet );
119cdf0e10cSrcweir }
120cdf0e10cSrcweir
cacheAutomaticStyle(const SfxItemSet & rSet,IStyleAccess::SwAutoStyleFamily eFamily)121cdf0e10cSrcweir StylePool::SfxItemSet_Pointer_t SwStyleManager::cacheAutomaticStyle( const SfxItemSet& rSet,
122cdf0e10cSrcweir IStyleAccess::SwAutoStyleFamily eFamily )
123cdf0e10cSrcweir {
124cdf0e10cSrcweir StylePool& rAutoPool = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? aAutoCharPool : aAutoParaPool;
125cdf0e10cSrcweir StylePool::SfxItemSet_Pointer_t pStyle = rAutoPool.insertItemSet( rSet );
126cdf0e10cSrcweir SwStyleCache* &rpCache = eFamily == IStyleAccess::AUTO_STYLE_CHAR ?
127cdf0e10cSrcweir mpCharCache : mpParaCache;
128cdf0e10cSrcweir if( !rpCache )
129cdf0e10cSrcweir rpCache = new SwStyleCache();
130cdf0e10cSrcweir rpCache->addStyleName( pStyle );
131cdf0e10cSrcweir return pStyle;
132cdf0e10cSrcweir }
133cdf0e10cSrcweir
getByName(const rtl::OUString & rName,IStyleAccess::SwAutoStyleFamily eFamily)134cdf0e10cSrcweir StylePool::SfxItemSet_Pointer_t SwStyleManager::getByName( const rtl::OUString& rName,
135cdf0e10cSrcweir IStyleAccess::SwAutoStyleFamily eFamily )
136cdf0e10cSrcweir {
137cdf0e10cSrcweir StylePool& rAutoPool = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? aAutoCharPool : aAutoParaPool;
138cdf0e10cSrcweir SwStyleCache* &rpCache = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? mpCharCache : mpParaCache;
139cdf0e10cSrcweir if( !rpCache )
140cdf0e10cSrcweir rpCache = new SwStyleCache();
141cdf0e10cSrcweir StylePool::SfxItemSet_Pointer_t pStyle = rpCache->getByName( rName );
142cdf0e10cSrcweir if( !pStyle.get() )
143cdf0e10cSrcweir {
144cdf0e10cSrcweir // Ok, ok, it's allowed to ask for uncached styles (from UNO) but it should not be done
145cdf0e10cSrcweir // during loading a document
146cdf0e10cSrcweir ASSERT( false, "Don't ask for uncached styles" );
147cdf0e10cSrcweir rpCache->addCompletePool( rAutoPool );
148cdf0e10cSrcweir pStyle = rpCache->getByName( rName );
149cdf0e10cSrcweir }
150cdf0e10cSrcweir return pStyle;
151cdf0e10cSrcweir }
152cdf0e10cSrcweir
getAllStyles(std::vector<StylePool::SfxItemSet_Pointer_t> & rStyles,IStyleAccess::SwAutoStyleFamily eFamily)153cdf0e10cSrcweir void SwStyleManager::getAllStyles( std::vector<StylePool::SfxItemSet_Pointer_t> &rStyles,
154cdf0e10cSrcweir IStyleAccess::SwAutoStyleFamily eFamily )
155cdf0e10cSrcweir {
156cdf0e10cSrcweir StylePool& rAutoPool = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? aAutoCharPool : aAutoParaPool;
157cdf0e10cSrcweir // --> OD 2008-03-07 #refactorlists#
158cdf0e10cSrcweir // setup <StylePool> iterator, which skips unused styles and ignorable items
159cdf0e10cSrcweir IStylePoolIteratorAccess *pIter = rAutoPool.createIterator( true, true );
160cdf0e10cSrcweir // <--
161cdf0e10cSrcweir StylePool::SfxItemSet_Pointer_t pStyle = pIter->getNext();
162cdf0e10cSrcweir while( pStyle.get() )
163cdf0e10cSrcweir {
164cdf0e10cSrcweir rStyles.push_back( pStyle );
165cdf0e10cSrcweir
166cdf0e10cSrcweir pStyle = pIter->getNext();
167cdf0e10cSrcweir }
168cdf0e10cSrcweir delete pIter;
169cdf0e10cSrcweir }
170