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