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 <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"
43 #include <vcl/dibtools.hxx>
45 #define FILETYPE_TEXT 1
46 #define FILETYPE_GRF 2
47 #define FILETYPE_OBJECT 3
49 struct Impl_DownLoadData
54 Impl_DownLoadData( const Link
& rLink
)
56 aTimer
.SetTimeout( 100 );
57 aTimer
.SetTimeoutHdl( rLink
);
58 aGrf
.SetDefaultType();
66 // --------------------------------------------------------------------------
69 SvFileObject::SvFileObject() :
70 pDownLoadData( NULL
), pOldParent( NULL
), nType( FILETYPE_TEXT
)
72 bLoadAgain
= sal_True
;
73 bSynchron
= bLoadError
= bWaitForData
= bDataReady
= bNativFormat
=
74 bClearMedium
= bStateChangeCalled
= bInCallDownLoad
= sal_False
;
78 SvFileObject::~SvFileObject()
82 xMed
->SetDoneLink( Link() );
89 sal_Bool
SvFileObject::GetData( ::com::sun::star::uno::Any
& rData
,
90 const OUString
& rMimeType
,
91 sal_Bool bGetSynchron
)
93 sal_uIntPtr nFmt
= SotExchange::GetFormatStringId( rMimeType
);
97 if( FORMAT_FILE
== nFmt
)
99 // The media in the application must be opened to lookup the
100 // relative file links!! This is done through the link manager
102 rData
<<= OUString( sFileNm
);
109 SfxMediumRef xTmpMed
;
111 if( FORMAT_GDIMETAFILE
== nFmt
|| FORMAT_BITMAP
== nFmt
||
112 SOT_FORMATSTR_ID_SVXB
== nFmt
)
116 // If the native format is reqested, has to be reset at the
117 // end of the flag. Is solely in the sw/ndgrf.cxx used when
118 // the link is removed form GraphicNode.
119 sal_Bool bOldNativFormat
= bNativFormat
;
121 // If about to print, waiting for the data to be available
124 // call a LoadFile every second time to test the loading
128 if( !bInCallDownLoad
)
131 while( bWaitForData
)
132 Application::Reschedule();
135 bClearMedium
= sal_True
;
140 ( !bWaitForData
&& ( xMed
.Is() || // was loaded as URL
141 ( bSynchron
&& LoadFile_Impl() && xMed
.Is() ) )) )
143 // If it was loaded from the Internet, do not retry
145 bLoadAgain
= !xMed
->IsRemote();
146 bLoadError
= !GetGraphic_Impl( aGrf
, xMed
->GetInStream() );
148 else if( !LoadFile_Impl() ||
149 !GetGraphic_Impl( aGrf
, xMed
.Is() ? xMed
->GetInStream() : 0 ))
153 aGrf
.SetDefaultType();
156 if( SOT_FORMATSTR_ID_SVXB
!= nFmt
)
157 nFmt
= (bLoadError
|| GRAPHIC_BITMAP
== aGrf
.GetType())
159 : FORMAT_GDIMETAFILE
;
161 SvMemoryStream
aMemStm( 0, 65535 );
164 case SOT_FORMATSTR_ID_SVXB
:
165 if( GRAPHIC_NONE
!= aGrf
.GetType() )
167 aMemStm
.SetVersion( SOFFICE_FILEFORMAT_50
);
174 const Bitmap
aBitmap(aGrf
.GetBitmap());
176 if(!aBitmap
.IsEmpty())
178 WriteDIB(aBitmap
, aMemStm
, false, true);
185 if( aGrf
.GetGDIMetaFile().GetActionSize() )
187 GDIMetaFile
aMeta( aGrf
.GetGDIMetaFile() );
188 aMeta
.Write( aMemStm
);
191 rData
<<= css::uno::Sequence
< sal_Int8
>( (sal_Int8
*) aMemStm
.GetData(),
192 aMemStm
.Seek( STREAM_SEEK_TO_END
) );
194 bNativFormat
= bOldNativFormat
;
197 if( xMed
.Is() && !bSynchron
&& bClearMedium
)
200 bClearMedium
= sal_False
;
205 case FILETYPE_OBJECT
:
206 // TODO/LATER: possibility to insert a new object
207 rData
<<= OUString( sFileNm
);
210 return sal_True
/*0 != aTypeList.Count()*/;
213 sal_Bool
SvFileObject::Connect( sfx2::SvBaseLink
* pLink
)
215 if( !pLink
|| !pLink
->GetLinkManager() )
218 // Test if not another link of the same connection already exists
219 pLink
->GetLinkManager()->GetDisplayNames( pLink
, 0, &sFileNm
, 0, &sFilter
);
221 if( OBJECT_CLIENT_GRF
== pLink
->GetObjType() )
223 SfxObjectShellRef pShell
= pLink
->GetLinkManager()->GetPersist();
226 if( pShell
->IsAbortingImport() )
229 if( pShell
->GetMedium() )
230 sReferer
= pShell
->GetMedium()->GetName();
234 switch( pLink
->GetObjType() )
236 case OBJECT_CLIENT_GRF
:
237 nType
= FILETYPE_GRF
;
238 bSynchron
= pLink
->IsSynchron();
241 case OBJECT_CLIENT_FILE
:
242 nType
= FILETYPE_TEXT
;
245 case OBJECT_CLIENT_OLE
:
246 nType
= FILETYPE_OBJECT
;
247 // TODO/LATER: introduce own type to be used for exchanging
254 SetUpdateTimeout( 0 );
256 // and now register by this or other found Pseudo-Object
257 AddDataAdvise( pLink
, SotExchange::GetFormatMimeType( pLink
->GetContentType()), 0 );
261 sal_Bool
SvFileObject::LoadFile_Impl()
263 // We are still at Loading!!
264 if( bWaitForData
|| !bLoadAgain
|| xMed
.Is() || pDownLoadData
)
267 // at the moment on the current DocShell
268 xMed
= new SfxMedium( sFileNm
, sReferer
, STREAM_STD_READ
);
269 SvLinkSource::StreamToLoadFrom aStreamToLoadFrom
=
270 getStreamToLoadFrom();
271 xMed
->setStreamToLoadFrom(
272 aStreamToLoadFrom
.m_xInputStreamToLoadFrom
,
273 aStreamToLoadFrom
.m_bIsReadOnly
);
277 bLoadAgain
= bDataReady
= bInNewData
= sal_False
;
278 bWaitForData
= sal_True
;
280 SfxMediumRef xTmpMed
= xMed
;
281 bInCallDownLoad
= sal_True
;
282 xMed
->DownLoad( STATIC_LINK( this, SvFileObject
, LoadGrfReady_Impl
) );
283 bInCallDownLoad
= sal_False
;
285 bClearMedium
= !xMed
.Is();
287 xMed
= xTmpMed
; // If already finished in DownLoad
291 bWaitForData
= sal_True
;
292 bDataReady
= bInNewData
= sal_False
;
294 bLoadAgain
= !xMed
->IsRemote();
295 bWaitForData
= sal_False
;
297 // Graphic is finished, also send DataChanged of the Status change:
298 SendStateChg_Impl( xMed
->GetInStream() && xMed
->GetInStream()->GetError()
299 ? sfx2::LinkManager::STATE_LOAD_ERROR
: sfx2::LinkManager::STATE_LOAD_OK
);
304 sal_Bool
SvFileObject::GetGraphic_Impl( Graphic
& rGrf
, SvStream
* pStream
)
306 GraphicFilter
& rGF
= GraphicFilter::GetGraphicFilter();
308 const sal_uInt16 nFilter
= !sFilter
.isEmpty() && rGF
.GetImportFormatCount()
309 ? rGF
.GetImportFormatNumber( sFilter
)
310 : GRFILTER_FORMAT_DONTKNOW
;
315 // To avoid that a native link is created
316 if( ( !pStream
|| !pDownLoadData
) && !rGrf
.IsLink() &&
317 !rGrf
.GetContext() && !bNativFormat
)
318 rGrf
.SetLink( GfxLink() );
321 nRes
= xMed
.Is() ? GRFILTER_OPENERROR
322 : rGF
.ImportGraphic( rGrf
, INetURLObject(sFileNm
),
324 else if( !pDownLoadData
)
326 pStream
->Seek( STREAM_SEEK_TO_BEGIN
);
327 nRes
= rGF
.ImportGraphic( rGrf
, aEmptyStr
, *pStream
, nFilter
);
331 nRes
= rGF
.ImportGraphic( pDownLoadData
->aGrf
, aEmptyStr
,
336 rGrf
= pDownLoadData
->aGrf
;
337 if( GRAPHIC_NONE
== rGrf
.GetType() )
338 rGrf
.SetDefaultType();
341 if( !pDownLoadData
->aGrf
.GetContext() )
343 delete pDownLoadData
, pDownLoadData
= 0;
344 bDataReady
= sal_True
;
345 bWaitForData
= sal_False
;
350 if( pStream
&& ERRCODE_IO_PENDING
== pStream
->GetError() )
351 pStream
->ResetError();
356 if( xMed
.Is() && !pStream
)
358 DBG_WARNING3( "Graphic error [%d] - [%s] URL[%s]",
360 xMed
->GetPhysicalName().getStr(),
365 DBG_WARNING2( "Graphic error [%d] - [%s]",
366 nRes
, sFileNm
.getStr() );
371 return GRFILTER_OK
== nRes
;
374 /** detect the filter of the given file
377 specifies the URL of the file which filter is to detected.<br/>
378 If the URL doesn't denote a valid (existent and accessible) file, the
379 request is silently dropped.
381 OUString
impl_getFilter( const OUString
& _rURL
)
384 if ( _rURL
.isEmpty() )
389 css::uno::Reference
< ::com::sun::star::document::XTypeDetection
> xTypeDetection(
390 ::comphelper::getProcessServiceFactory()->createInstance(
391 OUString("com.sun.star.document.TypeDetection") ),
392 css::uno::UNO_QUERY
);
393 if ( xTypeDetection
.is() )
395 utl::MediaDescriptor aDescr
;
396 aDescr
[ utl::MediaDescriptor::PROP_URL() ] <<= OUString( _rURL
);
397 css::uno::Sequence
< css::beans::PropertyValue
> aDescrList
=
398 aDescr
.getAsConstPropertyValueList();
399 OUString sType
= xTypeDetection
->queryTypeByDescriptor( aDescrList
, sal_True
);
400 if ( !sType
.isEmpty() )
402 // Honor a selected/detected filter.
403 for (sal_Int32 i
=0; i
< aDescrList
.getLength(); ++i
)
405 if (aDescrList
[i
].Name
== "FilterName")
407 if (aDescrList
[i
].Value
>>= sFilter
)
411 if (sFilter
.isEmpty())
413 css::uno::Reference
< css::container::XNameAccess
> xTypeCont( xTypeDetection
,
414 css::uno::UNO_QUERY
);
415 if ( xTypeCont
.is() )
417 /* XXX: for fdo#69948 scenario the sequence returned by
418 * getByName() contains an empty PreferredFilter
419 * property value (since? expected?) */
420 ::comphelper::SequenceAsHashMap
lTypeProps( xTypeCont
->getByName( sType
) );
421 sFilter
= lTypeProps
.getUnpackedValueOrDefault(
422 OUString("PreferredFilter"), OUString() );
428 catch( const css::uno::Exception
& )
435 void SvFileObject::Edit( Window
* pParent
, sfx2::SvBaseLink
* pLink
, const Link
& rEndEditHdl
)
437 aEndEditLink
= rEndEditHdl
;
438 OUString sFile
, sRange
, sTmpFilter
;
439 if( pLink
&& pLink
->GetLinkManager() )
441 pLink
->GetLinkManager()->GetDisplayNames( pLink
, 0, &sFile
, &sRange
, &sTmpFilter
);
443 switch( pLink
->GetObjType() )
445 case OBJECT_CLIENT_GRF
:
447 nType
= FILETYPE_GRF
; // If not set already
449 SvxOpenGraphicDialog
aDlg(SfxResId(RID_SVXSTR_EDITGRFLINK
).toString());
450 aDlg
.EnableLink(sal_False
);
451 aDlg
.SetPath( sFile
, sal_True
);
452 aDlg
.SetCurrentFilter( sTmpFilter
);
454 if( !aDlg
.Execute() )
456 sFile
= aDlg
.GetPath();
457 sFile
+= OUString(::sfx2::cTokenSeparator
);
458 sFile
+= OUString(::sfx2::cTokenSeparator
);
459 sFile
+= aDlg
.GetCurrentFilter();
461 if ( aEndEditLink
.IsSet() )
462 aEndEditLink
.Call( &sFile
);
469 case OBJECT_CLIENT_OLE
:
471 nType
= FILETYPE_OBJECT
; // if not set already
472 pOldParent
= Application::GetDefDialogParent();
473 Application::SetDefDialogParent( pParent
);
475 ::sfx2::FileDialogHelper
& rFileDlg
=
476 pLink
->GetInsertFileDialog( OUString() );
477 rFileDlg
.StartExecuteModal(
478 LINK( this, SvFileObject
, DialogClosedHdl
) );
482 case OBJECT_CLIENT_FILE
:
484 nType
= FILETYPE_TEXT
; // if not set already
485 pOldParent
= Application::GetDefDialogParent();
486 Application::SetDefDialogParent( pParent
);
489 SfxObjectShell
* pShell
= pLink
->GetLinkManager()->GetPersist();
491 sFactory
= pShell
->GetFactory().GetFactoryName();
493 ::sfx2::FileDialogHelper
& rFileDlg
=
494 pLink
->GetInsertFileDialog(sFactory
);
495 rFileDlg
.StartExecuteModal(
496 LINK( this, SvFileObject
, DialogClosedHdl
) );
506 IMPL_STATIC_LINK( SvFileObject
, LoadGrfReady_Impl
, void*, EMPTYARG
)
508 // When we come form here there it can not be an error no more.
509 pThis
->bLoadError
= sal_False
;
510 pThis
->bWaitForData
= sal_False
;
511 pThis
->bInCallDownLoad
= sal_False
;
513 if( !pThis
->bInNewData
&& !pThis
->bDataReady
)
515 // Graphic is finished, also send DataChanged from Status change
516 pThis
->bDataReady
= sal_True
;
517 pThis
->SendStateChg_Impl( sfx2::LinkManager::STATE_LOAD_OK
);
519 // and then send the data again
520 pThis
->NotifyDataChanged();
523 if( pThis
->bDataReady
)
525 pThis
->bLoadAgain
= sal_True
;
526 if( pThis
->xMed
.Is() )
528 pThis
->xMed
->SetDoneLink( Link() );
530 Application::PostUserEvent(
531 STATIC_LINK( pThis
, SvFileObject
, DelMedium_Impl
),
532 new SfxMediumRef( pThis
->xMed
));
535 if( pThis
->pDownLoadData
)
536 delete pThis
->pDownLoadData
, pThis
->pDownLoadData
= 0;
542 IMPL_STATIC_LINK( SvFileObject
, DelMedium_Impl
, SfxMediumRef
*, pDelMed
)
549 IMPL_LINK( SvFileObject
, DialogClosedHdl
, sfx2::FileDialogHelper
*, _pFileDlg
)
552 Application::SetDefDialogParent( pOldParent
);
554 if ( FILETYPE_TEXT
== nType
|| FILETYPE_OBJECT
== nType
)
556 if ( _pFileDlg
&& _pFileDlg
->GetError() == ERRCODE_NONE
)
558 OUString
sURL( _pFileDlg
->GetPath() );
560 sFile
+= OUString(::sfx2::cTokenSeparator
);
561 sFile
+= OUString(::sfx2::cTokenSeparator
);
562 sFile
+= impl_getFilter( sURL
);
567 SAL_WARN( "sfx.appl", "SvFileObject::DialogClosedHdl(): wrong file type" );
570 if ( aEndEditLink
.IsSet() )
571 aEndEditLink
.Call( &sFile
);
577 The method determines whether the data-object can be read from a DDE.
579 The following can be returned:
580 ERRCODE_NONE if it has been completely read
581 ERRCODE_SO_PENDING if it has not been completely read
582 ERRCODE_SO_FALSE otherwise
584 sal_Bool
SvFileObject::IsPending() const
586 return FILETYPE_GRF
== nType
&& !bLoadError
&&
587 ( pDownLoadData
|| bWaitForData
);
589 sal_Bool
SvFileObject::IsDataComplete() const
591 sal_Bool bRet
= sal_False
;
592 if( FILETYPE_GRF
!= nType
)
594 else if( !bLoadError
&& ( !bWaitForData
&& !pDownLoadData
))
596 SvFileObject
* pThis
= (SvFileObject
*)this;
598 ( bSynchron
&& pThis
->LoadFile_Impl() && xMed
.Is() ) )
602 INetURLObject
aUrl( sFileNm
);
603 if( aUrl
.HasError() ||
604 INET_PROT_NOT_VALID
== aUrl
.GetProtocol() )
613 void SvFileObject::CancelTransfers()
615 // unsubscribe from the cache if in the middle of loading
618 // Do not set-up again
619 bLoadAgain
= sal_False
;
620 bDataReady
= bLoadError
= bWaitForData
= sal_True
;
621 SendStateChg_Impl( sfx2::LinkManager::STATE_LOAD_ABORT
);
626 void SvFileObject::SendStateChg_Impl( sfx2::LinkManager::LinkState nState
)
628 if( !bStateChangeCalled
&& HasDataLinks() )
631 aAny
<<= OUString::number( nState
);
632 DataChanged( SotExchange::GetFormatName(
633 sfx2::LinkManager::RegisterStatusInfoId()), aAny
);
634 bStateChangeCalled
= sal_True
;
639 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */