Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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"
42 : #include "app.hrc"
43 : #include <vcl/dibtools.hxx>
44 :
45 : #define FILETYPE_TEXT 1
46 : #define FILETYPE_GRF 2
47 : #define FILETYPE_OBJECT 3
48 :
49 64 : SvFileObject::SvFileObject()
50 : : nPostUserEventId(0)
51 : , pDelMed(NULL)
52 : , pOldParent(NULL)
53 : , nType(FILETYPE_TEXT)
54 : , bLoadAgain(true)
55 : , bSynchron(false)
56 : , bLoadError(false)
57 : , bWaitForData(false)
58 : , bInNewData(false)
59 : , bDataReady(false)
60 : , bNativFormat(false)
61 : , bClearMedium(false)
62 : , bStateChangeCalled(false)
63 64 : , bInCallDownload(false)
64 : {
65 64 : }
66 :
67 189 : SvFileObject::~SvFileObject()
68 : {
69 63 : if (xMed.Is())
70 : {
71 8 : xMed->SetDoneLink( Link<>() );
72 8 : xMed.Clear();
73 : }
74 63 : if (nPostUserEventId)
75 0 : Application::RemoveUserEvent(nPostUserEventId);
76 63 : delete pDelMed;
77 126 : }
78 :
79 69 : bool SvFileObject::GetData( ::com::sun::star::uno::Any & rData,
80 : const OUString & rMimeType,
81 : bool bGetSynchron )
82 : {
83 69 : SotClipboardFormatId nFmt = SotExchange::RegisterFormatMimeType( rMimeType );
84 69 : switch( nType )
85 : {
86 : case FILETYPE_TEXT:
87 31 : if( SotClipboardFormatId::SIMPLE_FILE == nFmt )
88 : {
89 : // The media in the application must be opened to lookup the
90 : // relative file links!! This is done through the link manager
91 : // of the Storage.
92 31 : rData <<= OUString( sFileNm );
93 : }
94 31 : break;
95 :
96 : case FILETYPE_GRF:
97 38 : if( !bLoadError )
98 : {
99 24 : SfxMediumRef xTmpMed;
100 :
101 24 : if( SotClipboardFormatId::GDIMETAFILE == nFmt || SotClipboardFormatId::BITMAP == nFmt ||
102 : SotClipboardFormatId::SVXB == nFmt )
103 : {
104 24 : Graphic aGrf;
105 :
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 24 : bool bOldNativFormat = bNativFormat;
110 :
111 : // If about to print, waiting for the data to be available
112 24 : if( bGetSynchron )
113 : {
114 : // call a LoadFile every second time to test the loading
115 5 : if( !xMed.Is() )
116 1 : LoadFile_Impl();
117 :
118 5 : if( !bInCallDownload )
119 : {
120 5 : xTmpMed = xMed;
121 43 : while( bWaitForData )
122 33 : Application::Reschedule();
123 :
124 5 : xMed = xTmpMed;
125 5 : bClearMedium = true;
126 : }
127 : }
128 :
129 30 : if( !bWaitForData && ( xMed.Is() || // was loaded as URL
130 0 : ( bSynchron && LoadFile_Impl() && xMed.Is() ) ) )
131 : {
132 : // If it was loaded from the Internet, do not retry
133 6 : if( !bGetSynchron )
134 1 : bLoadAgain = !xMed->IsRemote();
135 6 : bLoadError = !GetGraphic_Impl( aGrf, xMed->GetInStream() );
136 : }
137 18 : else if( !LoadFile_Impl() ||
138 0 : !GetGraphic_Impl( aGrf, xMed.Is() ? xMed->GetInStream() : 0 ))
139 : {
140 18 : if( !xMed.Is() )
141 0 : break;
142 18 : aGrf.SetDefaultType();
143 : }
144 :
145 24 : if( SotClipboardFormatId::SVXB != nFmt )
146 0 : nFmt = (bLoadError || GRAPHIC_BITMAP == aGrf.GetType())
147 : ? SotClipboardFormatId::BITMAP
148 0 : : SotClipboardFormatId::GDIMETAFILE;
149 :
150 48 : SvMemoryStream aMemStm( 0, 65535 );
151 24 : switch ( nFmt )
152 : {
153 : case SotClipboardFormatId::SVXB:
154 24 : if( GRAPHIC_NONE != aGrf.GetType() )
155 : {
156 24 : aMemStm.SetVersion( SOFFICE_FILEFORMAT_50 );
157 24 : WriteGraphic( aMemStm, aGrf );
158 : }
159 24 : break;
160 :
161 : case SotClipboardFormatId::BITMAP:
162 : {
163 0 : const Bitmap aBitmap(aGrf.GetBitmap());
164 :
165 0 : if(!aBitmap.IsEmpty())
166 : {
167 0 : WriteDIB(aBitmap, aMemStm, false, true);
168 : }
169 :
170 0 : break;
171 : }
172 :
173 : default:
174 0 : if( aGrf.GetGDIMetaFile().GetActionSize() )
175 : {
176 0 : GDIMetaFile aMeta( aGrf.GetGDIMetaFile() );
177 0 : aMeta.Write( aMemStm );
178 : }
179 : }
180 48 : rData <<= css::uno::Sequence< sal_Int8 >( static_cast<sal_Int8 const *>(aMemStm.GetData()),
181 48 : aMemStm.Seek( STREAM_SEEK_TO_END ) );
182 :
183 24 : bNativFormat = bOldNativFormat;
184 :
185 : // Everything ready?
186 24 : if( xMed.Is() && !bSynchron && bClearMedium )
187 : {
188 5 : xMed.Clear();
189 5 : bClearMedium = false;
190 24 : }
191 24 : }
192 : }
193 38 : break;
194 : case FILETYPE_OBJECT:
195 : // TODO/LATER: possibility to insert a new object
196 0 : rData <<= OUString( sFileNm );
197 0 : break;
198 : }
199 69 : return true/*0 != aTypeList.Count()*/;
200 : }
201 :
202 64 : bool SvFileObject::Connect( sfx2::SvBaseLink* pLink )
203 : {
204 64 : if( !pLink || !pLink->GetLinkManager() )
205 0 : return false;
206 :
207 : // Test if not another link of the same connection already exists
208 64 : sfx2::LinkManager::GetDisplayNames( pLink, 0, &sFileNm, 0, &sFilter );
209 :
210 64 : if( OBJECT_CLIENT_GRF == pLink->GetObjType() )
211 : {
212 29 : SfxObjectShellRef pShell = pLink->GetLinkManager()->GetPersist();
213 29 : if( pShell.Is() )
214 : {
215 28 : if( pShell->IsAbortingImport() )
216 0 : return false;
217 :
218 28 : if( pShell->GetMedium() )
219 28 : sReferer = pShell->GetMedium()->GetName();
220 29 : }
221 : }
222 :
223 64 : switch( pLink->GetObjType() )
224 : {
225 : case OBJECT_CLIENT_GRF:
226 29 : nType = FILETYPE_GRF;
227 29 : bSynchron = pLink->IsSynchron();
228 29 : break;
229 :
230 : case OBJECT_CLIENT_FILE:
231 35 : nType = FILETYPE_TEXT;
232 35 : break;
233 :
234 : case OBJECT_CLIENT_OLE:
235 0 : nType = FILETYPE_OBJECT;
236 : // TODO/LATER: introduce own type to be used for exchanging
237 0 : break;
238 :
239 : default:
240 0 : return false;
241 : }
242 :
243 64 : SetUpdateTimeout( 0 );
244 :
245 : // and now register by this or other found Pseudo-Object
246 64 : AddDataAdvise( pLink, SotExchange::GetFormatMimeType( pLink->GetContentType()), 0 );
247 64 : return true;
248 : }
249 :
250 19 : bool SvFileObject::LoadFile_Impl()
251 : {
252 : // We are still at Loading!!
253 19 : if( bWaitForData || !bLoadAgain || xMed.Is() )
254 8 : return false;
255 :
256 : // at the moment on the current DocShell
257 11 : xMed = new SfxMedium( sFileNm, sReferer, STREAM_STD_READ );
258 : SvLinkSource::StreamToLoadFrom aStreamToLoadFrom =
259 11 : getStreamToLoadFrom();
260 : xMed->setStreamToLoadFrom(
261 : aStreamToLoadFrom.m_xInputStreamToLoadFrom,
262 11 : aStreamToLoadFrom.m_bIsReadOnly);
263 :
264 11 : if( !bSynchron )
265 : {
266 11 : bLoadAgain = bDataReady = bInNewData = false;
267 11 : bWaitForData = true;
268 :
269 11 : SfxMediumRef xTmpMed = xMed;
270 11 : bInCallDownload = true;
271 11 : xMed->Download( LINK( this, SvFileObject, LoadGrfReady_Impl ) );
272 11 : bInCallDownload = false;
273 :
274 11 : bClearMedium = !xMed.Is();
275 11 : if( bClearMedium )
276 0 : xMed = xTmpMed; // If already finished in Download
277 11 : return bDataReady;
278 : }
279 :
280 0 : bWaitForData = true;
281 0 : bDataReady = bInNewData = false;
282 0 : xMed->Download();
283 0 : bLoadAgain = !xMed->IsRemote();
284 0 : bWaitForData = false;
285 :
286 : // Graphic is finished, also send DataChanged of the Status change:
287 0 : SendStateChg_Impl( xMed->GetInStream() && xMed->GetInStream()->GetError()
288 0 : ? sfx2::LinkManager::STATE_LOAD_ERROR : sfx2::LinkManager::STATE_LOAD_OK );
289 0 : return true;
290 : }
291 :
292 :
293 6 : bool SvFileObject::GetGraphic_Impl( Graphic& rGrf, SvStream* pStream )
294 : {
295 6 : GraphicFilter& rGF = GraphicFilter::GetGraphicFilter();
296 :
297 6 : const sal_uInt16 nFilter = !sFilter.isEmpty() && rGF.GetImportFormatCount()
298 0 : ? rGF.GetImportFormatNumber( sFilter )
299 6 : : GRFILTER_FORMAT_DONTKNOW;
300 :
301 : int nRes;
302 :
303 : // To avoid that a native link is created
304 18 : if( !rGrf.IsLink() &&
305 12 : !rGrf.GetContext() && !bNativFormat )
306 6 : rGrf.SetLink( GfxLink() );
307 :
308 6 : if( !pStream )
309 0 : nRes = xMed.Is() ? GRFILTER_OPENERROR
310 : : rGF.ImportGraphic( rGrf, INetURLObject(sFileNm),
311 0 : nFilter );
312 : else
313 : {
314 6 : pStream->Seek( STREAM_SEEK_TO_BEGIN );
315 :
316 : // #i123042# for e.g. SVG the path is needed, see same TaskID in svx for more info
317 6 : nRes = rGF.ImportGraphic( rGrf, sFileNm, *pStream, nFilter );
318 : }
319 :
320 6 : if( pStream && ERRCODE_IO_PENDING == pStream->GetError() )
321 0 : pStream->ResetError();
322 :
323 6 : if( nRes )
324 : {
325 0 : if( xMed.Is() && !pStream )
326 : SAL_WARN( "sfx.appl", "Graphic error [" << nRes << "] - [" << xMed->GetPhysicalName() << "] URL[" << sFileNm << "]" );
327 : else
328 : SAL_WARN( "sfx.appl", "Graphic error [" << nRes << "] - [" << sFileNm << "]" );
329 : }
330 :
331 6 : return GRFILTER_OK == nRes;
332 : }
333 :
334 : /** detect the filter of the given file
335 :
336 : @param _rURL
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.
340 : */
341 0 : OUString impl_getFilter( const OUString& _rURL )
342 : {
343 0 : OUString sFilter;
344 0 : if ( _rURL.isEmpty() )
345 0 : return sFilter;
346 :
347 : try
348 : {
349 : css::uno::Reference< ::com::sun::star::document::XTypeDetection > xTypeDetection(
350 0 : ::comphelper::getProcessServiceFactory()->createInstance(
351 0 : OUString("com.sun.star.document.TypeDetection") ),
352 0 : css::uno::UNO_QUERY );
353 0 : if ( xTypeDetection.is() )
354 : {
355 0 : utl::MediaDescriptor aDescr;
356 0 : aDescr[ utl::MediaDescriptor::PROP_URL() ] <<= OUString( _rURL );
357 : css::uno::Sequence< css::beans::PropertyValue > aDescrList =
358 0 : aDescr.getAsConstPropertyValueList();
359 0 : OUString sType = xTypeDetection->queryTypeByDescriptor( aDescrList, sal_True );
360 0 : if ( !sType.isEmpty() )
361 : {
362 : // Honor a selected/detected filter.
363 0 : for (sal_Int32 i=0; i < aDescrList.getLength(); ++i)
364 : {
365 0 : if (aDescrList[i].Name == "FilterName")
366 : {
367 0 : if (aDescrList[i].Value >>= sFilter)
368 0 : break;
369 : }
370 : }
371 0 : if (sFilter.isEmpty())
372 : {
373 : css::uno::Reference< css::container::XNameAccess > xTypeCont( xTypeDetection,
374 0 : css::uno::UNO_QUERY );
375 0 : if ( xTypeCont.is() )
376 : {
377 : /* XXX: for fdo#69948 scenario the sequence returned by
378 : * getByName() contains an empty PreferredFilter
379 : * property value (since? expected?) */
380 0 : ::comphelper::SequenceAsHashMap lTypeProps( xTypeCont->getByName( sType ) );
381 0 : sFilter = lTypeProps.getUnpackedValueOrDefault(
382 0 : OUString("PreferredFilter"), OUString() );
383 0 : }
384 : }
385 0 : }
386 0 : }
387 : }
388 0 : catch( const css::uno::Exception& )
389 : {
390 : }
391 :
392 0 : return sFilter;
393 : }
394 :
395 0 : void SvFileObject::Edit( vcl::Window* pParent, sfx2::SvBaseLink* pLink, const Link<>& rEndEditHdl )
396 : {
397 0 : aEndEditLink = rEndEditHdl;
398 0 : OUString sFile, sRange, sTmpFilter;
399 0 : if( pLink && pLink->GetLinkManager() )
400 : {
401 0 : sfx2::LinkManager::GetDisplayNames( pLink, 0, &sFile, &sRange, &sTmpFilter );
402 :
403 0 : switch( pLink->GetObjType() )
404 : {
405 : case OBJECT_CLIENT_GRF:
406 : {
407 0 : nType = FILETYPE_GRF; // If not set already
408 :
409 0 : SvxOpenGraphicDialog aDlg(SfxResId(RID_SVXSTR_EDITGRFLINK).toString());
410 0 : aDlg.EnableLink(false);
411 0 : aDlg.SetPath( sFile, true );
412 0 : aDlg.SetCurrentFilter( sTmpFilter );
413 :
414 0 : if( !aDlg.Execute() )
415 : {
416 0 : sFile = aDlg.GetPath();
417 0 : sFile += OUString(::sfx2::cTokenSeparator);
418 0 : sFile += OUString(::sfx2::cTokenSeparator);
419 0 : sFile += aDlg.GetCurrentFilter();
420 :
421 0 : if ( aEndEditLink.IsSet() )
422 0 : aEndEditLink.Call( &sFile );
423 : }
424 : else
425 0 : sFile.clear();
426 : }
427 0 : break;
428 :
429 : case OBJECT_CLIENT_OLE:
430 : {
431 0 : nType = FILETYPE_OBJECT; // if not set already
432 0 : pOldParent = Application::GetDefDialogParent();
433 0 : Application::SetDefDialogParent( pParent );
434 :
435 : ::sfx2::FileDialogHelper & rFileDlg =
436 0 : pLink->GetInsertFileDialog( OUString() );
437 : rFileDlg.StartExecuteModal(
438 0 : LINK( this, SvFileObject, DialogClosedHdl ) );
439 : }
440 0 : break;
441 :
442 : case OBJECT_CLIENT_FILE:
443 : {
444 0 : nType = FILETYPE_TEXT; // if not set already
445 0 : pOldParent = Application::GetDefDialogParent();
446 0 : Application::SetDefDialogParent( pParent );
447 :
448 0 : OUString sFactory;
449 0 : SfxObjectShell* pShell = pLink->GetLinkManager()->GetPersist();
450 0 : if ( pShell )
451 0 : sFactory = pShell->GetFactory().GetFactoryName();
452 :
453 : ::sfx2::FileDialogHelper & rFileDlg =
454 0 : pLink->GetInsertFileDialog(sFactory);
455 : rFileDlg.StartExecuteModal(
456 0 : LINK( this, SvFileObject, DialogClosedHdl ) );
457 : }
458 0 : break;
459 :
460 : default:
461 0 : sFile.clear();
462 : }
463 0 : }
464 0 : }
465 :
466 6 : IMPL_LINK_NOARG( SvFileObject, LoadGrfReady_Impl )
467 : {
468 : // When we come form here there it can not be an error no more.
469 3 : bLoadError = false;
470 3 : bWaitForData = false;
471 3 : bInCallDownload = false;
472 :
473 3 : if( !bInNewData && !bDataReady )
474 : {
475 : // Graphic is finished, also send DataChanged from Status change
476 3 : bDataReady = true;
477 3 : SendStateChg_Impl( sfx2::LinkManager::STATE_LOAD_OK );
478 :
479 : // and then send the data again
480 3 : NotifyDataChanged();
481 : }
482 :
483 3 : if( bDataReady )
484 : {
485 3 : bLoadAgain = true;
486 3 : if( xMed.Is() )
487 : {
488 0 : xMed->SetDoneLink( Link<>() );
489 0 : pDelMed = new SfxMediumRef(xMed);
490 : nPostUserEventId = Application::PostUserEvent(
491 : LINK( this, SvFileObject, DelMedium_Impl ),
492 0 : pDelMed);
493 0 : xMed.Clear();
494 : }
495 : }
496 :
497 3 : return 0;
498 : }
499 :
500 0 : IMPL_LINK( SvFileObject, DelMedium_Impl, SfxMediumRef*, deleteMedium )
501 : {
502 0 : nPostUserEventId = 0;
503 : assert(pDelMed == deleteMedium);
504 0 : pDelMed = NULL;
505 0 : delete deleteMedium;
506 0 : return 0;
507 : }
508 :
509 0 : IMPL_LINK( SvFileObject, DialogClosedHdl, sfx2::FileDialogHelper*, _pFileDlg )
510 : {
511 0 : OUString sFile;
512 0 : Application::SetDefDialogParent( pOldParent );
513 :
514 0 : if ( FILETYPE_TEXT == nType || FILETYPE_OBJECT == nType )
515 : {
516 0 : if ( _pFileDlg && _pFileDlg->GetError() == ERRCODE_NONE )
517 : {
518 0 : OUString sURL( _pFileDlg->GetPath() );
519 0 : sFile = sURL;
520 0 : sFile += OUString(::sfx2::cTokenSeparator);
521 0 : sFile += OUString(::sfx2::cTokenSeparator);
522 0 : sFile += impl_getFilter( sURL );
523 0 : }
524 : }
525 : else
526 : {
527 : SAL_WARN( "sfx.appl", "SvFileObject::DialogClosedHdl(): wrong file type" );
528 : }
529 :
530 0 : if ( aEndEditLink.IsSet() )
531 0 : aEndEditLink.Call( &sFile );
532 0 : return 0;
533 : }
534 :
535 : /* [Description]
536 :
537 : The method determines whether the data-object can be read from a DDE.
538 :
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
543 : */
544 34 : bool SvFileObject::IsPending() const
545 : {
546 34 : return FILETYPE_GRF == nType && !bLoadError && bWaitForData;
547 : }
548 :
549 39 : bool SvFileObject::IsDataComplete() const
550 : {
551 39 : bool bRet = false;
552 39 : if( FILETYPE_GRF != nType )
553 0 : bRet = true;
554 39 : else if( !bLoadError && !bWaitForData )
555 : {
556 9 : SvFileObject* pThis = const_cast<SvFileObject*>(this);
557 18 : if( bDataReady ||
558 0 : ( bSynchron && pThis->LoadFile_Impl() && xMed.Is() ) )
559 9 : bRet = true;
560 : else
561 : {
562 0 : INetURLObject aUrl( sFileNm );
563 0 : if( aUrl.HasError() ||
564 0 : INetProtocol::NotValid == aUrl.GetProtocol() )
565 0 : bRet = true;
566 : }
567 : }
568 39 : return bRet;
569 : }
570 :
571 :
572 :
573 14 : void SvFileObject::CancelTransfers()
574 : {
575 : // unsubscribe from the cache if in the middle of loading
576 14 : if( !bDataReady )
577 : {
578 : // Do not set-up again
579 11 : bLoadAgain = false;
580 11 : bDataReady = bLoadError = bWaitForData = true;
581 11 : SendStateChg_Impl( sfx2::LinkManager::STATE_LOAD_ABORT );
582 : }
583 14 : }
584 :
585 :
586 14 : void SvFileObject::SendStateChg_Impl( sfx2::LinkManager::LinkState nState )
587 : {
588 14 : if( !bStateChangeCalled && HasDataLinks() )
589 : {
590 14 : css::uno::Any aAny;
591 14 : aAny <<= OUString::number( nState );
592 : DataChanged( SotExchange::GetFormatName(
593 14 : sfx2::LinkManager::RegisterStatusInfoId()), aAny );
594 14 : bStateChangeCalled = true;
595 : }
596 662 : }
597 :
598 :
599 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|