tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / svx / source / gallery2 / gallery1.cxx
blob899c606f09623b6b620e69c62ca821eaa6d8d242
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 <o3tl/string_view.hxx>
35 #include <comphelper/configuration.hxx>
36 #include <unotools/ucbstreamhelper.hxx>
37 #include <unotools/pathoptions.hxx>
38 #include <svx/dialmgr.hxx>
39 #include <svx/gallery.hxx>
40 #include <svx/galleryobjectcollection.hxx>
41 #include <svx/strings.hrc>
42 #include <strings.hxx>
43 #include <svx/galmisc.hxx>
44 #include <svx/galtheme.hxx>
45 #include <svx/gallery1.hxx>
46 #include <galleryfilestorageentry.hxx>
47 #include <vcl/weld.hxx>
48 #include <com/sun/star/sdbc/XResultSet.hpp>
49 #include <com/sun/star/ucb/XContentAccess.hpp>
50 #include <memory>
53 using namespace ::com::sun::star;
56 constexpr std::pair<sal_uInt16, OUString> aUnlocalized[] =
58 { GALLERY_THEME_HOMEPAGE, RID_GALLERYSTR_THEME_HTMLBUTTONS },
59 { GALLERY_THEME_POWERPOINT, RID_GALLERYSTR_THEME_POWERPOINT },
60 { GALLERY_THEME_USERSOUNDS, RID_GALLERYSTR_THEME_USERSOUNDS },
61 { GALLERY_THEME_DUMMY5, RID_GALLERYSTR_THEME_DUMMY5 },
62 { GALLERY_THEME_RULERS, RID_GALLERYSTR_THEME_RULERS },
63 { GALLERY_THEME_FONTWORK, RID_GALLERYSTR_THEME_FONTWORK },
64 { GALLERY_THEME_FONTWORK_VERTICAL, RID_GALLERYSTR_THEME_FONTWORK_VERTICAL }
67 const std::pair<sal_uInt16, TranslateId> aLocalized[] =
69 { RID_GALLERY_THEME_3D, RID_GALLERYSTR_THEME_3D },
70 { RID_GALLERY_THEME_ANIMATIONS, RID_GALLERYSTR_THEME_ANIMATIONS },
71 { RID_GALLERY_THEME_BULLETS, RID_GALLERYSTR_THEME_BULLETS },
72 { RID_GALLERY_THEME_OFFICE, RID_GALLERYSTR_THEME_OFFICE },
73 { RID_GALLERY_THEME_FLAGS, RID_GALLERYSTR_THEME_FLAGS },
74 { RID_GALLERY_THEME_FLOWCHARTS, RID_GALLERYSTR_THEME_FLOWCHARTS },
75 { RID_GALLERY_THEME_EMOTICONS, RID_GALLERYSTR_THEME_EMOTICONS },
76 { RID_GALLERY_THEME_PHOTOS, RID_GALLERYSTR_THEME_PHOTOS },
77 { RID_GALLERY_THEME_BACKGROUNDS, RID_GALLERYSTR_THEME_BACKGROUNDS },
78 { RID_GALLERY_THEME_HOMEPAGE, RID_GALLERYSTR_THEME_HOMEPAGE },
79 { RID_GALLERY_THEME_INTERACTION, RID_GALLERYSTR_THEME_INTERACTION },
80 { RID_GALLERY_THEME_MAPS, RID_GALLERYSTR_THEME_MAPS },
81 { RID_GALLERY_THEME_PEOPLE, RID_GALLERYSTR_THEME_PEOPLE },
82 { RID_GALLERY_THEME_SURFACES, RID_GALLERYSTR_THEME_SURFACES },
83 { RID_GALLERY_THEME_SOUNDS, RID_GALLERYSTR_THEME_SOUNDS },
84 { RID_GALLERY_THEME_SYMBOLS, RID_GALLERYSTR_THEME_SYMBOLS },
85 { RID_GALLERY_THEME_MYTHEME, RID_GALLERYSTR_THEME_MYTHEME },
87 { RID_GALLERY_THEME_ARROWS, RID_GALLERYSTR_THEME_ARROWS },
88 { RID_GALLERY_THEME_BALLOONS, RID_GALLERYSTR_THEME_BALLOONS },
89 { RID_GALLERY_THEME_KEYBOARD, RID_GALLERYSTR_THEME_KEYBOARD },
90 { RID_GALLERY_THEME_TIME, RID_GALLERYSTR_THEME_TIME },
91 { RID_GALLERY_THEME_PRESENTATION, RID_GALLERYSTR_THEME_PRESENTATION },
92 { RID_GALLERY_THEME_CALENDAR, RID_GALLERYSTR_THEME_CALENDAR },
93 { RID_GALLERY_THEME_NAVIGATION, RID_GALLERYSTR_THEME_NAVIGATION },
94 { RID_GALLERY_THEME_COMMUNICATION, RID_GALLERYSTR_THEME_COMMUNICATION },
95 { RID_GALLERY_THEME_FINANCES, RID_GALLERYSTR_THEME_FINANCES },
96 { RID_GALLERY_THEME_COMPUTER, RID_GALLERYSTR_THEME_COMPUTER },
98 { RID_GALLERY_THEME_CLIMA, RID_GALLERYSTR_THEME_CLIMA },
99 { RID_GALLERY_THEME_EDUCATION, RID_GALLERYSTR_THEME_EDUCATION },
100 { RID_GALLERY_THEME_TROUBLE, RID_GALLERYSTR_THEME_TROUBLE },
101 { RID_GALLERY_THEME_SCREENBEANS, RID_GALLERYSTR_THEME_SCREENBEANS },
103 { RID_GALLERY_THEME_COMPUTERS, RID_GALLERYSTR_THEME_COMPUTERS },
104 { RID_GALLERY_THEME_DIAGRAMS, RID_GALLERYSTR_THEME_DIAGRAMS },
105 { RID_GALLERY_THEME_ENVIRONMENT, RID_GALLERYSTR_THEME_ENVIRONMENT },
106 { RID_GALLERY_THEME_FINANCE, RID_GALLERYSTR_THEME_FINANCE },
107 { RID_GALLERY_THEME_TRANSPORT, RID_GALLERYSTR_THEME_TRANSPORT },
108 { RID_GALLERY_THEME_TXTSHAPES, RID_GALLERYSTR_THEME_TXTSHAPES }
111 GalleryThemeEntry::GalleryThemeEntry( bool bCreateUniqueURL,
112 const INetURLObject& rBaseURL, const OUString& rName,
113 bool _bReadOnly, bool _bNewFile,
114 sal_uInt32 _nId, bool _bThemeNameFromResource ) :
115 nId ( _nId ),
116 bReadOnly ( _bReadOnly ),
117 bThemeNameFromResource ( _bThemeNameFromResource )
119 INetURLObject aURL( rBaseURL );
120 DBG_ASSERT( aURL.GetProtocol() != INetProtocol::NotValid, "invalid URL" );
122 if (bCreateUniqueURL)
124 GalleryFileStorageEntry::CreateUniqueURL(rBaseURL,aURL);
127 mpGalleryStorageEngineEntry = std::make_unique<GalleryFileStorageEntry>();
128 setStorageLocations(aURL);
130 SetModified( _bNewFile );
132 aName = mpGalleryStorageEngineEntry->ReadStrFromIni( u"name" );
134 // This is awful - we shouldn't use these resources if we
135 // possibly can avoid them
136 if( aName.isEmpty() && nId && bThemeNameFromResource )
138 //some of these are supposed to *not* be localized
139 //so catch them before looking up the resource
140 for (size_t i = 0; i < SAL_N_ELEMENTS(aUnlocalized); ++i)
142 if (aUnlocalized[i].first == nId)
144 aName = aUnlocalized[i].second;
145 break;
148 //look up the rest of the ids in string resources
149 if (aName.isEmpty())
151 for (size_t i = 0; i < SAL_N_ELEMENTS(aLocalized); ++i)
153 if (aLocalized[i].first == nId)
155 aName = SvxResId(aLocalized[i].second);
156 break;
162 if( aName.isEmpty() )
163 aName = rName;
166 GalleryThemeEntry::~GalleryThemeEntry()
169 void GalleryThemeEntry::setStorageLocations(INetURLObject & rURL)
171 mpGalleryStorageEngineEntry->setStorageLocations(rURL);
174 GalleryTheme* GalleryThemeEntry::createGalleryTheme(Gallery* pGallery)
176 return new GalleryTheme(pGallery,this);
179 std::unique_ptr<GalleryFileStorage> GalleryThemeEntry::createGalleryStorageEngine(GalleryObjectCollection& mrGalleryObjectCollection)
181 return mpGalleryStorageEngineEntry->createGalleryStorageEngine(mrGalleryObjectCollection, bReadOnly);
184 void GalleryTheme::InsertAllThemes(weld::ComboBox& rListBox)
186 for (size_t i = 0; i < SAL_N_ELEMENTS(aUnlocalized); ++i)
187 rListBox.append_text(aUnlocalized[i].second);
189 for (size_t i = 0; i < SAL_N_ELEMENTS(aLocalized); ++i)
190 rListBox.append_text(SvxResId(aLocalized[i].second));
193 void GalleryThemeEntry::SetName( const OUString& rNewName )
195 if( aName != rNewName )
197 aName = rNewName;
198 SetModified( true );
199 bThemeNameFromResource = false;
203 void GalleryThemeEntry::SetId( sal_uInt32 nNewId, bool bResetThemeName )
205 nId = nNewId;
206 SetModified( true );
207 bThemeNameFromResource = ( nId && bResetThemeName );
210 void GalleryThemeEntry::removeTheme()
212 mpGalleryStorageEngineEntry->removeTheme();
215 class GalleryThemeCacheEntry
217 private:
219 const GalleryThemeEntry* mpThemeEntry;
220 std::unique_ptr<GalleryTheme> mpTheme;
222 public:
224 GalleryThemeCacheEntry( const GalleryThemeEntry* pThemeEntry, std::unique_ptr<GalleryTheme> pTheme ) :
225 mpThemeEntry( pThemeEntry ), mpTheme( std::move(pTheme) ) {}
227 const GalleryThemeEntry* GetThemeEntry() const { return mpThemeEntry; }
228 GalleryTheme* GetTheme() const { return mpTheme.get(); }
232 Gallery::Gallery( std::u16string_view rMultiPath )
233 : bMultiPath ( false )
235 ImplLoad( rMultiPath );
238 Gallery::~Gallery()
242 Gallery* Gallery::GetGalleryInstance()
244 // note: this would deadlock if it used osl::Mutex::getGlobalMutex()
245 static Gallery *const s_pGallery(
246 comphelper::IsFuzzing() ? nullptr :
247 new Gallery(SvtPathOptions().GetGalleryPath()));
249 return s_pGallery;
252 void Gallery::ImplLoad( std::u16string_view rMultiPath )
254 bool bIsReadOnlyDir {false};
256 bMultiPath = !rMultiPath.empty();
258 INetURLObject aCurURL(SvtPathOptions().GetConfigPath());
259 ImplLoadSubDirs( aCurURL, bIsReadOnlyDir );
261 if( !bIsReadOnlyDir )
262 aUserURL = aCurURL;
264 if( bMultiPath )
266 bool bIsRelURL {true};
267 sal_Int32 nIdx {0};
270 aCurURL = INetURLObject(o3tl::getToken(rMultiPath, 0, ';', nIdx));
271 if (bIsRelURL)
273 aRelURL = aCurURL;
274 bIsRelURL = false;
277 ImplLoadSubDirs( aCurURL, bIsReadOnlyDir );
279 if( !bIsReadOnlyDir )
280 aUserURL = aCurURL;
282 while (nIdx>0);
284 else
285 aRelURL = INetURLObject( rMultiPath );
287 DBG_ASSERT( aUserURL.GetProtocol() != INetProtocol::NotValid, "no writable Gallery user directory available" );
288 DBG_ASSERT( aRelURL.GetProtocol() != INetProtocol::NotValid, "invalid URL" );
291 void Gallery::ImplLoadSubDirs( const INetURLObject& rBaseURL, bool& rbDirIsReadOnly )
293 rbDirIsReadOnly = false;
297 uno::Reference< ucb::XCommandEnvironment > xEnv;
298 ::ucbhelper::Content aCnt( rBaseURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), xEnv, comphelper::getProcessComponentContext() );
300 uno::Sequence<OUString> aProps { u"Url"_ustr };
302 uno::Reference< sdbc::XResultSet > xResultSet( aCnt.createCursor( aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY ) );
304 #if defined(MACOSX) && HAVE_FEATURE_READONLY_INSTALLSET
305 if( rBaseURL.GetProtocol() == INetProtocol::File )
307 const char *appBundle = [[[NSBundle mainBundle] bundlePath] UTF8String];
308 OUString path = rBaseURL.GetURLPath();
309 if( path.startsWith( Concat2View(OUString( appBundle, strlen( appBundle ), RTL_TEXTENCODING_UTF8 ) + "/") ) )
310 rbDirIsReadOnly = true;
312 #else
315 // check readonlyness the very hard way
316 INetURLObject aTestURL( rBaseURL );
318 aTestURL.Append( u"cdefghij.klm" );
319 std::unique_ptr<SvStream> pTestStm(::utl::UcbStreamHelper::CreateStream( aTestURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), StreamMode::WRITE ));
321 if( pTestStm )
323 pTestStm->WriteInt32( sal_Int32(1) );
325 if( pTestStm->GetError() )
326 rbDirIsReadOnly = true;
328 pTestStm.reset();
329 KillFile( aTestURL );
331 else
332 rbDirIsReadOnly = true;
334 catch( const ucb::ContentCreationException& )
337 catch( const uno::RuntimeException& )
340 catch( const uno::Exception& )
343 #endif
344 if( xResultSet.is() )
346 uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
348 if( xContentAccess.is() )
350 static constexpr OUString s_sTitle = u"Title"_ustr;
351 static constexpr OUString s_sIsReadOnly = u"IsReadOnly"_ustr;
353 while( xResultSet->next() )
355 INetURLObject aThmURL( xContentAccess->queryContentIdentifierString() );
357 if (aThmURL.GetFileExtension().equalsIgnoreAsciiCase("thm"))
359 INetURLObject aSdgURL( aThmURL); aSdgURL.SetExtension( u"sdg" );
360 INetURLObject aSdvURL( aThmURL ); aSdvURL.SetExtension( u"sdv" );
364 ::ucbhelper::Content aThmCnt( aThmURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), xEnv, comphelper::getProcessComponentContext() );
365 ::ucbhelper::Content aSdgCnt( aSdgURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), xEnv, comphelper::getProcessComponentContext() );
366 ::ucbhelper::Content aSdvCnt( aSdvURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), xEnv, comphelper::getProcessComponentContext() );
368 OUString aTitle;
371 aThmCnt.getPropertyValue( s_sTitle ) >>= aTitle;
373 catch( const uno::RuntimeException& )
376 catch( const uno::Exception& )
380 if( !aTitle.isEmpty() )
382 bool bReadOnly = false;
386 aThmCnt.getPropertyValue( s_sIsReadOnly ) >>= bReadOnly;
388 catch( const uno::RuntimeException& )
391 catch( const uno::Exception& )
395 if( !bReadOnly )
399 aSdgCnt.getPropertyValue( s_sTitle ) >>= aTitle;
401 catch( const css::uno::RuntimeException& )
404 catch( const css::uno::Exception& )
408 if( !aTitle.isEmpty() )
412 aSdgCnt.getPropertyValue( s_sIsReadOnly ) >>= bReadOnly;
414 catch( const uno::RuntimeException& )
417 catch( const uno::Exception& )
423 if( !bReadOnly )
427 aSdvCnt.getPropertyValue( s_sTitle ) >>= aTitle;
429 catch( const css::uno::RuntimeException& )
432 catch( const css::uno::Exception& )
436 if( !aTitle.isEmpty() )
440 aSdvCnt.getPropertyValue( s_sIsReadOnly ) >>= bReadOnly;
442 catch( const uno::RuntimeException& )
445 catch( const uno::Exception& )
451 GalleryThemeEntry* pEntry = GalleryFileStorageEntry::CreateThemeEntry( aThmURL, rbDirIsReadOnly || bReadOnly );
453 if( pEntry )
454 aThemeList.emplace_back( pEntry );
457 catch( const ucb::ContentCreationException& )
460 catch( const uno::RuntimeException& )
463 catch( const uno::Exception& )
471 catch( const ucb::ContentCreationException& )
474 catch( const uno::RuntimeException& )
477 catch( const uno::Exception& )
482 GalleryThemeEntry* Gallery::ImplGetThemeEntry( std::u16string_view rThemeName )
484 if( !rThemeName.empty() )
486 for ( size_t i = 0, n = aThemeList.size(); i < n; ++i )
487 if( rThemeName == aThemeList[ i ]->GetThemeName() )
488 return aThemeList[ i ].get();
491 return nullptr;
494 OUString Gallery::GetThemeName( sal_uInt32 nThemeId ) const
496 GalleryThemeEntry* pFound = nullptr;
498 for ( size_t i = 0, n = aThemeList.size(); i < n && !pFound; ++i )
500 GalleryThemeEntry* pEntry = aThemeList[ i ].get();
501 if( nThemeId == pEntry->GetId() )
502 pFound = pEntry;
505 // try fallback, if no entry was found
506 if( !pFound )
508 OUString aFallback;
510 switch( nThemeId )
512 case GALLERY_THEME_3D:
513 aFallback = SvxResId(RID_GALLERYSTR_THEME_3D);
514 break;
515 case GALLERY_THEME_BULLETS:
516 aFallback = SvxResId(RID_GALLERYSTR_THEME_BULLETS);
517 break;
518 case GALLERY_THEME_HOMEPAGE:
519 aFallback = SvxResId(RID_GALLERYSTR_THEME_HOMEPAGE);
520 break;
521 case GALLERY_THEME_POWERPOINT:
522 aFallback = RID_GALLERYSTR_THEME_POWERPOINT;
523 break;
524 case GALLERY_THEME_FONTWORK:
525 aFallback = RID_GALLERYSTR_THEME_FONTWORK;
526 break;
527 case GALLERY_THEME_FONTWORK_VERTICAL:
528 aFallback = RID_GALLERYSTR_THEME_FONTWORK_VERTICAL;
529 break;
530 case GALLERY_THEME_SOUNDS:
531 aFallback = SvxResId(RID_GALLERYSTR_THEME_SOUNDS);
532 break;
533 case RID_GALLERY_THEME_ARROWS:
534 aFallback = SvxResId(RID_GALLERYSTR_THEME_ARROWS);
535 break;
536 case RID_GALLERY_THEME_COMPUTERS:
537 aFallback = SvxResId(RID_GALLERYSTR_THEME_COMPUTERS);
538 break;
539 case RID_GALLERY_THEME_DIAGRAMS:
540 aFallback = SvxResId(RID_GALLERYSTR_THEME_DIAGRAMS);
541 break;
542 case RID_GALLERY_THEME_EDUCATION:
543 aFallback = SvxResId(RID_GALLERYSTR_THEME_EDUCATION);
544 break;
545 case RID_GALLERY_THEME_ENVIRONMENT:
546 aFallback = SvxResId(RID_GALLERYSTR_THEME_ENVIRONMENT);
547 break;
548 case RID_GALLERY_THEME_FINANCE:
549 aFallback = SvxResId(RID_GALLERYSTR_THEME_FINANCE);
550 break;
551 case RID_GALLERY_THEME_PEOPLE:
552 aFallback = SvxResId(RID_GALLERYSTR_THEME_PEOPLE);
553 break;
554 case RID_GALLERY_THEME_SYMBOLS:
555 aFallback = SvxResId(RID_GALLERYSTR_THEME_SYMBOLS);
556 break;
557 case RID_GALLERY_THEME_TRANSPORT:
558 aFallback = SvxResId(RID_GALLERYSTR_THEME_TRANSPORT);
559 break;
560 case RID_GALLERY_THEME_TXTSHAPES:
561 aFallback = SvxResId(RID_GALLERYSTR_THEME_TXTSHAPES);
562 break;
563 default:
564 break;
567 pFound = const_cast<Gallery*>(this)->ImplGetThemeEntry(aFallback);
570 return( pFound ? pFound->GetThemeName() : OUString() );
573 bool Gallery::HasTheme( std::u16string_view rThemeName )
575 return( ImplGetThemeEntry( rThemeName ) != nullptr );
578 bool Gallery::CreateTheme( const OUString& rThemeName )
580 bool bRet = false;
582 if( !HasTheme( rThemeName ) && ( GetUserURL().GetProtocol() != INetProtocol::NotValid ) )
584 INetURLObject aURL( GetUserURL() );
585 aURL.Append( rThemeName );
586 GalleryThemeEntry* pNewEntry = new GalleryThemeEntry(
587 true, aURL, rThemeName,
588 false, true, 0, false );
590 aThemeList.emplace_back( pNewEntry );
591 delete pNewEntry->createGalleryTheme( this );
592 Broadcast( GalleryHint( GalleryHintType::THEME_CREATED, rThemeName ) );
593 bRet = true;
596 return bRet;
599 void Gallery::RenameTheme( const OUString& rOldName, const OUString& rNewName )
601 GalleryThemeEntry* pThemeEntry = ImplGetThemeEntry( rOldName );
603 // check if the new theme name is already present
604 if( !pThemeEntry || HasTheme( rNewName ) || pThemeEntry->IsReadOnly() )
605 return;
607 SfxListener aListener;
608 GalleryTheme* pThm = AcquireTheme( rOldName, aListener );
610 if( pThm )
612 pThemeEntry->SetName( rNewName );
613 if (pThm->pThm->IsModified())
614 if (!pThm->mpGalleryStorageEngine->implWrite(*pThm, pThm->pThm))
615 pThm->ImplSetModified(false);
617 Broadcast( GalleryHint( GalleryHintType::THEME_RENAMED, rOldName, pThm->GetName() ) );
618 ReleaseTheme( pThm, aListener );
622 bool Gallery::RemoveTheme( const OUString& rThemeName )
624 GalleryThemeEntry* pThemeEntry = ImplGetThemeEntry( rThemeName );
625 bool bRet = false;
627 if( pThemeEntry && !pThemeEntry->IsReadOnly() )
629 Broadcast( GalleryHint( GalleryHintType::CLOSE_THEME, rThemeName ) );
631 SfxListener aListener;
632 GalleryTheme* pThm = AcquireTheme( rThemeName, aListener );
634 if( pThm )
636 ReleaseTheme(pThm, aListener);
637 pThemeEntry->removeTheme();
640 auto it = std::find_if(aThemeList.begin(), aThemeList.end(),
641 [&pThemeEntry](const std::unique_ptr<GalleryThemeEntry>& rpEntry) { return pThemeEntry == rpEntry.get(); });
642 if (it != aThemeList.end())
643 aThemeList.erase( it );
645 Broadcast( GalleryHint( GalleryHintType::THEME_REMOVED, rThemeName ) );
647 bRet = true;
650 return bRet;
653 std::unique_ptr<GalleryTheme> GalleryThemeEntry::getCachedTheme(Gallery* pGallery)
655 std::unique_ptr<GalleryTheme> pNewTheme;
656 pNewTheme.reset(createGalleryTheme(pGallery));
657 mpGalleryStorageEngineEntry->getCachedTheme(pNewTheme);
658 return pNewTheme;
661 GalleryTheme* Gallery::ImplGetCachedTheme(GalleryThemeEntry* pThemeEntry)
663 GalleryTheme* pTheme = nullptr;
665 if( pThemeEntry )
667 auto it = std::find_if(aThemeCache.begin(), aThemeCache.end(),
668 [&pThemeEntry](const GalleryThemeCacheEntry* pEntry) { return pThemeEntry == pEntry->GetThemeEntry(); });
669 if (it != aThemeCache.end())
670 pTheme = (*it)->GetTheme();
672 if( !pTheme )
674 std::unique_ptr<GalleryTheme> pNewTheme = pThemeEntry->getCachedTheme(this);
675 if (pNewTheme)
677 aThemeCache.push_back( new GalleryThemeCacheEntry( pThemeEntry, std::move(pNewTheme) ));
678 pTheme = aThemeCache.back()->GetTheme();
683 return pTheme;
686 void Gallery::ImplDeleteCachedTheme( GalleryTheme const * pTheme )
688 auto it = std::find_if(aThemeCache.begin(), aThemeCache.end(),
689 [&pTheme](const GalleryThemeCacheEntry* pEntry) { return pTheme == pEntry->GetTheme(); });
690 if (it != aThemeCache.end())
692 delete *it;
693 aThemeCache.erase(it);
697 GalleryTheme* Gallery::AcquireTheme( std::u16string_view rThemeName, SfxListener& rListener )
699 GalleryTheme* pTheme = nullptr;
700 GalleryThemeEntry* pThemeEntry = ImplGetThemeEntry( rThemeName );
702 if( pThemeEntry )
704 pTheme = ImplGetCachedTheme( pThemeEntry );
705 if (pTheme)
706 rListener.StartListening(*pTheme, DuplicateHandling::Prevent);
708 return pTheme;
711 void Gallery::ReleaseTheme( GalleryTheme* pTheme, SfxListener& rListener )
713 if( pTheme )
715 rListener.EndListening( *pTheme );
717 if( !pTheme->HasListeners() )
718 ImplDeleteCachedTheme( pTheme );
722 bool GalleryThemeEntry::IsDefault() const
724 return nId > 0 && nId != GALLERY_THEME_MYTHEME;
727 GalleryStorageLocations& GalleryThemeEntry::getGalleryStorageLocations() const
729 return mpGalleryStorageEngineEntry->getGalleryStorageLocations();
732 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */