1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include <sal/config.h>
22 #include <sot/storage.hxx>
23 #include <unotools/streamwrap.hxx>
24 #include <comphelper/processfactory.hxx>
25 #include <comphelper/string.hxx>
26 #include <ucbhelper/content.hxx>
27 #include <com/sun/star/ucb/ContentCreationException.hpp>
28 #include <tools/urlobj.hxx>
29 #include <vcl/graphicfilter.hxx>
30 #include <svl/itempool.hxx>
31 #include <sfx2/docfile.hxx>
32 #include <vcl/svapp.hxx>
33 #include <vcl/window.hxx>
34 #include <svx/svdpage.hxx>
35 #include <svx/dialmgr.hxx>
36 #include <svx/svdograf.hxx>
37 #include <svx/fmmodel.hxx>
38 #include <svx/unomodel.hxx>
40 #include <svx/strings.hrc>
41 #include <svx/galtheme.hxx>
42 #include <svx/galmisc.hxx>
43 #include <osl/diagnose.h>
44 #include <com/sun/star/awt/XProgressMonitor.hpp>
45 #include <com/sun/star/ucb/TransferInfo.hpp>
46 #include <com/sun/star/ucb/NameClash.hpp>
49 using namespace ::com::sun::star
;
51 GalleryGraphicImportRet
GalleryGraphicImport( const INetURLObject
& rURL
, Graphic
& rGraphic
,
52 OUString
& rFilterName
)
54 GalleryGraphicImportRet nRet
= GalleryGraphicImportRet::IMPORT_NONE
;
55 SfxMedium
aMedium( rURL
.GetMainURL( INetURLObject::DecodeMechanism::NONE
), StreamMode::READ
);
59 SvStream
* pIStm
= aMedium
.GetInStream();
63 GraphicFilter
& rGraphicFilter
= GraphicFilter::GetGraphicFilter();
66 if( !rGraphicFilter
.ImportGraphic( rGraphic
, rURL
.GetMainURL( INetURLObject::DecodeMechanism::NONE
), *pIStm
, GRFILTER_FORMAT_DONTKNOW
, &nFormat
) )
68 rFilterName
= rGraphicFilter
.GetImportFormatName( nFormat
);
69 nRet
= GalleryGraphicImportRet::IMPORT_FILE
;
76 bool GallerySvDrawImport( SvStream
& rIStm
, SdrModel
& rModel
)
81 if( GalleryCodec::IsCoded( rIStm
, nVersion
) )
83 SvMemoryStream
aMemStm( 65535, 65535 );
84 GalleryCodec
aCodec( rIStm
);
86 aCodec
.Read( aMemStm
);
91 OSL_FAIL( "staroffice binary file formats are no longer supported inside the gallery!" );
94 else if( 2 == nVersion
)
96 // recall to read as XML
97 bRet
= GallerySvDrawImport( aMemStm
, rModel
);
103 uno::Reference
< io::XInputStream
> xInputStream( new utl::OInputStreamWrapper( rIStm
) );
105 rModel
.GetItemPool().SetDefaultMetric( MapUnit::Map100thMM
);
106 uno::Reference
< lang::XComponent
> xComponent
;
108 bRet
= SvxDrawingLayerImport( &rModel
, xInputStream
, xComponent
, "com.sun.star.comp.Draw.XMLOasisImporter" );
109 if( !bRet
|| (rModel
.GetPageCount() == 0) )
112 bRet
= SvxDrawingLayerImport( &rModel
, xInputStream
, xComponent
, "com.sun.star.comp.Draw.XMLImporter" );
120 bool CreateIMapGraphic( const FmFormModel
& rModel
, Graphic
& rGraphic
, ImageMap
& rImageMap
)
122 if (! rModel
.GetPageCount() )
125 const SdrPage
* pPage
= rModel
.GetPage( 0 );
126 const SdrObject
* pObj
= pPage
->GetObj( 0 );
128 if ( pPage
->GetObjCount() != 1 )
130 auto pGrafObj
= dynamic_cast<const SdrGrafObj
*>( pObj
);
135 const sal_uInt16 nCount
= pObj
->GetUserDataCount();
137 // Exist in the user data an IMap information?
138 for ( sal_uInt16 i
= 0; i
< nCount
; i
++ )
140 const SdrObjUserData
* pUserData
= pObj
->GetUserData( i
);
142 if ( ( pUserData
->GetInventor() == SdrInventor::SgaImap
) && ( pUserData
->GetId() == ID_IMAPINFO
) )
144 rGraphic
= pGrafObj
->GetGraphic();
145 rImageMap
= static_cast<const SgaIMapInfo
*>( pUserData
)->GetImageMap();
154 OUString
GetReducedString( const INetURLObject
& rURL
, sal_Int32 nMaxLen
)
156 OUString
aReduced( rURL
.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous
) );
158 aReduced
= aReduced
.copy(aReduced
.lastIndexOf('/')+1);
160 if( INetProtocol::PrivSoffice
!= rURL
.GetProtocol() )
162 sal_Unicode aDelimiter
;
163 const OUString
aPath( rURL
.getFSysPath( FSysStyle::Detect
, &aDelimiter
) );
164 const OUString
aName( aReduced
);
166 if( aPath
.getLength() > nMaxLen
)
168 sal_Int32 nPathPrefixLen
= nMaxLen
- aName
.getLength() - 4;
170 if (nPathPrefixLen
>= 0)
172 aReduced
= OUString::Concat(aPath
.subView(0, nPathPrefixLen
)) + "..."
173 + OUStringChar(aDelimiter
) + aName
;
177 aReduced
+= "..." + OUStringChar(aDelimiter
) + "..."
178 + aName
.subView( aName
.getLength() - (nMaxLen
- 7) );
188 OUString
GetSvDrawStreamNameFromURL( const INetURLObject
& rSvDrawObjURL
)
192 if( rSvDrawObjURL
.GetProtocol() == INetProtocol::PrivSoffice
&&
193 comphelper::string::getTokenCount(rSvDrawObjURL
.GetMainURL( INetURLObject::DecodeMechanism::NONE
), '/') == 3 )
195 aRet
= rSvDrawObjURL
.GetMainURL( INetURLObject::DecodeMechanism::NONE
).getToken( 2, '/' );
201 bool FileExists( const INetURLObject
& rURL
)
205 if( rURL
.GetProtocol() != INetProtocol::NotValid
)
209 ::ucbhelper::Content
aCnt( rURL
.GetMainURL( INetURLObject::DecodeMechanism::NONE
), uno::Reference
< ucb::XCommandEnvironment
>(), comphelper::getProcessComponentContext() );
212 aCnt
.getPropertyValue("Title") >>= aTitle
;
213 bRet
= ( !aTitle
.isEmpty() );
215 catch( const ucb::ContentCreationException
& )
218 catch( const uno::RuntimeException
& )
221 catch( const uno::Exception
& )
229 bool CreateDir( const INetURLObject
& rURL
)
231 bool bRet
= FileExists( rURL
);
237 uno::Reference
< ucb::XCommandEnvironment
> aCmdEnv
;
238 INetURLObject
aParentURL( rURL
);
239 aParentURL
.removeSegment();
240 ::ucbhelper::Content
aParent( aParentURL
.GetMainURL( INetURLObject::DecodeMechanism::NONE
), aCmdEnv
, comphelper::getProcessComponentContext() );
241 uno::Sequence
< OUString
> aProps
{ "Title" };
242 uno::Sequence
< uno::Any
> aValues
{ uno::Any(rURL
.GetLastName()) };
244 ::ucbhelper::Content
aContent( rURL
.GetMainURL( INetURLObject::DecodeMechanism::NONE
), aCmdEnv
, comphelper::getProcessComponentContext() );
245 bRet
= aParent
.insertNewContent( "application/vnd.sun.staroffice.fsys-folder", aProps
, aValues
, aContent
);
247 catch( const ucb::ContentCreationException
& )
250 catch( const uno::RuntimeException
& )
253 catch( const uno::Exception
& )
261 bool CopyFile( const INetURLObject
& rSrcURL
, const INetURLObject
& rDstURL
)
267 ::ucbhelper::Content
aDestPath( rDstURL
.GetMainURL( INetURLObject::DecodeMechanism::NONE
), uno::Reference
< ucb::XCommandEnvironment
>(), comphelper::getProcessComponentContext() );
269 aDestPath
.executeCommand( "transfer",
270 uno::Any( ucb::TransferInfo( false, rSrcURL
.GetMainURL( INetURLObject::DecodeMechanism::NONE
),
271 rDstURL
.GetLastName(), ucb::NameClash::OVERWRITE
) ) );
274 catch( const ucb::ContentCreationException
& )
277 catch( const uno::RuntimeException
& )
280 catch( const uno::Exception
& )
287 bool KillFile( const INetURLObject
& rURL
)
289 bool bRet
= FileExists( rURL
);
295 ::ucbhelper::Content
aCnt( rURL
.GetMainURL( INetURLObject::DecodeMechanism::NONE
), uno::Reference
< ucb::XCommandEnvironment
>(), comphelper::getProcessComponentContext() );
296 aCnt
.executeCommand( "delete", uno::Any( true ) );
298 catch( const ucb::ContentCreationException
& )
302 catch( const uno::RuntimeException
& )
306 catch( const uno::Exception
& )
316 GalleryProgress::GalleryProgress( const GraphicFilter
* pFilter
)
319 uno::Reference
< lang::XMultiServiceFactory
> xMgr( ::comphelper::getProcessServiceFactory() );
321 uno::Reference
< awt::XProgressMonitor
> xMonitor( xMgr
->createInstance( "com.sun.star.awt.XProgressMonitor" ),
324 if ( !xMonitor
.is() )
327 mxProgressBar
= xMonitor
;
329 OUString aProgressText
;
333 aProgressText
= SvxResId(RID_SVXSTR_GALLERY_FILTER
);
334 // pFilter->SetUpdatePercentHdl( LINK( this, GalleryProgress, Update ) ); // sj: progress wasn't working up from SO7 at all
335 // // so I am removing this. The gallery progress should
336 // // be changed to use the XStatusIndicator instead of XProgressMonitor
339 aProgressText
= "Gallery";
341 xMonitor
->addText( "Gallery", aProgressText
, false ) ;
342 mxProgressBar
->setRange( 0, GALLERY_PROGRESS_RANGE
);
345 GalleryProgress::~GalleryProgress()
349 void GalleryProgress::Update( sal_Int32 nVal
, sal_Int32 nMaxVal
)
351 if( mxProgressBar
.is() && nMaxVal
)
352 mxProgressBar
->setValue( std::min
<sal_Int32
>( static_cast<double>(nVal
) / nMaxVal
* GALLERY_PROGRESS_RANGE
,
353 GALLERY_PROGRESS_RANGE
) );
357 GalleryTransferable::GalleryTransferable( GalleryTheme
* pTheme
, sal_uInt32 nObjectPos
, bool bLazy
) :
359 meObjectKind( pTheme
? mpTheme
->GetObjectKind(nObjectPos
) : SgaObjKind::NONE
),
360 mnObjectPos( nObjectPos
)
366 void GalleryTransferable::SelectObject(sal_uInt32 nObjectPos
)
368 if (nObjectPos
== mnObjectPos
)
371 mnObjectPos
= nObjectPos
;
372 meObjectKind
= mpTheme
? mpTheme
->GetObjectKind(mnObjectPos
) : SgaObjKind::NONE
;
377 GalleryTransferable::~GalleryTransferable()
381 void GalleryTransferable::InitData( bool bLazy
)
383 switch( meObjectKind
)
385 case SgaObjKind::SvDraw
:
389 if( !mpGraphicObject
)
393 if (mpTheme
&& mpTheme
->GetGraphic(mnObjectPos
, aGraphic
))
394 mpGraphicObject
.reset(new GraphicObject( std::move(aGraphic
) ));
397 if( !mxModelStream
.is() )
399 mxModelStream
= new SotTempStream( "" );
400 mxModelStream
->SetBufferSize( 16348 );
402 if (!mpTheme
|| !mpTheme
->GetModelStream(mnObjectPos
, mxModelStream
))
403 mxModelStream
.clear();
405 mxModelStream
->Seek( 0 );
411 case SgaObjKind::Animation
:
412 case SgaObjKind::Bitmap
:
413 case SgaObjKind::Inet
:
414 case SgaObjKind::Sound
:
418 mpURL
.reset(new INetURLObject
);
420 if (!mpTheme
|| !mpTheme
->GetURL(mnObjectPos
, *mpURL
))
426 if( ( SgaObjKind::Sound
!= meObjectKind
) && !mpGraphicObject
)
430 if (mpTheme
&& mpTheme
->GetGraphic(mnObjectPos
, aGraphic
))
431 mpGraphicObject
.reset(new GraphicObject( std::move(aGraphic
) ));
437 OSL_FAIL( "GalleryTransferable::GalleryTransferable: invalid object type" );
442 void GalleryTransferable::AddSupportedFormats()
444 if( SgaObjKind::SvDraw
== meObjectKind
)
446 AddFormat( SotClipboardFormatId::DRAWING
);
447 AddFormat( SotClipboardFormatId::SVXB
);
448 AddFormat( SotClipboardFormatId::GDIMETAFILE
);
449 AddFormat( SotClipboardFormatId::BITMAP
);
454 AddFormat( SotClipboardFormatId::SIMPLE_FILE
);
456 if( mpGraphicObject
)
458 AddFormat( SotClipboardFormatId::SVXB
);
460 if( mpGraphicObject
->GetType() == GraphicType::GdiMetafile
)
462 AddFormat( SotClipboardFormatId::GDIMETAFILE
);
463 AddFormat( SotClipboardFormatId::BITMAP
);
467 AddFormat( SotClipboardFormatId::BITMAP
);
468 AddFormat( SotClipboardFormatId::GDIMETAFILE
);
474 bool GalleryTransferable::GetData( const datatransfer::DataFlavor
& rFlavor
, const OUString
& /*rDestDoc*/ )
476 SotClipboardFormatId nFormat
= SotExchange::GetFormat( rFlavor
);
481 if( ( SotClipboardFormatId::DRAWING
== nFormat
) && ( SgaObjKind::SvDraw
== meObjectKind
) )
483 bRet
= ( mxModelStream
.is() && SetObject( mxModelStream
.get(), 0, rFlavor
) );
485 else if( ( SotClipboardFormatId::SIMPLE_FILE
== nFormat
) && mpURL
)
487 bRet
= SetString( mpURL
->GetMainURL( INetURLObject::DecodeMechanism::NONE
) );
489 else if( ( SotClipboardFormatId::SVXB
== nFormat
) && mpGraphicObject
)
491 bRet
= SetGraphic( mpGraphicObject
->GetGraphic() );
493 else if( ( SotClipboardFormatId::GDIMETAFILE
== nFormat
) && mpGraphicObject
)
495 bRet
= SetGDIMetaFile( mpGraphicObject
->GetGraphic().GetGDIMetaFile() );
497 else if( ( SotClipboardFormatId::BITMAP
== nFormat
) && mpGraphicObject
)
499 bRet
= SetBitmapEx( mpGraphicObject
->GetGraphic().GetBitmapEx(), rFlavor
);
505 bool GalleryTransferable::WriteObject( tools::SvRef
<SotTempStream
>& rxOStm
, void* pUserObject
,
506 sal_uInt32
, const datatransfer::DataFlavor
& )
512 rxOStm
->WriteStream( *static_cast< SotStorageStream
* >( pUserObject
) );
513 bRet
= ( rxOStm
->GetError() == ERRCODE_NONE
);
519 void GalleryTransferable::DragFinished( sal_Int8 nDropAction
)
523 mpTheme
->SetDragging( false );
524 mpTheme
->SetDragPos( 0 );
528 vcl::Window
*pFocusWindow
= Application::GetFocusWindow();
530 pFocusWindow
->GrabFocusToDocument();
534 void GalleryTransferable::ObjectReleased()
536 mxModelStream
.clear();
537 mpGraphicObject
.reset();
541 bool GalleryTransferable::StartDrag()
544 if (mpTheme
&& mpTheme
->GetURL(mnObjectPos
, aURL
) && aURL
.GetProtocol() != INetProtocol::NotValid
)
546 mpTheme
->SetDragging( true );
547 mpTheme
->SetDragPos( mnObjectPos
);
553 INetURLObject
ImplGetURL(const GalleryObject
* pObject
)
558 aURL
= *pObject
->m_oStorageUrl
;
563 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */