LibreOffice Module sfx2 (master) 1
lnkbase2.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
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>
33#include <tools/debug.hxx>
34#include <svl/svdde.hxx>
35#include <osl/diagnose.h>
36
37using namespace ::com::sun::star;
38using namespace ::com::sun::star::uno;
39
40namespace sfx2
41{
42
43namespace {
44class ImplDdeItem;
45}
46
47// only for internal management
49{
51 {
52 // applies for all links
54 // Not Ole-Links
55 bool bIntrnlLnk; // It is an internal link
57 };
58
59 struct tDDEType
60 {
61 ImplDdeItem* pItem;
62 };
63
64 union {
67 };
69 {
70 ClientType.nCntntType = SotClipboardFormatId::NONE;
71 ClientType.bIntrnlLnk = false;
73 DDEType.pItem = nullptr;
74 }
75};
76
77namespace {
78
79class ImplDdeItem : public DdeGetPutItem
80{
81 SvBaseLink* pLink;
83 Sequence< sal_Int8 > aSeq; // Datacontainer for DdeData !!!
84 bool bIsValidData : 1;
85 bool bIsInDTOR : 1;
86public:
87#if defined(_WIN32)
88 ImplDdeItem( SvBaseLink& rLink, const OUString& rStr )
89 : DdeGetPutItem( rStr ), pLink( &rLink ), bIsValidData( false ),
90 bIsInDTOR( false )
91 {}
92#endif
93 virtual ~ImplDdeItem() override;
94
95 virtual DdeData* Get( SotClipboardFormatId ) override;
96 virtual bool Put( const DdeData* ) override;
97 virtual void AdviseLoop( bool ) override;
98
99 void Notify()
100 {
101 bIsValidData = false;
103 }
104
105 bool IsInDTOR() const { return bIsInDTOR; }
106};
107
108}
109
111 : m_pLinkMgr( nullptr )
112 , m_pParentWin( nullptr )
113 , m_bIsConnect( false )
114 , m_bIsReadOnly(false)
115{
117 pImplData.reset( new ImplBaseLinkData );
118 bVisible = bSynchron = true;
119 bWasLastEditOK = false;
120}
121
122
124 : m_pLinkMgr( nullptr )
125 , m_pParentWin( nullptr )
126 , m_bIsConnect( false )
127 , m_bIsReadOnly(false)
128{
130 pImplData.reset( new ImplBaseLinkData );
131 bVisible = bSynchron = true;
132 bWasLastEditOK = false;
133
134 // It is going to be an OLE-Link,
135 pImplData->ClientType.nUpdateMode = nUpdateMode;
136 pImplData->ClientType.nCntntType = nContentType;
137 pImplData->ClientType.bIntrnlLnk = false;
138}
139
140#if defined(_WIN32)
141
142static DdeTopic* FindTopic( const OUString & rLinkName, sal_uInt16* pItemStt )
143{
144 if( rLinkName.isEmpty() )
145 return nullptr;
146
147 OUString sNm( rLinkName );
148 sal_Int32 nTokenPos = 0;
149 OUString sService( sNm.getToken( 0, cTokenSeparator, nTokenPos ) );
150
152 for (auto const& elem : rSvc)
153 {
154 if(elem->GetName() == sService)
155 {
156 // then we search for the Topic
157 OUString sTopic( sNm.getToken( 0, cTokenSeparator, nTokenPos ) );
158 if( pItemStt )
159 *pItemStt = nTokenPos;
160
161 std::vector<DdeTopic*>& rTopics = elem->GetTopics();
162
163 for (auto const& topic : rTopics)
164 if( topic->GetName() == sTopic )
165 return topic;
166 break;
167 }
168 }
169 return nullptr;
170}
171
172SvBaseLink::SvBaseLink( const OUString& rLinkName, SvBaseLinkObjectType nObjectType, SvLinkSource* pObj )
173 : m_pLinkMgr( nullptr )
174 , m_pParentWin( nullptr )
175 , m_bIsConnect( false )
176 , m_bIsReadOnly(false)
177{
178 bVisible = bSynchron = true;
179 bWasLastEditOK = false;
180 aLinkName = rLinkName;
181 pImplData.reset( new ImplBaseLinkData );
182 mnObjType = nObjectType;
183
184 if( !pObj )
185 {
186 DBG_ASSERT( pObj, "Where is my left-most object" );
187 return;
188 }
189
191 {
192 sal_uInt16 nItemStt = 0;
193 DdeTopic* pTopic = FindTopic( aLinkName, &nItemStt );
194 if( pTopic )
195 {
196 // then we have it all together
197 // MM_TODO how do I get the name
198 OUString aStr = aLinkName; // xLinkName->GetDisplayName();
199 aStr = aStr.copy( nItemStt );
200 pImplData->DDEType.pItem = new ImplDdeItem( *this, aStr );
201 pTopic->InsertItem( pImplData->DDEType.pItem );
202
203 // store the Advice
204 xObj = pObj;
205 }
206 }
207 else if( pObj->Connect( this ) )
208 xObj = pObj;
209}
210
211#endif
212
214{
215 Disconnect();
216
218 {
219 if( !pImplData->DDEType.pItem->IsInDTOR() )
220 delete pImplData->DDEType.pItem;
221 }
222
223 pImplData.reset();
224}
225
226IMPL_LINK( SvBaseLink, EndEditHdl, const OUString&, _rNewName, void )
227{
228 OUString sNewName = _rNewName;
229 if ( !ExecuteEdit( sNewName ) )
230 sNewName.clear();
231 bWasLastEditOK = !sNewName.isEmpty();
232 m_aEndEditLink.Call( *this );
233}
234
235
237{
238 DBG_ASSERT( mnObjType != SvBaseLinkObjectType::ClientDde, "type already set" );
239 DBG_ASSERT( !xObj.is(), "object exist" );
240
241 mnObjType = mnObjTypeP;
242}
243
244
245void SvBaseLink::SetName( const OUString & rNm )
246{
247 aLinkName = rNm;
248}
249
250
252{
254 pImplData->ClientType.bIntrnlLnk) ||
256 "no intern link" );
257 xObj = pObj;
258}
259
260
261void SvBaseLink::SetLinkSourceName( const OUString & rLnkNm )
262{
263 if( aLinkName == rLnkNm )
264 return;
265
266 AddNextRef(); // should be superfluous
267 // remove old connection
268 Disconnect();
269
270 aLinkName = rLnkNm;
271
272 // New Connection
274 ReleaseRef(); // should be superfluous
275}
276
277
279{
280 if( isClientType(mnObjType) &&
281 pImplData->ClientType.nUpdateMode != nMode )
282 {
283 AddNextRef();
284 Disconnect();
285
286 pImplData->ClientType.nUpdateMode = nMode;
288 ReleaseRef();
289 }
290}
291
292// #i88291#
294{
296 if( xObj.is() )
297 {
298 xObj->clearStreamToLoadFrom();
299 }
300}
301
303{
305 {
306 AddNextRef();
307 Disconnect();
308
310 ReleaseRef();
311 if( xObj.is() )
312 {
313 xObj->setStreamToLoadFrom(m_xInputStreamToLoadFrom,m_bIsReadOnly);
314 OUString sMimeType( SotExchange::GetFormatMimeType(
315 pImplData->ClientType.nCntntType ));
316 Any aData;
317
318 if( xObj->GetData( aData, sMimeType ) )
319 {
320 UpdateResult eRes = DataChanged(sMimeType, aData);
321 bool bSuccess = eRes == SUCCESS;
322 //for manual Updates there is no need to hold the ServerObject
325 xObj->RemoveAllDataAdvise( this );
326 return bSuccess;
327 }
328 if( xObj.is() )
329 {
330 // should be asynchronous?
331 if( xObj->IsPending() )
332 return true;
333
334 // we do not need the object anymore
335 AddNextRef();
336 Disconnect();
337 ReleaseRef();
338 }
339 }
340 }
341 return false;
342}
343
344
346{
347 return isClientType(mnObjType)
348 ? pImplData->ClientType.nUpdateMode
350}
351
352
353void SvBaseLink::GetRealObject_( bool bConnect)
354{
355 if( !m_pLinkMgr )
356 return;
357
358 DBG_ASSERT( !xObj.is(), "object already exist" );
359
361 {
362 OUString sServer;
363 if( sfx2::LinkManager::GetDisplayNames( this, &sServer ) &&
364 sServer == Application::GetAppName() ) // internal Link !!!
365 {
366 // so that the Internal link can be created!
369
370 pImplData->ClientType.bIntrnlLnk = true;
371 mnObjType = SvBaseLinkObjectType::ClientDde; // so we know what it once was!
372 }
373 else
374 {
375 pImplData->ClientType.bIntrnlLnk = false;
377 }
378 }
379 else if( isClientType(mnObjType) )
381
382 if( bConnect && ( !xObj.is() || !xObj->Connect( this ) ) )
383 Disconnect();
384}
385
387{
389 return pImplData->ClientType.nCntntType;
390
391 return SotClipboardFormatId::NONE; // all Formats ?
392}
393
394
396{
398 {
399 pImplData->ClientType.nCntntType = nType;
400 }
401}
402
404{
405 return m_pLinkMgr;
406}
407
409{
410 return m_pLinkMgr;
411}
412
414{
415 m_pLinkMgr = _pMgr;
416}
417
419{
420 if( xObj.is() )
421 {
422 xObj->RemoveAllDataAdvise( this );
423 xObj->RemoveConnectAdvise( this );
424 xObj.clear();
425 }
426}
427
428SvBaseLink::UpdateResult SvBaseLink::DataChanged( const OUString &, const css::uno::Any & )
429{
431 {
432 if( pImplData->DDEType.pItem )
433 pImplData->DDEType.pItem->Notify();
434 }
435 return SUCCESS;
436}
437
438void SvBaseLink::Edit(weld::Window* pParent, const Link<SvBaseLink&,void>& rEndEditHdl )
439{
440 m_pParentWin = pParent;
441 m_aEndEditLink = rEndEditHdl;
442 m_bIsConnect = xObj.is();
443 if( !m_bIsConnect )
445
446 bool bAsync = false;
447 Link<const OUString&, void> aLink = LINK( this, SvBaseLink, EndEditHdl );
448
449 if( isClientType(mnObjType) && pImplData->ClientType.bIntrnlLnk )
450 {
451 if( m_pLinkMgr )
452 {
454 if( ref.is() )
455 {
456 ref->Edit( pParent, this, aLink );
457 bAsync = true;
458 }
459 }
460 }
461 else
462 {
463 xObj->Edit( pParent, this, aLink );
464 bAsync = true;
465 }
466
467 if ( !bAsync )
468 {
469 ExecuteEdit( OUString() );
470 bWasLastEditOK = false;
471 m_aEndEditLink.Call( *this );
472 }
473}
474
475bool SvBaseLink::ExecuteEdit( const OUString& _rNewName )
476{
477 if( !_rNewName.isEmpty() )
478 {
479 SetLinkSourceName( _rNewName );
480 if( !Update() )
481 {
482 OUString sApp, sTopic, sItem, sError;
483 sfx2::LinkManager::GetDisplayNames( this, &sApp, &sTopic, &sItem );
485 {
486 sError = SfxResId(STR_DDE_ERROR);
487
488 sal_Int32 nFndPos = sError.indexOf( "%1" );
489 if( -1 != nFndPos )
490 {
491 sError = sError.replaceAt( nFndPos, 2, sApp );
492 nFndPos = nFndPos + sApp.getLength();
493
494 if( -1 != ( nFndPos = sError.indexOf( "%2", nFndPos )))
495 {
496 sError = sError.replaceAt( nFndPos, 2, sTopic );
497 nFndPos = nFndPos + sTopic.getLength();
498
499 if( -1 != ( nFndPos = sError.indexOf( "%3", nFndPos )))
500 sError = sError.replaceAt( nFndPos, 2, sItem );
501 }
502 }
503 }
504 else
505 return false;
506
507 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_pParentWin,
508 VclMessageType::Warning, VclButtonsType::Ok, sError));
509 xBox->run();
510 }
511 }
512 else if( !m_bIsConnect )
513 Disconnect();
514 m_bIsConnect = false;
515 return true;
516}
517
519{
520 if( xObj.is() )
521 xObj->RemoveAllDataAdvise( this );
522}
523
525{
526 m_pFileDlg.reset( new FileDialogHelper(
527 ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE,
528 FileDialogFlags::Insert, rFactory, SfxFilterFlags::NONE, SfxFilterFlags::NONE, m_pParentWin) );
529 return *m_pFileDlg;
530}
531
532ImplDdeItem::~ImplDdeItem()
533{
534 bIsInDTOR = true;
535 // So that no-one gets the idea to delete the pointer when Disconnecting!
537 aRef->Disconnect();
538}
539
540DdeData* ImplDdeItem::Get( SotClipboardFormatId nFormat )
541{
542 if( pLink->GetObj() )
543 {
544 // is it still valid?
545 if( bIsValidData && nFormat == aData.GetFormat() )
546 return &aData;
547
548 Any aValue;
549 OUString sMimeType( SotExchange::GetFormatMimeType( nFormat ));
550 if( pLink->GetObj()->GetData( aValue, sMimeType ) )
551 {
552 if( aValue >>= aSeq )
553 {
554 aData = DdeData( aSeq.getConstArray(), aSeq.getLength(), nFormat );
555
556 bIsValidData = true;
557 return &aData;
558 }
559 }
560 }
561 aSeq.realloc( 0 );
562 bIsValidData = false;
563 return nullptr;
564}
565
566
567bool ImplDdeItem::Put( const DdeData* )
568{
569 OSL_FAIL( "ImplDdeItem::Put not implemented" );
570 return false;
571}
572
573
574void ImplDdeItem::AdviseLoop( bool bOpen )
575{
576 // Connection is closed, so also unsubscribe link
577 if( !pLink->GetObj() )
578 return;
579
580 if( bOpen )
581 {
582 // A connection is re-established
584 {
585 pLink->GetObj()->AddDataAdvise( pLink, "text/plain;charset=utf-16", ADVISEMODE_NODATA );
587 }
588 }
589 else
590 {
591 // So that no-one gets the idea to delete the pointer
592 // when Disconnecting!
594 aRef->Disconnect();
595 }
596}
597
598}
599
600/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, const ILibreOfficeKitNotifier *pNotifier=nullptr)
static OUString GetAppName()
SotClipboardFormatId GetFormat() const
void NotifyClient()
static DdeServices & GetServices()
void InsertItem(DdeItem *)
static OUString GetFormatMimeType(SotClipboardFormatId nFormat)
void ReleaseRef()
void AddNextRef()
static bool GetDisplayNames(const SvBaseLink *, OUString *pType, OUString *pFile=nullptr, OUString *pLink=nullptr, OUString *pFilter=nullptr)
Definition: linkmgr2.cxx:218
static SvLinkSourceRef CreateObj(SvBaseLink const *)
Definition: linkmgr2.cxx:348
virtual bool GetData(css::uno::Any &rData, const OUString &rMimeType, bool bSynchron=false)
Definition: linksrc.cxx:406
void AddDataAdvise(SvBaseLink *, const OUString &rMimeType, sal_uInt16 nAdviceMode)
Definition: linksrc.cxx:342
void AddConnectAdvise(SvBaseLink *)
Definition: linksrc.cxx:361
bool is() const
#define DBG_ASSERT(sCon, aError)
SotClipboardFormatId
void Notify(SwFlyFrame *pFly, SwPageFrame *pOld, const SwRect &rOld, const SwRect *pOldRect=nullptr)
#define ADVISEMODE_NODATA
Definition: linksrc.hxx:41
SvBaseLink * pLink
Definition: lnkbase2.cxx:81
Sequence< sal_Int8 > aSeq
Definition: lnkbase2.cxx:83
bool bIsValidData
Definition: lnkbase2.cxx:84
bool bIsInDTOR
Definition: lnkbase2.cxx:85
DdeData aData
Definition: lnkbase2.cxx:82
SfxLinkUpdateMode
Definition: lnkbase.hxx:38
aStr
Definition: mgetempl.cxx:407
SVXCORE_DLLPUBLIC MSO_SPT Get(const OUString &)
Put
const sal_Unicode cTokenSeparator
Definition: linkmgr.hxx:172
IMPL_LINK(SvDDEObject, ImplGetDDEData, const DdeData *, pData, void)
Definition: impldde.cxx:285
SvBaseLinkObjectType
Definition: lnkbase.hxx:52
constexpr bool isClientType(SvBaseLinkObjectType t)
Definition: lnkbase.hxx:62
QPRO_FUNC_TYPE nType
OUString SfxResId(TranslateId aId)
Definition: sfxresid.cxx:22
SotClipboardFormatId nCntntType
Definition: lnkbase2.cxx:53
tClientType ClientType
Definition: lnkbase2.cxx:65
::std::vector< DdeService * > DdeServices