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 :
21 : #include <X11/Xatom.h>
22 : #include <X11_clipboard.hxx>
23 : #include <X11_transferable.hxx>
24 : #include <com/sun/star/lang/DisposedException.hpp>
25 : #include <com/sun/star/datatransfer/clipboard/RenderingCapabilities.hpp>
26 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
27 : #include <com/sun/star/lang/XSingleServiceFactory.hpp>
28 : #include <com/sun/star/registry/XRegistryKey.hpp>
29 : #include <uno/dispatcher.h> // declaration of generic uno interface
30 : #include <uno/mapping.hxx> // mapping stuff
31 : #include <cppuhelper/factory.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 :
48 0 : X11Clipboard::X11Clipboard( SelectionManager& rManager, Atom aSelection ) :
49 : ::cppu::WeakComponentImplHelper2<
50 : ::com::sun::star::datatransfer::clipboard::XSystemClipboard,
51 : ::com::sun::star::lang::XServiceInfo
52 0 : >( rManager.getMutex() ),
53 :
54 : m_rSelectionManager( rManager ),
55 : m_xSelectionManager( & rManager ),
56 0 : m_aSelection( aSelection )
57 : {
58 : #if OSL_DEBUG_LEVEL > 1
59 : fprintf( stderr, "creating instance of X11Clipboard (this=%p)\n", this );
60 : #endif
61 :
62 0 : if( m_aSelection != None )
63 : {
64 0 : m_rSelectionManager.registerHandler( m_aSelection, *this );
65 : }
66 : else
67 : {
68 0 : m_rSelectionManager.registerHandler( XA_PRIMARY, *this );
69 0 : m_rSelectionManager.registerHandler( m_rSelectionManager.getAtom( OUString("CLIPBOARD") ), *this );
70 : }
71 0 : }
72 :
73 : // ------------------------------------------------------------------------
74 :
75 0 : X11Clipboard::~X11Clipboard()
76 : {
77 0 : MutexGuard aGuard( *Mutex::getGlobalMutex() );
78 :
79 : #if OSL_DEBUG_LEVEL > 1
80 : 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() );
81 : #endif
82 0 : if( m_aSelection != None )
83 0 : m_rSelectionManager.deregisterHandler( m_aSelection );
84 : else
85 : {
86 0 : m_rSelectionManager.deregisterHandler( XA_PRIMARY );
87 0 : m_rSelectionManager.deregisterHandler( m_rSelectionManager.getAtom( OUString("CLIPBOARD") ) );
88 0 : }
89 0 : }
90 :
91 :
92 : // ------------------------------------------------------------------------
93 :
94 0 : void X11Clipboard::fireChangedContentsEvent()
95 : {
96 0 : ClearableMutexGuard aGuard( m_rSelectionManager.getMutex() );
97 : #if OSL_DEBUG_LEVEL > 1
98 : fprintf( stderr, "X11Clipboard::fireChangedContentsEvent for %s (%" SAL_PRI_SIZET "u listeners)\n",
99 : OUStringToOString( m_rSelectionManager.getString( m_aSelection ), RTL_TEXTENCODING_ISO_8859_1 ).getStr(), m_aListeners.size() );
100 : #endif
101 0 : ::std::list< Reference< XClipboardListener > > listeners( m_aListeners );
102 0 : aGuard.clear();
103 :
104 0 : ClipboardEvent aEvent( static_cast<OWeakObject*>(this), m_aContents);
105 0 : while( listeners.begin() != listeners.end() )
106 : {
107 0 : if( listeners.front().is() )
108 0 : listeners.front()->changedContents(aEvent);
109 0 : listeners.pop_front();
110 0 : }
111 0 : }
112 :
113 : // ------------------------------------------------------------------------
114 :
115 0 : void X11Clipboard::clearContents()
116 : {
117 0 : ClearableMutexGuard aGuard(m_rSelectionManager.getMutex());
118 : // protect against deletion during outside call
119 0 : Reference< XClipboard > xThis( static_cast<XClipboard*>(this));
120 : // copy member references on stack so they can be called
121 : // without having the mutex
122 0 : Reference< XClipboardOwner > xOwner( m_aOwner );
123 0 : Reference< XTransferable > xTrans( m_aContents );
124 : // clear members
125 0 : m_aOwner.clear();
126 0 : m_aContents.clear();
127 :
128 : // release the mutex
129 0 : aGuard.clear();
130 :
131 : // inform previous owner of lost ownership
132 0 : if ( xOwner.is() )
133 0 : xOwner->lostOwnership(xThis, m_aContents);
134 0 : }
135 :
136 : // ------------------------------------------------------------------------
137 :
138 0 : Reference< XTransferable > SAL_CALL X11Clipboard::getContents()
139 : throw(RuntimeException)
140 : {
141 0 : MutexGuard aGuard(m_rSelectionManager.getMutex());
142 :
143 0 : if( ! m_aContents.is() )
144 0 : m_aContents = new X11Transferable( SelectionManager::get(), m_aSelection );
145 0 : return m_aContents;
146 : }
147 :
148 : // ------------------------------------------------------------------------
149 :
150 0 : void SAL_CALL X11Clipboard::setContents(
151 : const Reference< XTransferable >& xTrans,
152 : const Reference< XClipboardOwner >& xClipboardOwner )
153 : throw(RuntimeException)
154 : {
155 : // remember old values for callbacks before setting the new ones.
156 0 : ClearableMutexGuard aGuard(m_rSelectionManager.getMutex());
157 :
158 0 : Reference< XClipboardOwner > oldOwner( m_aOwner );
159 0 : m_aOwner = xClipboardOwner;
160 :
161 0 : Reference< XTransferable > oldContents( m_aContents );
162 0 : m_aContents = xTrans;
163 :
164 0 : aGuard.clear();
165 :
166 : // for now request ownership for both selections
167 0 : if( m_aSelection != None )
168 0 : m_rSelectionManager.requestOwnership( m_aSelection );
169 : else
170 : {
171 0 : m_rSelectionManager.requestOwnership( XA_PRIMARY );
172 0 : m_rSelectionManager.requestOwnership( m_rSelectionManager.getAtom( OUString("CLIPBOARD") ) );
173 : }
174 :
175 : // notify old owner on loss of ownership
176 0 : if( oldOwner.is() )
177 0 : oldOwner->lostOwnership(static_cast < XClipboard * > (this), oldContents);
178 :
179 : // notify all listeners on content changes
180 0 : fireChangedContentsEvent();
181 0 : }
182 :
183 : // ------------------------------------------------------------------------
184 :
185 0 : OUString SAL_CALL X11Clipboard::getName()
186 : throw(RuntimeException)
187 : {
188 0 : return m_rSelectionManager.getString( m_aSelection );
189 : }
190 :
191 : // ------------------------------------------------------------------------
192 :
193 0 : sal_Int8 SAL_CALL X11Clipboard::getRenderingCapabilities()
194 : throw(RuntimeException)
195 : {
196 0 : return RenderingCapabilities::Delayed;
197 : }
198 :
199 :
200 : // ------------------------------------------------------------------------
201 0 : void SAL_CALL X11Clipboard::addClipboardListener( const Reference< XClipboardListener >& listener )
202 : throw(RuntimeException)
203 : {
204 0 : MutexGuard aGuard( m_rSelectionManager.getMutex() );
205 0 : m_aListeners.push_back( listener );
206 0 : }
207 :
208 : // ------------------------------------------------------------------------
209 :
210 0 : void SAL_CALL X11Clipboard::removeClipboardListener( const Reference< XClipboardListener >& listener )
211 : throw(RuntimeException)
212 : {
213 0 : MutexGuard aGuard( m_rSelectionManager.getMutex() );
214 0 : m_aListeners.remove( listener );
215 0 : }
216 :
217 :
218 : // ------------------------------------------------------------------------
219 :
220 0 : Reference< XTransferable > X11Clipboard::getTransferable()
221 : {
222 0 : return getContents();
223 : }
224 :
225 : // ------------------------------------------------------------------------
226 :
227 0 : void X11Clipboard::clearTransferable()
228 : {
229 0 : clearContents();
230 0 : }
231 :
232 : // ------------------------------------------------------------------------
233 :
234 0 : void X11Clipboard::fireContentsChanged()
235 : {
236 0 : fireChangedContentsEvent();
237 0 : }
238 :
239 : // ------------------------------------------------------------------------
240 :
241 0 : Reference< XInterface > X11Clipboard::getReference() throw()
242 : {
243 0 : return Reference< XInterface >( static_cast< OWeakObject* >(this) );
244 : }
245 :
246 : // ------------------------------------------------------------------------
247 :
248 0 : OUString SAL_CALL X11Clipboard::getImplementationName( )
249 : throw(RuntimeException)
250 : {
251 0 : return OUString(X11_CLIPBOARD_IMPLEMENTATION_NAME);
252 : }
253 :
254 : // ------------------------------------------------------------------------
255 :
256 0 : sal_Bool SAL_CALL X11Clipboard::supportsService( const OUString& ServiceName )
257 : throw(RuntimeException)
258 : {
259 0 : Sequence < OUString > SupportedServicesNames = X11Clipboard_getSupportedServiceNames();
260 :
261 0 : for ( sal_Int32 n = SupportedServicesNames.getLength(); n--; )
262 0 : if (SupportedServicesNames[n] == ServiceName)
263 0 : return sal_True;
264 :
265 0 : return sal_False;
266 : }
267 :
268 : // ------------------------------------------------------------------------
269 :
270 0 : void SAL_CALL X11Clipboard::initialize( const Sequence< Any >& ) throw( ::com::sun::star::uno::Exception )
271 : {
272 0 : }
273 :
274 : // ------------------------------------------------------------------------
275 :
276 0 : Sequence< OUString > SAL_CALL X11Clipboard::getSupportedServiceNames( )
277 : throw(RuntimeException)
278 : {
279 0 : return X11Clipboard_getSupportedServiceNames();
280 : }
281 :
282 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|