nss: upgrade to release 3.73
[LibreOffice.git] / vcl / unx / generic / dtrans / X11_clipboard.cxx
blob17104ca75ecadc1da467a4b5f5603ccc0267a38e
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 <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
29 #include <stdio.h>
30 #endif
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;
37 using namespace cppu;
38 using namespace osl;
39 using namespace x11;
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="
52 << this << ").");
53 #endif
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);
64 else
66 rManager.registerHandler(XA_PRIMARY, *cb);
67 rManager.registerHandler(rManager.getAtom("CLIPBOARD"), *cb);
69 return cb.get();
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="
78 << this
79 << ", Selection=\""
80 << m_xSelectionManager->getString( m_aSelection )
81 << "\").");
82 #endif
84 if( m_aSelection != None )
85 m_xSelectionManager->deregisterHandler( m_aSelection );
86 else
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).");
100 #endif
101 ::std::vector< Reference< XClipboardListener > > listeners( m_aListeners );
102 aGuard.clear();
104 ClipboardEvent aEvent( static_cast<OWeakObject*>(this), m_aContents);
105 for (auto const& listener : listeners)
107 if( listener.is() )
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 );
121 // clear members
122 m_aOwner.clear();
123 m_aContents.clear();
125 // release the mutex
126 aGuard.clear();
128 // inform previous owner of lost ownership
129 if ( xOwner.is() )
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 );
139 return m_aContents;
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;
155 aGuard.clear();
157 // for now request ownership for both selections
158 if( m_aSelection != None )
159 m_xSelectionManager->requestOwnership( m_aSelection );
160 else
162 m_xSelectionManager->requestOwnership( XA_PRIMARY );
163 m_xSelectionManager->requestOwnership( m_xSelectionManager->getAtom( "CLIPBOARD" ) );
166 // notify old owner on loss of ownership
167 if( oldOwner.is() )
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()
203 clearContents();
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: */