Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "swstylemanager.hxx"
21 : #include <boost/unordered_map.hpp>
22 : #include <svl/stylepool.hxx>
23 : #include <doc.hxx>
24 : #include <charfmt.hxx>
25 : #include <docary.hxx>
26 : #include <swtypes.hxx>
27 : #include <istyleaccess.hxx>
28 :
29 : typedef ::boost::unordered_map< const OUString,
30 : StylePool::SfxItemSet_Pointer_t,
31 : OUStringHash,
32 : ::std::equal_to< OUString > > SwStyleNameCache;
33 :
34 518 : class SwStyleCache
35 : {
36 : SwStyleNameCache mMap;
37 : public:
38 518 : SwStyleCache() {}
39 2672 : void addStyleName( StylePool::SfxItemSet_Pointer_t pStyle )
40 2672 : { mMap[ StylePool::nameOf(pStyle) ] = pStyle; }
41 : void addCompletePool( StylePool& rPool );
42 11764 : StylePool::SfxItemSet_Pointer_t getByName( const OUString& rName ) { return mMap[rName]; }
43 : };
44 :
45 0 : void SwStyleCache::addCompletePool( StylePool& rPool )
46 : {
47 0 : IStylePoolIteratorAccess *pIter = rPool.createIterator();
48 0 : StylePool::SfxItemSet_Pointer_t pStyle = pIter->getNext();
49 0 : while( pStyle.get() )
50 : {
51 0 : OUString aName( StylePool::nameOf(pStyle) );
52 0 : mMap[ aName ] = pStyle;
53 0 : pStyle = pIter->getNext();
54 0 : }
55 0 : delete pIter;
56 0 : }
57 :
58 : class SwStyleManager : public IStyleAccess
59 : {
60 : StylePool aAutoCharPool;
61 : StylePool aAutoParaPool;
62 : SwStyleCache *mpCharCache;
63 : SwStyleCache *mpParaCache;
64 :
65 : public:
66 : // accept empty item set for ignorable paragraph items.
67 5052 : SwStyleManager( SfxItemSet* pIgnorableParagraphItems )
68 : : aAutoCharPool(),
69 : aAutoParaPool( pIgnorableParagraphItems ),
70 : mpCharCache(0),
71 5052 : mpParaCache(0)
72 5052 : {}
73 : virtual ~SwStyleManager();
74 : virtual StylePool::SfxItemSet_Pointer_t getAutomaticStyle( const SfxItemSet& rSet,
75 : IStyleAccess::SwAutoStyleFamily eFamily ) SAL_OVERRIDE;
76 : virtual StylePool::SfxItemSet_Pointer_t getByName( const OUString& rName,
77 : IStyleAccess::SwAutoStyleFamily eFamily ) SAL_OVERRIDE;
78 : virtual void getAllStyles( std::vector<StylePool::SfxItemSet_Pointer_t> &rStyles,
79 : IStyleAccess::SwAutoStyleFamily eFamily ) SAL_OVERRIDE;
80 : virtual StylePool::SfxItemSet_Pointer_t cacheAutomaticStyle( const SfxItemSet& rSet,
81 : SwAutoStyleFamily eFamily ) SAL_OVERRIDE;
82 : virtual void clearCaches() SAL_OVERRIDE;
83 : };
84 :
85 5052 : IStyleAccess *createStyleManager( SfxItemSet* pIgnorableParagraphItems )
86 : {
87 5052 : return new SwStyleManager( pIgnorableParagraphItems );
88 : }
89 :
90 15135 : SwStyleManager::~SwStyleManager()
91 : {
92 5045 : delete mpCharCache;
93 5045 : delete mpParaCache;
94 10090 : }
95 :
96 318 : void SwStyleManager::clearCaches()
97 : {
98 318 : delete mpCharCache;
99 318 : mpCharCache = 0;
100 318 : delete mpParaCache;
101 318 : mpParaCache = 0;
102 318 : }
103 :
104 370640 : StylePool::SfxItemSet_Pointer_t SwStyleManager::getAutomaticStyle( const SfxItemSet& rSet,
105 : IStyleAccess::SwAutoStyleFamily eFamily )
106 : {
107 370640 : StylePool& rAutoPool = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? aAutoCharPool : aAutoParaPool;
108 370640 : return rAutoPool.insertItemSet( rSet );
109 : }
110 :
111 2672 : StylePool::SfxItemSet_Pointer_t SwStyleManager::cacheAutomaticStyle( const SfxItemSet& rSet,
112 : IStyleAccess::SwAutoStyleFamily eFamily )
113 : {
114 2672 : StylePool& rAutoPool = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? aAutoCharPool : aAutoParaPool;
115 2672 : StylePool::SfxItemSet_Pointer_t pStyle = rAutoPool.insertItemSet( rSet );
116 : SwStyleCache* &rpCache = eFamily == IStyleAccess::AUTO_STYLE_CHAR ?
117 2672 : mpCharCache : mpParaCache;
118 2672 : if( !rpCache )
119 518 : rpCache = new SwStyleCache();
120 2672 : rpCache->addStyleName( pStyle );
121 2672 : return pStyle;
122 : }
123 :
124 11764 : StylePool::SfxItemSet_Pointer_t SwStyleManager::getByName( const OUString& rName,
125 : IStyleAccess::SwAutoStyleFamily eFamily )
126 : {
127 11764 : StylePool& rAutoPool = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? aAutoCharPool : aAutoParaPool;
128 11764 : SwStyleCache* &rpCache = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? mpCharCache : mpParaCache;
129 11764 : if( !rpCache )
130 0 : rpCache = new SwStyleCache();
131 11764 : StylePool::SfxItemSet_Pointer_t pStyle = rpCache->getByName( rName );
132 11764 : if( !pStyle.get() )
133 : {
134 : // Ok, ok, it's allowed to ask for uncached styles (from UNO) but it should not be done
135 : // during loading a document
136 : OSL_FAIL( "Don't ask for uncached styles" );
137 0 : rpCache->addCompletePool( rAutoPool );
138 0 : pStyle = rpCache->getByName( rName );
139 : }
140 11764 : return pStyle;
141 : }
142 :
143 132 : void SwStyleManager::getAllStyles( std::vector<StylePool::SfxItemSet_Pointer_t> &rStyles,
144 : IStyleAccess::SwAutoStyleFamily eFamily )
145 : {
146 132 : StylePool& rAutoPool = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? aAutoCharPool : aAutoParaPool;
147 : // setup <StylePool> iterator, which skips unused styles and ignorable items
148 132 : IStylePoolIteratorAccess *pIter = rAutoPool.createIterator( true, true );
149 132 : StylePool::SfxItemSet_Pointer_t pStyle = pIter->getNext();
150 352 : while( pStyle.get() )
151 : {
152 88 : rStyles.push_back( pStyle );
153 :
154 88 : pStyle = pIter->getNext();
155 : }
156 132 : delete pIter;
157 402 : }
158 :
159 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|