char array should be null terminated
[LibreOffice.git] / svx / source / gallery2 / gallery1.cxx
blob165936cae991ca3557c5a7eebf0bb6dcc51874ee
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 // MARKER(update_precomp.py): autogen include statement, do not remove
30 #include "precompiled_svx.hxx"
32 #define ENABLE_BYTESTRING_STREAM_OPERATORS
34 #include <tools/vcompat.hxx>
35 #include <ucbhelper/content.hxx>
36 #include <unotools/ucbstreamhelper.hxx>
37 #include <unotools/pathoptions.hxx>
38 #include <sfx2/docfile.hxx>
39 #include "svx/gallery.hxx"
40 #include "gallery.hrc"
41 #include "svx/galmisc.hxx"
42 #include "svx/galtheme.hxx"
43 #include "svx/gallery1.hxx"
44 #include <com/sun/star/sdbc/XResultSet.hpp>
45 #include <com/sun/star/ucb/XContentAccess.hpp>
47 #define ENABLE_BYTESTRING_STREAM_OPERATORS
49 // --------------
50 // - Namespaces -
51 // --------------
53 using namespace ::rtl;
54 using namespace ::com::sun::star;
56 // ---------------------
57 // - GalleryThemeEntry -
58 // ---------------------
60 GalleryThemeEntry::GalleryThemeEntry( const INetURLObject& rBaseURL, const String& rName,
61 sal_uInt32 _nFileNumber, sal_Bool _bReadOnly, sal_Bool _bImported,
62 sal_Bool _bNewFile, sal_uInt32 _nId, sal_Bool _bThemeNameFromResource ) :
63 nFileNumber ( _nFileNumber ),
64 nId ( _nId ),
65 bReadOnly ( _bReadOnly || _bImported ),
66 bImported ( _bImported ),
67 bThemeNameFromResource ( _bThemeNameFromResource )
69 INetURLObject aURL( rBaseURL );
70 DBG_ASSERT( aURL.GetProtocol() != INET_PROT_NOT_VALID, "invalid URL" );
71 String aFileName( String( RTL_CONSTASCII_USTRINGPARAM( "sg" ) ) );
73 aURL.Append( ( aFileName += String::CreateFromInt32( nFileNumber ) ) += String( RTL_CONSTASCII_USTRINGPARAM( ".thm" ) ) );
74 aThmURL = ImplGetURLIgnoreCase( aURL );
76 aURL.setExtension( String( RTL_CONSTASCII_USTRINGPARAM( "sdg" ) ) );
77 aSdgURL = ImplGetURLIgnoreCase( aURL );
79 aURL.setExtension( String( RTL_CONSTASCII_USTRINGPARAM( "sdv" ) ) );
80 aSdvURL = ImplGetURLIgnoreCase( aURL );
82 SetModified( _bNewFile );
84 if( nId && bThemeNameFromResource )
85 aName = String( GAL_RESID( RID_GALLERYSTR_THEME_START + (sal_uInt16) nId ) );
87 if( !aName.Len() )
88 aName = rName;
91 // -----------------------------------------------------------------------------
93 INetURLObject GalleryThemeEntry::ImplGetURLIgnoreCase( const INetURLObject& rURL ) const
95 INetURLObject aURL( rURL );
96 String aFileName;
98 // check original file name
99 if( !FileExists( aURL ) )
101 // check upper case file name
102 aURL.setName( aURL.getName().toAsciiUpperCase() );
104 if(!FileExists( aURL ) )
106 // check lower case file name
107 aURL.setName( aURL.getName().toAsciiLowerCase() );
111 return aURL;
114 // -----------------------------------------------------------------------------
116 void GalleryThemeEntry::SetName( const String& rNewName )
118 if( aName != rNewName )
120 aName = rNewName;
121 SetModified( sal_True );
122 bThemeNameFromResource = sal_False;
126 // -----------------------------------------------------------------------------
128 void GalleryThemeEntry::SetId( sal_uInt32 nNewId, sal_Bool bResetThemeName )
130 nId = nNewId;
131 SetModified( sal_True );
132 bThemeNameFromResource = ( nId && bResetThemeName );
135 // ---------------------------
136 // - GalleryImportThemeEntry -
137 // ---------------------------
139 SvStream& operator<<( SvStream& rOut, const GalleryImportThemeEntry& rEntry )
141 ByteString aDummy;
143 rOut << ByteString( rEntry.aThemeName, RTL_TEXTENCODING_UTF8 ) <<
144 ByteString( rEntry.aUIName, RTL_TEXTENCODING_UTF8 ) <<
145 ByteString( String(rEntry.aURL.GetMainURL( INetURLObject::NO_DECODE )), RTL_TEXTENCODING_UTF8 ) <<
146 ByteString( rEntry.aImportName, RTL_TEXTENCODING_UTF8 ) <<
147 aDummy;
149 return rOut;
152 // ------------------------------------------------------------------------
154 SvStream& operator>>( SvStream& rIn, GalleryImportThemeEntry& rEntry )
156 ByteString aTmpStr;
158 rIn >> aTmpStr; rEntry.aThemeName = String( aTmpStr, RTL_TEXTENCODING_UTF8 );
159 rIn >> aTmpStr; rEntry.aUIName = String( aTmpStr, RTL_TEXTENCODING_UTF8 );
160 rIn >> aTmpStr; rEntry.aURL = INetURLObject( String( aTmpStr, RTL_TEXTENCODING_UTF8 ) );
161 rIn >> aTmpStr; rEntry.aImportName = String( aTmpStr, RTL_TEXTENCODING_UTF8 );
162 rIn >> aTmpStr;
164 return rIn;
167 // --------------------------
168 // - GalleryThemeCacheEntry -
169 // --------------------------
171 class GalleryThemeCacheEntry
173 private:
175 const GalleryThemeEntry* mpThemeEntry;
176 GalleryTheme* mpTheme;
178 public:
180 GalleryThemeCacheEntry( const GalleryThemeEntry* pThemeEntry, GalleryTheme* pTheme ) :
181 mpThemeEntry( pThemeEntry ), mpTheme( pTheme ) {}
182 ~GalleryThemeCacheEntry() { delete mpTheme; }
184 const GalleryThemeEntry* GetThemeEntry() const { return mpThemeEntry; }
185 GalleryTheme* GetTheme() const { return mpTheme; }
188 // -----------
189 // - Gallery -
190 // -----------
192 Gallery::Gallery( const String& rMultiPath )
193 : nReadTextEncoding ( gsl_getSystemTextEncoding() )
194 , nLastFileNumber ( 0 )
195 , bMultiPath ( sal_False )
197 ImplLoad( rMultiPath );
200 // ------------------------------------------------------------------------
202 Gallery::~Gallery()
204 // Themen-Liste loeschen
205 for ( size_t i = 0, n = aThemeList.size(); i < n; ++i )
206 delete aThemeList[ i ];
207 aThemeList.clear();
209 // Import-Liste loeschen
210 for ( size_t i = 0, n = aImportList.size(); i < n; ++i )
211 delete aImportList[ i ];
212 aImportList.clear();
215 // ------------------------------------------------------------------------
217 Gallery* Gallery::GetGalleryInstance()
219 static Gallery* pGallery = NULL;
221 if( !pGallery )
223 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
224 if( !pGallery )
226 pGallery = new Gallery( SvtPathOptions().GetGalleryPath() );
230 return pGallery;
233 // ------------------------------------------------------------------------
235 void Gallery::ImplLoad( const String& rMultiPath )
237 const sal_uInt16 nTokenCount = rMultiPath.GetTokenCount( ';' );
238 sal_Bool bIsReadOnlyDir;
240 bMultiPath = ( nTokenCount > 0 );
242 INetURLObject aCurURL(SvtPathOptions().GetConfigPath());
243 ImplLoadSubDirs( aCurURL, bIsReadOnlyDir );
245 if( !bIsReadOnlyDir )
246 aUserURL = aCurURL;
248 if( bMultiPath )
250 aRelURL = INetURLObject( rMultiPath.GetToken( 0, ';' ) );
252 for( sal_uInt16 i = 0UL; i < nTokenCount; i++ )
254 aCurURL = INetURLObject(rMultiPath.GetToken( i, ';' ));
256 ImplLoadSubDirs( aCurURL, bIsReadOnlyDir );
258 if( !bIsReadOnlyDir )
259 aUserURL = aCurURL;
262 else
263 aRelURL = INetURLObject( rMultiPath );
265 DBG_ASSERT( aUserURL.GetProtocol() != INET_PROT_NOT_VALID, "no writable Gallery user directory available" );
266 DBG_ASSERT( aRelURL.GetProtocol() != INET_PROT_NOT_VALID, "invalid URL" );
268 ImplLoadImports();
271 // ------------------------------------------------------------------------
273 void Gallery::ImplLoadSubDirs( const INetURLObject& rBaseURL, sal_Bool& rbDirIsReadOnly )
275 rbDirIsReadOnly = sal_False;
279 uno::Reference< ucb::XCommandEnvironment > xEnv;
280 ::ucbhelper::Content aCnt( rBaseURL.GetMainURL( INetURLObject::NO_DECODE ), xEnv );
282 uno::Sequence< OUString > aProps( 1 );
283 aProps.getArray()[ 0 ] = OUString(RTL_CONSTASCII_USTRINGPARAM("Url"));
285 uno::Reference< sdbc::XResultSet > xResultSet( aCnt.createCursor( aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY ) );
289 // check readonlyness the very hard way
290 INetURLObject aTestURL( rBaseURL );
291 String aTestFile( RTL_CONSTASCII_USTRINGPARAM( "cdefghij.klm" ) );
293 aTestURL.Append( aTestFile );
294 SvStream* pTestStm = ::utl::UcbStreamHelper::CreateStream( aTestURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_WRITE );
296 if( pTestStm )
298 *pTestStm << 1;
300 if( pTestStm->GetError() )
301 rbDirIsReadOnly = sal_True;
303 delete pTestStm;
304 KillFile( aTestURL );
306 else
307 rbDirIsReadOnly = sal_True;
309 catch( const ucb::ContentCreationException& )
312 catch( const uno::RuntimeException& )
315 catch( const uno::Exception& )
319 if( xResultSet.is() )
321 uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
323 if( xContentAccess.is() )
325 while( xResultSet->next() )
327 INetURLObject aThmURL( xContentAccess->queryContentIdentifierString() );
329 if(aThmURL.GetExtension().equalsIgnoreAsciiCaseAscii("thm"))
331 INetURLObject aSdgURL( aThmURL); aSdgURL.SetExtension( OUString(RTL_CONSTASCII_USTRINGPARAM("sdg")) );
332 INetURLObject aSdvURL( aThmURL ); aSdvURL.SetExtension( OUString(RTL_CONSTASCII_USTRINGPARAM("sdv")) );
333 const OUString aTitleProp( RTL_CONSTASCII_USTRINGPARAM("Title") );
334 const OUString aReadOnlyProp( RTL_CONSTASCII_USTRINGPARAM("IsReadOnly") );
335 OUString aTitle;
336 sal_Bool bReadOnly = sal_False;
340 ::ucbhelper::Content aThmCnt( aThmURL.GetMainURL( INetURLObject::NO_DECODE ), xEnv );
341 ::ucbhelper::Content aSdgCnt( aSdgURL.GetMainURL( INetURLObject::NO_DECODE ), xEnv );
342 ::ucbhelper::Content aSdvCnt( aSdvURL.GetMainURL( INetURLObject::NO_DECODE ), xEnv );
346 aThmCnt.getPropertyValue( aTitleProp ) >>= aTitle;
348 catch( const uno::RuntimeException& )
351 catch( const uno::Exception& )
355 if( aTitle.getLength() )
359 aThmCnt.getPropertyValue( aReadOnlyProp ) >>= bReadOnly;
361 catch( const uno::RuntimeException& )
364 catch( const uno::Exception& )
368 if( !bReadOnly )
372 aSdgCnt.getPropertyValue( aTitleProp ) >>= aTitle;
374 catch( const ::com::sun::star::uno::RuntimeException& )
377 catch( const ::com::sun::star::uno::Exception& )
381 if( aTitle.getLength() )
385 aSdgCnt.getPropertyValue( aReadOnlyProp ) >>= bReadOnly;
387 catch( const uno::RuntimeException& )
390 catch( const uno::Exception& )
396 if( !bReadOnly )
400 aSdvCnt.getPropertyValue( aTitleProp ) >>= aTitle;
402 catch( const ::com::sun::star::uno::RuntimeException& )
405 catch( const ::com::sun::star::uno::Exception& )
409 if( aTitle.getLength() )
413 aSdvCnt.getPropertyValue( aReadOnlyProp ) >>= bReadOnly;
415 catch( const uno::RuntimeException& )
418 catch( const uno::Exception& )
424 GalleryThemeEntry* pEntry = GalleryTheme::CreateThemeEntry( aThmURL, rbDirIsReadOnly || bReadOnly );
426 if( pEntry )
428 const sal_uIntPtr nFileNumber = (sal_uIntPtr) String(aThmURL.GetBase()).Erase( 0, 2 ).Erase( 6 ).ToInt32();
430 aThemeList.push_back( pEntry );
432 if( nFileNumber > nLastFileNumber )
433 nLastFileNumber = nFileNumber;
437 catch( const ucb::ContentCreationException& )
440 catch( const uno::RuntimeException& )
443 catch( const uno::Exception& )
451 catch( const ucb::ContentCreationException& )
454 catch( const uno::RuntimeException& )
457 catch( const uno::Exception& )
462 // ------------------------------------------------------------------------
464 void Gallery::ImplLoadImports()
466 INetURLObject aURL( GetUserURL() );
468 aURL.Append( String( RTL_CONSTASCII_USTRINGPARAM( "gallery.sdi" ) ) );
470 if( FileExists( aURL ) )
472 SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( aURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ );
474 if( pIStm )
476 GalleryThemeEntry* pThemeEntry;
477 GalleryImportThemeEntry* pImportEntry;
478 INetURLObject aFile;
479 sal_uInt32 nInventor;
480 sal_uInt32 nCount;
481 sal_uInt16 nId;
482 sal_uInt16 i;
483 sal_uInt16 nTempCharSet;
485 for ( size_t j = 0, n = aImportList.size(); j < n; ++j )
486 delete aImportList[ j ];
487 aImportList.clear();
489 *pIStm >> nInventor;
491 if( nInventor == COMPAT_FORMAT( 'S', 'G', 'A', '3' ) )
493 *pIStm >> nId >> nCount >> nTempCharSet;
495 for( i = 0; i < nCount; i++ )
497 pImportEntry = new GalleryImportThemeEntry;
499 *pIStm >> *pImportEntry;
500 aImportList.push_back( pImportEntry );
501 aFile = INetURLObject( pImportEntry->aURL );
502 pThemeEntry = new GalleryThemeEntry( aFile,
503 pImportEntry->aUIName,
504 String(aFile.GetBase()).Erase( 0, 2 ).Erase( 6 ).ToInt32(),
505 sal_True, sal_True, sal_False, 0, sal_False );
507 aThemeList.push_back( pThemeEntry );
511 delete pIStm;
516 // ------------------------------------------------------------------------
518 void Gallery::ImplWriteImportList()
520 INetURLObject aURL( GetUserURL() );
521 aURL.Append( ( String( "gallery.sdi", RTL_TEXTENCODING_UTF8 ) ) );
522 SvStream* pOStm = ::utl::UcbStreamHelper::CreateStream( aURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_WRITE | STREAM_TRUNC );
524 if( pOStm )
526 const sal_uInt32 nInventor = (sal_uInt32) COMPAT_FORMAT( 'S', 'G', 'A', '3' );
527 const sal_uInt16 nId = 0x0004;
529 *pOStm << nInventor << nId << (sal_uInt32) aImportList.size() << (sal_uInt16) gsl_getSystemTextEncoding();
531 for ( size_t i = 0, n = aImportList.size(); i < n; ++i )
532 *pOStm << *aImportList[ i ];
534 if( pOStm->GetError() )
535 ErrorHandler::HandleError( ERRCODE_IO_GENERAL );
537 delete pOStm;
541 // ------------------------------------------------------------------------
543 GalleryThemeEntry* Gallery::ImplGetThemeEntry( const String& rThemeName )
545 GalleryThemeEntry* pFound = NULL;
547 if( rThemeName.Len() )
548 for ( size_t i = 0, n = aThemeList.size(); i < n && !pFound; ++i )
549 if( rThemeName == aThemeList[ i ]->GetThemeName() )
550 pFound = aThemeList[ i ];
552 return pFound;
555 // ------------------------------------------------------------------------
557 GalleryImportThemeEntry* Gallery::ImplGetImportThemeEntry( const String& rImportName )
559 for ( size_t i = 0, n = aImportList.size(); i < n; ++i )
560 if ( rImportName == aImportList[ i ]->aUIName )
561 return aImportList[ i ];
562 return NULL;
565 // ------------------------------------------------------------------------
567 String Gallery::GetThemeName( sal_uIntPtr nThemeId ) const
569 GalleryThemeEntry* pFound = NULL;
571 for ( size_t i = 0, n = aThemeList.size(); i < n && !pFound; ++i )
573 GalleryThemeEntry* pEntry = aThemeList[ i ];
574 if( nThemeId == pEntry->GetId() )
575 pFound = pEntry;
578 // try fallback, if no entry was found
579 if( !pFound )
581 ByteString aFallback;
583 switch( nThemeId )
585 case( GALLERY_THEME_3D ): aFallback = "3D"; break;
586 case( GALLERY_THEME_BULLETS ): aFallback = "Bullets"; break;
587 case( GALLERY_THEME_HOMEPAGE ): aFallback = "Homepage"; break;
588 case( GALLERY_THEME_HTMLBUTTONS ): aFallback = "private://gallery/hidden/HtmlExportButtons"; break;
589 case( GALLERY_THEME_POWERPOINT ): aFallback = "private://gallery/hidden/imgppt"; break;
590 case( GALLERY_THEME_FONTWORK ): aFallback = "private://gallery/hidden/fontwork"; break;
591 case( GALLERY_THEME_FONTWORK_VERTICAL ): aFallback = "private://gallery/hidden/fontworkvertical"; break;
592 case( GALLERY_THEME_RULERS ): aFallback = "Rulers"; break;
593 case( GALLERY_THEME_SOUNDS ): aFallback = "Sounds"; break;
595 default:
596 break;
599 pFound = ( (Gallery*) this )->ImplGetThemeEntry( String::CreateFromAscii( aFallback.GetBuffer() ) );
602 return( pFound ? pFound->GetThemeName() : String() );
605 // ------------------------------------------------------------------------
607 sal_Bool Gallery::HasTheme( const String& rThemeName )
609 return( ImplGetThemeEntry( rThemeName ) != NULL );
612 // ------------------------------------------------------------------------
614 sal_Bool Gallery::CreateTheme( const String& rThemeName, sal_uInt32 nNumFrom )
616 sal_Bool bRet = sal_False;
618 if( !HasTheme( rThemeName ) && ( GetUserURL().GetProtocol() != INET_PROT_NOT_VALID ) )
620 nLastFileNumber = nNumFrom > nLastFileNumber ? nNumFrom : nLastFileNumber + 1;
621 GalleryThemeEntry* pNewEntry = new GalleryThemeEntry( GetUserURL(), rThemeName,
622 nLastFileNumber,
623 sal_False, sal_False, sal_True, 0, sal_False );
625 aThemeList.push_back( pNewEntry );
626 delete( new GalleryTheme( this, pNewEntry ) );
627 Broadcast( GalleryHint( GALLERY_HINT_THEME_CREATED, rThemeName ) );
628 bRet = sal_True;
631 return bRet;
634 // ------------------------------------------------------------------------
636 sal_Bool Gallery::RenameTheme( const String& rOldName, const String& rNewName )
638 GalleryThemeEntry* pThemeEntry = ImplGetThemeEntry( rOldName );
639 sal_Bool bRet = sal_False;
641 // Ueberpruefen, ob neuer Themenname schon vorhanden ist
642 if( pThemeEntry && !HasTheme( rNewName ) && ( !pThemeEntry->IsReadOnly() || pThemeEntry->IsImported() ) )
644 SfxListener aListener;
645 GalleryTheme* pThm = AcquireTheme( rOldName, aListener );
647 if( pThm )
649 const String aOldName( rOldName );
651 pThemeEntry->SetName( rNewName );
652 pThm->ImplWrite();
654 if( pThemeEntry->IsImported() )
656 pThm->SetImportName( rNewName );
658 GalleryImportThemeEntry* pImportEntry = ImplGetImportThemeEntry( rOldName );
660 if( pImportEntry )
662 pImportEntry->aUIName = rNewName;
663 ImplWriteImportList();
667 Broadcast( GalleryHint( GALLERY_HINT_THEME_RENAMED, aOldName, pThm->GetName() ) );
668 ReleaseTheme( pThm, aListener );
669 bRet = sal_True;
673 return bRet;
676 // ------------------------------------------------------------------------
678 sal_Bool Gallery::RemoveTheme( const String& rThemeName )
680 GalleryThemeEntry* pThemeEntry = ImplGetThemeEntry( rThemeName );
681 sal_Bool bRet = sal_False;
683 if( pThemeEntry && ( !pThemeEntry->IsReadOnly() || pThemeEntry->IsImported() ) )
685 Broadcast( GalleryHint( GALLERY_HINT_CLOSE_THEME, rThemeName ) );
687 if( pThemeEntry->IsImported() )
689 GalleryImportThemeEntry* pImportEntry = ImplGetImportThemeEntry( rThemeName );
691 if( pImportEntry )
693 for ( GalleryImportThemeList::iterator it = aImportList.begin(); it < aImportList.end(); ++it )
695 if ( *it == pImportEntry )
697 delete pImportEntry;
698 aImportList.erase( it );
699 break;
704 else
706 SfxListener aListener;
707 GalleryTheme* pThm = AcquireTheme( rThemeName, aListener );
709 if( pThm )
711 INetURLObject aThmURL( pThm->GetThmURL() );
712 INetURLObject aSdgURL( pThm->GetSdgURL() );
713 INetURLObject aSdvURL( pThm->GetSdvURL() );
715 ReleaseTheme( pThm, aListener );
717 KillFile( aThmURL );
718 KillFile( aSdgURL );
719 KillFile( aSdvURL );
723 for ( GalleryThemeList::iterator it = aThemeList.begin(); it < aThemeList.end(); ++it )
725 if ( pThemeEntry == *it ) {
726 delete pThemeEntry;
727 aThemeList.erase( it );
728 break;
732 Broadcast( GalleryHint( GALLERY_HINT_THEME_REMOVED, rThemeName ) );
734 bRet = sal_True;
737 return bRet;
740 // ------------------------------------------------------------------------
742 INetURLObject Gallery::GetImportURL( const String& rThemeName )
744 INetURLObject aURL;
745 GalleryImportThemeEntry* pImportEntry = ImplGetImportThemeEntry( rThemeName );
747 if( pImportEntry )
749 aURL = pImportEntry->aURL;
750 DBG_ASSERT( aURL.GetProtocol() != INET_PROT_NOT_VALID, "invalid URL" );
753 return aURL;
756 // ------------------------------------------------------------------------
758 GalleryTheme* Gallery::ImplGetCachedTheme( const GalleryThemeEntry* pThemeEntry )
760 GalleryTheme* pTheme = NULL;
762 if( pThemeEntry )
764 for (GalleryCacheThemeList::const_iterator it = aThemeCache.begin(); it != aThemeCache.end(); ++it)
766 if (pThemeEntry == (*it)->GetThemeEntry())
768 pTheme = (*it)->GetTheme();
769 break;
773 if( !pTheme )
775 INetURLObject aURL;
777 if( !pThemeEntry->IsImported() )
778 aURL = pThemeEntry->GetThmURL();
779 else
780 aURL = GetImportURL( pThemeEntry->GetThemeName() );
782 DBG_ASSERT( aURL.GetProtocol() != INET_PROT_NOT_VALID, "invalid URL" );
784 if( FileExists( aURL ) )
786 SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( aURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ );
788 if( pIStm )
790 pTheme = new GalleryTheme( this, (GalleryThemeEntry*) pThemeEntry );
791 *pIStm >> *pTheme;
793 if( pIStm->GetError() )
794 delete pTheme, pTheme = NULL;
795 else if( pThemeEntry->IsImported() )
796 pTheme->SetImportName( pThemeEntry->GetThemeName() );
798 delete pIStm;
802 if( pTheme )
803 aThemeCache.push_back( new GalleryThemeCacheEntry( pThemeEntry, pTheme ));
807 return pTheme;
810 // ------------------------------------------------------------------------
812 void Gallery::ImplDeleteCachedTheme( GalleryTheme* pTheme )
814 for (GalleryCacheThemeList::iterator it = aThemeCache.begin(); it != aThemeCache.end(); ++it)
816 if (pTheme == (*it)->GetTheme())
818 delete *it;
819 aThemeCache.erase(it);
820 break;
825 // ------------------------------------------------------------------------
827 GalleryTheme* Gallery::AcquireTheme( const String& rThemeName, SfxListener& rListener )
829 GalleryTheme* pTheme = NULL;
830 GalleryThemeEntry* pThemeEntry = ImplGetThemeEntry( rThemeName );
832 if( pThemeEntry && ( ( pTheme = ImplGetCachedTheme( pThemeEntry ) ) != NULL ) )
833 rListener.StartListening( *pTheme );
835 return pTheme;
838 // ------------------------------------------------------------------------
840 void Gallery::ReleaseTheme( GalleryTheme* pTheme, SfxListener& rListener )
842 if( pTheme )
844 rListener.EndListening( *pTheme );
846 if( !pTheme->HasListeners() )
847 ImplDeleteCachedTheme( pTheme );
851 sal_Bool GalleryThemeEntry::IsDefault() const
852 { return( ( nId > 0 ) && ( nId != ( RID_GALLERYSTR_THEME_MYTHEME - RID_GALLERYSTR_THEME_START ) ) ); }
854 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */