1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
22 #include <cppuhelper/supportsservice.hxx>
24 using namespace com::sun::star::container
;
25 using namespace com::sun::star::datatransfer
;
26 using namespace com::sun::star::datatransfer::clipboard
;
27 using namespace com::sun::star::lang
;
28 using namespace com::sun::star::uno
;
33 using ::dtrans::ClipboardManager
;
35 ClipboardManager::ClipboardManager():
36 WeakComponentImplHelper3
< XClipboardManager
, XEventListener
, XServiceInfo
> (m_aMutex
),
37 m_aDefaultName(OUString("default"))
41 ClipboardManager::~ClipboardManager()
45 OUString SAL_CALL
ClipboardManager::getImplementationName( )
46 throw(RuntimeException
)
48 return OUString(CLIPBOARDMANAGER_IMPLEMENTATION_NAME
);
51 sal_Bool SAL_CALL
ClipboardManager::supportsService( const OUString
& ServiceName
)
52 throw(RuntimeException
)
54 return cppu::supportsService(this, ServiceName
);
57 Sequence
< OUString
> SAL_CALL
ClipboardManager::getSupportedServiceNames( )
58 throw(RuntimeException
)
60 return ClipboardManager_getSupportedServiceNames();
63 Reference
< XClipboard
> SAL_CALL
ClipboardManager::getClipboard( const OUString
& aName
)
64 throw(NoSuchElementException
, RuntimeException
)
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())
79 throw NoSuchElementException(aName
, static_cast < XClipboardManager
* > (this));
82 void SAL_CALL
ClipboardManager::addClipboard( const Reference
< XClipboard
>& xClipboard
)
83 throw(IllegalArgumentException
, ElementExistException
, RuntimeException
)
85 OSL_ASSERT(xClipboard
.is());
89 throw IllegalArgumentException("empty reference",
90 static_cast < XClipboardManager
* > (this), 1);
92 // the name "default" is reserved for internal use
93 OUString aName
= xClipboard
->getName();
94 if ( m_aDefaultName
== aName
)
95 throw IllegalArgumentException("name reserved",
96 static_cast < XClipboardManager
* > (this), 1);
98 // try to add new clipboard to the list
99 ClearableMutexGuard
aGuard(m_aMutex
);
100 if (!rBHelper
.bDisposed
&& !rBHelper
.bInDispose
)
102 pair
< const OUString
, Reference
< XClipboard
> > value (
103 aName
.getLength() ? aName
: m_aDefaultName
,
106 pair
< ClipboardMap::iterator
, bool > p
= m_aClipboardMap
.insert(value
);
109 // insert failed, element must exist already
111 throw ElementExistException(aName
, static_cast < XClipboardManager
* > (this));
113 // request disposing notifications
114 Reference
< XComponent
> xComponent(xClipboard
, UNO_QUERY
);
116 xComponent
->addEventListener(static_cast < XEventListener
* > (this));
120 void SAL_CALL
ClipboardManager::removeClipboard( const OUString
& aName
)
121 throw(RuntimeException
)
123 MutexGuard
aGuard(m_aMutex
);
124 if (!rBHelper
.bDisposed
)
125 m_aClipboardMap
.erase(aName
.getLength() ? aName
: m_aDefaultName
);
128 Sequence
< OUString
> SAL_CALL
ClipboardManager::listClipboardNames()
129 throw(RuntimeException
)
131 MutexGuard
aGuard(m_aMutex
);
133 if (rBHelper
.bDisposed
)
134 throw DisposedException("object is disposed.",
135 static_cast < XClipboardManager
* > (this));
137 if (rBHelper
.bInDispose
)
138 return Sequence
< OUString
> ();
140 Sequence
< OUString
> aRet(m_aClipboardMap
.size());
141 ClipboardMap::iterator iter
= m_aClipboardMap
.begin();
142 ClipboardMap::iterator imax
= m_aClipboardMap
.end();
144 for (sal_Int32 n
= 0; iter
!= imax
; ++iter
)
145 aRet
[n
++] = iter
->first
;
150 void SAL_CALL
ClipboardManager::dispose()
151 throw(RuntimeException
)
153 ClearableMutexGuard
aGuard( rBHelper
.rMutex
);
154 if (!rBHelper
.bDisposed
&& !rBHelper
.bInDispose
)
156 rBHelper
.bInDispose
= sal_True
;
159 // give everyone a chance to save his clipboard instance
160 EventObject
aEvt(static_cast < XClipboardManager
* > (this));
161 rBHelper
.aLC
.disposeAndClear( aEvt
);
163 // removeClipboard is still allowed here, so make a copy of the
164 // list (to ensure integrety) and clear the original.
165 ClearableMutexGuard
aGuard2( rBHelper
.rMutex
);
166 ClipboardMap
aCopy(m_aClipboardMap
);
167 m_aClipboardMap
.clear();
170 // dispose all clipboards still in list
171 ClipboardMap::iterator iter
= aCopy
.begin();
172 ClipboardMap::iterator imax
= aCopy
.end();
174 for (; iter
!= imax
; ++iter
)
176 Reference
< XComponent
> xComponent(iter
->second
, UNO_QUERY
);
181 xComponent
->removeEventListener(static_cast < XEventListener
* > (this));
182 xComponent
->dispose();
184 catch (const Exception
&)
186 // exceptions can be safely ignored here.
191 rBHelper
.bDisposed
= sal_True
;
192 rBHelper
.bInDispose
= sal_False
;
196 void SAL_CALL
ClipboardManager::disposing( const EventObject
& event
)
197 throw(RuntimeException
)
199 Reference
< XClipboard
> xClipboard(event
.Source
, UNO_QUERY
);
202 removeClipboard(xClipboard
->getName());
205 Reference
< XInterface
> SAL_CALL
ClipboardManager_createInstance(
206 const Reference
< XMultiServiceFactory
> & /*xMultiServiceFactory*/)
208 return Reference
< XInterface
>( ( OWeakObject
* ) new ClipboardManager());
211 Sequence
< OUString
> SAL_CALL
ClipboardManager_getSupportedServiceNames()
213 Sequence
< OUString
> SupportedServicesNames( 1 );
214 SupportedServicesNames
[0] =
215 OUString("com.sun.star.datatransfer.clipboard.ClipboardManager");
216 return SupportedServicesNames
;
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */