LibreOffice Module sd (master) 1
stlfamily.cxx
Go to the documentation of this file.
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 <com/sun/star/lang/DisposedException.hpp>
21#include <com/sun/star/lang/IllegalAccessException.hpp>
22#include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
24
25#include <utility>
26#include <vcl/svapp.hxx>
27
28#include <svl/style.hxx>
29
30#include <tools/debug.hxx>
31#include <unotools/weakref.hxx>
32
33#include <strings.hrc>
34#include <stlfamily.hxx>
35#include <stlsheet.hxx>
36#include <sdresid.hxx>
37#include <sdpage.hxx>
38#include <glob.hxx>
39
40#include <map>
41#include <memory>
42
43using namespace ::com::sun::star::uno;
44using namespace ::com::sun::star::lang;
45using namespace ::com::sun::star::container;
46using namespace ::com::sun::star::style;
47using namespace ::com::sun::star::beans;
48
49typedef std::map< OUString, rtl::Reference< SdStyleSheet > > PresStyleMap;
50
52{
54 OUString maLayoutName;
55
58
59private:
61};
62
64{
65 auto pMasterPage = mxMasterPage.get();
66 if (!pMasterPage)
67 return maStyleSheets;
68
69 if (pMasterPage->GetLayoutName() != maLayoutName )
70 {
71 maLayoutName = pMasterPage->GetLayoutName();
72
73 OUString aLayoutName( maLayoutName );
74 const sal_Int32 nLen = aLayoutName.indexOf(SD_LT_SEPARATOR ) + 4;
75 aLayoutName = aLayoutName.copy(0, nLen );
76
77 if( (maStyleSheets.empty()) || !(*maStyleSheets.begin()).second->GetName().startsWith( aLayoutName) )
78 {
79 maStyleSheets.clear();
80
81 // The iterator will return only style sheets of family master page
82 std::shared_ptr<SfxStyleSheetIterator> aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), SfxStyleFamily::Page);
83 for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
84 pStyle = aSSSIterator->Next() )
85 {
86 // we assume that we have only SdStyleSheets
87 SdStyleSheet* pSdStyle = static_cast< SdStyleSheet* >( pStyle );
88 if (pSdStyle->GetName().startsWith(aLayoutName))
89 {
90 maStyleSheets[ pSdStyle->GetApiName() ].set( pSdStyle );
91 }
92 }
93 }
94 }
95
96 return maStyleSheets;
97}
98
100: mnFamily( nFamily )
101, mxPool(std::move( xPool ))
102{
103}
104
106: mnFamily( SfxStyleFamily::Page )
107, mxPool( xPool )
108, mpImpl( new SdStyleFamilyImpl )
109{
110 mpImpl->mxMasterPage = const_cast< SdPage* >( pMasterPage );
111 mpImpl->mxPool = xPool;
112}
113
115{
116 DBG_ASSERT( !mxPool.is(), "SdStyleFamily::~SdStyleFamily(), dispose me first!" );
117}
118
120{
121 if( !mxPool.is() )
122 throw DisposedException();
123}
124
126{
127 Reference< XStyle > xStyle( rElement, UNO_QUERY );
128 SdStyleSheet* pStyle = static_cast< SdStyleSheet* >( xStyle.get() );
129
130 if( pStyle == nullptr || (pStyle->GetFamily() != mnFamily) || (pStyle->GetPool() != mxPool.get()) || (mxPool->Find( pStyle->GetName(), mnFamily) != nullptr) )
131 throw IllegalArgumentException();
132
133 return pStyle;
134}
135
137{
138 SdStyleSheet* pRet = nullptr;
139 if( !rName.isEmpty() )
140 {
141 if( mnFamily == SfxStyleFamily::Page )
142 {
143 PresStyleMap& rStyleMap = mpImpl->getStyleSheets();
144 PresStyleMap::iterator iter( rStyleMap.find(rName) );
145 if( iter != rStyleMap.end() )
146 pRet = (*iter).second.get();
147 }
148 else
149 {
150 std::shared_ptr<SfxStyleSheetIterator> aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), mnFamily);
151 for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
152 pStyle = aSSSIterator->Next() )
153 {
154 // we assume that we have only SdStyleSheets
155 SdStyleSheet* pSdStyle = static_cast< SdStyleSheet* >( pStyle );
156 if (pSdStyle->GetApiName() == rName)
157 {
158 pRet = pSdStyle;
159 break;
160 }
161 }
162 }
163 }
164 if( pRet )
165 return pRet;
166
167 throw NoSuchElementException();
168}
169
170// XServiceInfo
172{
173 return "SdStyleFamily";
174}
175
176sal_Bool SAL_CALL SdStyleFamily::supportsService( const OUString& ServiceName )
177{
178 return cppu::supportsService( this, ServiceName );
179}
180
181Sequence< OUString > SAL_CALL SdStyleFamily::getSupportedServiceNames()
182{
183 return { "com.sun.star.style.StyleFamily" };
184}
185
186// XNamed
187OUString SAL_CALL SdStyleFamily::getName()
188{
189 if( mnFamily == SfxStyleFamily::Page )
190 {
191 rtl::Reference<SdPage> pPage = mpImpl->mxMasterPage.get();
192 if( pPage == nullptr )
193 throw DisposedException();
194
195 OUString aLayoutName( pPage->GetLayoutName() );
196 sal_Int32 nIndex = aLayoutName.indexOf(SD_LT_SEPARATOR);
197 if( nIndex != -1 )
198 aLayoutName = aLayoutName.copy(0, nIndex);
199
200 return aLayoutName;
201 }
202 else
203 {
205 }
206}
207
208void SAL_CALL SdStyleFamily::setName( const OUString& )
209{
210}
211
212// XNameAccess
213
214Any SAL_CALL SdStyleFamily::getByName( const OUString& rName )
215{
216 SolarMutexGuard aGuard;
218 return Any( Reference< XStyle >( static_cast<SfxUnoStyleSheet*>(GetSheetByName( rName )) ) );
219}
220
221Sequence< OUString > SAL_CALL SdStyleFamily::getElementNames()
222{
223 SolarMutexGuard aGuard;
224
226
227 if( mnFamily == SfxStyleFamily::Page )
228 {
229 PresStyleMap& rStyleMap = mpImpl->getStyleSheets();
230 Sequence< OUString > aNames( rStyleMap.size() );
231
232 OUString* pNames = aNames.getArray();
233 for( const auto& rEntry : rStyleMap )
234 {
235 rtl::Reference< SdStyleSheet > xStyle( rEntry.second );
236 if( xStyle.is() )
237 {
238 *pNames++ = xStyle->GetApiName();
239 }
240 }
241
242 return aNames;
243 }
244 else
245 {
246 std::vector< OUString > aNames;
247 std::shared_ptr<SfxStyleSheetIterator> aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), mnFamily);
248 for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
249 pStyle = aSSSIterator->Next() )
250 {
251 // we assume that we have only SdStyleSheets
252 SdStyleSheet* pSdStyle = static_cast< SdStyleSheet* >( pStyle );
253 aNames.push_back(pSdStyle->GetApiName());
254 }
255 return Sequence< OUString >( &(*aNames.begin()), aNames.size() );
256 }
257}
258
259sal_Bool SAL_CALL SdStyleFamily::hasByName( const OUString& aName )
260{
261 SolarMutexGuard aGuard;
263
264 if( !aName.isEmpty() )
265 {
266 if( mnFamily == SfxStyleFamily::Page )
267 {
268 PresStyleMap& rStyleSheets = mpImpl->getStyleSheets();
269 PresStyleMap::iterator iter( rStyleSheets.find(aName) );
270 return iter != rStyleSheets.end();
271 }
272 else
273 {
274 std::shared_ptr<SfxStyleSheetIterator> aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), mnFamily);
275 for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
276 pStyle = aSSSIterator->Next() )
277 {
278 // we assume that we have only SdStyleSheets
279 SdStyleSheet* pSdStyle = static_cast< SdStyleSheet* >( pStyle );
280 if (pSdStyle->GetApiName() == aName)
281 {
282 return true;
283 }
284 }
285 }
286 }
287
288 return false;
289}
290
291// XElementAccess
292
294{
296}
297
299{
300 SolarMutexGuard aGuard;
302
303 if( mnFamily == SfxStyleFamily::Page )
304 {
305 return true;
306 }
307 else
308 {
309 std::shared_ptr<SfxStyleSheetIterator> aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), mnFamily);
310 if (aSSSIterator->First())
311 {
312 return true;
313 }
314 }
315
316 return false;
317}
318
319// XIndexAccess
320
321sal_Int32 SAL_CALL SdStyleFamily::getCount()
322{
323 SolarMutexGuard aGuard;
325
326 sal_Int32 nCount = 0;
327 if( mnFamily == SfxStyleFamily::Page )
328 {
329 return mpImpl->getStyleSheets().size();
330 }
331 else
332 {
333 std::shared_ptr<SfxStyleSheetIterator> aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), mnFamily);
334 for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
335 pStyle = aSSSIterator->Next() )
336 {
337 nCount++;
338 }
339 }
340
341 return nCount;
342}
343
345{
346 SolarMutexGuard aGuard;
348
349 if( Index >= 0 )
350 {
351 if( mnFamily == SfxStyleFamily::Page )
352 {
353 PresStyleMap& rStyleSheets = mpImpl->getStyleSheets();
354 if( Index < static_cast<sal_Int32>(rStyleSheets.size()) )
355 {
356 PresStyleMap::iterator iter( rStyleSheets.begin() );
357 std::advance(iter, Index);
358 return Any( Reference< XStyle >( (*iter).second ) );
359 }
360 }
361 else
362 {
363 std::shared_ptr<SfxStyleSheetIterator> aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), mnFamily);
364 for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
365 pStyle = aSSSIterator->Next() )
366 {
367 // we assume that we have only SdStyleSheets
368 SdStyleSheet* pSdStyle = static_cast< SdStyleSheet* >( pStyle );
369 if( Index-- == 0 )
370 {
371 return Any( Reference< XStyle >( pSdStyle ) );
372 }
373 }
374 }
375 }
376
377 throw IndexOutOfBoundsException();
378}
379
380// XNameContainer
381
382void SAL_CALL SdStyleFamily::insertByName( const OUString& rName, const Any& rElement )
383{
384 SolarMutexGuard aGuard;
386
387 if(rName.isEmpty())
388 throw IllegalArgumentException();
389
390 SdStyleSheet* pStyle = GetValidNewSheet( rElement );
391 if( !pStyle->SetName( rName ) )
392 throw ElementExistException();
393
394 pStyle->SetApiName( rName );
395 mxPool->Insert( pStyle );
396}
397
398void SAL_CALL SdStyleFamily::removeByName( const OUString& rName )
399{
400 SolarMutexGuard aGuard;
402
403 SdStyleSheet* pStyle = GetSheetByName( rName );
404
405 if( !pStyle->IsUserDefined() )
406 throw WrappedTargetException();
407
408 mxPool->Remove( pStyle );
409}
410
411// XNameReplace
412
413void SAL_CALL SdStyleFamily::replaceByName( const OUString& rName, const Any& aElement )
414{
415 SolarMutexGuard aGuard;
417
418 SdStyleSheet* pOldStyle = GetSheetByName( rName );
419 SdStyleSheet* pNewStyle = GetValidNewSheet( aElement );
420
421 mxPool->Remove( pOldStyle );
422 mxPool->Insert( pNewStyle );
423}
424
425// XSingleServiceFactory
426
427Reference< XInterface > SAL_CALL SdStyleFamily::createInstance()
428{
429 SolarMutexGuard aGuard;
431
432 if( mnFamily == SfxStyleFamily::Page )
433 {
434 throw IllegalAccessException();
435 }
436 return Reference<XInterface>(
437 static_cast<XStyle*>(SdStyleSheet::CreateEmptyUserStyle(*mxPool, mnFamily).get()));
438}
439
440Reference< XInterface > SAL_CALL SdStyleFamily::createInstanceWithArguments( const Sequence< Any >& )
441{
442 return createInstance();
443}
444
445// XComponent
446
448{
449 if( mxPool.is() )
450 mxPool.clear();
451
452 mpImpl.reset();
453}
454
455void SAL_CALL SdStyleFamily::addEventListener( const Reference< XEventListener >& )
456{
457}
458
459void SAL_CALL SdStyleFamily::removeEventListener( const Reference< XEventListener >& )
460{
461}
462
463// XPropertySet
464
465Reference<XPropertySetInfo> SdStyleFamily::getPropertySetInfo()
466{
467 OSL_FAIL( "###unexpected!" );
468 return Reference<XPropertySetInfo>();
469}
470
471void SdStyleFamily::setPropertyValue( const OUString& , const Any& )
472{
473 OSL_FAIL( "###unexpected!" );
474}
475
476Any SdStyleFamily::getPropertyValue( const OUString& PropertyName )
477{
478 if ( PropertyName != "DisplayName" )
479 {
480 throw UnknownPropertyException( "unknown property: " + PropertyName, static_cast<OWeakObject *>(this) );
481 }
482
483 SolarMutexGuard aGuard;
484 OUString sDisplayName;
485 switch( mnFamily )
486 {
487 case SfxStyleFamily::Page: sDisplayName = getName(); break;
488 case SfxStyleFamily::Frame: sDisplayName = SdResId(STR_CELL_STYLE_FAMILY); break;
489 default: sDisplayName = SdResId(STR_GRAPHICS_STYLE_FAMILY); break;
490 }
491 return Any( sDisplayName );
492}
493
494void SdStyleFamily::addPropertyChangeListener( const OUString& , const Reference<XPropertyChangeListener>& )
495{
496 OSL_FAIL( "###unexpected!" );
497}
498
499void SdStyleFamily::removePropertyChangeListener( const OUString& , const Reference<XPropertyChangeListener>& )
500{
501 OSL_FAIL( "###unexpected!" );
502}
503
504void SdStyleFamily::addVetoableChangeListener( const OUString& , const Reference<XVetoableChangeListener>& )
505{
506 OSL_FAIL( "###unexpected!" );
507}
508
509void SdStyleFamily::removeVetoableChangeListener( const OUString& , const Reference<XVetoableChangeListener>& )
510{
511 OSL_FAIL( "###unexpected!" );
512}
513
514/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _ADOIndex Index
SdStyleFamily(rtl::Reference< SfxStyleSheetPool > xPool, SfxStyleFamily nFamily)
creates the style family for the given SfxStyleFamily
Definition: stlfamily.cxx:99
rtl::Reference< SfxStyleSheetPool > mxPool
Definition: stlfamily.hxx:110
virtual sal_Int32 SAL_CALL getCount() override
Definition: stlfamily.cxx:321
virtual css::uno::Any SAL_CALL getPropertyValue(const OUString &PropertyName) override
Definition: stlfamily.cxx:476
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: stlfamily.cxx:176
virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override
Definition: stlfamily.cxx:465
virtual OUString SAL_CALL getImplementationName() override
Definition: stlfamily.cxx:171
virtual ~SdStyleFamily() override
Definition: stlfamily.cxx:114
virtual void SAL_CALL removePropertyChangeListener(const OUString &aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > &aListener) override
Definition: stlfamily.cxx:499
SfxStyleFamily mnFamily
Definition: stlfamily.hxx:109
SdStyleSheet * GetSheetByName(const OUString &rName)
Definition: stlfamily.cxx:136
virtual void SAL_CALL setName(const OUString &aName) override
Definition: stlfamily.cxx:208
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: stlfamily.cxx:181
virtual css::uno::Type SAL_CALL getElementType() override
Definition: stlfamily.cxx:293
virtual void SAL_CALL addEventListener(const css::uno::Reference< css::lang::XEventListener > &xListener) override
Definition: stlfamily.cxx:455
std::unique_ptr< SdStyleFamilyImpl > mpImpl
Definition: stlfamily.hxx:111
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
Definition: stlfamily.cxx:221
virtual sal_Bool SAL_CALL hasElements() override
Definition: stlfamily.cxx:298
void throwIfDisposed() const
Definition: stlfamily.cxx:119
virtual void SAL_CALL removeVetoableChangeListener(const OUString &PropertyName, const css::uno::Reference< css::beans::XVetoableChangeListener > &aListener) override
Definition: stlfamily.cxx:509
virtual css::uno::Any SAL_CALL getByIndex(sal_Int32 Index) override
Definition: stlfamily.cxx:344
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithArguments(const css::uno::Sequence< css::uno::Any > &aArguments) override
Definition: stlfamily.cxx:440
virtual OUString SAL_CALL getName() override
Definition: stlfamily.cxx:187
virtual css::uno::Any SAL_CALL getByName(const OUString &aName) override
Definition: stlfamily.cxx:214
SdStyleSheet * GetValidNewSheet(const css::uno::Any &rElement)
Definition: stlfamily.cxx:125
virtual void SAL_CALL replaceByName(const OUString &aName, const css::uno::Any &aElement) override
Definition: stlfamily.cxx:413
virtual void SAL_CALL addPropertyChangeListener(const OUString &aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > &xListener) override
Definition: stlfamily.cxx:494
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
Definition: stlfamily.cxx:259
virtual void SAL_CALL removeEventListener(const css::uno::Reference< css::lang::XEventListener > &aListener) override
Definition: stlfamily.cxx:459
virtual void SAL_CALL dispose() override
Definition: stlfamily.cxx:447
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstance() override
Definition: stlfamily.cxx:427
virtual void SAL_CALL addVetoableChangeListener(const OUString &PropertyName, const css::uno::Reference< css::beans::XVetoableChangeListener > &aListener) override
Definition: stlfamily.cxx:504
virtual void SAL_CALL removeByName(const OUString &Name) override
Definition: stlfamily.cxx:398
virtual void SAL_CALL setPropertyValue(const OUString &aPropertyName, const css::uno::Any &aValue) override
Definition: stlfamily.cxx:471
virtual void SAL_CALL insertByName(const OUString &aName, const css::uno::Any &aElement) override
Definition: stlfamily.cxx:382
OUString const & GetApiName() const
Definition: stlsheet.cxx:161
static OUString GetFamilyString(SfxStyleFamily eFamily)
Definition: stlsheet.cxx:696
virtual bool SetName(const OUString &rNewName, bool bReindexNow=true) override
Definition: stlsheet.cxx:891
static rtl::Reference< SdStyleSheet > CreateEmptyUserStyle(SfxStyleSheetBasePool &rPool, SfxStyleFamily eFamily)
Definition: stlsheet.cxx:716
void SetApiName(const OUString &rApiName)
Definition: stlsheet.cxx:156
css::uno::Type const & get()
rtl::Reference< interface_type > SAL_CALL get() const
int nCount
#define DBG_ASSERT(sCon, aError)
OUString sDisplayName
constexpr OUStringLiteral SD_LT_SEPARATOR
Definition: glob.hxx:49
sal_Int32 nIndex
OUString aName
Type
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
OUString SdResId(TranslateId aId)
Definition: sdmod.cxx:83
std::map< OUString, rtl::Reference< SdStyleSheet > > PresStyleMap
Definition: stlfamily.cxx:49
PresStyleMap & getStyleSheets()
Definition: stlfamily.cxx:63
OUString maLayoutName
Definition: stlfamily.cxx:54
unotools::WeakReference< SdPage > mxMasterPage
Definition: stlfamily.cxx:53
rtl::Reference< SfxStyleSheetPool > mxPool
Definition: stlfamily.cxx:57
PresStyleMap maStyleSheets
Definition: stlfamily.cxx:60
SfxStyleFamily
unsigned char sal_Bool
@ Page
Definition: unomodel.hxx:385