xref: /core/svx/source/gallery2/gallery1.cxx (revision bc91cc47)
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 <config_features.h>
21 
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
28 
29 #include <sal/config.h>
30 
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>
51 
52 
53 using namespace ::com::sun::star;
54 
55 
56 constexpr std::pair<sal_uInt16, OUString> aUnlocalized[] =
57 {
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 }
65 };
66 
67 const std::pair<sal_uInt16, TranslateId> aLocalized[] =
68 {
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 },
86 
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 },
97 
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 },
102 
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 }
109 };
110 
GalleryThemeEntry(bool bCreateUniqueURL,const INetURLObject & rBaseURL,const OUString & rName,bool _bReadOnly,bool _bNewFile,sal_uInt32 _nId,bool _bThemeNameFromResource)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 )
118 {
119     INetURLObject aURL( rBaseURL );
120     DBG_ASSERT( aURL.GetProtocol() != INetProtocol::NotValid, "invalid URL" );
121 
122     if (bCreateUniqueURL)
123     {
124         GalleryFileStorageEntry::CreateUniqueURL(rBaseURL,aURL);
125     }
126 
127     mpGalleryStorageEngineEntry = std::make_unique<GalleryFileStorageEntry>();
128     setStorageLocations(aURL);
129 
130     SetModified( _bNewFile );
131 
132     aName = mpGalleryStorageEngineEntry->ReadStrFromIni( u"name" );
133 
134     // This is awful - we shouldn't use these resources if we
135     // possibly can avoid them
136     if( aName.isEmpty() && nId && bThemeNameFromResource )
137     {
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)
141         {
142             if (aUnlocalized[i].first == nId)
143             {
144                 aName = aUnlocalized[i].second;
145                 break;
146             }
147         }
148         //look up the rest of the ids in string resources
149         if (aName.isEmpty())
150         {
151             for (size_t i = 0; i < SAL_N_ELEMENTS(aLocalized); ++i)
152             {
153                 if (aLocalized[i].first == nId)
154                 {
155                     aName = SvxResId(aLocalized[i].second);
156                     break;
157                 }
158             }
159         }
160     }
161 
162     if( aName.isEmpty() )
163         aName = rName;
164 }
165 
~GalleryThemeEntry()166 GalleryThemeEntry::~GalleryThemeEntry()
167 {}
168 
setStorageLocations(INetURLObject & rURL)169 void GalleryThemeEntry::setStorageLocations(INetURLObject & rURL)
170 {
171     mpGalleryStorageEngineEntry->setStorageLocations(rURL);
172 }
173 
createGalleryTheme(Gallery * pGallery)174 GalleryTheme* GalleryThemeEntry::createGalleryTheme(Gallery* pGallery)
175 {
176     return new GalleryTheme(pGallery,this);
177 }
178 
createGalleryStorageEngine(GalleryObjectCollection & mrGalleryObjectCollection)179 std::unique_ptr<GalleryFileStorage> GalleryThemeEntry::createGalleryStorageEngine(GalleryObjectCollection& mrGalleryObjectCollection)
180 {
181     return mpGalleryStorageEngineEntry->createGalleryStorageEngine(mrGalleryObjectCollection, bReadOnly);
182 }
183 
InsertAllThemes(weld::ComboBox & rListBox)184 void GalleryTheme::InsertAllThemes(weld::ComboBox& rListBox)
185 {
186     for (size_t i = 0; i < SAL_N_ELEMENTS(aUnlocalized); ++i)
187         rListBox.append_text(aUnlocalized[i].second);
188 
189     for (size_t i = 0; i < SAL_N_ELEMENTS(aLocalized); ++i)
190         rListBox.append_text(SvxResId(aLocalized[i].second));
191 }
192 
SetName(const OUString & rNewName)193 void GalleryThemeEntry::SetName( const OUString& rNewName )
194 {
195     if( aName != rNewName )
196     {
197         aName = rNewName;
198         SetModified( true );
199         bThemeNameFromResource = false;
200     }
201 }
202 
SetId(sal_uInt32 nNewId,bool bResetThemeName)203 void GalleryThemeEntry::SetId( sal_uInt32 nNewId, bool bResetThemeName )
204 {
205     nId = nNewId;
206     SetModified( true );
207     bThemeNameFromResource = ( nId && bResetThemeName );
208 }
209 
removeTheme()210 void GalleryThemeEntry::removeTheme()
211 {
212     mpGalleryStorageEngineEntry->removeTheme();
213 }
214 
215 class GalleryThemeCacheEntry
216 {
217 private:
218 
219     const GalleryThemeEntry*        mpThemeEntry;
220     std::unique_ptr<GalleryTheme>   mpTheme;
221 
222 public:
223 
GalleryThemeCacheEntry(const GalleryThemeEntry * pThemeEntry,std::unique_ptr<GalleryTheme> pTheme)224                                 GalleryThemeCacheEntry( const GalleryThemeEntry* pThemeEntry, std::unique_ptr<GalleryTheme> pTheme ) :
225                                     mpThemeEntry( pThemeEntry ), mpTheme( std::move(pTheme) ) {}
226 
GetThemeEntry() const227     const GalleryThemeEntry*    GetThemeEntry() const { return mpThemeEntry; }
GetTheme() const228     GalleryTheme*               GetTheme() const { return mpTheme.get(); }
229 };
230 
231 
Gallery(std::u16string_view rMultiPath)232 Gallery::Gallery( std::u16string_view rMultiPath )
233 :       bMultiPath          ( false )
234 {
235     ImplLoad( rMultiPath );
236 }
237 
~Gallery()238 Gallery::~Gallery()
239 {
240 }
241 
GetGalleryInstance()242 Gallery* Gallery::GetGalleryInstance()
243 {
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()));
248 
249     return s_pGallery;
250 }
251 
ImplLoad(std::u16string_view rMultiPath)252 void Gallery::ImplLoad( std::u16string_view rMultiPath )
253 {
254     bool bIsReadOnlyDir {false};
255 
256     bMultiPath = !rMultiPath.empty();
257 
258     INetURLObject aCurURL(SvtPathOptions().GetConfigPath());
259     ImplLoadSubDirs( aCurURL, bIsReadOnlyDir );
260 
261     if( !bIsReadOnlyDir )
262         aUserURL = aCurURL;
263 
264     if( bMultiPath )
265     {
266         bool bIsRelURL {true};
267         sal_Int32 nIdx {0};
268         do
269         {
270             aCurURL = INetURLObject(o3tl::getToken(rMultiPath, 0, ';', nIdx));
271             if (bIsRelURL)
272             {
273                 aRelURL = aCurURL;
274                 bIsRelURL = false;
275             }
276 
277             ImplLoadSubDirs( aCurURL, bIsReadOnlyDir );
278 
279             if( !bIsReadOnlyDir )
280                 aUserURL = aCurURL;
281         }
282         while (nIdx>0);
283     }
284     else
285         aRelURL = INetURLObject( rMultiPath );
286 
287     DBG_ASSERT( aUserURL.GetProtocol() != INetProtocol::NotValid, "no writable Gallery user directory available" );
288     DBG_ASSERT( aRelURL.GetProtocol() != INetProtocol::NotValid, "invalid URL" );
289 }
290 
ImplLoadSubDirs(const INetURLObject & rBaseURL,bool & rbDirIsReadOnly)291 void Gallery::ImplLoadSubDirs( const INetURLObject& rBaseURL, bool& rbDirIsReadOnly )
292 {
293     rbDirIsReadOnly = false;
294 
295     try
296     {
297         uno::Reference< ucb::XCommandEnvironment > xEnv;
298         ::ucbhelper::Content                       aCnt( rBaseURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), xEnv, comphelper::getProcessComponentContext() );
299 
300         uno::Sequence<OUString> aProps { u"Url"_ustr };
301 
302         uno::Reference< sdbc::XResultSet > xResultSet( aCnt.createCursor( aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY ) );
303 
304 #if defined(MACOSX) && HAVE_FEATURE_READONLY_INSTALLSET
305         if( rBaseURL.GetProtocol() == INetProtocol::File )
306         {
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;
311         }
312 #else
313         try
314         {
315             // check readonlyness the very hard way
316             INetURLObject   aTestURL( rBaseURL );
317 
318             aTestURL.Append( u"cdefghij.klm" );
319             std::unique_ptr<SvStream> pTestStm(::utl::UcbStreamHelper::CreateStream( aTestURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), StreamMode::WRITE ));
320 
321             if( pTestStm )
322             {
323                 pTestStm->WriteInt32( sal_Int32(1) );
324 
325                 if( pTestStm->GetError() )
326                     rbDirIsReadOnly = true;
327 
328                 pTestStm.reset();
329                 KillFile( aTestURL );
330             }
331             else
332                 rbDirIsReadOnly = true;
333         }
334         catch( const ucb::ContentCreationException& )
335         {
336         }
337         catch( const uno::RuntimeException& )
338         {
339         }
340         catch( const uno::Exception& )
341         {
342         }
343 #endif
344         if( xResultSet.is() )
345         {
346             uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
347 
348             if( xContentAccess.is() )
349             {
350                 static constexpr OUString s_sTitle = u"Title"_ustr;
351                 static constexpr OUString s_sIsReadOnly = u"IsReadOnly"_ustr;
352 
353                 while( xResultSet->next() )
354                 {
355                     INetURLObject aThmURL( xContentAccess->queryContentIdentifierString() );
356 
357                     if (aThmURL.GetFileExtension().equalsIgnoreAsciiCase("thm"))
358                     {
359                         INetURLObject   aSdgURL( aThmURL); aSdgURL.SetExtension( u"sdg" );
360                         INetURLObject   aSdvURL( aThmURL ); aSdvURL.SetExtension( u"sdv" );
361 
362                         try
363                         {
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() );
367 
368                             OUString        aTitle;
369                             try
370                             {
371                                 aThmCnt.getPropertyValue( s_sTitle ) >>= aTitle;
372                             }
373                             catch( const uno::RuntimeException& )
374                             {
375                             }
376                             catch( const uno::Exception& )
377                             {
378                             }
379 
380                             if( !aTitle.isEmpty() )
381                             {
382                                 bool bReadOnly = false;
383 
384                                 try
385                                 {
386                                     aThmCnt.getPropertyValue( s_sIsReadOnly ) >>= bReadOnly;
387                                 }
388                                 catch( const uno::RuntimeException& )
389                                 {
390                                 }
391                                 catch( const uno::Exception& )
392                                 {
393                                 }
394 
395                                 if( !bReadOnly )
396                                 {
397                                     try
398                                     {
399                                         aSdgCnt.getPropertyValue( s_sTitle ) >>= aTitle;
400                                     }
401                                     catch( const css::uno::RuntimeException& )
402                                     {
403                                     }
404                                     catch( const css::uno::Exception& )
405                                     {
406                                     }
407 
408                                     if( !aTitle.isEmpty() )
409                                     {
410                                         try
411                                         {
412                                             aSdgCnt.getPropertyValue( s_sIsReadOnly ) >>= bReadOnly;
413                                         }
414                                         catch( const uno::RuntimeException& )
415                                         {
416                                         }
417                                         catch( const uno::Exception& )
418                                         {
419                                         }
420                                     }
421                                 }
422 
423                                 if( !bReadOnly )
424                                 {
425                                     try
426                                     {
427                                         aSdvCnt.getPropertyValue( s_sTitle ) >>= aTitle;
428                                     }
429                                     catch( const css::uno::RuntimeException& )
430                                     {
431                                     }
432                                     catch( const css::uno::Exception& )
433                                     {
434                                     }
435 
436                                     if( !aTitle.isEmpty() )
437                                     {
438                                         try
439                                         {
440                                             aSdvCnt.getPropertyValue( s_sIsReadOnly ) >>= bReadOnly;
441                                         }
442                                         catch( const uno::RuntimeException& )
443                                         {
444                                         }
445                                         catch( const uno::Exception& )
446                                         {
447                                         }
448                                     }
449                                 }
450 
451                                 GalleryThemeEntry* pEntry = GalleryFileStorageEntry::CreateThemeEntry( aThmURL, rbDirIsReadOnly || bReadOnly );
452 
453                                 if( pEntry )
454                                     aThemeList.emplace_back( pEntry );
455                             }
456                         }
457                         catch( const ucb::ContentCreationException& )
458                         {
459                         }
460                         catch( const uno::RuntimeException& )
461                         {
462                         }
463                         catch( const uno::Exception& )
464                         {
465                         }
466                     }
467                 }
468             }
469         }
470     }
471     catch( const ucb::ContentCreationException& )
472     {
473     }
474     catch( const uno::RuntimeException& )
475     {
476     }
477     catch( const uno::Exception& )
478     {
479     }
480 }
481 
ImplGetThemeEntry(std::u16string_view rThemeName)482 GalleryThemeEntry* Gallery::ImplGetThemeEntry( std::u16string_view rThemeName )
483 {
484     if( !rThemeName.empty() )
485     {
486         for ( size_t i = 0, n = aThemeList.size(); i < n; ++i )
487             if( rThemeName == aThemeList[ i ]->GetThemeName() )
488                 return aThemeList[ i ].get();
489     }
490 
491     return nullptr;
492 }
493 
GetThemeName(sal_uInt32 nThemeId) const494 OUString Gallery::GetThemeName( sal_uInt32 nThemeId ) const
495 {
496     GalleryThemeEntry* pFound = nullptr;
497 
498     for ( size_t i = 0, n = aThemeList.size(); i < n && !pFound; ++i )
499     {
500         GalleryThemeEntry* pEntry = aThemeList[ i ].get();
501         if( nThemeId == pEntry->GetId() )
502             pFound = pEntry;
503     }
504 
505     // try fallback, if no entry was found
506     if( !pFound )
507     {
508         OUString aFallback;
509 
510         switch( nThemeId )
511         {
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;
565         }
566 
567         pFound = const_cast<Gallery*>(this)->ImplGetThemeEntry(aFallback);
568     }
569 
570     return( pFound ? pFound->GetThemeName() : OUString() );
571 }
572 
HasTheme(std::u16string_view rThemeName)573 bool Gallery::HasTheme( std::u16string_view rThemeName )
574 {
575     return( ImplGetThemeEntry( rThemeName ) != nullptr );
576 }
577 
CreateTheme(const OUString & rThemeName)578 bool Gallery::CreateTheme( const OUString& rThemeName )
579 {
580     bool bRet = false;
581 
582     if( !HasTheme( rThemeName ) && ( GetUserURL().GetProtocol() != INetProtocol::NotValid ) )
583     {
584         INetURLObject aURL( GetUserURL() );
585         aURL.Append( rThemeName );
586         GalleryThemeEntry* pNewEntry = new GalleryThemeEntry(
587                 true, aURL, rThemeName,
588                 false, true, 0, false );
589 
590         aThemeList.emplace_back( pNewEntry );
591         delete pNewEntry->createGalleryTheme( this );
592         Broadcast( GalleryHint( GalleryHintType::THEME_CREATED, rThemeName ) );
593         bRet = true;
594     }
595 
596     return bRet;
597 }
598 
RenameTheme(const OUString & rOldName,const OUString & rNewName)599 void Gallery::RenameTheme( const OUString& rOldName, const OUString& rNewName )
600 {
601     GalleryThemeEntry*      pThemeEntry = ImplGetThemeEntry( rOldName );
602 
603     // check if the new theme name is already present
604     if( !pThemeEntry || HasTheme( rNewName ) || pThemeEntry->IsReadOnly() )
605         return;
606 
607     SfxListener   aListener;
608     GalleryTheme* pThm = AcquireTheme( rOldName, aListener );
609 
610     if( pThm )
611     {
612         pThemeEntry->SetName( rNewName );
613         if (pThm->pThm->IsModified())
614             if (!pThm->mpGalleryStorageEngine->implWrite(*pThm, pThm->pThm))
615                 pThm->ImplSetModified(false);
616 
617         Broadcast( GalleryHint( GalleryHintType::THEME_RENAMED, rOldName, pThm->GetName() ) );
618         ReleaseTheme( pThm, aListener );
619     }
620 }
621 
RemoveTheme(const OUString & rThemeName)622 bool Gallery::RemoveTheme( const OUString& rThemeName )
623 {
624     GalleryThemeEntry*  pThemeEntry = ImplGetThemeEntry( rThemeName );
625     bool                bRet = false;
626 
627     if( pThemeEntry && !pThemeEntry->IsReadOnly() )
628     {
629         Broadcast( GalleryHint( GalleryHintType::CLOSE_THEME, rThemeName ) );
630 
631         SfxListener     aListener;
632         GalleryTheme*   pThm = AcquireTheme( rThemeName, aListener );
633 
634         if( pThm )
635         {
636             ReleaseTheme(pThm, aListener);
637             pThemeEntry->removeTheme();
638         }
639 
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 );
644 
645         Broadcast( GalleryHint( GalleryHintType::THEME_REMOVED, rThemeName ) );
646 
647         bRet = true;
648     }
649 
650     return bRet;
651 }
652 
getCachedTheme(Gallery * pGallery)653 std::unique_ptr<GalleryTheme> GalleryThemeEntry::getCachedTheme(Gallery* pGallery)
654 {
655     std::unique_ptr<GalleryTheme> pNewTheme;
656     pNewTheme.reset(createGalleryTheme(pGallery));
657     mpGalleryStorageEngineEntry->getCachedTheme(pNewTheme);
658     return pNewTheme;
659 }
660 
ImplGetCachedTheme(GalleryThemeEntry * pThemeEntry)661 GalleryTheme* Gallery::ImplGetCachedTheme(GalleryThemeEntry* pThemeEntry)
662 {
663     GalleryTheme* pTheme = nullptr;
664 
665     if( pThemeEntry )
666     {
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();
671 
672         if( !pTheme )
673         {
674             std::unique_ptr<GalleryTheme> pNewTheme = pThemeEntry->getCachedTheme(this);
675             if (pNewTheme)
676             {
677                 aThemeCache.push_back( new GalleryThemeCacheEntry( pThemeEntry, std::move(pNewTheme) ));
678                 pTheme = aThemeCache.back()->GetTheme();
679             }
680         }
681     }
682 
683     return pTheme;
684 }
685 
ImplDeleteCachedTheme(GalleryTheme const * pTheme)686 void Gallery::ImplDeleteCachedTheme( GalleryTheme const * pTheme )
687 {
688     auto it = std::find_if(aThemeCache.begin(), aThemeCache.end(),
689         [&pTheme](const GalleryThemeCacheEntry* pEntry) { return pTheme == pEntry->GetTheme(); });
690     if (it != aThemeCache.end())
691     {
692         delete *it;
693         aThemeCache.erase(it);
694     }
695 }
696 
AcquireTheme(std::u16string_view rThemeName,SfxListener & rListener)697 GalleryTheme* Gallery::AcquireTheme( std::u16string_view rThemeName, SfxListener& rListener )
698 {
699     GalleryTheme*           pTheme = nullptr;
700     GalleryThemeEntry*      pThemeEntry = ImplGetThemeEntry( rThemeName );
701 
702     if( pThemeEntry )
703     {
704         pTheme = ImplGetCachedTheme( pThemeEntry );
705         if (pTheme)
706             rListener.StartListening(*pTheme, DuplicateHandling::Prevent);
707     }
708     return pTheme;
709 }
710 
ReleaseTheme(GalleryTheme * pTheme,SfxListener & rListener)711 void Gallery::ReleaseTheme( GalleryTheme* pTheme, SfxListener& rListener )
712 {
713     if( pTheme )
714     {
715         rListener.EndListening( *pTheme );
716 
717         if( !pTheme->HasListeners() )
718             ImplDeleteCachedTheme( pTheme );
719     }
720 }
721 
IsDefault() const722 bool GalleryThemeEntry::IsDefault() const
723 {
724     return nId > 0 && nId != GALLERY_THEME_MYTHEME;
725 }
726 
getGalleryStorageLocations() const727 GalleryStorageLocations& GalleryThemeEntry::getGalleryStorageLocations() const
728 {
729     return mpGalleryStorageEngineEntry->getGalleryStorageLocations();
730 }
731 
732 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
733