1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: X11_clipboard.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dtrans.hxx"
34 #include <X11/Xatom.h>
35 #include <X11_clipboard.hxx>
36 #include <X11_transferable.hxx>
37 #include <com/sun/star/lang/DisposedException.hpp>
38 #include <com/sun/star/datatransfer/clipboard/RenderingCapabilities.hpp>
39 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
40 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
41 #include <com/sun/star/registry/XRegistryKey.hpp>
42 #include <uno/dispatcher.h> // declaration of generic uno interface
43 #include <uno/mapping.hxx> // mapping stuff
44 #include <cppuhelper/factory.hxx>
45 #include <rtl/tencinfo.h>
47 #if OSL_DEBUG_LEVEL > 1
51 using namespace com::sun::star::datatransfer
;
52 using namespace com::sun::star::datatransfer::clipboard
;
53 using namespace com::sun::star::lang
;
54 using namespace com::sun::star::uno
;
55 using namespace com::sun::star::awt
;
61 X11Clipboard::X11Clipboard( SelectionManager
& rManager
, Atom aSelection
) :
62 ::cppu::WeakComponentImplHelper4
<
63 ::com::sun::star::datatransfer::clipboard::XClipboardEx
,
64 ::com::sun::star::datatransfer::clipboard::XClipboardNotifier
,
65 ::com::sun::star::lang::XServiceInfo
,
66 ::com::sun::star::lang::XInitialization
67 >( rManager
.getMutex() ),
69 m_rSelectionManager( rManager
),
70 m_xSelectionManager( & rManager
),
71 m_aSelection( aSelection
)
73 #if OSL_DEBUG_LEVEL > 1
74 fprintf( stderr
, "creating instance of X11Clipboard (this=%p)\n", this );
77 if( m_aSelection
!= None
)
79 m_rSelectionManager
.registerHandler( m_aSelection
, *this );
83 m_rSelectionManager
.registerHandler( XA_PRIMARY
, *this );
84 m_rSelectionManager
.registerHandler( m_rSelectionManager
.getAtom( OUString::createFromAscii( "CLIPBOARD" ) ), *this );
88 // ------------------------------------------------------------------------
90 X11Clipboard::~X11Clipboard()
92 MutexGuard
aGuard( *Mutex::getGlobalMutex() );
94 #if OSL_DEBUG_LEVEL > 1
95 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() );
97 if( m_aSelection
!= None
)
98 m_rSelectionManager
.deregisterHandler( m_aSelection
);
101 m_rSelectionManager
.deregisterHandler( XA_PRIMARY
);
102 m_rSelectionManager
.deregisterHandler( m_rSelectionManager
.getAtom( OUString::createFromAscii( "CLIPBOARD" ) ) );
107 // ------------------------------------------------------------------------
109 void X11Clipboard::fireChangedContentsEvent()
111 ClearableMutexGuard
aGuard( m_rSelectionManager
.getMutex() );
112 #if OSL_DEBUG_LEVEL > 1
113 fprintf( stderr
, "X11Clipboard::fireChangedContentsEvent for %s (%d listeners)\n",
114 OUStringToOString( m_rSelectionManager
.getString( m_aSelection
), RTL_TEXTENCODING_ISO_8859_1
).getStr(), m_aListeners
.size() );
116 ::std::list
< Reference
< XClipboardListener
> > listeners( m_aListeners
);
119 ClipboardEvent
aEvent( static_cast<OWeakObject
*>(this), m_aContents
);
120 while( listeners
.begin() != listeners
.end() )
122 if( listeners
.front().is() )
123 listeners
.front()->changedContents(aEvent
);
124 listeners
.pop_front();
128 // ------------------------------------------------------------------------
130 void X11Clipboard::clearContents()
132 ClearableMutexGuard
aGuard(m_rSelectionManager
.getMutex());
133 // protect against deletion during outside call
134 Reference
< XClipboard
> xThis( static_cast<XClipboard
*>(this));
135 // copy member references on stack so they can be called
136 // without having the mutex
137 Reference
< XClipboardOwner
> xOwner( m_aOwner
);
138 Reference
< XTransferable
> xTrans( m_aContents
);
146 // inform previous owner of lost ownership
148 xOwner
->lostOwnership(xThis
, m_aContents
);
151 // ------------------------------------------------------------------------
153 Reference
< XTransferable
> SAL_CALL
X11Clipboard::getContents()
154 throw(RuntimeException
)
156 MutexGuard
aGuard(m_rSelectionManager
.getMutex());
158 if( ! m_aContents
.is() )
159 m_aContents
= new X11Transferable( SelectionManager::get(), static_cast< OWeakObject
* >(this), m_aSelection
);
163 // ------------------------------------------------------------------------
165 void SAL_CALL
X11Clipboard::setContents(
166 const Reference
< XTransferable
>& xTrans
,
167 const Reference
< XClipboardOwner
>& xClipboardOwner
)
168 throw(RuntimeException
)
170 // remember old values for callbacks before setting the new ones.
171 ClearableMutexGuard
aGuard(m_rSelectionManager
.getMutex());
173 Reference
< XClipboardOwner
> oldOwner( m_aOwner
);
174 m_aOwner
= xClipboardOwner
;
176 Reference
< XTransferable
> oldContents( m_aContents
);
177 m_aContents
= xTrans
;
181 // for now request ownership for both selections
182 if( m_aSelection
!= None
)
183 m_rSelectionManager
.requestOwnership( m_aSelection
);
186 m_rSelectionManager
.requestOwnership( XA_PRIMARY
);
187 m_rSelectionManager
.requestOwnership( m_rSelectionManager
.getAtom( OUString::createFromAscii( "CLIPBOARD" ) ) );
190 // notify old owner on loss of ownership
192 oldOwner
->lostOwnership(static_cast < XClipboard
* > (this), oldContents
);
194 // notify all listeners on content changes
195 fireChangedContentsEvent();
198 // ------------------------------------------------------------------------
200 OUString SAL_CALL
X11Clipboard::getName()
201 throw(RuntimeException
)
203 return m_rSelectionManager
.getString( m_aSelection
);
206 // ------------------------------------------------------------------------
208 sal_Int8 SAL_CALL
X11Clipboard::getRenderingCapabilities()
209 throw(RuntimeException
)
211 return RenderingCapabilities::Delayed
;
215 // ------------------------------------------------------------------------
216 void SAL_CALL
X11Clipboard::addClipboardListener( const Reference
< XClipboardListener
>& listener
)
217 throw(RuntimeException
)
219 MutexGuard
aGuard( m_rSelectionManager
.getMutex() );
220 m_aListeners
.push_back( listener
);
223 // ------------------------------------------------------------------------
225 void SAL_CALL
X11Clipboard::removeClipboardListener( const Reference
< XClipboardListener
>& listener
)
226 throw(RuntimeException
)
228 MutexGuard
aGuard( m_rSelectionManager
.getMutex() );
229 m_aListeners
.remove( listener
);
233 // ------------------------------------------------------------------------
235 Reference
< XTransferable
> X11Clipboard::getTransferable()
237 return getContents();
240 // ------------------------------------------------------------------------
242 void X11Clipboard::clearTransferable()
247 // ------------------------------------------------------------------------
249 void X11Clipboard::fireContentsChanged()
251 fireChangedContentsEvent();
254 // ------------------------------------------------------------------------
256 Reference
< XInterface
> X11Clipboard::getReference() throw()
258 return Reference
< XInterface
>( static_cast< OWeakObject
* >(this) );
261 // ------------------------------------------------------------------------
263 OUString SAL_CALL
X11Clipboard::getImplementationName( )
264 throw(RuntimeException
)
266 return OUString::createFromAscii(X11_CLIPBOARD_IMPLEMENTATION_NAME
);
269 // ------------------------------------------------------------------------
271 sal_Bool SAL_CALL
X11Clipboard::supportsService( const OUString
& ServiceName
)
272 throw(RuntimeException
)
274 Sequence
< OUString
> SupportedServicesNames
= X11Clipboard_getSupportedServiceNames();
276 for ( sal_Int32 n
= SupportedServicesNames
.getLength(); n
--; )
277 if (SupportedServicesNames
[n
].compareTo(ServiceName
) == 0)
283 // ------------------------------------------------------------------------
285 void SAL_CALL
X11Clipboard::initialize( const Sequence
< Any
>& ) throw( ::com::sun::star::uno::Exception
)
289 // ------------------------------------------------------------------------
291 Sequence
< OUString
> SAL_CALL
X11Clipboard::getSupportedServiceNames( )
292 throw(RuntimeException
)
294 return X11Clipboard_getSupportedServiceNames();