xref: /aoo41x/main/svx/source/gallery2/gallery1.cxx (revision f6e50924)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svx.hxx"
26 
27 #define ENABLE_BYTESTRING_STREAM_OPERATORS
28 
29 #include <tools/vcompat.hxx>
30 #include <ucbhelper/content.hxx>
31 #include <unotools/ucbstreamhelper.hxx>
32 #include <unotools/pathoptions.hxx>
33 #include <sfx2/docfile.hxx>
34 #include "svx/gallery.hxx"
35 #include "gallery.hrc"
36 #include "svx/galmisc.hxx"
37 #include "svx/galtheme.hxx"
38 #include "svx/gallery1.hxx"
39 #include <com/sun/star/sdbc/XResultSet.hpp>
40 #include <com/sun/star/ucb/XContentAccess.hpp>
41 
42 #define ENABLE_BYTESTRING_STREAM_OPERATORS
43 
44 // --------------
45 // - Namespaces -
46 // --------------
47 
48 using namespace ::rtl;
49 using namespace ::com::sun::star;
50 
51 // ---------------------
52 // - GalleryThemeEntry -
53 // ---------------------
54 
55 GalleryThemeEntry::GalleryThemeEntry( const INetURLObject& rBaseURL, const String& rName,
56 									  sal_uInt32 _nFileNumber, sal_Bool _bReadOnly, sal_Bool _bImported,
57 									  sal_Bool _bNewFile, sal_uInt32 _nId, sal_Bool _bThemeNameFromResource ) :
58 		nFileNumber                             ( _nFileNumber ),
59 		nId                                     ( _nId ),
60 		bReadOnly                               ( _bReadOnly || _bImported ),
61 		bImported                               ( _bImported ),
62 		bThemeNameFromResource  ( _bThemeNameFromResource )
63 {
64 	INetURLObject aURL( rBaseURL );
65 	DBG_ASSERT( aURL.GetProtocol() != INET_PROT_NOT_VALID, "invalid URL" );
66 	String aFileName( String( RTL_CONSTASCII_USTRINGPARAM( "sg" ) ) );
67 
68 	aURL.Append( ( aFileName += String::CreateFromInt32( nFileNumber ) ) += String( RTL_CONSTASCII_USTRINGPARAM( ".thm" ) ) );
69 	aThmURL = ImplGetURLIgnoreCase( aURL );
70 
71 	aURL.setExtension( String( RTL_CONSTASCII_USTRINGPARAM( "sdg" ) ) );
72 	aSdgURL = ImplGetURLIgnoreCase( aURL );
73 
74 	aURL.setExtension( String( RTL_CONSTASCII_USTRINGPARAM( "sdv" ) ) );
75 	aSdvURL = ImplGetURLIgnoreCase( aURL );
76 
77 	SetModified( _bNewFile );
78 
79 	if( nId && bThemeNameFromResource )
80 		aName = String( GAL_RESID( RID_GALLERYSTR_THEME_START + (sal_uInt16) nId ) );
81 
82 	if( !aName.Len() )
83 		aName = rName;
84 }
85 
86 // -----------------------------------------------------------------------------
87 
88 INetURLObject GalleryThemeEntry::ImplGetURLIgnoreCase( const INetURLObject& rURL ) const
89 {
90 	INetURLObject	aURL( rURL );
91 	String			aFileName;
92 	sal_Bool			bExists = sal_False;
93 
94 	// check original file name
95 	if( FileExists( aURL ) )
96 		bExists = sal_True;
97 	else
98 	{
99 		// check upper case file name
100 		aURL.setName( aURL.getName().toAsciiUpperCase() );
101 
102 		if( FileExists( aURL ) )
103 			bExists = sal_True;
104 		else
105 		{
106 			// check lower case file name
107 			aURL.setName( aURL.getName().toAsciiLowerCase() );
108 
109 			if( FileExists( aURL ) )
110 				bExists = sal_True;
111 		}
112 	}
113 
114 	return aURL;
115 }
116 
117 // -----------------------------------------------------------------------------
118 
119 void GalleryThemeEntry::SetName( const String& rNewName )
120 {
121 	if( aName != rNewName )
122 	{
123 		aName = rNewName;
124 		SetModified( sal_True );
125 		bThemeNameFromResource = sal_False;
126 	}
127 }
128 
129 // -----------------------------------------------------------------------------
130 
131 void GalleryThemeEntry::SetId( sal_uInt32 nNewId, sal_Bool bResetThemeName )
132 {
133 	nId = nNewId;
134 	SetModified( sal_True );
135 	bThemeNameFromResource = ( nId && bResetThemeName );
136 }
137 
138 // ---------------------------
139 // - GalleryImportThemeEntry -
140 // ---------------------------
141 
142 SvStream& operator<<( SvStream& rOut, const GalleryImportThemeEntry& rEntry )
143 {
144 	ByteString aDummy;
145 
146 	rOut << ByteString( rEntry.aThemeName, RTL_TEXTENCODING_UTF8 ) <<
147 			ByteString( rEntry.aUIName, RTL_TEXTENCODING_UTF8 ) <<
148 			ByteString( String(rEntry.aURL.GetMainURL( INetURLObject::NO_DECODE )), RTL_TEXTENCODING_UTF8 ) <<
149 			ByteString( rEntry.aImportName, RTL_TEXTENCODING_UTF8 ) <<
150 			aDummy;
151 
152 	return rOut;
153 }
154 
155 // ------------------------------------------------------------------------
156 
157 SvStream& operator>>( SvStream& rIn, GalleryImportThemeEntry& rEntry )
158 {
159 	ByteString aTmpStr;
160 
161 	rIn >> aTmpStr; rEntry.aThemeName = String( aTmpStr, RTL_TEXTENCODING_UTF8 );
162 	rIn >> aTmpStr; rEntry.aUIName = String( aTmpStr, RTL_TEXTENCODING_UTF8 );
163 	rIn >> aTmpStr; rEntry.aURL = INetURLObject( String( aTmpStr, RTL_TEXTENCODING_UTF8 ) );
164 	rIn >> aTmpStr; rEntry.aImportName = String( aTmpStr, RTL_TEXTENCODING_UTF8 );
165 	rIn >> aTmpStr;
166 
167 	return rIn;
168 }
169 
170 // --------------------------
171 // - GalleryThemeCacheEntry -
172 // --------------------------
173 
174 class GalleryThemeCacheEntry;
175 DBG_NAME(GalleryThemeCacheEntry)
176 class GalleryThemeCacheEntry
177 {
178 private:
179 
180 	const GalleryThemeEntry*        mpThemeEntry;
181 	GalleryTheme*                           mpTheme;
182 
183 public:
184 
185 								GalleryThemeCacheEntry( const GalleryThemeEntry* pThemeEntry, GalleryTheme* pTheme ) :
186 									mpThemeEntry( pThemeEntry ), mpTheme( pTheme ) {DBG_CTOR(GalleryThemeCacheEntry,NULL);}
187 								~GalleryThemeCacheEntry() { delete mpTheme;DBG_DTOR(GalleryThemeCacheEntry,NULL); }
188 
189 	const GalleryThemeEntry*        GetThemeEntry() const { return mpThemeEntry; }
190 	GalleryTheme*                           GetTheme() const { return mpTheme; }
191 };
192 
193 // -----------
194 // - Gallery -
195 // -----------
196 Gallery::Gallery( const String& rMultiPath )
197 :		nReadTextEncoding	( gsl_getSystemTextEncoding() )
198 ,		nLastFileNumber		( 0 )
199 ,		bMultiPath			( sal_False )
200 {
201 	ImplLoad( rMultiPath );
202 }
203 
204 // ------------------------------------------------------------------------
205 
206 Gallery::~Gallery()
207 {
208 	// Themen-Liste loeschen
209 	for( GalleryThemeEntry* pThemeEntry = aThemeList.First(); pThemeEntry; pThemeEntry = aThemeList.Next() )
210 		delete pThemeEntry;
211 
212 	// Import-Liste loeschen
213 	for( GalleryImportThemeEntry* pImportEntry = aImportList.First(); pImportEntry; pImportEntry = aImportList.Next() )
214 		delete pImportEntry;
215 }
216 
217 // ------------------------------------------------------------------------
218 
219 Gallery* Gallery::GetGalleryInstance()
220 {
221 	static Gallery* pGallery = NULL;
222 
223 	if( !pGallery )
224 	{
225 	    ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
226 		if( !pGallery )
227 		{
228 			pGallery = new Gallery( SvtPathOptions().GetGalleryPath() );
229 		}
230 	}
231 
232 	return pGallery;
233 }
234 
235 // ------------------------------------------------------------------------
236 
237 void Gallery::ImplLoad( const String& rMultiPath )
238 {
239 	const sal_uInt16    nTokenCount = rMultiPath.GetTokenCount( ';' );
240     sal_Bool        bIsReadOnlyDir;
241 
242 	bMultiPath = ( nTokenCount > 0 );
243 
244     INetURLObject aCurURL(SvtPathOptions().GetConfigPath());
245 	ImplLoadSubDirs( aCurURL, bIsReadOnlyDir );
246 
247 	if( !bIsReadOnlyDir )
248 	    aUserURL = aCurURL;
249 
250 	if( bMultiPath )
251 	{
252 		aRelURL = INetURLObject( rMultiPath.GetToken( 0, ';' ) );
253 
254 		for( sal_uInt16 i = 0UL; i < nTokenCount; i++ )
255 		{
256 		    aCurURL = INetURLObject(rMultiPath.GetToken( i, ';' ));
257 
258 			ImplLoadSubDirs( aCurURL, bIsReadOnlyDir );
259 
260 			if( !bIsReadOnlyDir )
261 			    aUserURL = aCurURL;
262 	    }
263 	}
264 	else
265 		aRelURL = INetURLObject( rMultiPath );
266 
267 	DBG_ASSERT( aUserURL.GetProtocol() != INET_PROT_NOT_VALID, "no writable Gallery user directory available" );
268 	DBG_ASSERT( aRelURL.GetProtocol() != INET_PROT_NOT_VALID, "invalid URL" );
269 
270 	ImplLoadImports();
271 }
272 
273 // ------------------------------------------------------------------------
274 
275 void Gallery::ImplLoadSubDirs( const INetURLObject& rBaseURL, sal_Bool& rbDirIsReadOnly )
276 {
277     rbDirIsReadOnly = sal_False;
278 
279 	try
280 	{
281 		uno::Reference< ucb::XCommandEnvironment > xEnv;
282 		::ucbhelper::Content					   aCnt( rBaseURL.GetMainURL( INetURLObject::NO_DECODE ), xEnv );
283 
284 		uno::Sequence< OUString > aProps( 1 );
285 		aProps.getArray()[ 0 ] = OUString::createFromAscii( "Url" );
286 
287 		uno::Reference< sdbc::XResultSet > xResultSet( aCnt.createCursor( aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY ) );
288 
289 		try
290 		{
291 		    // check readonlyness the very hard way
292         	INetURLObject   aTestURL( rBaseURL );
293 	        String          aTestFile( RTL_CONSTASCII_USTRINGPARAM( "cdefghij.klm" ) );
294 
295     	    aTestURL.Append( aTestFile );
296 	        SvStream* pTestStm = ::utl::UcbStreamHelper::CreateStream( aTestURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_WRITE );
297 
298 	        if( pTestStm )
299 	        {
300 	            *pTestStm << 1;
301 
302 	            if( pTestStm->GetError() )
303 	                rbDirIsReadOnly = sal_True;
304 
305 	            delete pTestStm;
306 	            KillFile( aTestURL );
307 	        }
308 	        else
309 	            rbDirIsReadOnly = sal_True;
310 	    }
311 	    catch( const ucb::ContentCreationException& )
312 	    {
313 	    }
314 	    catch( const uno::RuntimeException& )
315 	    {
316 	    }
317 	    catch( const uno::Exception& )
318 	    {
319 	    }
320 
321 		if( xResultSet.is() )
322 		{
323 			uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
324 
325 			if( xContentAccess.is() )
326 			{
327                 static const ::rtl::OUString s_sTitle(RTL_CONSTASCII_USTRINGPARAM("Title"));
328                 static const ::rtl::OUString s_sIsReadOnly(RTL_CONSTASCII_USTRINGPARAM("IsReadOnly"));
329                 static const ::rtl::OUString s_sSDG_EXT(RTL_CONSTASCII_USTRINGPARAM("sdg"));
330                 static const ::rtl::OUString s_sSDV_EXT(RTL_CONSTASCII_USTRINGPARAM("sdv"));
331 
332 				while( xResultSet->next() )
333 				{
334 					INetURLObject aThmURL( xContentAccess->queryContentIdentifierString() );
335 
336 					if(aThmURL.GetExtension().equalsIgnoreAsciiCaseAscii("thm"))
337 					{
338 						INetURLObject	aSdgURL( aThmURL); aSdgURL.SetExtension( s_sSDG_EXT );
339 						INetURLObject	aSdvURL( aThmURL ); aSdvURL.SetExtension( s_sSDV_EXT );
340 
341 						OUString		aTitle;
342 						sal_Bool		bReadOnly = sal_False;
343 
344 						try
345 						{
346 						    ::ucbhelper::Content aThmCnt( aThmURL.GetMainURL( INetURLObject::NO_DECODE ), xEnv );
347 						    ::ucbhelper::Content aSdgCnt( aSdgURL.GetMainURL( INetURLObject::NO_DECODE ), xEnv );
348 						    ::ucbhelper::Content aSdvCnt( aSdvURL.GetMainURL( INetURLObject::NO_DECODE ), xEnv );
349 
350 						    try
351 						    {
352 						        aThmCnt.getPropertyValue( s_sTitle ) >>= aTitle;
353 						    }
354 	                        catch( const uno::RuntimeException& )
355 	                        {
356 	                        }
357 	                        catch( const uno::Exception& )
358 	                        {
359 	                        }
360 
361 						    if( aTitle.getLength() )
362 						    {
363 						        try
364 						        {
365 							        aThmCnt.getPropertyValue( s_sIsReadOnly ) >>= bReadOnly;
366 						        }
367 	                            catch( const uno::RuntimeException& )
368 	                            {
369 	                            }
370 	                            catch( const uno::Exception& )
371 	                            {
372 	                            }
373 
374 							    if( !bReadOnly )
375 							    {
376 							        try
377 							        {
378 								        aSdgCnt.getPropertyValue( s_sTitle ) >>= aTitle;
379 						            }
380 	                                catch( const ::com::sun::star::uno::RuntimeException& )
381 	                                {
382 	                                }
383 	                                catch( const ::com::sun::star::uno::Exception& )
384 	                                {
385 	                                }
386 
387 								    if( aTitle.getLength() )
388 								    {
389 								        try
390 								        {
391 	    								    aSdgCnt.getPropertyValue( s_sIsReadOnly ) >>= bReadOnly;
392     					                }
393 	                                    catch( const uno::RuntimeException& )
394 	                                    {
395 	                                    }
396 	                                    catch( const uno::Exception& )
397 	                                    {
398 	                                    }
399 							        }
400 							    }
401 
402 							    if( !bReadOnly )
403 							    {
404 							        try
405 							        {
406 								        aSdvCnt.getPropertyValue( s_sTitle ) >>= aTitle;
407     					            }
408 	                                catch( const ::com::sun::star::uno::RuntimeException& )
409 	                                {
410 	                                }
411 	                                catch( const ::com::sun::star::uno::Exception& )
412 	                                {
413 	                                }
414 
415 								    if( aTitle.getLength() )
416 								    {
417 								        try
418 								        {
419     									    aSdvCnt.getPropertyValue( s_sIsReadOnly ) >>= bReadOnly;
420     					                }
421 	                                    catch( const uno::RuntimeException& )
422 	                                    {
423 	                                    }
424 	                                    catch( const uno::Exception& )
425 	                                    {
426 	                                    }
427 								    }
428 							    }
429 
430 							    GalleryThemeEntry* pEntry = GalleryTheme::CreateThemeEntry( aThmURL, rbDirIsReadOnly || bReadOnly );
431 
432 							    if( pEntry )
433 							    {
434 								    const sal_uIntPtr nFileNumber = (sal_uIntPtr) String(aThmURL.GetBase()).Erase( 0, 2 ).Erase( 6 ).ToInt32();
435 
436 								    aThemeList.Insert( pEntry, LIST_APPEND );
437 
438 								    if( nFileNumber > nLastFileNumber )
439 									    nLastFileNumber = nFileNumber;
440 							    }
441 						    }
442 						}
443 	                    catch( const ucb::ContentCreationException& )
444 	                    {
445 	                    }
446 	                    catch( const uno::RuntimeException& )
447 	                    {
448 	                    }
449 	                    catch( const uno::Exception& )
450 	                    {
451 	                    }
452 					}
453 				}
454 			}
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 void Gallery::ImplLoadImports()
471 {
472 	INetURLObject aURL( GetUserURL() );
473 
474 	aURL.Append( String( RTL_CONSTASCII_USTRINGPARAM( "gallery.sdi" ) ) );
475 
476 	if( FileExists( aURL ) )
477 	{
478 		SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( aURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ );
479 
480 		if( pIStm )
481 		{
482 			GalleryThemeEntry*			pThemeEntry;
483 			GalleryImportThemeEntry*	pImportEntry;
484 			INetURLObject               aFile;
485 			sal_uInt32                      nInventor;
486 			sal_uInt32                      nCount;
487 			sal_uInt16                      nId;
488 			sal_uInt16                      i;
489 			sal_uInt16                      nTempCharSet;
490 
491 			for( pImportEntry = aImportList.First(); pImportEntry; pImportEntry = aImportList.Next() )
492 				delete pImportEntry;
493 
494 			aImportList.Clear();
495 			*pIStm >> nInventor;
496 
497 			if( nInventor == COMPAT_FORMAT( 'S', 'G', 'A', '3' ) )
498 			{
499 				*pIStm >> nId >> nCount >> nTempCharSet;
500 
501 				for( i = 0; i < nCount; i++ )
502 				{
503 					pImportEntry = new GalleryImportThemeEntry;
504 
505 					*pIStm >> *pImportEntry;
506 					aImportList.Insert( pImportEntry, LIST_APPEND );
507 					aFile = INetURLObject( pImportEntry->aURL );
508 					pThemeEntry = new GalleryThemeEntry( aFile,
509 														 pImportEntry->aUIName,
510 														 String(aFile.GetBase()).Erase( 0, 2 ).Erase( 6 ).ToInt32(),
511 														 sal_True, sal_True, sal_False, 0, sal_False );
512 
513 					aThemeList.Insert( pThemeEntry, LIST_APPEND );
514 				}
515 			}
516 
517 			delete pIStm;
518 		}
519 	}
520 }
521 
522 // ------------------------------------------------------------------------
523 
524 void Gallery::ImplWriteImportList()
525 {
526 	INetURLObject aURL( GetUserURL() );
527 	aURL.Append( ( String( "gallery.sdi", RTL_TEXTENCODING_UTF8 ) ) );
528 	SvStream* pOStm = ::utl::UcbStreamHelper::CreateStream( aURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_WRITE | STREAM_TRUNC );
529 
530 	if( pOStm )
531 	{
532 		const sal_uInt32 nInventor = (sal_uInt32) COMPAT_FORMAT( 'S', 'G', 'A', '3' );
533 		const sal_uInt16 nId = 0x0004;
534 
535 		*pOStm << nInventor << nId << (sal_uInt32) aImportList.Count() << (sal_uInt16) gsl_getSystemTextEncoding();
536 
537 		for( GalleryImportThemeEntry* pImportEntry = aImportList.First(); pImportEntry; pImportEntry = aImportList.Next() )
538 			*pOStm << *pImportEntry;
539 
540 		if( pOStm->GetError() )
541 			ErrorHandler::HandleError( ERRCODE_IO_GENERAL );
542 
543 		delete pOStm;
544 	}
545 }
546 
547 // ------------------------------------------------------------------------
548 
549 GalleryThemeEntry* Gallery::ImplGetThemeEntry( const String& rThemeName )
550 {
551 	GalleryThemeEntry* pFound = NULL;
552 
553 	if( rThemeName.Len() )
554 		for( GalleryThemeEntry* pEntry = aThemeList.First(); pEntry && !pFound; pEntry = aThemeList.Next() )
555 			if( rThemeName == pEntry->GetThemeName() )
556 				pFound = pEntry;
557 
558 	return pFound;
559 }
560 
561 // ------------------------------------------------------------------------
562 
563 GalleryImportThemeEntry* Gallery::ImplGetImportThemeEntry( const String& rImportName )
564 {
565 	GalleryImportThemeEntry* pFound = NULL;
566 
567 	for( GalleryImportThemeEntry* pImportEntry = aImportList.First(); pImportEntry && !pFound; pImportEntry = aImportList.Next() )
568 		if ( rImportName == pImportEntry->aUIName )
569 			pFound = pImportEntry;
570 
571 	return pFound;
572 }
573 
574 // ------------------------------------------------------------------------
575 
576 String Gallery::GetThemeName( sal_uIntPtr nThemeId ) const
577 {
578 	GalleryThemeEntry* pFound = NULL;
579 
580 	for( sal_uIntPtr n = 0, nCount = aThemeList.Count(); n < nCount; n++ )
581 	{
582 		GalleryThemeEntry* pEntry = aThemeList.GetObject( n );
583 
584 		if( nThemeId == pEntry->GetId() )
585 			pFound = pEntry;
586 	}
587 
588 	// try fallback, if no entry was found
589 	if( !pFound )
590 	{
591 		ByteString      aFallback;
592 
593 		switch( nThemeId )
594 		{
595 			case( GALLERY_THEME_3D ): aFallback = "3D"; break;
596 			case( GALLERY_THEME_BULLETS ): aFallback = "Bullets"; break;
597 			case( GALLERY_THEME_HOMEPAGE ): aFallback = "Homepage"; break;
598 			case( GALLERY_THEME_POWERPOINT ): aFallback = "private://gallery/hidden/imgppt"; break;
599 			case( GALLERY_THEME_FONTWORK ): aFallback = "private://gallery/hidden/fontwork"; break;
600 			case( GALLERY_THEME_FONTWORK_VERTICAL ): aFallback = "private://gallery/hidden/fontworkvertical"; break;
601 			case( GALLERY_THEME_RULERS ): aFallback = "Rulers"; break;
602 			case( GALLERY_THEME_SOUNDS ): aFallback = "Sounds"; break;
603 
604 			default:
605 			break;
606 		}
607 
608 		pFound = ( (Gallery*) this )->ImplGetThemeEntry( String::CreateFromAscii( aFallback.GetBuffer() ) );
609 	}
610 
611 	return( pFound ? pFound->GetThemeName() : String() );
612 }
613 
614 // ------------------------------------------------------------------------
615 
616 sal_Bool Gallery::HasTheme( const String& rThemeName )
617 {
618 	return( ImplGetThemeEntry( rThemeName ) != NULL );
619 }
620 
621 // ------------------------------------------------------------------------
622 
623 sal_Bool Gallery::CreateTheme( const String& rThemeName, sal_uInt32 nNumFrom )
624 {
625 	sal_Bool bRet = sal_False;
626 
627 	if( !HasTheme( rThemeName ) && ( GetUserURL().GetProtocol() != INET_PROT_NOT_VALID ) )
628 	{
629 		nLastFileNumber = nNumFrom > nLastFileNumber ? nNumFrom : nLastFileNumber + 1;
630 		GalleryThemeEntry* pNewEntry = new GalleryThemeEntry( GetUserURL(), rThemeName,
631 															  nLastFileNumber,
632 															  sal_False, sal_False, sal_True, 0, sal_False );
633 
634 		aThemeList.Insert( pNewEntry, LIST_APPEND );
635 		delete( new GalleryTheme( this, pNewEntry ) );
636 		Broadcast( GalleryHint( GALLERY_HINT_THEME_CREATED, rThemeName ) );
637 		bRet = sal_True;
638 	}
639 
640 	return bRet;
641 }
642 
643 // ------------------------------------------------------------------------
644 
645 sal_Bool Gallery::CreateImportTheme( const INetURLObject& rURL, const String& rImportName )
646 {
647 	INetURLObject	aURL( rURL );
648 	sal_Bool			bRet = sal_False;
649 
650 	DBG_ASSERT( aURL.GetProtocol() != INET_PROT_NOT_VALID, "invalid URL" );
651 
652 	if( FileExists( aURL ) )
653 	{
654 		SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( aURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ );
655 
656 		if( pIStm )
657 		{
658 			sal_uIntPtr   nStmErr;
659 			sal_uInt16  nId;
660 
661 			*pIStm >> nId;
662 
663 			if( nId > 0x0004 )
664 				ErrorHandler::HandleError( ERRCODE_IO_GENERAL );
665 			else
666 			{
667 				ByteString				aTmpStr;
668 				String                  aThemeName; *pIStm >> aTmpStr; aThemeName = String( aTmpStr, RTL_TEXTENCODING_UTF8 );
669 				GalleryThemeEntry*      pThemeEntry = new GalleryThemeEntry( aURL, rImportName,
670 																			 String(aURL.GetBase()).Erase( 0, 2 ).Erase( 6 ).ToInt32(),
671 																			 sal_True, sal_True, sal_True, 0, sal_False );
672 				GalleryTheme*           pImportTheme = new GalleryTheme( this, pThemeEntry );
673 
674 				pIStm->Seek( STREAM_SEEK_TO_BEGIN );
675 				*pIStm >> *pImportTheme;
676 				nStmErr = pIStm->GetError();
677 
678 				if( nStmErr )
679 				{
680 					delete pThemeEntry;
681 					ErrorHandler::HandleError( ERRCODE_IO_GENERAL );
682 				}
683 				else
684 				{
685 					String  aName( rImportName );
686 					String  aNewName( aName );
687 					sal_uIntPtr   nCount = 0;
688 
689 					aName += ' ';
690 
691 					while ( HasTheme( aNewName ) && ( nCount++ < 16000 ) )
692 					{
693 						aNewName = aName;
694 						aNewName += String::CreateFromInt32( nCount );
695 					}
696 
697 					pImportTheme->SetImportName( aNewName );
698 					aThemeList.Insert( pThemeEntry, LIST_APPEND );
699 
700 					// Thema in Import-Liste eintragen und Import-Liste     speichern
701 					GalleryImportThemeEntry* pImportEntry = new GalleryImportThemeEntry;
702 					pImportEntry->aThemeName = pImportEntry->aUIName = aNewName;
703 					pImportEntry->aURL = rURL;
704 					pImportEntry->aImportName = rImportName;
705 					aImportList.Insert( pImportEntry, LIST_APPEND );
706 					ImplWriteImportList();
707 					bRet = sal_True;
708 				}
709 
710 				delete pImportTheme;
711 			}
712 
713 			delete pIStm;
714 		}
715 	}
716 
717 	return bRet;
718 }
719 
720 // ------------------------------------------------------------------------
721 
722 sal_Bool Gallery::RenameTheme( const String& rOldName, const String& rNewName )
723 {
724 	GalleryThemeEntry*      pThemeEntry = ImplGetThemeEntry( rOldName );
725 	sal_Bool                            bRet = sal_False;
726 
727 	// Ueberpruefen, ob neuer Themenname schon vorhanden ist
728 	if( pThemeEntry && !HasTheme( rNewName ) && ( !pThemeEntry->IsReadOnly() || pThemeEntry->IsImported() ) )
729 	{
730 		SfxListener   aListener;
731 		GalleryTheme* pThm = AcquireTheme( rOldName, aListener );
732 
733 		if( pThm )
734 		{
735 			const String aOldName( rOldName );
736 
737 			pThemeEntry->SetName( rNewName );
738 			pThm->ImplWrite();
739 
740 			if( pThemeEntry->IsImported() )
741 			{
742 				pThm->SetImportName( rNewName );
743 
744 				GalleryImportThemeEntry* pImportEntry = ImplGetImportThemeEntry( rOldName );
745 
746 				if( pImportEntry )
747 				{
748 					pImportEntry->aUIName = rNewName;
749 					ImplWriteImportList();
750 				}
751 			}
752 
753 			Broadcast( GalleryHint( GALLERY_HINT_THEME_RENAMED, aOldName, pThm->GetName() ) );
754 			ReleaseTheme( pThm, aListener );
755 			bRet = sal_True;
756 		}
757 	}
758 
759 	return bRet;
760 }
761 
762 // ------------------------------------------------------------------------
763 
764 sal_Bool Gallery::RemoveTheme( const String& rThemeName )
765 {
766 	GalleryThemeEntry*	pThemeEntry = ImplGetThemeEntry( rThemeName );
767 	sal_Bool                bRet = sal_False;
768 
769 	if( pThemeEntry && ( !pThemeEntry->IsReadOnly() || pThemeEntry->IsImported() ) )
770 	{
771 		Broadcast( GalleryHint( GALLERY_HINT_CLOSE_THEME, rThemeName ) );
772 
773 		if( pThemeEntry->IsImported() )
774 		{
775 			GalleryImportThemeEntry* pImportEntry = ImplGetImportThemeEntry( rThemeName );
776 
777 			if( pImportEntry )
778 			{
779 				delete aImportList.Remove( pImportEntry );
780 				ImplWriteImportList();
781 			}
782 		}
783 		else
784 		{
785 			SfxListener		aListener;
786 			GalleryTheme*	pThm = AcquireTheme( rThemeName, aListener );
787 
788             if( pThm )
789             {
790                 INetURLObject	aThmURL( pThm->GetThmURL() );
791                 INetURLObject	aSdgURL( pThm->GetSdgURL() );
792                 INetURLObject	aSdvURL( pThm->GetSdvURL() );
793 
794                 ReleaseTheme( pThm, aListener );
795 
796                 KillFile( aThmURL );
797                 KillFile( aSdgURL );
798                 KillFile( aSdvURL );
799             }
800 		}
801 
802 		delete aThemeList.Remove( pThemeEntry );
803 		Broadcast( GalleryHint( GALLERY_HINT_THEME_REMOVED, rThemeName ) );
804 
805 		bRet = sal_True;
806 	}
807 
808 	return bRet;
809 }
810 
811 // ------------------------------------------------------------------------
812 
813 INetURLObject Gallery::GetImportURL( const String& rThemeName )
814 {
815 	INetURLObject				aURL;
816 	GalleryImportThemeEntry*	pImportEntry = ImplGetImportThemeEntry( rThemeName );
817 
818 	if( pImportEntry )
819 	{
820 		aURL = pImportEntry->aURL;
821 		DBG_ASSERT( aURL.GetProtocol() != INET_PROT_NOT_VALID, "invalid URL" );
822 	}
823 
824 	return aURL;
825 }
826 
827 // ------------------------------------------------------------------------
828 
829 GalleryTheme* Gallery::ImplGetCachedTheme( const GalleryThemeEntry* pThemeEntry )
830 {
831 	GalleryTheme* pTheme = NULL;
832 
833 	if( pThemeEntry )
834 	{
835 		GalleryThemeCacheEntry* pEntry;
836 
837 		for( pEntry = (GalleryThemeCacheEntry*) aThemeCache.First(); pEntry && !pTheme; pEntry = (GalleryThemeCacheEntry*) aThemeCache.Next() )
838 			if( pThemeEntry == pEntry->GetThemeEntry() )
839 				pTheme = pEntry->GetTheme();
840 
841 		if( !pTheme )
842 		{
843 			INetURLObject aURL;
844 
845 			if( !pThemeEntry->IsImported() )
846 				aURL = pThemeEntry->GetThmURL();
847 			else
848 				aURL = GetImportURL( pThemeEntry->GetThemeName() );
849 
850 			DBG_ASSERT( aURL.GetProtocol() != INET_PROT_NOT_VALID, "invalid URL" );
851 
852 			if( FileExists( aURL ) )
853 			{
854 				SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( aURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ );
855 
856 				if( pIStm )
857 				{
858 					pTheme = new GalleryTheme( this, (GalleryThemeEntry*) pThemeEntry );
859 					*pIStm >> *pTheme;
860 
861 					if( pIStm->GetError() )
862 						delete pTheme, pTheme = NULL;
863 					else if( pThemeEntry->IsImported() )
864 						pTheme->SetImportName( pThemeEntry->GetThemeName() );
865 
866 					delete pIStm;
867 				}
868 			}
869 
870 			if( pTheme )
871 				aThemeCache.Insert( new GalleryThemeCacheEntry( pThemeEntry, pTheme ), LIST_APPEND );
872 		}
873 	}
874 
875 	return pTheme;
876 }
877 
878 // ------------------------------------------------------------------------
879 
880 void Gallery::ImplDeleteCachedTheme( GalleryTheme* pTheme )
881 {
882 	GalleryThemeCacheEntry* pEntry;
883 	sal_Bool                                    bDone = sal_False;
884 
885 	for( pEntry = (GalleryThemeCacheEntry*) aThemeCache.First(); pEntry && !bDone; pEntry = (GalleryThemeCacheEntry*) aThemeCache.Next() )
886 	{
887 		if( pTheme == pEntry->GetTheme() )
888 		{
889 			delete (GalleryThemeCacheEntry*) aThemeCache.Remove( pEntry );
890 			bDone = sal_True;
891 		}
892 	}
893 }
894 
895 // ------------------------------------------------------------------------
896 
897 GalleryTheme* Gallery::AcquireTheme( const String& rThemeName, SfxListener& rListener )
898 {
899 	GalleryTheme*           pTheme = NULL;
900 	GalleryThemeEntry*      pThemeEntry = ImplGetThemeEntry( rThemeName );
901 
902 	if( pThemeEntry && ( ( pTheme = ImplGetCachedTheme( pThemeEntry ) ) != NULL ) )
903 		rListener.StartListening( *pTheme );
904 
905 	return pTheme;
906 }
907 
908 // ------------------------------------------------------------------------
909 
910 void Gallery::ReleaseTheme( GalleryTheme* pTheme, SfxListener& rListener )
911 {
912 	if( pTheme )
913 	{
914 		rListener.EndListening( *pTheme );
915 
916 		if( !pTheme->HasListeners() )
917 			ImplDeleteCachedTheme( pTheme );
918 	}
919 }
920 
921 sal_Bool GalleryThemeEntry::IsDefault() const
922 { return( ( nId > 0 ) && ( nId != ( RID_GALLERYSTR_THEME_MYTHEME - RID_GALLERYSTR_THEME_START ) ) ); }
923 
924