1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <vcl/wrkwin.hxx>
31 #include <vcl/msgbox.hxx>
32 #include <tools/urlobj.hxx>
33 #include <tools/stream.hxx>
34 #include <sot/formats.hxx>
35 #include <svtools/filter.hxx>
36 #include <sfx2/lnkbase.hxx>
37 #include <sfx2/app.hxx>
38 #include <sfx2/progress.hxx>
39 #include <sfx2/docfilt.hxx>
40 #include <sfx2/filedlghelper.hxx>
41 #include <sot/exchange.hxx>
42 #include <com/sun/star/uno/Any.hxx>
43 #include <com/sun/star/uno/Sequence.hxx>
44 #include <sfx2/docfac.hxx>
45 #include <com/sun/star/document/XTypeDetection.hpp>
46 #include <comphelper/mediadescriptor.hxx>
47 #include <comphelper/processfactory.hxx>
48 #include <sfx2/linkmgr.hxx>
49 #include <sfx2/opengrf.hxx>
50 #include "sfx2/sfxresid.hxx"
51 #include "fileobj.hxx"
54 namespace css
= ::com::sun::star
;
56 #define FILETYPE_TEXT 1
57 #define FILETYPE_GRF 2
58 #define FILETYPE_OBJECT 3
60 struct Impl_DownLoadData
65 Impl_DownLoadData( const Link
& rLink
)
67 aTimer
.SetTimeout( 100 );
68 aTimer
.SetTimeoutHdl( rLink
);
69 aGrf
.SetDefaultType();
77 // --------------------------------------------------------------------------
80 SvFileObject::SvFileObject() :
81 pDownLoadData( NULL
), pOldParent( NULL
), nType( FILETYPE_TEXT
)
83 bLoadAgain
= sal_True
;
84 bSynchron
= bLoadError
= bWaitForData
= bDataReady
= bNativFormat
=
85 bClearMedium
= bStateChangeCalled
= bInCallDownLoad
= sal_False
;
89 SvFileObject::~SvFileObject()
93 xMed
->SetDataAvailableLink( Link() );
94 xMed
->SetDoneLink( Link() );
101 sal_Bool
SvFileObject::GetData( ::com::sun::star::uno::Any
& rData
,
102 const String
& rMimeType
,
103 sal_Bool bGetSynchron
)
105 sal_uIntPtr nFmt
= SotExchange::GetFormatStringId( rMimeType
);
109 if( FORMAT_FILE
== nFmt
)
111 // The media in the application must be opened to lookup the
112 // relative file links!! This is done through the link manager
114 rData
<<= rtl::OUString( sFileNm
);
121 SfxMediumRef xTmpMed
;
123 if( FORMAT_GDIMETAFILE
== nFmt
|| FORMAT_BITMAP
== nFmt
||
124 SOT_FORMATSTR_ID_SVXB
== nFmt
)
128 // If the native format is reqested, has to be reset at the
129 // end of the flag. Is solely in the sw/ndgrf.cxx used when
130 // the link is removed form GraphicNode.
131 sal_Bool bOldNativFormat
= bNativFormat
;
133 // If about to print, waiting for the data to be available
136 // call a LoadFile every second time to test the loading
140 if( !bInCallDownLoad
)
143 while( bWaitForData
)
144 Application::Reschedule();
147 bClearMedium
= sal_True
;
152 ( !bWaitForData
&& ( xMed
.Is() || // was loaded as URL
153 ( bSynchron
&& LoadFile_Impl() && xMed
.Is() ) )) )
155 // If it was loaded from the Internet, do not retry
157 bLoadAgain
= !xMed
->IsRemote();
158 bLoadError
= !GetGraphic_Impl( aGrf
, xMed
->GetInStream() );
160 else if( !LoadFile_Impl() ||
161 !GetGraphic_Impl( aGrf
, xMed
.Is() ? xMed
->GetInStream() : 0 ))
165 aGrf
.SetDefaultType();
168 if( SOT_FORMATSTR_ID_SVXB
!= nFmt
)
169 nFmt
= (bLoadError
|| GRAPHIC_BITMAP
== aGrf
.GetType())
171 : FORMAT_GDIMETAFILE
;
173 SvMemoryStream
aMemStm( 0, 65535 );
176 case SOT_FORMATSTR_ID_SVXB
:
177 if( GRAPHIC_NONE
!= aGrf
.GetType() )
179 aMemStm
.SetVersion( SOFFICE_FILEFORMAT_50
);
185 if( !aGrf
.GetBitmap().IsEmpty())
186 aMemStm
<< aGrf
.GetBitmap();
190 if( aGrf
.GetGDIMetaFile().GetActionSize() )
192 GDIMetaFile
aMeta( aGrf
.GetGDIMetaFile() );
193 aMeta
.Write( aMemStm
);
196 rData
<<= css::uno::Sequence
< sal_Int8
>( (sal_Int8
*) aMemStm
.GetData(),
197 aMemStm
.Seek( STREAM_SEEK_TO_END
) );
199 bNativFormat
= bOldNativFormat
;
202 if( xMed
.Is() && !bSynchron
&& bClearMedium
)
205 bClearMedium
= sal_False
;
210 case FILETYPE_OBJECT
:
211 // TODO/LATER: possibility to insert a new object
212 rData
<<= rtl::OUString( sFileNm
);
215 return sal_True
/*0 != aTypeList.Count()*/;
218 sal_Bool
SvFileObject::Connect( sfx2::SvBaseLink
* pLink
)
220 if( !pLink
|| !pLink
->GetLinkManager() )
223 // Test if not another link of the same connection already exists
224 pLink
->GetLinkManager()->GetDisplayNames( pLink
, 0, &sFileNm
, 0, &sFilter
);
226 if( OBJECT_CLIENT_GRF
== pLink
->GetObjType() )
228 SfxObjectShellRef pShell
= pLink
->GetLinkManager()->GetPersist();
231 if( pShell
->IsAbortingImport() )
234 if( pShell
->GetMedium() )
235 sReferer
= pShell
->GetMedium()->GetName();
239 switch( pLink
->GetObjType() )
241 case OBJECT_CLIENT_GRF
:
242 nType
= FILETYPE_GRF
;
243 bSynchron
= pLink
->IsSynchron();
246 case OBJECT_CLIENT_FILE
:
247 nType
= FILETYPE_TEXT
;
250 case OBJECT_CLIENT_OLE
:
251 nType
= FILETYPE_OBJECT
;
252 // TODO/LATER: introduce own type to be used for exchanging
259 SetUpdateTimeout( 0 );
261 // and now register by this or other found Pseudo-Object
262 AddDataAdvise( pLink
, SotExchange::GetFormatMimeType( pLink
->GetContentType()), 0 );
266 sal_Bool
SvFileObject::LoadFile_Impl()
268 // We are still at Loading!!
269 if( bWaitForData
|| !bLoadAgain
|| xMed
.Is() || pDownLoadData
)
272 // at the moment on the current DocShell
273 xMed
= new SfxMedium( sFileNm
, STREAM_STD_READ
);
274 SvLinkSource::StreamToLoadFrom aStreamToLoadFrom
=
275 getStreamToLoadFrom();
276 xMed
->setStreamToLoadFrom(
277 aStreamToLoadFrom
.m_xInputStreamToLoadFrom
,
278 aStreamToLoadFrom
.m_bIsReadOnly
);
280 xMed
->SetReferer( sReferer
);
284 bLoadAgain
= bDataReady
= bInNewData
= sal_False
;
285 bWaitForData
= sal_True
;
287 SfxMediumRef xTmpMed
= xMed
;
288 xMed
->SetDataAvailableLink( STATIC_LINK( this, SvFileObject
, LoadGrfNewData_Impl
) );
289 bInCallDownLoad
= sal_True
;
290 xMed
->DownLoad( STATIC_LINK( this, SvFileObject
, LoadGrfReady_Impl
) );
291 bInCallDownLoad
= sal_False
;
293 bClearMedium
= !xMed
.Is();
295 xMed
= xTmpMed
; // If already finished in DownLoad
299 bWaitForData
= sal_True
;
300 bDataReady
= bInNewData
= sal_False
;
302 bLoadAgain
= !xMed
->IsRemote();
303 bWaitForData
= sal_False
;
305 // Graphic is finished, also send DataChanged of the Status change:
306 SendStateChg_Impl( xMed
->GetInStream() && xMed
->GetInStream()->GetError()
307 ? sfx2::LinkManager::STATE_LOAD_ERROR
: sfx2::LinkManager::STATE_LOAD_OK
);
312 sal_Bool
SvFileObject::GetGraphic_Impl( Graphic
& rGrf
, SvStream
* pStream
)
314 GraphicFilter
& rGF
= GraphicFilter::GetGraphicFilter();
316 const sal_uInt16 nFilter
= sFilter
.Len() && rGF
.GetImportFormatCount()
317 ? rGF
.GetImportFormatNumber( sFilter
)
318 : GRFILTER_FORMAT_DONTKNOW
;
323 // To avoid that a native link is created
324 if( ( !pStream
|| !pDownLoadData
) && !rGrf
.IsLink() &&
325 !rGrf
.GetContext() && !bNativFormat
)
326 rGrf
.SetLink( GfxLink() );
329 nRes
= xMed
.Is() ? GRFILTER_OPENERROR
330 : rGF
.ImportGraphic( rGrf
, INetURLObject(sFileNm
),
332 else if( !pDownLoadData
)
334 pStream
->Seek( STREAM_SEEK_TO_BEGIN
);
335 nRes
= rGF
.ImportGraphic( rGrf
, aEmptyStr
, *pStream
, nFilter
);
339 nRes
= rGF
.ImportGraphic( pDownLoadData
->aGrf
, aEmptyStr
,
344 rGrf
= pDownLoadData
->aGrf
;
345 if( GRAPHIC_NONE
== rGrf
.GetType() )
346 rGrf
.SetDefaultType();
349 if( !pDownLoadData
->aGrf
.GetContext() )
351 xMed
->SetDataAvailableLink( Link() );
352 delete pDownLoadData
, pDownLoadData
= 0;
353 bDataReady
= sal_True
;
354 bWaitForData
= sal_False
;
358 // Set up Timer, to return back
359 pDownLoadData
->aTimer
.Start();
364 if( pStream
&& ERRCODE_IO_PENDING
== pStream
->GetError() )
365 pStream
->ResetError();
370 if( xMed
.Is() && !pStream
)
372 DBG_WARNING3( "Graphic error [%d] - [%s] URL[%s]",
374 xMed
->GetPhysicalName().getStr(),
375 sFileNm
.GetBuffer() );
379 DBG_WARNING2( "Graphic error [%d] - [%s]",
380 nRes
, sFileNm
.GetBuffer() );
385 return GRFILTER_OK
== nRes
;
388 /** detect the filter of the given file
391 specifies the URL of the file which filter is to detected.<br/>
392 If the URL doesn't denote a valid (existent and accessible) file, the
393 request is silently dropped.
395 String
impl_getFilter( const String
& _rURL
)
398 if ( _rURL
.Len() == 0 )
403 css::uno::Reference
< ::com::sun::star::document::XTypeDetection
> xTypeDetection(
404 ::comphelper::getProcessServiceFactory()->createInstance(
405 ::rtl::OUString("com.sun.star.document.TypeDetection") ),
406 css::uno::UNO_QUERY
);
407 if ( xTypeDetection
.is() )
409 ::comphelper::MediaDescriptor aDescr
;
410 aDescr
[ ::comphelper::MediaDescriptor::PROP_URL() ] <<= ::rtl::OUString( _rURL
);
411 css::uno::Sequence
< css::beans::PropertyValue
> aDescrList
=
412 aDescr
.getAsConstPropertyValueList();
413 ::rtl::OUString sType
= xTypeDetection
->queryTypeByDescriptor( aDescrList
, sal_True
);
414 if ( !sType
.isEmpty() )
416 css::uno::Reference
< css::container::XNameAccess
> xTypeCont( xTypeDetection
,
417 css::uno::UNO_QUERY
);
418 if ( xTypeCont
.is() )
420 ::comphelper::SequenceAsHashMap
lTypeProps( xTypeCont
->getByName( sType
) );
421 sFilter
= lTypeProps
.getUnpackedValueOrDefault(
422 ::rtl::OUString("PreferredFilter"), ::rtl::OUString() );
427 catch( const css::uno::Exception
& )
434 void SvFileObject::Edit( Window
* pParent
, sfx2::SvBaseLink
* pLink
, const Link
& rEndEditHdl
)
436 aEndEditLink
= rEndEditHdl
;
437 String sFile
, sRange
, sTmpFilter
;
438 if( pLink
&& pLink
->GetLinkManager() )
440 pLink
->GetLinkManager()->GetDisplayNames( pLink
, 0, &sFile
, &sRange
, &sTmpFilter
);
442 switch( pLink
->GetObjType() )
444 case OBJECT_CLIENT_GRF
:
446 nType
= FILETYPE_GRF
; // If not set already
448 SvxOpenGraphicDialog
aDlg(SfxResId(RID_SVXSTR_EDITGRFLINK
).toString());
449 aDlg
.EnableLink(sal_False
);
450 aDlg
.SetPath( sFile
, sal_True
);
451 aDlg
.SetCurrentFilter( sTmpFilter
);
453 if( !aDlg
.Execute() )
455 sFile
= aDlg
.GetPath();
456 sFile
+= ::sfx2::cTokenSeperator
;
457 sFile
+= ::sfx2::cTokenSeperator
;
458 sFile
+= aDlg
.GetCurrentFilter();
460 if ( aEndEditLink
.IsSet() )
461 aEndEditLink
.Call( &sFile
);
468 case OBJECT_CLIENT_OLE
:
470 nType
= FILETYPE_OBJECT
; // if not set already
471 pOldParent
= Application::GetDefDialogParent();
472 Application::SetDefDialogParent( pParent
);
474 ::sfx2::FileDialogHelper
& rFileDlg
=
475 pLink
->GetInsertFileDialog( String() );
476 rFileDlg
.StartExecuteModal(
477 LINK( this, SvFileObject
, DialogClosedHdl
) );
481 case OBJECT_CLIENT_FILE
:
483 nType
= FILETYPE_TEXT
; // if not set already
484 pOldParent
= Application::GetDefDialogParent();
485 Application::SetDefDialogParent( pParent
);
488 SfxObjectShell
* pShell
= pLink
->GetLinkManager()->GetPersist();
490 sFactory
= pShell
->GetFactory().GetFactoryName();
492 ::sfx2::FileDialogHelper
& rFileDlg
=
493 pLink
->GetInsertFileDialog(sFactory
);
494 rFileDlg
.StartExecuteModal(
495 LINK( this, SvFileObject
, DialogClosedHdl
) );
505 IMPL_STATIC_LINK( SvFileObject
, LoadGrfReady_Impl
, void*, EMPTYARG
)
507 // When we come form here there it can not be an error no more.
508 pThis
->bLoadError
= sal_False
;
509 pThis
->bWaitForData
= sal_False
;
510 pThis
->bInCallDownLoad
= sal_False
;
512 if( !pThis
->bInNewData
&& !pThis
->bDataReady
)
514 // Graphic is finished, also send DataChanged from Status change
515 pThis
->bDataReady
= sal_True
;
516 pThis
->SendStateChg_Impl( sfx2::LinkManager::STATE_LOAD_OK
);
518 // and then send the data again
519 pThis
->NotifyDataChanged();
522 if( pThis
->bDataReady
)
524 pThis
->bLoadAgain
= sal_True
;
525 if( pThis
->xMed
.Is() )
527 pThis
->xMed
->SetDataAvailableLink( Link() );
528 pThis
->xMed
->SetDoneLink( Link() );
530 Application::PostUserEvent(
531 STATIC_LINK( pThis
, SvFileObject
, DelMedium_Impl
),
532 new SfxMediumRef( pThis
->xMed
));
535 if( pThis
->pDownLoadData
)
536 delete pThis
->pDownLoadData
, pThis
->pDownLoadData
= 0;
542 IMPL_STATIC_LINK( SvFileObject
, DelMedium_Impl
, SfxMediumRef
*, pDelMed
)
549 IMPL_STATIC_LINK( SvFileObject
, LoadGrfNewData_Impl
, void*, EMPTYARG
)
551 // When we come form here there it can not be an error no more.
552 if( pThis
->bInNewData
)
555 pThis
->bInNewData
= sal_True
;
556 pThis
->bLoadError
= sal_False
;
558 if( !pThis
->pDownLoadData
)
560 pThis
->pDownLoadData
= new Impl_DownLoadData(
561 STATIC_LINK( pThis
, SvFileObject
, LoadGrfNewData_Impl
) );
562 // Set Zero-link, so that no temporary graphics can be swapped out,
563 // the filter checks whether a link is set already => if so, is _no_
564 // new link set, the link here must be set (before it is first
565 // filtered), to prevent, that the context will be reset
566 // (aynchronous loading)
567 if( !pThis
->bNativFormat
)
569 static GfxLink aDummyLink
;
570 pThis
->pDownLoadData
->aGrf
.SetLink( aDummyLink
);
574 pThis
->NotifyDataChanged();
576 SvStream
* pStrm
= pThis
->xMed
.Is() ? pThis
->xMed
->GetInStream() : 0;
577 if( pStrm
&& pStrm
->GetError() )
579 if( ERRCODE_IO_PENDING
== pStrm
->GetError() )
582 // a DataReady in DataChanged?
583 else if( pThis
->bWaitForData
&& pThis
->pDownLoadData
)
585 pThis
->bLoadError
= sal_True
;
589 if( pThis
->bDataReady
)
591 // Graphic is finished, also send DataChanged from Status change
592 pThis
->SendStateChg_Impl( pStrm
->GetError() ? sfx2::LinkManager::STATE_LOAD_ERROR
: sfx2::LinkManager::STATE_LOAD_OK
);
595 pThis
->bInNewData
= sal_False
;
599 IMPL_LINK( SvFileObject
, DialogClosedHdl
, sfx2::FileDialogHelper
*, _pFileDlg
)
602 Application::SetDefDialogParent( pOldParent
);
604 if ( FILETYPE_TEXT
== nType
|| FILETYPE_OBJECT
== nType
)
606 if ( _pFileDlg
&& _pFileDlg
->GetError() == ERRCODE_NONE
)
608 String
sURL( _pFileDlg
->GetPath() );
610 sFile
+= ::sfx2::cTokenSeperator
;
611 sFile
+= ::sfx2::cTokenSeperator
;
612 sFile
+= impl_getFilter( sURL
);
617 SAL_WARN( "sfx2.appl", "SvFileObject::DialogClosedHdl(): wrong file type" );
620 if ( aEndEditLink
.IsSet() )
621 aEndEditLink
.Call( &sFile
);
627 The method determines whether the data-object can be read from a DDE.
629 The following can be returned:
630 ERRCODE_NONE if it has been completely read
631 ERRCODE_SO_PENDING if it has not been completely read
632 ERRCODE_SO_FALSE otherwise
634 sal_Bool
SvFileObject::IsPending() const
636 return FILETYPE_GRF
== nType
&& !bLoadError
&&
637 ( pDownLoadData
|| bWaitForData
);
639 sal_Bool
SvFileObject::IsDataComplete() const
641 sal_Bool bRet
= sal_False
;
642 if( FILETYPE_GRF
!= nType
)
644 else if( !bLoadError
&& ( !bWaitForData
&& !pDownLoadData
))
646 SvFileObject
* pThis
= (SvFileObject
*)this;
648 ( bSynchron
&& pThis
->LoadFile_Impl() && xMed
.Is() ) )
652 INetURLObject
aUrl( sFileNm
);
653 if( aUrl
.HasError() ||
654 INET_PROT_NOT_VALID
== aUrl
.GetProtocol() )
663 void SvFileObject::CancelTransfers()
665 // unsubscribe from the cache if in the middle of loading
668 // Do not set-up again
669 bLoadAgain
= sal_False
;
670 bDataReady
= bLoadError
= bWaitForData
= sal_True
;
671 SendStateChg_Impl( sfx2::LinkManager::STATE_LOAD_ABORT
);
676 void SvFileObject::SendStateChg_Impl( sfx2::LinkManager::LinkState nState
)
678 if( !bStateChangeCalled
&& HasDataLinks() )
681 aAny
<<= rtl::OUString::valueOf( (sal_Int32
)nState
);
682 DataChanged( SotExchange::GetFormatName(
683 sfx2::LinkManager::RegisterStatusInfoId()), aAny
);
684 bStateChangeCalled
= sal_True
;
689 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */