fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / svx / source / gallery2 / galmisc.cxx
blob499ab340d2387bc08138995eb71aed7bc6e43c45
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <unotools/streamwrap.hxx>
22 #include <unotools/ucbstreamhelper.hxx>
23 #include <comphelper/processfactory.hxx>
24 #include <comphelper/string.hxx>
25 #include <ucbhelper/content.hxx>
26 #include <tools/resmgr.hxx>
27 #include <tools/urlobj.hxx>
28 #include <svl/solar.hrc>
29 #include <svl/urihelper.hxx>
30 #include <vcl/graphicfilter.hxx>
31 #include <svl/itempool.hxx>
32 #include <sfx2/docfile.hxx>
33 #include <avmedia/mediawindow.hxx>
34 #include <vcl/svapp.hxx>
35 #include <svx/svdpage.hxx>
36 #include <svx/svdograf.hxx>
37 #include <svx/fmmodel.hxx>
38 #include <svx/fmview.hxx>
39 #include <svx/unomodel.hxx>
40 #include "codec.hxx"
41 #include "gallery.hrc"
42 #include "svx/gallery1.hxx"
43 #include "svx/galtheme.hxx"
44 #include "svx/galmisc.hxx"
45 #include <com/sun/star/sdbc/XResultSet.hpp>
46 #include <com/sun/star/ucb/XContentAccess.hpp>
47 #include <com/sun/star/ucb/TransferInfo.hpp>
48 #include <com/sun/star/ucb/NameClash.hpp>
50 // --------------
51 // - Namespaces -
52 // --------------
54 using namespace ::rtl;
55 using namespace ::com::sun::star;
57 // ----------
58 // - ResMgr -
59 // ----------
61 ResMgr* GetGalleryResMgr()
63 static ResMgr* pGalleryResMgr = NULL;
65 if( !pGalleryResMgr )
67 pGalleryResMgr = ResMgr::CreateResMgr(
68 "gal", Application::GetSettings().GetUILanguageTag() );
71 return pGalleryResMgr;
74 // -------------------------
75 // - GalleryResGetBitmapEx -
76 // -------------------------
78 BitmapEx GalleryResGetBitmapEx( sal_uInt32 nId )
80 BitmapEx aBmpEx( GAL_RES( nId ) );
82 if( !aBmpEx.IsTransparent() )
83 aBmpEx = BitmapEx( aBmpEx.GetBitmap(), COL_LIGHTMAGENTA );
85 return aBmpEx;
88 // ----------------------
89 // - SgaUserDataFactory -
90 // ----------------------
92 IMPL_LINK( SgaUserDataFactory, MakeUserData, SdrObjFactory*, pObjFactory )
94 if ( pObjFactory->nInventor == IV_IMAPINFO && pObjFactory->nIdentifier == ID_IMAPINFO )
95 pObjFactory->pNewData = new SgaIMapInfo;
97 return 0L;
100 // ------------------------
101 // - GalleryGraphicImport -
102 // ------------------------
104 sal_uInt16 GalleryGraphicImport( const INetURLObject& rURL, Graphic& rGraphic,
105 String& rFilterName, sal_Bool bShowProgress )
107 sal_uInt16 nRet = SGA_IMPORT_NONE;
108 SfxMedium aMedium( rURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ );
110 aMedium.DownLoad();
112 SvStream* pIStm = aMedium.GetInStream();
114 if( pIStm )
116 GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
117 GalleryProgress* pProgress = bShowProgress ? new GalleryProgress( &rGraphicFilter ) : NULL;
118 sal_uInt16 nFormat;
120 if( !rGraphicFilter.ImportGraphic( rGraphic, rURL.GetMainURL( INetURLObject::NO_DECODE ), *pIStm, GRFILTER_FORMAT_DONTKNOW, &nFormat ) )
122 rFilterName = rGraphicFilter.GetImportFormatName( nFormat );
123 nRet = SGA_IMPORT_FILE;
126 delete pProgress;
129 return nRet;
132 // -----------------------
133 // - GallerySvDrawImport -
134 // -----------------------
136 sal_Bool GallerySvDrawImport( SvStream& rIStm, SdrModel& rModel )
138 sal_uInt32 nVersion;
139 sal_Bool bRet = sal_False;
141 if( GalleryCodec::IsCoded( rIStm, nVersion ) )
143 SvMemoryStream aMemStm( 65535, 65535 );
144 GalleryCodec aCodec( rIStm );
146 aCodec.Read( aMemStm );
147 aMemStm.Seek( 0UL );
149 if( 1 == nVersion )
151 OSL_FAIL( "staroffice binary file formats are no longer supported inside the gallery!" );
152 bRet = false;
154 else if( 2 == nVersion )
156 // recall to read as XML
157 bRet = GallerySvDrawImport( aMemStm, rModel );
160 else
162 // read as XML
163 uno::Reference< io::XInputStream > xInputStream( new utl::OInputStreamWrapper( rIStm ) );
165 rModel.GetItemPool().SetDefaultMetric( SFX_MAPUNIT_100TH_MM );
166 uno::Reference< lang::XComponent > xComponent;
168 bRet = SvxDrawingLayerImport( &rModel, xInputStream, xComponent, "com.sun.star.comp.Draw.XMLOasisImporter" );
169 if( !bRet || (rModel.GetPageCount() == 0) )
171 rIStm.Seek(0);
172 bRet = SvxDrawingLayerImport( &rModel, xInputStream, xComponent, "com.sun.star.comp.Draw.XMLImporter" );
177 return bRet;
180 // ---------------------
181 // - CreateIMapGraphic -
182 // ---------------------
184 sal_Bool CreateIMapGraphic( const FmFormModel& rModel, Graphic& rGraphic, ImageMap& rImageMap )
186 sal_Bool bRet = sal_False;
188 if ( rModel.GetPageCount() )
190 const SdrPage* pPage = rModel.GetPage( 0 );
191 const SdrObject* pObj = pPage->GetObj( 0 );
193 if ( pPage->GetObjCount() == 1 && pObj->ISA( SdrGrafObj ) )
195 const sal_uInt16 nCount = pObj->GetUserDataCount();
197 // gibt es in den User-Daten eine IMap-Information?
198 for ( sal_uInt16 i = 0; i < nCount; i++ )
200 const SdrObjUserData* pUserData = pObj->GetUserData( i );
202 if ( ( pUserData->GetInventor() == IV_IMAPINFO ) && ( pUserData->GetId() == ID_IMAPINFO ) )
204 rGraphic = ( (SdrGrafObj*) pObj )->GetGraphic();
205 rImageMap = ( (SgaIMapInfo*) pUserData )->GetImageMap();
206 bRet = sal_True;
207 break;
213 return bRet;
216 // --------------------
217 // - GetReducedString -
218 // --------------------
220 String GetReducedString( const INetURLObject& rURL, sal_uIntPtr nMaxLen )
222 String aReduced( rURL.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS ) );
224 aReduced = aReduced.GetToken( comphelper::string::getTokenCount(aReduced, '/') - 1, '/' );
226 if( INET_PROT_PRIV_SOFFICE != rURL.GetProtocol() )
228 sal_Unicode aDelimiter;
229 const String aPath( rURL.getFSysPath( INetURLObject::FSYS_DETECT, &aDelimiter ) );
230 const String aName( aReduced );
232 if( aPath.Len() > nMaxLen )
234 aReduced = aPath.Copy( 0, (sal_uInt16)( nMaxLen - aName.Len() - 4 ) );
235 aReduced += String( RTL_CONSTASCII_USTRINGPARAM( "..." ) );
236 aReduced += aDelimiter;
237 aReduced += aName;
239 else
240 aReduced = aPath;
243 return aReduced;
246 // -----------------------------------------------------------------------------
248 String GetSvDrawStreamNameFromURL( const INetURLObject& rSvDrawObjURL )
250 String aRet;
252 if( rSvDrawObjURL.GetProtocol() == INET_PROT_PRIV_SOFFICE &&
253 comphelper::string::getTokenCount(rSvDrawObjURL.GetMainURL( INetURLObject::NO_DECODE ), '/') == 3 )
255 aRet = String(rSvDrawObjURL.GetMainURL( INetURLObject::NO_DECODE )).GetToken( 2, '/' );
258 return aRet;
261 // -----------------------------------------------------------------------------
263 sal_Bool FileExists( const INetURLObject& rURL )
265 sal_Bool bRet = sal_False;
267 if( rURL.GetProtocol() != INET_PROT_NOT_VALID )
271 ::ucbhelper::Content aCnt( rURL.GetMainURL( INetURLObject::NO_DECODE ), uno::Reference< ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
272 OUString aTitle;
274 aCnt.getPropertyValue( OUString("Title") ) >>= aTitle;
275 bRet = ( !aTitle.isEmpty() );
277 catch( const ucb::ContentCreationException& )
280 catch( const uno::RuntimeException& )
283 catch( const uno::Exception& )
288 return bRet;
291 // -----------------------------------------------------------------------------
293 sal_Bool CreateDir( const INetURLObject& rURL )
295 sal_Bool bRet = FileExists( rURL );
297 if( !bRet )
301 uno::Reference< ucb::XCommandEnvironment > aCmdEnv;
302 INetURLObject aNewFolderURL( rURL );
303 INetURLObject aParentURL( aNewFolderURL ); aParentURL.removeSegment();
304 ::ucbhelper::Content aParent( aParentURL.GetMainURL( INetURLObject::NO_DECODE ), aCmdEnv, comphelper::getProcessComponentContext() );
305 uno::Sequence< OUString > aProps( 1 );
306 uno::Sequence< uno::Any > aValues( 1 );
308 aProps.getArray()[ 0 ] = OUString("Title");
309 aValues.getArray()[ 0 ] = uno::makeAny( OUString( aNewFolderURL.GetName() ) );
311 ::ucbhelper::Content aContent( aNewFolderURL.GetMainURL( INetURLObject::NO_DECODE ), aCmdEnv, comphelper::getProcessComponentContext() );
312 bRet = aParent.insertNewContent( OUString("application/vnd.sun.staroffice.fsys-folder"), aProps, aValues, aContent );
314 catch( const ucb::ContentCreationException& )
317 catch( const uno::RuntimeException& )
320 catch( const uno::Exception& )
325 return bRet;
328 // -----------------------------------------------------------------------------
330 sal_Bool CopyFile( const INetURLObject& rSrcURL, const INetURLObject& rDstURL )
332 sal_Bool bRet = sal_False;
336 ::ucbhelper::Content aDestPath( rDstURL.GetMainURL( INetURLObject::NO_DECODE ), uno::Reference< ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
338 aDestPath.executeCommand( OUString("transfer"),
339 uno::makeAny( ucb::TransferInfo( sal_False, rSrcURL.GetMainURL( INetURLObject::NO_DECODE ),
340 rDstURL.GetName(), ucb::NameClash::OVERWRITE ) ) );
341 bRet = sal_True;
343 catch( const ucb::ContentCreationException& )
346 catch( const uno::RuntimeException& )
349 catch( const uno::Exception& )
353 return bRet;
356 // -----------------------------------------------------------------------------
358 sal_Bool KillFile( const INetURLObject& rURL )
360 sal_Bool bRet = FileExists( rURL );
362 if( bRet )
366 ::ucbhelper::Content aCnt( rURL.GetMainURL( INetURLObject::NO_DECODE ), uno::Reference< ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
367 aCnt.executeCommand( OUString("delete"), uno::makeAny( sal_Bool( sal_True ) ) );
369 catch( const ucb::ContentCreationException& )
371 bRet = sal_False;
373 catch( const uno::RuntimeException& )
375 bRet = sal_False;
377 catch( const uno::Exception& )
379 bRet = sal_False;
383 return bRet;
386 // -------------------
387 // - GalleryProgress -
388 // -------------------
389 DBG_NAME(GalleryProgress)
391 GalleryProgress::GalleryProgress( GraphicFilter* pFilter ) :
392 mpFilter( pFilter )
394 DBG_CTOR(GalleryProgress,NULL);
396 uno::Reference< lang::XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
398 uno::Reference< awt::XProgressMonitor > xMonitor( xMgr->createInstance(
399 OUString("com.sun.star.awt.XProgressMonitor") ),
400 uno::UNO_QUERY );
402 if ( xMonitor.is() )
404 mxProgressBar = uno::Reference< awt::XProgressBar >( xMonitor, uno::UNO_QUERY );
406 if( mxProgressBar.is() )
408 String aProgressText;
410 if( mpFilter )
412 aProgressText = GAL_RESSTR(RID_SVXSTR_GALLERY_FILTER);
413 // mpFilter->SetUpdatePercentHdl( LINK( this, GalleryProgress, Update ) ); // sj: progress wasn't working up from SO7 at all
414 // // so I am removing this. The gallery progress should
415 // // be changed to use the XStatusIndicator instead of XProgressMonitor
417 else
418 aProgressText = String( RTL_CONSTASCII_USTRINGPARAM( "Gallery" ) );
420 xMonitor->addText( String( RTL_CONSTASCII_USTRINGPARAM( "Gallery" ) ), aProgressText, sal_False ) ;
421 mxProgressBar->setRange( 0, GALLERY_PROGRESS_RANGE );
426 // ------------------------------------------------------------------------
428 GalleryProgress::~GalleryProgress()
430 DBG_DTOR(GalleryProgress,NULL);
433 // ------------------------------------------------------------------------
435 void GalleryProgress::Update( sal_uIntPtr nVal, sal_uIntPtr nMaxVal )
437 if( mxProgressBar.is() && nMaxVal )
438 mxProgressBar->setValue( std::min( (sal_uIntPtr)( (double) nVal / nMaxVal * GALLERY_PROGRESS_RANGE ), (sal_uIntPtr) GALLERY_PROGRESS_RANGE ) );
441 // -----------------------
442 // - GalleryTransferable -
443 // -----------------------
444 DBG_NAME(GalleryTransferable)
446 GalleryTransferable::GalleryTransferable( GalleryTheme* pTheme, sal_uIntPtr nObjectPos, bool bLazy ) :
447 mpTheme( pTheme ),
448 meObjectKind( mpTheme->GetObjectKind( nObjectPos ) ),
449 mnObjectPos( nObjectPos ),
450 mpGraphicObject( NULL ),
451 mpImageMap( NULL ),
452 mpURL( NULL )
454 DBG_CTOR(GalleryTransferable,NULL);
456 InitData( bLazy );
459 // ------------------------------------------------------------------------
461 GalleryTransferable::~GalleryTransferable()
463 DBG_DTOR(GalleryTransferable,NULL);
466 // ------------------------------------------------------------------------
468 void GalleryTransferable::InitData( bool bLazy )
470 switch( meObjectKind )
472 case( SGA_OBJ_SVDRAW ):
474 if( !bLazy )
476 if( !mpGraphicObject )
478 Graphic aGraphic;
480 if( mpTheme->GetGraphic( mnObjectPos, aGraphic ) )
481 mpGraphicObject = new GraphicObject( aGraphic );
484 if( !mxModelStream.Is() )
486 mxModelStream = new SotStorageStream( String() );
487 mxModelStream->SetBufferSize( 16348 );
489 if( !mpTheme->GetModelStream( mnObjectPos, mxModelStream ) )
490 mxModelStream.Clear();
491 else
492 mxModelStream->Seek( 0 );
496 break;
498 case( SGA_OBJ_ANIM ):
499 case( SGA_OBJ_BMP ):
500 case( SGA_OBJ_INET ):
501 case( SGA_OBJ_SOUND ):
503 if( !mpURL )
505 mpURL = new INetURLObject;
507 if( !mpTheme->GetURL( mnObjectPos, *mpURL ) )
508 delete mpURL, mpURL = NULL;
511 if( ( SGA_OBJ_SOUND != meObjectKind ) && !mpGraphicObject )
513 Graphic aGraphic;
515 if( mpTheme->GetGraphic( mnObjectPos, aGraphic ) )
516 mpGraphicObject = new GraphicObject( aGraphic );
519 break;
521 default:
522 OSL_FAIL( "GalleryTransferable::GalleryTransferable: invalid object type" );
523 break;
527 // ------------------------------------------------------------------------
529 void GalleryTransferable::AddSupportedFormats()
531 if( SGA_OBJ_SVDRAW == meObjectKind )
533 AddFormat( SOT_FORMATSTR_ID_DRAWING );
534 AddFormat( SOT_FORMATSTR_ID_SVXB );
535 AddFormat( FORMAT_GDIMETAFILE );
536 AddFormat( FORMAT_BITMAP );
538 else
540 if( mpURL )
541 AddFormat( FORMAT_FILE );
543 if( mpGraphicObject )
545 AddFormat( SOT_FORMATSTR_ID_SVXB );
547 if( mpGraphicObject->GetType() == GRAPHIC_GDIMETAFILE )
549 AddFormat( FORMAT_GDIMETAFILE );
550 AddFormat( FORMAT_BITMAP );
552 else
554 AddFormat( FORMAT_BITMAP );
555 AddFormat( FORMAT_GDIMETAFILE );
561 // ------------------------------------------------------------------------
563 sal_Bool GalleryTransferable::GetData( const datatransfer::DataFlavor& rFlavor )
565 sal_uInt32 nFormat = SotExchange::GetFormat( rFlavor );
566 sal_Bool bRet = sal_False;
568 InitData( false );
570 if( ( SOT_FORMATSTR_ID_DRAWING == nFormat ) && ( SGA_OBJ_SVDRAW == meObjectKind ) )
572 bRet = ( mxModelStream.Is() && SetObject( &mxModelStream, 0, rFlavor ) );
574 else if( ( SOT_FORMATSTR_ID_SVIM == nFormat ) && mpImageMap )
576 // TODO/MBA: do we need a BaseURL here?!
577 bRet = SetImageMap( *mpImageMap, rFlavor );
579 else if( ( FORMAT_FILE == nFormat ) && mpURL )
581 bRet = SetString( mpURL->GetMainURL( INetURLObject::NO_DECODE ), rFlavor );
583 else if( ( SOT_FORMATSTR_ID_SVXB == nFormat ) && mpGraphicObject )
585 bRet = SetGraphic( mpGraphicObject->GetGraphic(), rFlavor );
587 else if( ( FORMAT_GDIMETAFILE == nFormat ) && mpGraphicObject )
589 bRet = SetGDIMetaFile( mpGraphicObject->GetGraphic().GetGDIMetaFile(), rFlavor );
591 else if( ( FORMAT_BITMAP == nFormat ) && mpGraphicObject )
593 bRet = SetBitmap( mpGraphicObject->GetGraphic().GetBitmap(), rFlavor );
596 return bRet;
599 // ------------------------------------------------------------------------
601 sal_Bool GalleryTransferable::WriteObject( SotStorageStreamRef& rxOStm, void* pUserObject,
602 sal_uInt32, const datatransfer::DataFlavor& )
604 sal_Bool bRet = sal_False;
606 if( pUserObject )
608 *rxOStm << *static_cast< SotStorageStream* >( pUserObject );
609 bRet = ( rxOStm->GetError() == ERRCODE_NONE );
612 return bRet;
615 // ------------------------------------------------------------------------
617 void GalleryTransferable::DragFinished( sal_Int8 nDropAction )
619 mpTheme->SetDragging( sal_False );
620 mpTheme->SetDragPos( 0 );
621 if ( nDropAction )
623 Window *pFocusWindow = Application::GetFocusWindow();
624 if ( pFocusWindow )
625 pFocusWindow->GrabFocusToDocument();
629 // ------------------------------------------------------------------------
631 void GalleryTransferable::ObjectReleased()
633 mxModelStream.Clear();
634 delete mpGraphicObject, mpGraphicObject = NULL;
635 delete mpImageMap, mpImageMap = NULL;
636 delete mpURL, mpURL = NULL;
639 // ------------------------------------------------------------------------
641 void GalleryTransferable::CopyToClipboard( Window* pWindow )
643 TransferableHelper::CopyToClipboard( pWindow );
646 // ------------------------------------------------------------------------
648 void GalleryTransferable::StartDrag( Window* pWindow, sal_Int8 nDragSourceActions,
649 sal_Int32 nDragPointer, sal_Int32 nDragImage )
651 INetURLObject aURL;
653 if( mpTheme->GetURL( mnObjectPos, aURL ) && ( aURL.GetProtocol() != INET_PROT_NOT_VALID ) )
655 mpTheme->SetDragging( sal_True );
656 mpTheme->SetDragPos( mnObjectPos );
657 TransferableHelper::StartDrag( pWindow, nDragSourceActions, nDragPointer, nDragImage );
661 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */