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)
51 , nType(SvFileObjectType::Text
)
58 , bStateChangeCalled(false)
62 SvFileObject::~SvFileObject()
66 xMed
->SetDoneLink( Link
<void*,void>() );
70 Application::RemoveUserEvent(nPostUserEventId
);
73 bool SvFileObject::GetData( css::uno::Any
& rData
,
74 const OUString
& rMimeType
,
75 bool /*bGetSynchron*/ )
77 SotClipboardFormatId nFmt
= SotExchange::RegisterFormatMimeType( rMimeType
);
80 case SvFileObjectType::Text
:
81 if( SotClipboardFormatId::SIMPLE_FILE
== nFmt
)
83 // The media in the application must be opened to lookup the
84 // relative file links!! This is done through the link manager
90 case SvFileObjectType::Graphic
:
91 if (SotClipboardFormatId::GDIMETAFILE
== nFmt
92 || SotClipboardFormatId::BITMAP
== nFmt
93 || SotClipboardFormatId::SVXB
== nFmt
)
98 case SvFileObjectType::Object
:
99 // TODO/LATER: possibility to insert a new object
103 return true/*0 != aTypeList.Count()*/;
106 bool SvFileObject::Connect( sfx2::SvBaseLink
* pLink
)
108 if( !pLink
|| !pLink
->GetLinkManager() )
111 // Test if not another link of the same connection already exists
112 sfx2::LinkManager::GetDisplayNames( pLink
, nullptr, &sFileNm
, nullptr, &sFilter
);
114 if( sfx2::SvBaseLinkObjectType::ClientGraphic
== pLink
->GetObjType() )
116 SfxObjectShellRef pShell
= pLink
->GetLinkManager()->GetPersist();
119 if( pShell
->IsAbortingImport() )
122 if( pShell
->GetMedium() )
123 sReferer
= pShell
->GetMedium()->GetName();
127 switch( pLink
->GetObjType() )
129 case sfx2::SvBaseLinkObjectType::ClientGraphic
:
130 nType
= SvFileObjectType::Graphic
;
131 bSynchron
= pLink
->IsSynchron();
134 case sfx2::SvBaseLinkObjectType::ClientFile
:
135 nType
= SvFileObjectType::Text
;
138 case sfx2::SvBaseLinkObjectType::ClientOle
:
139 nType
= SvFileObjectType::Object
;
140 // TODO/LATER: introduce own type to be used for exchanging
147 SetUpdateTimeout( 0 );
149 // and now register by this or other found Pseudo-Object
150 AddDataAdvise( pLink
, SotExchange::GetFormatMimeType( pLink
->GetContentType()), 0 );
154 bool SvFileObject::LoadFile_Impl()
156 // We are still at Loading!!
157 if( bWaitForData
|| !bLoadAgain
|| xMed
.is() )
160 // at the moment on the current DocShell
161 xMed
= new SfxMedium( sFileNm
, sReferer
, StreamMode::STD_READ
);
162 SvLinkSource::StreamToLoadFrom aStreamToLoadFrom
=
163 getStreamToLoadFrom();
164 xMed
->setStreamToLoadFrom(
165 aStreamToLoadFrom
.m_xInputStreamToLoadFrom
,
166 aStreamToLoadFrom
.m_bIsReadOnly
);
170 bLoadAgain
= bDataReady
= false;
173 tools::SvRef
<SfxMedium
> xTmpMed
= xMed
;
174 xMed
->Download( LINK( this, SvFileObject
, LoadGrfReady_Impl
) );
176 bClearMedium
= !xMed
.is();
178 xMed
= xTmpMed
; // If already finished in Download
185 bLoadAgain
= !xMed
->IsRemote();
186 bWaitForData
= false;
188 // Graphic is finished, also send DataChanged of the Status change:
189 SendStateChg_Impl( xMed
->GetInStream() && xMed
->GetInStream()->GetError()
190 ? sfx2::LinkManager::STATE_LOAD_ERROR
: sfx2::LinkManager::STATE_LOAD_OK
);
195 /** detect the filter of the given file
198 specifies the URL of the file which filter is to detected.<br/>
199 If the URL doesn't denote a valid (existent and accessible) file, the
200 request is silently dropped.
202 static OUString
impl_getFilter( const OUString
& _rURL
)
205 if ( _rURL
.isEmpty() )
210 css::uno::Reference
< css::document::XTypeDetection
> xTypeDetection(
211 ::comphelper::getProcessServiceFactory()->createInstance( "com.sun.star.document.TypeDetection" ),
212 css::uno::UNO_QUERY
);
213 if ( xTypeDetection
.is() )
215 utl::MediaDescriptor aDescr
;
216 aDescr
[ utl::MediaDescriptor::PROP_URL() ] <<= _rURL
;
217 css::uno::Sequence
< css::beans::PropertyValue
> aDescrList
=
218 aDescr
.getAsConstPropertyValueList();
219 OUString sType
= xTypeDetection
->queryTypeByDescriptor( aDescrList
, true );
220 if ( !sType
.isEmpty() )
222 // Honor a selected/detected filter.
223 for (const auto& rDescr
: std::as_const(aDescrList
))
225 if (rDescr
.Name
== "FilterName")
227 if (rDescr
.Value
>>= sFilter
)
231 if (sFilter
.isEmpty())
233 css::uno::Reference
< css::container::XNameAccess
> xTypeCont( xTypeDetection
,
234 css::uno::UNO_QUERY
);
235 if ( xTypeCont
.is() )
237 /* XXX: for fdo#69948 scenario the sequence returned by
238 * getByName() contains an empty PreferredFilter
239 * property value (since? expected?) */
240 ::comphelper::SequenceAsHashMap
lTypeProps( xTypeCont
->getByName( sType
) );
241 sFilter
= lTypeProps
.getUnpackedValueOrDefault(
242 "PreferredFilter", OUString() );
248 catch( const css::uno::Exception
& )
255 void SvFileObject::Edit(weld::Window
* pParent
, sfx2::SvBaseLink
* pLink
, const Link
<const OUString
&, void>& rEndEditHdl
)
257 aEndEditLink
= rEndEditHdl
;
258 OUString sFile
, sRange
, sTmpFilter
;
259 if( !pLink
|| !pLink
->GetLinkManager() )
262 sfx2::LinkManager::GetDisplayNames( pLink
, nullptr, &sFile
, &sRange
, &sTmpFilter
);
264 switch( pLink
->GetObjType() )
266 case sfx2::SvBaseLinkObjectType::ClientGraphic
:
268 nType
= SvFileObjectType::Graphic
; // If not set already
270 SvxOpenGraphicDialog
aDlg(SfxResId(RID_SVXSTR_EDITGRFLINK
), pParent
);
271 aDlg
.EnableLink(false);
272 aDlg
.SetPath( sFile
, true );
273 aDlg
.SetCurrentFilter( sTmpFilter
);
275 if( !aDlg
.Execute() )
277 sFile
= aDlg
.GetPath()
278 + OUStringChar(sfx2::cTokenSeparator
)
279 + OUStringChar(sfx2::cTokenSeparator
)
280 + aDlg
.GetDetectedFilter();
282 aEndEditLink
.Call( sFile
);
289 case sfx2::SvBaseLinkObjectType::ClientOle
:
291 nType
= SvFileObjectType::Object
; // if not set already
293 ::sfx2::FileDialogHelper
& rFileDlg
=
294 pLink
->GetInsertFileDialog( OUString() );
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
.StartExecuteModal(
312 LINK( this, SvFileObject
, DialogClosedHdl
) );
321 IMPL_LINK_NOARG( SvFileObject
, LoadGrfReady_Impl
, void*, void )
323 // When we come from here there it can not be an error no more.
325 bWaitForData
= false;
329 // Graphic is finished, also send DataChanged from Status change
331 SendStateChg_Impl( sfx2::LinkManager::STATE_LOAD_OK
);
333 // and then send the data again
342 xMed
->SetDoneLink( Link
<void*,void>() );
344 nPostUserEventId
= Application::PostUserEvent(
345 LINK( this, SvFileObject
, DelMedium_Impl
));
351 IMPL_LINK_NOARG( SvFileObject
, DelMedium_Impl
, void*, void )
353 nPostUserEventId
= nullptr;
357 IMPL_LINK( SvFileObject
, DialogClosedHdl
, sfx2::FileDialogHelper
*, _pFileDlg
, void )
361 if ( SvFileObjectType::Text
== nType
|| SvFileObjectType::Object
== nType
)
363 if ( _pFileDlg
&& _pFileDlg
->GetError() == ERRCODE_NONE
)
365 OUString
sURL( _pFileDlg
->GetPath() );
366 sFile
= sURL
+ OUStringChar(sfx2::cTokenSeparator
)
367 + OUStringChar(sfx2::cTokenSeparator
)
368 + impl_getFilter( sURL
);
373 SAL_WARN( "sfx.appl", "SvFileObject::DialogClosedHdl(): wrong file type" );
376 aEndEditLink
.Call( sFile
);
380 The method determines whether the data-object can be read from a DDE.
382 bool SvFileObject::IsPending() const
384 return SvFileObjectType::Graphic
== nType
&& !bLoadError
&& bWaitForData
;
387 bool SvFileObject::IsDataComplete() const
390 if( SvFileObjectType::Graphic
!= nType
)
392 else if( !bLoadError
&& !bWaitForData
)
394 SvFileObject
* pThis
= const_cast<SvFileObject
*>(this);
396 ( bSynchron
&& pThis
->LoadFile_Impl() && xMed
.is() ) )
400 INetURLObject
aUrl( sFileNm
);
401 if( aUrl
.HasError() ||
402 INetProtocol::NotValid
== aUrl
.GetProtocol() )
410 void SvFileObject::CancelTransfers()
412 // unsubscribe from the cache if in the middle of loading
415 // Do not set-up again
417 bDataReady
= bLoadError
= bWaitForData
= true;
418 SendStateChg_Impl( sfx2::LinkManager::STATE_LOAD_ABORT
);
423 void SvFileObject::SendStateChg_Impl( sfx2::LinkManager::LinkState nState
)
425 if( !bStateChangeCalled
&& HasDataLinks() )
427 DataChanged( SotExchange::GetFormatName(
428 sfx2::LinkManager::RegisterStatusInfoId()), css::uno::makeAny(OUString::number( nState
)) );
429 bStateChangeCalled
= true;
434 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */