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 <X11/Xatom.h>
21 #include "X11_clipboard.hxx"
22 #include "X11_transferable.hxx"
23 #include <com/sun/star/datatransfer/clipboard/RenderingCapabilities.hpp>
24 #include <cppuhelper/supportsservice.hxx>
25 #include <rtl/ref.hxx>
26 #include <sal/log.hxx>
28 #if OSL_DEBUG_LEVEL > 1
32 using namespace com::sun::star::datatransfer
;
33 using namespace com::sun::star::datatransfer::clipboard
;
34 using namespace com::sun::star::lang
;
35 using namespace com::sun::star::uno
;
36 using namespace com::sun::star::awt
;
41 X11Clipboard::X11Clipboard( SelectionManager
& rManager
, Atom aSelection
) :
42 ::cppu::WeakComponentImplHelper
<
43 css::datatransfer::clipboard::XSystemClipboard
,
44 css::lang::XServiceInfo
45 >( rManager
.getMutex() ),
47 m_xSelectionManager( &rManager
),
48 m_aSelection( aSelection
)
50 #if OSL_DEBUG_LEVEL > 1
51 SAL_INFO("vcl.unx.dtrans", "creating instance of X11Clipboard (this="
56 css::uno::Reference
<css::datatransfer::clipboard::XClipboard
>
57 X11Clipboard::create( SelectionManager
& rManager
, Atom aSelection
)
59 rtl::Reference
<X11Clipboard
> cb(new X11Clipboard(rManager
, aSelection
));
60 if( aSelection
!= None
)
62 rManager
.registerHandler(aSelection
, *cb
);
66 rManager
.registerHandler(XA_PRIMARY
, *cb
);
67 rManager
.registerHandler(rManager
.getAtom("CLIPBOARD"), *cb
);
72 X11Clipboard::~X11Clipboard()
74 MutexGuard
aGuard( *Mutex::getGlobalMutex() );
76 #if OSL_DEBUG_LEVEL > 1
77 SAL_INFO("vcl.unx.dtrans", "shutting down instance of X11Clipboard (this="
80 << m_xSelectionManager
->getString( m_aSelection
)
84 if( m_aSelection
!= None
)
85 m_xSelectionManager
->deregisterHandler( m_aSelection
);
88 m_xSelectionManager
->deregisterHandler( XA_PRIMARY
);
89 m_xSelectionManager
->deregisterHandler( m_xSelectionManager
->getAtom( "CLIPBOARD" ) );
93 void X11Clipboard::fireChangedContentsEvent()
95 ClearableMutexGuard
aGuard( m_xSelectionManager
->getMutex() );
96 #if OSL_DEBUG_LEVEL > 1
97 SAL_INFO("vcl.unx.dtrans", "X11Clipboard::fireChangedContentsEvent for "
98 << m_xSelectionManager
->getString( m_aSelection
)
99 << " (" << m_aListeners
.size() << " listeners).");
101 ::std::vector
< Reference
< XClipboardListener
> > listeners( m_aListeners
);
104 ClipboardEvent
aEvent( static_cast<OWeakObject
*>(this), m_aContents
);
105 for (auto const& listener
: listeners
)
108 listener
->changedContents(aEvent
);
112 void X11Clipboard::clearContents()
114 ClearableMutexGuard
aGuard(m_xSelectionManager
->getMutex());
115 // protect against deletion during outside call
116 Reference
< XClipboard
> xThis( static_cast<XClipboard
*>(this));
117 // copy member references on stack so they can be called
118 // without having the mutex
119 Reference
< XClipboardOwner
> xOwner( m_aOwner
);
120 Reference
< XTransferable
> xKeepAlive( m_aContents
);
128 // inform previous owner of lost ownership
130 xOwner
->lostOwnership(xThis
, m_aContents
);
133 Reference
< XTransferable
> SAL_CALL
X11Clipboard::getContents()
135 MutexGuard
aGuard(m_xSelectionManager
->getMutex());
137 if( ! m_aContents
.is() )
138 m_aContents
= new X11Transferable( SelectionManager::get(), m_aSelection
);
142 void SAL_CALL
X11Clipboard::setContents(
143 const Reference
< XTransferable
>& xTrans
,
144 const Reference
< XClipboardOwner
>& xClipboardOwner
)
146 // remember old values for callbacks before setting the new ones.
147 ClearableMutexGuard
aGuard(m_xSelectionManager
->getMutex());
149 Reference
< XClipboardOwner
> oldOwner( m_aOwner
);
150 m_aOwner
= xClipboardOwner
;
152 Reference
< XTransferable
> oldContents( m_aContents
);
153 m_aContents
= xTrans
;
157 // for now request ownership for both selections
158 if( m_aSelection
!= None
)
159 m_xSelectionManager
->requestOwnership( m_aSelection
);
162 m_xSelectionManager
->requestOwnership( XA_PRIMARY
);
163 m_xSelectionManager
->requestOwnership( m_xSelectionManager
->getAtom( "CLIPBOARD" ) );
166 // notify old owner on loss of ownership
168 oldOwner
->lostOwnership(static_cast < XClipboard
* > (this), oldContents
);
170 // notify all listeners on content changes
171 fireChangedContentsEvent();
174 OUString SAL_CALL
X11Clipboard::getName()
176 return m_xSelectionManager
->getString( m_aSelection
);
179 sal_Int8 SAL_CALL
X11Clipboard::getRenderingCapabilities()
181 return RenderingCapabilities::Delayed
;
184 void SAL_CALL
X11Clipboard::addClipboardListener( const Reference
< XClipboardListener
>& listener
)
186 MutexGuard
aGuard( m_xSelectionManager
->getMutex() );
187 m_aListeners
.push_back( listener
);
190 void SAL_CALL
X11Clipboard::removeClipboardListener( const Reference
< XClipboardListener
>& listener
)
192 MutexGuard
aGuard( m_xSelectionManager
->getMutex() );
193 m_aListeners
.erase( std::remove(m_aListeners
.begin(), m_aListeners
.end(), listener
), m_aListeners
.end() );
196 Reference
< XTransferable
> X11Clipboard::getTransferable()
198 return getContents();
201 void X11Clipboard::clearTransferable()
206 void X11Clipboard::fireContentsChanged()
208 fireChangedContentsEvent();
211 Reference
< XInterface
> X11Clipboard::getReference() throw()
213 return Reference
< XInterface
>( static_cast< OWeakObject
* >(this) );
216 OUString SAL_CALL
X11Clipboard::getImplementationName( )
218 return X11_CLIPBOARD_IMPLEMENTATION_NAME
;
221 sal_Bool SAL_CALL
X11Clipboard::supportsService( const OUString
& ServiceName
)
223 return cppu::supportsService(this, ServiceName
);
226 Sequence
< OUString
> SAL_CALL
X11Clipboard::getSupportedServiceNames( )
228 return X11Clipboard_getSupportedServiceNames();
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */