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