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