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 <unotools/ucbstreamhelper.hxx>
31 #include <unotools/localfilehelper.hxx>
32 #include <ucbhelper/content.hxx>
33 #include <ucbhelper/contentbroker.hxx>
34 #include <unotools/datetime.hxx>
35 #include <svx/svdotext.hxx>
36 #include "svx/svditext.hxx"
37 #include <svx/svdmodel.hxx>
38 #include <editeng/editdata.hxx>
39 #include <sfx2/lnkbase.hxx>
40 #include <sfx2/linkmgr.hxx>
41 #include <tools/urlobj.hxx>
42 #include <svl/urihelper.hxx>
43 #include <tools/tenccvt.hxx>
46 class ImpSdrObjTextLink
: public ::sfx2::SvBaseLink
51 ImpSdrObjTextLink( SdrTextObj
* pObj1
)
52 : ::sfx2::SvBaseLink( ::sfx2::LINKUPDATE_ONCALL
, FORMAT_FILE
),
55 virtual ~ImpSdrObjTextLink();
57 virtual void Closed();
58 virtual ::sfx2::SvBaseLink::UpdateResult
DataChanged(
59 const String
& rMimeType
, const ::com::sun::star::uno::Any
& rValue
);
61 sal_Bool
Connect() { return 0 != SvBaseLink::GetRealObject(); }
64 ImpSdrObjTextLink::~ImpSdrObjTextLink()
68 void ImpSdrObjTextLink::Closed()
72 // set pLink of the object to NULL, because we are destroying the link instance now
73 ImpSdrObjTextLinkUserData
* pData
=pSdrObj
->GetLinkUserData();
74 if (pData
!=NULL
) pData
->pLink
=NULL
;
75 pSdrObj
->ReleaseTextLink();
81 ::sfx2::SvBaseLink::UpdateResult
ImpSdrObjTextLink::DataChanged(
82 const String
& /*rMimeType*/, const ::com::sun::star::uno::Any
& /*rValue */)
84 bool bForceReload
= false;
85 SdrModel
* pModel
= pSdrObj
? pSdrObj
->GetModel() : 0;
86 sfx2::LinkManager
* pLinkManager
= pModel
? pModel
->GetLinkManager() : 0;
89 ImpSdrObjTextLinkUserData
* pData
=pSdrObj
->GetLinkUserData();
94 pLinkManager
->GetDisplayNames( this, 0,&aFile
, 0, &aFilter
);
96 if( !pData
->aFileName
.Equals( aFile
) ||
97 !pData
->aFilterName
.Equals( aFilter
))
99 pData
->aFileName
= aFile
;
100 pData
->aFilterName
= aFilter
;
101 pSdrObj
->SetChanged();
107 pSdrObj
->ReloadLinkedText( bForceReload
);
113 TYPEINIT1(ImpSdrObjTextLinkUserData
,SdrObjUserData
);
115 ImpSdrObjTextLinkUserData::ImpSdrObjTextLinkUserData(SdrTextObj
* pObj1
):
116 SdrObjUserData(SdrInventor
,SDRUSERDATA_OBJTEXTLINK
,0),
118 aFileDate0( DateTime::EMPTY
),
120 eCharSet(RTL_TEXTENCODING_DONTKNOW
)
124 ImpSdrObjTextLinkUserData::~ImpSdrObjTextLinkUserData()
129 SdrObjUserData
* ImpSdrObjTextLinkUserData::Clone(SdrObject
* pObj1
) const
131 ImpSdrObjTextLinkUserData
* pData
=new ImpSdrObjTextLinkUserData((SdrTextObj
*)pObj1
);
132 pData
->aFileName
=aFileName
;
133 pData
->aFilterName
=aFilterName
;
134 pData
->aFileDate0
=aFileDate0
;
135 pData
->eCharSet
=eCharSet
;
141 void SdrTextObj::SetTextLink(const String
& rFileName
, const String
& rFilterName
, rtl_TextEncoding eCharSet
)
143 if(eCharSet
== RTL_TEXTENCODING_DONTKNOW
)
144 eCharSet
= osl_getThreadTextEncoding();
146 ImpSdrObjTextLinkUserData
* pData
=GetLinkUserData();
150 pData
=new ImpSdrObjTextLinkUserData(this);
151 pData
->aFileName
=rFileName
;
152 pData
->aFilterName
=rFilterName
;
153 pData
->eCharSet
=eCharSet
;
154 AppendUserData(pData
);
158 void SdrTextObj::ReleaseTextLink()
161 sal_uInt16 nAnz
=GetUserDataCount();
162 for (sal_uInt16 nNum
=nAnz
; nNum
>0;) {
164 SdrObjUserData
* pData
=GetUserData(nNum
);
165 if (pData
->GetInventor()==SdrInventor
&& pData
->GetId()==SDRUSERDATA_OBJTEXTLINK
) {
166 DeleteUserData(nNum
);
171 bool SdrTextObj::ReloadLinkedText( bool bForceLoad
)
173 ImpSdrObjTextLinkUserData
* pData
= GetLinkUserData();
178 ::ucbhelper::ContentBroker
* pBroker
= ::ucbhelper::ContentBroker::get();
179 DateTime
aFileDT( DateTime::EMPTY
);
180 sal_Bool bExists
= sal_False
, bLoad
= sal_False
;
188 INetURLObject
aURL( pData
->aFileName
);
189 DBG_ASSERT( aURL
.GetProtocol() != INET_PROT_NOT_VALID
, "invalid URL" );
191 ::ucbhelper::Content
aCnt( aURL
.GetMainURL( INetURLObject::NO_DECODE
), ::com::sun::star::uno::Reference
< ::com::sun::star::ucb::XCommandEnvironment
>() );
192 ::com::sun::star::uno::Any
aAny( aCnt
.getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DateModified" ) ) ) );
193 ::com::sun::star::util::DateTime aDateTime
;
196 ::utl::typeConvert( aDateTime
, aFileDT
);
209 bLoad
= ( aFileDT
> pData
->aFileDate0
);
213 bRet
= LoadText( pData
->aFileName
, pData
->aFilterName
, pData
->eCharSet
);
216 pData
->aFileDate0
= aFileDT
;
223 bool SdrTextObj::LoadText(const String
& rFileName
, const String
& /*rFilterName*/, rtl_TextEncoding eCharSet
)
225 INetURLObject
aFileURL( rFileName
);
226 sal_Bool bRet
= sal_False
;
228 if( aFileURL
.GetProtocol() == INET_PROT_NOT_VALID
)
230 rtl::OUString aFileURLStr
;
232 if( ::utl::LocalFileHelper::ConvertPhysicalNameToURL( rFileName
, aFileURLStr
) )
233 aFileURL
= INetURLObject( aFileURLStr
);
235 aFileURL
.SetSmartURL( rFileName
);
238 DBG_ASSERT( aFileURL
.GetProtocol() != INET_PROT_NOT_VALID
, "invalid URL" );
240 SvStream
* pIStm
= ::utl::UcbStreamHelper::CreateStream( aFileURL
.GetMainURL( INetURLObject::NO_DECODE
), STREAM_READ
);
244 pIStm
->SetStreamCharSet(GetSOLoadTextEncoding(eCharSet
));
248 pIStm
->Read(cRTF
, 5);
250 sal_Bool bRTF
= cRTF
[0] == '{' && cRTF
[1] == '\\' && cRTF
[2] == 'r' && cRTF
[3] == 't' && cRTF
[4] == 'f';
254 if( !pIStm
->GetError() )
256 SetText( *pIStm
, aFileURL
.GetMainURL( INetURLObject::NO_DECODE
), sal::static_int_cast
< sal_uInt16
>( bRTF
? EE_FORMAT_RTF
: EE_FORMAT_TEXT
) );
266 ImpSdrObjTextLinkUserData
* SdrTextObj::GetLinkUserData() const
268 ImpSdrObjTextLinkUserData
* pData
=NULL
;
269 sal_uInt16 nAnz
=GetUserDataCount();
270 for (sal_uInt16 nNum
=nAnz
; nNum
>0 && pData
==NULL
;) {
272 pData
=(ImpSdrObjTextLinkUserData
*)GetUserData(nNum
);
273 if (pData
->GetInventor()!=SdrInventor
|| pData
->GetId()!=SDRUSERDATA_OBJTEXTLINK
) {
280 void SdrTextObj::ImpLinkAnmeldung()
282 ImpSdrObjTextLinkUserData
* pData
=GetLinkUserData();
283 sfx2::LinkManager
* pLinkManager
=pModel
!=NULL
? pModel
->GetLinkManager() : NULL
;
284 if (pLinkManager
!=NULL
&& pData
!=NULL
&& pData
->pLink
==NULL
) { // don't register twice
285 pData
->pLink
=new ImpSdrObjTextLink(this);
287 pLinkManager
->InsertFileLink(*pData
->pLink
,OBJECT_CLIENT_FILE
,pData
->aFileName
,
288 pData
->aFilterName
.Len() ?
289 &pData
->aFilterName
: (const String
*)NULL
,
290 (const String
*)NULL
);
292 pLinkManager
->InsertFileLink(*pData
->pLink
,OBJECT_CLIENT_FILE
,pData
->aFileName
,
293 pData
->aFilterName
.Len() ? &pData
->aFilterName
: NULL
,NULL
);
295 pData
->pLink
->Connect();
299 void SdrTextObj::ImpLinkAbmeldung()
301 ImpSdrObjTextLinkUserData
* pData
=GetLinkUserData();
302 sfx2::LinkManager
* pLinkManager
=pModel
!=NULL
? pModel
->GetLinkManager() : NULL
;
303 if (pLinkManager
!=NULL
&& pData
!=NULL
&& pData
->pLink
!=NULL
) { // don't register twice
304 // when doing Remove, *pLink is deleted implicitly
305 pLinkManager
->Remove( pData
->pLink
);
310 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */