Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / svdraw / svdotxln.cxx
blob44a55fdb4ea2f603e66f49feffd938ae8a1d4579
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
36 #include <memory>
38 class ImpSdrObjTextLink: public ::sfx2::SvBaseLink
40 SdrTextObj* pSdrObj;
42 public:
43 explicit ImpSdrObjTextLink( SdrTextObj* pObj1 )
44 : ::sfx2::SvBaseLink( ::SfxLinkUpdateMode::ONCALL, SotClipboardFormatId::SIMPLE_FILE ),
45 pSdrObj( pObj1 )
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()
55 if (pSdrObj )
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();
62 SvBaseLink::Closed();
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);
73 if( pLinkManager )
75 ImpSdrObjTextLinkUserData* pData=pSdrObj->GetLinkUserData();
76 if( pData )
78 OUString aFile;
79 OUString aFilter;
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();
88 bForceReload = true;
92 if (pSdrObj )
93 pSdrObj->ReloadLinkedText( bForceReload );
95 return SUCCESS;
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) {
128 ReleaseTextLink();
130 pData=new ImpSdrObjTextLinkUserData;
131 pData->maFileName = rFileName;
132 pData->maFilterName = rFilterName;
133 pData->meCharSet = eCharSet;
134 AppendUserData(std::unique_ptr<SdrObjUserData>(pData));
135 ImpRegisterLink();
138 void SdrTextObj::ReleaseTextLink()
140 ImpDeregisterLink();
141 sal_uInt16 nCount=GetUserDataCount();
142 for (sal_uInt16 nNum=nCount; nNum>0;) {
143 nNum--;
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();
154 bool bRet = true;
156 if( pData )
158 DateTime aFileDT( DateTime::EMPTY );
159 bool bExists = true;
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;
170 aAny >>= aDateTime;
171 ::utl::typeConvert( aDateTime, aFileDT );
173 catch( ... )
175 bExists = false;
178 if( bExists )
180 bool bLoad = false;
181 if( bForceLoad )
182 bLoad = true;
183 else
184 bLoad = ( aFileDT > pData->maFileDate0 );
186 if( bLoad )
188 bRet = LoadText( pData->maFileName, pData->meCharSet );
191 pData->maFileDate0 = aFileDT;
195 return bRet;
198 bool SdrTextObj::LoadText(const OUString& rFileName, rtl_TextEncoding eCharSet)
200 INetURLObject aFileURL( rFileName );
201 bool bRet = false;
203 if( aFileURL.GetProtocol() == INetProtocol::NotValid )
205 OUString aFileURLStr;
207 if( osl::FileBase::getFileURLFromSystemPath( rFileName, aFileURLStr ) == osl::FileBase::E_None )
208 aFileURL = INetURLObject( aFileURLStr );
209 else
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 ));
217 if( pIStm )
219 pIStm->SetStreamCharSet(GetSOLoadTextEncoding(eCharSet));
221 char cRTF[5];
222 cRTF[4] = 0;
223 pIStm->ReadBytes(cRTF, 5);
225 bool bRTF = cRTF[0] == '{' && cRTF[1] == '\\' && cRTF[2] == 'r' && cRTF[3] == 't' && cRTF[4] == 'f';
227 pIStm->Seek(0);
229 if( !pIStm->GetError() )
231 SetText( *pIStm, aFileURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), bRTF ? EETextFormat::Rtf : EETextFormat::Text );
232 bRet = true;
236 return bRet;
239 ImpSdrObjTextLinkUserData* SdrTextObj::GetLinkUserData() const
241 sal_uInt16 nCount=GetUserDataCount();
242 for (sal_uInt16 nNum=nCount; nNum>0;) {
243 nNum--;
244 SdrObjUserData * pData=GetUserData(nNum);
245 if (pData->GetInventor() == SdrInventor::Default
246 && pData->GetId() == SDRUSERDATA_OBJTEXTLINK)
248 return static_cast<ImpSdrObjTextLinkUserData *>(pData);
251 return nullptr;
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();
269 if (!pData)
270 return;
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: */