Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / gallery2 / galmisc.cxx
blobab5713f7a4dff8da8d8757f44b935baa2c6cdb54
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 .
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>
39 #include "codec.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>
47 #include <memory>
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 );
57 aMedium.Download();
59 SvStream* pIStm = aMedium.GetInStream();
61 if( pIStm )
63 GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
64 sal_uInt16 nFormat;
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;
73 return nRet;
76 bool GallerySvDrawImport( SvStream& rIStm, SdrModel& rModel )
78 sal_uInt32 nVersion;
79 bool bRet = false;
81 if( GalleryCodec::IsCoded( rIStm, nVersion ) )
83 SvMemoryStream aMemStm( 65535, 65535 );
84 GalleryCodec aCodec( rIStm );
86 aCodec.Read( aMemStm );
87 aMemStm.Seek( 0 );
89 if( 1 == nVersion )
91 OSL_FAIL( "staroffice binary file formats are no longer supported inside the gallery!" );
92 bRet = false;
94 else if( 2 == nVersion )
96 // recall to read as XML
97 bRet = GallerySvDrawImport( aMemStm, rModel );
100 else
102 // read as XML
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) )
111 rIStm.Seek(0);
112 bRet = SvxDrawingLayerImport( &rModel, xInputStream, xComponent, "com.sun.star.comp.Draw.XMLImporter" );
117 return bRet;
120 bool CreateIMapGraphic( const FmFormModel& rModel, Graphic& rGraphic, ImageMap& rImageMap )
122 if (! rModel.GetPageCount() )
123 return false;
125 const SdrPage* pPage = rModel.GetPage( 0 );
126 const SdrObject* pObj = pPage->GetObj( 0 );
128 if ( pPage->GetObjCount() != 1 )
129 return false;
130 auto pGrafObj = dynamic_cast<const SdrGrafObj*>( pObj);
131 if (!pGrafObj)
132 return false;
134 bool bRet = false;
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();
146 bRet = true;
147 break;
151 return bRet;
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;
175 else
177 aReduced += "..." + OUStringChar(aDelimiter) + "..."
178 + aName.subView( aName.getLength() - (nMaxLen - 7) );
181 else
182 aReduced = aPath;
185 return aReduced;
188 OUString GetSvDrawStreamNameFromURL( const INetURLObject& rSvDrawObjURL )
190 OUString aRet;
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, '/' );
198 return aRet;
201 bool FileExists( const INetURLObject& rURL )
203 bool bRet = false;
205 if( rURL.GetProtocol() != INetProtocol::NotValid )
209 ::ucbhelper::Content aCnt( rURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), uno::Reference< ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
210 OUString aTitle;
212 aCnt.getPropertyValue("Title") >>= aTitle;
213 bRet = ( !aTitle.isEmpty() );
215 catch( const ucb::ContentCreationException& )
218 catch( const uno::RuntimeException& )
221 catch( const uno::Exception& )
226 return bRet;
229 bool CreateDir( const INetURLObject& rURL )
231 bool bRet = FileExists( rURL );
233 if( !bRet )
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& )
258 return bRet;
261 bool CopyFile( const INetURLObject& rSrcURL, const INetURLObject& rDstURL )
263 bool bRet = false;
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 ) ) );
272 bRet = true;
274 catch( const ucb::ContentCreationException& )
277 catch( const uno::RuntimeException& )
280 catch( const uno::Exception& )
284 return bRet;
287 bool KillFile( const INetURLObject& rURL )
289 bool bRet = FileExists( rURL );
291 if( bRet )
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& )
300 bRet = false;
302 catch( const uno::RuntimeException& )
304 bRet = false;
306 catch( const uno::Exception& )
308 bRet = false;
312 return bRet;
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" ),
322 uno::UNO_QUERY );
324 if ( !xMonitor.is() )
325 return;
327 mxProgressBar = xMonitor;
329 OUString aProgressText;
331 if( pFilter )
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
338 else
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 ) :
358 mpTheme( pTheme ),
359 meObjectKind( pTheme ? mpTheme->GetObjectKind(nObjectPos) : SgaObjKind::NONE),
360 mnObjectPos( nObjectPos )
363 InitData( bLazy );
366 void GalleryTransferable::SelectObject(sal_uInt32 nObjectPos)
368 if (nObjectPos == mnObjectPos)
369 return;
370 ClearFormats();
371 mnObjectPos = nObjectPos;
372 meObjectKind = mpTheme ? mpTheme->GetObjectKind(mnObjectPos) : SgaObjKind::NONE;
373 ObjectReleased();
374 InitData(true);
377 GalleryTransferable::~GalleryTransferable()
381 void GalleryTransferable::InitData( bool bLazy )
383 switch( meObjectKind )
385 case SgaObjKind::SvDraw:
387 if( !bLazy )
389 if( !mpGraphicObject )
391 Graphic aGraphic;
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();
404 else
405 mxModelStream->Seek( 0 );
409 break;
411 case SgaObjKind::Animation:
412 case SgaObjKind::Bitmap:
413 case SgaObjKind::Inet:
414 case SgaObjKind::Sound:
416 if( !mpURL )
418 mpURL.reset(new INetURLObject);
420 if (!mpTheme || !mpTheme->GetURL(mnObjectPos, *mpURL))
422 mpURL.reset();
426 if( ( SgaObjKind::Sound != meObjectKind ) && !mpGraphicObject )
428 Graphic aGraphic;
430 if (mpTheme && mpTheme->GetGraphic(mnObjectPos, aGraphic))
431 mpGraphicObject.reset(new GraphicObject( std::move(aGraphic) ));
434 break;
436 default:
437 OSL_FAIL( "GalleryTransferable::GalleryTransferable: invalid object type" );
438 break;
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 );
451 else
453 if( mpURL )
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 );
465 else
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 );
477 bool bRet = false;
479 InitData( false );
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 );
502 return bRet;
505 bool GalleryTransferable::WriteObject( tools::SvRef<SotTempStream>& rxOStm, void* pUserObject,
506 sal_uInt32, const datatransfer::DataFlavor& )
508 bool bRet = false;
510 if( pUserObject )
512 rxOStm->WriteStream( *static_cast< SotStorageStream* >( pUserObject ) );
513 bRet = ( rxOStm->GetError() == ERRCODE_NONE );
516 return bRet;
519 void GalleryTransferable::DragFinished( sal_Int8 nDropAction )
521 if (mpTheme)
523 mpTheme->SetDragging( false );
524 mpTheme->SetDragPos( 0 );
526 if ( nDropAction )
528 vcl::Window *pFocusWindow = Application::GetFocusWindow();
529 if ( pFocusWindow )
530 pFocusWindow->GrabFocusToDocument();
534 void GalleryTransferable::ObjectReleased()
536 mxModelStream.clear();
537 mpGraphicObject.reset();
538 mpURL.reset();
541 bool GalleryTransferable::StartDrag()
543 INetURLObject aURL;
544 if (mpTheme && mpTheme->GetURL(mnObjectPos, aURL) && aURL.GetProtocol() != INetProtocol::NotValid)
546 mpTheme->SetDragging( true );
547 mpTheme->SetDragPos( mnObjectPos );
548 return false;
550 return true;
553 INetURLObject ImplGetURL(const GalleryObject* pObject)
555 INetURLObject aURL;
557 if (pObject)
558 aURL = *pObject->m_oStorageUrl;
560 return aURL;
563 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */