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 0 : class SwStyleCache
35 : {
36 : SwStyleNameCache mMap;
37 : public:
38 0 : SwStyleCache() {}
39 0 : void addStyleName( StylePool::SfxItemSet_Pointer_t pStyle )
40 0 : { mMap[ StylePool::nameOf(pStyle) ] = pStyle; }
41 : void addCompletePool( StylePool& rPool );
42 0 : 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 0 : SwStyleManager( SfxItemSet* pIgnorableParagraphItems )
68 : : aAutoCharPool(),
69 : aAutoParaPool( pIgnorableParagraphItems ),
70 : mpCharCache(0),
71 0 : mpParaCache(0)
72 0 : {}
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 0 : IStyleAccess *createStyleManager( SfxItemSet* pIgnorableParagraphItems )
86 : {
87 0 : return new SwStyleManager( pIgnorableParagraphItems );
88 : }
89 :
90 0 : SwStyleManager::~SwStyleManager()
91 : {
92 0 : delete mpCharCache;
93 0 : delete mpParaCache;
94 0 : }
95 :
96 0 : void SwStyleManager::clearCaches()
97 : {
98 0 : delete mpCharCache;
99 0 : mpCharCache = 0;
100 0 : delete mpParaCache;
101 0 : mpParaCache = 0;
102 0 : }
103 :
104 0 : StylePool::SfxItemSet_Pointer_t SwStyleManager::getAutomaticStyle( const SfxItemSet& rSet,
105 : IStyleAccess::SwAutoStyleFamily eFamily )
106 : {
107 0 : StylePool& rAutoPool = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? aAutoCharPool : aAutoParaPool;
108 0 : return rAutoPool.insertItemSet( rSet );
109 : }
110 :
111 0 : StylePool::SfxItemSet_Pointer_t SwStyleManager::cacheAutomaticStyle( const SfxItemSet& rSet,
112 : IStyleAccess::SwAutoStyleFamily eFamily )
113 : {
114 0 : StylePool& rAutoPool = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? aAutoCharPool : aAutoParaPool;
115 0 : StylePool::SfxItemSet_Pointer_t pStyle = rAutoPool.insertItemSet( rSet );
116 : SwStyleCache* &rpCache = eFamily == IStyleAccess::AUTO_STYLE_CHAR ?
117 0 : mpCharCache : mpParaCache;
118 0 : if( !rpCache )
119 0 : rpCache = new SwStyleCache();
120 0 : rpCache->addStyleName( pStyle );
121 0 : return pStyle;
122 : }
123 :
124 0 : StylePool::SfxItemSet_Pointer_t SwStyleManager::getByName( const OUString& rName,
125 : IStyleAccess::SwAutoStyleFamily eFamily )
126 : {
127 0 : StylePool& rAutoPool = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? aAutoCharPool : aAutoParaPool;
128 0 : SwStyleCache* &rpCache = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? mpCharCache : mpParaCache;
129 0 : if( !rpCache )
130 0 : rpCache = new SwStyleCache();
131 0 : StylePool::SfxItemSet_Pointer_t pStyle = rpCache->getByName( rName );
132 0 : 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 0 : return pStyle;
141 : }
142 :
143 0 : void SwStyleManager::getAllStyles( std::vector<StylePool::SfxItemSet_Pointer_t> &rStyles,
144 : IStyleAccess::SwAutoStyleFamily eFamily )
145 : {
146 0 : StylePool& rAutoPool = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? aAutoCharPool : aAutoParaPool;
147 : // setup <StylePool> iterator, which skips unused styles and ignorable items
148 0 : IStylePoolIteratorAccess *pIter = rAutoPool.createIterator( true, true );
149 0 : StylePool::SfxItemSet_Pointer_t pStyle = pIter->getNext();
150 0 : while( pStyle.get() )
151 : {
152 0 : rStyles.push_back( pStyle );
153 :
154 0 : pStyle = pIter->getNext();
155 : }
156 0 : delete pIter;
157 0 : }
158 :
159 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|