bump product version to 4.1.6.2
[LibreOffice.git] / dtrans / source / generic / clipboardmanager.cxx
blob696ca9ca030aded2c4f644c0d8aed7a42ad82650
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/lang/DisposedException.hpp>
23 using namespace com::sun::star::container;
24 using namespace com::sun::star::datatransfer;
25 using namespace com::sun::star::datatransfer::clipboard;
26 using namespace com::sun::star::lang;
27 using namespace com::sun::star::uno;
28 using namespace cppu;
29 using namespace osl;
30 using namespace std;
32 using ::dtrans::ClipboardManager;
34 // ------------------------------------------------------------------------
36 ClipboardManager::ClipboardManager():
37 WeakComponentImplHelper3< XClipboardManager, XEventListener, XServiceInfo > (m_aMutex),
38 m_aDefaultName(OUString("default"))
42 // ------------------------------------------------------------------------
44 ClipboardManager::~ClipboardManager()
48 // ------------------------------------------------------------------------
50 OUString SAL_CALL ClipboardManager::getImplementationName( )
51 throw(RuntimeException)
53 return OUString(CLIPBOARDMANAGER_IMPLEMENTATION_NAME);
56 // ------------------------------------------------------------------------
58 sal_Bool SAL_CALL ClipboardManager::supportsService( const OUString& ServiceName )
59 throw(RuntimeException)
61 Sequence < OUString > SupportedServicesNames = ClipboardManager_getSupportedServiceNames();
63 for ( sal_Int32 n = 0, nmax = SupportedServicesNames.getLength(); n < nmax; n++ )
64 if ( SupportedServicesNames[n] == ServiceName )
65 return sal_True;
67 return sal_False;
70 // ------------------------------------------------------------------------
72 Sequence< OUString > SAL_CALL ClipboardManager::getSupportedServiceNames( )
73 throw(RuntimeException)
75 return ClipboardManager_getSupportedServiceNames();
78 // ------------------------------------------------------------------------
80 Reference< XClipboard > SAL_CALL ClipboardManager::getClipboard( const OUString& aName )
81 throw(NoSuchElementException, RuntimeException)
83 MutexGuard aGuard(m_aMutex);
85 // object is disposed already
86 if (rBHelper.bDisposed)
87 throw DisposedException(OUString("object is disposed."),
88 static_cast < XClipboardManager * > (this));
90 ClipboardMap::iterator iter =
91 m_aClipboardMap.find(aName.getLength() ? aName : m_aDefaultName);
93 if (iter != m_aClipboardMap.end())
94 return iter->second;
96 throw NoSuchElementException(aName, static_cast < XClipboardManager * > (this));
99 // ------------------------------------------------------------------------
101 void SAL_CALL ClipboardManager::addClipboard( const Reference< XClipboard >& xClipboard )
102 throw(IllegalArgumentException, ElementExistException, RuntimeException)
104 OSL_ASSERT(xClipboard.is());
106 // check parameter
107 if (!xClipboard.is())
108 throw IllegalArgumentException(OUString("empty reference"),
109 static_cast < XClipboardManager * > (this), 1);
111 // the name "default" is reserved for internal use
112 OUString aName = xClipboard->getName();
113 if ( m_aDefaultName == aName )
114 throw IllegalArgumentException(OUString("name reserved"),
115 static_cast < XClipboardManager * > (this), 1);
117 // try to add new clipboard to the list
118 ClearableMutexGuard aGuard(m_aMutex);
119 if (!rBHelper.bDisposed && !rBHelper.bInDispose)
121 pair< const OUString, Reference< XClipboard > > value (
122 aName.getLength() ? aName : m_aDefaultName,
123 xClipboard );
125 pair< ClipboardMap::iterator, bool > p = m_aClipboardMap.insert(value);
126 aGuard.clear();
128 // insert failed, element must exist already
129 if (!p.second)
130 throw ElementExistException(aName, static_cast < XClipboardManager * > (this));
132 // request disposing notifications
133 Reference< XComponent > xComponent(xClipboard, UNO_QUERY);
134 if (xComponent.is())
135 xComponent->addEventListener(static_cast < XEventListener * > (this));
139 // ------------------------------------------------------------------------
141 void SAL_CALL ClipboardManager::removeClipboard( const OUString& aName )
142 throw(RuntimeException)
144 MutexGuard aGuard(m_aMutex);
145 if (!rBHelper.bDisposed)
146 m_aClipboardMap.erase(aName.getLength() ? aName : m_aDefaultName );
149 // ------------------------------------------------------------------------
151 Sequence< OUString > SAL_CALL ClipboardManager::listClipboardNames()
152 throw(RuntimeException)
154 MutexGuard aGuard(m_aMutex);
156 if (rBHelper.bDisposed)
157 throw DisposedException(OUString("object is disposed."),
158 static_cast < XClipboardManager * > (this));
160 if (rBHelper.bInDispose)
161 return Sequence< OUString > ();
163 Sequence< OUString > aRet(m_aClipboardMap.size());
164 ClipboardMap::iterator iter = m_aClipboardMap.begin();
165 ClipboardMap::iterator imax = m_aClipboardMap.end();
167 for (sal_Int32 n = 0; iter != imax; ++iter)
168 aRet[n++] = iter->first;
170 return aRet;
173 // ------------------------------------------------------------------------
175 void SAL_CALL ClipboardManager::dispose()
176 throw(RuntimeException)
178 ClearableMutexGuard aGuard( rBHelper.rMutex );
179 if (!rBHelper.bDisposed && !rBHelper.bInDispose)
181 rBHelper.bInDispose = sal_True;
182 aGuard.clear();
184 // give everyone a chance to save his clipboard instance
185 EventObject aEvt(static_cast < XClipboardManager * > (this));
186 rBHelper.aLC.disposeAndClear( aEvt );
188 // removeClipboard is still allowed here, so make a copy of the
189 // list (to ensure integrety) and clear the original.
190 ClearableMutexGuard aGuard2( rBHelper.rMutex );
191 ClipboardMap aCopy(m_aClipboardMap);
192 m_aClipboardMap.clear();
193 aGuard2.clear();
195 // dispose all clipboards still in list
196 ClipboardMap::iterator iter = aCopy.begin();
197 ClipboardMap::iterator imax = aCopy.end();
199 for (; iter != imax; ++iter)
201 Reference< XComponent > xComponent(iter->second, UNO_QUERY);
202 if (xComponent.is())
206 xComponent->removeEventListener(static_cast < XEventListener * > (this));
207 xComponent->dispose();
209 catch (const Exception&)
211 // exceptions can be safely ignored here.
216 rBHelper.bDisposed = sal_True;
217 rBHelper.bInDispose = sal_False;
221 // ------------------------------------------------------------------------
223 void SAL_CALL ClipboardManager::disposing( const EventObject& event )
224 throw(RuntimeException)
226 Reference < XClipboard > xClipboard(event.Source, UNO_QUERY);
228 if (xClipboard.is())
229 removeClipboard(xClipboard->getName());
232 // ------------------------------------------------------------------------
234 Reference< XInterface > SAL_CALL ClipboardManager_createInstance(
235 const Reference< XMultiServiceFactory > & /*xMultiServiceFactory*/)
237 return Reference < XInterface >( ( OWeakObject * ) new ClipboardManager());
240 // ------------------------------------------------------------------------
242 Sequence< OUString > SAL_CALL ClipboardManager_getSupportedServiceNames()
244 Sequence < OUString > SupportedServicesNames( 1 );
245 SupportedServicesNames[0] =
246 OUString("com.sun.star.datatransfer.clipboard.ClipboardManager");
247 return SupportedServicesNames;
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */