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