LibreOffice Module dtrans (master)  1
clipboardmanager.cxx
Go to the documentation of this file.
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 #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>
26 #include <comphelper/sequence.hxx>
27 
28 using namespace com::sun::star::container;
29 using namespace com::sun::star::datatransfer;
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;
36 
37 using ::dtrans::ClipboardManager;
38 
39 ClipboardManager::ClipboardManager():
40  WeakComponentImplHelper< XClipboardManager, XEventListener, XServiceInfo > (m_aMutex),
41  m_aDefaultName(OUString("default"))
42 {
43 }
44 
46 {
47 }
48 
50 {
52 }
53 
54 sal_Bool SAL_CALL ClipboardManager::supportsService( const OUString& ServiceName )
55 {
56  return cppu::supportsService(this, ServiceName);
57 }
58 
60 {
62 }
63 
65 {
66  MutexGuard aGuard(m_aMutex);
67 
68  // object is disposed already
69  if (rBHelper.bDisposed)
70  throw DisposedException("object is disposed.",
71  static_cast < XClipboardManager * > (this));
72 
73  ClipboardMap::iterator iter =
74  m_aClipboardMap.find(aName.getLength() ? aName : m_aDefaultName);
75 
76  if (iter != m_aClipboardMap.end())
77  return iter->second;
78 
79  throw NoSuchElementException(aName, static_cast < XClipboardManager * > (this));
80 }
81 
82 void SAL_CALL ClipboardManager::addClipboard( const Reference< XClipboard >& xClipboard )
83 {
84  OSL_ASSERT(xClipboard.is());
85 
86  // check parameter
87  if (!xClipboard.is())
88  throw IllegalArgumentException("empty reference",
89  static_cast < XClipboardManager * > (this), 1);
90 
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);
96 
97  // try to add new clipboard to the list
98  ClearableMutexGuard aGuard(m_aMutex);
99  if (!rBHelper.bDisposed && !rBHelper.bInDispose)
100  {
101  pair< const OUString, Reference< XClipboard > > value (
102  aName.getLength() ? aName : m_aDefaultName,
103  xClipboard );
104 
105  pair< ClipboardMap::iterator, bool > p = m_aClipboardMap.insert(value);
106  aGuard.clear();
107 
108  // insert failed, element must exist already
109  if (!p.second)
110  throw ElementExistException(aName, static_cast < XClipboardManager * > (this));
111 
112  // request disposing notifications
113  Reference< XComponent > xComponent(xClipboard, UNO_QUERY);
114  if (xComponent.is())
115  xComponent->addEventListener(static_cast < XEventListener * > (this));
116  }
117 }
118 
119 void SAL_CALL ClipboardManager::removeClipboard( const OUString& aName )
120 {
121  MutexGuard aGuard(m_aMutex);
122  if (!rBHelper.bDisposed)
123  m_aClipboardMap.erase(aName.getLength() ? aName : m_aDefaultName );
124 }
125 
127 {
128  MutexGuard aGuard(m_aMutex);
129 
130  if (rBHelper.bDisposed)
131  throw DisposedException("object is disposed.",
132  static_cast < XClipboardManager * > (this));
133 
134  if (rBHelper.bInDispose)
135  return Sequence< OUString > ();
136 
138 }
139 
141 {
142  ClearableMutexGuard aGuard( rBHelper.rMutex );
143  if (!rBHelper.bDisposed && !rBHelper.bInDispose)
144  {
145  rBHelper.bInDispose = true;
146  aGuard.clear();
147 
148  // give everyone a chance to save his clipboard instance
149  EventObject aEvt(static_cast < XClipboardManager * > (this));
150  rBHelper.aLC.disposeAndClear( aEvt );
151 
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 );
156  m_aClipboardMap.clear();
157  aGuard2.clear();
158 
159  // dispose all clipboards still in list
160  for (auto const& elem : aCopy)
161  {
162  Reference< XComponent > xComponent(elem.second, UNO_QUERY);
163  if (xComponent.is())
164  {
165  try
166  {
167  xComponent->removeEventListener(static_cast < XEventListener * > (this));
168  xComponent->dispose();
169  }
170  catch (const Exception&)
171  {
172  // exceptions can be safely ignored here.
173  }
174  }
175  }
176 
177  rBHelper.bDisposed = true;
178  rBHelper.bInDispose = false;
179  }
180 }
181 
182 void SAL_CALL ClipboardManager::disposing( const EventObject& event )
183 {
184  Reference < XClipboard > xClipboard(event.Source, UNO_QUERY);
185 
186  if (xClipboard.is())
187  removeClipboard(xClipboard->getName());
188 }
189 
191  const Reference< XMultiServiceFactory > & /*xMultiServiceFactory*/)
192 {
193  return Reference < XInterface >(static_cast<OWeakObject *>(new ClipboardManager()));
194 }
195 
197 {
198  Sequence < OUString > SupportedServicesNames { "com.sun.star.datatransfer.clipboard.ClipboardManager" };
199  return SupportedServicesNames;
200 }
201 
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
osl::Mutex m_aMutex
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
#define CLIPBOARDMANAGER_IMPLEMENTATION_NAME
Reference< XInterface > ClipboardManager_createInstance(const Reference< XMultiServiceFactory > &)
virtual ~ClipboardManager() override
virtual OUString SAL_CALL getImplementationName() override
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
css::uno::Reference< css::lang::XComponent > xComponent
std::map< OUString, css::uno::Reference< css::datatransfer::clipboard::XClipboard > > ClipboardMap
unsigned char sal_Bool
Sequence< OUString > ClipboardManager_getSupportedServiceNames()
virtual void SAL_CALL removeClipboard(const OUString &aName) override
virtual void SAL_CALL addClipboard(const css::uno::Reference< css::datatransfer::clipboard::XClipboard > &xClipboard) override
virtual css::uno::Sequence< OUString > SAL_CALL listClipboardNames() override
virtual void SAL_CALL dispose() override
OUString aName
void * p
Any value
virtual css::uno::Reference< css::datatransfer::clipboard::XClipboard > SAL_CALL getClipboard(const OUString &aName) override
css::uno::Sequence< typename M::key_type > mapKeysToSequence(M const &map)
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override