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/file.hxx>
24 #include <osl/thread.h>
25 #include <unotools/ucbstreamhelper.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 <tools/debug.hxx>
35 #include <tools/tenccvt.hxx>
38 class ImpSdrObjTextLink
: public ::sfx2::SvBaseLink
43 explicit ImpSdrObjTextLink( SdrTextObj
* pObj1
)
44 : ::sfx2::SvBaseLink( ::SfxLinkUpdateMode::ONCALL
, SotClipboardFormatId::SIMPLE_FILE
),
48 virtual void Closed() override
;
49 virtual ::sfx2::SvBaseLink::UpdateResult
DataChanged(
50 const OUString
& rMimeType
, const css::uno::Any
& rValue
) override
;
53 void ImpSdrObjTextLink::Closed()
57 // set pLink of the object to NULL, because we are destroying the link instance now
58 ImpSdrObjTextLinkUserData
* pData
=pSdrObj
->GetLinkUserData();
59 if (pData
!=nullptr) pData
->mpLink
= nullptr;
60 pSdrObj
->ReleaseTextLink();
66 ::sfx2::SvBaseLink::UpdateResult
ImpSdrObjTextLink::DataChanged(
67 const OUString
& /*rMimeType*/, const css::uno::Any
& /*rValue */)
69 bool bForceReload
= false;
70 SdrModel
* pModel(pSdrObj
? &pSdrObj
->getSdrModelFromSdrObject() : nullptr);
71 sfx2::LinkManager
* pLinkManager(pModel
? pModel
->GetLinkManager() : nullptr);
75 ImpSdrObjTextLinkUserData
* pData
=pSdrObj
->GetLinkUserData();
80 sfx2::LinkManager::GetDisplayNames( this, nullptr,&aFile
, nullptr, &aFilter
);
82 if( pData
->maFileName
!= aFile
||
83 pData
->maFilterName
!= aFilter
)
85 pData
->maFileName
= aFile
;
86 pData
->maFilterName
= aFilter
;
87 pSdrObj
->SetChanged();
93 pSdrObj
->ReloadLinkedText( bForceReload
);
99 ImpSdrObjTextLinkUserData::ImpSdrObjTextLinkUserData():
100 SdrObjUserData(SdrInventor::Default
,SDRUSERDATA_OBJTEXTLINK
),
101 maFileDate0( DateTime::EMPTY
),
102 meCharSet(RTL_TEXTENCODING_DONTKNOW
)
106 ImpSdrObjTextLinkUserData::~ImpSdrObjTextLinkUserData()
110 std::unique_ptr
<SdrObjUserData
> ImpSdrObjTextLinkUserData::Clone(SdrObject
* ) const
112 ImpSdrObjTextLinkUserData
* pData
= new ImpSdrObjTextLinkUserData
;
113 pData
->maFileName
= maFileName
;
114 pData
->maFilterName
= maFilterName
;
115 pData
->maFileDate0
= maFileDate0
;
116 pData
->meCharSet
= meCharSet
;
117 pData
->mpLink
= nullptr;
118 return std::unique_ptr
<SdrObjUserData
>(pData
);
122 void SdrTextObj::SetTextLink(const OUString
& rFileName
, const OUString
& rFilterName
)
124 rtl_TextEncoding eCharSet
= osl_getThreadTextEncoding();
126 ImpSdrObjTextLinkUserData
* pData
=GetLinkUserData();
127 if (pData
!=nullptr) {
130 pData
=new ImpSdrObjTextLinkUserData
;
131 pData
->maFileName
= rFileName
;
132 pData
->maFilterName
= rFilterName
;
133 pData
->meCharSet
= eCharSet
;
134 AppendUserData(std::unique_ptr
<SdrObjUserData
>(pData
));
138 void SdrTextObj::ReleaseTextLink()
141 sal_uInt16 nCount
=GetUserDataCount();
142 for (sal_uInt16 nNum
=nCount
; nNum
>0;) {
144 SdrObjUserData
* pData
=GetUserData(nNum
);
145 if (pData
->GetInventor()==SdrInventor::Default
&& pData
->GetId()==SDRUSERDATA_OBJTEXTLINK
) {
146 DeleteUserData(nNum
);
151 bool SdrTextObj::ReloadLinkedText( bool bForceLoad
)
153 ImpSdrObjTextLinkUserData
* pData
= GetLinkUserData();
158 DateTime
aFileDT( DateTime::EMPTY
);
163 INetURLObject
aURL( pData
->maFileName
);
164 DBG_ASSERT( aURL
.GetProtocol() != INetProtocol::NotValid
, "invalid URL" );
166 ::ucbhelper::Content
aCnt( aURL
.GetMainURL( INetURLObject::DecodeMechanism::NONE
), css::uno::Reference
< css::ucb::XCommandEnvironment
>(), comphelper::getProcessComponentContext() );
167 css::uno::Any
aAny( aCnt
.getPropertyValue("DateModified") );
168 css::util::DateTime aDateTime
;
171 ::utl::typeConvert( aDateTime
, aFileDT
);
184 bLoad
= ( aFileDT
> pData
->maFileDate0
);
188 bRet
= LoadText( pData
->maFileName
, pData
->meCharSet
);
191 pData
->maFileDate0
= aFileDT
;
198 bool SdrTextObj::LoadText(const OUString
& rFileName
, rtl_TextEncoding eCharSet
)
200 INetURLObject
aFileURL( rFileName
);
203 if( aFileURL
.GetProtocol() == INetProtocol::NotValid
)
205 OUString aFileURLStr
;
207 if( osl::FileBase::getFileURLFromSystemPath( rFileName
, aFileURLStr
) == osl::FileBase::E_None
)
208 aFileURL
= INetURLObject( aFileURLStr
);
210 aFileURL
.SetSmartURL( rFileName
);
213 DBG_ASSERT( aFileURL
.GetProtocol() != INetProtocol::NotValid
, "invalid URL" );
215 std::unique_ptr
<SvStream
> pIStm(::utl::UcbStreamHelper::CreateStream( aFileURL
.GetMainURL( INetURLObject::DecodeMechanism::NONE
), StreamMode::READ
));
219 pIStm
->SetStreamCharSet(GetSOLoadTextEncoding(eCharSet
));
223 pIStm
->ReadBytes(cRTF
, 5);
225 bool bRTF
= cRTF
[0] == '{' && cRTF
[1] == '\\' && cRTF
[2] == 'r' && cRTF
[3] == 't' && cRTF
[4] == 'f';
229 if( !pIStm
->GetError() )
231 SetText( *pIStm
, aFileURL
.GetMainURL( INetURLObject::DecodeMechanism::NONE
), bRTF
? EETextFormat::Rtf
: EETextFormat::Text
);
239 ImpSdrObjTextLinkUserData
* SdrTextObj::GetLinkUserData() const
241 sal_uInt16 nCount
=GetUserDataCount();
242 for (sal_uInt16 nNum
=nCount
; nNum
>0;) {
244 SdrObjUserData
* pData
=GetUserData(nNum
);
245 if (pData
->GetInventor() == SdrInventor::Default
246 && pData
->GetId() == SDRUSERDATA_OBJTEXTLINK
)
248 return static_cast<ImpSdrObjTextLinkUserData
*>(pData
);
254 void SdrTextObj::ImpRegisterLink()
256 ImpSdrObjTextLinkUserData
* pData
=GetLinkUserData();
257 sfx2::LinkManager
* pLinkManager(getSdrModelFromSdrObject().GetLinkManager());
258 if (pLinkManager
!=nullptr && pData
!=nullptr && pData
->mpLink
==nullptr) { // don't register twice
259 pData
->mpLink
= new ImpSdrObjTextLink(this);
260 pLinkManager
->InsertFileLink(*pData
->mpLink
,sfx2::SvBaseLinkObjectType::ClientFile
,pData
->maFileName
,
261 !pData
->maFilterName
.isEmpty() ?
262 &pData
->maFilterName
: nullptr);
266 void SdrTextObj::ImpDeregisterLink()
268 ImpSdrObjTextLinkUserData
* pData
=GetLinkUserData();
271 sfx2::LinkManager
* pLinkManager(getSdrModelFromSdrObject().GetLinkManager());
272 if (pLinkManager
!=nullptr && pData
->mpLink
!=nullptr) { // don't register twice
273 // when doing Remove, *pLink is deleted implicitly
274 pLinkManager
->Remove( pData
->mpLink
.get() );
275 pData
->mpLink
=nullptr;
279 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */