tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / sfx2 / source / appl / fileobj.cxx
blob2d4aa100d9d359881b18bdc33772ca90b66bb38e
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 , nType(SvFileObjectType::Text)
51 , bLoadAgain(true)
52 , bSynchron(false)
53 , bLoadError(false)
54 , bWaitForData(false)
55 , bDataReady(false)
56 , bClearMedium(false)
57 , bStateChangeCalled(false)
61 SvFileObject::~SvFileObject()
63 if (xMed.is())
65 xMed->SetDoneLink( Link<void*,void>() );
66 xMed.clear();
68 if (nPostUserEventId)
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 );
77 switch( nType )
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
84 // of the Storage.
85 rData <<= sFileNm;
87 break;
89 case SvFileObjectType::Graphic:
90 if (SotClipboardFormatId::GDIMETAFILE == nFmt
91 || SotClipboardFormatId::BITMAP == nFmt
92 || SotClipboardFormatId::SVXB == nFmt)
94 rData <<= sFileNm;
96 break;
97 case SvFileObjectType::Object:
98 // TODO/LATER: possibility to insert a new object
99 rData <<= sFileNm;
100 break;
102 return true/*0 != aTypeList.Count()*/;
105 bool SvFileObject::Connect( sfx2::SvBaseLink* pLink )
107 if( !pLink || !pLink->GetLinkManager() )
108 return false;
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();
116 if( pShell.is() )
118 if( pShell->IsAbortingImport() )
119 return false;
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();
131 break;
133 case sfx2::SvBaseLinkObjectType::ClientFile:
134 nType = SvFileObjectType::Text;
135 break;
137 case sfx2::SvBaseLinkObjectType::ClientOle:
138 nType = SvFileObjectType::Object;
139 // TODO/LATER: introduce own type to be used for exchanging
140 break;
142 default:
143 return false;
146 SetUpdateTimeout( 0 );
148 // and now register by this or other found Pseudo-Object
149 AddDataAdvise( pLink, SotExchange::GetFormatMimeType( pLink->GetContentType()), 0 );
150 return true;
153 bool SvFileObject::LoadFile_Impl()
155 // We are still at Loading!!
156 if( bWaitForData || !bLoadAgain || xMed.is() )
157 return false;
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);
167 if( !bSynchron )
169 bLoadAgain = bDataReady = false;
170 bWaitForData = true;
172 tools::SvRef<SfxMedium> xTmpMed = xMed;
173 xMed->Download( LINK( this, SvFileObject, LoadGrfReady_Impl ) );
175 bClearMedium = !xMed.is();
176 if( bClearMedium )
177 xMed = std::move(xTmpMed); // If already finished in Download
178 return bDataReady;
181 bWaitForData = true;
182 bDataReady = false;
183 xMed->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 );
190 return true;
194 /** detect the filter of the given file
196 @param _rURL
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 )
203 OUString sFilter;
204 if ( _rURL.isEmpty() )
205 return sFilter;
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)
227 break;
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& )
251 return sFilter;
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() )
259 return;
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 );
283 else
284 sFile.clear();
286 break;
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 ) );
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.SetContext(sfx2::FileDialogHelper::LinkClientFile);
312 rFileDlg.StartExecuteModal(
313 LINK( this, SvFileObject, DialogClosedHdl ) );
315 break;
317 default:
318 sFile.clear();
322 IMPL_LINK_NOARG( SvFileObject, LoadGrfReady_Impl, void*, void )
324 // When we come from here there it can not be an error no more.
325 bLoadError = false;
326 bWaitForData = false;
328 if( !bDataReady )
330 // Graphic is finished, also send DataChanged from Status change
331 bDataReady = true;
332 SendStateChg_Impl( sfx2::LinkManager::STATE_LOAD_OK );
334 // and then send the data again
335 NotifyDataChanged();
338 if( bDataReady )
340 bLoadAgain = true;
341 if( xMed.is() )
343 xMed->SetDoneLink( Link<void*,void>() );
344 mxDelMed = xMed;
345 nPostUserEventId = Application::PostUserEvent(
346 LINK( this, SvFileObject, DelMedium_Impl ));
347 xMed.clear();
352 IMPL_LINK_NOARG( SvFileObject, DelMedium_Impl, void*, void )
354 nPostUserEventId = nullptr;
355 mxDelMed.clear();
358 IMPL_LINK( SvFileObject, DialogClosedHdl, sfx2::FileDialogHelper*, _pFileDlg, void )
360 OUString sFile;
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 );
372 else
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
390 bool bRet = false;
391 if( SvFileObjectType::Graphic != nType )
392 bRet = true;
393 else if( !bLoadError && !bWaitForData )
395 SvFileObject* pThis = const_cast<SvFileObject*>(this);
396 if( bDataReady ||
397 ( bSynchron && pThis->LoadFile_Impl() && xMed.is() ) )
398 bRet = true;
399 else
401 INetURLObject aUrl( sFileNm );
402 if( aUrl.HasError() ||
403 INetProtocol::NotValid == aUrl.GetProtocol() )
404 bRet = true;
407 return bRet;
411 void SvFileObject::CancelTransfers()
413 // unsubscribe from the cache if in the middle of loading
414 if( !bDataReady )
416 // Do not set-up again
417 bLoadAgain = false;
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: */