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