1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: galmisc.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
34 #include <unotools/streamwrap.hxx>
35 #include <unotools/ucbstreamhelper.hxx>
36 #include <unotools/processfactory.hxx>
37 #include <ucbhelper/content.hxx>
38 #include <tools/resmgr.hxx>
39 #include <tools/urlobj.hxx>
40 #include <svtools/solar.hrc>
41 #include <svtools/urihelper.hxx>
42 #include <svtools/filter.hxx>
43 #include <svtools/itempool.hxx>
44 #include <sfx2/docfile.hxx>
45 #include <avmedia/mediawindow.hxx>
46 #include <vcl/svapp.hxx>
49 #include <svx/svdpage.hxx>
50 #include <svx/svdograf.hxx>
51 #include <svx/fmmodel.hxx>
52 #include <svx/fmview.hxx>
53 #include <svx/unomodel.hxx>
55 #include "gallery.hrc"
56 #include "gallery1.hxx"
57 #include "galtheme.hxx"
58 #include "galmisc.hxx"
59 #include <com/sun/star/sdbc/XResultSet.hpp>
60 #include <com/sun/star/ucb/XContentAccess.hpp>
61 #include <com/sun/star/ucb/TransferInfo.hpp>
62 #include <com/sun/star/ucb/NameClash.hpp>
68 using namespace ::rtl
;
69 using namespace ::com::sun::star
;
75 ResMgr
* GetGalleryResMgr()
77 static ResMgr
* pGalleryResMgr
= NULL
;
81 ByteString
aResMgrName( "gal" );
82 pGalleryResMgr
= ResMgr::CreateResMgr(
83 aResMgrName
.GetBuffer(), Application::GetSettings().GetUILocale() );
86 return pGalleryResMgr
;
89 // -------------------------
90 // - GalleryResGetBitmapEx -
91 // -------------------------
93 BitmapEx
GalleryResGetBitmapEx( sal_uInt32 nId
)
95 BitmapEx
aBmpEx( GAL_RESID( nId
) );
97 if( !aBmpEx
.IsTransparent() )
98 aBmpEx
= BitmapEx( aBmpEx
.GetBitmap(), COL_LIGHTMAGENTA
);
103 // ----------------------
104 // - SgaUserDataFactory -
105 // ----------------------
107 IMPL_LINK( SgaUserDataFactory
, MakeUserData
, SdrObjFactory
*, pObjFactory
)
109 if ( pObjFactory
->nInventor
== IV_IMAPINFO
&& pObjFactory
->nIdentifier
== ID_IMAPINFO
)
110 pObjFactory
->pNewData
= new SgaIMapInfo
;
115 // ------------------------
116 // - GalleryGraphicImport -
117 // ------------------------
119 USHORT
GalleryGraphicImport( const INetURLObject
& rURL
, Graphic
& rGraphic
,
120 String
& rFilterName
, BOOL bShowProgress
)
122 USHORT nRet
= SGA_IMPORT_NONE
;
123 SfxMedium
aMedium( rURL
.GetMainURL( INetURLObject::NO_DECODE
), STREAM_READ
, TRUE
);
128 SvStream
* pIStm
= aMedium
.GetInStream();
132 GraphicFilter
* pGraphicFilter
= GetGrfFilter();
133 GalleryProgress
* pProgress
= bShowProgress
? new GalleryProgress( pGraphicFilter
) : NULL
;
136 if( !pGraphicFilter
->ImportGraphic( rGraphic
, rURL
.GetMainURL( INetURLObject::NO_DECODE
), *pIStm
, GRFILTER_FORMAT_DONTKNOW
, &nFormat
) )
138 rFilterName
= pGraphicFilter
->GetImportFormatName( nFormat
);
139 nRet
= SGA_IMPORT_FILE
;
148 // -----------------------
149 // - GallerySvDrawImport -
150 // -----------------------
152 BOOL
GallerySvDrawImport( SvStream
& rIStm
, SdrModel
& rModel
)
157 if( GalleryCodec::IsCoded( rIStm
, nVersion
) )
159 SvMemoryStream
aMemStm( 65535, 65535 );
160 GalleryCodec
aCodec( rIStm
);
162 aCodec
.Read( aMemStm
);
167 DBG_ERROR( "staroffice binary file formats are no longer supported inside the gallery!" );
170 else if( 2 == nVersion
)
172 // recall to read as XML
173 bRet
= GallerySvDrawImport( aMemStm
, rModel
);
179 uno::Reference
< io::XInputStream
> xInputStream( new utl::OInputStreamWrapper( rIStm
) );
181 rModel
.GetItemPool().SetDefaultMetric( SFX_MAPUNIT_100TH_MM
);
182 uno::Reference
< lang::XComponent
> xComponent
;
184 bRet
= SvxDrawingLayerImport( &rModel
, xInputStream
, xComponent
, "com.sun.star.comp.Draw.XMLOasisImporter" );
185 if( !bRet
|| (rModel
.GetPageCount() == 0) )
188 bRet
= SvxDrawingLayerImport( &rModel
, xInputStream
, xComponent
, "com.sun.star.comp.Draw.XMLImporter" );
196 // ---------------------
197 // - CreateIMapGraphic -
198 // ---------------------
200 BOOL
CreateIMapGraphic( const FmFormModel
& rModel
, Graphic
& rGraphic
, ImageMap
& rImageMap
)
204 if ( rModel
.GetPageCount() )
206 const SdrPage
* pPage
= rModel
.GetPage( 0 );
207 const SdrObject
* pObj
= pPage
->GetObj( 0 );
209 if ( pPage
->GetObjCount() == 1 && pObj
->ISA( SdrGrafObj
) )
211 const USHORT nCount
= pObj
->GetUserDataCount();
213 // gibt es in den User-Daten eine IMap-Information?
214 for ( USHORT i
= 0; i
< nCount
; i
++ )
216 const SdrObjUserData
* pUserData
= pObj
->GetUserData( i
);
218 if ( ( pUserData
->GetInventor() == IV_IMAPINFO
) && ( pUserData
->GetId() == ID_IMAPINFO
) )
220 rGraphic
= ( (SdrGrafObj
*) pObj
)->GetGraphic();
221 rImageMap
= ( (SgaIMapInfo
*) pUserData
)->GetImageMap();
232 // --------------------
233 // - GetReducedString -
234 // --------------------
236 String
GetReducedString( const INetURLObject
& rURL
, ULONG nMaxLen
)
238 String
aReduced( rURL
.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS
) );
240 aReduced
= aReduced
.GetToken( aReduced
.GetTokenCount( '/' ) - 1, '/' );
242 if( INET_PROT_PRIV_SOFFICE
!= rURL
.GetProtocol() )
244 sal_Unicode aDelimiter
;
245 const String
aPath( rURL
.getFSysPath( INetURLObject::FSYS_DETECT
, &aDelimiter
) );
246 const String
aName( aReduced
);
248 if( aPath
.Len() > nMaxLen
)
250 aReduced
= aPath
.Copy( 0, (USHORT
)( nMaxLen
- aName
.Len() - 4 ) );
251 aReduced
+= String( RTL_CONSTASCII_USTRINGPARAM( "..." ) );
252 aReduced
+= aDelimiter
;
262 // -----------------------------------------------------------------------------
264 String
GetSvDrawStreamNameFromURL( const INetURLObject
& rSvDrawObjURL
)
268 if( rSvDrawObjURL
.GetProtocol() == INET_PROT_PRIV_SOFFICE
&&
269 String(rSvDrawObjURL
.GetMainURL( INetURLObject::NO_DECODE
)).GetTokenCount( '/' ) == 3 )
271 aRet
= String(rSvDrawObjURL
.GetMainURL( INetURLObject::NO_DECODE
)).GetToken( 2, '/' );
277 // -----------------------------------------------------------------------------
279 BOOL
FileExists( const INetURLObject
& rURL
)
283 if( rURL
.GetProtocol() != INET_PROT_NOT_VALID
)
287 ::ucbhelper::Content
aCnt( rURL
.GetMainURL( INetURLObject::NO_DECODE
), uno::Reference
< ucb::XCommandEnvironment
>() );
290 aCnt
.getPropertyValue( OUString::createFromAscii( "Title" ) ) >>= aTitle
;
291 bRet
= ( aTitle
.getLength() > 0 );
293 catch( const ucb::ContentCreationException
& )
296 catch( const uno::RuntimeException
& )
299 catch( const uno::Exception
& )
307 // -----------------------------------------------------------------------------
309 BOOL
CreateDir( const INetURLObject
& rURL
)
311 BOOL bRet
= FileExists( rURL
);
317 uno::Reference
< ucb::XCommandEnvironment
> aCmdEnv
;
318 INetURLObject
aNewFolderURL( rURL
);
319 INetURLObject
aParentURL( aNewFolderURL
); aParentURL
.removeSegment();
320 ::ucbhelper::Content
aParent( aParentURL
.GetMainURL( INetURLObject::NO_DECODE
), aCmdEnv
);
321 uno::Sequence
< OUString
> aProps( 1 );
322 uno::Sequence
< uno::Any
> aValues( 1 );
324 aProps
.getArray()[ 0 ] = OUString::createFromAscii( "Title" );
325 aValues
.getArray()[ 0 ] = uno::makeAny( OUString( aNewFolderURL
.GetName() ) );
327 ::ucbhelper::Content
aContent( aNewFolderURL
.GetMainURL( INetURLObject::NO_DECODE
), aCmdEnv
);
328 bRet
= aParent
.insertNewContent( OUString::createFromAscii( "application/vnd.sun.staroffice.fsys-folder" ), aProps
, aValues
, aContent
);
330 catch( const ucb::ContentCreationException
& )
333 catch( const uno::RuntimeException
& )
336 catch( const uno::Exception
& )
344 // -----------------------------------------------------------------------------
346 BOOL
CopyFile( const INetURLObject
& rSrcURL
, const INetURLObject
& rDstURL
)
352 ::ucbhelper::Content
aDestPath( rDstURL
.GetMainURL( INetURLObject::NO_DECODE
), uno::Reference
< ucb::XCommandEnvironment
>() );
354 aDestPath
.executeCommand( OUString::createFromAscii( "transfer" ),
355 uno::makeAny( ucb::TransferInfo( sal_False
, rSrcURL
.GetMainURL( INetURLObject::NO_DECODE
),
356 rDstURL
.GetName(), ucb::NameClash::OVERWRITE
) ) );
359 catch( const ucb::ContentCreationException
& )
362 catch( const uno::RuntimeException
& )
365 catch( const uno::Exception
& )
372 // -----------------------------------------------------------------------------
374 BOOL
KillFile( const INetURLObject
& rURL
)
376 BOOL bRet
= FileExists( rURL
);
382 ::ucbhelper::Content
aCnt( rURL
.GetMainURL( INetURLObject::NO_DECODE
), uno::Reference
< ucb::XCommandEnvironment
>() );
383 aCnt
.executeCommand( OUString::createFromAscii( "delete" ), uno::makeAny( sal_Bool( sal_True
) ) );
385 catch( const ucb::ContentCreationException
& )
389 catch( const uno::RuntimeException
& )
393 catch( const uno::Exception
& )
402 // -------------------
403 // - GalleryProgress -
404 // -------------------
406 GalleryProgress::GalleryProgress( GraphicFilter
* pFilter
) :
409 uno::Reference
< lang::XMultiServiceFactory
> xMgr( ::utl::getProcessServiceFactory() );
413 uno::Reference
< awt::XProgressMonitor
> xMonitor( xMgr
->createInstance(
414 ::rtl::OUString::createFromAscii( "com.sun.star.awt.XProgressMonitor" ) ),
419 mxProgressBar
= uno::Reference
< awt::XProgressBar
>( xMonitor
, uno::UNO_QUERY
);
421 if( mxProgressBar
.is() )
423 String aProgressText
;
427 aProgressText
= String( GAL_RESID( RID_SVXSTR_GALLERY_FILTER
) );
428 // mpFilter->SetUpdatePercentHdl( LINK( this, GalleryProgress, Update ) ); // sj: progress wasn't working up from SO7 at all
429 // // so I am removing this. The gallery progress should
430 // // be changed to use the XStatusIndicator instead of XProgressMonitor
433 aProgressText
= String( RTL_CONSTASCII_USTRINGPARAM( "Gallery" ) );
435 xMonitor
->addText( String( RTL_CONSTASCII_USTRINGPARAM( "Gallery" ) ), aProgressText
, sal_False
) ;
436 mxProgressBar
->setRange( 0, GALLERY_PROGRESS_RANGE
);
442 // ------------------------------------------------------------------------
444 GalleryProgress::~GalleryProgress()
447 // mpFilter->SetUpdatePercentHdl( Link() );
450 // ------------------------------------------------------------------------
452 void GalleryProgress::Update( ULONG nVal
, ULONG nMaxVal
)
454 if( mxProgressBar
.is() && nMaxVal
)
455 mxProgressBar
->setValue( Min( (ULONG
)( (double) nVal
/ nMaxVal
* GALLERY_PROGRESS_RANGE
), (ULONG
) GALLERY_PROGRESS_RANGE
) );
458 // -----------------------
459 // - GalleryTransferable -
460 // -----------------------
462 GalleryTransferable::GalleryTransferable( GalleryTheme
* pTheme
, ULONG nObjectPos
, bool bLazy
) :
464 meObjectKind( mpTheme
->GetObjectKind( nObjectPos
) ),
465 mnObjectPos( nObjectPos
),
466 mpGraphicObject( NULL
),
473 // ------------------------------------------------------------------------
475 GalleryTransferable::~GalleryTransferable()
479 // ------------------------------------------------------------------------
481 void GalleryTransferable::InitData( bool bLazy
)
483 switch( meObjectKind
)
485 case( SGA_OBJ_SVDRAW
):
489 if( !mpGraphicObject
)
493 if( mpTheme
->GetGraphic( mnObjectPos
, aGraphic
) )
494 mpGraphicObject
= new GraphicObject( aGraphic
);
497 if( !mxModelStream
.Is() )
499 mxModelStream
= new SotStorageStream( String() );
500 mxModelStream
->SetBufferSize( 16348 );
502 if( !mpTheme
->GetModelStream( mnObjectPos
, mxModelStream
) )
503 mxModelStream
.Clear();
505 mxModelStream
->Seek( 0 );
511 case( SGA_OBJ_ANIM
):
513 case( SGA_OBJ_INET
):
514 case( SGA_OBJ_SOUND
):
518 mpURL
= new INetURLObject
;
520 if( !mpTheme
->GetURL( mnObjectPos
, *mpURL
) )
521 delete mpURL
, mpURL
= NULL
;
524 if( ( SGA_OBJ_SOUND
!= meObjectKind
) && !mpGraphicObject
)
528 if( mpTheme
->GetGraphic( mnObjectPos
, aGraphic
) )
529 mpGraphicObject
= new GraphicObject( aGraphic
);
535 DBG_ERROR( "GalleryTransferable::GalleryTransferable: invalid object type" );
540 // ------------------------------------------------------------------------
542 void GalleryTransferable::AddSupportedFormats()
544 if( SGA_OBJ_SVDRAW
== meObjectKind
)
546 AddFormat( SOT_FORMATSTR_ID_DRAWING
);
547 AddFormat( SOT_FORMATSTR_ID_SVXB
);
548 AddFormat( FORMAT_GDIMETAFILE
);
549 AddFormat( FORMAT_BITMAP
);
554 AddFormat( FORMAT_FILE
);
556 if( mpGraphicObject
)
558 AddFormat( SOT_FORMATSTR_ID_SVXB
);
560 if( mpGraphicObject
->GetType() == GRAPHIC_GDIMETAFILE
)
562 AddFormat( FORMAT_GDIMETAFILE
);
563 AddFormat( FORMAT_BITMAP
);
567 AddFormat( FORMAT_BITMAP
);
568 AddFormat( FORMAT_GDIMETAFILE
);
574 // ------------------------------------------------------------------------
576 sal_Bool
GalleryTransferable::GetData( const datatransfer::DataFlavor
& rFlavor
)
578 sal_uInt32 nFormat
= SotExchange::GetFormat( rFlavor
);
579 sal_Bool bRet
= sal_False
;
583 if( ( SOT_FORMATSTR_ID_DRAWING
== nFormat
) && ( SGA_OBJ_SVDRAW
== meObjectKind
) )
585 bRet
= ( mxModelStream
.Is() && SetObject( &mxModelStream
, 0, rFlavor
) );
587 else if( ( SOT_FORMATSTR_ID_SVIM
== nFormat
) && mpImageMap
)
589 // TODO/MBA: do we need a BaseURL here?!
590 bRet
= SetImageMap( *mpImageMap
, rFlavor
);
592 else if( ( FORMAT_FILE
== nFormat
) && mpURL
)
594 bRet
= SetString( mpURL
->GetMainURL( INetURLObject::NO_DECODE
), rFlavor
);
596 else if( ( SOT_FORMATSTR_ID_SVXB
== nFormat
) && mpGraphicObject
)
598 bRet
= SetGraphic( mpGraphicObject
->GetGraphic(), rFlavor
);
600 else if( ( FORMAT_GDIMETAFILE
== nFormat
) && mpGraphicObject
)
602 bRet
= SetGDIMetaFile( mpGraphicObject
->GetGraphic().GetGDIMetaFile(), rFlavor
);
604 else if( ( FORMAT_BITMAP
== nFormat
) && mpGraphicObject
)
606 bRet
= SetBitmap( mpGraphicObject
->GetGraphic().GetBitmap(), rFlavor
);
612 // ------------------------------------------------------------------------
614 sal_Bool
GalleryTransferable::WriteObject( SotStorageStreamRef
& rxOStm
, void* pUserObject
,
615 sal_uInt32
, const datatransfer::DataFlavor
& )
617 sal_Bool bRet
= sal_False
;
621 *rxOStm
<< *static_cast< SotStorageStream
* >( pUserObject
);
622 bRet
= ( rxOStm
->GetError() == ERRCODE_NONE
);
628 // ------------------------------------------------------------------------
630 void GalleryTransferable::DragFinished( sal_Int8 nDropAction
)
632 mpTheme
->SetDragging( FALSE
);
633 mpTheme
->SetDragPos( 0 );
636 Window
*pFocusWindow
= Application::GetFocusWindow();
638 pFocusWindow
->GrabFocusToDocument();
642 // ------------------------------------------------------------------------
644 void GalleryTransferable::ObjectReleased()
646 mxModelStream
.Clear();
647 delete mpGraphicObject
, mpGraphicObject
= NULL
;
648 delete mpImageMap
, mpImageMap
= NULL
;
649 delete mpURL
, mpURL
= NULL
;
652 // ------------------------------------------------------------------------
654 void GalleryTransferable::CopyToClipboard( Window
* pWindow
)
656 TransferableHelper::CopyToClipboard( pWindow
);
659 // ------------------------------------------------------------------------
661 void GalleryTransferable::StartDrag( Window
* pWindow
, sal_Int8 nDragSourceActions
,
662 sal_Int32 nDragPointer
, sal_Int32 nDragImage
)
666 if( mpTheme
->GetURL( mnObjectPos
, aURL
) && ( aURL
.GetProtocol() != INET_PROT_NOT_VALID
) )
668 mpTheme
->SetDragging( sal_True
);
669 mpTheme
->SetDragPos( mnObjectPos
);
670 TransferableHelper::StartDrag( pWindow
, nDragSourceActions
, nDragPointer
, nDragImage
);