use insert function instead of for loop
[LibreOffice.git] / sfx2 / source / appl / lnkbase2.cxx
blob4b4f297b62a7655e0692c366d269b75a1095b74c
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 .
21 #include <memory>
22 #include <sfx2/lnkbase.hxx>
23 #include <sot/exchange.hxx>
24 #include <com/sun/star/uno/Any.hxx>
25 #include <com/sun/star/uno/Sequence.hxx>
26 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
27 #include <sfx2/linkmgr.hxx>
28 #include <vcl/svapp.hxx>
29 #include <vcl/weld.hxx>
30 #include <sfx2/strings.hrc>
31 #include <sfx2/sfxresid.hxx>
32 #include <sfx2/filedlghelper.hxx>
33 #include <tools/debug.hxx>
34 #include <svl/svdde.hxx>
35 #include <osl/diagnose.h>
36 #include <officecfg/Office/Common.hxx>
38 using namespace ::com::sun::star;
39 using namespace ::com::sun::star::uno;
41 namespace sfx2
44 namespace {
45 class ImplDdeItem;
48 // only for internal management
49 struct ImplBaseLinkData
51 struct tClientType
53 // applies for all links
54 SotClipboardFormatId nCntntType; // Update Format
55 // Not Ole-Links
56 bool bIntrnlLnk; // It is an internal link
57 SfxLinkUpdateMode nUpdateMode; // UpdateMode
60 struct tDDEType
62 ImplDdeItem* pItem;
65 union {
66 tClientType ClientType;
67 tDDEType DDEType;
69 ImplBaseLinkData()
71 ClientType.nCntntType = SotClipboardFormatId::NONE;
72 ClientType.bIntrnlLnk = false;
73 ClientType.nUpdateMode = SfxLinkUpdateMode::NONE;
74 DDEType.pItem = nullptr;
78 namespace {
80 class ImplDdeItem : public DdeGetPutItem
82 SvBaseLink* pLink;
83 DdeData aData;
84 Sequence< sal_Int8 > aSeq; // Datacontainer for DdeData !!!
85 bool bIsValidData : 1;
86 bool bIsInDTOR : 1;
87 public:
88 #if defined(_WIN32)
89 ImplDdeItem( SvBaseLink& rLink, const OUString& rStr )
90 : DdeGetPutItem( rStr ), pLink( &rLink ), bIsValidData( false ),
91 bIsInDTOR( false )
93 #endif
94 virtual ~ImplDdeItem() override;
96 virtual DdeData* Get( SotClipboardFormatId ) override;
97 virtual bool Put( const DdeData* ) override;
98 virtual void AdviseLoop( bool ) override;
100 void Notify()
102 bIsValidData = false;
103 DdeGetPutItem::NotifyClient();
106 bool IsInDTOR() const { return bIsInDTOR; }
111 SvBaseLink::SvBaseLink()
112 : m_pLinkMgr( nullptr )
113 , m_pParentWin( nullptr )
114 , m_bIsConnect( false )
115 , m_bIsReadOnly(false)
117 mnObjType = SvBaseLinkObjectType::ClientSo;
118 pImplData.reset( new ImplBaseLinkData );
119 bVisible = bSynchron = true;
120 bWasLastEditOK = false;
124 SvBaseLink::SvBaseLink( SfxLinkUpdateMode nUpdateMode, SotClipboardFormatId nContentType )
125 : m_pLinkMgr( nullptr )
126 , m_pParentWin( nullptr )
127 , m_bIsConnect( false )
128 , m_bIsReadOnly(false)
130 mnObjType = SvBaseLinkObjectType::ClientSo;
131 pImplData.reset( new ImplBaseLinkData );
132 bVisible = bSynchron = true;
133 bWasLastEditOK = false;
135 // It is going to be an OLE-Link,
136 pImplData->ClientType.nUpdateMode = nUpdateMode;
137 pImplData->ClientType.nCntntType = nContentType;
138 pImplData->ClientType.bIntrnlLnk = false;
141 #if defined(_WIN32)
143 static DdeTopic* FindTopic( const OUString & rLinkName, sal_uInt16* pItemStt )
145 if( rLinkName.isEmpty() )
146 return nullptr;
148 OUString sNm( rLinkName );
149 sal_Int32 nTokenPos = 0;
150 OUString sService( sNm.getToken( 0, cTokenSeparator, nTokenPos ) );
152 DdeServices& rSvc = DdeService::GetServices();
153 for (auto const& elem : rSvc)
155 if(elem->GetName() == sService)
157 // then we search for the Topic
158 OUString sTopic( sNm.getToken( 0, cTokenSeparator, nTokenPos ) );
159 if( pItemStt )
160 *pItemStt = nTokenPos;
162 std::vector<DdeTopic*>& rTopics = elem->GetTopics();
164 for (auto const& topic : rTopics)
165 if( topic->GetName() == sTopic )
166 return topic;
167 break;
170 return nullptr;
173 SvBaseLink::SvBaseLink( const OUString& rLinkName, SvBaseLinkObjectType nObjectType, SvLinkSource* pObj )
174 : m_pLinkMgr( nullptr )
175 , m_pParentWin( nullptr )
176 , m_bIsConnect( false )
177 , m_bIsReadOnly(false)
179 bVisible = bSynchron = true;
180 bWasLastEditOK = false;
181 aLinkName = rLinkName;
182 pImplData.reset( new ImplBaseLinkData );
183 mnObjType = nObjectType;
185 if( !pObj )
187 DBG_ASSERT( pObj, "Where is my left-most object" );
188 return;
191 if( SvBaseLinkObjectType::DdeExternal == mnObjType )
193 sal_uInt16 nItemStt = 0;
194 DdeTopic* pTopic = FindTopic( aLinkName, &nItemStt );
195 if( pTopic )
197 // then we have it all together
198 // MM_TODO how do I get the name
199 OUString aStr = aLinkName; // xLinkName->GetDisplayName();
200 aStr = aStr.copy( nItemStt );
201 pImplData->DDEType.pItem = new ImplDdeItem( *this, aStr );
202 pTopic->InsertItem( pImplData->DDEType.pItem );
204 // store the Advice
205 xObj = pObj;
208 else if( pObj->Connect( this ) )
209 xObj = pObj;
212 #endif
214 SvBaseLink::~SvBaseLink()
216 Disconnect();
218 if( mnObjType == SvBaseLinkObjectType::DdeExternal )
220 if( !pImplData->DDEType.pItem->IsInDTOR() )
221 delete pImplData->DDEType.pItem;
224 pImplData.reset();
227 IMPL_LINK( SvBaseLink, EndEditHdl, const OUString&, _rNewName, void )
229 OUString sNewName = _rNewName;
230 if ( !ExecuteEdit( sNewName ) )
231 sNewName.clear();
232 bWasLastEditOK = !sNewName.isEmpty();
233 m_aEndEditLink.Call( *this );
237 void SvBaseLink::SetObjType( SvBaseLinkObjectType mnObjTypeP )
239 DBG_ASSERT( mnObjType != SvBaseLinkObjectType::ClientDde, "type already set" );
240 DBG_ASSERT( !xObj.is(), "object exist" );
242 mnObjType = mnObjTypeP;
246 void SvBaseLink::SetName( const OUString & rNm )
248 aLinkName = rNm;
252 void SvBaseLink::SetObj( SvLinkSource * pObj )
254 DBG_ASSERT( (isClientType(mnObjType) &&
255 pImplData->ClientType.bIntrnlLnk) ||
256 mnObjType == SvBaseLinkObjectType::ClientGraphic,
257 "no intern link" );
258 xObj = pObj;
262 void SvBaseLink::SetLinkSourceName( const OUString & rLnkNm )
264 if( aLinkName == rLnkNm )
265 return;
267 AddNextRef(); // should be superfluous
268 // remove old connection
269 Disconnect();
271 aLinkName = rLnkNm;
273 // New Connection
274 GetRealObject_();
275 ReleaseRef(); // should be superfluous
279 void SvBaseLink::SetUpdateMode( SfxLinkUpdateMode nMode )
281 if( isClientType(mnObjType) &&
282 pImplData->ClientType.nUpdateMode != nMode )
284 AddNextRef();
285 Disconnect();
287 pImplData->ClientType.nUpdateMode = nMode;
288 GetRealObject_();
289 ReleaseRef();
293 // #i88291#
294 void SvBaseLink::clearStreamToLoadFrom()
296 m_xInputStreamToLoadFrom.clear();
297 if( xObj.is() )
299 xObj->clearStreamToLoadFrom();
303 bool SvBaseLink::Update()
305 if(officecfg::Office::Common::Security::Scripting::DisableActiveContent::get())
306 return false;
308 if( isClientType(mnObjType) )
310 AddNextRef();
311 Disconnect();
313 GetRealObject_();
314 ReleaseRef();
315 if( xObj.is() )
317 xObj->setStreamToLoadFrom(m_xInputStreamToLoadFrom,m_bIsReadOnly);
318 OUString sMimeType( SotExchange::GetFormatMimeType(
319 pImplData->ClientType.nCntntType ));
320 Any aData;
322 if( xObj->GetData( aData, sMimeType ) )
324 UpdateResult eRes = DataChanged(sMimeType, aData);
325 bool bSuccess = eRes == SUCCESS;
326 //for manual Updates there is no need to hold the ServerObject
327 if( SvBaseLinkObjectType::ClientDde == mnObjType &&
328 SfxLinkUpdateMode::ONCALL == GetUpdateMode() && xObj.is() )
329 xObj->RemoveAllDataAdvise( this );
330 return bSuccess;
332 if( xObj.is() )
334 // should be asynchronous?
335 if( xObj->IsPending() )
336 return true;
338 // we do not need the object anymore
339 AddNextRef();
340 Disconnect();
341 ReleaseRef();
345 return false;
349 SfxLinkUpdateMode SvBaseLink::GetUpdateMode() const
351 return isClientType(mnObjType)
352 ? pImplData->ClientType.nUpdateMode
353 : SfxLinkUpdateMode::ONCALL;
357 void SvBaseLink::GetRealObject_( bool bConnect)
359 if( !m_pLinkMgr )
360 return;
362 DBG_ASSERT( !xObj.is(), "object already exist" );
364 if( SvBaseLinkObjectType::ClientDde == mnObjType )
366 OUString sServer;
367 if( sfx2::LinkManager::GetDisplayNames( this, &sServer ) &&
368 sServer == Application::GetAppName() ) // internal Link !!!
370 // so that the Internal link can be created!
371 mnObjType = SvBaseLinkObjectType::Internal;
372 xObj = sfx2::LinkManager::CreateObj( this );
374 pImplData->ClientType.bIntrnlLnk = true;
375 mnObjType = SvBaseLinkObjectType::ClientDde; // so we know what it once was!
377 else
379 pImplData->ClientType.bIntrnlLnk = false;
380 xObj = sfx2::LinkManager::CreateObj( this );
383 else if( isClientType(mnObjType) )
384 xObj = sfx2::LinkManager::CreateObj( this );
386 if( bConnect && ( !xObj.is() || !xObj->Connect( this ) ) )
387 Disconnect();
390 SotClipboardFormatId SvBaseLink::GetContentType() const
392 if( isClientType(mnObjType) )
393 return pImplData->ClientType.nCntntType;
395 return SotClipboardFormatId::NONE; // all Formats ?
399 void SvBaseLink::SetContentType( SotClipboardFormatId nType )
401 if( isClientType(mnObjType) )
403 pImplData->ClientType.nCntntType = nType;
407 LinkManager* SvBaseLink::GetLinkManager()
409 return m_pLinkMgr;
412 const LinkManager* SvBaseLink::GetLinkManager() const
414 return m_pLinkMgr;
417 void SvBaseLink::SetLinkManager( LinkManager* _pMgr )
419 m_pLinkMgr = _pMgr;
422 void SvBaseLink::Disconnect()
424 if( xObj.is() )
426 xObj->RemoveAllDataAdvise( this );
427 xObj->RemoveConnectAdvise( this );
428 xObj.clear();
432 SvBaseLink::UpdateResult SvBaseLink::DataChanged( const OUString &, const css::uno::Any & )
434 if ( mnObjType == SvBaseLinkObjectType::DdeExternal )
436 if( pImplData->DDEType.pItem )
437 pImplData->DDEType.pItem->Notify();
439 return SUCCESS;
442 void SvBaseLink::Edit(weld::Window* pParent, const Link<SvBaseLink&,void>& rEndEditHdl )
444 m_pParentWin = pParent;
445 m_aEndEditLink = rEndEditHdl;
446 m_bIsConnect = xObj.is();
447 if( !m_bIsConnect )
448 GetRealObject_( xObj.is() );
450 bool bAsync = false;
451 Link<const OUString&, void> aLink = LINK( this, SvBaseLink, EndEditHdl );
453 if( isClientType(mnObjType) && pImplData->ClientType.bIntrnlLnk )
455 if( m_pLinkMgr )
457 SvLinkSourceRef ref = sfx2::LinkManager::CreateObj( this );
458 if( ref.is() )
460 ref->Edit( pParent, this, aLink );
461 bAsync = true;
465 else
467 xObj->Edit( pParent, this, aLink );
468 bAsync = true;
471 if ( !bAsync )
473 ExecuteEdit( OUString() );
474 bWasLastEditOK = false;
475 m_aEndEditLink.Call( *this );
479 bool SvBaseLink::ExecuteEdit( const OUString& _rNewName )
481 if( !_rNewName.isEmpty() )
483 SetLinkSourceName( _rNewName );
484 if( !Update() )
486 OUString sApp, sTopic, sItem, sError;
487 sfx2::LinkManager::GetDisplayNames( this, &sApp, &sTopic, &sItem );
488 if( mnObjType == SvBaseLinkObjectType::ClientDde )
490 sError = SfxResId(STR_DDE_ERROR);
492 sal_Int32 nFndPos = sError.indexOf( "%1" );
493 if( -1 != nFndPos )
495 sError = sError.replaceAt( nFndPos, 2, sApp );
496 nFndPos = nFndPos + sApp.getLength();
498 if( -1 != ( nFndPos = sError.indexOf( "%2", nFndPos )))
500 sError = sError.replaceAt( nFndPos, 2, sTopic );
501 nFndPos = nFndPos + sTopic.getLength();
503 if( -1 != ( nFndPos = sError.indexOf( "%3", nFndPos )))
504 sError = sError.replaceAt( nFndPos, 2, sItem );
508 else
509 return false;
511 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_pParentWin,
512 VclMessageType::Warning, VclButtonsType::Ok, sError));
513 xBox->run();
516 else if( !m_bIsConnect )
517 Disconnect();
518 m_bIsConnect = false;
519 return true;
522 void SvBaseLink::Closed()
524 if( xObj.is() )
525 xObj->RemoveAllDataAdvise( this );
528 FileDialogHelper & SvBaseLink::GetInsertFileDialog(const OUString& rFactory)
530 m_pFileDlg.reset( new FileDialogHelper(
531 ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE,
532 FileDialogFlags::Insert, rFactory, SfxFilterFlags::NONE, SfxFilterFlags::NONE, m_pParentWin) );
533 return *m_pFileDlg;
536 ImplDdeItem::~ImplDdeItem()
538 bIsInDTOR = true;
539 // So that no-one gets the idea to delete the pointer when Disconnecting!
540 tools::SvRef<SvBaseLink> aRef( pLink );
541 aRef->Disconnect();
544 DdeData* ImplDdeItem::Get( SotClipboardFormatId nFormat )
546 if( pLink->GetObj() )
548 // is it still valid?
549 if( bIsValidData && nFormat == aData.GetFormat() )
550 return &aData;
552 Any aValue;
553 OUString sMimeType( SotExchange::GetFormatMimeType( nFormat ));
554 if( pLink->GetObj()->GetData( aValue, sMimeType ) )
556 if( aValue >>= aSeq )
558 aData = DdeData( aSeq.getConstArray(), aSeq.getLength(), nFormat );
560 bIsValidData = true;
561 return &aData;
565 aSeq.realloc( 0 );
566 bIsValidData = false;
567 return nullptr;
571 bool ImplDdeItem::Put( const DdeData* )
573 OSL_FAIL( "ImplDdeItem::Put not implemented" );
574 return false;
578 void ImplDdeItem::AdviseLoop( bool bOpen )
580 // Connection is closed, so also unsubscribe link
581 if( !pLink->GetObj() )
582 return;
584 if( bOpen )
586 // A connection is re-established
587 if( SvBaseLinkObjectType::DdeExternal == pLink->GetObjType() )
589 pLink->GetObj()->AddDataAdvise( pLink, u"text/plain;charset=utf-16"_ustr, ADVISEMODE_NODATA );
590 pLink->GetObj()->AddConnectAdvise( pLink );
593 else
595 // So that no-one gets the idea to delete the pointer
596 // when Disconnecting!
597 tools::SvRef<SvBaseLink> aRef( pLink );
598 aRef->Disconnect();
604 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */