bump product version to 5.0.4.1
[LibreOffice.git] / comphelper / source / misc / SelectionMultiplex.cxx
blobbff3e33819faf08518aaf01d6c853b1233d00b66
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 <comphelper/SelectionMultiplex.hxx>
22 #include <osl/diagnose.h>
25 namespace comphelper
29 using namespace ::com::sun::star::uno;
30 using namespace ::com::sun::star::lang;
31 using namespace ::com::sun::star::view;
33 OSelectionChangeListener::~OSelectionChangeListener()
35 if (m_pAdapter)
36 m_pAdapter->dispose();
40 void OSelectionChangeListener::_disposing(const EventObject&)
41 throw (RuntimeException, std::exception)
43 // nothing to do here
47 void OSelectionChangeListener::setAdapter(OSelectionChangeMultiplexer* pAdapter)
49 if (m_pAdapter)
51 ::osl::MutexGuard aGuard(m_rMutex);
52 m_pAdapter->release();
53 m_pAdapter = NULL;
56 if (pAdapter)
58 ::osl::MutexGuard aGuard(m_rMutex);
59 m_pAdapter = pAdapter;
60 m_pAdapter->acquire();
64 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 ,m_bAutoSetRelease(_bAutoReleaseSet)
71 m_pListener->setAdapter(this);
72 osl_atomic_increment(&m_refCount);
74 Reference< XSelectionChangeListener> xPreventDelete(this);
75 m_xSet->addSelectionChangeListener(xPreventDelete);
77 osl_atomic_decrement(&m_refCount);
81 OSelectionChangeMultiplexer::~OSelectionChangeMultiplexer()
86 void OSelectionChangeMultiplexer::lock()
88 ++m_nLockCount;
92 void OSelectionChangeMultiplexer::unlock()
94 --m_nLockCount;
98 void OSelectionChangeMultiplexer::dispose()
100 if (m_bListening)
102 Reference< XSelectionChangeListener> xPreventDelete(this);
104 m_xSet->removeSelectionChangeListener(xPreventDelete);
106 m_pListener->setAdapter(NULL);
108 m_pListener = NULL;
109 m_bListening = false;
111 if (m_bAutoSetRelease)
112 m_xSet = NULL;
116 // XEventListener
118 void SAL_CALL OSelectionChangeMultiplexer::disposing( const EventObject& _rSource) throw( RuntimeException, std::exception)
120 if (m_pListener)
122 // tell the listener
123 if (!locked())
124 m_pListener->_disposing(_rSource);
125 // disconnect the listener
126 if (m_pListener) // may have been reset whilest calling into _disposing
127 m_pListener->setAdapter(NULL);
130 m_pListener = NULL;
131 m_bListening = false;
133 if (m_bAutoSetRelease)
134 m_xSet = NULL;
137 // XSelectionChangeListener
139 void SAL_CALL OSelectionChangeMultiplexer::selectionChanged( const EventObject& _rEvent ) throw( RuntimeException, std::exception)
141 if (m_pListener && !locked())
142 m_pListener->_selectionChanged(_rEvent);
148 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */