Update ooo320-m1
[ooovba.git] / svx / source / gallery2 / gallery1.cxx
blob4751c8107f1b52ff1e13908bbeb6cb53c38673c3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: gallery1.cxx,v $
10 * $Revision: 1.26 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
34 #define ENABLE_BYTESTRING_STREAM_OPERATORS
36 #include <tools/vcompat.hxx>
37 #include <ucbhelper/content.hxx>
38 #include <unotools/ucbstreamhelper.hxx>
39 #include <svtools/pathoptions.hxx>
40 #include <sfx2/docfile.hxx>
41 #include "gallery.hxx"
42 #include "galmisc.hxx"
43 #include "galtheme.hxx"
44 #include "gallery1.hxx"
45 #include <com/sun/star/sdbc/XResultSet.hpp>
46 #include <com/sun/star/ucb/XContentAccess.hpp>
48 #define ENABLE_BYTESTRING_STREAM_OPERATORS
50 // --------------
51 // - Namespaces -
52 // --------------
54 using namespace ::rtl;
55 using namespace ::com::sun::star;
57 // ---------------------
58 // - GalleryThemeEntry -
59 // ---------------------
61 GalleryThemeEntry::GalleryThemeEntry( const INetURLObject& rBaseURL, const String& rName,
62 UINT32 _nFileNumber, BOOL _bReadOnly, BOOL _bImported,
63 BOOL _bNewFile, UINT32 _nId, BOOL _bThemeNameFromResource ) :
64 nFileNumber ( _nFileNumber ),
65 nId ( _nId ),
66 bReadOnly ( _bReadOnly || _bImported ),
67 bImported ( _bImported ),
68 bThemeNameFromResource ( _bThemeNameFromResource )
70 INetURLObject aURL( rBaseURL );
71 DBG_ASSERT( aURL.GetProtocol() != INET_PROT_NOT_VALID, "invalid URL" );
72 String aFileName( String( RTL_CONSTASCII_USTRINGPARAM( "sg" ) ) );
74 aURL.Append( ( aFileName += String::CreateFromInt32( nFileNumber ) ) += String( RTL_CONSTASCII_USTRINGPARAM( ".thm" ) ) );
75 aThmURL = ImplGetURLIgnoreCase( aURL );
77 aURL.setExtension( String( RTL_CONSTASCII_USTRINGPARAM( "sdg" ) ) );
78 aSdgURL = ImplGetURLIgnoreCase( aURL );
80 aURL.setExtension( String( RTL_CONSTASCII_USTRINGPARAM( "sdv" ) ) );
81 aSdvURL = ImplGetURLIgnoreCase( aURL );
83 SetModified( _bNewFile );
85 if( nId && bThemeNameFromResource )
86 aName = String( GAL_RESID( RID_GALLERYSTR_THEME_START + (USHORT) nId ) );
88 if( !aName.Len() )
89 aName = rName;
92 // -----------------------------------------------------------------------------
94 INetURLObject GalleryThemeEntry::ImplGetURLIgnoreCase( const INetURLObject& rURL ) const
96 INetURLObject aURL( rURL );
97 String aFileName;
98 BOOL bExists = FALSE;
100 // check original file name
101 if( FileExists( aURL ) )
102 bExists = TRUE;
103 else
105 // check upper case file name
106 aURL.setName( aURL.getName().toAsciiUpperCase() );
108 if( FileExists( aURL ) )
109 bExists = TRUE;
110 else
112 // check lower case file name
113 aURL.setName( aURL.getName().toAsciiLowerCase() );
115 if( FileExists( aURL ) )
116 bExists = TRUE;
120 return aURL;
123 // -----------------------------------------------------------------------------
125 void GalleryThemeEntry::SetName( const String& rNewName )
127 if( aName != rNewName )
129 aName = rNewName;
130 SetModified( TRUE );
131 bThemeNameFromResource = FALSE;
135 // -----------------------------------------------------------------------------
137 void GalleryThemeEntry::SetId( UINT32 nNewId, BOOL bResetThemeName )
139 nId = nNewId;
140 SetModified( TRUE );
141 bThemeNameFromResource = ( nId && bResetThemeName );
144 // ---------------------------
145 // - GalleryImportThemeEntry -
146 // ---------------------------
148 SvStream& operator<<( SvStream& rOut, const GalleryImportThemeEntry& rEntry )
150 ByteString aDummy;
152 rOut << ByteString( rEntry.aThemeName, RTL_TEXTENCODING_UTF8 ) <<
153 ByteString( rEntry.aUIName, RTL_TEXTENCODING_UTF8 ) <<
154 ByteString( String(rEntry.aURL.GetMainURL( INetURLObject::NO_DECODE )), RTL_TEXTENCODING_UTF8 ) <<
155 ByteString( rEntry.aImportName, RTL_TEXTENCODING_UTF8 ) <<
156 aDummy;
158 return rOut;
161 // ------------------------------------------------------------------------
163 SvStream& operator>>( SvStream& rIn, GalleryImportThemeEntry& rEntry )
165 ByteString aTmpStr;
167 rIn >> aTmpStr; rEntry.aThemeName = String( aTmpStr, RTL_TEXTENCODING_UTF8 );
168 rIn >> aTmpStr; rEntry.aUIName = String( aTmpStr, RTL_TEXTENCODING_UTF8 );
169 rIn >> aTmpStr; rEntry.aURL = INetURLObject( String( aTmpStr, RTL_TEXTENCODING_UTF8 ) );
170 rIn >> aTmpStr; rEntry.aImportName = String( aTmpStr, RTL_TEXTENCODING_UTF8 );
171 rIn >> aTmpStr;
173 return rIn;
176 // --------------------------
177 // - GalleryThemeCacheEntry -
178 // --------------------------
180 class GalleryThemeCacheEntry
182 private:
184 const GalleryThemeEntry* mpThemeEntry;
185 GalleryTheme* mpTheme;
187 public:
189 GalleryThemeCacheEntry( const GalleryThemeEntry* pThemeEntry, GalleryTheme* pTheme ) :
190 mpThemeEntry( pThemeEntry ), mpTheme( pTheme ) {}
191 ~GalleryThemeCacheEntry() { delete mpTheme; }
193 const GalleryThemeEntry* GetThemeEntry() const { return mpThemeEntry; }
194 GalleryTheme* GetTheme() const { return mpTheme; }
197 // -----------
198 // - Gallery -
199 // -----------
201 Gallery::Gallery( const String& rMultiPath )
202 : nReadTextEncoding ( gsl_getSystemTextEncoding() )
203 , nLastFileNumber ( 0 )
204 , bMultiPath ( FALSE )
206 ImplLoad( rMultiPath );
209 // ------------------------------------------------------------------------
211 Gallery::~Gallery()
213 // Themen-Liste loeschen
214 for( GalleryThemeEntry* pThemeEntry = aThemeList.First(); pThemeEntry; pThemeEntry = aThemeList.Next() )
215 delete pThemeEntry;
217 // Import-Liste loeschen
218 for( GalleryImportThemeEntry* pImportEntry = aImportList.First(); pImportEntry; pImportEntry = aImportList.Next() )
219 delete pImportEntry;
222 // ------------------------------------------------------------------------
224 Gallery* Gallery::GetGalleryInstance()
226 static Gallery* pGallery = NULL;
228 if( !pGallery )
230 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
231 if( !pGallery )
233 pGallery = new Gallery( SvtPathOptions().GetGalleryPath() );
237 return pGallery;
240 // ------------------------------------------------------------------------
242 void Gallery::ImplLoad( const String& rMultiPath )
244 const USHORT nTokenCount = rMultiPath.GetTokenCount( ';' );
245 sal_Bool bIsReadOnlyDir;
247 bMultiPath = ( nTokenCount > 0 );
249 INetURLObject aCurURL(SvtPathOptions().GetConfigPath());
250 ImplLoadSubDirs( aCurURL, bIsReadOnlyDir );
252 if( !bIsReadOnlyDir )
253 aUserURL = aCurURL;
255 if( bMultiPath )
257 aRelURL = INetURLObject( rMultiPath.GetToken( 0, ';' ) );
259 for( USHORT i = 0UL; i < nTokenCount; i++ )
261 aCurURL = INetURLObject(rMultiPath.GetToken( i, ';' ));
263 ImplLoadSubDirs( aCurURL, bIsReadOnlyDir );
265 if( !bIsReadOnlyDir )
266 aUserURL = aCurURL;
269 else
270 aRelURL = INetURLObject( rMultiPath );
272 DBG_ASSERT( aUserURL.GetProtocol() != INET_PROT_NOT_VALID, "no writable Gallery user directory available" );
273 DBG_ASSERT( aRelURL.GetProtocol() != INET_PROT_NOT_VALID, "invalid URL" );
275 ImplLoadImports();
278 // ------------------------------------------------------------------------
280 void Gallery::ImplLoadSubDirs( const INetURLObject& rBaseURL, sal_Bool& rbDirIsReadOnly )
282 rbDirIsReadOnly = sal_False;
286 uno::Reference< ucb::XCommandEnvironment > xEnv;
287 ::ucbhelper::Content aCnt( rBaseURL.GetMainURL( INetURLObject::NO_DECODE ), xEnv );
289 uno::Sequence< OUString > aProps( 1 );
290 aProps.getArray()[ 0 ] = OUString::createFromAscii( "Url" );
292 uno::Reference< sdbc::XResultSet > xResultSet( aCnt.createCursor( aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY ) );
296 // check readonlyness the very hard way
297 INetURLObject aTestURL( rBaseURL );
298 String aTestFile( RTL_CONSTASCII_USTRINGPARAM( "cdefghij.klm" ) );
300 aTestURL.Append( aTestFile );
301 SvStream* pTestStm = ::utl::UcbStreamHelper::CreateStream( aTestURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_WRITE );
303 if( pTestStm )
305 *pTestStm << 1;
307 if( pTestStm->GetError() )
308 rbDirIsReadOnly = sal_True;
310 delete pTestStm;
311 KillFile( aTestURL );
313 else
314 rbDirIsReadOnly = sal_True;
316 catch( const ucb::ContentCreationException& )
319 catch( const uno::RuntimeException& )
322 catch( const uno::Exception& )
326 if( xResultSet.is() )
328 uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
330 if( xContentAccess.is() )
332 while( xResultSet->next() )
334 INetURLObject aThmURL( xContentAccess->queryContentIdentifierString() );
336 if(aThmURL.GetExtension().equalsIgnoreAsciiCaseAscii("thm"))
338 INetURLObject aSdgURL( aThmURL); aSdgURL.SetExtension( OUString::createFromAscii( "sdg" ) );
339 INetURLObject aSdvURL( aThmURL ); aSdvURL.SetExtension( OUString::createFromAscii( "sdv" ) );
340 const OUString aTitleProp( OUString::createFromAscii( "Title" ) );
341 const OUString aReadOnlyProp( OUString::createFromAscii( "IsReadOnly" ) );
342 OUString aTitle;
343 sal_Bool bReadOnly = sal_False;
347 ::ucbhelper::Content aThmCnt( aThmURL.GetMainURL( INetURLObject::NO_DECODE ), xEnv );
348 ::ucbhelper::Content aSdgCnt( aSdgURL.GetMainURL( INetURLObject::NO_DECODE ), xEnv );
349 ::ucbhelper::Content aSdvCnt( aSdvURL.GetMainURL( INetURLObject::NO_DECODE ), xEnv );
353 aThmCnt.getPropertyValue( aTitleProp ) >>= aTitle;
355 catch( const uno::RuntimeException& )
358 catch( const uno::Exception& )
362 if( aTitle.getLength() )
366 aThmCnt.getPropertyValue( aReadOnlyProp ) >>= bReadOnly;
368 catch( const uno::RuntimeException& )
371 catch( const uno::Exception& )
375 if( !bReadOnly )
379 aSdgCnt.getPropertyValue( aTitleProp ) >>= aTitle;
381 catch( const ::com::sun::star::uno::RuntimeException& )
384 catch( const ::com::sun::star::uno::Exception& )
388 if( aTitle.getLength() )
392 aSdgCnt.getPropertyValue( aReadOnlyProp ) >>= bReadOnly;
394 catch( const uno::RuntimeException& )
397 catch( const uno::Exception& )
403 if( !bReadOnly )
407 aSdvCnt.getPropertyValue( aTitleProp ) >>= aTitle;
409 catch( const ::com::sun::star::uno::RuntimeException& )
412 catch( const ::com::sun::star::uno::Exception& )
416 if( aTitle.getLength() )
420 aSdvCnt.getPropertyValue( aReadOnlyProp ) >>= bReadOnly;
422 catch( const uno::RuntimeException& )
425 catch( const uno::Exception& )
431 GalleryThemeEntry* pEntry = GalleryTheme::CreateThemeEntry( aThmURL, rbDirIsReadOnly || bReadOnly );
433 if( pEntry )
435 const ULONG nFileNumber = (ULONG) String(aThmURL.GetBase()).Erase( 0, 2 ).Erase( 6 ).ToInt32();
437 aThemeList.Insert( pEntry, LIST_APPEND );
439 if( nFileNumber > nLastFileNumber )
440 nLastFileNumber = nFileNumber;
444 catch( const ucb::ContentCreationException& )
447 catch( const uno::RuntimeException& )
450 catch( const uno::Exception& )
458 catch( const ucb::ContentCreationException& )
461 catch( const uno::RuntimeException& )
464 catch( const uno::Exception& )
469 // ------------------------------------------------------------------------
471 void Gallery::ImplLoadImports()
473 INetURLObject aURL( GetUserURL() );
475 aURL.Append( String( RTL_CONSTASCII_USTRINGPARAM( "gallery.sdi" ) ) );
477 if( FileExists( aURL ) )
479 SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( aURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ );
481 if( pIStm )
483 GalleryThemeEntry* pThemeEntry;
484 GalleryImportThemeEntry* pImportEntry;
485 INetURLObject aFile;
486 UINT32 nInventor;
487 UINT32 nCount;
488 UINT16 nId;
489 UINT16 i;
490 UINT16 nTempCharSet;
492 for( pImportEntry = aImportList.First(); pImportEntry; pImportEntry = aImportList.Next() )
493 delete pImportEntry;
495 aImportList.Clear();
496 *pIStm >> nInventor;
498 if( nInventor == COMPAT_FORMAT( 'S', 'G', 'A', '3' ) )
500 *pIStm >> nId >> nCount >> nTempCharSet;
502 for( i = 0; i < nCount; i++ )
504 pImportEntry = new GalleryImportThemeEntry;
506 *pIStm >> *pImportEntry;
507 aImportList.Insert( pImportEntry, LIST_APPEND );
508 aFile = INetURLObject( pImportEntry->aURL );
509 pThemeEntry = new GalleryThemeEntry( aFile,
510 pImportEntry->aUIName,
511 String(aFile.GetBase()).Erase( 0, 2 ).Erase( 6 ).ToInt32(),
512 TRUE, TRUE, FALSE, 0, FALSE );
514 aThemeList.Insert( pThemeEntry, LIST_APPEND );
518 delete pIStm;
523 // ------------------------------------------------------------------------
525 void Gallery::ImplWriteImportList()
527 INetURLObject aURL( GetUserURL() );
528 aURL.Append( ( String( "gallery.sdi", RTL_TEXTENCODING_UTF8 ) ) );
529 SvStream* pOStm = ::utl::UcbStreamHelper::CreateStream( aURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_WRITE | STREAM_TRUNC );
531 if( pOStm )
533 const UINT32 nInventor = (UINT32) COMPAT_FORMAT( 'S', 'G', 'A', '3' );
534 const UINT16 nId = 0x0004;
536 *pOStm << nInventor << nId << (UINT32) aImportList.Count() << (UINT16) gsl_getSystemTextEncoding();
538 for( GalleryImportThemeEntry* pImportEntry = aImportList.First(); pImportEntry; pImportEntry = aImportList.Next() )
539 *pOStm << *pImportEntry;
541 if( pOStm->GetError() )
542 ErrorHandler::HandleError( ERRCODE_IO_GENERAL );
544 delete pOStm;
548 // ------------------------------------------------------------------------
550 GalleryThemeEntry* Gallery::ImplGetThemeEntry( const String& rThemeName )
552 GalleryThemeEntry* pFound = NULL;
554 if( rThemeName.Len() )
555 for( GalleryThemeEntry* pEntry = aThemeList.First(); pEntry && !pFound; pEntry = aThemeList.Next() )
556 if( rThemeName == pEntry->GetThemeName() )
557 pFound = pEntry;
559 return pFound;
562 // ------------------------------------------------------------------------
564 GalleryImportThemeEntry* Gallery::ImplGetImportThemeEntry( const String& rImportName )
566 GalleryImportThemeEntry* pFound = NULL;
568 for( GalleryImportThemeEntry* pImportEntry = aImportList.First(); pImportEntry && !pFound; pImportEntry = aImportList.Next() )
569 if ( rImportName == pImportEntry->aUIName )
570 pFound = pImportEntry;
572 return pFound;
575 // ------------------------------------------------------------------------
577 String Gallery::GetThemeName( ULONG nThemeId ) const
579 GalleryThemeEntry* pFound = NULL;
581 for( ULONG n = 0, nCount = aThemeList.Count(); n < nCount; n++ )
583 GalleryThemeEntry* pEntry = aThemeList.GetObject( n );
585 if( nThemeId == pEntry->GetId() )
586 pFound = pEntry;
589 // try fallback, if no entry was found
590 if( !pFound )
592 ByteString aFallback;
594 switch( nThemeId )
596 case( GALLERY_THEME_3D ): aFallback = "3D"; break;
597 case( GALLERY_THEME_BULLETS ): aFallback = "Bullets"; break;
598 case( GALLERY_THEME_HOMEPAGE ): aFallback = "Homepage"; break;
599 case( GALLERY_THEME_HTMLBUTTONS ): aFallback = "private://gallery/hidden/HtmlExportButtons"; break;
600 case( GALLERY_THEME_POWERPOINT ): aFallback = "private://gallery/hidden/imgppt"; break;
601 case( GALLERY_THEME_FONTWORK ): aFallback = "private://gallery/hidden/fontwork"; break;
602 case( GALLERY_THEME_FONTWORK_VERTICAL ): aFallback = "private://gallery/hidden/fontworkvertical"; break;
603 case( GALLERY_THEME_RULERS ): aFallback = "Rulers"; break;
604 case( GALLERY_THEME_SOUNDS ): aFallback = "Sounds"; break;
606 default:
607 break;
610 pFound = ( (Gallery*) this )->ImplGetThemeEntry( String::CreateFromAscii( aFallback.GetBuffer() ) );
613 return( pFound ? pFound->GetThemeName() : String() );
616 // ------------------------------------------------------------------------
618 BOOL Gallery::HasTheme( const String& rThemeName )
620 return( ImplGetThemeEntry( rThemeName ) != NULL );
623 // ------------------------------------------------------------------------
625 BOOL Gallery::CreateTheme( const String& rThemeName, UINT32 nNumFrom )
627 BOOL bRet = FALSE;
629 if( !HasTheme( rThemeName ) && ( GetUserURL().GetProtocol() != INET_PROT_NOT_VALID ) )
631 nLastFileNumber=nNumFrom > nLastFileNumber ? nNumFrom : ++nLastFileNumber;
632 GalleryThemeEntry* pNewEntry = new GalleryThemeEntry( GetUserURL(), rThemeName,
633 nLastFileNumber,
634 FALSE, FALSE, TRUE, 0, FALSE );
636 aThemeList.Insert( pNewEntry, LIST_APPEND );
637 delete( new GalleryTheme( this, pNewEntry ) );
638 Broadcast( GalleryHint( GALLERY_HINT_THEME_CREATED, rThemeName ) );
639 bRet = TRUE;
642 return bRet;
645 // ------------------------------------------------------------------------
647 BOOL Gallery::CreateImportTheme( const INetURLObject& rURL, const String& rImportName )
649 INetURLObject aURL( rURL );
650 BOOL bRet = FALSE;
652 DBG_ASSERT( aURL.GetProtocol() != INET_PROT_NOT_VALID, "invalid URL" );
654 if( FileExists( aURL ) )
656 SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( aURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ );
658 if( pIStm )
660 ULONG nStmErr;
661 UINT16 nId;
663 *pIStm >> nId;
665 if( nId > 0x0004 )
666 ErrorHandler::HandleError( ERRCODE_IO_GENERAL );
667 else
669 ByteString aTmpStr;
670 String aThemeName; *pIStm >> aTmpStr; aThemeName = String( aTmpStr, RTL_TEXTENCODING_UTF8 );
671 GalleryThemeEntry* pThemeEntry = new GalleryThemeEntry( aURL, rImportName,
672 String(aURL.GetBase()).Erase( 0, 2 ).Erase( 6 ).ToInt32(),
673 TRUE, TRUE, TRUE, 0, FALSE );
674 GalleryTheme* pImportTheme = new GalleryTheme( this, pThemeEntry );
676 pIStm->Seek( STREAM_SEEK_TO_BEGIN );
677 *pIStm >> *pImportTheme;
678 nStmErr = pIStm->GetError();
680 if( nStmErr )
682 delete pThemeEntry;
683 ErrorHandler::HandleError( ERRCODE_IO_GENERAL );
685 else
687 String aName( rImportName );
688 String aNewName( aName );
689 ULONG nCount = 0;
691 aName += ' ';
693 while ( HasTheme( aNewName ) && ( nCount++ < 16000 ) )
695 aNewName = aName;
696 aNewName += String::CreateFromInt32( nCount );
699 pImportTheme->SetImportName( aNewName );
700 aThemeList.Insert( pThemeEntry, LIST_APPEND );
702 // Thema in Import-Liste eintragen und Import-Liste speichern
703 GalleryImportThemeEntry* pImportEntry = new GalleryImportThemeEntry;
704 pImportEntry->aThemeName = pImportEntry->aUIName = aNewName;
705 pImportEntry->aURL = rURL;
706 pImportEntry->aImportName = rImportName;
707 aImportList.Insert( pImportEntry, LIST_APPEND );
708 ImplWriteImportList();
709 bRet = TRUE;
712 delete pImportTheme;
715 delete pIStm;
719 return bRet;
722 // ------------------------------------------------------------------------
724 BOOL Gallery::RenameTheme( const String& rOldName, const String& rNewName )
726 GalleryThemeEntry* pThemeEntry = ImplGetThemeEntry( rOldName );
727 BOOL bRet = FALSE;
729 // Ueberpruefen, ob neuer Themenname schon vorhanden ist
730 if( pThemeEntry && !HasTheme( rNewName ) && ( !pThemeEntry->IsReadOnly() || pThemeEntry->IsImported() ) )
732 SfxListener aListener;
733 GalleryTheme* pThm = AcquireTheme( rOldName, aListener );
735 if( pThm )
737 const String aOldName( rOldName );
739 pThemeEntry->SetName( rNewName );
740 pThm->ImplWrite();
742 if( pThemeEntry->IsImported() )
744 pThm->SetImportName( rNewName );
746 GalleryImportThemeEntry* pImportEntry = ImplGetImportThemeEntry( rOldName );
748 if( pImportEntry )
750 pImportEntry->aUIName = rNewName;
751 ImplWriteImportList();
755 Broadcast( GalleryHint( GALLERY_HINT_THEME_RENAMED, aOldName, pThm->GetName() ) );
756 ReleaseTheme( pThm, aListener );
757 bRet = TRUE;
761 return bRet;
764 // ------------------------------------------------------------------------
766 BOOL Gallery::RemoveTheme( const String& rThemeName )
768 GalleryThemeEntry* pThemeEntry = ImplGetThemeEntry( rThemeName );
769 BOOL bRet = FALSE;
771 if( pThemeEntry && ( !pThemeEntry->IsReadOnly() || pThemeEntry->IsImported() ) )
773 Broadcast( GalleryHint( GALLERY_HINT_CLOSE_THEME, rThemeName ) );
775 if( pThemeEntry->IsImported() )
777 GalleryImportThemeEntry* pImportEntry = ImplGetImportThemeEntry( rThemeName );
779 if( pImportEntry )
781 delete aImportList.Remove( pImportEntry );
782 ImplWriteImportList();
785 else
787 SfxListener aListener;
788 GalleryTheme* pThm = AcquireTheme( rThemeName, aListener );
790 if( pThm )
792 INetURLObject aThmURL( pThm->GetThmURL() );
793 INetURLObject aSdgURL( pThm->GetSdgURL() );
794 INetURLObject aSdvURL( pThm->GetSdvURL() );
796 ReleaseTheme( pThm, aListener );
798 KillFile( aThmURL );
799 KillFile( aSdgURL );
800 KillFile( aSdvURL );
804 delete aThemeList.Remove( pThemeEntry );
805 Broadcast( GalleryHint( GALLERY_HINT_THEME_REMOVED, rThemeName ) );
807 bRet = TRUE;
810 return bRet;
813 // ------------------------------------------------------------------------
815 INetURLObject Gallery::GetImportURL( const String& rThemeName )
817 INetURLObject aURL;
818 GalleryImportThemeEntry* pImportEntry = ImplGetImportThemeEntry( rThemeName );
820 if( pImportEntry )
822 aURL = pImportEntry->aURL;
823 DBG_ASSERT( aURL.GetProtocol() != INET_PROT_NOT_VALID, "invalid URL" );
826 return aURL;
829 // ------------------------------------------------------------------------
831 GalleryTheme* Gallery::ImplGetCachedTheme( const GalleryThemeEntry* pThemeEntry )
833 GalleryTheme* pTheme = NULL;
835 if( pThemeEntry )
837 GalleryThemeCacheEntry* pEntry;
839 for( pEntry = (GalleryThemeCacheEntry*) aThemeCache.First(); pEntry && !pTheme; pEntry = (GalleryThemeCacheEntry*) aThemeCache.Next() )
840 if( pThemeEntry == pEntry->GetThemeEntry() )
841 pTheme = pEntry->GetTheme();
843 if( !pTheme )
845 INetURLObject aURL;
847 if( !pThemeEntry->IsImported() )
848 aURL = pThemeEntry->GetThmURL();
849 else
850 aURL = GetImportURL( pThemeEntry->GetThemeName() );
852 DBG_ASSERT( aURL.GetProtocol() != INET_PROT_NOT_VALID, "invalid URL" );
854 if( FileExists( aURL ) )
856 SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( aURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ );
858 if( pIStm )
860 pTheme = new GalleryTheme( this, (GalleryThemeEntry*) pThemeEntry );
861 *pIStm >> *pTheme;
863 if( pIStm->GetError() )
864 delete pTheme, pTheme = NULL;
865 else if( pThemeEntry->IsImported() )
866 pTheme->SetImportName( pThemeEntry->GetThemeName() );
868 delete pIStm;
872 if( pTheme )
873 aThemeCache.Insert( new GalleryThemeCacheEntry( pThemeEntry, pTheme ), LIST_APPEND );
877 return pTheme;
880 // ------------------------------------------------------------------------
882 void Gallery::ImplDeleteCachedTheme( GalleryTheme* pTheme )
884 GalleryThemeCacheEntry* pEntry;
885 BOOL bDone = FALSE;
887 for( pEntry = (GalleryThemeCacheEntry*) aThemeCache.First(); pEntry && !bDone; pEntry = (GalleryThemeCacheEntry*) aThemeCache.Next() )
889 if( pTheme == pEntry->GetTheme() )
891 delete (GalleryThemeCacheEntry*) aThemeCache.Remove( pEntry );
892 bDone = TRUE;
897 // ------------------------------------------------------------------------
899 GalleryTheme* Gallery::AcquireTheme( const String& rThemeName, SfxListener& rListener )
901 GalleryTheme* pTheme = NULL;
902 GalleryThemeEntry* pThemeEntry = ImplGetThemeEntry( rThemeName );
904 if( pThemeEntry && ( ( pTheme = ImplGetCachedTheme( pThemeEntry ) ) != NULL ) )
905 rListener.StartListening( *pTheme );
907 return pTheme;
910 // ------------------------------------------------------------------------
912 void Gallery::ReleaseTheme( GalleryTheme* pTheme, SfxListener& rListener )
914 if( pTheme )
916 rListener.EndListening( *pTheme );
918 if( !pTheme->HasListeners() )
919 ImplDeleteCachedTheme( pTheme );