remove assert looking for new compatibilityMode DOCX
[LibreOffice.git] / vcl / unx / generic / dtrans / X11_clipboard.cxx
blob1a0e5c685c718d54278696c7336b059b781a0302
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 cppu;
37 using namespace osl;
38 using namespace x11;
40 X11Clipboard::X11Clipboard( SelectionManager& rManager, Atom aSelection ) :
41 ::cppu::WeakComponentImplHelper<
42 css::datatransfer::clipboard::XSystemClipboard,
43 css::lang::XServiceInfo
44 >( rManager.getMutex() ),
46 m_xSelectionManager( &rManager ),
47 m_aSelection( aSelection )
49 #if OSL_DEBUG_LEVEL > 1
50 SAL_INFO("vcl.unx.dtrans", "creating instance of X11Clipboard (this="
51 << this << ").");
52 #endif
55 css::uno::Reference<css::datatransfer::clipboard::XClipboard>
56 X11Clipboard::create( SelectionManager& rManager, Atom aSelection )
58 rtl::Reference<X11Clipboard> cb(new X11Clipboard(rManager, aSelection));
59 if( aSelection != None )
61 rManager.registerHandler(aSelection, *cb);
63 else
65 rManager.registerHandler(XA_PRIMARY, *cb);
66 rManager.registerHandler(rManager.getAtom(u"CLIPBOARD"_ustr), *cb);
68 return cb;
71 X11Clipboard::~X11Clipboard()
73 MutexGuard aGuard( *Mutex::getGlobalMutex() );
75 #if OSL_DEBUG_LEVEL > 1
76 SAL_INFO("vcl.unx.dtrans", "shutting down instance of X11Clipboard (this="
77 << this
78 << ", Selection=\""
79 << m_xSelectionManager->getString( m_aSelection )
80 << "\").");
81 #endif
83 if( m_aSelection != None )
84 m_xSelectionManager->deregisterHandler( m_aSelection );
85 else
87 m_xSelectionManager->deregisterHandler( XA_PRIMARY );
88 m_xSelectionManager->deregisterHandler( m_xSelectionManager->getAtom( u"CLIPBOARD"_ustr ) );
92 void X11Clipboard::fireChangedContentsEvent()
94 ClearableMutexGuard aGuard( m_xSelectionManager->getMutex() );
95 #if OSL_DEBUG_LEVEL > 1
96 SAL_INFO("vcl.unx.dtrans", "X11Clipboard::fireChangedContentsEvent for "
97 << m_xSelectionManager->getString( m_aSelection )
98 << " (" << m_aListeners.size() << " listeners).");
99 #endif
100 ::std::vector< Reference< XClipboardListener > > listeners( m_aListeners );
101 aGuard.clear();
103 ClipboardEvent aEvent(getXWeak(), m_aContents);
104 for (auto const& listener : listeners)
106 if( listener.is() )
107 listener->changedContents(aEvent);
111 void X11Clipboard::clearContents()
113 ClearableMutexGuard aGuard(m_xSelectionManager->getMutex());
114 // protect against deletion during outside call
115 Reference< XClipboard > xThis( static_cast<XClipboard*>(this));
116 // copy member references on stack so they can be called
117 // without having the mutex
118 Reference< XClipboardOwner > xOwner( m_aOwner );
119 Reference< XTransferable > xKeepAlive( m_aContents );
120 // clear members
121 m_aOwner.clear();
122 m_aContents.clear();
124 // release the mutex
125 aGuard.clear();
127 // inform previous owner of lost ownership
128 if ( xOwner.is() )
129 xOwner->lostOwnership(xThis, m_aContents);
132 Reference< XTransferable > SAL_CALL X11Clipboard::getContents()
134 MutexGuard aGuard(m_xSelectionManager->getMutex());
136 if( ! m_aContents.is() )
137 m_aContents = new X11Transferable( SelectionManager::get(), m_aSelection );
138 return m_aContents;
141 void SAL_CALL X11Clipboard::setContents(
142 const Reference< XTransferable >& xTrans,
143 const Reference< XClipboardOwner >& xClipboardOwner )
145 // remember old values for callbacks before setting the new ones.
146 ClearableMutexGuard aGuard(m_xSelectionManager->getMutex());
148 Reference< XClipboardOwner > oldOwner( m_aOwner );
149 m_aOwner = xClipboardOwner;
151 Reference< XTransferable > oldContents( m_aContents );
152 m_aContents = xTrans;
154 aGuard.clear();
156 // for now request ownership for both selections
157 if( m_aSelection != None )
158 m_xSelectionManager->requestOwnership( m_aSelection );
159 else
161 m_xSelectionManager->requestOwnership( XA_PRIMARY );
162 m_xSelectionManager->requestOwnership( m_xSelectionManager->getAtom( u"CLIPBOARD"_ustr ) );
165 // notify old owner on loss of ownership
166 if( oldOwner.is() )
167 oldOwner->lostOwnership(static_cast < XClipboard * > (this), oldContents);
169 // notify all listeners on content changes
170 fireChangedContentsEvent();
173 OUString SAL_CALL X11Clipboard::getName()
175 return m_xSelectionManager->getString( m_aSelection );
178 sal_Int8 SAL_CALL X11Clipboard::getRenderingCapabilities()
180 return RenderingCapabilities::Delayed;
183 void SAL_CALL X11Clipboard::addClipboardListener( const Reference< XClipboardListener >& listener )
185 MutexGuard aGuard( m_xSelectionManager->getMutex() );
186 m_aListeners.push_back( listener );
189 void SAL_CALL X11Clipboard::removeClipboardListener( const Reference< XClipboardListener >& listener )
191 MutexGuard aGuard( m_xSelectionManager->getMutex() );
192 std::erase(m_aListeners, listener);
195 Reference< XTransferable > X11Clipboard::getTransferable()
197 return getContents();
200 void X11Clipboard::clearTransferable()
202 clearContents();
205 void X11Clipboard::fireContentsChanged()
207 fireChangedContentsEvent();
210 Reference< XInterface > X11Clipboard::getReference() noexcept
212 return getXWeak();
215 OUString SAL_CALL X11Clipboard::getImplementationName( )
217 return X11_CLIPBOARD_IMPLEMENTATION_NAME;
220 sal_Bool SAL_CALL X11Clipboard::supportsService( const OUString& ServiceName )
222 return cppu::supportsService(this, ServiceName);
225 Sequence< OUString > SAL_CALL X11Clipboard::getSupportedServiceNames( )
227 return X11Clipboard_getSupportedServiceNames();
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */