1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_vcl.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <X11/Xatom.h>
32*cdf0e10cSrcweir #include <X11_clipboard.hxx>
33*cdf0e10cSrcweir #include <X11_transferable.hxx>
34*cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/datatransfer/clipboard/RenderingCapabilities.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
39*cdf0e10cSrcweir #include <uno/dispatcher.h> // declaration of generic uno interface
40*cdf0e10cSrcweir #include <uno/mapping.hxx> // mapping stuff
41*cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
42*cdf0e10cSrcweir #include <rtl/tencinfo.h>
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
45*cdf0e10cSrcweir #include <stdio.h>
46*cdf0e10cSrcweir #endif
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir using namespace com::sun::star::datatransfer;
49*cdf0e10cSrcweir using namespace com::sun::star::datatransfer::clipboard;
50*cdf0e10cSrcweir using namespace com::sun::star::lang;
51*cdf0e10cSrcweir using namespace com::sun::star::uno;
52*cdf0e10cSrcweir using namespace com::sun::star::awt;
53*cdf0e10cSrcweir using namespace cppu;
54*cdf0e10cSrcweir using namespace osl;
55*cdf0e10cSrcweir using namespace rtl;
56*cdf0e10cSrcweir using namespace x11;
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir X11Clipboard::X11Clipboard( SelectionManager& rManager, Atom aSelection ) :
59*cdf0e10cSrcweir         ::cppu::WeakComponentImplHelper4<
60*cdf0e10cSrcweir     ::com::sun::star::datatransfer::clipboard::XClipboardEx,
61*cdf0e10cSrcweir     ::com::sun::star::datatransfer::clipboard::XClipboardNotifier,
62*cdf0e10cSrcweir     ::com::sun::star::lang::XServiceInfo,
63*cdf0e10cSrcweir     ::com::sun::star::lang::XInitialization
64*cdf0e10cSrcweir     >( rManager.getMutex() ),
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir 		m_rSelectionManager( rManager ),
67*cdf0e10cSrcweir 		m_xSelectionManager( & rManager ),
68*cdf0e10cSrcweir 		m_aSelection( aSelection )
69*cdf0e10cSrcweir {
70*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
71*cdf0e10cSrcweir 	fprintf( stderr, "creating instance of X11Clipboard (this=%p)\n", this );
72*cdf0e10cSrcweir #endif
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir 	if( m_aSelection != None )
75*cdf0e10cSrcweir     {
76*cdf0e10cSrcweir 		m_rSelectionManager.registerHandler( m_aSelection, *this );
77*cdf0e10cSrcweir     }
78*cdf0e10cSrcweir 	else
79*cdf0e10cSrcweir 	{
80*cdf0e10cSrcweir 		m_rSelectionManager.registerHandler( XA_PRIMARY, *this );
81*cdf0e10cSrcweir 		m_rSelectionManager.registerHandler( m_rSelectionManager.getAtom( OUString::createFromAscii( "CLIPBOARD" ) ), *this );
82*cdf0e10cSrcweir 	}
83*cdf0e10cSrcweir }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir // ------------------------------------------------------------------------
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir X11Clipboard::~X11Clipboard()
88*cdf0e10cSrcweir {
89*cdf0e10cSrcweir 	MutexGuard aGuard( *Mutex::getGlobalMutex() );
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
92*cdf0e10cSrcweir 	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() );
93*cdf0e10cSrcweir #endif
94*cdf0e10cSrcweir 	if( m_aSelection != None )
95*cdf0e10cSrcweir 		m_rSelectionManager.deregisterHandler( m_aSelection );
96*cdf0e10cSrcweir 	else
97*cdf0e10cSrcweir 	{
98*cdf0e10cSrcweir 		m_rSelectionManager.deregisterHandler( XA_PRIMARY );
99*cdf0e10cSrcweir 		m_rSelectionManager.deregisterHandler( m_rSelectionManager.getAtom( OUString::createFromAscii( "CLIPBOARD" ) ) );
100*cdf0e10cSrcweir 	}
101*cdf0e10cSrcweir }
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir // ------------------------------------------------------------------------
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir void X11Clipboard::fireChangedContentsEvent()
107*cdf0e10cSrcweir {
108*cdf0e10cSrcweir     ClearableMutexGuard aGuard( m_rSelectionManager.getMutex() );
109*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
110*cdf0e10cSrcweir     fprintf( stderr, "X11Clipboard::fireChangedContentsEvent for %s (%d listeners)\n",
111*cdf0e10cSrcweir              OUStringToOString( m_rSelectionManager.getString( m_aSelection ), RTL_TEXTENCODING_ISO_8859_1 ).getStr(), m_aListeners.size() );
112*cdf0e10cSrcweir #endif
113*cdf0e10cSrcweir     ::std::list< Reference< XClipboardListener > > listeners( m_aListeners );
114*cdf0e10cSrcweir     aGuard.clear();
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir     ClipboardEvent aEvent( static_cast<OWeakObject*>(this), m_aContents);
117*cdf0e10cSrcweir     while( listeners.begin() != listeners.end() )
118*cdf0e10cSrcweir     {
119*cdf0e10cSrcweir         if( listeners.front().is() )
120*cdf0e10cSrcweir             listeners.front()->changedContents(aEvent);
121*cdf0e10cSrcweir         listeners.pop_front();
122*cdf0e10cSrcweir     }
123*cdf0e10cSrcweir }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir // ------------------------------------------------------------------------
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir void X11Clipboard::clearContents()
128*cdf0e10cSrcweir {
129*cdf0e10cSrcweir 	ClearableMutexGuard aGuard(m_rSelectionManager.getMutex());
130*cdf0e10cSrcweir     // protect against deletion during outside call
131*cdf0e10cSrcweir     Reference< XClipboard > xThis( static_cast<XClipboard*>(this));
132*cdf0e10cSrcweir     // copy member references on stack so they can be called
133*cdf0e10cSrcweir     // without having the mutex
134*cdf0e10cSrcweir     Reference< XClipboardOwner > xOwner( m_aOwner );
135*cdf0e10cSrcweir     Reference< XTransferable > xTrans( m_aContents );
136*cdf0e10cSrcweir 	// clear members
137*cdf0e10cSrcweir     m_aOwner.clear();
138*cdf0e10cSrcweir     m_aContents.clear();
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir     // release the mutex
141*cdf0e10cSrcweir     aGuard.clear();
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir     // inform previous owner of lost ownership
144*cdf0e10cSrcweir 	if ( xOwner.is() )
145*cdf0e10cSrcweir 		xOwner->lostOwnership(xThis, m_aContents);
146*cdf0e10cSrcweir }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir // ------------------------------------------------------------------------
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir Reference< XTransferable > SAL_CALL X11Clipboard::getContents()
151*cdf0e10cSrcweir 	throw(RuntimeException)
152*cdf0e10cSrcweir {
153*cdf0e10cSrcweir 	MutexGuard aGuard(m_rSelectionManager.getMutex());
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir 	if( ! m_aContents.is() )
156*cdf0e10cSrcweir 		m_aContents = new X11Transferable( SelectionManager::get(), static_cast< OWeakObject* >(this), m_aSelection );
157*cdf0e10cSrcweir 	return m_aContents;
158*cdf0e10cSrcweir }
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir // ------------------------------------------------------------------------
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir void SAL_CALL X11Clipboard::setContents(
163*cdf0e10cSrcweir 	const Reference< XTransferable >& xTrans,
164*cdf0e10cSrcweir 	const Reference< XClipboardOwner >& xClipboardOwner )
165*cdf0e10cSrcweir 	throw(RuntimeException)
166*cdf0e10cSrcweir {
167*cdf0e10cSrcweir 	// remember old values for callbacks before setting the new ones.
168*cdf0e10cSrcweir 	ClearableMutexGuard aGuard(m_rSelectionManager.getMutex());
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir 	Reference< XClipboardOwner > oldOwner( m_aOwner );
171*cdf0e10cSrcweir 	m_aOwner = xClipboardOwner;
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir 	Reference< XTransferable > oldContents( m_aContents );
174*cdf0e10cSrcweir 	m_aContents = xTrans;
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir 	aGuard.clear();
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir 	// for now request ownership for both selections
179*cdf0e10cSrcweir 	if( m_aSelection != None )
180*cdf0e10cSrcweir 		m_rSelectionManager.requestOwnership( m_aSelection );
181*cdf0e10cSrcweir 	else
182*cdf0e10cSrcweir 	{
183*cdf0e10cSrcweir 		m_rSelectionManager.requestOwnership( XA_PRIMARY );
184*cdf0e10cSrcweir 		m_rSelectionManager.requestOwnership( m_rSelectionManager.getAtom( OUString::createFromAscii( "CLIPBOARD" ) ) );
185*cdf0e10cSrcweir 	}
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir 	// notify old owner on loss of ownership
188*cdf0e10cSrcweir 	if( oldOwner.is() )
189*cdf0e10cSrcweir 		oldOwner->lostOwnership(static_cast < XClipboard * > (this), oldContents);
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir 	// notify all listeners on content changes
192*cdf0e10cSrcweir 	fireChangedContentsEvent();
193*cdf0e10cSrcweir }
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir // ------------------------------------------------------------------------
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir OUString SAL_CALL X11Clipboard::getName()
198*cdf0e10cSrcweir 	throw(RuntimeException)
199*cdf0e10cSrcweir {
200*cdf0e10cSrcweir 	return m_rSelectionManager.getString( m_aSelection );
201*cdf0e10cSrcweir }
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir // ------------------------------------------------------------------------
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir sal_Int8 SAL_CALL X11Clipboard::getRenderingCapabilities()
206*cdf0e10cSrcweir 	throw(RuntimeException)
207*cdf0e10cSrcweir {
208*cdf0e10cSrcweir 	return RenderingCapabilities::Delayed;
209*cdf0e10cSrcweir }
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir // ------------------------------------------------------------------------
213*cdf0e10cSrcweir void SAL_CALL X11Clipboard::addClipboardListener( const Reference< XClipboardListener >& listener )
214*cdf0e10cSrcweir 	throw(RuntimeException)
215*cdf0e10cSrcweir {
216*cdf0e10cSrcweir     MutexGuard aGuard( m_rSelectionManager.getMutex() );
217*cdf0e10cSrcweir     m_aListeners.push_back( listener );
218*cdf0e10cSrcweir }
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir // ------------------------------------------------------------------------
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir void SAL_CALL X11Clipboard::removeClipboardListener( const Reference< XClipboardListener >& listener )
223*cdf0e10cSrcweir 	throw(RuntimeException)
224*cdf0e10cSrcweir {
225*cdf0e10cSrcweir     MutexGuard aGuard( m_rSelectionManager.getMutex() );
226*cdf0e10cSrcweir     m_aListeners.remove( listener );
227*cdf0e10cSrcweir }
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir // ------------------------------------------------------------------------
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir Reference< XTransferable > X11Clipboard::getTransferable()
233*cdf0e10cSrcweir {
234*cdf0e10cSrcweir 	return getContents();
235*cdf0e10cSrcweir }
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir // ------------------------------------------------------------------------
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir void X11Clipboard::clearTransferable()
240*cdf0e10cSrcweir {
241*cdf0e10cSrcweir 	clearContents();
242*cdf0e10cSrcweir }
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir // ------------------------------------------------------------------------
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir void X11Clipboard::fireContentsChanged()
247*cdf0e10cSrcweir {
248*cdf0e10cSrcweir     fireChangedContentsEvent();
249*cdf0e10cSrcweir }
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir // ------------------------------------------------------------------------
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir Reference< XInterface > X11Clipboard::getReference() throw()
254*cdf0e10cSrcweir {
255*cdf0e10cSrcweir     return Reference< XInterface >( static_cast< OWeakObject* >(this) );
256*cdf0e10cSrcweir }
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir // ------------------------------------------------------------------------
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir OUString SAL_CALL X11Clipboard::getImplementationName(  )
261*cdf0e10cSrcweir 	throw(RuntimeException)
262*cdf0e10cSrcweir {
263*cdf0e10cSrcweir 	return OUString::createFromAscii(X11_CLIPBOARD_IMPLEMENTATION_NAME);
264*cdf0e10cSrcweir }
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir // ------------------------------------------------------------------------
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir sal_Bool SAL_CALL X11Clipboard::supportsService( const OUString& ServiceName )
269*cdf0e10cSrcweir 	throw(RuntimeException)
270*cdf0e10cSrcweir {
271*cdf0e10cSrcweir 	Sequence < OUString > SupportedServicesNames = X11Clipboard_getSupportedServiceNames();
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir 	for ( sal_Int32 n = SupportedServicesNames.getLength(); n--; )
274*cdf0e10cSrcweir 		if (SupportedServicesNames[n].compareTo(ServiceName) == 0)
275*cdf0e10cSrcweir 			return sal_True;
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir 	return sal_False;
278*cdf0e10cSrcweir }
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir // ------------------------------------------------------------------------
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir void SAL_CALL X11Clipboard::initialize( const Sequence< Any >& ) throw( ::com::sun::star::uno::Exception )
283*cdf0e10cSrcweir {
284*cdf0e10cSrcweir }
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir // ------------------------------------------------------------------------
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir Sequence< OUString > SAL_CALL X11Clipboard::getSupportedServiceNames(	 )
289*cdf0e10cSrcweir 	throw(RuntimeException)
290*cdf0e10cSrcweir {
291*cdf0e10cSrcweir 	return X11Clipboard_getSupportedServiceNames();
292*cdf0e10cSrcweir }
293*cdf0e10cSrcweir 
294