char array should be null terminated
[LibreOffice.git] / svx / source / gallery2 / galmisc.cxx
blobbe92ac7ebba915b3f1f6ca95526a7b50e912d41d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 // MARKER(update_precomp.py): autogen include statement, do not remove
30 #include "precompiled_svx.hxx"
32 #include <unotools/streamwrap.hxx>
33 #include <unotools/ucbstreamhelper.hxx>
34 #include <unotools/processfactory.hxx>
35 #include <ucbhelper/content.hxx>
36 #include <tools/resmgr.hxx>
37 #include <tools/urlobj.hxx>
38 #include <svl/solar.hrc>
39 #include <svl/urihelper.hxx>
40 #include <svtools/filter.hxx>
41 #include <svl/itempool.hxx>
42 #include <sfx2/docfile.hxx>
43 #include <avmedia/mediawindow.hxx>
44 #include <vcl/svapp.hxx>
46 #include "svtools/filter.hxx"
47 #include <svx/svdpage.hxx>
48 #include <svx/svdograf.hxx>
49 #include <svx/fmmodel.hxx>
50 #include <svx/fmview.hxx>
51 #include <svx/unomodel.hxx>
52 #include "codec.hxx"
53 #include "gallery.hrc"
54 #include "svx/gallery1.hxx"
55 #include "svx/galtheme.hxx"
56 #include "svx/galmisc.hxx"
57 #include <com/sun/star/sdbc/XResultSet.hpp>
58 #include <com/sun/star/ucb/XContentAccess.hpp>
59 #include <com/sun/star/ucb/TransferInfo.hpp>
60 #include <com/sun/star/ucb/NameClash.hpp>
62 // --------------
63 // - Namespaces -
64 // --------------
66 using namespace ::rtl;
67 using namespace ::com::sun::star;
69 // ----------
70 // - ResMgr -
71 // ----------
73 ResMgr* GetGalleryResMgr()
75 static ResMgr* pGalleryResMgr = NULL;
77 if( !pGalleryResMgr )
79 pGalleryResMgr = ResMgr::CreateResMgr(
80 "gal", Application::GetSettings().GetUILocale() );
83 return pGalleryResMgr;
86 // -------------------------
87 // - GalleryResGetBitmapEx -
88 // -------------------------
90 BitmapEx GalleryResGetBitmapEx( sal_uInt32 nId )
92 BitmapEx aBmpEx( GAL_RESID( nId ) );
94 if( !aBmpEx.IsTransparent() )
95 aBmpEx = BitmapEx( aBmpEx.GetBitmap(), COL_LIGHTMAGENTA );
97 return aBmpEx;
100 // ----------------------
101 // - SgaUserDataFactory -
102 // ----------------------
104 IMPL_LINK( SgaUserDataFactory, MakeUserData, SdrObjFactory*, pObjFactory )
106 if ( pObjFactory->nInventor == IV_IMAPINFO && pObjFactory->nIdentifier == ID_IMAPINFO )
107 pObjFactory->pNewData = new SgaIMapInfo;
109 return 0L;
112 // ------------------------
113 // - GalleryGraphicImport -
114 // ------------------------
116 sal_uInt16 GalleryGraphicImport( const INetURLObject& rURL, Graphic& rGraphic,
117 String& rFilterName, sal_Bool bShowProgress )
119 sal_uInt16 nRet = SGA_IMPORT_NONE;
120 SfxMedium aMedium( rURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ, sal_True );
121 String aFilterName;
123 aMedium.DownLoad();
125 SvStream* pIStm = aMedium.GetInStream();
127 if( pIStm )
129 GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
130 GalleryProgress* pProgress = bShowProgress ? new GalleryProgress( &rGraphicFilter ) : NULL;
131 sal_uInt16 nFormat;
133 if( !rGraphicFilter.ImportGraphic( rGraphic, rURL.GetMainURL( INetURLObject::NO_DECODE ), *pIStm, GRFILTER_FORMAT_DONTKNOW, &nFormat ) )
135 rFilterName = rGraphicFilter.GetImportFormatName( nFormat );
136 nRet = SGA_IMPORT_FILE;
139 delete pProgress;
142 return nRet;
145 // -----------------------
146 // - GallerySvDrawImport -
147 // -----------------------
149 sal_Bool GallerySvDrawImport( SvStream& rIStm, SdrModel& rModel )
151 sal_uInt32 nVersion;
152 sal_Bool bRet = sal_False;
154 if( GalleryCodec::IsCoded( rIStm, nVersion ) )
156 SvMemoryStream aMemStm( 65535, 65535 );
157 GalleryCodec aCodec( rIStm );
159 aCodec.Read( aMemStm );
160 aMemStm.Seek( 0UL );
162 if( 1 == nVersion )
164 OSL_FAIL( "staroffice binary file formats are no longer supported inside the gallery!" );
165 bRet = false;
167 else if( 2 == nVersion )
169 // recall to read as XML
170 bRet = GallerySvDrawImport( aMemStm, rModel );
173 else
175 // read as XML
176 uno::Reference< io::XInputStream > xInputStream( new utl::OInputStreamWrapper( rIStm ) );
178 rModel.GetItemPool().SetDefaultMetric( SFX_MAPUNIT_100TH_MM );
179 uno::Reference< lang::XComponent > xComponent;
181 bRet = SvxDrawingLayerImport( &rModel, xInputStream, xComponent, "com.sun.star.comp.Draw.XMLOasisImporter" );
182 if( !bRet || (rModel.GetPageCount() == 0) )
184 rIStm.Seek(0);
185 bRet = SvxDrawingLayerImport( &rModel, xInputStream, xComponent, "com.sun.star.comp.Draw.XMLImporter" );
190 return bRet;
193 // ---------------------
194 // - CreateIMapGraphic -
195 // ---------------------
197 sal_Bool CreateIMapGraphic( const FmFormModel& rModel, Graphic& rGraphic, ImageMap& rImageMap )
199 sal_Bool bRet = sal_False;
201 if ( rModel.GetPageCount() )
203 const SdrPage* pPage = rModel.GetPage( 0 );
204 const SdrObject* pObj = pPage->GetObj( 0 );
206 if ( pPage->GetObjCount() == 1 && pObj->ISA( SdrGrafObj ) )
208 const sal_uInt16 nCount = pObj->GetUserDataCount();
210 // gibt es in den User-Daten eine IMap-Information?
211 for ( sal_uInt16 i = 0; i < nCount; i++ )
213 const SdrObjUserData* pUserData = pObj->GetUserData( i );
215 if ( ( pUserData->GetInventor() == IV_IMAPINFO ) && ( pUserData->GetId() == ID_IMAPINFO ) )
217 rGraphic = ( (SdrGrafObj*) pObj )->GetGraphic();
218 rImageMap = ( (SgaIMapInfo*) pUserData )->GetImageMap();
219 bRet = sal_True;
220 break;
226 return bRet;
229 // --------------------
230 // - GetReducedString -
231 // --------------------
233 String GetReducedString( const INetURLObject& rURL, sal_uIntPtr nMaxLen )
235 String aReduced( rURL.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS ) );
237 aReduced = aReduced.GetToken( aReduced.GetTokenCount( '/' ) - 1, '/' );
239 if( INET_PROT_PRIV_SOFFICE != rURL.GetProtocol() )
241 sal_Unicode aDelimiter;
242 const String aPath( rURL.getFSysPath( INetURLObject::FSYS_DETECT, &aDelimiter ) );
243 const String aName( aReduced );
245 if( aPath.Len() > nMaxLen )
247 aReduced = aPath.Copy( 0, (sal_uInt16)( nMaxLen - aName.Len() - 4 ) );
248 aReduced += String( RTL_CONSTASCII_USTRINGPARAM( "..." ) );
249 aReduced += aDelimiter;
250 aReduced += aName;
252 else
253 aReduced = aPath;
256 return aReduced;
259 // -----------------------------------------------------------------------------
261 String GetSvDrawStreamNameFromURL( const INetURLObject& rSvDrawObjURL )
263 String aRet;
265 if( rSvDrawObjURL.GetProtocol() == INET_PROT_PRIV_SOFFICE &&
266 String(rSvDrawObjURL.GetMainURL( INetURLObject::NO_DECODE )).GetTokenCount( '/' ) == 3 )
268 aRet = String(rSvDrawObjURL.GetMainURL( INetURLObject::NO_DECODE )).GetToken( 2, '/' );
271 return aRet;
274 // -----------------------------------------------------------------------------
276 sal_Bool FileExists( const INetURLObject& rURL )
278 sal_Bool bRet = sal_False;
280 if( rURL.GetProtocol() != INET_PROT_NOT_VALID )
284 ::ucbhelper::Content aCnt( rURL.GetMainURL( INetURLObject::NO_DECODE ), uno::Reference< ucb::XCommandEnvironment >() );
285 OUString aTitle;
287 aCnt.getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("Title")) ) >>= aTitle;
288 bRet = ( aTitle.getLength() > 0 );
290 catch( const ucb::ContentCreationException& )
293 catch( const uno::RuntimeException& )
296 catch( const uno::Exception& )
301 return bRet;
304 // -----------------------------------------------------------------------------
306 sal_Bool CreateDir( const INetURLObject& rURL )
308 sal_Bool bRet = FileExists( rURL );
310 if( !bRet )
314 uno::Reference< ucb::XCommandEnvironment > aCmdEnv;
315 INetURLObject aNewFolderURL( rURL );
316 INetURLObject aParentURL( aNewFolderURL ); aParentURL.removeSegment();
317 ::ucbhelper::Content aParent( aParentURL.GetMainURL( INetURLObject::NO_DECODE ), aCmdEnv );
318 uno::Sequence< OUString > aProps( 1 );
319 uno::Sequence< uno::Any > aValues( 1 );
321 aProps.getArray()[ 0 ] = OUString(RTL_CONSTASCII_USTRINGPARAM("Title"));
322 aValues.getArray()[ 0 ] = uno::makeAny( OUString( aNewFolderURL.GetName() ) );
324 ::ucbhelper::Content aContent( aNewFolderURL.GetMainURL( INetURLObject::NO_DECODE ), aCmdEnv );
325 bRet = aParent.insertNewContent( OUString(RTL_CONSTASCII_USTRINGPARAM("application/vnd.sun.staroffice.fsys-folder")), aProps, aValues, aContent );
327 catch( const ucb::ContentCreationException& )
330 catch( const uno::RuntimeException& )
333 catch( const uno::Exception& )
338 return bRet;
341 // -----------------------------------------------------------------------------
343 sal_Bool CopyFile( const INetURLObject& rSrcURL, const INetURLObject& rDstURL )
345 sal_Bool bRet = sal_False;
349 ::ucbhelper::Content aDestPath( rDstURL.GetMainURL( INetURLObject::NO_DECODE ), uno::Reference< ucb::XCommandEnvironment >() );
351 aDestPath.executeCommand( OUString(RTL_CONSTASCII_USTRINGPARAM("transfer")),
352 uno::makeAny( ucb::TransferInfo( sal_False, rSrcURL.GetMainURL( INetURLObject::NO_DECODE ),
353 rDstURL.GetName(), ucb::NameClash::OVERWRITE ) ) );
354 bRet = sal_True;
356 catch( const ucb::ContentCreationException& )
359 catch( const uno::RuntimeException& )
362 catch( const uno::Exception& )
366 return bRet;
369 // -----------------------------------------------------------------------------
371 sal_Bool KillFile( const INetURLObject& rURL )
373 sal_Bool bRet = FileExists( rURL );
375 if( bRet )
379 ::ucbhelper::Content aCnt( rURL.GetMainURL( INetURLObject::NO_DECODE ), uno::Reference< ucb::XCommandEnvironment >() );
380 aCnt.executeCommand( OUString(RTL_CONSTASCII_USTRINGPARAM("delete")), uno::makeAny( sal_Bool( sal_True ) ) );
382 catch( const ucb::ContentCreationException& )
384 bRet = sal_False;
386 catch( const uno::RuntimeException& )
388 bRet = sal_False;
390 catch( const uno::Exception& )
392 bRet = sal_False;
396 return bRet;
399 // -------------------
400 // - GalleryProgress -
401 // -------------------
403 GalleryProgress::GalleryProgress( GraphicFilter* pFilter ) :
404 mpFilter( pFilter )
406 uno::Reference< lang::XMultiServiceFactory > xMgr( ::utl::getProcessServiceFactory() );
408 if( xMgr.is() )
410 uno::Reference< awt::XProgressMonitor > xMonitor( xMgr->createInstance(
411 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.awt.XProgressMonitor")) ),
412 uno::UNO_QUERY );
414 if ( xMonitor.is() )
416 mxProgressBar = uno::Reference< awt::XProgressBar >( xMonitor, uno::UNO_QUERY );
418 if( mxProgressBar.is() )
420 String aProgressText;
422 if( mpFilter )
424 aProgressText = String( GAL_RESID( RID_SVXSTR_GALLERY_FILTER ) );
425 // mpFilter->SetUpdatePercentHdl( LINK( this, GalleryProgress, Update ) ); // sj: progress wasn't working up from SO7 at all
426 // // so I am removing this. The gallery progress should
427 // // be changed to use the XStatusIndicator instead of XProgressMonitor
429 else
430 aProgressText = String( RTL_CONSTASCII_USTRINGPARAM( "Gallery" ) );
432 xMonitor->addText( String( RTL_CONSTASCII_USTRINGPARAM( "Gallery" ) ), aProgressText, sal_False ) ;
433 mxProgressBar->setRange( 0, GALLERY_PROGRESS_RANGE );
439 // ------------------------------------------------------------------------
441 GalleryProgress::~GalleryProgress()
443 // if( mpFilter )
444 // mpFilter->SetUpdatePercentHdl( Link() );
447 // ------------------------------------------------------------------------
449 void GalleryProgress::Update( sal_uIntPtr nVal, sal_uIntPtr nMaxVal )
451 if( mxProgressBar.is() && nMaxVal )
452 mxProgressBar->setValue( Min( (sal_uIntPtr)( (double) nVal / nMaxVal * GALLERY_PROGRESS_RANGE ), (sal_uIntPtr) GALLERY_PROGRESS_RANGE ) );
455 // -----------------------
456 // - GalleryTransferable -
457 // -----------------------
459 GalleryTransferable::GalleryTransferable( GalleryTheme* pTheme, sal_uIntPtr nObjectPos, bool bLazy ) :
460 mpTheme( pTheme ),
461 meObjectKind( mpTheme->GetObjectKind( nObjectPos ) ),
462 mnObjectPos( nObjectPos ),
463 mpGraphicObject( NULL ),
464 mpImageMap( NULL ),
465 mpURL( NULL )
467 InitData( bLazy );
470 // ------------------------------------------------------------------------
472 GalleryTransferable::~GalleryTransferable()
476 // ------------------------------------------------------------------------
478 void GalleryTransferable::InitData( bool bLazy )
480 switch( meObjectKind )
482 case( SGA_OBJ_SVDRAW ):
484 if( !bLazy )
486 if( !mpGraphicObject )
488 Graphic aGraphic;
490 if( mpTheme->GetGraphic( mnObjectPos, aGraphic ) )
491 mpGraphicObject = new GraphicObject( aGraphic );
494 if( !mxModelStream.Is() )
496 mxModelStream = new SotStorageStream( String() );
497 mxModelStream->SetBufferSize( 16348 );
499 if( !mpTheme->GetModelStream( mnObjectPos, mxModelStream ) )
500 mxModelStream.Clear();
501 else
502 mxModelStream->Seek( 0 );
506 break;
508 case( SGA_OBJ_ANIM ):
509 case( SGA_OBJ_BMP ):
510 case( SGA_OBJ_INET ):
511 case( SGA_OBJ_SOUND ):
513 if( !mpURL )
515 mpURL = new INetURLObject;
517 if( !mpTheme->GetURL( mnObjectPos, *mpURL ) )
518 delete mpURL, mpURL = NULL;
521 if( ( SGA_OBJ_SOUND != meObjectKind ) && !mpGraphicObject )
523 Graphic aGraphic;
525 if( mpTheme->GetGraphic( mnObjectPos, aGraphic ) )
526 mpGraphicObject = new GraphicObject( aGraphic );
529 break;
531 default:
532 OSL_FAIL( "GalleryTransferable::GalleryTransferable: invalid object type" );
533 break;
537 // ------------------------------------------------------------------------
539 void GalleryTransferable::AddSupportedFormats()
541 if( SGA_OBJ_SVDRAW == meObjectKind )
543 AddFormat( SOT_FORMATSTR_ID_DRAWING );
544 AddFormat( SOT_FORMATSTR_ID_SVXB );
545 AddFormat( FORMAT_GDIMETAFILE );
546 AddFormat( FORMAT_BITMAP );
548 else
550 if( mpURL )
551 AddFormat( FORMAT_FILE );
553 if( mpGraphicObject )
555 AddFormat( SOT_FORMATSTR_ID_SVXB );
557 if( mpGraphicObject->GetType() == GRAPHIC_GDIMETAFILE )
559 AddFormat( FORMAT_GDIMETAFILE );
560 AddFormat( FORMAT_BITMAP );
562 else
564 AddFormat( FORMAT_BITMAP );
565 AddFormat( FORMAT_GDIMETAFILE );
571 // ------------------------------------------------------------------------
573 sal_Bool GalleryTransferable::GetData( const datatransfer::DataFlavor& rFlavor )
575 sal_uInt32 nFormat = SotExchange::GetFormat( rFlavor );
576 sal_Bool bRet = sal_False;
578 InitData( false );
580 if( ( SOT_FORMATSTR_ID_DRAWING == nFormat ) && ( SGA_OBJ_SVDRAW == meObjectKind ) )
582 bRet = ( mxModelStream.Is() && SetObject( &mxModelStream, 0, rFlavor ) );
584 else if( ( SOT_FORMATSTR_ID_SVIM == nFormat ) && mpImageMap )
586 // TODO/MBA: do we need a BaseURL here?!
587 bRet = SetImageMap( *mpImageMap, rFlavor );
589 else if( ( FORMAT_FILE == nFormat ) && mpURL )
591 bRet = SetString( mpURL->GetMainURL( INetURLObject::NO_DECODE ), rFlavor );
593 else if( ( SOT_FORMATSTR_ID_SVXB == nFormat ) && mpGraphicObject )
595 bRet = SetGraphic( mpGraphicObject->GetGraphic(), rFlavor );
597 else if( ( FORMAT_GDIMETAFILE == nFormat ) && mpGraphicObject )
599 bRet = SetGDIMetaFile( mpGraphicObject->GetGraphic().GetGDIMetaFile(), rFlavor );
601 else if( ( FORMAT_BITMAP == nFormat ) && mpGraphicObject )
603 bRet = SetBitmap( mpGraphicObject->GetGraphic().GetBitmap(), rFlavor );
606 return bRet;
609 // ------------------------------------------------------------------------
611 sal_Bool GalleryTransferable::WriteObject( SotStorageStreamRef& rxOStm, void* pUserObject,
612 sal_uInt32, const datatransfer::DataFlavor& )
614 sal_Bool bRet = sal_False;
616 if( pUserObject )
618 *rxOStm << *static_cast< SotStorageStream* >( pUserObject );
619 bRet = ( rxOStm->GetError() == ERRCODE_NONE );
622 return bRet;
625 // ------------------------------------------------------------------------
627 void GalleryTransferable::DragFinished( sal_Int8 nDropAction )
629 mpTheme->SetDragging( sal_False );
630 mpTheme->SetDragPos( 0 );
631 if ( nDropAction )
633 Window *pFocusWindow = Application::GetFocusWindow();
634 if ( pFocusWindow )
635 pFocusWindow->GrabFocusToDocument();
639 // ------------------------------------------------------------------------
641 void GalleryTransferable::ObjectReleased()
643 mxModelStream.Clear();
644 delete mpGraphicObject, mpGraphicObject = NULL;
645 delete mpImageMap, mpImageMap = NULL;
646 delete mpURL, mpURL = NULL;
649 // ------------------------------------------------------------------------
651 void GalleryTransferable::CopyToClipboard( Window* pWindow )
653 TransferableHelper::CopyToClipboard( pWindow );
656 // ------------------------------------------------------------------------
658 void GalleryTransferable::StartDrag( Window* pWindow, sal_Int8 nDragSourceActions,
659 sal_Int32 nDragPointer, sal_Int32 nDragImage )
661 INetURLObject aURL;
663 if( mpTheme->GetURL( mnObjectPos, aURL ) && ( aURL.GetProtocol() != INET_PROT_NOT_VALID ) )
665 mpTheme->SetDragging( sal_True );
666 mpTheme->SetDragPos( mnObjectPos );
667 TransferableHelper::StartDrag( pWindow, nDragSourceActions, nDragPointer, nDragImage );
671 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */