merge the formfield patch from ooo-build
[ooovba.git] / svx / source / gallery2 / galmisc.cxx
blobc7f058ca80183e5ea42f5a2a7219c7ba0b773161
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: galmisc.cxx,v $
10 * $Revision: 1.46 $
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>
48 #include "impgrf.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>
54 #include "codec.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>
64 // --------------
65 // - Namespaces -
66 // --------------
68 using namespace ::rtl;
69 using namespace ::com::sun::star;
71 // ----------
72 // - ResMgr -
73 // ----------
75 ResMgr* GetGalleryResMgr()
77 static ResMgr* pGalleryResMgr = NULL;
79 if( !pGalleryResMgr )
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 );
100 return aBmpEx;
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;
112 return 0L;
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 );
124 String aFilterName;
126 aMedium.DownLoad();
128 SvStream* pIStm = aMedium.GetInStream();
130 if( pIStm )
132 GraphicFilter* pGraphicFilter = GetGrfFilter();
133 GalleryProgress* pProgress = bShowProgress ? new GalleryProgress( pGraphicFilter ) : NULL;
134 USHORT nFormat;
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;
142 delete pProgress;
145 return nRet;
148 // -----------------------
149 // - GallerySvDrawImport -
150 // -----------------------
152 BOOL GallerySvDrawImport( SvStream& rIStm, SdrModel& rModel )
154 UINT32 nVersion;
155 BOOL bRet = FALSE;
157 if( GalleryCodec::IsCoded( rIStm, nVersion ) )
159 SvMemoryStream aMemStm( 65535, 65535 );
160 GalleryCodec aCodec( rIStm );
162 aCodec.Read( aMemStm );
163 aMemStm.Seek( 0UL );
165 if( 1 == nVersion )
167 DBG_ERROR( "staroffice binary file formats are no longer supported inside the gallery!" );
168 bRet = false;
170 else if( 2 == nVersion )
172 // recall to read as XML
173 bRet = GallerySvDrawImport( aMemStm, rModel );
176 else
178 // read as XML
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) )
187 rIStm.Seek(0);
188 bRet = SvxDrawingLayerImport( &rModel, xInputStream, xComponent, "com.sun.star.comp.Draw.XMLImporter" );
193 return bRet;
196 // ---------------------
197 // - CreateIMapGraphic -
198 // ---------------------
200 BOOL CreateIMapGraphic( const FmFormModel& rModel, Graphic& rGraphic, ImageMap& rImageMap )
202 BOOL bRet = FALSE;
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();
222 bRet = TRUE;
223 break;
229 return bRet;
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;
253 aReduced += aName;
255 else
256 aReduced = aPath;
259 return aReduced;
262 // -----------------------------------------------------------------------------
264 String GetSvDrawStreamNameFromURL( const INetURLObject& rSvDrawObjURL )
266 String aRet;
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, '/' );
274 return aRet;
277 // -----------------------------------------------------------------------------
279 BOOL FileExists( const INetURLObject& rURL )
281 BOOL bRet = FALSE;
283 if( rURL.GetProtocol() != INET_PROT_NOT_VALID )
287 ::ucbhelper::Content aCnt( rURL.GetMainURL( INetURLObject::NO_DECODE ), uno::Reference< ucb::XCommandEnvironment >() );
288 OUString aTitle;
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& )
304 return bRet;
307 // -----------------------------------------------------------------------------
309 BOOL CreateDir( const INetURLObject& rURL )
311 BOOL bRet = FileExists( rURL );
313 if( !bRet )
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& )
341 return bRet;
344 // -----------------------------------------------------------------------------
346 BOOL CopyFile( const INetURLObject& rSrcURL, const INetURLObject& rDstURL )
348 BOOL bRet = FALSE;
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 ) ) );
357 bRet = TRUE;
359 catch( const ucb::ContentCreationException& )
362 catch( const uno::RuntimeException& )
365 catch( const uno::Exception& )
369 return bRet;
372 // -----------------------------------------------------------------------------
374 BOOL KillFile( const INetURLObject& rURL )
376 BOOL bRet = FileExists( rURL );
378 if( bRet )
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& )
387 bRet = FALSE;
389 catch( const uno::RuntimeException& )
391 bRet = FALSE;
393 catch( const uno::Exception& )
395 bRet = FALSE;
399 return bRet;
402 // -------------------
403 // - GalleryProgress -
404 // -------------------
406 GalleryProgress::GalleryProgress( GraphicFilter* pFilter ) :
407 mpFilter( pFilter )
409 uno::Reference< lang::XMultiServiceFactory > xMgr( ::utl::getProcessServiceFactory() );
411 if( xMgr.is() )
413 uno::Reference< awt::XProgressMonitor > xMonitor( xMgr->createInstance(
414 ::rtl::OUString::createFromAscii( "com.sun.star.awt.XProgressMonitor" ) ),
415 uno::UNO_QUERY );
417 if ( xMonitor.is() )
419 mxProgressBar = uno::Reference< awt::XProgressBar >( xMonitor, uno::UNO_QUERY );
421 if( mxProgressBar.is() )
423 String aProgressText;
425 if( mpFilter )
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
432 else
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()
446 // if( mpFilter )
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 ) :
463 mpTheme( pTheme ),
464 meObjectKind( mpTheme->GetObjectKind( nObjectPos ) ),
465 mnObjectPos( nObjectPos ),
466 mpGraphicObject( NULL ),
467 mpImageMap( NULL ),
468 mpURL( NULL )
470 InitData( bLazy );
473 // ------------------------------------------------------------------------
475 GalleryTransferable::~GalleryTransferable()
479 // ------------------------------------------------------------------------
481 void GalleryTransferable::InitData( bool bLazy )
483 switch( meObjectKind )
485 case( SGA_OBJ_SVDRAW ):
487 if( !bLazy )
489 if( !mpGraphicObject )
491 Graphic aGraphic;
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();
504 else
505 mxModelStream->Seek( 0 );
509 break;
511 case( SGA_OBJ_ANIM ):
512 case( SGA_OBJ_BMP ):
513 case( SGA_OBJ_INET ):
514 case( SGA_OBJ_SOUND ):
516 if( !mpURL )
518 mpURL = new INetURLObject;
520 if( !mpTheme->GetURL( mnObjectPos, *mpURL ) )
521 delete mpURL, mpURL = NULL;
524 if( ( SGA_OBJ_SOUND != meObjectKind ) && !mpGraphicObject )
526 Graphic aGraphic;
528 if( mpTheme->GetGraphic( mnObjectPos, aGraphic ) )
529 mpGraphicObject = new GraphicObject( aGraphic );
532 break;
534 default:
535 DBG_ERROR( "GalleryTransferable::GalleryTransferable: invalid object type" );
536 break;
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 );
551 else
553 if( mpURL )
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 );
565 else
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;
581 InitData( 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 );
609 return bRet;
612 // ------------------------------------------------------------------------
614 sal_Bool GalleryTransferable::WriteObject( SotStorageStreamRef& rxOStm, void* pUserObject,
615 sal_uInt32, const datatransfer::DataFlavor& )
617 sal_Bool bRet = sal_False;
619 if( pUserObject )
621 *rxOStm << *static_cast< SotStorageStream* >( pUserObject );
622 bRet = ( rxOStm->GetError() == ERRCODE_NONE );
625 return bRet;
628 // ------------------------------------------------------------------------
630 void GalleryTransferable::DragFinished( sal_Int8 nDropAction )
632 mpTheme->SetDragging( FALSE );
633 mpTheme->SetDragPos( 0 );
634 if ( nDropAction )
636 Window *pFocusWindow = Application::GetFocusWindow();
637 if ( pFocusWindow )
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 )
664 INetURLObject aURL;
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 );