bump product version to 7.2.5.1
[LibreOffice.git] / svx / source / gallery2 / gallery1.cxx
blobb6b5b00d9a0f51e57d299bed352d2092cf51b0d4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <config_features.h>
22 #if defined(MACOSX) && HAVE_FEATURE_READONLY_INSTALLSET
23 #define __ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES 0
24 #include <premac.h>
25 #include <Foundation/Foundation.h>
26 #include <postmac.h>
27 #endif
29 #include <sal/config.h>
31 #include <comphelper/processfactory.hxx>
32 #include <ucbhelper/content.hxx>
33 #include <com/sun/star/ucb/ContentCreationException.hpp>
34 #include <unotools/configmgr.hxx>
35 #include <unotools/ucbstreamhelper.hxx>
36 #include <unotools/pathoptions.hxx>
37 #include <svx/dialmgr.hxx>
38 #include <svx/gallery.hxx>
39 #include <svx/galleryobjectcollection.hxx>
40 #include <svx/strings.hrc>
41 #include <strings.hxx>
42 #include <svx/galmisc.hxx>
43 #include <svx/galtheme.hxx>
44 #include <svx/gallery1.hxx>
45 #include <svx/gallerybinaryengineentry.hxx>
46 #include <vcl/weld.hxx>
47 #include <com/sun/star/sdbc/XResultSet.hpp>
48 #include <com/sun/star/ucb/XContentAccess.hpp>
49 #include <memory>
52 using namespace ::com::sun::star;
55 const std::pair<sal_uInt16, const char*> aUnlocalized[] =
57 { GALLERY_THEME_HOMEPAGE, RID_GALLERYSTR_THEME_HTMLBUTTONS },
58 { GALLERY_THEME_POWERPOINT, RID_GALLERYSTR_THEME_POWERPOINT },
59 { GALLERY_THEME_USERSOUNDS, RID_GALLERYSTR_THEME_USERSOUNDS },
60 { GALLERY_THEME_DUMMY5, RID_GALLERYSTR_THEME_DUMMY5 },
61 { GALLERY_THEME_RULERS, RID_GALLERYSTR_THEME_RULERS },
62 { GALLERY_THEME_FONTWORK, RID_GALLERYSTR_THEME_FONTWORK },
63 { GALLERY_THEME_FONTWORK_VERTICAL, RID_GALLERYSTR_THEME_FONTWORK_VERTICAL }
66 const std::pair<sal_uInt16, const char*> aLocalized[] =
68 { RID_GALLERY_THEME_3D, RID_GALLERYSTR_THEME_3D },
69 { RID_GALLERY_THEME_ANIMATIONS, RID_GALLERYSTR_THEME_ANIMATIONS },
70 { RID_GALLERY_THEME_BULLETS, RID_GALLERYSTR_THEME_BULLETS },
71 { RID_GALLERY_THEME_OFFICE, RID_GALLERYSTR_THEME_OFFICE },
72 { RID_GALLERY_THEME_FLAGS, RID_GALLERYSTR_THEME_FLAGS },
73 { RID_GALLERY_THEME_FLOWCHARTS, RID_GALLERYSTR_THEME_FLOWCHARTS },
74 { RID_GALLERY_THEME_EMOTICONS, RID_GALLERYSTR_THEME_EMOTICONS },
75 { RID_GALLERY_THEME_PHOTOS, RID_GALLERYSTR_THEME_PHOTOS },
76 { RID_GALLERY_THEME_BACKGROUNDS, RID_GALLERYSTR_THEME_BACKGROUNDS },
77 { RID_GALLERY_THEME_HOMEPAGE, RID_GALLERYSTR_THEME_HOMEPAGE },
78 { RID_GALLERY_THEME_INTERACTION, RID_GALLERYSTR_THEME_INTERACTION },
79 { RID_GALLERY_THEME_MAPS, RID_GALLERYSTR_THEME_MAPS },
80 { RID_GALLERY_THEME_PEOPLE, RID_GALLERYSTR_THEME_PEOPLE },
81 { RID_GALLERY_THEME_SURFACES, RID_GALLERYSTR_THEME_SURFACES },
82 { RID_GALLERY_THEME_SOUNDS, RID_GALLERYSTR_THEME_SOUNDS },
83 { RID_GALLERY_THEME_SYMBOLS, RID_GALLERYSTR_THEME_SYMBOLS },
84 { RID_GALLERY_THEME_MYTHEME, RID_GALLERYSTR_THEME_MYTHEME },
86 { RID_GALLERY_THEME_ARROWS, RID_GALLERYSTR_THEME_ARROWS },
87 { RID_GALLERY_THEME_BALLOONS, RID_GALLERYSTR_THEME_BALLOONS },
88 { RID_GALLERY_THEME_KEYBOARD, RID_GALLERYSTR_THEME_KEYBOARD },
89 { RID_GALLERY_THEME_TIME, RID_GALLERYSTR_THEME_TIME },
90 { RID_GALLERY_THEME_PRESENTATION, RID_GALLERYSTR_THEME_PRESENTATION },
91 { RID_GALLERY_THEME_CALENDAR, RID_GALLERYSTR_THEME_CALENDAR },
92 { RID_GALLERY_THEME_NAVIGATION, RID_GALLERYSTR_THEME_NAVIGATION },
93 { RID_GALLERY_THEME_COMMUNICATION, RID_GALLERYSTR_THEME_COMMUNICATION },
94 { RID_GALLERY_THEME_FINANCES, RID_GALLERYSTR_THEME_FINANCES },
95 { RID_GALLERY_THEME_COMPUTER, RID_GALLERYSTR_THEME_COMPUTER },
97 { RID_GALLERY_THEME_CLIMA, RID_GALLERYSTR_THEME_CLIMA },
98 { RID_GALLERY_THEME_EDUCATION, RID_GALLERYSTR_THEME_EDUCATION },
99 { RID_GALLERY_THEME_TROUBLE, RID_GALLERYSTR_THEME_TROUBLE },
100 { RID_GALLERY_THEME_SCREENBEANS, RID_GALLERYSTR_THEME_SCREENBEANS },
102 { RID_GALLERY_THEME_COMPUTERS, RID_GALLERYSTR_THEME_COMPUTERS },
103 { RID_GALLERY_THEME_DIAGRAMS, RID_GALLERYSTR_THEME_DIAGRAMS },
104 { RID_GALLERY_THEME_ENVIRONMENT, RID_GALLERYSTR_THEME_ENVIRONMENT },
105 { RID_GALLERY_THEME_FINANCE, RID_GALLERYSTR_THEME_FINANCE },
106 { RID_GALLERY_THEME_TRANSPORT, RID_GALLERYSTR_THEME_TRANSPORT },
107 { RID_GALLERY_THEME_TXTSHAPES, RID_GALLERYSTR_THEME_TXTSHAPES }
110 GalleryThemeEntry::GalleryThemeEntry( bool bCreateUniqueURL,
111 const INetURLObject& rBaseURL, const OUString& rName,
112 bool _bReadOnly, bool _bNewFile,
113 sal_uInt32 _nId, bool _bThemeNameFromResource ) :
114 nId ( _nId ),
115 bReadOnly ( _bReadOnly ),
116 bThemeNameFromResource ( _bThemeNameFromResource )
118 INetURLObject aURL( rBaseURL );
119 DBG_ASSERT( aURL.GetProtocol() != INetProtocol::NotValid, "invalid URL" );
121 if (bCreateUniqueURL)
123 GalleryBinaryEngineEntry::CreateUniqueURL(rBaseURL,aURL);
126 mpGalleryStorageEngineEntry = std::make_unique<GalleryBinaryEngineEntry>();
127 setStorageLocations(aURL);
129 SetModified( _bNewFile );
131 aName = mpGalleryStorageEngineEntry->ReadStrFromIni( u"name" );
133 // This is awful - we shouldn't use these resources if we
134 // possibly can avoid them
135 if( aName.isEmpty() && nId && bThemeNameFromResource )
137 //some of these are supposed to *not* be localized
138 //so catch them before looking up the resource
139 for (size_t i = 0; i < SAL_N_ELEMENTS(aUnlocalized); ++i)
141 if (aUnlocalized[i].first == nId)
143 aName = OUString::createFromAscii(aUnlocalized[i].second);
144 break;
147 //look up the rest of the ids in string resources
148 if (aName.isEmpty())
150 for (size_t i = 0; i < SAL_N_ELEMENTS(aLocalized); ++i)
152 if (aLocalized[i].first == nId)
154 aName = SvxResId(aLocalized[i].second);
155 break;
161 if( aName.isEmpty() )
162 aName = rName;
165 void GalleryThemeEntry::setStorageLocations(INetURLObject & rURL)
167 mpGalleryStorageEngineEntry->setStorageLocations(rURL);
170 GalleryTheme* GalleryThemeEntry::createGalleryTheme(Gallery* pGallery)
172 return new GalleryTheme(pGallery,this);
175 std::unique_ptr<GalleryBinaryEngine> GalleryThemeEntry::createGalleryStorageEngine(GalleryObjectCollection& mrGalleryObjectCollection)
177 return mpGalleryStorageEngineEntry->createGalleryStorageEngine(mrGalleryObjectCollection, bReadOnly);
180 void GalleryTheme::InsertAllThemes(weld::ComboBox& rListBox)
182 for (size_t i = 0; i < SAL_N_ELEMENTS(aUnlocalized); ++i)
183 rListBox.append_text(OUString::createFromAscii(aUnlocalized[i].second));
185 for (size_t i = 0; i < SAL_N_ELEMENTS(aLocalized); ++i)
186 rListBox.append_text(SvxResId(aLocalized[i].second));
189 void GalleryThemeEntry::SetName( const OUString& rNewName )
191 if( aName != rNewName )
193 aName = rNewName;
194 SetModified( true );
195 bThemeNameFromResource = false;
199 void GalleryThemeEntry::SetId( sal_uInt32 nNewId, bool bResetThemeName )
201 nId = nNewId;
202 SetModified( true );
203 bThemeNameFromResource = ( nId && bResetThemeName );
206 void GalleryThemeEntry::removeTheme()
208 mpGalleryStorageEngineEntry->removeTheme();
211 class GalleryThemeCacheEntry
213 private:
215 const GalleryThemeEntry* mpThemeEntry;
216 std::unique_ptr<GalleryTheme> mpTheme;
218 public:
220 GalleryThemeCacheEntry( const GalleryThemeEntry* pThemeEntry, std::unique_ptr<GalleryTheme> pTheme ) :
221 mpThemeEntry( pThemeEntry ), mpTheme( std::move(pTheme) ) {}
223 const GalleryThemeEntry* GetThemeEntry() const { return mpThemeEntry; }
224 GalleryTheme* GetTheme() const { return mpTheme.get(); }
228 Gallery::Gallery( const OUString& rMultiPath )
229 : bMultiPath ( false )
231 ImplLoad( rMultiPath );
234 Gallery::~Gallery()
238 Gallery* Gallery::GetGalleryInstance()
240 // note: this would deadlock if it used osl::Mutex::getGlobalMutex()
241 static Gallery *const s_pGallery(
242 utl::ConfigManager::IsFuzzing() ? nullptr :
243 new Gallery(SvtPathOptions().GetGalleryPath()));
245 return s_pGallery;
248 void Gallery::ImplLoad( const OUString& rMultiPath )
250 bool bIsReadOnlyDir {false};
252 bMultiPath = !rMultiPath.isEmpty();
254 INetURLObject aCurURL(SvtPathOptions().GetConfigPath());
255 ImplLoadSubDirs( aCurURL, bIsReadOnlyDir );
257 if( !bIsReadOnlyDir )
258 aUserURL = aCurURL;
260 if( bMultiPath )
262 bool bIsRelURL {true};
263 sal_Int32 nIdx {0};
266 aCurURL = INetURLObject(rMultiPath.getToken(0, ';', nIdx));
267 if (bIsRelURL)
269 aRelURL = aCurURL;
270 bIsRelURL = false;
273 ImplLoadSubDirs( aCurURL, bIsReadOnlyDir );
275 if( !bIsReadOnlyDir )
276 aUserURL = aCurURL;
278 while (nIdx>0);
280 else
281 aRelURL = INetURLObject( rMultiPath );
283 DBG_ASSERT( aUserURL.GetProtocol() != INetProtocol::NotValid, "no writable Gallery user directory available" );
284 DBG_ASSERT( aRelURL.GetProtocol() != INetProtocol::NotValid, "invalid URL" );
287 void Gallery::ImplLoadSubDirs( const INetURLObject& rBaseURL, bool& rbDirIsReadOnly )
289 rbDirIsReadOnly = false;
293 uno::Reference< ucb::XCommandEnvironment > xEnv;
294 ::ucbhelper::Content aCnt( rBaseURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), xEnv, comphelper::getProcessComponentContext() );
296 uno::Sequence<OUString> aProps { "Url" };
298 uno::Reference< sdbc::XResultSet > xResultSet( aCnt.createCursor( aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY ) );
300 #if defined(MACOSX) && HAVE_FEATURE_READONLY_INSTALLSET
301 if( rBaseURL.GetProtocol() == INetProtocol::File )
303 const char *appBundle = [[[NSBundle mainBundle] bundlePath] UTF8String];
304 OUString path = rBaseURL.GetURLPath();
305 if( path.startsWith( OUString(OUString( appBundle, strlen( appBundle ), RTL_TEXTENCODING_UTF8 ) + "/") ) )
306 rbDirIsReadOnly = true;
308 #else
311 // check readonlyness the very hard way
312 INetURLObject aTestURL( rBaseURL );
314 aTestURL.Append( u"cdefghij.klm" );
315 std::unique_ptr<SvStream> pTestStm(::utl::UcbStreamHelper::CreateStream( aTestURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), StreamMode::WRITE ));
317 if( pTestStm )
319 pTestStm->WriteInt32( sal_Int32(1) );
321 if( pTestStm->GetError() )
322 rbDirIsReadOnly = true;
324 pTestStm.reset();
325 KillFile( aTestURL );
327 else
328 rbDirIsReadOnly = true;
330 catch( const ucb::ContentCreationException& )
333 catch( const uno::RuntimeException& )
336 catch( const uno::Exception& )
339 #endif
340 if( xResultSet.is() )
342 uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
344 if( xContentAccess.is() )
346 static constexpr OUStringLiteral s_sTitle = u"Title";
347 static constexpr OUStringLiteral s_sIsReadOnly = u"IsReadOnly";
349 while( xResultSet->next() )
351 INetURLObject aThmURL( xContentAccess->queryContentIdentifierString() );
353 if (aThmURL.GetFileExtension().equalsIgnoreAsciiCase("thm"))
355 INetURLObject aSdgURL( aThmURL); aSdgURL.SetExtension( u"sdg" );
356 INetURLObject aSdvURL( aThmURL ); aSdvURL.SetExtension( u"sdv" );
360 ::ucbhelper::Content aThmCnt( aThmURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), xEnv, comphelper::getProcessComponentContext() );
361 ::ucbhelper::Content aSdgCnt( aSdgURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), xEnv, comphelper::getProcessComponentContext() );
362 ::ucbhelper::Content aSdvCnt( aSdvURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), xEnv, comphelper::getProcessComponentContext() );
364 OUString aTitle;
367 aThmCnt.getPropertyValue( s_sTitle ) >>= aTitle;
369 catch( const uno::RuntimeException& )
372 catch( const uno::Exception& )
376 if( !aTitle.isEmpty() )
378 bool bReadOnly = false;
382 aThmCnt.getPropertyValue( s_sIsReadOnly ) >>= bReadOnly;
384 catch( const uno::RuntimeException& )
387 catch( const uno::Exception& )
391 if( !bReadOnly )
395 aSdgCnt.getPropertyValue( s_sTitle ) >>= aTitle;
397 catch( const css::uno::RuntimeException& )
400 catch( const css::uno::Exception& )
404 if( !aTitle.isEmpty() )
408 aSdgCnt.getPropertyValue( s_sIsReadOnly ) >>= bReadOnly;
410 catch( const uno::RuntimeException& )
413 catch( const uno::Exception& )
419 if( !bReadOnly )
423 aSdvCnt.getPropertyValue( s_sTitle ) >>= aTitle;
425 catch( const css::uno::RuntimeException& )
428 catch( const css::uno::Exception& )
432 if( !aTitle.isEmpty() )
436 aSdvCnt.getPropertyValue( s_sIsReadOnly ) >>= bReadOnly;
438 catch( const uno::RuntimeException& )
441 catch( const uno::Exception& )
447 GalleryThemeEntry* pEntry = GalleryBinaryEngineEntry::CreateThemeEntry( aThmURL, rbDirIsReadOnly || bReadOnly );
449 if( pEntry )
450 aThemeList.emplace_back( pEntry );
453 catch( const ucb::ContentCreationException& )
456 catch( const uno::RuntimeException& )
459 catch( const uno::Exception& )
467 catch( const ucb::ContentCreationException& )
470 catch( const uno::RuntimeException& )
473 catch( const uno::Exception& )
478 GalleryThemeEntry* Gallery::ImplGetThemeEntry( std::u16string_view rThemeName )
480 if( !rThemeName.empty() )
482 for ( size_t i = 0, n = aThemeList.size(); i < n; ++i )
483 if( rThemeName == aThemeList[ i ]->GetThemeName() )
484 return aThemeList[ i ].get();
487 return nullptr;
490 OUString Gallery::GetThemeName( sal_uInt32 nThemeId ) const
492 GalleryThemeEntry* pFound = nullptr;
494 for ( size_t i = 0, n = aThemeList.size(); i < n && !pFound; ++i )
496 GalleryThemeEntry* pEntry = aThemeList[ i ].get();
497 if( nThemeId == pEntry->GetId() )
498 pFound = pEntry;
501 // try fallback, if no entry was found
502 if( !pFound )
504 OUString aFallback;
506 switch( nThemeId )
508 case GALLERY_THEME_3D:
509 aFallback = SvxResId(RID_GALLERYSTR_THEME_3D);
510 break;
511 case GALLERY_THEME_BULLETS:
512 aFallback = SvxResId(RID_GALLERYSTR_THEME_BULLETS);
513 break;
514 case GALLERY_THEME_HOMEPAGE:
515 aFallback = SvxResId(RID_GALLERYSTR_THEME_HOMEPAGE);
516 break;
517 case GALLERY_THEME_POWERPOINT:
518 aFallback = RID_GALLERYSTR_THEME_POWERPOINT;
519 break;
520 case GALLERY_THEME_FONTWORK:
521 aFallback = RID_GALLERYSTR_THEME_FONTWORK;
522 break;
523 case GALLERY_THEME_FONTWORK_VERTICAL:
524 aFallback = RID_GALLERYSTR_THEME_FONTWORK_VERTICAL;
525 break;
526 case GALLERY_THEME_SOUNDS:
527 aFallback = SvxResId(RID_GALLERYSTR_THEME_SOUNDS);
528 break;
529 case RID_GALLERY_THEME_ARROWS:
530 aFallback = SvxResId(RID_GALLERYSTR_THEME_ARROWS);
531 break;
532 case RID_GALLERY_THEME_COMPUTERS:
533 aFallback = SvxResId(RID_GALLERYSTR_THEME_COMPUTERS);
534 break;
535 case RID_GALLERY_THEME_DIAGRAMS:
536 aFallback = SvxResId(RID_GALLERYSTR_THEME_DIAGRAMS);
537 break;
538 case RID_GALLERY_THEME_EDUCATION:
539 aFallback = SvxResId(RID_GALLERYSTR_THEME_EDUCATION);
540 break;
541 case RID_GALLERY_THEME_ENVIRONMENT:
542 aFallback = SvxResId(RID_GALLERYSTR_THEME_ENVIRONMENT);
543 break;
544 case RID_GALLERY_THEME_FINANCE:
545 aFallback = SvxResId(RID_GALLERYSTR_THEME_FINANCE);
546 break;
547 case RID_GALLERY_THEME_PEOPLE:
548 aFallback = SvxResId(RID_GALLERYSTR_THEME_PEOPLE);
549 break;
550 case RID_GALLERY_THEME_SYMBOLS:
551 aFallback = SvxResId(RID_GALLERYSTR_THEME_SYMBOLS);
552 break;
553 case RID_GALLERY_THEME_TRANSPORT:
554 aFallback = SvxResId(RID_GALLERYSTR_THEME_TRANSPORT);
555 break;
556 case RID_GALLERY_THEME_TXTSHAPES:
557 aFallback = SvxResId(RID_GALLERYSTR_THEME_TXTSHAPES);
558 break;
559 default:
560 break;
563 pFound = const_cast<Gallery*>(this)->ImplGetThemeEntry(aFallback);
566 return( pFound ? pFound->GetThemeName() : OUString() );
569 bool Gallery::HasTheme( std::u16string_view rThemeName )
571 return( ImplGetThemeEntry( rThemeName ) != nullptr );
574 bool Gallery::CreateTheme( const OUString& rThemeName )
576 bool bRet = false;
578 if( !HasTheme( rThemeName ) && ( GetUserURL().GetProtocol() != INetProtocol::NotValid ) )
580 INetURLObject aURL( GetUserURL() );
581 aURL.Append( rThemeName );
582 GalleryThemeEntry* pNewEntry = new GalleryThemeEntry(
583 true, aURL, rThemeName,
584 false, true, 0, false );
586 aThemeList.emplace_back( pNewEntry );
587 delete pNewEntry->createGalleryTheme( this );
588 Broadcast( GalleryHint( GalleryHintType::THEME_CREATED, rThemeName ) );
589 bRet = true;
592 return bRet;
595 void Gallery::RenameTheme( const OUString& rOldName, const OUString& rNewName )
597 GalleryThemeEntry* pThemeEntry = ImplGetThemeEntry( rOldName );
599 // check if the new theme name is already present
600 if( !pThemeEntry || HasTheme( rNewName ) || pThemeEntry->IsReadOnly() )
601 return;
603 SfxListener aListener;
604 GalleryTheme* pThm = AcquireTheme( rOldName, aListener );
606 if( pThm )
608 pThemeEntry->SetName( rNewName );
609 if (pThm->pThm->IsModified())
610 if (!pThm->mpGalleryStorageEngine->implWrite(*pThm, pThm->pThm))
611 pThm->ImplSetModified(false);
613 Broadcast( GalleryHint( GalleryHintType::THEME_RENAMED, rOldName, pThm->GetName() ) );
614 ReleaseTheme( pThm, aListener );
618 bool Gallery::RemoveTheme( const OUString& rThemeName )
620 GalleryThemeEntry* pThemeEntry = ImplGetThemeEntry( rThemeName );
621 bool bRet = false;
623 if( pThemeEntry && !pThemeEntry->IsReadOnly() )
625 Broadcast( GalleryHint( GalleryHintType::CLOSE_THEME, rThemeName ) );
627 SfxListener aListener;
628 GalleryTheme* pThm = AcquireTheme( rThemeName, aListener );
630 if( pThm )
632 ReleaseTheme(pThm, aListener);
633 pThemeEntry->removeTheme();
636 auto it = std::find_if(aThemeList.begin(), aThemeList.end(),
637 [&pThemeEntry](const std::unique_ptr<GalleryThemeEntry>& rpEntry) { return pThemeEntry == rpEntry.get(); });
638 if (it != aThemeList.end())
639 aThemeList.erase( it );
641 Broadcast( GalleryHint( GalleryHintType::THEME_REMOVED, rThemeName ) );
643 bRet = true;
646 return bRet;
649 std::unique_ptr<GalleryTheme> GalleryThemeEntry::getCachedTheme(Gallery* pGallery)
651 std::unique_ptr<GalleryTheme> pNewTheme;
652 pNewTheme.reset(createGalleryTheme(pGallery));
653 mpGalleryStorageEngineEntry->getCachedTheme(pNewTheme);
654 return pNewTheme;
657 GalleryTheme* Gallery::ImplGetCachedTheme(GalleryThemeEntry* pThemeEntry)
659 GalleryTheme* pTheme = nullptr;
661 if( pThemeEntry )
663 auto it = std::find_if(aThemeCache.begin(), aThemeCache.end(),
664 [&pThemeEntry](const GalleryThemeCacheEntry* pEntry) { return pThemeEntry == pEntry->GetThemeEntry(); });
665 if (it != aThemeCache.end())
666 pTheme = (*it)->GetTheme();
668 if( !pTheme )
670 std::unique_ptr<GalleryTheme> pNewTheme = pThemeEntry->getCachedTheme(this);
671 if (pNewTheme)
673 aThemeCache.push_back( new GalleryThemeCacheEntry( pThemeEntry, std::move(pNewTheme) ));
674 pTheme = aThemeCache.back()->GetTheme();
679 return pTheme;
682 void Gallery::ImplDeleteCachedTheme( GalleryTheme const * pTheme )
684 auto it = std::find_if(aThemeCache.begin(), aThemeCache.end(),
685 [&pTheme](const GalleryThemeCacheEntry* pEntry) { return pTheme == pEntry->GetTheme(); });
686 if (it != aThemeCache.end())
688 delete *it;
689 aThemeCache.erase(it);
693 GalleryTheme* Gallery::AcquireTheme( std::u16string_view rThemeName, SfxListener& rListener )
695 GalleryTheme* pTheme = nullptr;
696 GalleryThemeEntry* pThemeEntry = ImplGetThemeEntry( rThemeName );
698 if( pThemeEntry )
700 pTheme = ImplGetCachedTheme( pThemeEntry );
701 if (pTheme)
702 rListener.StartListening(*pTheme, DuplicateHandling::Prevent);
704 return pTheme;
707 void Gallery::ReleaseTheme( GalleryTheme* pTheme, SfxListener& rListener )
709 if( pTheme )
711 rListener.EndListening( *pTheme );
713 if( !pTheme->HasListeners() )
714 ImplDeleteCachedTheme( pTheme );
718 bool GalleryThemeEntry::IsDefault() const
720 return nId > 0 && nId != GALLERY_THEME_MYTHEME;
723 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */