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