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