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 88 : 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 88 : , bInCallDownload(false)
62 : {
63 88 : }
64 :
65 264 : SvFileObject::~SvFileObject()
66 : {
67 88 : if ( xMed.Is() )
68 : {
69 0 : xMed->SetDoneLink( Link() );
70 0 : xMed.Clear();
71 : }
72 176 : }
73 :
74 :
75 104 : bool SvFileObject::GetData( ::com::sun::star::uno::Any & rData,
76 : const OUString & rMimeType,
77 : bool bGetSynchron )
78 : {
79 104 : sal_uIntPtr nFmt = SotExchange::GetFormatStringId( rMimeType );
80 104 : switch( nType )
81 : {
82 : case FILETYPE_TEXT:
83 70 : 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 70 : rData <<= OUString( sFileNm );
89 : }
90 70 : break;
91 :
92 : case FILETYPE_GRF:
93 34 : if( !bLoadError )
94 : {
95 28 : SfxMediumRef xTmpMed;
96 :
97 28 : if( FORMAT_GDIMETAFILE == nFmt || FORMAT_BITMAP == nFmt ||
98 : SOT_FORMATSTR_ID_SVXB == nFmt )
99 : {
100 28 : 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 28 : bool bOldNativFormat = bNativFormat;
106 :
107 : // If about to print, waiting for the data to be available
108 28 : if( bGetSynchron )
109 : {
110 : // call a LoadFile every second time to test the loading
111 16 : if( !xMed.Is() )
112 0 : LoadFile_Impl();
113 :
114 16 : if( !bInCallDownload )
115 : {
116 16 : xTmpMed = xMed;
117 36 : while( bWaitForData )
118 4 : Application::Reschedule();
119 :
120 16 : xMed = xTmpMed;
121 16 : bClearMedium = true;
122 : }
123 : }
124 :
125 44 : 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 16 : if( !bGetSynchron )
130 0 : bLoadAgain = !xMed->IsRemote();
131 16 : bLoadError = !GetGraphic_Impl( aGrf, xMed->GetInStream() );
132 : }
133 12 : else if( !LoadFile_Impl() ||
134 0 : !GetGraphic_Impl( aGrf, xMed.Is() ? xMed->GetInStream() : 0 ))
135 : {
136 12 : if( !xMed.Is() )
137 0 : break;
138 12 : aGrf.SetDefaultType();
139 : }
140 :
141 28 : if( SOT_FORMATSTR_ID_SVXB != nFmt )
142 0 : nFmt = (bLoadError || GRAPHIC_BITMAP == aGrf.GetType())
143 : ? FORMAT_BITMAP
144 0 : : FORMAT_GDIMETAFILE;
145 :
146 56 : SvMemoryStream aMemStm( 0, 65535 );
147 28 : switch ( nFmt )
148 : {
149 : case SOT_FORMATSTR_ID_SVXB:
150 28 : if( GRAPHIC_NONE != aGrf.GetType() )
151 : {
152 12 : aMemStm.SetVersion( SOFFICE_FILEFORMAT_50 );
153 12 : WriteGraphic( aMemStm, aGrf );
154 : }
155 28 : 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 56 : rData <<= css::uno::Sequence< sal_Int8 >( (sal_Int8*) aMemStm.GetData(),
177 56 : aMemStm.Seek( STREAM_SEEK_TO_END ) );
178 :
179 28 : bNativFormat = bOldNativFormat;
180 :
181 : // Everything ready?
182 28 : if( xMed.Is() && !bSynchron && bClearMedium )
183 : {
184 16 : xMed.Clear();
185 16 : bClearMedium = false;
186 28 : }
187 28 : }
188 : }
189 34 : break;
190 : case FILETYPE_OBJECT:
191 : // TODO/LATER: possibility to insert a new object
192 0 : rData <<= OUString( sFileNm );
193 0 : break;
194 : }
195 104 : return true/*0 != aTypeList.Count()*/;
196 : }
197 :
198 88 : bool SvFileObject::Connect( sfx2::SvBaseLink* pLink )
199 : {
200 88 : if( !pLink || !pLink->GetLinkManager() )
201 0 : return false;
202 :
203 : // Test if not another link of the same connection already exists
204 88 : pLink->GetLinkManager()->GetDisplayNames( pLink, 0, &sFileNm, 0, &sFilter );
205 :
206 88 : if( OBJECT_CLIENT_GRF == pLink->GetObjType() )
207 : {
208 18 : SfxObjectShellRef pShell = pLink->GetLinkManager()->GetPersist();
209 18 : if( pShell.Is() )
210 : {
211 18 : if( pShell->IsAbortingImport() )
212 0 : return false;
213 :
214 18 : if( pShell->GetMedium() )
215 18 : sReferer = pShell->GetMedium()->GetName();
216 18 : }
217 : }
218 :
219 88 : switch( pLink->GetObjType() )
220 : {
221 : case OBJECT_CLIENT_GRF:
222 18 : nType = FILETYPE_GRF;
223 18 : bSynchron = pLink->IsSynchron();
224 18 : break;
225 :
226 : case OBJECT_CLIENT_FILE:
227 70 : nType = FILETYPE_TEXT;
228 70 : 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 88 : SetUpdateTimeout( 0 );
240 :
241 : // and now register by this or other found Pseudo-Object
242 88 : AddDataAdvise( pLink, SotExchange::GetFormatMimeType( pLink->GetContentType()), 0 );
243 88 : return true;
244 : }
245 :
246 12 : bool SvFileObject::LoadFile_Impl()
247 : {
248 : // We are still at Loading!!
249 12 : if( bWaitForData || !bLoadAgain || xMed.Is() )
250 0 : return false;
251 :
252 : // at the moment on the current DocShell
253 12 : xMed = new SfxMedium( sFileNm, sReferer, STREAM_STD_READ );
254 : SvLinkSource::StreamToLoadFrom aStreamToLoadFrom =
255 12 : getStreamToLoadFrom();
256 : xMed->setStreamToLoadFrom(
257 : aStreamToLoadFrom.m_xInputStreamToLoadFrom,
258 12 : aStreamToLoadFrom.m_bIsReadOnly);
259 :
260 12 : if( !bSynchron )
261 : {
262 12 : bLoadAgain = bDataReady = bInNewData = false;
263 12 : bWaitForData = true;
264 :
265 12 : SfxMediumRef xTmpMed = xMed;
266 12 : bInCallDownload = true;
267 12 : xMed->Download( STATIC_LINK( this, SvFileObject, LoadGrfReady_Impl ) );
268 12 : bInCallDownload = false;
269 :
270 12 : bClearMedium = !xMed.Is();
271 12 : if( bClearMedium )
272 0 : xMed = xTmpMed; // If already finished in Download
273 12 : 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 16 : bool SvFileObject::GetGraphic_Impl( Graphic& rGrf, SvStream* pStream )
290 : {
291 16 : GraphicFilter& rGF = GraphicFilter::GetGraphicFilter();
292 :
293 20 : const sal_uInt16 nFilter = !sFilter.isEmpty() && rGF.GetImportFormatCount()
294 4 : ? rGF.GetImportFormatNumber( sFilter )
295 20 : : GRFILTER_FORMAT_DONTKNOW;
296 :
297 : int nRes;
298 :
299 : // To avoid that a native link is created
300 48 : if( !rGrf.IsLink() &&
301 32 : !rGrf.GetContext() && !bNativFormat )
302 16 : rGrf.SetLink( GfxLink() );
303 :
304 16 : if( !pStream )
305 16 : nRes = xMed.Is() ? GRFILTER_OPENERROR
306 : : rGF.ImportGraphic( rGrf, INetURLObject(sFileNm),
307 16 : 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 16 : if( pStream && ERRCODE_IO_PENDING == pStream->GetError() )
317 0 : pStream->ResetError();
318 :
319 16 : if( nRes )
320 : {
321 16 : 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 16 : 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( vcl::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 24 : 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 12 : pThis->bLoadError = false;
466 12 : pThis->bWaitForData = false;
467 12 : pThis->bInCallDownload = false;
468 :
469 12 : if( !pThis->bInNewData && !pThis->bDataReady )
470 : {
471 : // Graphic is finished, also send DataChanged from Status change
472 12 : pThis->bDataReady = true;
473 12 : pThis->SendStateChg_Impl( sfx2::LinkManager::STATE_LOAD_OK );
474 :
475 : // and then send the data again
476 12 : pThis->NotifyDataChanged();
477 : }
478 :
479 12 : if( pThis->bDataReady )
480 : {
481 12 : pThis->bLoadAgain = true;
482 12 : 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 12 : 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 28 : bool SvFileObject::IsPending() const
539 : {
540 28 : return FILETYPE_GRF == nType && !bLoadError && bWaitForData;
541 : }
542 :
543 68 : bool SvFileObject::IsDataComplete() const
544 : {
545 68 : bool bRet = false;
546 68 : if( FILETYPE_GRF != nType )
547 0 : bRet = true;
548 68 : 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 68 : return bRet;
563 : }
564 :
565 :
566 :
567 8 : void SvFileObject::CancelTransfers()
568 : {
569 : // unsubscribe from the cache if in the middle of loading
570 8 : if( !bDataReady )
571 : {
572 : // Do not set-up again
573 2 : bLoadAgain = false;
574 2 : bDataReady = bLoadError = bWaitForData = true;
575 2 : SendStateChg_Impl( sfx2::LinkManager::STATE_LOAD_ABORT );
576 : }
577 8 : }
578 :
579 :
580 14 : void SvFileObject::SendStateChg_Impl( sfx2::LinkManager::LinkState nState )
581 : {
582 14 : if( !bStateChangeCalled && HasDataLinks() )
583 : {
584 14 : css::uno::Any aAny;
585 14 : aAny <<= OUString::number( nState );
586 : DataChanged( SotExchange::GetFormatName(
587 14 : sfx2::LinkManager::RegisterStatusInfoId()), aAny );
588 14 : bStateChangeCalled = true;
589 : }
590 965 : }
591 :
592 :
593 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|