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 
42 public:
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->pLink=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->aFileName != aFile ||
83  pData->aFilterName != aFilter )
84  {
85  pData->aFileName = aFile;
86  pData->aFilterName = aFilter;
88  bForceReload = true;
89  }
90  }
91  }
92  if (pSdrObj )
93  pSdrObj->ReloadLinkedText( bForceReload );
94 
95  return SUCCESS;
96 }
97 
98 
101  aFileDate0( DateTime::EMPTY ),
102  eCharSet(RTL_TEXTENCODING_DONTKNOW)
103 {
104 }
105 
107 {
108 }
109 
110 std::unique_ptr<SdrObjUserData> ImpSdrObjTextLinkUserData::Clone(SdrObject* ) const
111 {
113  pData->aFileName =aFileName;
114  pData->aFilterName=aFilterName;
115  pData->aFileDate0 =aFileDate0;
116  pData->eCharSet =eCharSet;
117  pData->pLink=nullptr;
118  return std::unique_ptr<SdrObjUserData>(pData);
119 }
120 
121 
122 void SdrTextObj::SetTextLink(const OUString& rFileName, const OUString& rFilterName)
123 {
124  rtl_TextEncoding eCharSet = osl_getThreadTextEncoding();
125 
127  if (pData!=nullptr) {
128  ReleaseTextLink();
129  }
130  pData=new ImpSdrObjTextLinkUserData;
131  pData->aFileName=rFileName;
132  pData->aFilterName=rFilterName;
133  pData->eCharSet=eCharSet;
134  AppendUserData(std::unique_ptr<SdrObjUserData>(pData));
135  ImpRegisterLink();
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 
151 bool 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->aFileName );
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->aFileDate0 );
185 
186  if( bLoad )
187  {
188  bRet = LoadText( pData->aFileName, pData->eCharSet );
189  }
190 
191  pData->aFileDate0 = aFileDT;
192  }
193  }
194 
195  return bRet;
196 }
197 
198 bool 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->pLink==nullptr) { // don't register twice
259  pData->pLink = new ImpSdrObjTextLink(this);
261  !pData->aFilterName.isEmpty() ?
262  &pData->aFilterName : nullptr);
263  }
264 }
265 
267 {
269  sfx2::LinkManager* pLinkManager(getSdrModelFromSdrObject().GetLinkManager());
270  if (pLinkManager!=nullptr && pData!=nullptr && pData->pLink!=nullptr) { // don't register twice
271  // when doing Remove, *pLink is deleted implicitly
272  pLinkManager->Remove( pData->pLink.get() );
273  pData->pLink=nullptr;
274  }
275 }
276 
277 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
User data of a drawing object, e.g.
Definition: svdobj.hxx:204
URL aURL
bool LoadText(const OUString &rFileName, rtl_TextEncoding eCharSet)
Definition: svdotxln.cxx:198
std::unique_ptr< ContentProperties > pData
void ReleaseTextLink()
Definition: svdotxln.cxx:138
SdrInventor
Definition: svdobj.hxx:152
SVX_DLLPRIVATE void ImpDeregisterLink()
Definition: svdotxln.cxx:266
#define SDRUSERDATA_OBJTEXTLINK
Definition: svdotext.hxx:88
static std::unique_ptr< SvStream > CreateStream(const OUString &rFileName, StreamMode eOpenMode, css::uno::Reference< css::awt::XWindow > xParentWin=nullptr)
void SetTextLink(const OUString &rFileName, const OUString &rFilterName)
Definition: svdotxln.cxx:122
int nCount
SotClipboardFormatId
void InsertFileLink(sfx2::SvBaseLink &, SvBaseLinkObjectType nFileType, const OUString &rFileNm, const OUString *pFilterNm=nullptr, const OUString *pRange=nullptr)
SVX_DLLPRIVATE ImpSdrObjTextLinkUserData * GetLinkUserData() const
Definition: svdotxln.cxx:239
T * get() const
#define DBG_ASSERT(sCon, aError)
void Remove(SvBaseLink const *pLink)
friend class ImpSdrObjTextLinkUserData
Definition: svdotext.hxx:168
void SetText(const OUString &rStr)
Definition: svdotext.cxx:204
void DeleteUserData(sal_uInt16 nNum)
Definition: svdobj.cxx:2635
SdrModel & getSdrModelFromSdrObject() const
Definition: svdobj.cxx:273
tools::SvRef< ImpSdrObjTextLink > pLink
Definition: svdotext.hxx:114
rtl_TextEncoding GetSOLoadTextEncoding(rtl_TextEncoding eEncoding)
Abstract DrawObject.
Definition: svdobj.hxx:312
SVX_DLLPRIVATE void ImpRegisterLink()
Definition: svdotxln.cxx:254
void AppendUserData(std::unique_ptr< SdrObjUserData > pData)
Definition: svdobj.cxx:2620
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
static bool GetDisplayNames(const SvBaseLink *, OUString *pType, OUString *pFile=nullptr, OUString *pLink=nullptr, OUString *pFilter=nullptr)
rtl_TextEncoding eCharSet
Definition: svdotext.hxx:115
bool ReloadLinkedText(bool bForceLoad)
Definition: svdotxln.cxx:151
sal_uInt16 GetId() const
Definition: svdobj.hxx:220
INetProtocol GetProtocol() const
SdrObjUserData * GetUserData(sal_uInt16 nNum) const
Definition: svdobj.cxx:2614
virtual ~ImpSdrObjTextLinkUserData() override
Definition: svdotxln.cxx:106
friend class ImpSdrObjTextLink
Definition: svdotext.hxx:167
Reference< XComponentContext > getProcessComponentContext()
sal_uInt16 GetUserDataCount() const
Definition: svdobj.cxx:2608
virtual std::unique_ptr< SdrObjUserData > Clone(SdrObject *pObj1) const override
Definition: svdotxln.cxx:110
virtual void SetChanged()
Definition: svdobj.cxx:929
SdrInventor GetInventor() const
Definition: svdobj.hxx:219
SfxLinkUpdateMode
bool SetSmartURL(OUString const &rTheAbsURIRef, EncodeMechanism eMechanism=EncodeMechanism::WasEncoded, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8, FSysStyle eStyle=FSysStyle::Detect)