LibreOffice Module sw (master)  1
ddefld.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 
22 #include <o3tl/any.hxx>
23 #include <osl/diagnose.h>
24 #include <osl/thread.h>
25 #include <rtl/ustrbuf.hxx>
26 #include <sfx2/linkmgr.hxx>
27 #include <sot/exchange.hxx>
28 #include <doc.hxx>
30 #include <IDocumentState.hxx>
32 #include <editsh.hxx>
33 #include <fmtfld.hxx>
34 #include <ddefld.hxx>
35 #include <swbaslnk.hxx>
36 #include <unofldmid.h>
37 #include <hints.hxx>
38 
39 using namespace ::com::sun::star;
40 
41 namespace {
42 
43 class SwIntrnlRefLink : public SwBaseLink
44 {
45  SwDDEFieldType& m_rFieldType;
46 
47 public:
48  SwIntrnlRefLink(SwDDEFieldType& rType, SfxLinkUpdateMode nUpdateType)
49  : SwBaseLink(nUpdateType, SotClipboardFormatId::STRING)
50  , m_rFieldType(rType)
51  {}
52 
53  virtual void Closed() override;
54  virtual ::sfx2::SvBaseLink::UpdateResult DataChanged(
55  const OUString& rMimeType, const css::uno::Any & rValue ) override;
56 
57  virtual const SwNode* GetAnchor() const override;
58  virtual bool IsInRange( sal_uLong nSttNd, sal_uLong nEndNd ) const override;
59 };
60 
61 }
62 
63 ::sfx2::SvBaseLink::UpdateResult SwIntrnlRefLink::DataChanged( const OUString& rMimeType,
64  const uno::Any & rValue )
65 {
66  switch( SotExchange::GetFormatIdFromMimeType( rMimeType ) )
67  {
68  case SotClipboardFormatId::STRING:
69  if( !IsNoDataFlag() )
70  {
71  uno::Sequence< sal_Int8 > aSeq;
72  rValue >>= aSeq;
73  OUString sStr( reinterpret_cast<char const *>(aSeq.getConstArray()), aSeq.getLength(), osl_getThreadTextEncoding() );
74 
75  // remove not needed CR-LF at the end
76  sal_Int32 n = sStr.getLength();
77  while( n && 0 == sStr[ n-1 ] )
78  --n;
79  if( n && 0x0a == sStr[ n-1 ] )
80  --n;
81  if( n && 0x0d == sStr[ n-1 ] )
82  --n;
83 
84  bool bDel = n != sStr.getLength();
85  if( bDel )
86  sStr = sStr.copy( 0, n );
87 
88  m_rFieldType.SetExpansion(sStr);
89  // set Expansion first! (otherwise this flag will be deleted)
90  m_rFieldType.SetCRLFDelFlag(bDel);
91  }
92  break;
93 
94  // other formats
95  default:
96  return SUCCESS;
97  }
98 
99  OSL_ENSURE(m_rFieldType.GetDoc(), "no pDoc");
100 
101  // no dependencies left?
102  if (m_rFieldType.HasWriterListeners() && !m_rFieldType.IsModifyLocked() && !ChkNoDataFlag())
103  {
105  SwEditShell* pESh = m_rFieldType.GetDoc()->GetEditShell();
106 
107  // Search for fields. If no valid found, disconnect.
108  SwMsgPoolItem aUpdateDDE( RES_UPDATEDDETBL );
109  m_rFieldType.LockModify();
110 
111  std::vector<SwFormatField*> vFields;
112  m_rFieldType.GatherFields(vFields, false);
113  if(vFields.size())
114  {
115  if(pESh)
116  pESh->StartAllAction();
117  else if(pSh)
118  pSh->StartAction();
119  }
120 
121  for(auto pFormatField: vFields)
122  {
123  // a DDE table or a DDE field attribute in the text
124  if(pFormatField->GetTextField())
125  pFormatField->UpdateTextNode( nullptr, &aUpdateDDE );
126  }
127 
128  m_rFieldType.UnlockModify();
129 
130  if(vFields.size())
131  {
132  if(pESh)
133  pESh->EndAllAction();
134  else if(pSh)
135  pSh->EndAction();
136 
137  if(pSh)
139  }
140  }
141 
142  return SUCCESS;
143 }
144 
145 void SwIntrnlRefLink::Closed()
146 {
147  if (m_rFieldType.GetDoc() && !m_rFieldType.GetDoc()->IsInDtor())
148  {
149  // advise goes, convert all fields into text?
151  SwEditShell* pESh = m_rFieldType.GetDoc()->GetEditShell();
152  if( pESh )
153  {
154  pESh->StartAllAction();
155  pESh->FieldToText(&m_rFieldType);
156  pESh->EndAllAction();
157  }
158  else
159  {
160  pSh->StartAction();
161  // to call at the doc ??
162  pSh->EndAction();
163  }
164  }
165  SvBaseLink::Closed();
166 }
167 
169 
170 const SwNode* SwIntrnlRefLink::GetAnchor() const
171 {
172  // here, any anchor of the normal NodesArray should be sufficient
173  const SwNode* pNd = nullptr;
174  m_rFieldType.CallSwClientNotify(
175  sw::LinkAnchorSearchHint(m_rFieldType.GetDoc()->GetNodes(), pNd));
176  return pNd;
177 }
178 
179 bool SwIntrnlRefLink::IsInRange( sal_uLong nSttNd, sal_uLong nEndNd ) const
180 {
181  bool bInRange = false;
182  m_rFieldType.CallSwClientNotify(sw::InRangeSearchHint(nSttNd, nEndNd, bInRange));
183  return bInRange;
184 }
185 
186 SwDDEFieldType::SwDDEFieldType(const OUString& rName,
187  const OUString& rCmd, SfxLinkUpdateMode nUpdateType )
188  : SwFieldType( SwFieldIds::Dde ),
189  m_aName( rName ), m_pDoc( nullptr ), m_nRefCount( 0 )
190 {
191  m_bCRLFFlag = m_bDeleted = false;
192  m_RefLink = new SwIntrnlRefLink( *this, nUpdateType );
193  SetCmd( rCmd );
194 }
195 
197 {
198  if( m_pDoc && !m_pDoc->IsInDtor() )
201 }
202 
203 std::unique_ptr<SwFieldType> SwDDEFieldType::Copy() const
204 {
205  std::unique_ptr<SwDDEFieldType> pType(new SwDDEFieldType( m_aName, GetCmd(), GetType() ));
206  pType->m_aExpansion = m_aExpansion;
207  pType->m_bCRLFFlag = m_bCRLFFlag;
208  pType->m_bDeleted = m_bDeleted;
209  pType->SetDoc( m_pDoc );
210  return pType;
211 }
212 
213 OUString SwDDEFieldType::GetName() const
214 {
215  return m_aName;
216 }
217 
218 void SwDDEFieldType::SetCmd( const OUString& _aStr )
219 {
220  OUString aStr = _aStr;
221  sal_Int32 nIndex = 0;
222  do
223  {
224  aStr = aStr.replaceFirst(" ", " ", &nIndex);
225  } while (nIndex>=0);
226  m_RefLink->SetLinkSourceName( aStr );
227 }
228 
229 OUString const & SwDDEFieldType::GetCmd() const
230 {
231  return m_RefLink->GetLinkSourceName();
232 }
233 
235 {
236  if( pNewDoc == m_pDoc )
237  return;
238 
239  if( m_pDoc && m_RefLink.is() )
240  {
241  OSL_ENSURE( !m_nRefCount, "How do we get the references?" );
243  }
244 
245  m_pDoc = pNewDoc;
246  if( m_pDoc && m_nRefCount )
247  {
250  }
251 }
252 
254 {
255  if( m_nRefCount )
256  {
260  m_RefLink->Update();
261  }
262  else
263  {
264  Disconnect();
266  }
267 }
268 
269 void SwDDEFieldType::QueryValue( uno::Any& rVal, sal_uInt16 nWhichId ) const
270 {
271  sal_Int32 nPart = -1;
272  switch( nWhichId )
273  {
274  case FIELD_PROP_PAR2: nPart = 2; break;
275  case FIELD_PROP_PAR4: nPart = 1; break;
276  case FIELD_PROP_SUBTYPE: nPart = 0; break;
277  case FIELD_PROP_BOOL1:
278  rVal <<= GetType() == SfxLinkUpdateMode::ALWAYS;
279  break;
280  case FIELD_PROP_PAR5:
281  rVal <<= m_aExpansion;
282  break;
283  default:
284  assert(false);
285  }
286  if ( nPart>=0 )
287  rVal <<= GetCmd().getToken(nPart, sfx2::cTokenSeparator);
288 }
289 
290 void SwDDEFieldType::PutValue( const uno::Any& rVal, sal_uInt16 nWhichId )
291 {
292  sal_Int32 nPart = -1;
293  switch( nWhichId )
294  {
295  case FIELD_PROP_PAR2: nPart = 2; break;
296  case FIELD_PROP_PAR4: nPart = 1; break;
297  case FIELD_PROP_SUBTYPE: nPart = 0; break;
298  case FIELD_PROP_BOOL1:
299  SetType( *o3tl::doAccess<bool>(rVal) ?
300  SfxLinkUpdateMode::ALWAYS :
301  SfxLinkUpdateMode::ONCALL );
302  break;
303  case FIELD_PROP_PAR5:
304  rVal >>= m_aExpansion;
305  break;
306  default:
307  assert(false);
308  }
309  if( nPart<0 )
310  return;
311 
312  const OUString sOldCmd( GetCmd() );
313  OUStringBuffer sNewCmd;
314  sal_Int32 nIndex = 0;
315  for (sal_Int32 i=0; i<3; ++i)
316  {
317  OUString sToken = sOldCmd.getToken(0, sfx2::cTokenSeparator, nIndex);
318  if (i==nPart)
319  {
320  rVal >>= sToken;
321  }
322  sNewCmd.append((i < 2)
323  ? sToken + OUStringChar(sfx2::cTokenSeparator) : sToken);
324  }
325  SetCmd( sNewCmd.makeStringAndClear() );
326 }
327 
329  : SwField(pInitType)
330 {
331 }
332 
334 {
335  if( GetTyp()->HasOnlyOneListener() )
336  static_cast<SwDDEFieldType*>(GetTyp())->Disconnect();
337 }
338 
339 OUString SwDDEField::ExpandImpl(SwRootFrame const*const) const
340 {
341  OUString aStr = static_cast<SwDDEFieldType*>(GetTyp())->GetExpansion();
342  aStr = aStr.replaceAll("\r", "");
343  aStr = aStr.replaceAll("\t", " ");
344  aStr = aStr.replaceAll("\n", "|");
345  if (aStr.endsWith("|"))
346  {
347  return aStr.copy(0, aStr.getLength()-1);
348  }
349  return aStr;
350 }
351 
352 std::unique_ptr<SwField> SwDDEField::Copy() const
353 {
354  return std::make_unique<SwDDEField>(static_cast<SwDDEFieldType*>(GetTyp()));
355 }
356 
358 OUString SwDDEField::GetPar1() const
359 {
360  return static_cast<const SwDDEFieldType*>(GetTyp())->GetName();
361 }
362 
364 OUString SwDDEField::GetPar2() const
365 {
366  return static_cast<const SwDDEFieldType*>(GetTyp())->GetCmd();
367 }
368 
370 void SwDDEField::SetPar2(const OUString& rStr)
371 {
372  static_cast<SwDDEFieldType*>(GetTyp())->SetCmd(rStr);
373 }
374 
375 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString m_aName
Definition: ddefld.hxx:53
Instances of SwFields and those derived from it occur 0 to n times.
Definition: fldbas.hxx:240
bool is() const
virtual void SetPar2(const OUString &rStr) override
set field type command
Definition: ddefld.cxx:370
STRING
sal_Int32 nIndex
void SetCmd(const OUString &aStr)
Definition: ddefld.cxx:218
SwDDEFieldType(const OUString &rName, const OUString &rCmd, SfxLinkUpdateMode)
Definition: ddefld.cxx:186
bool IsInRange(const WhichRangesContainer &pRange, const sal_uInt16 nId)
check if ID is in range of attribute set IDs
Definition: swatrset.cxx:434
OUString m_aName
#define FIELD_PROP_PAR5
Definition: unofldmid.h:43
virtual ~SwDDEField() override
Definition: ddefld.cxx:333
constexpr TypedWhichId< SwMsgPoolItem > RES_UPDATEDDETBL(171)
virtual void SetModified()=0
Must be called manually at changes of format.
sal_uIntPtr sal_uLong
const sal_Unicode cTokenSeparator
void SetType(SfxLinkUpdateMode nType)
Definition: ddefld.hxx:84
Base class of all fields.
Definition: fldbas.hxx:289
sal_Int64 n
SfxLinkUpdateMode GetType() const
Definition: ddefld.hxx:83
Definition: doc.hxx:188
void Disconnect()
Definition: ddefld.hxx:89
IDocumentLinksAdministration const & getIDocumentLinksAdministration() const
Definition: doc.cxx:260
SwDoc * m_pDoc
Definition: ddefld.hxx:57
virtual ~LinkAnchorSearchHint() override
Definition: ddefld.cxx:168
void EndAllAction()
Definition: edws.cxx:97
The root element of a Writer document layout.
Definition: rootfrm.hxx:82
#define FIELD_PROP_SUBTYPE
Definition: unofldmid.h:27
OUString const & GetCmd() const
Definition: ddefld.cxx:229
virtual OUString ExpandImpl(SwRootFrame const *pLayout) const override
Definition: ddefld.cxx:339
SAL_DLLPRIVATE void RefCntChgd()
Definition: ddefld.cxx:253
void SetDoc(SwDoc *pDoc)
Definition: ddefld.cxx:234
virtual void PutValue(const css::uno::Any &rVal, sal_uInt16 nWhich) override
Definition: ddefld.cxx:290
SwDoc * GetDoc() const
Definition: viewsh.hxx:281
SotClipboardFormatId
virtual std::unique_ptr< SwFieldType > Copy() const override
Definition: ddefld.cxx:203
T * get() const
tools::SvRef< sfx2::SvBaseLink > m_RefLink
Definition: ddefld.hxx:56
int i
void Remove(SvBaseLink const *pLink)
SwEditShell const * GetEditShell() const
Definition: doccorr.cxx:328
virtual OUString GetPar1() const override
Get parameter via types.
Definition: ddefld.cxx:358
SwFieldIds
Definition: fldbas.hxx:44
virtual OUString GetPar2() const override
get field type command
Definition: ddefld.cxx:364
void StartAction()
Definition: viewsh.hxx:590
bool m_bCRLFFlag
Definition: ddefld.hxx:60
IDocumentState const & getIDocumentState() const
Definition: doc.cxx:394
SwDDEField(SwDDEFieldType *)
Definition: ddefld.cxx:328
IDocumentLayoutAccess const & getIDocumentLayoutAccess() const
Definition: doc.cxx:405
SwFieldType * GetTyp() const
Definition: fldbas.hxx:392
void EndAction(const bool bIdleEnd=false)
Definition: viewsh.hxx:595
#define FIELD_PROP_BOOL1
Definition: unofldmid.h:28
void InsertDDELink(SvBaseLink *, const OUString &rServer, std::u16string_view rTopic, std::u16string_view rItem)
void FieldToText(SwFieldType const *pType)
Definition: edfld.cxx:124
#define FIELD_PROP_PAR2
Definition: unofldmid.h:24
virtual const SwViewShell * GetCurrentViewShell() const =0
Returns the layout set at the document.
static SotClipboardFormatId GetFormatIdFromMimeType(const OUString &rMimeType)
Sequence< sal_Int8 > aSeq
virtual sfx2::LinkManager & GetLinkManager()=0
bool IsInDtor() const
Definition: doc.hxx:404
OUString m_aExpansion
Definition: ddefld.hxx:54
#define FIELD_PROP_PAR4
Definition: unofldmid.h:36
virtual std::unique_ptr< SwField > Copy() const override
Definition: ddefld.cxx:352
virtual ~SwDDEFieldType() override
Definition: ddefld.cxx:196
void StartAllAction()
For all views of this document.
Definition: edws.cxx:86
virtual OUString GetName() const override
Only in derived classes.
Definition: ddefld.cxx:213
aStr
virtual void QueryValue(css::uno::Any &rVal, sal_uInt16 nWhich) const override
Definition: ddefld.cxx:269
bool m_bDeleted
Definition: ddefld.hxx:61
SfxLinkUpdateMode
sal_uInt16 m_nRefCount
Definition: ddefld.hxx:59
virtual bool IsVisibleLinks() const =0
Insert links in-/visibly into LinkManager (linked ranges).
Base class of the Writer document model elements.
Definition: node.hxx:80