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 <osl/diagnose.h>
21 #include "WinClipboard.hxx"
22 #include <com/sun/star/datatransfer/clipboard/ClipboardEvent.hpp>
23 #include <com/sun/star/lang/DisposedException.hpp>
24 #include <com/sun/star/lang/IllegalArgumentException.hpp>
25 #include <cppuhelper/supportsservice.hxx>
26 #include "WinClipbImpl.hxx"
32 using namespace com::sun::star::uno
;
33 using namespace com::sun::star::datatransfer
;
34 using namespace com::sun::star::datatransfer::clipboard
;
35 using namespace com::sun::star::lang
;
37 #define WINCLIPBOARD_IMPL_NAME "com.sun.star.datatransfer.clipboard.ClipboardW32"
41 Sequence
< OUString
> WinClipboard_getSupportedServiceNames()
43 Sequence
< OUString
> aRet
{ "com.sun.star.datatransfer.clipboard.SystemClipboard" };
49 CWinClipboard::CWinClipboard( const Reference
< XComponentContext
>& rxContext
, const OUString
& aClipboardName
) :
50 WeakComponentImplHelper
< XSystemClipboard
, XFlushableClipboard
, XServiceInfo
>( m_aCbListenerMutex
),
51 m_xContext( rxContext
)
53 m_pImpl
.reset( new CWinClipbImpl( aClipboardName
, this ) );
58 // to avoid unnecessary traffic we check first if there is a clipboard
59 // content which was set via setContent, in this case we don't need
60 // to query the content from the clipboard, create a new wrapper object
61 // and so on, we simply return the original XTransferable instead of our
64 Reference
< XTransferable
> SAL_CALL
CWinClipboard::getContents( )
66 MutexGuard
aGuard( m_aMutex
);
68 if ( rBHelper
.bDisposed
)
69 throw DisposedException("object is already disposed",
70 static_cast< XClipboardEx
* >( this ) );
72 if ( nullptr != m_pImpl
.get( ) )
73 return m_pImpl
->getContents( );
75 return Reference
< XTransferable
>( );
78 void SAL_CALL
CWinClipboard::setContents( const Reference
< XTransferable
>& xTransferable
,
79 const Reference
< XClipboardOwner
>& xClipboardOwner
)
81 MutexGuard
aGuard( m_aMutex
);
83 if ( rBHelper
.bDisposed
)
84 throw DisposedException("object is already disposed",
85 static_cast< XClipboardEx
* >( this ) );
87 if ( nullptr != m_pImpl
.get( ) )
88 m_pImpl
->setContents( xTransferable
, xClipboardOwner
);
91 OUString SAL_CALL
CWinClipboard::getName( )
93 if ( rBHelper
.bDisposed
)
94 throw DisposedException("object is already disposed",
95 static_cast< XClipboardEx
* >( this ) );
97 if ( nullptr != m_pImpl
.get( ) )
98 return m_pImpl
->getName( );
103 // XFlushableClipboard
105 void SAL_CALL
CWinClipboard::flushClipboard( )
107 MutexGuard
aGuard( m_aMutex
);
109 if ( rBHelper
.bDisposed
)
110 throw DisposedException("object is already disposed",
111 static_cast< XClipboardEx
* >( this ) );
113 if ( nullptr != m_pImpl
.get( ) )
114 m_pImpl
->flushClipboard( );
119 sal_Int8 SAL_CALL
CWinClipboard::getRenderingCapabilities( )
121 if ( rBHelper
.bDisposed
)
122 throw DisposedException("object is already disposed",
123 static_cast< XClipboardEx
* >( this ) );
125 if ( nullptr != m_pImpl
.get( ) )
126 return CWinClipbImpl::getRenderingCapabilities( );
131 // XClipboardNotifier
133 void SAL_CALL
CWinClipboard::addClipboardListener( const Reference
< XClipboardListener
>& listener
)
135 if ( rBHelper
.bDisposed
)
136 throw DisposedException("object is already disposed",
137 static_cast< XClipboardEx
* >( this ) );
139 // check input parameter
140 if ( !listener
.is( ) )
141 throw IllegalArgumentException("empty reference",
142 static_cast< XClipboardEx
* >( this ),
145 rBHelper
.aLC
.addInterface( cppu::UnoType
<decltype(listener
)>::get(), listener
);
148 void SAL_CALL
CWinClipboard::removeClipboardListener( const Reference
< XClipboardListener
>& listener
)
150 if ( rBHelper
.bDisposed
)
151 throw DisposedException("object is already disposed",
152 static_cast< XClipboardEx
* >( this ) );
154 // check input parameter
155 if ( !listener
.is( ) )
156 throw IllegalArgumentException("empty reference",
157 static_cast< XClipboardEx
* >( this ),
160 rBHelper
.aLC
.removeInterface( cppu::UnoType
<decltype(listener
)>::get(), listener
);
163 void CWinClipboard::notifyAllClipboardListener( )
165 if ( !rBHelper
.bDisposed
)
167 ClearableMutexGuard
aGuard( rBHelper
.rMutex
);
168 if ( !rBHelper
.bDisposed
)
172 OInterfaceContainerHelper
* pICHelper
= rBHelper
.aLC
.getContainer(
173 cppu::UnoType
<XClipboardListener
>::get());
179 OInterfaceIteratorHelper
iter(*pICHelper
);
180 Reference
<XTransferable
> rXTransf(m_pImpl
->getContents());
181 ClipboardEvent
aClipbEvent(static_cast<XClipboard
*>(this), rXTransf
);
183 while(iter
.hasMoreElements())
187 Reference
<XClipboardListener
> xCBListener(iter
.next(), UNO_QUERY
);
188 if (xCBListener
.is())
189 xCBListener
->changedContents(aClipbEvent
);
191 catch(RuntimeException
&)
193 OSL_FAIL( "RuntimeException caught" );
197 catch(const css::lang::DisposedException
&)
199 OSL_FAIL("Service Manager disposed");
201 // no further clipboard changed notifications
202 m_pImpl
->unregisterClipboardViewer();
210 // overwritten base class method which will be
211 // called by the base class dispose method
213 void SAL_CALL
CWinClipboard::disposing()
218 // force destruction of the impl class
224 OUString SAL_CALL
CWinClipboard::getImplementationName( )
226 return WINCLIPBOARD_IMPL_NAME
;
229 sal_Bool SAL_CALL
CWinClipboard::supportsService( const OUString
& ServiceName
)
231 return cppu::supportsService(this, ServiceName
);
234 Sequence
< OUString
> SAL_CALL
CWinClipboard::getSupportedServiceNames( )
236 return WinClipboard_getSupportedServiceNames();
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */