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 SvFileObject::SvFileObject()
53 , nType(FILETYPE_TEXT
)
62 , bStateChangeCalled(false)
63 , bInCallDownload(false)
67 SvFileObject::~SvFileObject()
71 xMed
->SetDoneLink( Link
<>() );
75 Application::RemoveUserEvent(nPostUserEventId
);
79 bool SvFileObject::GetData( ::com::sun::star::uno::Any
& rData
,
80 const OUString
& rMimeType
,
83 SotClipboardFormatId nFmt
= SotExchange::RegisterFormatMimeType( rMimeType
);
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
92 rData
<<= OUString( sFileNm
);
101 if( SotClipboardFormatId::GDIMETAFILE
== nFmt
|| SotClipboardFormatId::BITMAP
== nFmt
||
102 SotClipboardFormatId::SVXB
== nFmt
)
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
114 // call a LoadFile every second time to test the loading
118 if( !bInCallDownload
)
121 while( bWaitForData
)
122 Application::Reschedule();
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
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 ))
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 );
153 case SotClipboardFormatId::SVXB
:
154 if( GRAPHIC_NONE
!= aGrf
.GetType() )
156 aMemStm
.SetVersion( SOFFICE_FILEFORMAT_50
);
157 WriteGraphic( aMemStm
, aGrf
);
161 case SotClipboardFormatId::BITMAP
:
163 const Bitmap
aBitmap(aGrf
.GetBitmap());
165 if(!aBitmap
.IsEmpty())
167 WriteDIB(aBitmap
, aMemStm
, false, true);
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
;
186 if( xMed
.Is() && !bSynchron
&& bClearMedium
)
189 bClearMedium
= false;
194 case FILETYPE_OBJECT
:
195 // TODO/LATER: possibility to insert a new object
196 rData
<<= OUString( sFileNm
);
199 return true/*0 != aTypeList.Count()*/;
202 bool SvFileObject::Connect( sfx2::SvBaseLink
* pLink
)
204 if( !pLink
|| !pLink
->GetLinkManager() )
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();
215 if( pShell
->IsAbortingImport() )
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();
230 case OBJECT_CLIENT_FILE
:
231 nType
= FILETYPE_TEXT
;
234 case OBJECT_CLIENT_OLE
:
235 nType
= FILETYPE_OBJECT
;
236 // TODO/LATER: introduce own type to be used for exchanging
243 SetUpdateTimeout( 0 );
245 // and now register by this or other found Pseudo-Object
246 AddDataAdvise( pLink
, SotExchange::GetFormatMimeType( pLink
->GetContentType()), 0 );
250 bool SvFileObject::LoadFile_Impl()
252 // We are still at Loading!!
253 if( bWaitForData
|| !bLoadAgain
|| xMed
.Is() )
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
);
266 bLoadAgain
= bDataReady
= bInNewData
= false;
269 SfxMediumRef xTmpMed
= xMed
;
270 bInCallDownload
= true;
271 xMed
->Download( LINK( this, SvFileObject
, LoadGrfReady_Impl
) );
272 bInCallDownload
= false;
274 bClearMedium
= !xMed
.Is();
276 xMed
= xTmpMed
; // If already finished in Download
281 bDataReady
= bInNewData
= false;
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
);
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
;
303 // To avoid that a native link is created
304 if( !rGrf
.IsLink() &&
305 !rGrf
.GetContext() && !bNativFormat
)
306 rGrf
.SetLink( GfxLink() );
309 nRes
= xMed
.Is() ? GRFILTER_OPENERROR
310 : rGF
.ImportGraphic( rGrf
, INetURLObject(sFileNm
),
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();
325 if( xMed
.Is() && !pStream
)
326 SAL_WARN( "sfx.appl", "Graphic error [" << nRes
<< "] - [" << xMed
->GetPhysicalName() << "] URL[" << sFileNm
<< "]" );
328 SAL_WARN( "sfx.appl", "Graphic error [" << nRes
<< "] - [" << sFileNm
<< "]" );
331 return GRFILTER_OK
== nRes
;
334 /** detect the filter of the given file
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
)
344 if ( _rURL
.isEmpty() )
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
)
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
& )
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
);
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
) );
442 case OBJECT_CLIENT_FILE
:
444 nType
= FILETYPE_TEXT
; // if not set already
445 pOldParent
= Application::GetDefDialogParent();
446 Application::SetDefDialogParent( pParent
);
449 SfxObjectShell
* pShell
= pLink
->GetLinkManager()->GetPersist();
451 sFactory
= pShell
->GetFactory().GetFactoryName();
453 ::sfx2::FileDialogHelper
& rFileDlg
=
454 pLink
->GetInsertFileDialog(sFactory
);
455 rFileDlg
.StartExecuteModal(
456 LINK( this, SvFileObject
, DialogClosedHdl
) );
466 IMPL_LINK_NOARG( SvFileObject
, LoadGrfReady_Impl
)
468 // When we come form here there it can not be an error no more.
470 bWaitForData
= false;
471 bInCallDownload
= false;
473 if( !bInNewData
&& !bDataReady
)
475 // Graphic is finished, also send DataChanged from Status change
477 SendStateChg_Impl( sfx2::LinkManager::STATE_LOAD_OK
);
479 // and then send the data again
488 xMed
->SetDoneLink( Link
<>() );
489 pDelMed
= new SfxMediumRef(xMed
);
490 nPostUserEventId
= Application::PostUserEvent(
491 LINK( this, SvFileObject
, DelMedium_Impl
),
500 IMPL_LINK( SvFileObject
, DelMedium_Impl
, SfxMediumRef
*, deleteMedium
)
502 nPostUserEventId
= 0;
503 assert(pDelMed
== deleteMedium
);
509 IMPL_LINK( SvFileObject
, DialogClosedHdl
, sfx2::FileDialogHelper
*, _pFileDlg
)
512 Application::SetDefDialogParent( pOldParent
);
514 if ( FILETYPE_TEXT
== nType
|| FILETYPE_OBJECT
== nType
)
516 if ( _pFileDlg
&& _pFileDlg
->GetError() == ERRCODE_NONE
)
518 OUString
sURL( _pFileDlg
->GetPath() );
520 sFile
+= OUString(::sfx2::cTokenSeparator
);
521 sFile
+= OUString(::sfx2::cTokenSeparator
);
522 sFile
+= impl_getFilter( sURL
);
527 SAL_WARN( "sfx.appl", "SvFileObject::DialogClosedHdl(): wrong file type" );
530 if ( aEndEditLink
.IsSet() )
531 aEndEditLink
.Call( &sFile
);
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
552 if( FILETYPE_GRF
!= nType
)
554 else if( !bLoadError
&& !bWaitForData
)
556 SvFileObject
* pThis
= const_cast<SvFileObject
*>(this);
558 ( bSynchron
&& pThis
->LoadFile_Impl() && xMed
.Is() ) )
562 INetURLObject
aUrl( sFileNm
);
563 if( aUrl
.HasError() ||
564 INetProtocol::NotValid
== aUrl
.GetProtocol() )
573 void SvFileObject::CancelTransfers()
575 // unsubscribe from the cache if in the middle of loading
578 // Do not set-up again
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() )
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: */