bump product version to 7.6.3.2-android
[LibreOffice.git] / vcl / win / dtrans / clipboardmanager.cxx
blobbff5aec49fa901935112883e0a2e013512055c2e
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 .
20 #include "clipboardmanager.hxx"
21 #include <com/sun/star/container/ElementExistException.hpp>
22 #include <com/sun/star/container/NoSuchElementException.hpp>
23 #include <com/sun/star/lang/DisposedException.hpp>
24 #include <com/sun/star/lang/IllegalArgumentException.hpp>
25 #include <com/sun/star/uno/XComponentContext.hpp>
26 #include <cppuhelper/supportsservice.hxx>
27 #include <comphelper/sequence.hxx>
28 #include <rtl/ref.hxx>
30 using namespace com::sun::star::container;
31 using namespace com::sun::star::datatransfer;
32 using namespace com::sun::star::datatransfer::clipboard;
33 using namespace com::sun::star::lang;
34 using namespace com::sun::star::uno;
35 using namespace cppu;
36 using namespace osl;
38 using ::dtrans::ClipboardManager;
40 static std::mutex g_InstanceGuard;
41 static rtl::Reference<ClipboardManager> g_Instance;
42 static bool g_Disposed = false;
45 ClipboardManager::ClipboardManager():
46 m_aDefaultName(OUString("default"))
50 ClipboardManager::~ClipboardManager()
54 OUString SAL_CALL ClipboardManager::getImplementationName( )
56 return "com.sun.star.comp.datatransfer.ClipboardManager";
59 sal_Bool SAL_CALL ClipboardManager::supportsService( const OUString& ServiceName )
61 return cppu::supportsService(this, ServiceName);
64 Sequence< OUString > SAL_CALL ClipboardManager::getSupportedServiceNames( )
66 return { "com.sun.star.datatransfer.clipboard.ClipboardManager" };
69 Reference< XClipboard > SAL_CALL ClipboardManager::getClipboard( const OUString& aName )
71 std::unique_lock aGuard(m_aMutex);
73 // object is disposed already
74 if (m_bDisposed)
75 throw DisposedException("object is disposed.",
76 static_cast < XClipboardManager * > (this));
78 ClipboardMap::iterator iter =
79 m_aClipboardMap.find(aName.getLength() ? aName : m_aDefaultName);
81 if (iter != m_aClipboardMap.end())
82 return iter->second;
84 throw NoSuchElementException(aName, static_cast < XClipboardManager * > (this));
87 void SAL_CALL ClipboardManager::addClipboard( const Reference< XClipboard >& xClipboard )
89 OSL_ASSERT(xClipboard.is());
91 // check parameter
92 if (!xClipboard.is())
93 throw IllegalArgumentException("empty reference",
94 static_cast < XClipboardManager * > (this), 1);
96 // the name "default" is reserved for internal use
97 OUString aName = xClipboard->getName();
98 if ( m_aDefaultName == aName )
99 throw IllegalArgumentException("name reserved",
100 static_cast < XClipboardManager * > (this), 1);
102 // try to add new clipboard to the list
103 std::unique_lock aGuard(m_aMutex);
104 if (!m_bDisposed)
106 std::pair< const OUString, Reference< XClipboard > > value (
107 aName.getLength() ? aName : m_aDefaultName,
108 xClipboard );
110 std::pair< ClipboardMap::iterator, bool > p = m_aClipboardMap.insert(value);
111 aGuard.unlock();
113 // insert failed, element must exist already
114 if (!p.second)
115 throw ElementExistException(aName, static_cast < XClipboardManager * > (this));
117 // request disposing notifications
118 Reference< XComponent > xComponent(xClipboard, UNO_QUERY);
119 if (xComponent.is())
120 xComponent->addEventListener(static_cast < XEventListener * > (this));
124 void SAL_CALL ClipboardManager::removeClipboard( const OUString& aName )
126 std::unique_lock aGuard(m_aMutex);
127 if (!m_bDisposed)
128 m_aClipboardMap.erase(aName.getLength() ? aName : m_aDefaultName );
131 Sequence< OUString > SAL_CALL ClipboardManager::listClipboardNames()
133 std::unique_lock aGuard(m_aMutex);
135 if (m_bDisposed)
136 throw DisposedException("object is disposed.",
137 static_cast < XClipboardManager * > (this));
139 return comphelper::mapKeysToSequence(m_aClipboardMap);
142 void ClipboardManager::disposing(std::unique_lock<std::mutex>& rGuard)
144 rGuard.unlock();
147 std::unique_lock aGuard(g_InstanceGuard);
148 g_Instance.clear();
149 g_Disposed = true;
152 // removeClipboard is still allowed here, so make a copy of the
153 // list (to ensure integrity) and clear the original.
154 rGuard.lock();
155 ClipboardMap aCopy;
156 std::swap(aCopy, m_aClipboardMap);
157 rGuard.unlock();
159 // dispose all clipboards still in list
160 for (auto const& elem : aCopy)
162 Reference< XComponent > xComponent(elem.second, UNO_QUERY);
163 if (xComponent.is())
167 xComponent->removeEventListener(static_cast < XEventListener * > (this));
168 xComponent->dispose();
170 catch (const Exception&)
172 // exceptions can be safely ignored here.
178 void SAL_CALL ClipboardManager::disposing( const EventObject& event )
180 Reference < XClipboard > xClipboard(event.Source, UNO_QUERY);
182 if (xClipboard.is())
183 removeClipboard(xClipboard->getName());
186 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
187 dtrans_ClipboardManager_get_implementation(
188 css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&)
190 std::unique_lock aGuard(g_InstanceGuard);
191 if (g_Disposed)
192 return nullptr;
193 if (!g_Instance)
194 g_Instance.set(new ClipboardManager());
195 return cppu::acquire(g_Instance.get());
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */