xref: /AOO41X/main/svx/source/gallery2/galtheme.cxx (revision 6a6ec68d792bd477e5b23798f42a1a9de0925497)
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::ReleaseObject( SgaObject* pObject )
475 {
476     delete pObject;
477 }
478 
479 // ------------------------------------------------------------------------
480 
481 sal_Bool GalleryTheme::RemoveObject( sal_uIntPtr nPos )
482 {
483     GalleryObject* pEntry = aObjectList.Remove( nPos );
484 
485     if( !aObjectList.Count() )
486         KillFile( GetSdgURL() );
487 
488     if( pEntry )
489     {
490         if( SGA_OBJ_SVDRAW == pEntry->eObjKind )
491             aSvDrawStorageRef->Remove( pEntry->aURL.GetMainURL( INetURLObject::NO_DECODE ) );
492 
493         Broadcast( GalleryHint( GALLERY_HINT_CLOSE_OBJECT, GetName(), reinterpret_cast< sal_uIntPtr >( pEntry ) ) );
494         delete pEntry;
495         Broadcast( GalleryHint( GALLERY_HINT_OBJECT_REMOVED, GetName(), reinterpret_cast< sal_uIntPtr >( pEntry ) ) );
496 
497         ImplSetModified( sal_True );
498         ImplBroadcast( nPos );
499     }
500 
501     return( pEntry != NULL );
502 }
503 
504 // ------------------------------------------------------------------------
505 
506 sal_Bool GalleryTheme::ChangeObjectPos( sal_uIntPtr nOldPos, sal_uIntPtr nNewPos )
507 {
508     sal_Bool bRet = sal_False;
509 
510     if( nOldPos != nNewPos )
511     {
512         GalleryObject* pEntry = aObjectList.GetObject( nOldPos );
513 
514         if( pEntry )
515         {
516             aObjectList.Insert( pEntry, nNewPos );
517 
518             if( nNewPos < nOldPos )
519                 nOldPos++;
520 
521             aObjectList.Remove( nOldPos );
522             ImplSetModified( bRet = sal_True );
523             ImplBroadcast( ( nNewPos < nOldPos ) ? nNewPos : ( nNewPos - 1 ) );
524         }
525     }
526 
527     return bRet;
528 }
529 
530 // ------------------------------------------------------------------------
531 
532 void GalleryTheme::Actualize( const Link& rActualizeLink, GalleryProgress* pProgress )
533 {
534     if( !IsReadOnly() && !IsImported() )
535     {
536         Graphic         aGraphic;
537         String          aFormat;
538         GalleryObject*  pEntry;
539         const sal_uIntPtr       nCount = aObjectList.Count();
540         sal_uIntPtr         i;
541 
542         LockBroadcaster();
543         bAbortActualize = sal_False;
544 
545         // LoeschFlag zuruecksetzen
546         for ( i = 0; i < nCount; i++ )
547             aObjectList.GetObject( i )->bDummy = sal_False;
548 
549         for( i = 0; ( i < nCount ) && !bAbortActualize; i++ )
550         {
551             if( pProgress )
552                 pProgress->Update( i, nCount - 1 );
553 
554             pEntry = aObjectList.GetObject( i );
555 
556             const INetURLObject aURL( pEntry->aURL );
557 
558             rActualizeLink.Call( (void*) &aURL );
559 
560             // SvDraw-Objekte werden spaeter aktualisiert
561             if( pEntry->eObjKind != SGA_OBJ_SVDRAW )
562             {
563                 // Hier muss noch etwas eingebaut werden,
564                 // das Files auf den ensprechenden Eintrag matched
565                 // Grafiken als Grafik-Objekte in die Gallery aufnehmen
566                 if( pEntry->eObjKind == SGA_OBJ_SOUND )
567                 {
568                     SgaObjectSound aObjSound( aURL );
569                     if( !InsertObject( aObjSound ) )
570                         pEntry->bDummy = sal_True;
571                 }
572                 else
573                 {
574                     aGraphic.Clear();
575 
576                     if ( GalleryGraphicImport( aURL, aGraphic, aFormat ) )
577                     {
578                         SgaObject* pNewObj;
579 
580                         if ( SGA_OBJ_INET == pEntry->eObjKind )
581                             pNewObj = (SgaObject*) new SgaObjectINet( aGraphic, aURL, aFormat );
582                         else if ( aGraphic.IsAnimated() )
583                             pNewObj = (SgaObject*) new SgaObjectAnim( aGraphic, aURL, aFormat );
584                         else
585                             pNewObj = (SgaObject*) new SgaObjectBmp( aGraphic, aURL, aFormat );
586 
587                         if( !InsertObject( *pNewObj ) )
588                             pEntry->bDummy = sal_True;
589 
590                         delete pNewObj;
591                     }
592                     else
593                         pEntry->bDummy = sal_True; // Loesch-Flag setzen
594                 }
595             }
596             else
597             {
598                 if ( aSvDrawStorageRef.Is() )
599                 {
600                     const String        aStmName( GetSvDrawStreamNameFromURL( pEntry->aURL ) );
601                     SvStorageStreamRef  pIStm = aSvDrawStorageRef->OpenSotStream( aStmName, STREAM_READ );
602 
603                     if( pIStm && !pIStm->GetError() )
604                     {
605                         pIStm->SetBufferSize( 16384 );
606 
607                         SgaObjectSvDraw aNewObj( *pIStm, pEntry->aURL );
608 
609                         if( !InsertObject( aNewObj ) )
610                             pEntry->bDummy = sal_True;
611 
612                         pIStm->SetBufferSize( 0L );
613                     }
614                 }
615             }
616         }
617 
618         // remove all entries with set flag
619         pEntry = aObjectList.First();
620         while( pEntry )
621         {
622             if( pEntry->bDummy )
623             {
624                 Broadcast( GalleryHint( GALLERY_HINT_CLOSE_OBJECT, GetName(), reinterpret_cast< sal_uIntPtr >( pEntry ) ) );
625                 delete aObjectList.Remove( pEntry );
626                 Broadcast( GalleryHint( GALLERY_HINT_OBJECT_REMOVED, GetName(), reinterpret_cast< sal_uIntPtr >( pEntry ) ) );
627 
628                 pEntry = aObjectList.GetCurObject();
629             }
630             else
631                 pEntry = aObjectList.Next();
632         }
633 
634         // update theme
635         ::utl::TempFile aTmp;
636         INetURLObject   aInURL( GetSdgURL() );
637         INetURLObject   aTmpURL( aTmp.GetURL() );
638 
639         DBG_ASSERT( aInURL.GetProtocol() != INET_PROT_NOT_VALID, "invalid URL" );
640         DBG_ASSERT( aTmpURL.GetProtocol() != INET_PROT_NOT_VALID, "invalid URL" );
641 
642         SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( aInURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ );
643         SvStream* pTmpStm = ::utl::UcbStreamHelper::CreateStream( aTmpURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_WRITE | STREAM_TRUNC );
644 
645         if( pIStm && pTmpStm )
646         {
647             pEntry = aObjectList.First();
648 
649             while( pEntry )
650             {
651                 SgaObject* pObj;
652 
653                 switch( pEntry->eObjKind )
654                 {
655                     case( SGA_OBJ_BMP ):    pObj = new SgaObjectBmp(); break;
656                     case( SGA_OBJ_ANIM ):   pObj = new SgaObjectAnim(); break;
657                     case( SGA_OBJ_INET ):   pObj = new SgaObjectINet(); break;
658                     case( SGA_OBJ_SVDRAW ): pObj = new SgaObjectSvDraw(); break;
659                     case (SGA_OBJ_SOUND):   pObj = new SgaObjectSound(); break;
660 
661                     default:
662                         pObj = NULL;
663                     break;
664                 }
665 
666                 if( pObj )
667                 {
668                     pIStm->Seek( pEntry->nOffset );
669                     *pIStm >> *pObj;
670                     pEntry->nOffset = pTmpStm->Tell();
671                     *pTmpStm << *pObj;
672                     delete pObj;
673                 }
674 
675                 pEntry = aObjectList.Next();
676             }
677         }
678         else
679         {
680             DBG_ERROR( "File(s) could not be opened" );
681         }
682 
683         delete pIStm;
684         delete pTmpStm;
685 
686         CopyFile( aTmpURL, aInURL );
687         KillFile( aTmpURL );
688 
689         sal_uIntPtr nStorErr = 0;
690 
691         {
692             SvStorageRef aTempStorageRef( new SvStorage( sal_False, aTmpURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_STD_READWRITE ) );
693             aSvDrawStorageRef->CopyTo( aTempStorageRef );
694             nStorErr = aSvDrawStorageRef->GetError();
695         }
696 
697         if( !nStorErr )
698         {
699             aSvDrawStorageRef.Clear();
700             CopyFile( aTmpURL, GetSdvURL() );
701             ImplCreateSvDrawStorage();
702         }
703 
704         KillFile( aTmpURL );
705         ImplSetModified( sal_True );
706         ImplWrite();
707         UnlockBroadcaster();
708     }
709 }
710 
711 // ------------------------------------------------------------------------
712 
713 GalleryThemeEntry* GalleryTheme::CreateThemeEntry( const INetURLObject& rURL, sal_Bool bReadOnly )
714 {
715     DBG_ASSERT( rURL.GetProtocol() != INET_PROT_NOT_VALID, "invalid URL" );
716 
717     GalleryThemeEntry*  pRet = NULL;
718 
719     if( FileExists( rURL ) )
720     {
721         SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( rURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ );
722 
723         if( pIStm )
724         {
725             String          aThemeName;
726             sal_uInt32      nThemeId = 0;
727             sal_uInt16      nVersion;
728             sal_Bool            bThemeNameFromResource = sal_False;
729 
730             *pIStm >> nVersion;
731 
732             if( nVersion <= 0x00ff )
733             {
734                 ByteString aTmpStr;
735 
736                 *pIStm >> aTmpStr; aThemeName = String( aTmpStr.GetBuffer(), RTL_TEXTENCODING_UTF8 );
737 
738                 // Charakterkonvertierung durchfuehren
739                 if( nVersion >= 0x0004 )
740                 {
741                     sal_uInt32  nCount;
742                     sal_uInt16  nTemp16;
743 
744                     *pIStm >> nCount >> nTemp16;
745                     pIStm->Seek( STREAM_SEEK_TO_END );
746 
747                     // pruefen, ob es sich um eine neuere Version handelt;
748                     // daher um 520Bytes (8Bytes Kennung + 512Bytes Reserverpuffer ) zurueckspringen,
749                     // falls dies ueberhaupt moeglich ist
750                     if( pIStm->Tell() >= 520 )
751                     {
752                         sal_uInt32 nId1, nId2;
753 
754                         pIStm->SeekRel( -520 );
755                         *pIStm >> nId1 >> nId2;
756 
757                         if( nId1 == COMPAT_FORMAT( 'G', 'A', 'L', 'R' ) &&
758                             nId2 == COMPAT_FORMAT( 'E', 'S', 'R', 'V' ) )
759                         {
760                             VersionCompat* pCompat = new VersionCompat( *pIStm, STREAM_READ );
761 
762                             *pIStm >> nThemeId;
763 
764                             if( pCompat->GetVersion() >= 2 )
765                             {
766                                 *pIStm >> bThemeNameFromResource;
767                             }
768 
769                             delete pCompat;
770                         }
771                     }
772                 }
773 
774                 INetURLObject aPathURL( rURL );
775 
776                 aPathURL.removeSegment();
777                 aPathURL.removeFinalSlash();
778                 pRet = new GalleryThemeEntry( aPathURL, aThemeName,
779                                               String(rURL.GetBase()).Copy( 2, 6 ).ToInt32(),
780                                               bReadOnly, sal_False, sal_False, nThemeId,
781                                               bThemeNameFromResource );
782             }
783 
784             delete pIStm;
785         }
786     }
787 
788     return pRet;
789 }
790 
791 // -----------------------------------------------------------------------------
792 
793 sal_Bool GalleryTheme::GetThumb( sal_uIntPtr nPos, BitmapEx& rBmp, sal_Bool )
794 {
795     SgaObject*  pObj = AcquireObject( nPos );
796     sal_Bool        bRet = sal_False;
797 
798     if( pObj )
799     {
800         rBmp = pObj->GetThumbBmp();
801         ReleaseObject( pObj );
802         bRet = sal_True;
803     }
804 
805     return bRet;
806 }
807 
808 // -----------------------------------------------------------------------------
809 
810 sal_Bool GalleryTheme::GetGraphic( sal_uIntPtr nPos, Graphic& rGraphic, sal_Bool bProgress )
811 {
812     const GalleryObject*    pObject = ImplGetGalleryObject( nPos );
813     sal_Bool                    bRet = sal_False;
814 
815     if( pObject )
816     {
817         const INetURLObject aURL( ImplGetURL( pObject ) );
818 
819         switch( pObject->eObjKind )
820         {
821             case( SGA_OBJ_BMP ):
822             case( SGA_OBJ_ANIM ):
823             case( SGA_OBJ_INET ):
824             {
825                 String aFilterDummy;
826                 bRet = ( GalleryGraphicImport( aURL, rGraphic, aFilterDummy, bProgress ) != SGA_IMPORT_NONE );
827             }
828             break;
829 
830             case( SGA_OBJ_SVDRAW ):
831             {
832                 SvxGalleryDrawModel aModel;
833 
834                 if( aModel.GetModel() )
835                 {
836                     if( GetModel( nPos, *aModel.GetModel(), bProgress ) )
837                     {
838                         ImageMap aIMap;
839 
840                         if( CreateIMapGraphic( *aModel.GetModel(), rGraphic, aIMap ) )
841                             bRet = sal_True;
842                         else
843                         {
844                             VirtualDevice aVDev;
845                             aVDev.SetMapMode( MapMode( MAP_100TH_MM ) );
846                             FmFormView aView( aModel.GetModel(), &aVDev );
847 
848                             aView.hideMarkHandles();
849                             aView.ShowSdrPage(aView.GetModel()->GetPage(0));
850                             aView.MarkAll();
851                             rGraphic = aView.GetAllMarkedGraphic();
852                             bRet = sal_True;
853                         }
854                     }
855                 }
856             }
857             break;
858 
859             case( SGA_OBJ_SOUND ):
860             {
861                 SgaObject* pObj = AcquireObject( nPos );
862 
863                 if( pObj )
864                 {
865                     rGraphic = pObj->GetThumbBmp();
866                     //Bitmap aBmp( pObj->GetThumbBmp() );
867                     //aBmp.Replace( COL_LIGHTMAGENTA, COL_WHITE );
868                     //rGraphic = aBmp;
869                     ReleaseObject( pObj );
870                     bRet = sal_True;
871                 }
872             }
873             break;
874 
875             default:
876             break;
877         }
878     }
879 
880     return bRet;
881 }
882 
883 // -----------------------------------------------------------------------------
884 
885 sal_Bool GalleryTheme::InsertGraphic( const Graphic& rGraphic, sal_uIntPtr nInsertPos )
886 {
887     sal_Bool bRet = sal_False;
888 
889     if( rGraphic.GetType() != GRAPHIC_NONE )
890     {
891         sal_uIntPtr           nExportFormat = CVT_UNKNOWN;
892         const GfxLink   aGfxLink( ( (Graphic&) rGraphic ).GetLink() );
893 
894         if( aGfxLink.GetDataSize() )
895         {
896             switch( aGfxLink.GetType() )
897             {
898                 case( GFX_LINK_TYPE_EPS_BUFFER ): nExportFormat = CVT_SVM; break;
899                 case( GFX_LINK_TYPE_NATIVE_GIF ): nExportFormat = CVT_GIF; break;
900                 case( GFX_LINK_TYPE_NATIVE_JPG ): nExportFormat = CVT_JPG; break;
901                 case( GFX_LINK_TYPE_NATIVE_PNG ): nExportFormat = CVT_PNG; break;
902                 case( GFX_LINK_TYPE_NATIVE_TIF ): nExportFormat = CVT_TIF; break;
903                 case( GFX_LINK_TYPE_NATIVE_WMF ): nExportFormat = CVT_WMF; break;
904                 case( GFX_LINK_TYPE_NATIVE_MET ): nExportFormat = CVT_MET; break;
905                 case( GFX_LINK_TYPE_NATIVE_PCT ): nExportFormat = CVT_PCT; break;
906                 case( GFX_LINK_TYPE_NATIVE_SVG ): nExportFormat = CVT_SVG; break;
907                 default:
908                     break;
909             }
910         }
911         else
912         {
913             if( rGraphic.GetType() == GRAPHIC_BITMAP )
914             {
915                 if( rGraphic.IsAnimated() )
916                     nExportFormat = CVT_GIF;
917                 else
918                     nExportFormat = CVT_PNG;
919             }
920             else
921                 nExportFormat = CVT_SVM;
922         }
923 
924         const INetURLObject aURL( ImplCreateUniqueURL( SGA_OBJ_BMP, nExportFormat ) );
925         SvStream*           pOStm = ::utl::UcbStreamHelper::CreateStream( aURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_WRITE | STREAM_TRUNC );
926 
927         if( pOStm )
928         {
929             pOStm->SetVersion( SOFFICE_FILEFORMAT_50 );
930 
931             if( CVT_SVM == nExportFormat )
932             {
933                 GDIMetaFile aMtf( rGraphic.GetGDIMetaFile() );
934 
935                 aMtf.Write( *pOStm );
936                 bRet = ( pOStm->GetError() == ERRCODE_NONE );
937             }
938             else
939             {
940                 if( aGfxLink.GetDataSize() && aGfxLink.GetData() )
941                 {
942                     pOStm->Write( aGfxLink.GetData(), aGfxLink.GetDataSize() );
943                     bRet = ( pOStm->GetError() == ERRCODE_NONE );
944                 }
945                 else
946                     bRet = ( GraphicConverter::Export( *pOStm, rGraphic, nExportFormat ) == ERRCODE_NONE );
947             }
948 
949             delete pOStm;
950         }
951 
952         if( bRet )
953         {
954             const SgaObjectBmp aObjBmp( aURL.GetMainURL( INetURLObject::NO_DECODE ) );
955             InsertObject( aObjBmp, nInsertPos );
956         }
957     }
958 
959     return bRet;
960 }
961 
962 // -----------------------------------------------------------------------------
963 
964 sal_Bool GalleryTheme::GetModel( sal_uIntPtr nPos, SdrModel& rModel, sal_Bool )
965 {
966     const GalleryObject*    pObject = ImplGetGalleryObject( nPos );
967     sal_Bool                    bRet = sal_False;
968 
969     if( pObject && ( SGA_OBJ_SVDRAW == pObject->eObjKind ) )
970     {
971         const INetURLObject aURL( ImplGetURL( pObject ) );
972         SvStorageRef        xStor( GetSvDrawStorage() );
973 
974         if( xStor.Is() )
975         {
976             const String        aStmName( GetSvDrawStreamNameFromURL( aURL ) );
977             SvStorageStreamRef  xIStm( xStor->OpenSotStream( aStmName, STREAM_READ ) );
978 
979             if( xIStm.Is() && !xIStm->GetError() )
980             {
981                 xIStm->SetBufferSize( STREAMBUF_SIZE );
982                 bRet = GallerySvDrawImport( *xIStm, rModel );
983                 xIStm->SetBufferSize( 0L );
984             }
985         }
986     }
987 
988     return bRet;
989 }
990 
991 // -----------------------------------------------------------------------------
992 
993 sal_Bool GalleryTheme::InsertModel( const FmFormModel& rModel, sal_uIntPtr nInsertPos )
994 {
995     INetURLObject   aURL( ImplCreateUniqueURL( SGA_OBJ_SVDRAW ) );
996     SvStorageRef    xStor( GetSvDrawStorage() );
997     sal_Bool            bRet = sal_False;
998 
999     if( xStor.Is() )
1000     {
1001         const String        aStmName( GetSvDrawStreamNameFromURL( aURL ) );
1002         SvStorageStreamRef  xOStm( xStor->OpenSotStream( aStmName, STREAM_WRITE | STREAM_TRUNC ) );
1003 
1004         if( xOStm.Is() && !xOStm->GetError() )
1005         {
1006             SvMemoryStream  aMemStm( 65535, 65535 );
1007             FmFormModel*    pFormModel = (FmFormModel*) &rModel;
1008 
1009             pFormModel->BurnInStyleSheetAttributes();
1010 
1011             {
1012                 uno::Reference< io::XOutputStream > xDocOut( new utl::OOutputStreamWrapper( aMemStm ) );
1013 
1014                 if( xDocOut.is() )
1015                     SvxDrawingLayerExport( pFormModel, xDocOut );
1016             }
1017 
1018             aMemStm.Seek( 0 );
1019 
1020             xOStm->SetBufferSize( 16348 );
1021             GalleryCodec aCodec( *xOStm );
1022             aCodec.Write( aMemStm );
1023 
1024             if( !xOStm->GetError() )
1025             {
1026                 SgaObjectSvDraw aObjSvDraw( rModel, aURL );
1027                 bRet = InsertObject( aObjSvDraw, nInsertPos );
1028             }
1029 
1030             xOStm->SetBufferSize( 0L );
1031             xOStm->Commit();
1032         }
1033     }
1034 
1035     return bRet;
1036 }
1037 
1038 // -----------------------------------------------------------------------------
1039 
1040 sal_Bool GalleryTheme::GetModelStream( sal_uIntPtr nPos, SotStorageStreamRef& rxModelStream, sal_Bool )
1041 {
1042     const GalleryObject*    pObject = ImplGetGalleryObject( nPos );
1043     sal_Bool                    bRet = sal_False;
1044 
1045     if( pObject && ( SGA_OBJ_SVDRAW == pObject->eObjKind ) )
1046     {
1047         const INetURLObject aURL( ImplGetURL( pObject ) );
1048         SvStorageRef        xStor( GetSvDrawStorage() );
1049 
1050         if( xStor.Is() )
1051         {
1052             const String        aStmName( GetSvDrawStreamNameFromURL( aURL ) );
1053             SvStorageStreamRef  xIStm( xStor->OpenSotStream( aStmName, STREAM_READ ) );
1054 
1055             if( xIStm.Is() && !xIStm->GetError() )
1056             {
1057                 sal_uInt32 nVersion = 0;
1058 
1059                 xIStm->SetBufferSize( 16348 );
1060 
1061                 if( GalleryCodec::IsCoded( *xIStm, nVersion ) )
1062                 {
1063                     SvxGalleryDrawModel aModel;
1064 
1065                     if( aModel.GetModel() )
1066                     {
1067                         if( GallerySvDrawImport( *xIStm, *aModel.GetModel() ) )
1068                         {
1069                             aModel.GetModel()->BurnInStyleSheetAttributes();
1070 
1071                             {
1072                                 uno::Reference< io::XOutputStream > xDocOut( new utl::OOutputStreamWrapper( *rxModelStream ) );
1073 
1074                                 if( SvxDrawingLayerExport( aModel.GetModel(), xDocOut ) )
1075                                     rxModelStream->Commit();
1076                             }
1077                         }
1078 
1079                         bRet = ( rxModelStream->GetError() == ERRCODE_NONE );
1080                     }
1081                 }
1082 
1083                 xIStm->SetBufferSize( 0 );
1084             }
1085         }
1086     }
1087 
1088     return bRet;
1089 }
1090 
1091 // -----------------------------------------------------------------------------
1092 
1093 sal_Bool GalleryTheme::InsertModelStream( const SotStorageStreamRef& rxModelStream, sal_uIntPtr nInsertPos )
1094 {
1095     INetURLObject   aURL( ImplCreateUniqueURL( SGA_OBJ_SVDRAW ) );
1096     SvStorageRef    xStor( GetSvDrawStorage() );
1097     sal_Bool            bRet = sal_False;
1098 
1099     if( xStor.Is() )
1100     {
1101         const String        aStmName( GetSvDrawStreamNameFromURL( aURL ) );
1102         SvStorageStreamRef  xOStm( xStor->OpenSotStream( aStmName, STREAM_WRITE | STREAM_TRUNC ) );
1103 
1104         if( xOStm.Is() && !xOStm->GetError() )
1105         {
1106             GalleryCodec    aCodec( *xOStm );
1107             SvMemoryStream  aMemStm( 65535, 65535 );
1108 
1109             xOStm->SetBufferSize( 16348 );
1110             aCodec.Write( *rxModelStream );
1111 
1112             if( !xOStm->GetError() )
1113             {
1114                 xOStm->Seek( 0 );
1115                 SgaObjectSvDraw aObjSvDraw( *xOStm, aURL );
1116                 bRet = InsertObject( aObjSvDraw, nInsertPos );
1117             }
1118 
1119             xOStm->SetBufferSize( 0L );
1120             xOStm->Commit();
1121         }
1122     }
1123 
1124     return bRet;
1125 }
1126 
1127 // -----------------------------------------------------------------------------
1128 
1129 sal_Bool GalleryTheme::GetURL( sal_uIntPtr nPos, INetURLObject& rURL, sal_Bool )
1130 {
1131     const GalleryObject*    pObject = ImplGetGalleryObject( nPos );
1132     sal_Bool                    bRet = sal_False;
1133 
1134     if( pObject )
1135     {
1136         rURL = INetURLObject( ImplGetURL( pObject ) );
1137         bRet = sal_True;
1138     }
1139 
1140     return bRet;
1141 }
1142 
1143 // -----------------------------------------------------------------------------
1144 
1145 sal_Bool GalleryTheme::InsertURL( const INetURLObject& rURL, sal_uIntPtr nInsertPos )
1146 {
1147     Graphic         aGraphic;
1148     String          aFormat;
1149     SgaObject*      pNewObj = NULL;
1150     const sal_uInt16    nImportRet = GalleryGraphicImport( rURL, aGraphic, aFormat );
1151     sal_Bool            bRet = sal_False;
1152 
1153     if( nImportRet != SGA_IMPORT_NONE )
1154     {
1155         if ( SGA_IMPORT_INET == nImportRet )
1156             pNewObj = (SgaObject*) new SgaObjectINet( aGraphic, rURL, aFormat );
1157         else if ( aGraphic.IsAnimated() )
1158             pNewObj = (SgaObject*) new SgaObjectAnim( aGraphic, rURL, aFormat );
1159         else
1160             pNewObj = (SgaObject*) new SgaObjectBmp( aGraphic, rURL, aFormat );
1161     }
1162     else if( ::avmedia::MediaWindow::isMediaURL( rURL.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS ) ) )
1163         pNewObj = (SgaObject*) new SgaObjectSound( rURL );
1164 
1165     if( pNewObj && InsertObject( *pNewObj, nInsertPos ) )
1166         bRet = sal_True;
1167 
1168     delete pNewObj;
1169 
1170     return bRet;
1171 }
1172 
1173 // -----------------------------------------------------------------------------
1174 
1175 sal_Bool GalleryTheme::InsertFileOrDirURL( const INetURLObject& rFileOrDirURL, sal_uIntPtr nInsertPos )
1176 {
1177     INetURLObject                   aURL;
1178     ::std::vector< INetURLObject >  aURLVector;
1179     sal_Bool                            bRet = sal_False;
1180 
1181     try
1182     {
1183         ::ucbhelper::Content         aCnt( rFileOrDirURL.GetMainURL( INetURLObject::NO_DECODE ), uno::Reference< ucb::XCommandEnvironment >() );
1184         sal_Bool        bFolder = false;
1185 
1186         aCnt.getPropertyValue( OUString::createFromAscii( "IsFolder" ) ) >>= bFolder;
1187 
1188         if( bFolder )
1189         {
1190             uno::Sequence< OUString > aProps( 1 );
1191             aProps.getArray()[ 0 ] = OUString::createFromAscii( "Url" );
1192             uno::Reference< sdbc::XResultSet > xResultSet( aCnt.createCursor( aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY ) );
1193             uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
1194             if( xContentAccess.is() )
1195             {
1196                 while( xResultSet->next() )
1197                 {
1198                     aURL.SetSmartURL( xContentAccess->queryContentIdentifierString() );
1199                     aURLVector.push_back( aURL );
1200                 }
1201             }
1202         }
1203         else
1204             aURLVector.push_back( rFileOrDirURL );
1205     }
1206     catch( const ucb::ContentCreationException& )
1207     {
1208     }
1209     catch( const uno::RuntimeException& )
1210     {
1211     }
1212     catch( const uno::Exception& )
1213     {
1214     }
1215 
1216     ::std::vector< INetURLObject >::const_iterator aIter( aURLVector.begin() ), aEnd( aURLVector.end() );
1217 
1218     while( aIter != aEnd )
1219         bRet = bRet || InsertURL( *aIter++, nInsertPos );
1220 
1221     return bRet;
1222 }
1223 
1224 // -----------------------------------------------------------------------------
1225 
1226 sal_Bool GalleryTheme::InsertTransferable( const uno::Reference< datatransfer::XTransferable >& rxTransferable, sal_uIntPtr nInsertPos )
1227 {
1228     sal_Bool bRet = sal_False;
1229 
1230     if( rxTransferable.is() )
1231     {
1232         TransferableDataHelper  aDataHelper( rxTransferable );
1233         Graphic*                pGraphic = NULL;
1234 
1235         if( aDataHelper.HasFormat( SOT_FORMATSTR_ID_DRAWING ) )
1236         {
1237             SotStorageStreamRef xModelStm;
1238 
1239             if( aDataHelper.GetSotStorageStream( SOT_FORMATSTR_ID_DRAWING, xModelStm ) )
1240                 bRet = InsertModelStream( xModelStm, nInsertPos );
1241         }
1242         else if( aDataHelper.HasFormat( SOT_FORMAT_FILE_LIST ) ||
1243                  aDataHelper.HasFormat( FORMAT_FILE ) )
1244         {
1245             FileList aFileList;
1246 
1247             if( aDataHelper.HasFormat( SOT_FORMAT_FILE_LIST ) )
1248                 aDataHelper.GetFileList( SOT_FORMAT_FILE_LIST, aFileList );
1249             else
1250             {
1251                 String aFile;
1252 
1253                 aDataHelper.GetString( FORMAT_FILE, aFile );
1254 
1255                 if( aFile.Len() )
1256                     aFileList.AppendFile( aFile );
1257             }
1258 
1259             for( sal_uInt32 i = 0, nCount = aFileList.Count(); i < nCount; ++i )
1260             {
1261                 const String    aFile( aFileList.GetFile( i ) );
1262                 INetURLObject   aURL( aFile );
1263 
1264                 if( aURL.GetProtocol() == INET_PROT_NOT_VALID )
1265                 {
1266                     String aLocalURL;
1267 
1268                     if( ::utl::LocalFileHelper::ConvertPhysicalNameToURL( aFile, aLocalURL ) )
1269                         aURL = INetURLObject( aLocalURL );
1270                 }
1271 
1272                 if( aURL.GetProtocol() != INET_PROT_NOT_VALID )
1273                     bRet = InsertFileOrDirURL( aURL, nInsertPos );
1274             }
1275         }
1276         else
1277         {
1278             Graphic aGraphic;
1279             sal_uIntPtr nFormat = 0;
1280 
1281             if( aDataHelper.HasFormat( SOT_FORMATSTR_ID_SVXB ) )
1282                 nFormat = SOT_FORMATSTR_ID_SVXB;
1283             else if( aDataHelper.HasFormat( FORMAT_GDIMETAFILE ) )
1284                 nFormat = FORMAT_GDIMETAFILE;
1285             else if( aDataHelper.HasFormat( FORMAT_BITMAP ) )
1286                 nFormat = FORMAT_BITMAP;
1287 
1288             if( nFormat && aDataHelper.GetGraphic( nFormat, aGraphic ) )
1289                 pGraphic = new Graphic( aGraphic );
1290         }
1291 
1292         if( pGraphic )
1293         {
1294             bRet = sal_False;
1295 
1296             if( aDataHelper.HasFormat( SOT_FORMATSTR_ID_SVIM ) )
1297             {
1298 
1299                 ImageMap aImageMap;
1300 
1301                 // according to KA we don't need a BaseURL here
1302                 if( aDataHelper.GetImageMap( SOT_FORMATSTR_ID_SVIM, aImageMap ) )
1303                 {
1304                     SvxGalleryDrawModel aModel;
1305 
1306                     if( aModel.GetModel() )
1307                     {
1308                         SgaUserDataFactory  aFactory;
1309 
1310                         SdrPage*    pPage = aModel.GetModel()->GetPage(0);
1311                         SdrGrafObj* pGrafObj = new SdrGrafObj( *pGraphic );
1312 
1313                         pGrafObj->InsertUserData( new SgaIMapInfo( aImageMap ) );
1314                         pPage->InsertObject( pGrafObj );
1315                         bRet = InsertModel( *aModel.GetModel(), nInsertPos );
1316                     }
1317                 }
1318             }
1319 
1320             if( !bRet )
1321                 bRet = InsertGraphic( *pGraphic, nInsertPos );
1322 
1323             delete pGraphic;
1324         }
1325     }
1326 
1327     return bRet;
1328 }
1329 
1330 // -----------------------------------------------------------------------------
1331 
1332 void GalleryTheme::CopyToClipboard( Window* pWindow, sal_uIntPtr nPos )
1333 {
1334     GalleryTransferable* pTransferable = new GalleryTransferable( this, nPos, false );
1335     pTransferable->CopyToClipboard( pWindow );
1336 }
1337 
1338 // -----------------------------------------------------------------------------
1339 
1340 void GalleryTheme::StartDrag( Window* pWindow, sal_uIntPtr nPos )
1341 {
1342     GalleryTransferable* pTransferable = new GalleryTransferable( this, nPos, true );
1343     pTransferable->StartDrag( pWindow, DND_ACTION_COPY | DND_ACTION_LINK );
1344 }
1345 
1346 // -----------------------------------------------------------------------------
1347 
1348 SvStream& GalleryTheme::WriteData( SvStream& rOStm ) const
1349 {
1350     const INetURLObject aRelURL1( GetParent()->GetRelativeURL() );
1351     const INetURLObject aRelURL2( GetParent()->GetUserURL() );
1352     INetURLObject       aNewURL, aTempURL;
1353     sal_uInt32          nCount = GetObjectCount();
1354     sal_Bool                bRel;
1355 
1356     rOStm << (sal_uInt16) 0x0004;
1357     rOStm << ByteString( GetRealName(), RTL_TEXTENCODING_UTF8 );
1358     rOStm << nCount << (sal_uInt16) gsl_getSystemTextEncoding();
1359 
1360     for( sal_uInt32 i = 0; i < nCount; i++ )
1361     {
1362         const GalleryObject* pObj = ImplGetGalleryObject( i );
1363         String               aPath;
1364 
1365         if( SGA_OBJ_SVDRAW == pObj->eObjKind )
1366         {
1367             aPath = GetSvDrawStreamNameFromURL( pObj->aURL );
1368             bRel = sal_False;
1369         }
1370         else
1371         {
1372             aPath = pObj->aURL.GetMainURL( INetURLObject::NO_DECODE );
1373             bRel = ( ( aPath.Erase( sal::static_int_cast< xub_StrLen >( aRelURL1.GetMainURL( INetURLObject::NO_DECODE ).getLength() ) ) ) == String(aRelURL1.GetMainURL( INetURLObject::NO_DECODE ) ));
1374 
1375             if( bRel && ( pObj->aURL.GetMainURL( INetURLObject::NO_DECODE ).getLength() > ( aRelURL1.GetMainURL( INetURLObject::NO_DECODE ).getLength() + 1 ) ) )
1376             {
1377                 aPath = pObj->aURL.GetMainURL( INetURLObject::NO_DECODE );
1378                 aPath = aPath.Erase( 0, sal::static_int_cast< xub_StrLen >( aRelURL1.GetMainURL( INetURLObject::NO_DECODE ).getLength() ) );
1379             }
1380             else
1381             {
1382                 aPath = pObj->aURL.GetMainURL( INetURLObject::NO_DECODE );
1383                 bRel = ( ( aPath.Erase( sal::static_int_cast< xub_StrLen >( aRelURL2.GetMainURL( INetURLObject::NO_DECODE ).getLength() ) ) ) == String(aRelURL2.GetMainURL( INetURLObject::NO_DECODE ) ));
1384 
1385                 if( bRel && ( pObj->aURL.GetMainURL( INetURLObject::NO_DECODE ).getLength() > ( aRelURL2.GetMainURL( INetURLObject::NO_DECODE ).getLength() + 1 ) ) )
1386                 {
1387                     aPath = pObj->aURL.GetMainURL( INetURLObject::NO_DECODE );
1388                     aPath = aPath.Erase( 0, sal::static_int_cast< xub_StrLen >( aRelURL2.GetMainURL( INetURLObject::NO_DECODE ).getLength() ) );
1389                 }
1390                 else
1391                     aPath = pObj->aURL.GetMainURL( INetURLObject::NO_DECODE );
1392             }
1393         }
1394 
1395         aPath.SearchAndReplace(m_aDestDir, String());
1396         rOStm << bRel << ByteString( aPath, RTL_TEXTENCODING_UTF8 ) << pObj->nOffset << (sal_uInt16) pObj->eObjKind;
1397     }
1398 
1399     // neuerdings wird ein 512-Byte-Reservepuffer gechrieben;
1400     // um diesen zu erkennen werden zwei sal_uIntPtr-Ids geschrieben
1401     rOStm << COMPAT_FORMAT( 'G', 'A', 'L', 'R' ) << COMPAT_FORMAT( 'E', 'S', 'R', 'V' );
1402 
1403     const long      nReservePos = rOStm.Tell();
1404     VersionCompat*  pCompat = new VersionCompat( rOStm, STREAM_WRITE, 2 );
1405 
1406     rOStm << (sal_uInt32) GetId() << IsThemeNameFromResource(); // ab Version 2
1407 
1408     delete pCompat;
1409 
1410     // Rest des Puffers auffuellen
1411     const long  nRest = Max( 512L - ( (long) rOStm.Tell() - nReservePos ), 0L );
1412 
1413     if( nRest )
1414     {
1415         char* pReserve = new char[ nRest ];
1416         memset( pReserve, 0, nRest );
1417         rOStm.Write( pReserve, nRest );
1418         delete[] pReserve;
1419     }
1420 
1421     return rOStm;
1422 }
1423 
1424 // ------------------------------------------------------------------------
1425 
1426 SvStream& GalleryTheme::ReadData( SvStream& rIStm )
1427 {
1428     sal_uInt32          nCount;
1429     sal_uInt16          nVersion;
1430     ByteString          aTmpStr;
1431     String              aThemeName;
1432     rtl_TextEncoding    nTextEncoding;
1433 
1434     aImportName = String();
1435     rIStm >> nVersion >> aTmpStr >> nCount;
1436 
1437     if( nVersion >= 0x0004 )
1438     {
1439         sal_uInt16 nTmp16;
1440         rIStm >> nTmp16;
1441         nTextEncoding = (rtl_TextEncoding) nTmp16;
1442     }
1443     else
1444         nTextEncoding = RTL_TEXTENCODING_UTF8;
1445 
1446     aThemeName = String( aTmpStr.GetBuffer(), nTextEncoding );
1447 
1448     if( nCount <= ( 1L << 14 ) )
1449     {
1450         GalleryObject*  pObj;
1451         INetURLObject   aRelURL1( GetParent()->GetRelativeURL() );
1452         INetURLObject   aRelURL2( GetParent()->GetUserURL() );
1453         sal_uInt32      nId1, nId2;
1454         sal_Bool            bRel;
1455 
1456         for( pObj = aObjectList.First(); pObj; pObj = aObjectList.Next() )
1457         {
1458             Broadcast( GalleryHint( GALLERY_HINT_CLOSE_OBJECT, GetName(), reinterpret_cast< sal_uIntPtr >( pObj ) ) );
1459             delete pObj;
1460             Broadcast( GalleryHint( GALLERY_HINT_OBJECT_REMOVED, GetName(), reinterpret_cast< sal_uIntPtr >( pObj ) ) );
1461         }
1462 
1463         aObjectList.Clear();
1464 
1465         for( sal_uInt32 i = 0; i < nCount; i++ )
1466         {
1467             pObj = new GalleryObject;
1468 
1469             ByteString  aTempFileName;
1470             String      aFileName;
1471             String      aPath;
1472             sal_uInt16  nTemp;
1473 
1474             rIStm >> bRel >> aTempFileName >> pObj->nOffset;
1475             rIStm >> nTemp; pObj->eObjKind = (SgaObjKind) nTemp;
1476 
1477             aFileName = String( aTempFileName.GetBuffer(), gsl_getSystemTextEncoding() );
1478 
1479             if( bRel )
1480             {
1481                 aFileName.SearchAndReplaceAll( '\\', '/' );
1482                 aPath = aRelURL1.GetMainURL( INetURLObject::NO_DECODE );
1483 
1484                 if( aFileName.GetChar( 0 ) != '/' )
1485                         aPath += '/';
1486 
1487                 aPath += aFileName;
1488 
1489                 pObj->aURL = INetURLObject( aPath );
1490 
1491                 if( !FileExists( pObj->aURL ) )
1492                 {
1493                     aPath = aRelURL2.GetMainURL( INetURLObject::NO_DECODE );
1494 
1495                     if( aFileName.GetChar( 0 ) != '/' )
1496                         aPath += '/';
1497 
1498                     aPath += aFileName;
1499 
1500                     // assign this URL, even in the case it is not valid (#94482)
1501                     pObj->aURL = INetURLObject( aPath );
1502                 }
1503             }
1504             else
1505             {
1506                 if( SGA_OBJ_SVDRAW == pObj->eObjKind )
1507                 {
1508                     const static String aBaseURLStr( RTL_CONSTASCII_USTRINGPARAM( "gallery/svdraw/" ) );
1509 
1510                     String aDummyURL( aBaseURLStr );
1511                     pObj->aURL = INetURLObject( aDummyURL += aFileName, INET_PROT_PRIV_SOFFICE );
1512                 }
1513                 else
1514                 {
1515                     String aLocalURL;
1516 
1517                     pObj->aURL = INetURLObject( aFileName );
1518 
1519                     if( ( pObj->aURL.GetProtocol() == INET_PROT_NOT_VALID ) &&
1520                         ::utl::LocalFileHelper::ConvertPhysicalNameToURL( aFileName, aLocalURL ) )
1521                     {
1522                         pObj->aURL = INetURLObject( aLocalURL );
1523                     }
1524                 }
1525             }
1526 
1527             aObjectList.Insert( pObj, LIST_APPEND );
1528         }
1529 
1530         rIStm >> nId1 >> nId2;
1531 
1532         // in neueren Versionen befindet sich am Ende ein 512-Byte-Reservepuffer;
1533         // die Daten befinden sich am Anfang dieses Puffers und
1534         // sind durch eine VersionCompat geklammert
1535         if( !rIStm.IsEof() &&
1536             nId1 == COMPAT_FORMAT( 'G', 'A', 'L', 'R' ) &&
1537             nId2 == COMPAT_FORMAT( 'E', 'S', 'R', 'V' ) )
1538         {
1539             VersionCompat*  pCompat = new VersionCompat( rIStm, STREAM_READ );
1540             sal_uInt32      nTemp32;
1541             sal_Bool            bThemeNameFromResource = sal_False;
1542 
1543             rIStm >> nTemp32;
1544 
1545             if( pCompat->GetVersion() >= 2 )
1546             {
1547                 rIStm >> bThemeNameFromResource;
1548             }
1549 
1550             SetId( nTemp32, bThemeNameFromResource );
1551             delete pCompat;
1552         }
1553     }
1554     else
1555         rIStm.SetError( SVSTREAM_READ_ERROR );
1556 
1557     ImplSetModified( sal_False );
1558 
1559     return rIStm;
1560 }
1561 
1562 // ------------------------------------------------------------------------
1563 
1564 SvStream& operator<<( SvStream& rOut, const GalleryTheme& rTheme )
1565 {
1566     return rTheme.WriteData( rOut );
1567 }
1568 
1569 // ------------------------------------------------------------------------
1570 
1571 SvStream& operator>>( SvStream& rIn, GalleryTheme& rTheme )
1572 {
1573     return rTheme.ReadData( rIn );
1574 }
1575 
1576 void GalleryTheme::ImplSetModified( sal_Bool bModified )
1577 { pThm->SetModified( bModified ); }
1578 
1579 const String& GalleryTheme::GetRealName() const { return pThm->GetThemeName(); }
1580 const INetURLObject& GalleryTheme::GetThmURL() const { return pThm->GetThmURL(); }
1581 const INetURLObject& GalleryTheme::GetSdgURL() const { return pThm->GetSdgURL(); }
1582 const INetURLObject& GalleryTheme::GetSdvURL() const { return pThm->GetSdvURL(); }
1583 sal_uInt32 GalleryTheme::GetId() const { return pThm->GetId(); }
1584 void GalleryTheme::SetId( sal_uInt32 nNewId, sal_Bool bResetThemeName ) { pThm->SetId( nNewId, bResetThemeName ); }
1585 sal_Bool GalleryTheme::IsThemeNameFromResource() const { return pThm->IsNameFromResource(); }
1586 sal_Bool GalleryTheme::IsImported() const { return pThm->IsImported(); }
1587 sal_Bool GalleryTheme::IsReadOnly() const { return pThm->IsReadOnly(); }
1588 sal_Bool GalleryTheme::IsDefault() const { return pThm->IsDefault(); }
1589 sal_Bool GalleryTheme::IsModified() const { return pThm->IsModified(); }
1590 const String& GalleryTheme::GetName() const { return IsImported() ? aImportName : pThm->GetThemeName(); }
1591 
1592 void GalleryTheme::InsertAllThemes( ListBox& rListBox )
1593 {
1594     for( sal_uInt16 i = RID_GALLERYSTR_THEME_FIRST; i <= RID_GALLERYSTR_THEME_LAST; i++ )
1595         rListBox.InsertEntry( String( GAL_RESID( i ) ) );
1596 }
1597 
1598