1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
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>
29 #include <IDocumentLinksAdministration.hxx>
30 #include <IDocumentState.hxx>
31 #include <IDocumentLayoutAccess.hxx>
35 #include <swddetbl.hxx>
36 #include <swbaslnk.hxx>
37 #include <unofldmid.h>
41 using namespace ::com::sun::star
;
43 #define DDE_TXT_ENCODING osl_getThreadTextEncoding()
47 class SwIntrnlRefLink
: public SwBaseLink
49 SwDDEFieldType
& m_rFieldType
;
52 SwIntrnlRefLink(SwDDEFieldType
& rType
, SfxLinkUpdateMode nUpdateType
)
53 : SwBaseLink(nUpdateType
, SotClipboardFormatId::STRING
)
57 virtual void Closed() override
;
58 virtual ::sfx2::SvBaseLink::UpdateResult
DataChanged(
59 const OUString
& rMimeType
, const css::uno::Any
& rValue
) override
;
61 virtual const SwNode
* GetAnchor() const override
;
62 virtual bool IsInRange( sal_uLong nSttNd
, sal_uLong nEndNd
) const override
;
67 ::sfx2::SvBaseLink::UpdateResult
SwIntrnlRefLink::DataChanged( const OUString
& rMimeType
,
68 const uno::Any
& rValue
)
70 switch( SotExchange::GetFormatIdFromMimeType( rMimeType
) )
72 case SotClipboardFormatId::STRING
:
75 uno::Sequence
< sal_Int8
> aSeq
;
77 OUString
sStr( reinterpret_cast<char const *>(aSeq
.getConstArray()), aSeq
.getLength(), DDE_TXT_ENCODING
);
79 // remove not needed CR-LF at the end
80 sal_Int32 n
= sStr
.getLength();
81 while( n
&& 0 == sStr
[ n
-1 ] )
83 if( n
&& 0x0a == sStr
[ n
-1 ] )
85 if( n
&& 0x0d == sStr
[ n
-1 ] )
88 bool bDel
= n
!= sStr
.getLength();
90 sStr
= sStr
.copy( 0, n
);
92 m_rFieldType
.SetExpansion(sStr
);
93 // set Expansion first! (otherwise this flag will be deleted)
94 m_rFieldType
.SetCRLFDelFlag(bDel
);
103 OSL_ENSURE(m_rFieldType
.GetDoc(), "no pDoc");
105 // no dependencies left?
106 if (!m_rFieldType
.IsModifyLocked() && !ChkNoDataFlag())
108 SwViewShell
* pSh
= m_rFieldType
.GetDoc()->getIDocumentLayoutAccess().GetCurrentViewShell();
109 SwEditShell
* pESh
= m_rFieldType
.GetDoc()->GetEditShell();
111 // Search for fields. If no valid found, disconnect.
112 SwMsgPoolItem
aUpdateDDE( RES_UPDATEDDETBL
);
113 m_rFieldType
.LockModify();
115 std::vector
<SwFormatField
*> vFields
;
116 std::vector
<SwDDETable
*> vTables
;
117 m_rFieldType
.GatherFields(vFields
, false);
118 m_rFieldType
.GatherDdeTables(vTables
);
119 const bool bDoAction
= vFields
.size() || vTables
.size();
123 pESh
->StartAllAction();
128 // DDE fields attribute in the text
129 for(auto pFormatField
: vFields
)
131 if(pFormatField
->GetTextField())
132 pFormatField
->UpdateTextNode( nullptr, &aUpdateDDE
);
134 // a DDE tables in the text
135 for(auto pTable
: vTables
)
136 pTable
->ChangeContent();
138 m_rFieldType
.UnlockModify();
143 pESh
->EndAllAction();
148 pSh
->GetDoc()->getIDocumentState().SetModified();
155 void SwIntrnlRefLink::Closed()
157 if (m_rFieldType
.GetDoc() && !m_rFieldType
.GetDoc()->IsInDtor())
159 // advise goes, convert all fields into text?
160 SwViewShell
* pSh
= m_rFieldType
.GetDoc()->getIDocumentLayoutAccess().GetCurrentViewShell();
161 SwEditShell
* pESh
= m_rFieldType
.GetDoc()->GetEditShell();
164 pESh
->StartAllAction();
165 pESh
->FieldToText(&m_rFieldType
);
166 pESh
->EndAllAction();
171 // to call at the doc ??
175 SvBaseLink::Closed();
178 sw::LinkAnchorSearchHint::~LinkAnchorSearchHint() {};
180 const SwNode
* SwIntrnlRefLink::GetAnchor() const
182 // here, any anchor of the normal NodesArray should be sufficient
183 const SwNode
* pNd
= nullptr;
184 m_rFieldType
.CallSwClientNotify(
185 sw::LinkAnchorSearchHint(m_rFieldType
.GetDoc()->GetNodes(), pNd
));
189 bool SwIntrnlRefLink::IsInRange( sal_uLong nSttNd
, sal_uLong nEndNd
) const
191 bool bInRange
= false;
192 m_rFieldType
.CallSwClientNotify(sw::InRangeSearchHint(nSttNd
, nEndNd
, bInRange
));
196 SwDDEFieldType::SwDDEFieldType(const OUString
& rName
,
197 const OUString
& rCmd
, SfxLinkUpdateMode nUpdateType
)
198 : SwFieldType( SwFieldIds::Dde
),
199 m_aName( rName
), m_pDoc( nullptr ), m_nRefCount( 0 )
201 m_bCRLFFlag
= m_bDeleted
= false;
202 m_RefLink
= new SwIntrnlRefLink( *this, nUpdateType
);
206 SwDDEFieldType::~SwDDEFieldType()
208 if( m_pDoc
&& !m_pDoc
->IsInDtor() )
209 m_pDoc
->getIDocumentLinksAdministration().GetLinkManager().Remove( m_RefLink
.get() );
210 m_RefLink
->Disconnect();
213 std::unique_ptr
<SwFieldType
> SwDDEFieldType::Copy() const
215 std::unique_ptr
<SwDDEFieldType
> pType(new SwDDEFieldType( m_aName
, GetCmd(), GetType() ));
216 pType
->m_aExpansion
= m_aExpansion
;
217 pType
->m_bCRLFFlag
= m_bCRLFFlag
;
218 pType
->m_bDeleted
= m_bDeleted
;
219 pType
->SetDoc( m_pDoc
);
223 OUString
SwDDEFieldType::GetName() const
228 void SwDDEFieldType::SetCmd( const OUString
& _aStr
)
230 OUString aStr
= _aStr
;
231 sal_Int32 nIndex
= 0;
234 aStr
= aStr
.replaceFirst(" ", " ", &nIndex
);
236 m_RefLink
->SetLinkSourceName( aStr
);
239 OUString
const & SwDDEFieldType::GetCmd() const
241 return m_RefLink
->GetLinkSourceName();
244 void SwDDEFieldType::SetDoc( SwDoc
* pNewDoc
)
246 if( pNewDoc
== m_pDoc
)
249 if( m_pDoc
&& m_RefLink
.is() )
251 OSL_ENSURE( !m_nRefCount
, "How do we get the references?" );
252 m_pDoc
->getIDocumentLinksAdministration().GetLinkManager().Remove( m_RefLink
.get() );
256 if( m_pDoc
&& m_nRefCount
)
258 m_RefLink
->SetVisible( m_pDoc
->getIDocumentLinksAdministration().IsVisibleLinks() );
259 m_pDoc
->getIDocumentLinksAdministration().GetLinkManager().InsertDDELink( m_RefLink
.get() );
263 void SwDDEFieldType::RefCntChgd()
267 m_RefLink
->SetVisible( m_pDoc
->getIDocumentLinksAdministration().IsVisibleLinks() );
268 m_pDoc
->getIDocumentLinksAdministration().GetLinkManager().InsertDDELink( m_RefLink
.get() );
269 if( m_pDoc
->getIDocumentLayoutAccess().GetCurrentViewShell() )
275 m_pDoc
->getIDocumentLinksAdministration().GetLinkManager().Remove( m_RefLink
.get() );
279 void SwDDEFieldType::QueryValue( uno::Any
& rVal
, sal_uInt16 nWhichId
) const
281 sal_Int32 nPart
= -1;
284 case FIELD_PROP_PAR2
: nPart
= 2; break;
285 case FIELD_PROP_PAR4
: nPart
= 1; break;
286 case FIELD_PROP_SUBTYPE
: nPart
= 0; break;
287 case FIELD_PROP_BOOL1
:
288 rVal
<<= GetType() == SfxLinkUpdateMode::ALWAYS
;
290 case FIELD_PROP_PAR5
:
291 rVal
<<= m_aExpansion
;
297 rVal
<<= GetCmd().getToken(nPart
, sfx2::cTokenSeparator
);
300 void SwDDEFieldType::PutValue( const uno::Any
& rVal
, sal_uInt16 nWhichId
)
302 sal_Int32 nPart
= -1;
305 case FIELD_PROP_PAR2
: nPart
= 2; break;
306 case FIELD_PROP_PAR4
: nPart
= 1; break;
307 case FIELD_PROP_SUBTYPE
: nPart
= 0; break;
308 case FIELD_PROP_BOOL1
:
309 SetType( *o3tl::doAccess
<bool>(rVal
) ?
310 SfxLinkUpdateMode::ALWAYS
:
311 SfxLinkUpdateMode::ONCALL
);
313 case FIELD_PROP_PAR5
:
314 rVal
>>= m_aExpansion
;
322 const OUString
sOldCmd( GetCmd() );
323 OUStringBuffer sNewCmd
;
324 sal_Int32 nIndex
= 0;
325 for (sal_Int32 i
=0; i
<3; ++i
)
327 OUString sToken
= sOldCmd
.getToken(0, sfx2::cTokenSeparator
, nIndex
);
332 sNewCmd
.append((i
< 2)
333 ? sToken
+ OUStringChar(sfx2::cTokenSeparator
) : sToken
);
335 SetCmd( sNewCmd
.makeStringAndClear() );
338 SwDDEField::SwDDEField( SwDDEFieldType
* pInitType
)
343 SwDDEField::~SwDDEField()
345 if( GetTyp()->HasOnlyOneListener() )
346 static_cast<SwDDEFieldType
*>(GetTyp())->Disconnect();
349 OUString
SwDDEField::ExpandImpl(SwRootFrame
const*const) const
351 OUString aStr
= static_cast<SwDDEFieldType
*>(GetTyp())->GetExpansion();
352 aStr
= aStr
.replaceAll("\r", "");
353 aStr
= aStr
.replaceAll("\t", " ");
354 aStr
= aStr
.replaceAll("\n", "|");
355 if (aStr
.endsWith("|"))
357 return aStr
.copy(0, aStr
.getLength()-1);
362 std::unique_ptr
<SwField
> SwDDEField::Copy() const
364 return std::make_unique
<SwDDEField
>(static_cast<SwDDEFieldType
*>(GetTyp()));
367 /// get field type name
368 OUString
SwDDEField::GetPar1() const
370 return static_cast<const SwDDEFieldType
*>(GetTyp())->GetName();
373 /// get field type command
374 OUString
SwDDEField::GetPar2() const
376 return static_cast<const SwDDEFieldType
*>(GetTyp())->GetCmd();
379 /// set field type command
380 void SwDDEField::SetPar2(const OUString
& rStr
)
382 static_cast<SwDDEFieldType
*>(GetTyp())->SetCmd(rStr
);
385 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */