1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: fileobj.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
33 #include <vcl/wrkwin.hxx>
34 #include <vcl/msgbox.hxx>
35 #include <tools/urlobj.hxx>
36 #include <tools/stream.hxx>
37 #include <sot/formats.hxx>
38 #include <svtools/filter.hxx>
39 #include <sfx2/lnkbase.hxx>
40 #include <sfx2/app.hxx>
41 #include <sfx2/progress.hxx>
42 #ifndef _SFX_INTERNO_HXX
43 //#include <sfx2/interno.hxx>
45 #include <sfx2/docfilt.hxx>
46 #include <sfx2/filedlghelper.hxx>
47 #include <sot/exchange.hxx>
48 #include <com/sun/star/uno/Any.hxx>
49 #include <com/sun/star/uno/Sequence.hxx>
50 #include <sfx2/docfac.hxx>
51 #include <com/sun/star/document/XTypeDetection.hpp>
52 #include <comphelper/mediadescriptor.hxx>
53 #include <comphelper/processfactory.hxx>
55 #include "fileobj.hxx"
56 #include "linkmgr.hxx"
57 #include <svx/dialmgr.hxx>
58 #include <svx/dialogs.hrc>
59 #include "xoutbmp.hxx"
60 #include "opengrf.hxx"
63 namespace css
= ::com::sun::star
;
65 #define FILETYPE_TEXT 1
66 #define FILETYPE_GRF 2
67 #define FILETYPE_OBJECT 3
69 struct Impl_DownLoadData
74 Impl_DownLoadData( const Link
& rLink
)
76 aTimer
.SetTimeout( 100 );
77 aTimer
.SetTimeoutHdl( rLink
);
78 aGrf
.SetDefaultType();
86 // --------------------------------------------------------------------------
89 SvFileObject::SvFileObject() :
90 pDownLoadData( NULL
), pOldParent( NULL
), nType( FILETYPE_TEXT
)
93 bSynchron
= bLoadError
= bWaitForData
= bDataReady
= bNativFormat
=
94 bClearMedium
= bStateChangeCalled
= bInCallDownLoad
= FALSE
;
98 SvFileObject::~SvFileObject()
102 xMed
->SetDataAvailableLink( Link() );
103 xMed
->SetDoneLink( Link() );
106 delete pDownLoadData
;
110 BOOL
SvFileObject::GetData( ::com::sun::star::uno::Any
& rData
,
111 const String
& rMimeType
,
114 ULONG nFmt
= SotExchange::GetFormatStringId( rMimeType
);
118 if( FORMAT_FILE
== nFmt
)
120 // das Medium muss in der Applikation geoffnet werden, um die
121 // relativen Datei Links aufzuloesen!!!! Wird ueber den
122 // LinkManager und damit von dessen Storage erledigt.
123 rData
<<= rtl::OUString( sFileNm
);
126 ===========================================================================
127 JP 28.02.96: noch eine Baustelle:
128 Idee: hier das Medium und die DocShell anlegen, Doc laden
129 und ueber OLE-SS (GetObj(...)) den Bereich als
130 PseudoObject erfragen. Dieses mit den Daten oder
131 dessen Daten verschicken.
133 ===========================================================================
135 SfxMedium aMed( aFileNm.GetFull(), STREAM_READ, TRUE );
136 aMed.DownLoad(); // nur mal das Medium anfassen (DownLoaden)
138 if( aMed.IsStorage() )
139 pSvData->SetData( SvStorageRef( aMed.GetStorage() ),
143 SvStream* pStream = aMed.GetInStream();
147 UINT32 nLen = pStream->Seek( STREAM_SEEK_TO_END );
148 pStream->Seek( STREAM_SEEK_TO_BEGIN );
150 void* pData = SvMemAlloc( nLen );
151 pStream->Read( pData, nLen );
152 pSvData->SetData( pData, nLen, TRANSFER_MOVE );
161 SfxMediumRef xTmpMed
;
163 if( FORMAT_GDIMETAFILE
== nFmt
|| FORMAT_BITMAP
== nFmt
||
164 SOT_FORMATSTR_ID_SVXB
== nFmt
)
168 //JP 15.07.98: Bug 52959
169 // falls das Nativformat doch erwuenscht ist, muss am
170 // Ende das Flag zurueckgesetzt werden.
171 // wird einzig und allein im sw/ndgrf.cxx benutzt, wenn der Link vom
172 // GraphicNode entfernt wird.
173 BOOL bOldNativFormat
= bNativFormat
;
174 //!!?? bNativFormat = 0 != (ASPECT_ICON & pSvData->GetAspect());
176 // falls gedruckt werden soll, warten wir bis die
177 // Daten vorhanden sind
180 // testhalber mal ein LoadFile rufen um das nach-
181 // laden ueberahaupt anzustossen
185 if( !bInCallDownLoad
)
188 while( bWaitForData
)
189 Application::Reschedule();
197 ( !bWaitForData
&& ( xMed
.Is() || // wurde als URL geladen
198 ( bSynchron
&& LoadFile_Impl() && xMed
.Is() ) )) )
202 // falls es uebers Internet gesogen wurde, nicht
205 bLoadAgain
= !xMed
->IsRemote();
206 bLoadError
= !GetGraphic_Impl( aGrf
, xMed
->GetInStream() );
208 else if( !LoadFile_Impl() ||
209 !GetGraphic_Impl( aGrf
, xMed
.Is() ? xMed
->GetInStream() : 0 ))
213 aGrf
.SetDefaultType();
216 if( SOT_FORMATSTR_ID_SVXB
!= nFmt
)
217 nFmt
= (bLoadError
|| GRAPHIC_BITMAP
== aGrf
.GetType())
219 : FORMAT_GDIMETAFILE
;
221 SvMemoryStream
aMemStm( 0, 65535 );
224 case SOT_FORMATSTR_ID_SVXB
:
225 if( GRAPHIC_NONE
!= aGrf
.GetType() )
227 aMemStm
.SetVersion( SOFFICE_FILEFORMAT_50
);
233 if( !aGrf
.GetBitmap().IsEmpty())
234 aMemStm
<< aGrf
.GetBitmap();
238 if( aGrf
.GetGDIMetaFile().GetActionCount() )
240 GDIMetaFile
aMeta( aGrf
.GetGDIMetaFile() );
241 aMeta
.Write( aMemStm
);
244 rData
<<= css::uno::Sequence
< sal_Int8
>( (sal_Int8
*) aMemStm
.GetData(),
245 aMemStm
.Seek( STREAM_SEEK_TO_END
) );
247 bNativFormat
= bOldNativFormat
;
250 if( xMed
.Is() && !bSynchron
&& bClearMedium
)
253 bClearMedium
= FALSE
;
258 case FILETYPE_OBJECT
:
259 // TODO/LATER: possibility to insert a new object
260 rData
<<= rtl::OUString( sFileNm
);
263 return sal_True
/*0 != aTypeList.Count()*/;
269 BOOL
SvFileObject::Connect( sfx2::SvBaseLink
* pLink
)
271 if( !pLink
|| !pLink
->GetLinkManager() )
274 // teste doch mal, ob nicht ein anderer Link mit der gleichen
275 // Verbindung schon existiert
276 pLink
->GetLinkManager()->GetDisplayNames( pLink
, 0, &sFileNm
, 0, &sFilter
);
278 if( OBJECT_CLIENT_GRF
== pLink
->GetObjType() )
280 SfxObjectShellRef pShell
= pLink
->GetLinkManager()->GetPersist();
283 if( pShell
->IsAbortingImport() )
286 if( pShell
->GetMedium() )
287 sReferer
= pShell
->GetMedium()->GetName();
291 switch( pLink
->GetObjType() )
293 case OBJECT_CLIENT_GRF
:
294 nType
= FILETYPE_GRF
;
295 bSynchron
= pLink
->IsSynchron();
298 case OBJECT_CLIENT_FILE
:
299 nType
= FILETYPE_TEXT
;
302 case OBJECT_CLIENT_OLE
:
303 nType
= FILETYPE_OBJECT
;
304 // TODO/LATER: introduce own type to be used for exchanging
311 SetUpdateTimeout( 0 );
313 // und jetzt bei diesem oder gefundenem Pseudo-Object anmelden
314 AddDataAdvise( pLink
, SotExchange::GetFormatMimeType( pLink
->GetContentType()), 0 );
319 BOOL
SvFileObject::LoadFile_Impl()
321 // wir sind noch im Laden!!
322 if( bWaitForData
|| !bLoadAgain
|| xMed
.Is() || pDownLoadData
)
325 // z.Z. nur auf die aktuelle DocShell
326 xMed
= new SfxMedium( sFileNm
, STREAM_STD_READ
, TRUE
);
327 SvLinkSource::StreamToLoadFrom aStreamToLoadFrom
=
328 getStreamToLoadFrom();
329 xMed
->setStreamToLoadFrom(
330 aStreamToLoadFrom
.m_xInputStreamToLoadFrom
,
331 aStreamToLoadFrom
.m_bIsReadOnly
);
332 // setStreamToLoadFrom(0,0);
333 // Keinen Eintrag im Roter Button Menu
334 xMed
->SetDontCreateCancellable();
336 xMed
->SetReferer( sReferer
);
340 bLoadAgain
= bDataReady
= bInNewData
= FALSE
;
343 SfxMediumRef xTmpMed
= xMed
;
344 xMed
->SetDataAvailableLink( STATIC_LINK( this, SvFileObject
, LoadGrfNewData_Impl
) );
345 bInCallDownLoad
= TRUE
;
346 xMed
->DownLoad( STATIC_LINK( this, SvFileObject
, LoadGrfReady_Impl
) );
347 bInCallDownLoad
= FALSE
;
349 bClearMedium
= !xMed
.Is();
351 xMed
= xTmpMed
; // falls gleich im DownLoad schon schluss ist
356 bDataReady
= bInNewData
= FALSE
;
358 bLoadAgain
= !xMed
->IsRemote();
359 bWaitForData
= FALSE
;
361 // Grafik ist fertig, also DataChanged von der Statusaederung schicken:
362 SendStateChg_Impl( xMed
->GetInStream() && xMed
->GetInStream()->GetError()
363 ? STATE_LOAD_ERROR
: STATE_LOAD_OK
);
369 BOOL
SvFileObject::GetGraphic_Impl( Graphic
& rGrf
, SvStream
* pStream
)
371 GraphicFilter
* pGF
= GetGrfFilter();
373 const USHORT nFilter
= sFilter
.Len() && pGF
->GetImportFormatCount()
374 ? pGF
->GetImportFormatNumber( sFilter
)
375 : GRFILTER_FORMAT_DONTKNOW
;
380 // vermeiden, dass ein native Link angelegt wird
381 if( ( !pStream
|| !pDownLoadData
) && !rGrf
.IsLink() &&
382 !rGrf
.GetContext() && !bNativFormat
)
383 rGrf
.SetLink( GfxLink() );
386 nRes
= xMed
.Is() ? GRFILTER_OPENERROR
387 : pGF
->ImportGraphic( rGrf
, INetURLObject(sFileNm
),
389 else if( !pDownLoadData
)
391 pStream
->Seek( STREAM_SEEK_TO_BEGIN
);
392 nRes
= pGF
->ImportGraphic( rGrf
, aEmptyStr
, *pStream
, nFilter
);
396 nRes
= pGF
->ImportGraphic( pDownLoadData
->aGrf
, aEmptyStr
,
401 rGrf
= pDownLoadData
->aGrf
;
402 if( GRAPHIC_NONE
== rGrf
.GetType() )
403 rGrf
.SetDefaultType();
406 if( !pDownLoadData
->aGrf
.GetContext() )
408 xMed
->SetDataAvailableLink( Link() );
409 // xMed->SetDoneLink( Link() );
410 delete pDownLoadData
, pDownLoadData
= 0;
412 bWaitForData
= FALSE
;
416 // Timer aufsetzen, um zurueck zukehren
417 pDownLoadData
->aTimer
.Start();
422 if( pStream
&& ERRCODE_IO_PENDING
== pStream
->GetError() )
423 pStream
->ResetError();
428 if( xMed
.Is() && !pStream
)
430 DBG_WARNING3( "GrafikFehler [%d] - [%s] URL[%s]",
432 xMed
->GetPhysicalName().GetBuffer(),
433 sFileNm
.GetBuffer() );
437 DBG_WARNING2( "GrafikFehler [%d] - [%s]",
438 nRes
, sFileNm
.GetBuffer() );
443 return GRFILTER_OK
== nRes
;
446 /** detect the filter of the given file
449 specifies the URL of the file which filter is to detected.<br/>
450 If the URL doesn't denote a valid (existent and accessible) file, the
451 request is silently dropped.
453 String
impl_getFilter( const String
& _rURL
)
456 if ( _rURL
.Len() == 0 )
461 css::uno::Reference
< ::com::sun::star::document::XTypeDetection
> xTypeDetection(
462 ::comphelper::getProcessServiceFactory()->createInstance(
463 ::rtl::OUString::createFromAscii("com.sun.star.document.TypeDetection") ),
464 css::uno::UNO_QUERY
);
465 if ( xTypeDetection
.is() )
467 ::comphelper::MediaDescriptor aDescr
;
468 aDescr
[ ::comphelper::MediaDescriptor::PROP_URL() ] <<= ::rtl::OUString( _rURL
);
469 css::uno::Sequence
< css::beans::PropertyValue
> aDescrList
=
470 aDescr
.getAsConstPropertyValueList();
471 ::rtl::OUString sType
= xTypeDetection
->queryTypeByDescriptor( aDescrList
, sal_True
);
472 if ( sType
.getLength() )
474 css::uno::Reference
< css::container::XNameAccess
> xTypeCont( xTypeDetection
,
475 css::uno::UNO_QUERY
);
476 if ( xTypeCont
.is() )
478 ::comphelper::SequenceAsHashMap
lTypeProps( xTypeCont
->getByName( sType
) );
479 sFilter
= lTypeProps
.getUnpackedValueOrDefault(
480 ::rtl::OUString::createFromAscii("PreferredFilter"), ::rtl::OUString() );
485 catch( const css::uno::Exception
& )
492 void SvFileObject::Edit( Window
* pParent
, sfx2::SvBaseLink
* pLink
, const Link
& rEndEditHdl
)
494 aEndEditLink
= rEndEditHdl
;
495 String sFile
, sRange
, sTmpFilter
;
496 if( pLink
&& pLink
->GetLinkManager() )
498 pLink
->GetLinkManager()->GetDisplayNames( pLink
, 0, &sFile
, &sRange
, &sTmpFilter
);
500 switch( pLink
->GetObjType() )
502 case OBJECT_CLIENT_GRF
:
504 nType
= FILETYPE_GRF
; // falls noch nicht gesetzt
506 SvxOpenGraphicDialog
aDlg(ResId(RID_SVXSTR_EDITGRFLINK
, DIALOG_MGR()));
507 aDlg
.EnableLink(sal_False
);
508 aDlg
.SetPath( sFile
, sal_True
);
509 aDlg
.SetCurrentFilter( sTmpFilter
);
511 if( !aDlg
.Execute() )
513 sFile
= aDlg
.GetPath();
514 sFile
+= ::sfx2::cTokenSeperator
;
515 sFile
+= ::sfx2::cTokenSeperator
;
516 sFile
+= aDlg
.GetCurrentFilter();
518 if ( aEndEditLink
.IsSet() )
519 aEndEditLink
.Call( &sFile
);
526 case OBJECT_CLIENT_OLE
:
528 nType
= FILETYPE_OBJECT
; // if not set already
529 pOldParent
= Application::GetDefDialogParent();
530 Application::SetDefDialogParent( pParent
);
532 ::sfx2::FileDialogHelper
* pFileDlg
=
533 pLink
->GetFileDialog( (SFXWB_INSERT
| WB_3DLOOK
), String() );
534 pFileDlg
->StartExecuteModal( LINK( this, SvFileObject
, DialogClosedHdl
) );
538 case OBJECT_CLIENT_FILE
:
540 nType
= FILETYPE_TEXT
; // if not set already
541 pOldParent
= Application::GetDefDialogParent();
542 Application::SetDefDialogParent( pParent
);
545 SfxObjectShell
* pShell
= pLink
->GetLinkManager()->GetPersist();
547 sFactory
= pShell
->GetFactory().GetFactoryName();
549 ::sfx2::FileDialogHelper
* pFileDlg
=
550 pLink
->GetFileDialog( (SFXWB_INSERT
| WB_3DLOOK
), sFactory
);
551 pFileDlg
->StartExecuteModal( LINK( this, SvFileObject
, DialogClosedHdl
) );
561 IMPL_STATIC_LINK( SvFileObject
, LoadGrfReady_Impl
, void*, EMPTYARG
)
563 // wenn wir von hier kommen, kann es kein Fehler mehr sein
564 pThis
->bLoadError
= FALSE
;
565 pThis
->bWaitForData
= FALSE
;
566 pThis
->bInCallDownLoad
= FALSE
;
568 if( !pThis
->bInNewData
&& !pThis
->bDataReady
)
570 // Grafik ist fertig, also DataChanged von der Status-
571 // aederung schicken:
572 pThis
->bDataReady
= TRUE
;
573 pThis
->SendStateChg_Impl( STATE_LOAD_OK
);
575 // und dann nochmal die Daten senden
576 pThis
->NotifyDataChanged();
579 if( pThis
->bDataReady
)
581 pThis
->bLoadAgain
= TRUE
;
582 if( pThis
->xMed
.Is() )
584 pThis
->xMed
->SetDataAvailableLink( Link() );
585 pThis
->xMed
->SetDoneLink( Link() );
587 Application::PostUserEvent(
588 STATIC_LINK( pThis
, SvFileObject
, DelMedium_Impl
),
589 new SfxMediumRef( pThis
->xMed
));
592 if( pThis
->pDownLoadData
)
593 delete pThis
->pDownLoadData
, pThis
->pDownLoadData
= 0;
599 IMPL_STATIC_LINK( SvFileObject
, DelMedium_Impl
, SfxMediumRef
*, pDelMed
)
606 IMPL_STATIC_LINK( SvFileObject
, LoadGrfNewData_Impl
, void*, EMPTYARG
)
608 // wenn wir von hier kommen, kann es kein Fehler mehr sein
609 if( pThis
->bInNewData
)
612 pThis
->bInNewData
= TRUE
;
613 pThis
->bLoadError
= FALSE
;
615 if( !pThis
->pDownLoadData
)
617 pThis
->pDownLoadData
= new Impl_DownLoadData(
618 STATIC_LINK( pThis
, SvFileObject
, LoadGrfNewData_Impl
) );
620 // Null-Link setzen, damit keine temporaeren Grafiken
621 // rausgeswapt werden; der Filter prueft, ob schon
622 // ein Link gesetzt ist => falls dies zutrifft, wird
623 // _kein_ neuer Link gesetzt; der Link muss hier gesetzt werden,
624 // (bevor das erste Mal gefiltert wird), um zu verhindern,
625 // dass der Kontext zurueckgesetzt wird (aynchrones Laden)
626 if( !pThis
->bNativFormat
)
628 static GfxLink aDummyLink
;
629 pThis
->pDownLoadData
->aGrf
.SetLink( aDummyLink
);
633 pThis
->NotifyDataChanged();
635 SvStream
* pStrm
= pThis
->xMed
.Is() ? pThis
->xMed
->GetInStream() : 0;
636 if( pStrm
&& pStrm
->GetError() )
638 if( ERRCODE_IO_PENDING
== pStrm
->GetError() )
641 // im DataChanged ein DataReady?
642 else if( pThis
->bWaitForData
&& pThis
->pDownLoadData
)
644 pThis
->bLoadError
= TRUE
;
648 if( pThis
->bDataReady
)
650 // Grafik ist fertig, also DataChanged von der Status-
651 // aederung schicken:
652 pThis
->SendStateChg_Impl( pStrm
->GetError() ? STATE_LOAD_ERROR
656 pThis
->bInNewData
= FALSE
;
660 IMPL_LINK( SvFileObject
, DialogClosedHdl
, sfx2::FileDialogHelper
*, _pFileDlg
)
663 Application::SetDefDialogParent( pOldParent
);
665 if ( FILETYPE_TEXT
== nType
|| FILETYPE_OBJECT
== nType
)
667 if ( _pFileDlg
&& _pFileDlg
->GetError() == ERRCODE_NONE
)
669 String
sURL( _pFileDlg
->GetPath() );
671 sFile
+= ::sfx2::cTokenSeperator
;
672 sFile
+= ::sfx2::cTokenSeperator
;
673 sFile
+= impl_getFilter( sURL
);
678 DBG_ERRORFILE( "SvFileObject::DialogClosedHdl(): wrong file type" );
681 if ( aEndEditLink
.IsSet() )
682 aEndEditLink
.Call( &sFile
);
688 Die Methode stellt fest, ob aus einem DDE-Object die Daten gelesen
691 ERRCODE_NONE wenn sie komplett gelesen wurde
692 ERRCODE_SO_PENDING wenn sie noch nicht komplett gelesen wurde
693 ERRCODE_SO_FALSE sonst
695 BOOL
SvFileObject::IsPending() const
697 return FILETYPE_GRF
== nType
&& !bLoadError
&&
698 ( pDownLoadData
|| bWaitForData
);
700 BOOL
SvFileObject::IsDataComplete() const
703 if( FILETYPE_GRF
!= nType
)
705 else if( !bLoadError
&& ( !bWaitForData
&& !pDownLoadData
))
707 SvFileObject
* pThis
= (SvFileObject
*)this;
709 ( bSynchron
&& pThis
->LoadFile_Impl() && xMed
.Is() ) )
713 INetURLObject
aUrl( sFileNm
);
714 if( aUrl
.HasError() ||
715 INET_PROT_NOT_VALID
== aUrl
.GetProtocol() )
724 void SvFileObject::CancelTransfers()
727 xMed
->CancelTransfers();
729 // und aus dem Cache austragen, wenn man mitten im Laden ist
732 // nicht noch mal aufsetzen
734 bDataReady
= bLoadError
= bWaitForData
= TRUE
;
735 SendStateChg_Impl( STATE_LOAD_ABORT
);
740 void SvFileObject::SetTransferPriority( USHORT
)
745 void SvFileObject::SendStateChg_Impl( LinkState nState
)
747 if( !bStateChangeCalled
&& HasDataLinks() )
750 aAny
<<= rtl::OUString::valueOf( (sal_Int32
)nState
);
751 DataChanged( SotExchange::GetFormatName(
752 SvxLinkManager::RegisterStatusInfoId()), aAny
);
753 bStateChangeCalled
= TRUE
;