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