LibreOffice Module svx (master) 1
svdotxln.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
23#include <osl/file.hxx>
24#include <osl/thread.h>
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>
37
39{
41
42public:
43 explicit ImpSdrObjTextLink( SdrTextObj* pObj1 )
45 pSdrObj( pObj1 )
46 {}
47
48 virtual void Closed() override;
49 virtual ::sfx2::SvBaseLink::UpdateResult DataChanged(
50 const OUString& rMimeType, const css::uno::Any & rValue ) override;
51};
52
54{
55 if (pSdrObj )
56 {
57 // set pLink of the object to NULL, because we are destroying the link instance now
59 if (pData!=nullptr) pData->mpLink = nullptr;
61 }
62 SvBaseLink::Closed();
63}
64
65
67 const OUString& /*rMimeType*/, const css::uno::Any & /*rValue */)
68{
69 bool bForceReload = false;
70 SdrModel* pModel(pSdrObj ? &pSdrObj->getSdrModelFromSdrObject() : nullptr);
71 sfx2::LinkManager* pLinkManager(pModel ? pModel->GetLinkManager() : nullptr);
72
73 if( pLinkManager )
74 {
76 if( pData )
77 {
78 OUString aFile;
79 OUString aFilter;
80 sfx2::LinkManager::GetDisplayNames( this, nullptr,&aFile, nullptr, &aFilter );
81
82 if( pData->maFileName != aFile ||
83 pData->maFilterName != aFilter )
84 {
85 pData->maFileName = aFile;
86 pData->maFilterName = aFilter;
88 bForceReload = true;
89 }
90 }
91 }
92 if (pSdrObj )
93 pSdrObj->ReloadLinkedText( bForceReload );
94
95 return SUCCESS;
96}
97
98
101 maFileDate0( DateTime::EMPTY ),
102 meCharSet(RTL_TEXTENCODING_DONTKNOW)
103{
104}
105
107{
108}
109
110std::unique_ptr<SdrObjUserData> ImpSdrObjTextLinkUserData::Clone(SdrObject* ) const
111{
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);
119}
120
121
122void SdrTextObj::SetTextLink(const OUString& rFileName, const OUString& rFilterName)
123{
124 rtl_TextEncoding eCharSet = osl_getThreadTextEncoding();
125
127 if (pData!=nullptr) {
129 }
131 pData->maFileName = rFileName;
132 pData->maFilterName = rFilterName;
133 pData->meCharSet = eCharSet;
134 AppendUserData(std::unique_ptr<SdrObjUserData>(pData));
136}
137
139{
141 sal_uInt16 nCount=GetUserDataCount();
142 for (sal_uInt16 nNum=nCount; nNum>0;) {
143 nNum--;
145 if (pData->GetInventor()==SdrInventor::Default && pData->GetId()==SDRUSERDATA_OBJTEXTLINK) {
146 DeleteUserData(nNum);
147 }
148 }
149}
150
151bool SdrTextObj::ReloadLinkedText( bool bForceLoad)
152{
154 bool bRet = true;
155
156 if( pData )
157 {
158 DateTime aFileDT( DateTime::EMPTY );
159 bool bExists = true;
160
161 try
162 {
163 INetURLObject aURL( pData->maFileName );
164 DBG_ASSERT( aURL.GetProtocol() != INetProtocol::NotValid, "invalid URL" );
165
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;
169
170 aAny >>= aDateTime;
171 ::utl::typeConvert( aDateTime, aFileDT );
172 }
173 catch( ... )
174 {
175 bExists = false;
176 }
177
178 if( bExists )
179 {
180 bool bLoad = false;
181 if( bForceLoad )
182 bLoad = true;
183 else
184 bLoad = ( aFileDT > pData->maFileDate0 );
185
186 if( bLoad )
187 {
188 bRet = LoadText( pData->maFileName, pData->meCharSet );
189 }
190
191 pData->maFileDate0 = aFileDT;
192 }
193 }
194
195 return bRet;
196}
197
198bool SdrTextObj::LoadText(const OUString& rFileName, rtl_TextEncoding eCharSet)
199{
200 INetURLObject aFileURL( rFileName );
201 bool bRet = false;
202
203 if( aFileURL.GetProtocol() == INetProtocol::NotValid )
204 {
205 OUString aFileURLStr;
206
207 if( osl::FileBase::getFileURLFromSystemPath( rFileName, aFileURLStr ) == osl::FileBase::E_None )
208 aFileURL = INetURLObject( aFileURLStr );
209 else
210 aFileURL.SetSmartURL( rFileName );
211 }
212
213 DBG_ASSERT( aFileURL.GetProtocol() != INetProtocol::NotValid, "invalid URL" );
214
215 std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream( aFileURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), StreamMode::READ ));
216
217 if( pIStm )
218 {
219 pIStm->SetStreamCharSet(GetSOLoadTextEncoding(eCharSet));
220
221 char cRTF[5];
222 cRTF[4] = 0;
223 pIStm->ReadBytes(cRTF, 5);
224
225 bool bRTF = cRTF[0] == '{' && cRTF[1] == '\\' && cRTF[2] == 'r' && cRTF[3] == 't' && cRTF[4] == 'f';
226
227 pIStm->Seek(0);
228
229 if( !pIStm->GetError() )
230 {
231 SetText( *pIStm, aFileURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), bRTF ? EETextFormat::Rtf : EETextFormat::Text );
232 bRet = true;
233 }
234 }
235
236 return bRet;
237}
238
240{
241 sal_uInt16 nCount=GetUserDataCount();
242 for (sal_uInt16 nNum=nCount; nNum>0;) {
243 nNum--;
245 if (pData->GetInventor() == SdrInventor::Default
246 && pData->GetId() == SDRUSERDATA_OBJTEXTLINK)
247 {
248 return static_cast<ImpSdrObjTextLinkUserData *>(pData);
249 }
250 }
251 return nullptr;
252}
253
255{
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);
263 }
264}
265
267{
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;
276 }
277}
278
279/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
bool SetSmartURL(std::u16string_view rTheAbsURIRef, EncodeMechanism eMechanism=EncodeMechanism::WasEncoded, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8, FSysStyle eStyle=FSysStyle::Detect)
INetProtocol GetProtocol() const
virtual std::unique_ptr< SdrObjUserData > Clone(SdrObject *pObj1) const override
Definition: svdotxln.cxx:110
rtl_TextEncoding meCharSet
Definition: svdotext.hxx:106
virtual ~ImpSdrObjTextLinkUserData() override
Definition: svdotxln.cxx:106
sfx2::LinkManager * GetLinkManager()
Definition: svdmodel.hxx:352
User data of a drawing object, e.g.
Definition: svdobj.hxx:152
Abstract DrawObject.
Definition: svdobj.hxx:260
void AppendUserData(std::unique_ptr< SdrObjUserData > pData)
Definition: svdobj.cxx:2729
sal_uInt16 GetUserDataCount() const
Definition: svdobj.cxx:2717
SdrModel & getSdrModelFromSdrObject() const
Definition: svdobj.cxx:289
SdrObjUserData * GetUserData(sal_uInt16 nNum) const
Definition: svdobj.cxx:2723
virtual void SetChanged()
Definition: svdobj.cxx:1042
void DeleteUserData(sal_uInt16 nNum)
Definition: svdobj.cxx:2744
SVX_DLLPRIVATE ImpSdrObjTextLinkUserData * GetLinkUserData() const
Definition: svdotxln.cxx:239
bool LoadText(const OUString &rFileName, rtl_TextEncoding eCharSet)
Definition: svdotxln.cxx:198
SVX_DLLPRIVATE void ImpDeregisterLink()
Definition: svdotxln.cxx:266
bool ReloadLinkedText(bool bForceLoad)
Definition: svdotxln.cxx:151
SVX_DLLPRIVATE void ImpRegisterLink()
Definition: svdotxln.cxx:254
friend class ImpSdrObjTextLinkUserData
Definition: svdotext.hxx:154
void SetText(const OUString &rStr)
Definition: svdotext.cxx:241
friend class ImpSdrObjTextLink
Definition: svdotext.hxx:153
void SetTextLink(const OUString &rFileName, const OUString &rFilterName)
Definition: svdotxln.cxx:122
void ReleaseTextLink()
Definition: svdotxln.cxx:138
void InsertFileLink(sfx2::SvBaseLink &, SvBaseLinkObjectType nFileType, std::u16string_view rFileNm, const OUString *pFilterNm=nullptr, const OUString *pRange=nullptr)
static bool GetDisplayNames(const SvBaseLink *, OUString *pType, OUString *pFile=nullptr, OUString *pLink=nullptr, OUString *pFilter=nullptr)
void Remove(SvBaseLink const *pLink)
css::uno::Any getPropertyValue(const OUString &rPropertyName)
static std::unique_ptr< SvStream > CreateStream(const OUString &rFileName, StreamMode eOpenMode, css::uno::Reference< css::awt::XWindow > xParentWin=nullptr)
int nCount
#define DBG_ASSERT(sCon, aError)
URL aURL
SotClipboardFormatId
SfxLinkUpdateMode
std::unique_ptr< sal_Int32[]> pData
Reference< XComponentContext > getProcessComponentContext()
constexpr OUStringLiteral EMPTY
SdrInventor
Definition: svdobj.hxx:98
constexpr const sal_uInt16 SDRUSERDATA_OBJTEXTLINK
Definition: svdotext.hxx:84
TOOLS_DLLPUBLIC rtl_TextEncoding GetSOLoadTextEncoding(rtl_TextEncoding eEncoding)