xref: /aoo42x/main/svx/source/gallery2/galtheme.cxx (revision 21b36550)
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/urlobj.hxx>
30 #include <tools/vcompat.hxx>
31 #include <unotools/streamwrap.hxx>
32 #include <unotools/ucbstreamhelper.hxx>
33 #include <unotools/tempfile.hxx>
34 #include <unotools/localfilehelper.hxx>
35 #include <ucbhelper/content.hxx>
36 #include <sot/storage.hxx>
37 #include <sot/formats.hxx>
38 #include <sot/filelist.hxx>
39 #include <vcl/virdev.hxx>
40 #include <vcl/cvtgrf.hxx>
41 #include <svl/itempool.hxx>
42 #include <sfx2/docfile.hxx>
43 #include <avmedia/mediawindow.hxx>
44 #include <svx/svdograf.hxx>
45 #include <svx/fmpage.hxx>
46 #include "codec.hxx"
47 #include <svx/unomodel.hxx>
48 #include <svx/fmmodel.hxx>
49 #include <svx/fmview.hxx>
50 #include "svx/galmisc.hxx"
51 #include "svx/galtheme.hxx"
52 #include <com/sun/star/sdbc/XResultSet.hpp>
53 #include <com/sun/star/ucb/XContentAccess.hpp>
54 #include <com/sun/star/io/XInputStream.hpp>
55 #include "galobj.hxx"
56 #include <svx/gallery1.hxx>
57 #include "galtheme.hrc"
58 #include <vcl/lstbox.hxx>
59 #include "gallerydrawmodel.hxx"
60 
61 // --------------
62 // - Namespaces -
63 // --------------
64 
65 using namespace ::rtl;
66 using namespace ::com::sun::star;
67 
68 // ------------
69 // - SgaTheme -
70 // ------------
71 DBG_NAME(GalleryTheme)
72 
73 GalleryTheme::GalleryTheme( Gallery* pGallery, GalleryThemeEntry* pThemeEntry ) :
74 		pParent               ( pGallery ),
75 		pThm		          ( pThemeEntry ),
76         mnThemeLockCount      ( 0 ),
77 		mnBroadcasterLockCount( 0 ),
78 		nDragPos	          ( 0 ),
79 		bDragging	          ( sal_False )
80 {
81     DBG_CTOR(GalleryTheme,NULL);
82 
83 	ImplCreateSvDrawStorage();
84 
85 	if( pThm->IsImported() )
86 		aImportName = pThm->GetThemeName();
87 }
88 
89 // ------------------------------------------------------------------------
90 
91 GalleryTheme::~GalleryTheme()
92 {
93 	ImplWrite();
94 
95 	for( GalleryObject* pEntry = aObjectList.First(); pEntry; pEntry = aObjectList.Next() )
96 	{
97 		Broadcast( GalleryHint( GALLERY_HINT_CLOSE_OBJECT, GetName(), reinterpret_cast< sal_uIntPtr >( pEntry ) ) );
98 		delete pEntry;
99 		Broadcast( GalleryHint( GALLERY_HINT_OBJECT_REMOVED, GetName(), reinterpret_cast< sal_uIntPtr >( pEntry ) ) );
100 	}
101 
102     DBG_DTOR(GalleryTheme,NULL);
103 }
104 
105 // ------------------------------------------------------------------------
106 
107 void GalleryTheme::ImplCreateSvDrawStorage()
108 {
109 	if( !pThm->IsImported() )
110 	{
111 		aSvDrawStorageRef = new SvStorage( sal_False, GetSdvURL().GetMainURL( INetURLObject::NO_DECODE ), pThm->IsReadOnly() ? STREAM_READ : STREAM_STD_READWRITE );
112 		// #i50423# ReadOnly may not been set though the file can't be written (because of security reasons)
113 		if ( ( aSvDrawStorageRef->GetError() != ERRCODE_NONE ) && !pThm->IsReadOnly() )
114 			aSvDrawStorageRef = new SvStorage( sal_False, GetSdvURL().GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ );
115 	}
116 	else
117 		aSvDrawStorageRef.Clear();
118 }
119 
120 // ------------------------------------------------------------------------
121 
122 sal_Bool GalleryTheme::ImplWriteSgaObject( const SgaObject& rObj, sal_uIntPtr nPos, GalleryObject* pExistentEntry )
123 {
124 	SvStream*	pOStm = ::utl::UcbStreamHelper::CreateStream( GetSdgURL().GetMainURL( INetURLObject::NO_DECODE ), STREAM_WRITE );
125 	sal_Bool		bRet = sal_False;
126 
127 	if( pOStm )
128 	{
129 		const sal_uInt32 nOffset = pOStm->Seek( STREAM_SEEK_TO_END );
130 
131 		rObj.WriteData( *pOStm, m_aDestDir );
132 
133 		if( !pOStm->GetError() )
134 		{
135 			GalleryObject* pEntry;
136 
137 			if( !pExistentEntry )
138 			{
139 				pEntry = new GalleryObject;
140 				aObjectList.Insert( pEntry, nPos );
141 			}
142 			else
143 				pEntry = pExistentEntry;
144 
145 			pEntry->aURL = rObj.GetURL();
146 			pEntry->nOffset = nOffset;
147 			pEntry->eObjKind = rObj.GetObjKind();
148 			bRet = sal_True;
149 		}
150 
151 		delete pOStm;
152 	}
153 
154 	return bRet;
155 }
156 
157 // ------------------------------------------------------------------------
158 
159 SgaObject* GalleryTheme::ImplReadSgaObject( GalleryObject* pEntry )
160 {
161 	SgaObject* pSgaObj = NULL;
162 
163 	if( pEntry )
164 	{
165 		SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( GetSdgURL().GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ );
166 
167 		if( pIStm )
168 		{
169 			sal_uInt32 nInventor;
170 
171 			// Ueberpruefen, ob das File ein gueltiges SGA-File ist
172 			pIStm->Seek( pEntry->nOffset );
173 			*pIStm >> nInventor;
174 
175 			if( nInventor == COMPAT_FORMAT( 'S', 'G', 'A', '3' ) )
176 			{
177 				pIStm->Seek( pEntry->nOffset );
178 
179 				switch( pEntry->eObjKind )
180 				{
181 					case( SGA_OBJ_BMP ):	pSgaObj = new SgaObjectBmp(); break;
182 					case( SGA_OBJ_ANIM ):	pSgaObj = new SgaObjectAnim(); break;
183 					case( SGA_OBJ_INET ):	pSgaObj = new SgaObjectINet(); break;
184 					case( SGA_OBJ_SVDRAW ):	pSgaObj = new SgaObjectSvDraw(); break;
185 					case( SGA_OBJ_SOUND ):	pSgaObj = new SgaObjectSound(); break;
186 
187 					default:
188 					break;
189 				}
190 
191 				if( pSgaObj )
192 				{
193 					*pIStm >> *pSgaObj;
194 					pSgaObj->ImplUpdateURL( pEntry->aURL );
195 				}
196 			}
197 
198 			delete pIStm;
199 		}
200 	}
201 
202 	return pSgaObj;
203 }
204 
205 // ------------------------------------------------------------------------
206 
207 void GalleryTheme::ImplRead()
208 {
209 	SvStream* pIStm	= ::utl::UcbStreamHelper::CreateStream( GetThmURL().GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ );
210 
211 	if( pIStm )
212 	{
213 		*pIStm >> *this;
214 		delete pIStm;
215 	}
216 }
217 
218 // ------------------------------------------------------------------------
219 
220 void GalleryTheme::ImplWrite()
221 {
222 	if( IsModified() )
223 	{
224 		INetURLObject aPathURL( GetThmURL() );
225 
226 		aPathURL.removeSegment();
227 		aPathURL.removeFinalSlash();
228 
229 		DBG_ASSERT( aPathURL.GetProtocol() != INET_PROT_NOT_VALID, "invalid URL" );
230 
231 		if( FileExists( aPathURL ) || CreateDir( aPathURL ) )
232 		{
233 #ifdef UNX
234 			SvStream* pOStm	= ::utl::UcbStreamHelper::CreateStream( GetThmURL().GetMainURL( INetURLObject::NO_DECODE ), STREAM_WRITE | STREAM_COPY_ON_SYMLINK | STREAM_TRUNC );
235 #else
236 			SvStream* pOStm	= ::utl::UcbStreamHelper::CreateStream( GetThmURL().GetMainURL( INetURLObject::NO_DECODE ), STREAM_WRITE | STREAM_TRUNC );
237 #endif
238 
239 			if( pOStm )
240 			{
241 				*pOStm << *this;
242 				delete pOStm;
243 			}
244 
245 			ImplSetModified( sal_False );
246 		}
247 	}
248 }
249 
250 // ------------------------------------------------------------------------
251 
252 const GalleryObject* GalleryTheme::ImplGetGalleryObject( const INetURLObject& rURL )
253 {
254 	GalleryObject*	pEntry = aObjectList.First();
255 	GalleryObject*	pFoundEntry = NULL;
256 
257 	for( ; pEntry && !pFoundEntry; pEntry = aObjectList.Next() )
258 		if( pEntry->aURL == rURL )
259 			pFoundEntry = pEntry;
260 
261 	return pFoundEntry;
262 }
263 
264 // ------------------------------------------------------------------------
265 
266 INetURLObject GalleryTheme::ImplGetURL( const GalleryObject* pObject ) const
267 {
268 	INetURLObject aURL;
269 
270 	if( pObject )
271 	{
272 		if( IsImported() )
273 		{
274 			INetURLObject aPathURL( GetParent()->GetImportURL( GetName() ) );
275 
276 			aPathURL.removeSegment();
277 			aPathURL.removeFinalSlash();
278 			aPathURL.Append( pObject->aURL.GetName() );
279 			aURL = aPathURL;
280 		}
281 		else
282 			aURL = pObject->aURL;
283 	}
284 
285 	return aURL;
286 }
287 
288 // ------------------------------------------------------------------------
289 
290 INetURLObject GalleryTheme::ImplCreateUniqueURL( SgaObjKind eObjKind, sal_uIntPtr nFormat )
291 {
292     INetURLObject   aDir( GetParent()->GetUserURL() );
293     INetURLObject   aInfoFileURL( GetParent()->GetUserURL() );
294     INetURLObject   aNewURL;
295 	sal_uInt32		nNextNumber = 1999;
296     sal_Char const* pExt = NULL;
297     sal_Bool            bExists;
298 
299     aDir.Append( String( RTL_CONSTASCII_USTRINGPARAM( "dragdrop" ) ) );
300     CreateDir( aDir );
301 
302 	aInfoFileURL.Append( String( RTL_CONSTASCII_USTRINGPARAM( "sdddndx1" ) ) );
303 
304 	// read next possible number
305     if( FileExists( aInfoFileURL ) )
306 	{
307 		SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( aInfoFileURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ );
308 
309 		if( pIStm )
310 		{
311 			*pIStm >> nNextNumber;
312 			delete pIStm;
313 		}
314 	}
315 
316     // create extension
317     if( nFormat )
318     {
319         switch( nFormat )
320         {
321             case( CVT_BMP ): pExt = ".bmp"; break;
322             case( CVT_GIF ): pExt = ".gif"; break;
323             case( CVT_JPG ): pExt = ".jpg"; break;
324             case( CVT_MET ): pExt = ".met"; break;
325             case( CVT_PCT ): pExt = ".pct"; break;
326             case( CVT_PNG ): pExt = ".png"; break;
327             case( CVT_SVM ): pExt = ".svm"; break;
328             case( CVT_TIF ): pExt = ".tif"; break;
329             case( CVT_WMF ): pExt = ".wmf"; break;
330             case( CVT_EMF ): pExt = ".emf"; break;
331 
332             default:
333                 pExt = ".grf";
334             break;
335         }
336     }
337 
338     do
339     {
340         // get URL
341 	    if( SGA_OBJ_SVDRAW == eObjKind )
342 	    {
343             String aFileName( RTL_CONSTASCII_USTRINGPARAM( "gallery/svdraw/dd" ) );
344 		    aNewURL = INetURLObject( aFileName += String::CreateFromInt32( ++nNextNumber % 99999999 ), INET_PROT_PRIV_SOFFICE );
345 
346             bExists = sal_False;
347 
348 		    for( GalleryObject* pEntry = aObjectList.First(); pEntry && !bExists; pEntry = aObjectList.Next() )
349 			    if( pEntry->aURL == aNewURL )
350                     bExists = sal_True;
351 	    }
352 	    else
353 	    {
354             String aFileName( RTL_CONSTASCII_USTRINGPARAM( "dd" ) );
355 
356             aFileName += String::CreateFromInt32( ++nNextNumber % 999999 );
357             aFileName += String( pExt, RTL_TEXTENCODING_ASCII_US );
358 
359             aNewURL = aDir;
360 		    aNewURL.Append( aFileName );
361 
362             bExists = FileExists( aNewURL );
363 	    }
364     }
365     while( bExists );
366 
367 	// write updated number
368     SvStream* pOStm = ::utl::UcbStreamHelper::CreateStream( aInfoFileURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_WRITE );
369 
370 	if( pOStm )
371 	{
372 		*pOStm << nNextNumber;
373 		delete pOStm;
374 	}
375 
376 	return aNewURL;
377 }
378 
379 // ------------------------------------------------------------------------
380 
381 void GalleryTheme::ImplBroadcast( sal_uIntPtr nUpdatePos )
382 {
383 	if( !IsBroadcasterLocked() )
384 	{
385 		if( GetObjectCount() && ( nUpdatePos >= GetObjectCount() ) )
386 			nUpdatePos = GetObjectCount() - 1;
387 
388 		Broadcast( GalleryHint( GALLERY_HINT_THEME_UPDATEVIEW, GetName(), nUpdatePos ) );
389 	}
390 }
391 
392 // ------------------------------------------------------------------------
393 
394 sal_Bool GalleryTheme::UnlockTheme()
395 {
396     DBG_ASSERT( mnThemeLockCount, "Theme is not locked" );
397 
398     sal_Bool bRet = sal_False;
399 
400     if( mnThemeLockCount )
401     {
402         --mnThemeLockCount;
403         bRet = sal_True;
404     }
405 
406     return bRet;
407 }
408 
409 // ------------------------------------------------------------------------
410 
411 void GalleryTheme::UnlockBroadcaster( sal_uIntPtr nUpdatePos )
412 {
413 	DBG_ASSERT( mnBroadcasterLockCount, "Broadcaster is not locked" );
414 
415 	if( mnBroadcasterLockCount && !--mnBroadcasterLockCount )
416 		ImplBroadcast( nUpdatePos );
417 }
418 
419 // ------------------------------------------------------------------------
420 
421 sal_Bool GalleryTheme::InsertObject( const SgaObject& rObj, sal_uIntPtr nInsertPos )
422 {
423 	sal_Bool bRet = sal_False;
424 
425 	if( rObj.IsValid() )
426 	{
427 		GalleryObject*	pEntry = aObjectList.First();
428 		GalleryObject*	pFoundEntry = NULL;
429 
430 		for( ; pEntry && !pFoundEntry; pEntry = aObjectList.Next() )
431 			if( pEntry->aURL == rObj.GetURL() )
432 				pFoundEntry = pEntry;
433 
434 		if( pFoundEntry )
435         {
436     		GalleryObject aNewEntry;
437 
438             // update title of new object if neccessary
439             if( !rObj.GetTitle().Len() )
440             {
441                 SgaObject* pOldObj = ImplReadSgaObject( pFoundEntry );
442 
443                 if( pOldObj )
444                 {
445                     ( (SgaObject&) rObj ).SetTitle( pOldObj->GetTitle() );
446                     delete pOldObj;
447                 }
448             }
449             else if( rObj.GetTitle() == String( RTL_CONSTASCII_USTRINGPARAM( "__<empty>__" ) ) )
450                 ( (SgaObject&) rObj ).SetTitle( String() );
451 
452             ImplWriteSgaObject( rObj, nInsertPos, &aNewEntry );
453 			pFoundEntry->nOffset = aNewEntry.nOffset;
454         }
455 		else
456 			ImplWriteSgaObject( rObj, nInsertPos, NULL );
457 
458 		ImplSetModified( bRet = sal_True );
459 		ImplBroadcast( pFoundEntry ? aObjectList.GetPos( pFoundEntry ) : nInsertPos );
460 	}
461 
462 	return bRet;
463 }
464 
465 // ------------------------------------------------------------------------
466 
467 SgaObject* GalleryTheme::AcquireObject( sal_uIntPtr nPos )
468 {
469 	return ImplReadSgaObject( aObjectList.GetObject( nPos ) );
470 }
471 
472 // ------------------------------------------------------------------------
473 
474 void GalleryTheme::GetPreviewBitmapExAndStrings(sal_uIntPtr nPos, BitmapEx& rBitmapEx, Size& rSize, String& rTitle, String& rPath) const
475 {
476     const GalleryObject* pGalleryObject = aObjectList.GetObject(nPos);
477 
478     if(pGalleryObject)
479     {
480         rBitmapEx = pGalleryObject->maPreviewBitmapEx;
481         rSize = pGalleryObject->maPreparedSize;
482         rTitle = pGalleryObject->maTitle;
483         rPath = pGalleryObject->maPath;
484     }
485     else
486     {
487         OSL_ENSURE(false, "OOps, no GalleryObject at this index (!)");
488     }
489 }
490 
491 // ------------------------------------------------------------------------
492 
493 void GalleryTheme::SetPreviewBitmapExAndStrings(sal_uIntPtr nPos, const BitmapEx& rBitmapEx, const Size& rSize, const String& rTitle, const String& rPath)
494 {
495     GalleryObject* pGalleryObject = aObjectList.GetObject(nPos);
496 
497     if(pGalleryObject)
498     {
499         pGalleryObject->maPreviewBitmapEx = rBitmapEx;
500         pGalleryObject->maPreparedSize = rSize;
501         pGalleryObject->maTitle = rTitle;
502         pGalleryObject->maPath = rPath;
503     }
504     else
505     {
506         OSL_ENSURE(false, "OOps, no GalleryObject at this index (!)");
507     }
508 }
509 
510 // ------------------------------------------------------------------------
511 
512 void GalleryTheme::ReleaseObject( SgaObject* pObject )
513 {
514 	delete pObject;
515 }
516 
517 // ------------------------------------------------------------------------
518 
519 sal_Bool GalleryTheme::RemoveObject( sal_uIntPtr nPos )
520 {
521 	GalleryObject* pEntry = aObjectList.Remove( nPos );
522 
523 	if( !aObjectList.Count() )
524 		KillFile( GetSdgURL() );
525 
526 	if( pEntry )
527 	{
528 		if( SGA_OBJ_SVDRAW == pEntry->eObjKind )
529 			aSvDrawStorageRef->Remove( pEntry->aURL.GetMainURL( INetURLObject::NO_DECODE ) );
530 
531 		Broadcast( GalleryHint( GALLERY_HINT_CLOSE_OBJECT, GetName(), reinterpret_cast< sal_uIntPtr >( pEntry ) ) );
532 		delete pEntry;
533 		Broadcast( GalleryHint( GALLERY_HINT_OBJECT_REMOVED, GetName(), reinterpret_cast< sal_uIntPtr >( pEntry ) ) );
534 
535 		ImplSetModified( sal_True );
536 		ImplBroadcast( nPos );
537 	}
538 
539 	return( pEntry != NULL );
540 }
541 
542 // ------------------------------------------------------------------------
543 
544 sal_Bool GalleryTheme::ChangeObjectPos( sal_uIntPtr nOldPos, sal_uIntPtr nNewPos )
545 {
546 	sal_Bool bRet = sal_False;
547 
548 	if( nOldPos != nNewPos )
549 	{
550 		GalleryObject* pEntry = aObjectList.GetObject( nOldPos );
551 
552 		if( pEntry )
553 		{
554 			aObjectList.Insert( pEntry, nNewPos );
555 
556 			if( nNewPos < nOldPos )
557 				nOldPos++;
558 
559 			aObjectList.Remove( nOldPos );
560 			ImplSetModified( bRet = sal_True );
561 			ImplBroadcast( ( nNewPos < nOldPos ) ? nNewPos : ( nNewPos - 1 ) );
562 		}
563 	}
564 
565 	return bRet;
566 }
567 
568 // ------------------------------------------------------------------------
569 
570 void GalleryTheme::Actualize( const Link& rActualizeLink, GalleryProgress* pProgress )
571 {
572 	if( !IsReadOnly() && !IsImported() )
573 	{
574 		Graphic			aGraphic;
575 		String			aFormat;
576 		GalleryObject*	pEntry;
577 		const sal_uIntPtr		nCount = aObjectList.Count();
578 		sal_uIntPtr			i;
579 
580 		LockBroadcaster();
581 		bAbortActualize = sal_False;
582 
583 		// LoeschFlag zuruecksetzen
584 		for ( i = 0; i < nCount; i++ )
585 			aObjectList.GetObject( i )->mbDelete = false;
586 
587 		for( i = 0; ( i < nCount ) && !bAbortActualize; i++ )
588 		{
589 			if( pProgress )
590 				pProgress->Update( i, nCount - 1 );
591 
592 			pEntry = aObjectList.GetObject( i );
593 
594             const INetURLObject aURL( pEntry->aURL );
595 
596             rActualizeLink.Call( (void*) &aURL );
597 
598 			// SvDraw-Objekte werden spaeter aktualisiert
599 			if( pEntry->eObjKind != SGA_OBJ_SVDRAW )
600 			{
601 				// Hier muss noch etwas eingebaut werden,
602 				// das Files auf den ensprechenden Eintrag matched
603 				// Grafiken als Grafik-Objekte in die Gallery aufnehmen
604 				if( pEntry->eObjKind == SGA_OBJ_SOUND )
605 				{
606 					SgaObjectSound aObjSound( aURL );
607 					if( !InsertObject( aObjSound ) )
608 						pEntry->mbDelete = true;
609 				}
610 				else
611 				{
612 					aGraphic.Clear();
613 
614 					if ( GalleryGraphicImport( aURL, aGraphic, aFormat ) )
615 					{
616 						SgaObject* pNewObj;
617 
618 						if ( SGA_OBJ_INET == pEntry->eObjKind )
619 							pNewObj = (SgaObject*) new SgaObjectINet( aGraphic, aURL, aFormat );
620 						else if ( aGraphic.IsAnimated() )
621 							pNewObj = (SgaObject*) new SgaObjectAnim( aGraphic, aURL, aFormat );
622 						else
623 							pNewObj = (SgaObject*) new SgaObjectBmp( aGraphic, aURL, aFormat );
624 
625 						if( !InsertObject( *pNewObj ) )
626 							pEntry->mbDelete = true;
627 
628 						delete pNewObj;
629 					}
630 					else
631 						pEntry->mbDelete = true; // Loesch-Flag setzen
632 				}
633 			}
634 			else
635 			{
636 				if ( aSvDrawStorageRef.Is() )
637 				{
638 					const String		aStmName( GetSvDrawStreamNameFromURL( pEntry->aURL ) );
639 					SvStorageStreamRef	pIStm = aSvDrawStorageRef->OpenSotStream( aStmName, STREAM_READ );
640 
641 					if( pIStm && !pIStm->GetError() )
642 					{
643 						pIStm->SetBufferSize( 16384 );
644 
645 						SgaObjectSvDraw aNewObj( *pIStm, pEntry->aURL );
646 
647                         if( !InsertObject( aNewObj ) )
648 							pEntry->mbDelete = true;
649 
650 						pIStm->SetBufferSize( 0L );
651 					}
652 				}
653 			}
654 		}
655 
656 		// remove all entries with set flag
657 		pEntry = aObjectList.First();
658 		while( pEntry )
659 		{
660 			if( pEntry->mbDelete )
661 			{
662 				Broadcast( GalleryHint( GALLERY_HINT_CLOSE_OBJECT, GetName(), reinterpret_cast< sal_uIntPtr >( pEntry ) ) );
663 				delete aObjectList.Remove( pEntry );
664 				Broadcast( GalleryHint( GALLERY_HINT_OBJECT_REMOVED, GetName(), reinterpret_cast< sal_uIntPtr >( pEntry ) ) );
665 
666 				pEntry = aObjectList.GetCurObject();
667 			}
668 			else
669 				pEntry = aObjectList.Next();
670 		}
671 
672 		// update theme
673 		::utl::TempFile	aTmp;
674 		INetURLObject	aInURL( GetSdgURL() );
675 		INetURLObject	aTmpURL( aTmp.GetURL() );
676 
677 		DBG_ASSERT( aInURL.GetProtocol() != INET_PROT_NOT_VALID, "invalid URL" );
678 		DBG_ASSERT( aTmpURL.GetProtocol() != INET_PROT_NOT_VALID, "invalid URL" );
679 
680 		SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( aInURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ );
681 		SvStream* pTmpStm = ::utl::UcbStreamHelper::CreateStream( aTmpURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_WRITE | STREAM_TRUNC );
682 
683 		if( pIStm && pTmpStm )
684 		{
685 			pEntry = aObjectList.First();
686 
687 			while( pEntry )
688 			{
689 				SgaObject* pObj;
690 
691 				switch( pEntry->eObjKind )
692 				{
693 					case( SGA_OBJ_BMP ):	pObj = new SgaObjectBmp(); break;
694 					case( SGA_OBJ_ANIM ):	pObj = new SgaObjectAnim(); break;
695 					case( SGA_OBJ_INET ):	pObj = new SgaObjectINet(); break;
696 					case( SGA_OBJ_SVDRAW ):	pObj = new SgaObjectSvDraw(); break;
697 					case (SGA_OBJ_SOUND):	pObj = new SgaObjectSound(); break;
698 
699 					default:
700 						pObj = NULL;
701 					break;
702 				}
703 
704 				if( pObj )
705 				{
706 					pIStm->Seek( pEntry->nOffset );
707 					*pIStm >> *pObj;
708 					pEntry->nOffset = pTmpStm->Tell();
709 					*pTmpStm << *pObj;
710 					delete pObj;
711 				}
712 
713 				pEntry = aObjectList.Next();
714 			}
715 		}
716 		else
717 		{
718 			DBG_ERROR( "File(s) could not be opened" );
719 		}
720 
721 		delete pIStm;
722 		delete pTmpStm;
723 
724 		CopyFile( aTmpURL, aInURL );
725 		KillFile( aTmpURL );
726 
727 		sal_uIntPtr nStorErr = 0;
728 
729 		{
730 			SvStorageRef aTempStorageRef( new SvStorage( sal_False, aTmpURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_STD_READWRITE ) );
731 			aSvDrawStorageRef->CopyTo( aTempStorageRef );
732 			nStorErr = aSvDrawStorageRef->GetError();
733 		}
734 
735 		if( !nStorErr )
736 		{
737 			aSvDrawStorageRef.Clear();
738 			CopyFile( aTmpURL, GetSdvURL() );
739 			ImplCreateSvDrawStorage();
740 		}
741 
742 		KillFile( aTmpURL );
743 		ImplSetModified( sal_True );
744 		ImplWrite();
745 		UnlockBroadcaster();
746 	}
747 }
748 
749 // ------------------------------------------------------------------------
750 
751 GalleryThemeEntry* GalleryTheme::CreateThemeEntry( const INetURLObject& rURL, sal_Bool bReadOnly )
752 {
753 	DBG_ASSERT( rURL.GetProtocol() != INET_PROT_NOT_VALID, "invalid URL" );
754 
755 	GalleryThemeEntry*	pRet = NULL;
756 
757 	if( FileExists( rURL ) )
758 	{
759 		SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( rURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ );
760 
761 		if( pIStm )
762 		{
763 			String			aThemeName;
764 			sal_uInt32		nThemeId = 0;
765 			sal_uInt16		nVersion;
766 			sal_Bool			bThemeNameFromResource = sal_False;
767 
768 			*pIStm >> nVersion;
769 
770 			if( nVersion <= 0x00ff )
771 			{
772 				ByteString aTmpStr;
773 
774 				*pIStm >> aTmpStr; aThemeName = String( aTmpStr.GetBuffer(), RTL_TEXTENCODING_UTF8 );
775 
776 				// Charakterkonvertierung durchfuehren
777 				if( nVersion >= 0x0004 )
778 				{
779 					sal_uInt32	nCount;
780 					sal_uInt16	nTemp16;
781 
782 					*pIStm >> nCount >> nTemp16;
783 					pIStm->Seek( STREAM_SEEK_TO_END );
784 
785 					// pruefen, ob es sich um eine neuere Version handelt;
786 					// daher um 520Bytes (8Bytes Kennung + 512Bytes Reserverpuffer ) zurueckspringen,
787 					// falls dies ueberhaupt moeglich ist
788 					if( pIStm->Tell() >= 520 )
789 					{
790 						sal_uInt32 nId1, nId2;
791 
792 						pIStm->SeekRel( -520 );
793 						*pIStm >> nId1 >> nId2;
794 
795 						if( nId1 == COMPAT_FORMAT( 'G', 'A', 'L', 'R' ) &&
796 							nId2 == COMPAT_FORMAT( 'E', 'S', 'R', 'V' ) )
797 						{
798 							VersionCompat* pCompat = new VersionCompat( *pIStm, STREAM_READ );
799 
800 							*pIStm >> nThemeId;
801 
802 							if( pCompat->GetVersion() >= 2 )
803 							{
804 								*pIStm >> bThemeNameFromResource;
805 							}
806 
807 							delete pCompat;
808 						}
809 					}
810 				}
811 
812 				INetURLObject aPathURL( rURL );
813 
814 				aPathURL.removeSegment();
815 				aPathURL.removeFinalSlash();
816 				pRet = new GalleryThemeEntry( aPathURL, aThemeName,
817 											  String(rURL.GetBase()).Copy( 2, 6 ).ToInt32(),
818 											  bReadOnly, sal_False, sal_False, nThemeId,
819 											  bThemeNameFromResource );
820 			}
821 
822 			delete pIStm;
823 		}
824 	}
825 
826 	return pRet;
827 }
828 
829 // -----------------------------------------------------------------------------
830 
831 sal_Bool GalleryTheme::GetThumb( sal_uIntPtr nPos, BitmapEx& rBmp, sal_Bool )
832 {
833 	SgaObject*	pObj = AcquireObject( nPos );
834 	sal_Bool		bRet = sal_False;
835 
836 	if( pObj )
837 	{
838 		rBmp = pObj->GetThumbBmp();
839 		ReleaseObject( pObj );
840 		bRet = sal_True;
841 	}
842 
843 	return bRet;
844 }
845 
846 // -----------------------------------------------------------------------------
847 
848 sal_Bool GalleryTheme::GetGraphic( sal_uIntPtr nPos, Graphic& rGraphic, sal_Bool bProgress )
849 {
850 	const GalleryObject*	pObject = ImplGetGalleryObject( nPos );
851 	sal_Bool					bRet = sal_False;
852 
853 	if( pObject )
854 	{
855 		const INetURLObject aURL( ImplGetURL( pObject ) );
856 
857 		switch( pObject->eObjKind )
858 		{
859 			case( SGA_OBJ_BMP ):
860 			case( SGA_OBJ_ANIM ):
861 			case( SGA_OBJ_INET ):
862 			{
863 				String aFilterDummy;
864 				bRet = ( GalleryGraphicImport( aURL, rGraphic, aFilterDummy, bProgress ) != SGA_IMPORT_NONE );
865 			}
866 			break;
867 
868 			case( SGA_OBJ_SVDRAW ):
869 			{
870                 SvxGalleryDrawModel aModel;
871 
872                 if( aModel.GetModel() )
873                 {
874 				    if( GetModel( nPos, *aModel.GetModel(), bProgress ) )
875 				    {
876 					    ImageMap aIMap;
877 
878 					    if( CreateIMapGraphic( *aModel.GetModel(), rGraphic, aIMap ) )
879 						    bRet = sal_True;
880 					    else
881 					    {
882 						    VirtualDevice aVDev;
883 						    aVDev.SetMapMode( MapMode( MAP_100TH_MM ) );
884 						    FmFormView aView( aModel.GetModel(), &aVDev );
885 
886 						    aView.hideMarkHandles();
887 						    aView.ShowSdrPage(aView.GetModel()->GetPage(0));
888 						    aView.MarkAll();
889 						    rGraphic = aView.GetAllMarkedGraphic();
890 						    bRet = sal_True;
891 					    }
892 				    }
893                 }
894 			}
895 			break;
896 
897 			case( SGA_OBJ_SOUND ):
898 			{
899 				SgaObject* pObj = AcquireObject( nPos );
900 
901 				if( pObj )
902 				{
903                     rGraphic = pObj->GetThumbBmp();
904 					//Bitmap aBmp( pObj->GetThumbBmp() );
905 					//aBmp.Replace( COL_LIGHTMAGENTA, COL_WHITE );
906 					//rGraphic = aBmp;
907 					ReleaseObject( pObj );
908 					bRet = sal_True;
909 				}
910 			}
911 			break;
912 
913 			default:
914 			break;
915 		}
916 	}
917 
918 	return bRet;
919 }
920 
921 // -----------------------------------------------------------------------------
922 
923 sal_Bool GalleryTheme::InsertGraphic( const Graphic& rGraphic, sal_uIntPtr nInsertPos )
924 {
925 	sal_Bool bRet = sal_False;
926 
927 	if( rGraphic.GetType() != GRAPHIC_NONE )
928     {
929 		sal_uIntPtr           nExportFormat = CVT_UNKNOWN;
930 		const GfxLink	aGfxLink( ( (Graphic&) rGraphic ).GetLink() );
931 
932 		if( aGfxLink.GetDataSize() )
933 		{
934 			switch( aGfxLink.GetType() )
935 			{
936 				case( GFX_LINK_TYPE_EPS_BUFFER ): nExportFormat = CVT_SVM; break;
937 				case( GFX_LINK_TYPE_NATIVE_GIF ): nExportFormat = CVT_GIF; break;
938 				case( GFX_LINK_TYPE_NATIVE_JPG ): nExportFormat = CVT_JPG; break;
939 				case( GFX_LINK_TYPE_NATIVE_PNG ): nExportFormat = CVT_PNG; break;
940 				case( GFX_LINK_TYPE_NATIVE_TIF ): nExportFormat = CVT_TIF; break;
941 				case( GFX_LINK_TYPE_NATIVE_WMF ): nExportFormat = CVT_WMF; break;
942 				case( GFX_LINK_TYPE_NATIVE_MET ): nExportFormat = CVT_MET; break;
943 				case( GFX_LINK_TYPE_NATIVE_PCT ): nExportFormat = CVT_PCT; break;
944 				case( GFX_LINK_TYPE_NATIVE_SVG ): nExportFormat = CVT_SVG; break;
945 				default:
946 					break;
947 			}
948 		}
949 		else
950 		{
951 		    if( rGraphic.GetType() == GRAPHIC_BITMAP )
952 		    {
953 			    if( rGraphic.IsAnimated() )
954 				    nExportFormat = CVT_GIF;
955 			    else
956 				    nExportFormat = CVT_PNG;
957 		    }
958 		    else
959                 nExportFormat = CVT_SVM;
960 		}
961 
962         const INetURLObject aURL( ImplCreateUniqueURL( SGA_OBJ_BMP, nExportFormat ) );
963 		SvStream*           pOStm = ::utl::UcbStreamHelper::CreateStream( aURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_WRITE | STREAM_TRUNC );
964 
965         if( pOStm )
966         {
967 		    pOStm->SetVersion( SOFFICE_FILEFORMAT_50 );
968 
969             if( CVT_SVM == nExportFormat )
970             {
971                 GDIMetaFile aMtf( rGraphic.GetGDIMetaFile() );
972 
973 			    aMtf.Write( *pOStm );
974 			    bRet = ( pOStm->GetError() == ERRCODE_NONE );
975             }
976             else
977             {
978                 if( aGfxLink.GetDataSize() && aGfxLink.GetData() )
979                 {
980                     pOStm->Write( aGfxLink.GetData(), aGfxLink.GetDataSize() );
981                     bRet = ( pOStm->GetError() == ERRCODE_NONE );
982                 }
983                 else
984                     bRet = ( GraphicConverter::Export( *pOStm, rGraphic, nExportFormat ) == ERRCODE_NONE );
985             }
986 
987             delete pOStm;
988         }
989 
990 		if( bRet )
991 		{
992 			const SgaObjectBmp aObjBmp( aURL.GetMainURL( INetURLObject::NO_DECODE ) );
993 			InsertObject( aObjBmp, nInsertPos );
994 		}
995 	}
996 
997 	return bRet;
998 }
999 
1000 // -----------------------------------------------------------------------------
1001 
1002 sal_Bool GalleryTheme::GetModel( sal_uIntPtr nPos, SdrModel& rModel, sal_Bool )
1003 {
1004 	const GalleryObject*	pObject = ImplGetGalleryObject( nPos );
1005 	sal_Bool					bRet = sal_False;
1006 
1007 	if( pObject && ( SGA_OBJ_SVDRAW == pObject->eObjKind ) )
1008 	{
1009 		const INetURLObject aURL( ImplGetURL( pObject ) );
1010 		SvStorageRef		xStor( GetSvDrawStorage() );
1011 
1012 		if( xStor.Is() )
1013 		{
1014 			const String		aStmName( GetSvDrawStreamNameFromURL( aURL ) );
1015 			SvStorageStreamRef	xIStm( xStor->OpenSotStream( aStmName, STREAM_READ ) );
1016 
1017 			if( xIStm.Is() && !xIStm->GetError() )
1018 			{
1019 				xIStm->SetBufferSize( STREAMBUF_SIZE );
1020 				bRet = GallerySvDrawImport( *xIStm, rModel );
1021 				xIStm->SetBufferSize( 0L );
1022 			}
1023 		}
1024 	}
1025 
1026 	return bRet;
1027 }
1028 
1029 // -----------------------------------------------------------------------------
1030 
1031 sal_Bool GalleryTheme::InsertModel( const FmFormModel& rModel, sal_uIntPtr nInsertPos )
1032 {
1033 	INetURLObject	aURL( ImplCreateUniqueURL( SGA_OBJ_SVDRAW ) );
1034 	SvStorageRef	xStor( GetSvDrawStorage() );
1035 	sal_Bool			bRet = sal_False;
1036 
1037 	if( xStor.Is() )
1038 	{
1039 		const String		aStmName( GetSvDrawStreamNameFromURL( aURL ) );
1040 		SvStorageStreamRef	xOStm( xStor->OpenSotStream( aStmName, STREAM_WRITE | STREAM_TRUNC ) );
1041 
1042 		if( xOStm.Is() && !xOStm->GetError() )
1043 		{
1044 			SvMemoryStream	aMemStm( 65535, 65535 );
1045 			FmFormModel*	pFormModel = (FmFormModel*) &rModel;
1046 
1047 		    pFormModel->BurnInStyleSheetAttributes();
1048 
1049             {
1050 			    uno::Reference< io::XOutputStream > xDocOut( new utl::OOutputStreamWrapper( aMemStm ) );
1051 
1052         	    if( xDocOut.is() )
1053                     SvxDrawingLayerExport( pFormModel, xDocOut );
1054 		    }
1055 
1056             aMemStm.Seek( 0 );
1057 
1058 		    xOStm->SetBufferSize( 16348 );
1059 			GalleryCodec aCodec( *xOStm );
1060 		    aCodec.Write( aMemStm );
1061 
1062 		    if( !xOStm->GetError() )
1063 		    {
1064 			    SgaObjectSvDraw	aObjSvDraw( rModel, aURL );
1065 			    bRet = InsertObject( aObjSvDraw, nInsertPos );
1066 		    }
1067 
1068 		    xOStm->SetBufferSize( 0L );
1069             xOStm->Commit();
1070         }
1071 	}
1072 
1073 	return bRet;
1074 }
1075 
1076 // -----------------------------------------------------------------------------
1077 
1078 sal_Bool GalleryTheme::GetModelStream( sal_uIntPtr nPos, SotStorageStreamRef& rxModelStream, sal_Bool )
1079 {
1080 	const GalleryObject*	pObject = ImplGetGalleryObject( nPos );
1081 	sal_Bool					bRet = sal_False;
1082 
1083 	if( pObject && ( SGA_OBJ_SVDRAW == pObject->eObjKind ) )
1084 	{
1085 		const INetURLObject aURL( ImplGetURL( pObject ) );
1086 		SvStorageRef		xStor( GetSvDrawStorage() );
1087 
1088 		if( xStor.Is() )
1089 		{
1090 			const String		aStmName( GetSvDrawStreamNameFromURL( aURL ) );
1091 			SvStorageStreamRef	xIStm( xStor->OpenSotStream( aStmName, STREAM_READ ) );
1092 
1093 			if( xIStm.Is() && !xIStm->GetError() )
1094 			{
1095             	sal_uInt32 nVersion = 0;
1096 
1097                 xIStm->SetBufferSize( 16348 );
1098 
1099 	            if( GalleryCodec::IsCoded( *xIStm, nVersion ) )
1100                 {
1101                     SvxGalleryDrawModel aModel;
1102 
1103                     if( aModel.GetModel() )
1104                     {
1105                         if( GallerySvDrawImport( *xIStm, *aModel.GetModel() ) )
1106                         {
1107                             aModel.GetModel()->BurnInStyleSheetAttributes();
1108 
1109                             {
1110                                 uno::Reference< io::XOutputStream > xDocOut( new utl::OOutputStreamWrapper( *rxModelStream ) );
1111 
1112                                 if( SvxDrawingLayerExport( aModel.GetModel(), xDocOut ) )
1113                                     rxModelStream->Commit();
1114                             }
1115                         }
1116 
1117                         bRet = ( rxModelStream->GetError() == ERRCODE_NONE );
1118                     }
1119                 }
1120 
1121                 xIStm->SetBufferSize( 0 );
1122 			}
1123 		}
1124 	}
1125 
1126 	return bRet;
1127 }
1128 
1129 // -----------------------------------------------------------------------------
1130 
1131 sal_Bool GalleryTheme::InsertModelStream( const SotStorageStreamRef& rxModelStream, sal_uIntPtr nInsertPos )
1132 {
1133 	INetURLObject	aURL( ImplCreateUniqueURL( SGA_OBJ_SVDRAW ) );
1134 	SvStorageRef	xStor( GetSvDrawStorage() );
1135 	sal_Bool			bRet = sal_False;
1136 
1137 	if( xStor.Is() )
1138 	{
1139 		const String		aStmName( GetSvDrawStreamNameFromURL( aURL ) );
1140 		SvStorageStreamRef	xOStm( xStor->OpenSotStream( aStmName, STREAM_WRITE | STREAM_TRUNC ) );
1141 
1142 		if( xOStm.Is() && !xOStm->GetError() )
1143 		{
1144             GalleryCodec    aCodec( *xOStm );
1145             SvMemoryStream  aMemStm( 65535, 65535 );
1146 
1147 		    xOStm->SetBufferSize( 16348 );
1148             aCodec.Write( *rxModelStream );
1149 
1150 		    if( !xOStm->GetError() )
1151 		    {
1152 			    xOStm->Seek( 0 );
1153                 SgaObjectSvDraw	aObjSvDraw( *xOStm, aURL );
1154 			    bRet = InsertObject( aObjSvDraw, nInsertPos );
1155 		    }
1156 
1157 		    xOStm->SetBufferSize( 0L );
1158             xOStm->Commit();
1159         }
1160 	}
1161 
1162 	return bRet;
1163 }
1164 
1165 // -----------------------------------------------------------------------------
1166 
1167 sal_Bool GalleryTheme::GetURL( sal_uIntPtr nPos, INetURLObject& rURL, sal_Bool )
1168 {
1169 	const GalleryObject*	pObject = ImplGetGalleryObject( nPos );
1170 	sal_Bool					bRet = sal_False;
1171 
1172 	if( pObject )
1173 	{
1174 		rURL = INetURLObject( ImplGetURL( pObject ) );
1175 		bRet = sal_True;
1176 	}
1177 
1178 	return bRet;
1179 }
1180 
1181 // -----------------------------------------------------------------------------
1182 
1183 sal_Bool GalleryTheme::InsertURL( const INetURLObject& rURL, sal_uIntPtr nInsertPos )
1184 {
1185 	Graphic			aGraphic;
1186 	String			aFormat;
1187 	SgaObject*		pNewObj = NULL;
1188 	const sal_uInt16	nImportRet = GalleryGraphicImport( rURL, aGraphic, aFormat );
1189 	sal_Bool			bRet = sal_False;
1190 
1191 	if( nImportRet != SGA_IMPORT_NONE )
1192 	{
1193 		if ( SGA_IMPORT_INET == nImportRet )
1194 			pNewObj = (SgaObject*) new SgaObjectINet( aGraphic, rURL, aFormat );
1195 		else if ( aGraphic.IsAnimated() )
1196 			pNewObj = (SgaObject*) new SgaObjectAnim( aGraphic, rURL, aFormat );
1197 		else
1198 			pNewObj = (SgaObject*) new SgaObjectBmp( aGraphic, rURL, aFormat );
1199 	}
1200 	else if( ::avmedia::MediaWindow::isMediaURL( rURL.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS ) ) )
1201 		pNewObj = (SgaObject*) new SgaObjectSound( rURL );
1202 
1203 	if( pNewObj && InsertObject( *pNewObj, nInsertPos ) )
1204 		bRet = sal_True;
1205 
1206 	delete pNewObj;
1207 
1208 	return bRet;
1209 }
1210 
1211 // -----------------------------------------------------------------------------
1212 
1213 sal_Bool GalleryTheme::InsertFileOrDirURL( const INetURLObject& rFileOrDirURL, sal_uIntPtr nInsertPos )
1214 {
1215     INetURLObject                   aURL;
1216     ::std::vector< INetURLObject >  aURLVector;
1217 	sal_Bool                            bRet = sal_False;
1218 
1219 	try
1220 	{
1221 		::ucbhelper::Content         aCnt( rFileOrDirURL.GetMainURL( INetURLObject::NO_DECODE ), uno::Reference< ucb::XCommandEnvironment >() );
1222 		sal_Bool        bFolder = false;
1223 
1224 		aCnt.getPropertyValue( OUString::createFromAscii( "IsFolder" ) ) >>= bFolder;
1225 
1226 		if( bFolder )
1227 		{
1228 			uno::Sequence< OUString > aProps( 1 );
1229 			aProps.getArray()[ 0 ] = OUString::createFromAscii( "Url" );
1230 			uno::Reference< sdbc::XResultSet > xResultSet( aCnt.createCursor( aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY ) );
1231             uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
1232 			if( xContentAccess.is() )
1233 			{
1234 				while( xResultSet->next() )
1235 				{
1236 					aURL.SetSmartURL( xContentAccess->queryContentIdentifierString() );
1237                     aURLVector.push_back( aURL );
1238 				}
1239 			}
1240 		}
1241 		else
1242             aURLVector.push_back( rFileOrDirURL );
1243 	}
1244 	catch( const ucb::ContentCreationException& )
1245 	{
1246 	}
1247 	catch( const uno::RuntimeException& )
1248 	{
1249 	}
1250 	catch( const uno::Exception& )
1251 	{
1252 	}
1253 
1254     ::std::vector< INetURLObject >::const_iterator aIter( aURLVector.begin() ), aEnd( aURLVector.end() );
1255 
1256     while( aIter != aEnd )
1257         bRet = bRet || InsertURL( *aIter++, nInsertPos );
1258 
1259     return bRet;
1260 }
1261 
1262 // -----------------------------------------------------------------------------
1263 
1264 sal_Bool GalleryTheme::InsertTransferable( const uno::Reference< datatransfer::XTransferable >& rxTransferable, sal_uIntPtr nInsertPos )
1265 {
1266 	sal_Bool bRet = sal_False;
1267 
1268 	if( rxTransferable.is() )
1269 	{
1270 		TransferableDataHelper	aDataHelper( rxTransferable );
1271 		Graphic*				pGraphic = NULL;
1272 
1273 		if( aDataHelper.HasFormat( SOT_FORMATSTR_ID_DRAWING ) )
1274 		{
1275 			SotStorageStreamRef xModelStm;
1276 
1277 			if( aDataHelper.GetSotStorageStream( SOT_FORMATSTR_ID_DRAWING, xModelStm ) )
1278 				bRet = InsertModelStream( xModelStm, nInsertPos );
1279 		}
1280 		else if( aDataHelper.HasFormat( SOT_FORMAT_FILE_LIST ) ||
1281                  aDataHelper.HasFormat( FORMAT_FILE ) )
1282 		{
1283             FileList aFileList;
1284 
1285             if( aDataHelper.HasFormat( SOT_FORMAT_FILE_LIST ) )
1286                 aDataHelper.GetFileList( SOT_FORMAT_FILE_LIST, aFileList );
1287             else
1288             {
1289                 String aFile;
1290 
1291                 aDataHelper.GetString( FORMAT_FILE, aFile );
1292 
1293                 if( aFile.Len() )
1294                     aFileList.AppendFile( aFile );
1295             }
1296 
1297             for( sal_uInt32 i = 0, nCount = aFileList.Count(); i < nCount; ++i )
1298             {
1299                 const String    aFile( aFileList.GetFile( i ) );
1300                 INetURLObject   aURL( aFile );
1301 
1302                 if( aURL.GetProtocol() == INET_PROT_NOT_VALID )
1303                 {
1304                     String aLocalURL;
1305 
1306                     if( ::utl::LocalFileHelper::ConvertPhysicalNameToURL( aFile, aLocalURL ) )
1307                         aURL = INetURLObject( aLocalURL );
1308                 }
1309 
1310                 if( aURL.GetProtocol() != INET_PROT_NOT_VALID )
1311                     bRet = InsertFileOrDirURL( aURL, nInsertPos );
1312 			}
1313 		}
1314 		else
1315 		{
1316 			Graphic	aGraphic;
1317 			sal_uIntPtr	nFormat = 0;
1318 
1319 			if( aDataHelper.HasFormat( SOT_FORMATSTR_ID_SVXB ) )
1320 				nFormat = SOT_FORMATSTR_ID_SVXB;
1321 			else if( aDataHelper.HasFormat( FORMAT_GDIMETAFILE ) )
1322 				nFormat = FORMAT_GDIMETAFILE;
1323 			else if( aDataHelper.HasFormat( FORMAT_BITMAP ) )
1324 				nFormat = FORMAT_BITMAP;
1325 
1326 			if( nFormat && aDataHelper.GetGraphic( nFormat, aGraphic ) )
1327 				pGraphic = new Graphic( aGraphic );
1328 		}
1329 
1330 		if( pGraphic )
1331 		{
1332 			bRet = sal_False;
1333 
1334 			if( aDataHelper.HasFormat( SOT_FORMATSTR_ID_SVIM ) )
1335 			{
1336 
1337 				ImageMap aImageMap;
1338 
1339                 // according to KA we don't need a BaseURL here
1340                 if( aDataHelper.GetImageMap( SOT_FORMATSTR_ID_SVIM, aImageMap ) )
1341 				{
1342                     SvxGalleryDrawModel aModel;
1343 
1344                     if( aModel.GetModel() )
1345                     {
1346 					    SgaUserDataFactory	aFactory;
1347 
1348 					    SdrPage*	pPage = aModel.GetModel()->GetPage(0);
1349 					    SdrGrafObj*	pGrafObj = new SdrGrafObj( *pGraphic );
1350 
1351 					    pGrafObj->InsertUserData( new SgaIMapInfo( aImageMap ) );
1352 					    pPage->InsertObject( pGrafObj );
1353 					    bRet = InsertModel( *aModel.GetModel(), nInsertPos );
1354                     }
1355 				}
1356 			}
1357 
1358 			if( !bRet )
1359 				bRet = InsertGraphic( *pGraphic, nInsertPos );
1360 
1361 			delete pGraphic;
1362 		}
1363 	}
1364 
1365 	return bRet;
1366 }
1367 
1368 // -----------------------------------------------------------------------------
1369 
1370 void GalleryTheme::CopyToClipboard( Window* pWindow, sal_uIntPtr nPos )
1371 {
1372 	GalleryTransferable* pTransferable = new GalleryTransferable( this, nPos, false );
1373 	pTransferable->CopyToClipboard( pWindow );
1374 }
1375 
1376 // -----------------------------------------------------------------------------
1377 
1378 void GalleryTheme::StartDrag( Window* pWindow, sal_uIntPtr nPos )
1379 {
1380 	GalleryTransferable* pTransferable = new GalleryTransferable( this, nPos, true );
1381 	pTransferable->StartDrag( pWindow, DND_ACTION_COPY | DND_ACTION_LINK );
1382 }
1383 
1384 // -----------------------------------------------------------------------------
1385 
1386 SvStream& GalleryTheme::WriteData( SvStream& rOStm ) const
1387 {
1388 	const INetURLObject	aRelURL1( GetParent()->GetRelativeURL() );
1389 	const INetURLObject	aRelURL2( GetParent()->GetUserURL() );
1390 	INetURLObject		aNewURL, aTempURL;
1391 	sal_uInt32			nCount = GetObjectCount();
1392 	sal_Bool				bRel;
1393 
1394 	rOStm << (sal_uInt16) 0x0004;
1395 	rOStm << ByteString( GetRealName(), RTL_TEXTENCODING_UTF8 );
1396 	rOStm << nCount << (sal_uInt16) gsl_getSystemTextEncoding();
1397 
1398 	for( sal_uInt32 i = 0; i < nCount; i++ )
1399 	{
1400 		const GalleryObject* pObj = ImplGetGalleryObject( i );
1401 		String				 aPath;
1402 
1403 		if( SGA_OBJ_SVDRAW == pObj->eObjKind )
1404 		{
1405 			aPath = GetSvDrawStreamNameFromURL( pObj->aURL );
1406 			bRel = sal_False;
1407 		}
1408 		else
1409 		{
1410 			aPath = pObj->aURL.GetMainURL( INetURLObject::NO_DECODE );
1411 			bRel = ( ( aPath.Erase( sal::static_int_cast< xub_StrLen >( aRelURL1.GetMainURL( INetURLObject::NO_DECODE ).getLength() ) ) ) == String(aRelURL1.GetMainURL( INetURLObject::NO_DECODE ) ));
1412 
1413 			if( bRel && ( pObj->aURL.GetMainURL( INetURLObject::NO_DECODE ).getLength() > ( aRelURL1.GetMainURL( INetURLObject::NO_DECODE ).getLength() + 1 ) ) )
1414 			{
1415 				aPath = pObj->aURL.GetMainURL( INetURLObject::NO_DECODE );
1416 				aPath = aPath.Erase( 0, sal::static_int_cast< xub_StrLen >( aRelURL1.GetMainURL( INetURLObject::NO_DECODE ).getLength() ) );
1417 			}
1418 			else
1419 			{
1420 				aPath = pObj->aURL.GetMainURL( INetURLObject::NO_DECODE );
1421 				bRel = ( ( aPath.Erase( sal::static_int_cast< xub_StrLen >( aRelURL2.GetMainURL( INetURLObject::NO_DECODE ).getLength() ) ) ) == String(aRelURL2.GetMainURL( INetURLObject::NO_DECODE ) ));
1422 
1423 				if( bRel && ( pObj->aURL.GetMainURL( INetURLObject::NO_DECODE ).getLength() > ( aRelURL2.GetMainURL( INetURLObject::NO_DECODE ).getLength() + 1 ) ) )
1424 				{
1425 					aPath = pObj->aURL.GetMainURL( INetURLObject::NO_DECODE );
1426 					aPath = aPath.Erase( 0, sal::static_int_cast< xub_StrLen >( aRelURL2.GetMainURL( INetURLObject::NO_DECODE ).getLength() ) );
1427 				}
1428 				else
1429 					aPath = pObj->aURL.GetMainURL( INetURLObject::NO_DECODE );
1430 			}
1431 		}
1432 
1433 		aPath.SearchAndReplace(m_aDestDir, String());
1434 		rOStm << bRel << ByteString( aPath, RTL_TEXTENCODING_UTF8 ) << pObj->nOffset << (sal_uInt16) pObj->eObjKind;
1435 	}
1436 
1437 	// neuerdings wird ein 512-Byte-Reservepuffer gechrieben;
1438 	// um diesen zu erkennen werden zwei sal_uIntPtr-Ids geschrieben
1439 	rOStm << COMPAT_FORMAT( 'G', 'A', 'L', 'R' ) << COMPAT_FORMAT( 'E', 'S', 'R', 'V' );
1440 
1441 	const long		nReservePos = rOStm.Tell();
1442 	VersionCompat*	pCompat = new VersionCompat( rOStm, STREAM_WRITE, 2 );
1443 
1444 	rOStm << (sal_uInt32) GetId() << IsThemeNameFromResource(); // ab Version 2
1445 
1446 	delete pCompat;
1447 
1448 	// Rest des Puffers auffuellen
1449 	const long	nRest = Max( 512L - ( (long) rOStm.Tell() - nReservePos ), 0L );
1450 
1451 	if( nRest )
1452 	{
1453 		char* pReserve = new char[ nRest ];
1454 		memset( pReserve, 0, nRest );
1455 		rOStm.Write( pReserve, nRest );
1456 		delete[] pReserve;
1457 	}
1458 
1459 	return rOStm;
1460 }
1461 
1462 // ------------------------------------------------------------------------
1463 
1464 SvStream& GalleryTheme::ReadData( SvStream& rIStm )
1465 {
1466 	sal_uInt32			nCount;
1467 	sal_uInt16			nVersion;
1468 	ByteString			aTmpStr;
1469 	String				aThemeName;
1470 	rtl_TextEncoding	nTextEncoding;
1471 
1472 	aImportName = String();
1473 	rIStm >> nVersion >> aTmpStr >> nCount;
1474 
1475 	if( nVersion >= 0x0004 )
1476 	{
1477 		sal_uInt16 nTmp16;
1478 		rIStm >> nTmp16;
1479 		nTextEncoding = (rtl_TextEncoding) nTmp16;
1480 	}
1481 	else
1482 		nTextEncoding = RTL_TEXTENCODING_UTF8;
1483 
1484 	aThemeName = String( aTmpStr.GetBuffer(), nTextEncoding );
1485 
1486 	if( nCount <= ( 1L << 14 ) )
1487 	{
1488 		GalleryObject*	pObj;
1489 		INetURLObject	aRelURL1( GetParent()->GetRelativeURL() );
1490 		INetURLObject	aRelURL2( GetParent()->GetUserURL() );
1491 		sal_uInt32		nId1, nId2;
1492 		sal_Bool			bRel;
1493 
1494 		for( pObj = aObjectList.First(); pObj; pObj = aObjectList.Next() )
1495 		{
1496 			Broadcast( GalleryHint( GALLERY_HINT_CLOSE_OBJECT, GetName(), reinterpret_cast< sal_uIntPtr >( pObj ) ) );
1497 			delete pObj;
1498 			Broadcast( GalleryHint( GALLERY_HINT_OBJECT_REMOVED, GetName(), reinterpret_cast< sal_uIntPtr >( pObj ) ) );
1499 		}
1500 
1501 		aObjectList.Clear();
1502 
1503 		for( sal_uInt32 i = 0; i < nCount; i++ )
1504 		{
1505 			pObj = new GalleryObject;
1506 
1507 			ByteString	aTempFileName;
1508 			String		aFileName;
1509 			String		aPath;
1510 			sal_uInt16	nTemp;
1511 
1512 			rIStm >> bRel >> aTempFileName >> pObj->nOffset;
1513 			rIStm >> nTemp; pObj->eObjKind = (SgaObjKind) nTemp;
1514 
1515 			aFileName = String( aTempFileName.GetBuffer(), gsl_getSystemTextEncoding() );
1516 
1517 			if( bRel )
1518 			{
1519 				aFileName.SearchAndReplaceAll( '\\', '/' );
1520 				aPath = aRelURL1.GetMainURL( INetURLObject::NO_DECODE );
1521 
1522 				if( aFileName.GetChar( 0 ) != '/' )
1523 			    		aPath += '/';
1524 
1525 				aPath += aFileName;
1526 
1527 				pObj->aURL = INetURLObject( aPath );
1528 
1529 				if( !FileExists( pObj->aURL ) )
1530 				{
1531 					aPath = aRelURL2.GetMainURL( INetURLObject::NO_DECODE );
1532 
1533 					if( aFileName.GetChar( 0 ) != '/' )
1534 						aPath += '/';
1535 
1536 					aPath += aFileName;
1537 
1538 					// assign this URL, even in the case it is not valid (#94482)
1539                     pObj->aURL = INetURLObject( aPath );
1540 				}
1541 			}
1542 			else
1543 			{
1544 				if( SGA_OBJ_SVDRAW == pObj->eObjKind )
1545 				{
1546 					const static String aBaseURLStr( RTL_CONSTASCII_USTRINGPARAM( "gallery/svdraw/" ) );
1547 
1548 					String aDummyURL( aBaseURLStr );
1549 					pObj->aURL = INetURLObject( aDummyURL += aFileName, INET_PROT_PRIV_SOFFICE );
1550 				}
1551 				else
1552 				{
1553                     String aLocalURL;
1554 
1555 					pObj->aURL = INetURLObject( aFileName );
1556 
1557 					if( ( pObj->aURL.GetProtocol() == INET_PROT_NOT_VALID ) &&
1558 						::utl::LocalFileHelper::ConvertPhysicalNameToURL( aFileName, aLocalURL ) )
1559 					{
1560 						pObj->aURL = INetURLObject( aLocalURL );
1561 					}
1562 				}
1563 			}
1564 
1565 			aObjectList.Insert( pObj, LIST_APPEND );
1566 		}
1567 
1568 		rIStm >> nId1 >> nId2;
1569 
1570 		// in neueren Versionen befindet sich am Ende ein 512-Byte-Reservepuffer;
1571 		// die Daten befinden sich am Anfang dieses Puffers und
1572 		// sind durch eine VersionCompat geklammert
1573 		if( !rIStm.IsEof() &&
1574 			nId1 == COMPAT_FORMAT( 'G', 'A', 'L', 'R' ) &&
1575 			nId2 == COMPAT_FORMAT( 'E', 'S', 'R', 'V' ) )
1576 		{
1577 			VersionCompat*	pCompat = new VersionCompat( rIStm, STREAM_READ );
1578 			sal_uInt32		nTemp32;
1579 			sal_Bool			bThemeNameFromResource = sal_False;
1580 
1581 			rIStm >> nTemp32;
1582 
1583 			if( pCompat->GetVersion() >= 2 )
1584 			{
1585 				rIStm >> bThemeNameFromResource;
1586 			}
1587 
1588 			SetId( nTemp32, bThemeNameFromResource );
1589 			delete pCompat;
1590 		}
1591 	}
1592 	else
1593 		rIStm.SetError( SVSTREAM_READ_ERROR );
1594 
1595 	ImplSetModified( sal_False );
1596 
1597 	return rIStm;
1598 }
1599 
1600 // ------------------------------------------------------------------------
1601 
1602 SvStream& operator<<( SvStream& rOut, const GalleryTheme& rTheme )
1603 {
1604 	return rTheme.WriteData( rOut );
1605 }
1606 
1607 // ------------------------------------------------------------------------
1608 
1609 SvStream& operator>>( SvStream& rIn, GalleryTheme& rTheme )
1610 {
1611 	return rTheme.ReadData( rIn );
1612 }
1613 
1614 void GalleryTheme::ImplSetModified( sal_Bool bModified )
1615 { pThm->SetModified( bModified ); }
1616 
1617 const String& GalleryTheme::GetRealName() const { return pThm->GetThemeName(); }
1618 const INetURLObject& GalleryTheme::GetThmURL() const { return pThm->GetThmURL(); }
1619 const INetURLObject& GalleryTheme::GetSdgURL() const { return pThm->GetSdgURL(); }
1620 const INetURLObject& GalleryTheme::GetSdvURL() const { return pThm->GetSdvURL(); }
1621 sal_uInt32 GalleryTheme::GetId() const { return pThm->GetId(); }
1622 void GalleryTheme::SetId( sal_uInt32 nNewId, sal_Bool bResetThemeName ) { pThm->SetId( nNewId, bResetThemeName ); }
1623 sal_Bool GalleryTheme::IsThemeNameFromResource() const { return pThm->IsNameFromResource(); }
1624 sal_Bool GalleryTheme::IsImported() const { return pThm->IsImported(); }
1625 sal_Bool GalleryTheme::IsReadOnly() const { return pThm->IsReadOnly(); }
1626 sal_Bool GalleryTheme::IsDefault() const { return pThm->IsDefault(); }
1627 sal_Bool GalleryTheme::IsModified() const { return pThm->IsModified(); }
1628 const String& GalleryTheme::GetName() const	{ return IsImported() ? aImportName : pThm->GetThemeName(); }
1629 
1630 void GalleryTheme::InsertAllThemes( ListBox& rListBox )
1631 {
1632 	for( sal_uInt16 i = RID_GALLERYSTR_THEME_FIRST; i <= RID_GALLERYSTR_THEME_LAST; i++ )
1633 		rListBox.InsertEntry( String( GAL_RESID( i ) ) );
1634 }
1635 
1636