Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / dtrans / source / generic / clipboardmanager.cxx
blob0c5dd396fec986659353c724341d28f3394173f0
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 <cppuhelper/supportsservice.hxx>
26 #include <comphelper/sequence.hxx>
28 using namespace com::sun::star::container;
29 using namespace com::sun::star::datatransfer;
30 using namespace com::sun::star::datatransfer::clipboard;
31 using namespace com::sun::star::lang;
32 using namespace com::sun::star::uno;
33 using namespace cppu;
34 using namespace osl;
35 using namespace std;
37 using ::dtrans::ClipboardManager;
39 ClipboardManager::ClipboardManager():
40 WeakComponentImplHelper< XClipboardManager, XEventListener, XServiceInfo > (m_aMutex),
41 m_aDefaultName(OUString("default"))
45 ClipboardManager::~ClipboardManager()
49 OUString SAL_CALL ClipboardManager::getImplementationName( )
51 return CLIPBOARDMANAGER_IMPLEMENTATION_NAME;
54 sal_Bool SAL_CALL ClipboardManager::supportsService( const OUString& ServiceName )
56 return cppu::supportsService(this, ServiceName);
59 Sequence< OUString > SAL_CALL ClipboardManager::getSupportedServiceNames( )
61 return ClipboardManager_getSupportedServiceNames();
64 Reference< XClipboard > SAL_CALL ClipboardManager::getClipboard( const OUString& aName )
66 MutexGuard aGuard(m_aMutex);
68 // object is disposed already
69 if (rBHelper.bDisposed)
70 throw DisposedException("object is disposed.",
71 static_cast < XClipboardManager * > (this));
73 ClipboardMap::iterator iter =
74 m_aClipboardMap.find(aName.getLength() ? aName : m_aDefaultName);
76 if (iter != m_aClipboardMap.end())
77 return iter->second;
79 throw NoSuchElementException(aName, static_cast < XClipboardManager * > (this));
82 void SAL_CALL ClipboardManager::addClipboard( const Reference< XClipboard >& xClipboard )
84 OSL_ASSERT(xClipboard.is());
86 // check parameter
87 if (!xClipboard.is())
88 throw IllegalArgumentException("empty reference",
89 static_cast < XClipboardManager * > (this), 1);
91 // the name "default" is reserved for internal use
92 OUString aName = xClipboard->getName();
93 if ( m_aDefaultName == aName )
94 throw IllegalArgumentException("name reserved",
95 static_cast < XClipboardManager * > (this), 1);
97 // try to add new clipboard to the list
98 ClearableMutexGuard aGuard(m_aMutex);
99 if (!rBHelper.bDisposed && !rBHelper.bInDispose)
101 pair< const OUString, Reference< XClipboard > > value (
102 aName.getLength() ? aName : m_aDefaultName,
103 xClipboard );
105 pair< ClipboardMap::iterator, bool > p = m_aClipboardMap.insert(value);
106 aGuard.clear();
108 // insert failed, element must exist already
109 if (!p.second)
110 throw ElementExistException(aName, static_cast < XClipboardManager * > (this));
112 // request disposing notifications
113 Reference< XComponent > xComponent(xClipboard, UNO_QUERY);
114 if (xComponent.is())
115 xComponent->addEventListener(static_cast < XEventListener * > (this));
119 void SAL_CALL ClipboardManager::removeClipboard( const OUString& aName )
121 MutexGuard aGuard(m_aMutex);
122 if (!rBHelper.bDisposed)
123 m_aClipboardMap.erase(aName.getLength() ? aName : m_aDefaultName );
126 Sequence< OUString > SAL_CALL ClipboardManager::listClipboardNames()
128 MutexGuard aGuard(m_aMutex);
130 if (rBHelper.bDisposed)
131 throw DisposedException("object is disposed.",
132 static_cast < XClipboardManager * > (this));
134 if (rBHelper.bInDispose)
135 return Sequence< OUString > ();
137 return comphelper::mapKeysToSequence(m_aClipboardMap);
140 void SAL_CALL ClipboardManager::dispose()
142 ClearableMutexGuard aGuard( rBHelper.rMutex );
143 if (!rBHelper.bDisposed && !rBHelper.bInDispose)
145 rBHelper.bInDispose = true;
146 aGuard.clear();
148 // give everyone a chance to save his clipboard instance
149 EventObject aEvt(static_cast < XClipboardManager * > (this));
150 rBHelper.aLC.disposeAndClear( aEvt );
152 // removeClipboard is still allowed here, so make a copy of the
153 // list (to ensure integrity) and clear the original.
154 ClearableMutexGuard aGuard2( rBHelper.rMutex );
155 ClipboardMap aCopy(m_aClipboardMap);
156 m_aClipboardMap.clear();
157 aGuard2.clear();
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.
177 rBHelper.bDisposed = true;
178 rBHelper.bInDispose = false;
182 void SAL_CALL ClipboardManager::disposing( const EventObject& event )
184 Reference < XClipboard > xClipboard(event.Source, UNO_QUERY);
186 if (xClipboard.is())
187 removeClipboard(xClipboard->getName());
190 Reference< XInterface > ClipboardManager_createInstance(
191 const Reference< XMultiServiceFactory > & /*xMultiServiceFactory*/)
193 return Reference < XInterface >(static_cast<OWeakObject *>(new ClipboardManager()));
196 Sequence< OUString > ClipboardManager_getSupportedServiceNames()
198 Sequence < OUString > SupportedServicesNames { "com.sun.star.datatransfer.clipboard.ClipboardManager" };
199 return SupportedServicesNames;
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */