android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / core / fields / ddefld.cxx
blob46ac3d5049555bde7392724459bd46358f5efaf6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
28 #include <doc.hxx>
29 #include <IDocumentLinksAdministration.hxx>
30 #include <IDocumentState.hxx>
31 #include <IDocumentLayoutAccess.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>
41 using namespace ::com::sun::star;
43 namespace {
45 class SwIntrnlRefLink : public SwBaseLink
47 SwDDEFieldType& m_rFieldType;
49 public:
50 SwIntrnlRefLink(SwDDEFieldType& rType, SfxLinkUpdateMode nUpdateType)
51 : SwBaseLink(nUpdateType, SotClipboardFormatId::STRING)
52 , m_rFieldType(rType)
55 virtual void Closed() override;
56 virtual ::sfx2::SvBaseLink::UpdateResult DataChanged(
57 const OUString& rMimeType, const css::uno::Any & rValue ) override;
59 virtual const SwNode* GetAnchor() const override;
60 virtual bool IsInRange( SwNodeOffset nSttNd, SwNodeOffset nEndNd ) const override;
65 ::sfx2::SvBaseLink::UpdateResult SwIntrnlRefLink::DataChanged( const OUString& rMimeType,
66 const uno::Any & rValue )
68 switch( SotExchange::GetFormatIdFromMimeType( rMimeType ) )
70 case SotClipboardFormatId::STRING:
71 if( !IsNoDataFlag() )
73 OUString sStr;
74 if (!(rValue >>= sStr))
76 uno::Sequence< sal_Int8 > aSeq;
77 rValue >>= aSeq;
78 sStr = OUString(reinterpret_cast<char const*>(aSeq.getConstArray()), aSeq.getLength(), osl_getThreadTextEncoding());
81 // remove not needed CR-LF at the end
82 sal_Int32 n = sStr.getLength();
83 while( n && 0 == sStr[ n-1 ] )
84 --n;
85 if( n && 0x0a == sStr[ n-1 ] )
86 --n;
87 if( n && 0x0d == sStr[ n-1 ] )
88 --n;
90 bool bDel = n != sStr.getLength();
91 if( bDel )
92 sStr = sStr.copy( 0, n );
94 m_rFieldType.SetExpansion(sStr);
95 // set Expansion first! (otherwise this flag will be deleted)
96 m_rFieldType.SetCRLFDelFlag(bDel);
98 break;
100 // other formats
101 default:
102 return SUCCESS;
105 if(!ChkNoDataFlag())
106 m_rFieldType.UpdateDDE();
108 return SUCCESS;
111 void SwIntrnlRefLink::Closed()
113 if (m_rFieldType.GetDoc() && !m_rFieldType.GetDoc()->IsInDtor())
115 // advise goes, convert all fields into text?
116 SwViewShell* pSh = m_rFieldType.GetDoc()->getIDocumentLayoutAccess().GetCurrentViewShell();
117 if (SwEditShell* pESh = m_rFieldType.GetDoc()->GetEditShell())
119 pESh->StartAllAction();
120 pESh->FieldToText(&m_rFieldType);
121 pESh->EndAllAction();
123 else
125 pSh->StartAction();
126 // to call at the doc ??
127 pSh->EndAction();
130 SvBaseLink::Closed();
133 sw::LinkAnchorSearchHint::~LinkAnchorSearchHint() {};
135 const SwNode* SwIntrnlRefLink::GetAnchor() const
137 // here, any anchor of the normal NodesArray should be sufficient
138 const SwNode* pNd = nullptr;
139 m_rFieldType.CallSwClientNotify(
140 sw::LinkAnchorSearchHint(m_rFieldType.GetDoc()->GetNodes(), pNd));
141 return pNd;
144 bool SwIntrnlRefLink::IsInRange( SwNodeOffset nSttNd, SwNodeOffset nEndNd ) const
146 bool bInRange = false;
147 m_rFieldType.CallSwClientNotify(sw::InRangeSearchHint(nSttNd, nEndNd, bInRange));
148 return bInRange;
151 SwDDEFieldType::SwDDEFieldType( OUString aName,
152 const OUString& rCmd, SfxLinkUpdateMode nUpdateType )
153 : SwFieldType( SwFieldIds::Dde ),
154 m_aName( std::move(aName) ), m_pDoc( nullptr ), m_nRefCount( 0 )
156 m_bCRLFFlag = m_bDeleted = false;
157 m_RefLink = new SwIntrnlRefLink( *this, nUpdateType );
158 SetCmd( rCmd );
161 SwDDEFieldType::~SwDDEFieldType()
163 if( m_pDoc && !m_pDoc->IsInDtor() )
164 m_pDoc->getIDocumentLinksAdministration().GetLinkManager().Remove( m_RefLink.get() );
165 m_RefLink->Disconnect();
168 std::unique_ptr<SwFieldType> SwDDEFieldType::Copy() const
170 std::unique_ptr<SwDDEFieldType> pType(new SwDDEFieldType( m_aName, GetCmd(), GetType() ));
171 pType->m_aExpansion = m_aExpansion;
172 pType->m_bCRLFFlag = m_bCRLFFlag;
173 pType->m_bDeleted = m_bDeleted;
174 pType->SetDoc( m_pDoc );
175 return pType;
178 OUString SwDDEFieldType::GetName() const
180 return m_aName;
183 void SwDDEFieldType::SetCmd( const OUString& _aStr )
185 OUString aStr = _aStr;
186 sal_Int32 nIndex = 0;
189 aStr = aStr.replaceFirst(" ", " ", &nIndex);
190 } while (nIndex>=0);
191 m_RefLink->SetLinkSourceName( aStr );
194 OUString const & SwDDEFieldType::GetCmd() const
196 return m_RefLink->GetLinkSourceName();
199 void SwDDEFieldType::SetDoc( SwDoc* pNewDoc )
201 if( pNewDoc == m_pDoc )
202 return;
204 if( m_pDoc && m_RefLink.is() )
206 OSL_ENSURE( !m_nRefCount, "How do we get the references?" );
207 m_pDoc->getIDocumentLinksAdministration().GetLinkManager().Remove( m_RefLink.get() );
210 m_pDoc = pNewDoc;
211 if( m_pDoc && m_nRefCount )
213 m_RefLink->SetVisible( m_pDoc->getIDocumentLinksAdministration().IsVisibleLinks() );
214 m_pDoc->getIDocumentLinksAdministration().GetLinkManager().InsertDDELink( m_RefLink.get() );
218 void SwDDEFieldType::RefCntChgd()
220 if( m_nRefCount )
222 m_RefLink->SetVisible( m_pDoc->getIDocumentLinksAdministration().IsVisibleLinks() );
223 m_pDoc->getIDocumentLinksAdministration().GetLinkManager().InsertDDELink( m_RefLink.get() );
224 if( m_pDoc->getIDocumentLayoutAccess().GetCurrentViewShell() )
225 m_RefLink->Update();
227 else
229 Disconnect();
230 m_pDoc->getIDocumentLinksAdministration().GetLinkManager().Remove( m_RefLink.get() );
234 void SwDDEFieldType::QueryValue( uno::Any& rVal, sal_uInt16 nWhichId ) const
236 sal_Int32 nPart = -1;
237 switch( nWhichId )
239 case FIELD_PROP_PAR2: nPart = 2; break;
240 case FIELD_PROP_PAR4: nPart = 1; break;
241 case FIELD_PROP_SUBTYPE: nPart = 0; break;
242 case FIELD_PROP_BOOL1:
243 rVal <<= GetType() == SfxLinkUpdateMode::ALWAYS;
244 break;
245 case FIELD_PROP_PAR5:
246 rVal <<= m_aExpansion;
247 break;
248 default:
249 assert(false);
251 if ( nPart>=0 )
252 rVal <<= GetCmd().getToken(nPart, sfx2::cTokenSeparator);
255 void SwDDEFieldType::PutValue( const uno::Any& rVal, sal_uInt16 nWhichId )
257 sal_Int32 nPart = -1;
258 switch( nWhichId )
260 case FIELD_PROP_PAR2: nPart = 2; break;
261 case FIELD_PROP_PAR4: nPart = 1; break;
262 case FIELD_PROP_SUBTYPE: nPart = 0; break;
263 case FIELD_PROP_BOOL1:
264 SetType( *o3tl::doAccess<bool>(rVal) ?
265 SfxLinkUpdateMode::ALWAYS :
266 SfxLinkUpdateMode::ONCALL );
267 break;
268 case FIELD_PROP_PAR5:
269 rVal >>= m_aExpansion;
270 break;
271 default:
272 assert(false);
274 if( nPart<0 )
275 return;
277 const OUString sOldCmd( GetCmd() );
278 OUStringBuffer sNewCmd;
279 sal_Int32 nIndex = 0;
280 for (sal_Int32 i=0; i<3; ++i)
282 OUString sToken = sOldCmd.getToken(0, sfx2::cTokenSeparator, nIndex);
283 if (i==nPart)
285 rVal >>= sToken;
287 sNewCmd.append((i < 2)
288 ? sToken + OUStringChar(sfx2::cTokenSeparator) : sToken);
290 SetCmd( sNewCmd.makeStringAndClear() );
293 void SwDDEFieldType::UpdateDDE(const bool bNotifyShells)
295 auto pDoc = GetDoc();
296 assert(pDoc);
297 if(IsModifyLocked())
298 return;
299 SwViewShell* pSh = bNotifyShells ? pDoc->getIDocumentLayoutAccess().GetCurrentViewShell() : nullptr;
300 SwEditShell* pESh = bNotifyShells ? pDoc->GetEditShell() : nullptr;
302 // Search for fields. If no valid found, disconnect.
303 LockModify();
305 std::vector<SwFormatField*> vFields;
306 std::vector<SwDDETable*> vTables;
307 GatherFields(vFields, false);
308 GatherDdeTables(vTables);
309 const bool bDoAction = vFields.size() || vTables.size();
310 if(bDoAction)
312 if(pESh)
313 pESh->StartAllAction();
314 else if(pSh)
315 pSh->StartAction();
318 // a DDE tables in the text
319 for(auto pTable: vTables)
320 pTable->ChangeContent();
322 UnlockModify();
324 if(bDoAction)
326 if(pESh)
327 pESh->EndAllAction();
328 else if(pSh)
329 pSh->EndAction();
331 if(pSh)
332 pSh->GetDoc()->getIDocumentState().SetModified();
336 SwDDEField::SwDDEField( SwDDEFieldType* pInitType )
337 : SwField(pInitType)
341 SwDDEField::~SwDDEField()
343 if( GetTyp()->HasOnlyOneListener() )
344 static_cast<SwDDEFieldType*>(GetTyp())->Disconnect();
347 OUString SwDDEField::ExpandImpl(SwRootFrame const*const) const
349 OUString aStr = static_cast<SwDDEFieldType*>(GetTyp())->GetExpansion();
350 aStr = aStr.replaceAll("\r", "");
351 aStr = aStr.replaceAll("\t", " ");
352 aStr = aStr.replaceAll("\n", "|");
353 if (aStr.endsWith("|"))
355 return aStr.copy(0, aStr.getLength()-1);
357 return aStr;
360 std::unique_ptr<SwField> SwDDEField::Copy() const
362 return std::make_unique<SwDDEField>(static_cast<SwDDEFieldType*>(GetTyp()));
365 /// get field type name
366 OUString SwDDEField::GetPar1() const
368 return static_cast<const SwDDEFieldType*>(GetTyp())->GetName();
371 /// get field type command
372 OUString SwDDEField::GetPar2() const
374 return static_cast<const SwDDEFieldType*>(GetTyp())->GetCmd();
377 /// set field type command
378 void SwDDEField::SetPar2(const OUString& rStr)
380 static_cast<SwDDEFieldType*>(GetTyp())->SetCmd(rStr);
383 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */