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 <tools/urlobj.hxx>
21 #include <tools/stream.hxx>
22 #include <sot/formats.hxx>
23 #include <sal/log.hxx>
24 #include <sfx2/lnkbase.hxx>
25 #include <sfx2/filedlghelper.hxx>
26 #include <sot/exchange.hxx>
27 #include <com/sun/star/uno/Any.hxx>
28 #include <com/sun/star/uno/Sequence.hxx>
29 #include <sfx2/docfac.hxx>
30 #include <com/sun/star/document/XTypeDetection.hpp>
31 #include <com/sun/star/container/XNameAccess.hpp>
32 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33 #include <unotools/mediadescriptor.hxx>
34 #include <comphelper/processfactory.hxx>
35 #include <sfx2/linkmgr.hxx>
36 #include <sfx2/opengrf.hxx>
37 #include <sfx2/sfxresid.hxx>
38 #include <sfx2/objsh.hxx>
39 #include "fileobj.hxx"
40 #include <sfx2/strings.hrc>
41 #include <vcl/svapp.hxx>
43 enum class SvFileObjectType
45 Text
= 1, Graphic
= 2, Object
= 3
48 SvFileObject::SvFileObject()
49 : nPostUserEventId(nullptr)
50 , nType(SvFileObjectType::Text
)
57 , bStateChangeCalled(false)
61 SvFileObject::~SvFileObject()
65 xMed
->SetDoneLink( Link
<void*,void>() );
69 Application::RemoveUserEvent(nPostUserEventId
);
72 bool SvFileObject::GetData( css::uno::Any
& rData
,
73 const OUString
& rMimeType
,
74 bool /*bGetSynchron*/ )
76 SotClipboardFormatId nFmt
= SotExchange::RegisterFormatMimeType( rMimeType
);
79 case SvFileObjectType::Text
:
80 if( SotClipboardFormatId::SIMPLE_FILE
== nFmt
)
82 // The media in the application must be opened to lookup the
83 // relative file links!! This is done through the link manager
89 case SvFileObjectType::Graphic
:
90 if (SotClipboardFormatId::GDIMETAFILE
== nFmt
91 || SotClipboardFormatId::BITMAP
== nFmt
92 || SotClipboardFormatId::SVXB
== nFmt
)
97 case SvFileObjectType::Object
:
98 // TODO/LATER: possibility to insert a new object
102 return true/*0 != aTypeList.Count()*/;
105 bool SvFileObject::Connect( sfx2::SvBaseLink
* pLink
)
107 if( !pLink
|| !pLink
->GetLinkManager() )
110 // Test if not another link of the same connection already exists
111 sfx2::LinkManager::GetDisplayNames( pLink
, nullptr, &sFileNm
, nullptr, &sFilter
);
113 if( sfx2::SvBaseLinkObjectType::ClientGraphic
== pLink
->GetObjType() )
115 SfxObjectShellRef pShell
= pLink
->GetLinkManager()->GetPersist();
118 if( pShell
->IsAbortingImport() )
121 if( pShell
->GetMedium() )
122 sReferer
= pShell
->GetMedium()->GetName();
126 switch( pLink
->GetObjType() )
128 case sfx2::SvBaseLinkObjectType::ClientGraphic
:
129 nType
= SvFileObjectType::Graphic
;
130 bSynchron
= pLink
->IsSynchron();
133 case sfx2::SvBaseLinkObjectType::ClientFile
:
134 nType
= SvFileObjectType::Text
;
137 case sfx2::SvBaseLinkObjectType::ClientOle
:
138 nType
= SvFileObjectType::Object
;
139 // TODO/LATER: introduce own type to be used for exchanging
146 SetUpdateTimeout( 0 );
148 // and now register by this or other found Pseudo-Object
149 AddDataAdvise( pLink
, SotExchange::GetFormatMimeType( pLink
->GetContentType()), 0 );
153 bool SvFileObject::LoadFile_Impl()
155 // We are still at Loading!!
156 if( bWaitForData
|| !bLoadAgain
|| xMed
.is() )
159 // at the moment on the current DocShell
160 xMed
= new SfxMedium( sFileNm
, sReferer
, StreamMode::STD_READ
);
161 SvLinkSource::StreamToLoadFrom aStreamToLoadFrom
=
162 getStreamToLoadFrom();
163 xMed
->setStreamToLoadFrom(
164 aStreamToLoadFrom
.m_xInputStreamToLoadFrom
,
165 aStreamToLoadFrom
.m_bIsReadOnly
);
169 bLoadAgain
= bDataReady
= false;
172 tools::SvRef
<SfxMedium
> xTmpMed
= xMed
;
173 xMed
->Download( LINK( this, SvFileObject
, LoadGrfReady_Impl
) );
175 bClearMedium
= !xMed
.is();
177 xMed
= std::move(xTmpMed
); // If already finished in Download
184 bLoadAgain
= !xMed
->IsRemote();
185 bWaitForData
= false;
187 // Graphic is finished, also send DataChanged of the Status change:
188 SendStateChg_Impl( xMed
->GetInStream() && xMed
->GetInStream()->GetError()
189 ? sfx2::LinkManager::STATE_LOAD_ERROR
: sfx2::LinkManager::STATE_LOAD_OK
);
194 /** detect the filter of the given file
197 specifies the URL of the file which filter is to detected.<br/>
198 If the URL doesn't denote a valid (existent and accessible) file, the
199 request is silently dropped.
201 static OUString
impl_getFilter( const OUString
& _rURL
)
204 if ( _rURL
.isEmpty() )
209 css::uno::Reference
< css::document::XTypeDetection
> xTypeDetection(
210 ::comphelper::getProcessServiceFactory()->createInstance( u
"com.sun.star.document.TypeDetection"_ustr
),
211 css::uno::UNO_QUERY
);
212 if ( xTypeDetection
.is() )
214 utl::MediaDescriptor aDescr
;
215 aDescr
[ utl::MediaDescriptor::PROP_URL
] <<= _rURL
;
216 css::uno::Sequence
< css::beans::PropertyValue
> aDescrList
=
217 aDescr
.getAsConstPropertyValueList();
218 OUString sType
= xTypeDetection
->queryTypeByDescriptor( aDescrList
, true );
219 if ( !sType
.isEmpty() )
221 // Honor a selected/detected filter.
222 for (const auto& rDescr
: aDescrList
)
224 if (rDescr
.Name
== "FilterName")
226 if (rDescr
.Value
>>= sFilter
)
230 if (sFilter
.isEmpty())
232 css::uno::Reference
< css::container::XNameAccess
> xTypeCont( xTypeDetection
,
233 css::uno::UNO_QUERY
);
234 if ( xTypeCont
.is() )
236 /* XXX: for fdo#69948 scenario the sequence returned by
237 * getByName() contains an empty PreferredFilter
238 * property value (since? expected?) */
239 ::comphelper::SequenceAsHashMap
lTypeProps( xTypeCont
->getByName( sType
) );
240 sFilter
= lTypeProps
.getUnpackedValueOrDefault(
241 u
"PreferredFilter"_ustr
, OUString() );
247 catch( const css::uno::Exception
& )
254 void SvFileObject::Edit(weld::Window
* pParent
, sfx2::SvBaseLink
* pLink
, const Link
<const OUString
&, void>& rEndEditHdl
)
256 aEndEditLink
= rEndEditHdl
;
257 OUString sFile
, sRange
, sTmpFilter
;
258 if( !pLink
|| !pLink
->GetLinkManager() )
261 sfx2::LinkManager::GetDisplayNames( pLink
, nullptr, &sFile
, &sRange
, &sTmpFilter
);
263 switch( pLink
->GetObjType() )
265 case sfx2::SvBaseLinkObjectType::ClientGraphic
:
267 nType
= SvFileObjectType::Graphic
; // If not set already
269 SvxOpenGraphicDialog
aDlg(SfxResId(RID_SVXSTR_EDITGRFLINK
), pParent
);
270 aDlg
.EnableLink(false);
271 aDlg
.SetPath( sFile
, true );
272 aDlg
.SetCurrentFilter( sTmpFilter
);
274 if( !aDlg
.Execute() )
276 sFile
= aDlg
.GetPath()
277 + OUStringChar(sfx2::cTokenSeparator
)
278 + OUStringChar(sfx2::cTokenSeparator
)
279 + aDlg
.GetDetectedFilter();
281 aEndEditLink
.Call( sFile
);
288 case sfx2::SvBaseLinkObjectType::ClientOle
:
290 nType
= SvFileObjectType::Object
; // if not set already
292 ::sfx2::FileDialogHelper
& rFileDlg
=
293 pLink
->GetInsertFileDialog( OUString() );
294 rFileDlg
.SetContext(sfx2::FileDialogHelper::LinkClientOLE
);
295 rFileDlg
.StartExecuteModal(
296 LINK( this, SvFileObject
, DialogClosedHdl
) );
300 case sfx2::SvBaseLinkObjectType::ClientFile
:
302 nType
= SvFileObjectType::Text
; // if not set already
305 SfxObjectShell
* pShell
= pLink
->GetLinkManager()->GetPersist();
307 sFactory
= pShell
->GetFactory().GetFactoryName();
309 ::sfx2::FileDialogHelper
& rFileDlg
=
310 pLink
->GetInsertFileDialog(sFactory
);
311 rFileDlg
.SetContext(sfx2::FileDialogHelper::LinkClientFile
);
312 rFileDlg
.StartExecuteModal(
313 LINK( this, SvFileObject
, DialogClosedHdl
) );
322 IMPL_LINK_NOARG( SvFileObject
, LoadGrfReady_Impl
, void*, void )
324 // When we come from here there it can not be an error no more.
326 bWaitForData
= false;
330 // Graphic is finished, also send DataChanged from Status change
332 SendStateChg_Impl( sfx2::LinkManager::STATE_LOAD_OK
);
334 // and then send the data again
343 xMed
->SetDoneLink( Link
<void*,void>() );
345 nPostUserEventId
= Application::PostUserEvent(
346 LINK( this, SvFileObject
, DelMedium_Impl
));
352 IMPL_LINK_NOARG( SvFileObject
, DelMedium_Impl
, void*, void )
354 nPostUserEventId
= nullptr;
358 IMPL_LINK( SvFileObject
, DialogClosedHdl
, sfx2::FileDialogHelper
*, _pFileDlg
, void )
362 if ( SvFileObjectType::Text
== nType
|| SvFileObjectType::Object
== nType
)
364 if ( _pFileDlg
&& _pFileDlg
->GetError() == ERRCODE_NONE
)
366 OUString
sURL( _pFileDlg
->GetPath() );
367 sFile
= sURL
+ OUStringChar(sfx2::cTokenSeparator
)
368 + OUStringChar(sfx2::cTokenSeparator
)
369 + impl_getFilter( sURL
);
374 SAL_WARN( "sfx.appl", "SvFileObject::DialogClosedHdl(): wrong file type" );
377 aEndEditLink
.Call( sFile
);
381 The method determines whether the data-object can be read from a DDE.
383 bool SvFileObject::IsPending() const
385 return SvFileObjectType::Graphic
== nType
&& !bLoadError
&& bWaitForData
;
388 bool SvFileObject::IsDataComplete() const
391 if( SvFileObjectType::Graphic
!= nType
)
393 else if( !bLoadError
&& !bWaitForData
)
395 SvFileObject
* pThis
= const_cast<SvFileObject
*>(this);
397 ( bSynchron
&& pThis
->LoadFile_Impl() && xMed
.is() ) )
401 INetURLObject
aUrl( sFileNm
);
402 if( aUrl
.HasError() ||
403 INetProtocol::NotValid
== aUrl
.GetProtocol() )
411 void SvFileObject::CancelTransfers()
413 // unsubscribe from the cache if in the middle of loading
416 // Do not set-up again
418 bDataReady
= bLoadError
= bWaitForData
= true;
419 SendStateChg_Impl( sfx2::LinkManager::STATE_LOAD_ABORT
);
424 void SvFileObject::SendStateChg_Impl( sfx2::LinkManager::LinkState nState
)
426 if( !bStateChangeCalled
&& HasDataLinks() )
428 DataChanged( SotExchange::GetFormatName(
429 sfx2::LinkManager::RegisterStatusInfoId()), css::uno::Any(OUString::number( nState
)) );
430 bStateChangeCalled
= true;
435 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */