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