Branch libreoffice-5-0-4
[LibreOffice.git] / sfx2 / source / appl / fileobj.cxx
blob761666173002fed359b4087f5736f2517d2ce154
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 <vcl/wrkwin.hxx>
21 #include <vcl/msgbox.hxx>
22 #include <tools/urlobj.hxx>
23 #include <tools/stream.hxx>
24 #include <sot/formats.hxx>
25 #include <vcl/graphicfilter.hxx>
26 #include <sfx2/lnkbase.hxx>
27 #include <sfx2/app.hxx>
28 #include <sfx2/progress.hxx>
29 #include <sfx2/docfilt.hxx>
30 #include <sfx2/filedlghelper.hxx>
31 #include <sot/exchange.hxx>
32 #include <com/sun/star/uno/Any.hxx>
33 #include <com/sun/star/uno/Sequence.hxx>
34 #include <sfx2/docfac.hxx>
35 #include <com/sun/star/document/XTypeDetection.hpp>
36 #include <unotools/mediadescriptor.hxx>
37 #include <comphelper/processfactory.hxx>
38 #include <sfx2/linkmgr.hxx>
39 #include <sfx2/opengrf.hxx>
40 #include <sfx2/sfxresid.hxx>
41 #include "fileobj.hxx"
42 #include "app.hrc"
43 #include <vcl/dibtools.hxx>
45 #define FILETYPE_TEXT 1
46 #define FILETYPE_GRF 2
47 #define FILETYPE_OBJECT 3
49 SvFileObject::SvFileObject()
50 : nPostUserEventId(0)
51 , pDelMed(NULL)
52 , pOldParent(NULL)
53 , nType(FILETYPE_TEXT)
54 , bLoadAgain(true)
55 , bSynchron(false)
56 , bLoadError(false)
57 , bWaitForData(false)
58 , bInNewData(false)
59 , bDataReady(false)
60 , bNativFormat(false)
61 , bClearMedium(false)
62 , bStateChangeCalled(false)
63 , bInCallDownload(false)
67 SvFileObject::~SvFileObject()
69 if (xMed.Is())
71 xMed->SetDoneLink( Link<>() );
72 xMed.Clear();
74 if (nPostUserEventId)
75 Application::RemoveUserEvent(nPostUserEventId);
76 delete pDelMed;
79 bool SvFileObject::GetData( ::com::sun::star::uno::Any & rData,
80 const OUString & rMimeType,
81 bool bGetSynchron )
83 SotClipboardFormatId nFmt = SotExchange::RegisterFormatMimeType( rMimeType );
84 switch( nType )
86 case FILETYPE_TEXT:
87 if( SotClipboardFormatId::SIMPLE_FILE == nFmt )
89 // The media in the application must be opened to lookup the
90 // relative file links!! This is done through the link manager
91 // of the Storage.
92 rData <<= OUString( sFileNm );
94 break;
96 case FILETYPE_GRF:
97 if( !bLoadError )
99 SfxMediumRef xTmpMed;
101 if( SotClipboardFormatId::GDIMETAFILE == nFmt || SotClipboardFormatId::BITMAP == nFmt ||
102 SotClipboardFormatId::SVXB == nFmt )
104 Graphic aGrf;
106 // If the native format is reqested, has to be reset at the
107 // end of the flag. Is solely in the sw/ndgrf.cxx used when
108 // the link is removed form GraphicNode.
109 bool bOldNativFormat = bNativFormat;
111 // If about to print, waiting for the data to be available
112 if( bGetSynchron )
114 // call a LoadFile every second time to test the loading
115 if( !xMed.Is() )
116 LoadFile_Impl();
118 if( !bInCallDownload )
120 xTmpMed = xMed;
121 while( bWaitForData )
122 Application::Reschedule();
124 xMed = xTmpMed;
125 bClearMedium = true;
129 if( !bWaitForData && ( xMed.Is() || // was loaded as URL
130 ( bSynchron && LoadFile_Impl() && xMed.Is() ) ) )
132 // If it was loaded from the Internet, do not retry
133 if( !bGetSynchron )
134 bLoadAgain = !xMed->IsRemote();
135 bLoadError = !GetGraphic_Impl( aGrf, xMed->GetInStream() );
137 else if( !LoadFile_Impl() ||
138 !GetGraphic_Impl( aGrf, xMed.Is() ? xMed->GetInStream() : 0 ))
140 if( !xMed.Is() )
141 break;
142 aGrf.SetDefaultType();
145 if( SotClipboardFormatId::SVXB != nFmt )
146 nFmt = (bLoadError || GRAPHIC_BITMAP == aGrf.GetType())
147 ? SotClipboardFormatId::BITMAP
148 : SotClipboardFormatId::GDIMETAFILE;
150 SvMemoryStream aMemStm( 0, 65535 );
151 switch ( nFmt )
153 case SotClipboardFormatId::SVXB:
154 if( GRAPHIC_NONE != aGrf.GetType() )
156 aMemStm.SetVersion( SOFFICE_FILEFORMAT_50 );
157 WriteGraphic( aMemStm, aGrf );
159 break;
161 case SotClipboardFormatId::BITMAP:
163 const Bitmap aBitmap(aGrf.GetBitmap());
165 if(!aBitmap.IsEmpty())
167 WriteDIB(aBitmap, aMemStm, false, true);
170 break;
173 default:
174 if( aGrf.GetGDIMetaFile().GetActionSize() )
176 GDIMetaFile aMeta( aGrf.GetGDIMetaFile() );
177 aMeta.Write( aMemStm );
180 rData <<= css::uno::Sequence< sal_Int8 >( static_cast<sal_Int8 const *>(aMemStm.GetData()),
181 aMemStm.Seek( STREAM_SEEK_TO_END ) );
183 bNativFormat = bOldNativFormat;
185 // Everything ready?
186 if( xMed.Is() && !bSynchron && bClearMedium )
188 xMed.Clear();
189 bClearMedium = false;
193 break;
194 case FILETYPE_OBJECT:
195 // TODO/LATER: possibility to insert a new object
196 rData <<= OUString( sFileNm );
197 break;
199 return true/*0 != aTypeList.Count()*/;
202 bool SvFileObject::Connect( sfx2::SvBaseLink* pLink )
204 if( !pLink || !pLink->GetLinkManager() )
205 return false;
207 // Test if not another link of the same connection already exists
208 sfx2::LinkManager::GetDisplayNames( pLink, 0, &sFileNm, 0, &sFilter );
210 if( OBJECT_CLIENT_GRF == pLink->GetObjType() )
212 SfxObjectShellRef pShell = pLink->GetLinkManager()->GetPersist();
213 if( pShell.Is() )
215 if( pShell->IsAbortingImport() )
216 return false;
218 if( pShell->GetMedium() )
219 sReferer = pShell->GetMedium()->GetName();
223 switch( pLink->GetObjType() )
225 case OBJECT_CLIENT_GRF:
226 nType = FILETYPE_GRF;
227 bSynchron = pLink->IsSynchron();
228 break;
230 case OBJECT_CLIENT_FILE:
231 nType = FILETYPE_TEXT;
232 break;
234 case OBJECT_CLIENT_OLE:
235 nType = FILETYPE_OBJECT;
236 // TODO/LATER: introduce own type to be used for exchanging
237 break;
239 default:
240 return false;
243 SetUpdateTimeout( 0 );
245 // and now register by this or other found Pseudo-Object
246 AddDataAdvise( pLink, SotExchange::GetFormatMimeType( pLink->GetContentType()), 0 );
247 return true;
250 bool SvFileObject::LoadFile_Impl()
252 // We are still at Loading!!
253 if( bWaitForData || !bLoadAgain || xMed.Is() )
254 return false;
256 // at the moment on the current DocShell
257 xMed = new SfxMedium( sFileNm, sReferer, STREAM_STD_READ );
258 SvLinkSource::StreamToLoadFrom aStreamToLoadFrom =
259 getStreamToLoadFrom();
260 xMed->setStreamToLoadFrom(
261 aStreamToLoadFrom.m_xInputStreamToLoadFrom,
262 aStreamToLoadFrom.m_bIsReadOnly);
264 if( !bSynchron )
266 bLoadAgain = bDataReady = bInNewData = false;
267 bWaitForData = true;
269 SfxMediumRef xTmpMed = xMed;
270 bInCallDownload = true;
271 xMed->Download( LINK( this, SvFileObject, LoadGrfReady_Impl ) );
272 bInCallDownload = false;
274 bClearMedium = !xMed.Is();
275 if( bClearMedium )
276 xMed = xTmpMed; // If already finished in Download
277 return bDataReady;
280 bWaitForData = true;
281 bDataReady = bInNewData = false;
282 xMed->Download();
283 bLoadAgain = !xMed->IsRemote();
284 bWaitForData = false;
286 // Graphic is finished, also send DataChanged of the Status change:
287 SendStateChg_Impl( xMed->GetInStream() && xMed->GetInStream()->GetError()
288 ? sfx2::LinkManager::STATE_LOAD_ERROR : sfx2::LinkManager::STATE_LOAD_OK );
289 return true;
293 bool SvFileObject::GetGraphic_Impl( Graphic& rGrf, SvStream* pStream )
295 GraphicFilter& rGF = GraphicFilter::GetGraphicFilter();
297 const sal_uInt16 nFilter = !sFilter.isEmpty() && rGF.GetImportFormatCount()
298 ? rGF.GetImportFormatNumber( sFilter )
299 : GRFILTER_FORMAT_DONTKNOW;
301 int nRes;
303 // To avoid that a native link is created
304 if( !rGrf.IsLink() &&
305 !rGrf.GetContext() && !bNativFormat )
306 rGrf.SetLink( GfxLink() );
308 if( !pStream )
309 nRes = xMed.Is() ? GRFILTER_OPENERROR
310 : rGF.ImportGraphic( rGrf, INetURLObject(sFileNm),
311 nFilter );
312 else
314 pStream->Seek( STREAM_SEEK_TO_BEGIN );
316 // #i123042# for e.g. SVG the path is needed, see same TaskID in svx for more info
317 nRes = rGF.ImportGraphic( rGrf, sFileNm, *pStream, nFilter );
320 if( pStream && ERRCODE_IO_PENDING == pStream->GetError() )
321 pStream->ResetError();
323 if( nRes )
325 if( xMed.Is() && !pStream )
326 SAL_WARN( "sfx.appl", "Graphic error [" << nRes << "] - [" << xMed->GetPhysicalName() << "] URL[" << sFileNm << "]" );
327 else
328 SAL_WARN( "sfx.appl", "Graphic error [" << nRes << "] - [" << sFileNm << "]" );
331 return GRFILTER_OK == nRes;
334 /** detect the filter of the given file
336 @param _rURL
337 specifies the URL of the file which filter is to detected.<br/>
338 If the URL doesn't denote a valid (existent and accessible) file, the
339 request is silently dropped.
341 OUString impl_getFilter( const OUString& _rURL )
343 OUString sFilter;
344 if ( _rURL.isEmpty() )
345 return sFilter;
349 css::uno::Reference< ::com::sun::star::document::XTypeDetection > xTypeDetection(
350 ::comphelper::getProcessServiceFactory()->createInstance(
351 OUString("com.sun.star.document.TypeDetection") ),
352 css::uno::UNO_QUERY );
353 if ( xTypeDetection.is() )
355 utl::MediaDescriptor aDescr;
356 aDescr[ utl::MediaDescriptor::PROP_URL() ] <<= OUString( _rURL );
357 css::uno::Sequence< css::beans::PropertyValue > aDescrList =
358 aDescr.getAsConstPropertyValueList();
359 OUString sType = xTypeDetection->queryTypeByDescriptor( aDescrList, sal_True );
360 if ( !sType.isEmpty() )
362 // Honor a selected/detected filter.
363 for (sal_Int32 i=0; i < aDescrList.getLength(); ++i)
365 if (aDescrList[i].Name == "FilterName")
367 if (aDescrList[i].Value >>= sFilter)
368 break;
371 if (sFilter.isEmpty())
373 css::uno::Reference< css::container::XNameAccess > xTypeCont( xTypeDetection,
374 css::uno::UNO_QUERY );
375 if ( xTypeCont.is() )
377 /* XXX: for fdo#69948 scenario the sequence returned by
378 * getByName() contains an empty PreferredFilter
379 * property value (since? expected?) */
380 ::comphelper::SequenceAsHashMap lTypeProps( xTypeCont->getByName( sType ) );
381 sFilter = lTypeProps.getUnpackedValueOrDefault(
382 OUString("PreferredFilter"), OUString() );
388 catch( const css::uno::Exception& )
392 return sFilter;
395 void SvFileObject::Edit( vcl::Window* pParent, sfx2::SvBaseLink* pLink, const Link<>& rEndEditHdl )
397 aEndEditLink = rEndEditHdl;
398 OUString sFile, sRange, sTmpFilter;
399 if( pLink && pLink->GetLinkManager() )
401 sfx2::LinkManager::GetDisplayNames( pLink, 0, &sFile, &sRange, &sTmpFilter );
403 switch( pLink->GetObjType() )
405 case OBJECT_CLIENT_GRF:
407 nType = FILETYPE_GRF; // If not set already
409 SvxOpenGraphicDialog aDlg(SfxResId(RID_SVXSTR_EDITGRFLINK).toString());
410 aDlg.EnableLink(false);
411 aDlg.SetPath( sFile, true );
412 aDlg.SetCurrentFilter( sTmpFilter );
414 if( !aDlg.Execute() )
416 sFile = aDlg.GetPath();
417 sFile += OUString(::sfx2::cTokenSeparator);
418 sFile += OUString(::sfx2::cTokenSeparator);
419 sFile += aDlg.GetCurrentFilter();
421 if ( aEndEditLink.IsSet() )
422 aEndEditLink.Call( &sFile );
424 else
425 sFile.clear();
427 break;
429 case OBJECT_CLIENT_OLE:
431 nType = FILETYPE_OBJECT; // if not set already
432 pOldParent = Application::GetDefDialogParent();
433 Application::SetDefDialogParent( pParent );
435 ::sfx2::FileDialogHelper & rFileDlg =
436 pLink->GetInsertFileDialog( OUString() );
437 rFileDlg.StartExecuteModal(
438 LINK( this, SvFileObject, DialogClosedHdl ) );
440 break;
442 case OBJECT_CLIENT_FILE:
444 nType = FILETYPE_TEXT; // if not set already
445 pOldParent = Application::GetDefDialogParent();
446 Application::SetDefDialogParent( pParent );
448 OUString sFactory;
449 SfxObjectShell* pShell = pLink->GetLinkManager()->GetPersist();
450 if ( pShell )
451 sFactory = pShell->GetFactory().GetFactoryName();
453 ::sfx2::FileDialogHelper & rFileDlg =
454 pLink->GetInsertFileDialog(sFactory);
455 rFileDlg.StartExecuteModal(
456 LINK( this, SvFileObject, DialogClosedHdl ) );
458 break;
460 default:
461 sFile.clear();
466 IMPL_LINK_NOARG( SvFileObject, LoadGrfReady_Impl )
468 // When we come form here there it can not be an error no more.
469 bLoadError = false;
470 bWaitForData = false;
471 bInCallDownload = false;
473 if( !bInNewData && !bDataReady )
475 // Graphic is finished, also send DataChanged from Status change
476 bDataReady = true;
477 SendStateChg_Impl( sfx2::LinkManager::STATE_LOAD_OK );
479 // and then send the data again
480 NotifyDataChanged();
483 if( bDataReady )
485 bLoadAgain = true;
486 if( xMed.Is() )
488 xMed->SetDoneLink( Link<>() );
489 pDelMed = new SfxMediumRef(xMed);
490 nPostUserEventId = Application::PostUserEvent(
491 LINK( this, SvFileObject, DelMedium_Impl ),
492 pDelMed);
493 xMed.Clear();
497 return 0;
500 IMPL_LINK( SvFileObject, DelMedium_Impl, SfxMediumRef*, deleteMedium )
502 nPostUserEventId = 0;
503 assert(pDelMed == deleteMedium);
504 pDelMed = NULL;
505 delete deleteMedium;
506 return 0;
509 IMPL_LINK( SvFileObject, DialogClosedHdl, sfx2::FileDialogHelper*, _pFileDlg )
511 OUString sFile;
512 Application::SetDefDialogParent( pOldParent );
514 if ( FILETYPE_TEXT == nType || FILETYPE_OBJECT == nType )
516 if ( _pFileDlg && _pFileDlg->GetError() == ERRCODE_NONE )
518 OUString sURL( _pFileDlg->GetPath() );
519 sFile = sURL;
520 sFile += OUString(::sfx2::cTokenSeparator);
521 sFile += OUString(::sfx2::cTokenSeparator);
522 sFile += impl_getFilter( sURL );
525 else
527 SAL_WARN( "sfx.appl", "SvFileObject::DialogClosedHdl(): wrong file type" );
530 if ( aEndEditLink.IsSet() )
531 aEndEditLink.Call( &sFile );
532 return 0;
535 /* [Description]
537 The method determines whether the data-object can be read from a DDE.
539 The following can be returned:
540 ERRCODE_NONE if it has been completely read
541 ERRCODE_SO_PENDING if it has not been completely read
542 ERRCODE_SO_FALSE otherwise
544 bool SvFileObject::IsPending() const
546 return FILETYPE_GRF == nType && !bLoadError && bWaitForData;
549 bool SvFileObject::IsDataComplete() const
551 bool bRet = false;
552 if( FILETYPE_GRF != nType )
553 bRet = true;
554 else if( !bLoadError && !bWaitForData )
556 SvFileObject* pThis = const_cast<SvFileObject*>(this);
557 if( bDataReady ||
558 ( bSynchron && pThis->LoadFile_Impl() && xMed.Is() ) )
559 bRet = true;
560 else
562 INetURLObject aUrl( sFileNm );
563 if( aUrl.HasError() ||
564 INetProtocol::NotValid == aUrl.GetProtocol() )
565 bRet = true;
568 return bRet;
573 void SvFileObject::CancelTransfers()
575 // unsubscribe from the cache if in the middle of loading
576 if( !bDataReady )
578 // Do not set-up again
579 bLoadAgain = false;
580 bDataReady = bLoadError = bWaitForData = true;
581 SendStateChg_Impl( sfx2::LinkManager::STATE_LOAD_ABORT );
586 void SvFileObject::SendStateChg_Impl( sfx2::LinkManager::LinkState nState )
588 if( !bStateChangeCalled && HasDataLinks() )
590 css::uno::Any aAny;
591 aAny <<= OUString::number( nState );
592 DataChanged( SotExchange::GetFormatName(
593 sfx2::LinkManager::RegisterStatusInfoId()), aAny );
594 bStateChangeCalled = true;
599 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */