Coverage Report

Created: 2026-03-31 11:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/UnoControls/source/controls/OConnectionPointHelper.cxx
Line
Count
Source
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 <OConnectionPointHelper.hxx>
21
22
#include <OConnectionPointContainerHelper.hxx>
23
24
#include <com/sun/star/lang/InvalidListenerException.hpp>
25
#include <cppuhelper/queryinterface.hxx>
26
#include <comphelper/sequence.hxx>
27
28
//  namespaces
29
30
using namespace ::osl;
31
using namespace ::cppu;
32
using namespace ::com::sun::star::uno;
33
using namespace ::com::sun::star::lang;
34
35
namespace unocontrols {
36
37
//  construct/destruct
38
39
OConnectionPointHelper::OConnectionPointHelper(
40
    Mutex&                              aMutex                      ,
41
    OConnectionPointContainerHelper*    pContainerImplementation    ,
42
    Type const &                        aType
43
0
)   : m_aSharedMutex                ( aMutex                    )
44
0
    , m_oContainerWeakReference     ( pContainerImplementation  )
45
0
    , m_pContainerImplementation    ( pContainerImplementation  )
46
0
    , m_aInterfaceType              ( aType                     )
47
0
{
48
0
}
49
50
OConnectionPointHelper::~OConnectionPointHelper()
51
0
{
52
0
}
53
54
//  XInterface
55
Any SAL_CALL OConnectionPointHelper::queryInterface( const Type& aType )
56
0
{
57
    // Attention:
58
    //  Don't use mutex or guard in this method!!! Is a method of XInterface.
59
60
    // Ask for my own supported interfaces ...
61
0
    Any aReturn ( ::cppu::queryInterface(   aType                                       ,
62
0
                                            static_cast< XConnectionPoint*  > ( this )
63
0
                                        )
64
0
                );
65
66
0
    if (aReturn.hasValue())
67
0
        return aReturn;
68
69
    // If searched interface not supported by this class ...
70
    // ... ask baseclasses.
71
0
    return OWeakObject::queryInterface(aType);
72
0
}
73
74
//  XInterface
75
void SAL_CALL OConnectionPointHelper::acquire() noexcept
76
0
{
77
    // Attention:
78
    //  Don't use mutex or guard in this method!!! Is a method of XInterface.
79
80
    // Forward to baseclass
81
0
    OWeakObject::acquire();
82
0
}
83
84
//  XInterface
85
void SAL_CALL OConnectionPointHelper::release() noexcept
86
0
{
87
    // Attention:
88
    //  Don't use mutex or guard in this method!!! Is a method of XInterface.
89
90
    // Forward to baseclass
91
0
    OWeakObject::release();
92
0
}
93
94
//  XConnectionPoint
95
96
Type SAL_CALL OConnectionPointHelper::getConnectionType()
97
0
{
98
    // Ready for multithreading
99
0
    MutexGuard aGuard( m_aSharedMutex );
100
101
    // Set default return value, if method failed.
102
0
    if ( !impl_LockContainer() )
103
0
    {
104
0
        throw RuntimeException(u"Container does not exist!"_ustr);
105
0
    }
106
107
    // If container reference valid, return right type of supported interfaces of THIS connectionpoint.
108
0
    Type aReturnType = m_aInterfaceType;
109
    // Don't forget this!
110
0
    impl_UnlockContainer();
111
112
0
    return aReturnType;
113
0
}
114
115
//  XConnectionPoint
116
117
Reference< XConnectionPointContainer > SAL_CALL OConnectionPointHelper::getConnectionPointContainer()
118
0
{
119
    // Ready for multithreading
120
0
    MutexGuard aGuard( m_aSharedMutex );
121
    // Convert weakreference to correct uno3-reference and return value. It can be NULL, if container destroyed!
122
0
    return Reference< XConnectionPointContainer >( m_oContainerWeakReference.get(), UNO_QUERY );
123
0
}
124
125
//  XConnectionPoint
126
127
void SAL_CALL OConnectionPointHelper::advise( const Reference< XInterface >& xListener )
128
0
{
129
    // Ready for multithreading
130
0
    MutexGuard aGuard( m_aSharedMutex );
131
132
    // If type of listener not the same for this special container ...
133
0
    Any aCheckType = xListener->queryInterface( m_aInterfaceType );
134
0
    if ( aCheckType.hasValue() )
135
0
    {
136
        // ... throw an exception.
137
0
        throw InvalidListenerException();
138
0
    }
139
140
    // ListenerExistException is obsolete!?
141
    // It's the same container for XConnectionPointContainer and XConnectionPoint. But only here we must control, if a listener already exist!?
142
    // You can add a listener more than one time at XConnectionPointContainer, but here only one ...
143
144
    // Operation is permitted only, if reference to container is valid!
145
0
    if ( !impl_LockContainer() )
146
0
    {
147
0
        throw RuntimeException(u"Container does not exist!"_ustr);
148
0
    }
149
    // Forward it to OConnectionPointHelperContainer!
150
0
    m_pContainerImplementation->advise( m_aInterfaceType, xListener );
151
    // Don't forget this!
152
0
    impl_UnlockContainer();
153
0
}
154
155
//  XConnectionPoint
156
157
void SAL_CALL OConnectionPointHelper::unadvise( const Reference< XInterface >& xListener )
158
0
{
159
    // Ready for multithreading
160
0
    MutexGuard aGuard( m_aSharedMutex );
161
    // Operation is permitted only, if reference to container is valid!
162
0
    if ( !impl_LockContainer() )
163
0
    {
164
0
        throw RuntimeException(u"Container does not exist!"_ustr);
165
166
0
    }
167
    // Forward it to OConnectionPointHelperContainer!
168
0
    m_pContainerImplementation->unadvise( m_aInterfaceType, xListener );
169
    // Don't forget this!
170
0
    impl_UnlockContainer();
171
0
}
172
173
//  XConnectionPoint
174
175
Sequence< Reference< XInterface > > SAL_CALL OConnectionPointHelper::getConnections()
176
0
{
177
    // Ready for multithreading
178
0
    MutexGuard aGuard( m_aSharedMutex );
179
    // Operation is permitted only, if reference to container is valid!
180
0
    if ( !impl_LockContainer() )
181
0
    {
182
0
        throw RuntimeException(u"Container does not exist!"_ustr);
183
0
    }
184
    // Set default return value, if method failed.
185
0
    Sequence< Reference< XInterface > > seqReturnConnections;
186
    // Get reference to private member of OConnectionPointHelperContainer!
187
0
    comphelper::OMultiTypeInterfaceContainerHelper2& aSharedContainer = m_pContainerImplementation->impl_getMultiTypeContainer();
188
    // Get pointer to specialized container which hold all interfaces of searched type.
189
0
    comphelper::OInterfaceContainerHelper2* pSpecialContainer = aSharedContainer.getContainer( m_aInterfaceType );
190
    // Get elements of searched type, if some else exist.
191
0
    if ( pSpecialContainer != nullptr )
192
0
    {
193
0
        seqReturnConnections = comphelper::containerToSequence(pSpecialContainer->getElements());
194
0
    }
195
    // Don't forget this!
196
0
    impl_UnlockContainer();
197
198
0
    return seqReturnConnections;
199
0
}
200
201
//  private method
202
203
bool OConnectionPointHelper::impl_LockContainer()
204
0
{
205
    // Convert weakreference to hard uno3-reference and return state.
206
    // If this reference different from NULL, there exist a hard reference to container. Container-instance can't be destroyed.
207
    // Don't forget to "unlock" this reference!
208
0
    m_xLock = m_oContainerWeakReference.get();
209
0
    return m_xLock.is();
210
0
}
211
212
//  private method
213
214
void OConnectionPointHelper::impl_UnlockContainer()
215
0
{
216
    // Free hard uno3-reference to container.
217
    // see also "impl_LockContainer()"
218
0
    m_xLock.clear();
219
0
}
220
221
}   // namespace unocontrols
222
223
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */