Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <X11/Xatom.h>
21 : #include <X11_clipboard.hxx>
22 : #include <X11_transferable.hxx>
23 : #include <com/sun/star/lang/DisposedException.hpp>
24 : #include <com/sun/star/datatransfer/clipboard/RenderingCapabilities.hpp>
25 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 : #include <com/sun/star/lang/XSingleServiceFactory.hpp>
27 : #include <com/sun/star/registry/XRegistryKey.hpp>
28 : #include <uno/dispatcher.h>
29 : #include <uno/mapping.hxx>
30 : #include <cppuhelper/factory.hxx>
31 : #include <cppuhelper/supportsservice.hxx>
32 : #include <rtl/ref.hxx>
33 : #include <rtl/tencinfo.h>
34 :
35 : #if OSL_DEBUG_LEVEL > 1
36 : #include <stdio.h>
37 : #endif
38 :
39 : using namespace com::sun::star::datatransfer;
40 : using namespace com::sun::star::datatransfer::clipboard;
41 : using namespace com::sun::star::lang;
42 : using namespace com::sun::star::uno;
43 : using namespace com::sun::star::awt;
44 : using namespace cppu;
45 : using namespace osl;
46 : using namespace x11;
47 :
48 2 : X11Clipboard::X11Clipboard( SelectionManager& rManager, Atom aSelection ) :
49 : ::cppu::WeakComponentImplHelper2<
50 : ::com::sun::star::datatransfer::clipboard::XSystemClipboard,
51 : ::com::sun::star::lang::XServiceInfo
52 2 : >( rManager.getMutex() ),
53 :
54 : m_rSelectionManager( rManager ),
55 : m_xSelectionManager( & rManager ),
56 2 : m_aSelection( aSelection )
57 : {
58 : #if OSL_DEBUG_LEVEL > 1
59 : fprintf( stderr, "creating instance of X11Clipboard (this=%p)\n", this );
60 : #endif
61 2 : }
62 :
63 : css::uno::Reference<css::datatransfer::clipboard::XClipboard>
64 2 : X11Clipboard::create( SelectionManager& rManager, Atom aSelection )
65 : {
66 2 : rtl::Reference<X11Clipboard> cb(new X11Clipboard(rManager, aSelection));
67 2 : if( aSelection != None )
68 : {
69 2 : rManager.registerHandler( aSelection, *cb.get() );
70 : }
71 : else
72 : {
73 0 : rManager.registerHandler( XA_PRIMARY, *cb.get() );
74 0 : rManager.registerHandler( rManager.getAtom( OUString("CLIPBOARD") ), *cb.get() );
75 : }
76 2 : return cb.get();
77 : }
78 :
79 0 : X11Clipboard::~X11Clipboard()
80 : {
81 0 : MutexGuard aGuard( *Mutex::getGlobalMutex() );
82 :
83 : #if OSL_DEBUG_LEVEL > 1
84 : fprintf( stderr, "shutting down instance of X11Clipboard (this=%p, Selection=\"%s\")\n", this, OUStringToOString( m_rSelectionManager.getString( m_aSelection ), RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
85 : #endif
86 0 : if( m_aSelection != None )
87 0 : m_rSelectionManager.deregisterHandler( m_aSelection );
88 : else
89 : {
90 0 : m_rSelectionManager.deregisterHandler( XA_PRIMARY );
91 0 : m_rSelectionManager.deregisterHandler( m_rSelectionManager.getAtom( OUString("CLIPBOARD") ) );
92 0 : }
93 0 : }
94 :
95 0 : void X11Clipboard::fireChangedContentsEvent()
96 : {
97 0 : ClearableMutexGuard aGuard( m_rSelectionManager.getMutex() );
98 : #if OSL_DEBUG_LEVEL > 1
99 : fprintf( stderr, "X11Clipboard::fireChangedContentsEvent for %s (%" SAL_PRI_SIZET "u listeners)\n",
100 : OUStringToOString( m_rSelectionManager.getString( m_aSelection ), RTL_TEXTENCODING_ISO_8859_1 ).getStr(), m_aListeners.size() );
101 : #endif
102 0 : ::std::list< Reference< XClipboardListener > > listeners( m_aListeners );
103 0 : aGuard.clear();
104 :
105 0 : ClipboardEvent aEvent( static_cast<OWeakObject*>(this), m_aContents);
106 0 : while( listeners.begin() != listeners.end() )
107 : {
108 0 : if( listeners.front().is() )
109 0 : listeners.front()->changedContents(aEvent);
110 0 : listeners.pop_front();
111 0 : }
112 0 : }
113 :
114 0 : void X11Clipboard::clearContents()
115 : {
116 0 : ClearableMutexGuard aGuard(m_rSelectionManager.getMutex());
117 : // protect against deletion during outside call
118 0 : Reference< XClipboard > xThis( static_cast<XClipboard*>(this));
119 : // copy member references on stack so they can be called
120 : // without having the mutex
121 0 : Reference< XClipboardOwner > xOwner( m_aOwner );
122 0 : Reference< XTransferable > xTrans( m_aContents );
123 : // clear members
124 0 : m_aOwner.clear();
125 0 : m_aContents.clear();
126 :
127 : // release the mutex
128 0 : aGuard.clear();
129 :
130 : // inform previous owner of lost ownership
131 0 : if ( xOwner.is() )
132 0 : xOwner->lostOwnership(xThis, m_aContents);
133 0 : }
134 :
135 1 : Reference< XTransferable > SAL_CALL X11Clipboard::getContents()
136 : throw(RuntimeException, std::exception)
137 : {
138 1 : MutexGuard aGuard(m_rSelectionManager.getMutex());
139 :
140 1 : if( ! m_aContents.is() )
141 1 : m_aContents = new X11Transferable( SelectionManager::get(), m_aSelection );
142 1 : return m_aContents;
143 : }
144 :
145 0 : void SAL_CALL X11Clipboard::setContents(
146 : const Reference< XTransferable >& xTrans,
147 : const Reference< XClipboardOwner >& xClipboardOwner )
148 : throw(RuntimeException, std::exception)
149 : {
150 : // remember old values for callbacks before setting the new ones.
151 0 : ClearableMutexGuard aGuard(m_rSelectionManager.getMutex());
152 :
153 0 : Reference< XClipboardOwner > oldOwner( m_aOwner );
154 0 : m_aOwner = xClipboardOwner;
155 :
156 0 : Reference< XTransferable > oldContents( m_aContents );
157 0 : m_aContents = xTrans;
158 :
159 0 : aGuard.clear();
160 :
161 : // for now request ownership for both selections
162 0 : if( m_aSelection != None )
163 0 : m_rSelectionManager.requestOwnership( m_aSelection );
164 : else
165 : {
166 0 : m_rSelectionManager.requestOwnership( XA_PRIMARY );
167 0 : m_rSelectionManager.requestOwnership( m_rSelectionManager.getAtom( OUString("CLIPBOARD") ) );
168 : }
169 :
170 : // notify old owner on loss of ownership
171 0 : if( oldOwner.is() )
172 0 : oldOwner->lostOwnership(static_cast < XClipboard * > (this), oldContents);
173 :
174 : // notify all listeners on content changes
175 0 : fireChangedContentsEvent();
176 0 : }
177 :
178 0 : OUString SAL_CALL X11Clipboard::getName()
179 : throw(RuntimeException, std::exception)
180 : {
181 0 : return m_rSelectionManager.getString( m_aSelection );
182 : }
183 :
184 0 : sal_Int8 SAL_CALL X11Clipboard::getRenderingCapabilities()
185 : throw(RuntimeException, std::exception)
186 : {
187 0 : return RenderingCapabilities::Delayed;
188 : }
189 :
190 12 : void SAL_CALL X11Clipboard::addClipboardListener( const Reference< XClipboardListener >& listener )
191 : throw(RuntimeException, std::exception)
192 : {
193 12 : MutexGuard aGuard( m_rSelectionManager.getMutex() );
194 12 : m_aListeners.push_back( listener );
195 12 : }
196 :
197 11 : void SAL_CALL X11Clipboard::removeClipboardListener( const Reference< XClipboardListener >& listener )
198 : throw(RuntimeException, std::exception)
199 : {
200 11 : MutexGuard aGuard( m_rSelectionManager.getMutex() );
201 11 : m_aListeners.remove( listener );
202 11 : }
203 :
204 0 : Reference< XTransferable > X11Clipboard::getTransferable()
205 : {
206 0 : return getContents();
207 : }
208 :
209 0 : void X11Clipboard::clearTransferable()
210 : {
211 0 : clearContents();
212 0 : }
213 :
214 0 : void X11Clipboard::fireContentsChanged()
215 : {
216 0 : fireChangedContentsEvent();
217 0 : }
218 :
219 0 : Reference< XInterface > X11Clipboard::getReference() throw()
220 : {
221 0 : return Reference< XInterface >( static_cast< OWeakObject* >(this) );
222 : }
223 :
224 0 : OUString SAL_CALL X11Clipboard::getImplementationName( )
225 : throw(RuntimeException, std::exception)
226 : {
227 0 : return OUString(X11_CLIPBOARD_IMPLEMENTATION_NAME);
228 : }
229 :
230 0 : sal_Bool SAL_CALL X11Clipboard::supportsService( const OUString& ServiceName )
231 : throw(RuntimeException, std::exception)
232 : {
233 0 : return cppu::supportsService(this, ServiceName);
234 : }
235 :
236 0 : Sequence< OUString > SAL_CALL X11Clipboard::getSupportedServiceNames( )
237 : throw(RuntimeException, std::exception)
238 : {
239 0 : return X11Clipboard_getSupportedServiceNames();
240 : }
241 :
242 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|