Line data Source code
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 <comphelper/SelectionMultiplex.hxx>
22 : #include <osl/diagnose.h>
23 :
24 :
25 : namespace comphelper
26 : {
27 :
28 :
29 : using namespace ::com::sun::star::uno;
30 : using namespace ::com::sun::star::lang;
31 : using namespace ::com::sun::star::view;
32 :
33 0 : OSelectionChangeListener::~OSelectionChangeListener()
34 : {
35 0 : if (m_pAdapter)
36 0 : m_pAdapter->dispose();
37 0 : }
38 :
39 :
40 0 : void OSelectionChangeListener::_disposing(const EventObject&)
41 : throw (RuntimeException, std::exception)
42 : {
43 : // nothing to do here
44 0 : }
45 :
46 :
47 0 : void OSelectionChangeListener::setAdapter(OSelectionChangeMultiplexer* pAdapter)
48 : {
49 0 : if (m_pAdapter)
50 : {
51 0 : ::osl::MutexGuard aGuard(m_rMutex);
52 0 : m_pAdapter->release();
53 0 : m_pAdapter = NULL;
54 : }
55 :
56 0 : if (pAdapter)
57 : {
58 0 : ::osl::MutexGuard aGuard(m_rMutex);
59 0 : m_pAdapter = pAdapter;
60 0 : m_pAdapter->acquire();
61 : }
62 0 : }
63 :
64 0 : OSelectionChangeMultiplexer::OSelectionChangeMultiplexer(OSelectionChangeListener* _pListener, const Reference< XSelectionSupplier>& _rxSet, bool _bAutoReleaseSet)
65 : :m_xSet(_rxSet)
66 : ,m_pListener(_pListener)
67 : ,m_nLockCount(0)
68 : ,m_bListening(false)
69 0 : ,m_bAutoSetRelease(_bAutoReleaseSet)
70 : {
71 0 : m_pListener->setAdapter(this);
72 0 : osl_atomic_increment(&m_refCount);
73 : {
74 0 : Reference< XSelectionChangeListener> xPreventDelete(this);
75 0 : m_xSet->addSelectionChangeListener(xPreventDelete);
76 : }
77 0 : osl_atomic_decrement(&m_refCount);
78 0 : }
79 :
80 :
81 0 : OSelectionChangeMultiplexer::~OSelectionChangeMultiplexer()
82 : {
83 0 : }
84 :
85 :
86 0 : void OSelectionChangeMultiplexer::lock()
87 : {
88 0 : ++m_nLockCount;
89 0 : }
90 :
91 :
92 0 : void OSelectionChangeMultiplexer::unlock()
93 : {
94 0 : --m_nLockCount;
95 0 : }
96 :
97 :
98 0 : void OSelectionChangeMultiplexer::dispose()
99 : {
100 0 : if (m_bListening)
101 : {
102 0 : Reference< XSelectionChangeListener> xPreventDelete(this);
103 :
104 0 : m_xSet->removeSelectionChangeListener(xPreventDelete);
105 :
106 0 : m_pListener->setAdapter(NULL);
107 :
108 0 : m_pListener = NULL;
109 0 : m_bListening = false;
110 :
111 0 : if (m_bAutoSetRelease)
112 0 : m_xSet = NULL;
113 : }
114 0 : }
115 :
116 : // XEventListener
117 :
118 0 : void SAL_CALL OSelectionChangeMultiplexer::disposing( const EventObject& _rSource) throw( RuntimeException, std::exception)
119 : {
120 0 : if (m_pListener)
121 : {
122 : // tell the listener
123 0 : if (!locked())
124 0 : m_pListener->_disposing(_rSource);
125 : // disconnect the listener
126 0 : if (m_pListener) // may have been reset whilest calling into _disposing
127 0 : m_pListener->setAdapter(NULL);
128 : }
129 :
130 0 : m_pListener = NULL;
131 0 : m_bListening = false;
132 :
133 0 : if (m_bAutoSetRelease)
134 0 : m_xSet = NULL;
135 0 : }
136 :
137 : // XSelectionChangeListener
138 :
139 0 : void SAL_CALL OSelectionChangeMultiplexer::selectionChanged( const EventObject& _rEvent ) throw( RuntimeException, std::exception)
140 : {
141 0 : if (m_pListener && !locked())
142 0 : m_pListener->_selectionChanged(_rEvent);
143 0 : }
144 :
145 : }
146 :
147 :
148 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|