Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / sfx2 / source / appl / fileobj.cxx
blobebfe57edf2a3cf996eb030ab76130827082c9aab
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 <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 , mxDelMed()
51 , nType(SvFileObjectType::Text)
52 , bLoadAgain(true)
53 , bSynchron(false)
54 , bLoadError(false)
55 , bWaitForData(false)
56 , bDataReady(false)
57 , bClearMedium(false)
58 , bStateChangeCalled(false)
62 SvFileObject::~SvFileObject()
64 if (xMed.is())
66 xMed->SetDoneLink( Link<void*,void>() );
67 xMed.clear();
69 if (nPostUserEventId)
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 );
78 switch( nType )
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
85 // of the Storage.
86 rData <<= sFileNm;
88 break;
90 case SvFileObjectType::Graphic:
91 if (SotClipboardFormatId::GDIMETAFILE == nFmt
92 || SotClipboardFormatId::BITMAP == nFmt
93 || SotClipboardFormatId::SVXB == nFmt)
95 rData <<= sFileNm;
97 break;
98 case SvFileObjectType::Object:
99 // TODO/LATER: possibility to insert a new object
100 rData <<= sFileNm;
101 break;
103 return true/*0 != aTypeList.Count()*/;
106 bool SvFileObject::Connect( sfx2::SvBaseLink* pLink )
108 if( !pLink || !pLink->GetLinkManager() )
109 return false;
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();
117 if( pShell.is() )
119 if( pShell->IsAbortingImport() )
120 return false;
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();
132 break;
134 case sfx2::SvBaseLinkObjectType::ClientFile:
135 nType = SvFileObjectType::Text;
136 break;
138 case sfx2::SvBaseLinkObjectType::ClientOle:
139 nType = SvFileObjectType::Object;
140 // TODO/LATER: introduce own type to be used for exchanging
141 break;
143 default:
144 return false;
147 SetUpdateTimeout( 0 );
149 // and now register by this or other found Pseudo-Object
150 AddDataAdvise( pLink, SotExchange::GetFormatMimeType( pLink->GetContentType()), 0 );
151 return true;
154 bool SvFileObject::LoadFile_Impl()
156 // We are still at Loading!!
157 if( bWaitForData || !bLoadAgain || xMed.is() )
158 return false;
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);
168 if( !bSynchron )
170 bLoadAgain = bDataReady = false;
171 bWaitForData = true;
173 tools::SvRef<SfxMedium> xTmpMed = xMed;
174 xMed->Download( LINK( this, SvFileObject, LoadGrfReady_Impl ) );
176 bClearMedium = !xMed.is();
177 if( bClearMedium )
178 xMed = xTmpMed; // If already finished in Download
179 return bDataReady;
182 bWaitForData = true;
183 bDataReady = false;
184 xMed->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 );
191 return true;
195 /** detect the filter of the given file
197 @param _rURL
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 )
204 OUString sFilter;
205 if ( _rURL.isEmpty() )
206 return sFilter;
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)
228 break;
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& )
252 return sFilter;
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() )
260 return;
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 );
284 else
285 sFile.clear();
287 break;
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 ) );
298 break;
300 case sfx2::SvBaseLinkObjectType::ClientFile:
302 nType = SvFileObjectType::Text; // if not set already
304 OUString sFactory;
305 SfxObjectShell* pShell = pLink->GetLinkManager()->GetPersist();
306 if ( pShell )
307 sFactory = pShell->GetFactory().GetFactoryName();
309 ::sfx2::FileDialogHelper & rFileDlg =
310 pLink->GetInsertFileDialog(sFactory);
311 rFileDlg.StartExecuteModal(
312 LINK( this, SvFileObject, DialogClosedHdl ) );
314 break;
316 default:
317 sFile.clear();
321 IMPL_LINK_NOARG( SvFileObject, LoadGrfReady_Impl, void*, void )
323 // When we come from here there it can not be an error no more.
324 bLoadError = false;
325 bWaitForData = false;
327 if( !bDataReady )
329 // Graphic is finished, also send DataChanged from Status change
330 bDataReady = true;
331 SendStateChg_Impl( sfx2::LinkManager::STATE_LOAD_OK );
333 // and then send the data again
334 NotifyDataChanged();
337 if( bDataReady )
339 bLoadAgain = true;
340 if( xMed.is() )
342 xMed->SetDoneLink( Link<void*,void>() );
343 mxDelMed = xMed;
344 nPostUserEventId = Application::PostUserEvent(
345 LINK( this, SvFileObject, DelMedium_Impl ));
346 xMed.clear();
351 IMPL_LINK_NOARG( SvFileObject, DelMedium_Impl, void*, void )
353 nPostUserEventId = nullptr;
354 mxDelMed.clear();
357 IMPL_LINK( SvFileObject, DialogClosedHdl, sfx2::FileDialogHelper*, _pFileDlg, void )
359 OUString sFile;
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 );
371 else
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
389 bool bRet = false;
390 if( SvFileObjectType::Graphic != nType )
391 bRet = true;
392 else if( !bLoadError && !bWaitForData )
394 SvFileObject* pThis = const_cast<SvFileObject*>(this);
395 if( bDataReady ||
396 ( bSynchron && pThis->LoadFile_Impl() && xMed.is() ) )
397 bRet = true;
398 else
400 INetURLObject aUrl( sFileNm );
401 if( aUrl.HasError() ||
402 INetProtocol::NotValid == aUrl.GetProtocol() )
403 bRet = true;
406 return bRet;
410 void SvFileObject::CancelTransfers()
412 // unsubscribe from the cache if in the middle of loading
413 if( !bDataReady )
415 // Do not set-up again
416 bLoadAgain = false;
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: */