1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: clipboardmanager.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dtrans.hxx"
34 #include <clipboardmanager.hxx>
35 #include <com/sun/star/lang/DisposedException.hpp>
37 using namespace com::sun::star::container
;
38 using namespace com::sun::star::datatransfer
;
39 using namespace com::sun::star::datatransfer::clipboard
;
40 using namespace com::sun::star::lang
;
41 using namespace com::sun::star::uno
;
46 using ::dtrans::ClipboardManager
;
47 using ::rtl::OUString
;
49 // ------------------------------------------------------------------------
51 ClipboardManager::ClipboardManager():
52 WeakComponentImplHelper3
< XClipboardManager
, XEventListener
, XServiceInfo
> (m_aMutex
),
53 m_aDefaultName(OUString::createFromAscii("default"))
57 // ------------------------------------------------------------------------
59 ClipboardManager::~ClipboardManager()
63 // ------------------------------------------------------------------------
65 OUString SAL_CALL
ClipboardManager::getImplementationName( )
66 throw(RuntimeException
)
68 return OUString::createFromAscii(CLIPBOARDMANAGER_IMPLEMENTATION_NAME
);
71 // ------------------------------------------------------------------------
73 sal_Bool SAL_CALL
ClipboardManager::supportsService( const OUString
& ServiceName
)
74 throw(RuntimeException
)
76 Sequence
< OUString
> SupportedServicesNames
= ClipboardManager_getSupportedServiceNames();
78 for ( sal_Int32 n
= 0, nmax
= SupportedServicesNames
.getLength(); n
< nmax
; n
++ )
79 if (SupportedServicesNames
[n
].compareTo(ServiceName
) == 0)
85 // ------------------------------------------------------------------------
87 Sequence
< OUString
> SAL_CALL
ClipboardManager::getSupportedServiceNames( )
88 throw(RuntimeException
)
90 return ClipboardManager_getSupportedServiceNames();
93 // ------------------------------------------------------------------------
95 Reference
< XClipboard
> SAL_CALL
ClipboardManager::getClipboard( const OUString
& aName
)
96 throw(NoSuchElementException
, RuntimeException
)
98 MutexGuard
aGuard(m_aMutex
);
100 // object is disposed already
101 if (rBHelper
.bDisposed
)
102 throw DisposedException(OUString::createFromAscii("object is disposed."),
103 static_cast < XClipboardManager
* > (this));
105 ClipboardMap::iterator iter
=
106 m_aClipboardMap
.find(aName
.getLength() ? aName
: m_aDefaultName
);
108 if (iter
!= m_aClipboardMap
.end())
111 throw NoSuchElementException(aName
, static_cast < XClipboardManager
* > (this));
114 // ------------------------------------------------------------------------
116 void SAL_CALL
ClipboardManager::addClipboard( const Reference
< XClipboard
>& xClipboard
)
117 throw(IllegalArgumentException
, ElementExistException
, RuntimeException
)
119 OSL_ASSERT(xClipboard
.is());
122 if (!xClipboard
.is())
123 throw IllegalArgumentException(OUString::createFromAscii("empty reference"),
124 static_cast < XClipboardManager
* > (this), 1);
126 // the name "default" is reserved for internal use
127 OUString aName
= xClipboard
->getName();
128 if (m_aDefaultName
.compareTo(aName
) == 0)
129 throw IllegalArgumentException(OUString::createFromAscii("name reserved"),
130 static_cast < XClipboardManager
* > (this), 1);
132 // try to add new clipboard to the list
133 ClearableMutexGuard
aGuard(m_aMutex
);
134 if (!rBHelper
.bDisposed
&& !rBHelper
.bInDispose
)
136 pair
< const OUString
, Reference
< XClipboard
> > value (
137 aName
.getLength() ? aName
: m_aDefaultName
,
140 pair
< ClipboardMap::iterator
, bool > p
= m_aClipboardMap
.insert(value
);
143 // insert failed, element must exist already
145 throw ElementExistException(aName
, static_cast < XClipboardManager
* > (this));
147 // request disposing notifications
148 Reference
< XComponent
> xComponent(xClipboard
, UNO_QUERY
);
150 xComponent
->addEventListener(static_cast < XEventListener
* > (this));
154 // ------------------------------------------------------------------------
156 void SAL_CALL
ClipboardManager::removeClipboard( const OUString
& aName
)
157 throw(RuntimeException
)
159 MutexGuard
aGuard(m_aMutex
);
160 if (!rBHelper
.bDisposed
)
161 m_aClipboardMap
.erase(aName
.getLength() ? aName
: m_aDefaultName
);
164 // ------------------------------------------------------------------------
166 Sequence
< OUString
> SAL_CALL
ClipboardManager::listClipboardNames()
167 throw(RuntimeException
)
169 MutexGuard
aGuard(m_aMutex
);
171 if (rBHelper
.bDisposed
)
172 throw DisposedException(OUString::createFromAscii("object is disposed."),
173 static_cast < XClipboardManager
* > (this));
175 if (rBHelper
.bInDispose
)
176 return Sequence
< OUString
> ();
178 Sequence
< OUString
> aRet(m_aClipboardMap
.size());
179 ClipboardMap::iterator iter
= m_aClipboardMap
.begin();
180 ClipboardMap::iterator imax
= m_aClipboardMap
.end();
182 for (sal_Int32 n
= 0; iter
!= imax
; iter
++)
183 aRet
[n
++] = iter
->first
;
188 // ------------------------------------------------------------------------
190 void SAL_CALL
ClipboardManager::dispose()
191 throw(RuntimeException
)
193 ClearableMutexGuard
aGuard( rBHelper
.rMutex
);
194 if (!rBHelper
.bDisposed
&& !rBHelper
.bInDispose
)
196 rBHelper
.bInDispose
= sal_True
;
199 // give everyone a chance to save his clipboard instance
200 EventObject
aEvt(static_cast < XClipboardManager
* > (this));
201 rBHelper
.aLC
.disposeAndClear( aEvt
);
203 // removeClipboard is still allowed here, so make a copy of the
204 // list (to ensure integrety) and clear the original.
205 ClearableMutexGuard
aGuard2( rBHelper
.rMutex
);
206 ClipboardMap
aCopy(m_aClipboardMap
);
207 m_aClipboardMap
.clear();
210 // dispose all clipboards still in list
211 ClipboardMap::iterator iter
= aCopy
.begin();
212 ClipboardMap::iterator imax
= aCopy
.end();
214 for (; iter
!= imax
; iter
++)
216 Reference
< XComponent
> xComponent(iter
->second
, UNO_QUERY
);
221 xComponent
->removeEventListener(static_cast < XEventListener
* > (this));
222 xComponent
->dispose();
227 // exceptions can be safely ignored here.
232 rBHelper
.bDisposed
= sal_True
;
233 rBHelper
.bInDispose
= sal_False
;
237 // ------------------------------------------------------------------------
239 void SAL_CALL
ClipboardManager::disposing( const EventObject
& event
)
240 throw(RuntimeException
)
242 Reference
< XClipboard
> xClipboard(event
.Source
, UNO_QUERY
);
245 removeClipboard(xClipboard
->getName());
248 // ------------------------------------------------------------------------
250 Reference
< XInterface
> SAL_CALL
ClipboardManager_createInstance(
251 const Reference
< XMultiServiceFactory
> & /*xMultiServiceFactory*/)
253 return Reference
< XInterface
>( ( OWeakObject
* ) new ClipboardManager());
256 // ------------------------------------------------------------------------
258 Sequence
< OUString
> SAL_CALL
ClipboardManager_getSupportedServiceNames()
260 Sequence
< OUString
> SupportedServicesNames( 1 );
261 SupportedServicesNames
[0] =
262 OUString::createFromAscii("com.sun.star.datatransfer.clipboard.ClipboardManager");
263 return SupportedServicesNames
;