1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "sal/config.h"
22 #include <comphelper/processfactory.hxx>
23 #include <osl/thread.h>
24 #include <unotools/ucbstreamhelper.hxx>
25 #include <unotools/localfilehelper.hxx>
26 #include <ucbhelper/content.hxx>
27 #include <unotools/datetime.hxx>
28 #include <svx/svdotext.hxx>
29 #include <svx/svdmodel.hxx>
30 #include <editeng/editdata.hxx>
31 #include <sfx2/lnkbase.hxx>
32 #include <sfx2/linkmgr.hxx>
33 #include <tools/urlobj.hxx>
34 #include <svl/urihelper.hxx>
35 #include <tools/tenccvt.hxx>
38 class ImpSdrObjTextLink
: public ::sfx2::SvBaseLink
43 ImpSdrObjTextLink( SdrTextObj
* pObj1
)
44 : ::sfx2::SvBaseLink( ::sfx2::LINKUPDATE_ONCALL
, FORMAT_FILE
),
47 virtual ~ImpSdrObjTextLink();
49 virtual void Closed() SAL_OVERRIDE
;
50 virtual ::sfx2::SvBaseLink::UpdateResult
DataChanged(
51 const OUString
& rMimeType
, const ::com::sun::star::uno::Any
& rValue
) SAL_OVERRIDE
;
54 ImpSdrObjTextLink::~ImpSdrObjTextLink()
58 void ImpSdrObjTextLink::Closed()
62 // set pLink of the object to NULL, because we are destroying the link instance now
63 ImpSdrObjTextLinkUserData
* pData
=pSdrObj
->GetLinkUserData();
64 if (pData
!=NULL
) pData
->pLink
=NULL
;
65 pSdrObj
->ReleaseTextLink();
71 ::sfx2::SvBaseLink::UpdateResult
ImpSdrObjTextLink::DataChanged(
72 const OUString
& /*rMimeType*/, const ::com::sun::star::uno::Any
& /*rValue */)
74 bool bForceReload
= false;
75 SdrModel
* pModel
= pSdrObj
? pSdrObj
->GetModel() : 0;
76 sfx2::LinkManager
* pLinkManager
= pModel
? pModel
->GetLinkManager() : 0;
79 ImpSdrObjTextLinkUserData
* pData
=pSdrObj
->GetLinkUserData();
84 pLinkManager
->GetDisplayNames( this, 0,&aFile
, 0, &aFilter
);
86 if( pData
->aFileName
!= aFile
||
87 pData
->aFilterName
!= aFilter
)
89 pData
->aFileName
= aFile
;
90 pData
->aFilterName
= aFilter
;
91 pSdrObj
->SetChanged();
97 pSdrObj
->ReloadLinkedText( bForceReload
);
103 TYPEINIT1(ImpSdrObjTextLinkUserData
,SdrObjUserData
);
105 ImpSdrObjTextLinkUserData::ImpSdrObjTextLinkUserData(SdrTextObj
* pObj1
):
106 SdrObjUserData(SdrInventor
,SDRUSERDATA_OBJTEXTLINK
,0),
108 aFileDate0( DateTime::EMPTY
),
110 eCharSet(RTL_TEXTENCODING_DONTKNOW
)
114 ImpSdrObjTextLinkUserData::~ImpSdrObjTextLinkUserData()
119 SdrObjUserData
* ImpSdrObjTextLinkUserData::Clone(SdrObject
* pObj1
) const
121 ImpSdrObjTextLinkUserData
* pData
=new ImpSdrObjTextLinkUserData((SdrTextObj
*)pObj1
);
122 pData
->aFileName
=aFileName
;
123 pData
->aFilterName
=aFilterName
;
124 pData
->aFileDate0
=aFileDate0
;
125 pData
->eCharSet
=eCharSet
;
131 void SdrTextObj::SetTextLink(const OUString
& rFileName
, const OUString
& rFilterName
, rtl_TextEncoding eCharSet
)
133 if(eCharSet
== RTL_TEXTENCODING_DONTKNOW
)
134 eCharSet
= osl_getThreadTextEncoding();
136 ImpSdrObjTextLinkUserData
* pData
=GetLinkUserData();
140 pData
=new ImpSdrObjTextLinkUserData(this);
141 pData
->aFileName
=rFileName
;
142 pData
->aFilterName
=rFilterName
;
143 pData
->eCharSet
=eCharSet
;
144 AppendUserData(pData
);
148 void SdrTextObj::ReleaseTextLink()
151 sal_uInt16 nAnz
=GetUserDataCount();
152 for (sal_uInt16 nNum
=nAnz
; nNum
>0;) {
154 SdrObjUserData
* pData
=GetUserData(nNum
);
155 if (pData
->GetInventor()==SdrInventor
&& pData
->GetId()==SDRUSERDATA_OBJTEXTLINK
) {
156 DeleteUserData(nNum
);
161 bool SdrTextObj::ReloadLinkedText( bool bForceLoad
)
163 ImpSdrObjTextLinkUserData
* pData
= GetLinkUserData();
168 DateTime
aFileDT( DateTime::EMPTY
);
173 INetURLObject
aURL( pData
->aFileName
);
174 DBG_ASSERT( aURL
.GetProtocol() != INET_PROT_NOT_VALID
, "invalid URL" );
176 ::ucbhelper::Content
aCnt( aURL
.GetMainURL( INetURLObject::NO_DECODE
), ::com::sun::star::uno::Reference
< ::com::sun::star::ucb::XCommandEnvironment
>(), comphelper::getProcessComponentContext() );
177 ::com::sun::star::uno::Any
aAny( aCnt
.getPropertyValue("DateModified") );
178 ::com::sun::star::util::DateTime aDateTime
;
181 ::utl::typeConvert( aDateTime
, aFileDT
);
194 bLoad
= ( aFileDT
> pData
->aFileDate0
);
198 bRet
= LoadText( pData
->aFileName
, pData
->aFilterName
, pData
->eCharSet
);
201 pData
->aFileDate0
= aFileDT
;
208 bool SdrTextObj::LoadText(const OUString
& rFileName
, const OUString
& /*rFilterName*/, rtl_TextEncoding eCharSet
)
210 INetURLObject
aFileURL( rFileName
);
213 if( aFileURL
.GetProtocol() == INET_PROT_NOT_VALID
)
215 OUString aFileURLStr
;
217 if( ::utl::LocalFileHelper::ConvertPhysicalNameToURL( rFileName
, aFileURLStr
) )
218 aFileURL
= INetURLObject( aFileURLStr
);
220 aFileURL
.SetSmartURL( rFileName
);
223 DBG_ASSERT( aFileURL
.GetProtocol() != INET_PROT_NOT_VALID
, "invalid URL" );
225 SvStream
* pIStm
= ::utl::UcbStreamHelper::CreateStream( aFileURL
.GetMainURL( INetURLObject::NO_DECODE
), STREAM_READ
);
229 pIStm
->SetStreamCharSet(GetSOLoadTextEncoding(eCharSet
));
233 pIStm
->Read(cRTF
, 5);
235 bool bRTF
= cRTF
[0] == '{' && cRTF
[1] == '\\' && cRTF
[2] == 'r' && cRTF
[3] == 't' && cRTF
[4] == 'f';
239 if( !pIStm
->GetError() )
241 SetText( *pIStm
, aFileURL
.GetMainURL( INetURLObject::NO_DECODE
), sal::static_int_cast
< sal_uInt16
>( bRTF
? EE_FORMAT_RTF
: EE_FORMAT_TEXT
) );
251 ImpSdrObjTextLinkUserData
* SdrTextObj::GetLinkUserData() const
253 ImpSdrObjTextLinkUserData
* pData
=NULL
;
254 sal_uInt16 nAnz
=GetUserDataCount();
255 for (sal_uInt16 nNum
=nAnz
; nNum
>0 && pData
==NULL
;) {
257 pData
=(ImpSdrObjTextLinkUserData
*)GetUserData(nNum
);
258 if (pData
->GetInventor()!=SdrInventor
|| pData
->GetId()!=SDRUSERDATA_OBJTEXTLINK
) {
265 void SdrTextObj::ImpLinkAnmeldung()
267 ImpSdrObjTextLinkUserData
* pData
=GetLinkUserData();
268 sfx2::LinkManager
* pLinkManager
=pModel
!=NULL
? pModel
->GetLinkManager() : NULL
;
269 if (pLinkManager
!=NULL
&& pData
!=NULL
&& pData
->pLink
==NULL
) { // don't register twice
270 pData
->pLink
= new ImpSdrObjTextLink(this);
271 pLinkManager
->InsertFileLink(*pData
->pLink
,OBJECT_CLIENT_FILE
,pData
->aFileName
,
272 !pData
->aFilterName
.isEmpty() ?
273 &pData
->aFilterName
: NULL
,
278 void SdrTextObj::ImpLinkAbmeldung()
280 ImpSdrObjTextLinkUserData
* pData
=GetLinkUserData();
281 sfx2::LinkManager
* pLinkManager
=pModel
!=NULL
? pModel
->GetLinkManager() : NULL
;
282 if (pLinkManager
!=NULL
&& pData
!=NULL
&& pData
->pLink
!=NULL
) { // don't register twice
283 // when doing Remove, *pLink is deleted implicitly
284 pLinkManager
->Remove( pData
->pLink
);
289 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */