1*48123e16SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*48123e16SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*48123e16SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*48123e16SAndrew Rist  * distributed with this work for additional information
6*48123e16SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*48123e16SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*48123e16SAndrew Rist  * "License"); you may not use this file except in compliance
9*48123e16SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*48123e16SAndrew Rist  *
11*48123e16SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*48123e16SAndrew Rist  *
13*48123e16SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*48123e16SAndrew Rist  * software distributed under the License is distributed on an
15*48123e16SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*48123e16SAndrew Rist  * KIND, either express or implied.  See the License for the
17*48123e16SAndrew Rist  * specific language governing permissions and limitations
18*48123e16SAndrew Rist  * under the License.
19*48123e16SAndrew Rist  *
20*48123e16SAndrew Rist  *************************************************************/
21*48123e16SAndrew Rist 
22*48123e16SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_dtrans.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir //------------------------------------------------------------------------
28cdf0e10cSrcweir // includes
29cdf0e10cSrcweir //------------------------------------------------------------------------
30cdf0e10cSrcweir #include <osl/diagnose.h>
31cdf0e10cSrcweir #include "WinClipboard.hxx"
32cdf0e10cSrcweir #include <com/sun/star/datatransfer/clipboard/ClipboardEvent.hpp>
33cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
34cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp>
35cdf0e10cSrcweir #include "WinClipbImpl.hxx"
36cdf0e10cSrcweir 
37cdf0e10cSrcweir //------------------------------------------------------------------------
38cdf0e10cSrcweir // namespace directives
39cdf0e10cSrcweir //------------------------------------------------------------------------
40cdf0e10cSrcweir 
41cdf0e10cSrcweir using namespace rtl;
42cdf0e10cSrcweir using namespace osl;
43cdf0e10cSrcweir using namespace std;
44cdf0e10cSrcweir using namespace cppu;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir using namespace com::sun::star::uno;
47cdf0e10cSrcweir using namespace com::sun::star::datatransfer;
48cdf0e10cSrcweir using namespace com::sun::star::datatransfer::clipboard;
49cdf0e10cSrcweir using namespace com::sun::star::lang;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir //------------------------------------------------------------------------
52cdf0e10cSrcweir // defines
53cdf0e10cSrcweir //------------------------------------------------------------------------
54cdf0e10cSrcweir 
55cdf0e10cSrcweir #define WINCLIPBOARD_IMPL_NAME  "com.sun.star.datatransfer.clipboard.ClipboardW32"
56cdf0e10cSrcweir 
57cdf0e10cSrcweir //------------------------------------------------------------------------
58cdf0e10cSrcweir // helper functions
59cdf0e10cSrcweir //------------------------------------------------------------------------
60cdf0e10cSrcweir 
61cdf0e10cSrcweir namespace
62cdf0e10cSrcweir {
63cdf0e10cSrcweir 	Sequence< OUString > SAL_CALL WinClipboard_getSupportedServiceNames()
64cdf0e10cSrcweir 	{
65cdf0e10cSrcweir 		Sequence< OUString > aRet(1);
66cdf0e10cSrcweir 		aRet[0] = OUString::createFromAscii("com.sun.star.datatransfer.clipboard.SystemClipboard");
67cdf0e10cSrcweir 		return aRet;
68cdf0e10cSrcweir 	}
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir //------------------------------------------------------------------------
72cdf0e10cSrcweir // ctor
73cdf0e10cSrcweir //------------------------------------------------------------------------
74cdf0e10cSrcweir /*XEventListener,*/
75cdf0e10cSrcweir CWinClipboard::CWinClipboard( const Reference< XMultiServiceFactory >& rServiceManager, const OUString& aClipboardName ) :
76cdf0e10cSrcweir 	WeakComponentImplHelper4< XClipboardEx, XFlushableClipboard, XClipboardNotifier, XServiceInfo >( m_aCbListenerMutex ),
77cdf0e10cSrcweir 	m_SrvMgr( rServiceManager )
78cdf0e10cSrcweir {
79cdf0e10cSrcweir 	m_pImpl.reset( new CWinClipbImpl( aClipboardName, this ) );
80cdf0e10cSrcweir }
81cdf0e10cSrcweir 
82cdf0e10cSrcweir //========================================================================
83cdf0e10cSrcweir // XClipboard
84cdf0e10cSrcweir //========================================================================
85cdf0e10cSrcweir 
86cdf0e10cSrcweir //------------------------------------------------------------------------
87cdf0e10cSrcweir // getContent
88cdf0e10cSrcweir // to avoid unecessary traffic we check first if there is a clipboard
89cdf0e10cSrcweir // content which was set via setContent, in this case we don't need
90cdf0e10cSrcweir // to query the content from the clipboard, create a new wrapper object
91cdf0e10cSrcweir // and so on, we simply return the orignial XTransferable instead of our
92cdf0e10cSrcweir // DOTransferable
93cdf0e10cSrcweir //------------------------------------------------------------------------
94cdf0e10cSrcweir 
95cdf0e10cSrcweir Reference< XTransferable > SAL_CALL CWinClipboard::getContents( ) throw( RuntimeException )
96cdf0e10cSrcweir {
97cdf0e10cSrcweir 	MutexGuard aGuard( m_aMutex );
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 	if ( rBHelper.bDisposed )
100cdf0e10cSrcweir 		throw DisposedException( OUString::createFromAscii( "object is already disposed" ),
101cdf0e10cSrcweir 								 static_cast< XClipboardEx* >( this ) );
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 	if ( NULL != m_pImpl.get( ) )
104cdf0e10cSrcweir 		return m_pImpl->getContents( );
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 	return Reference< XTransferable >( );
107cdf0e10cSrcweir }
108cdf0e10cSrcweir 
109cdf0e10cSrcweir //------------------------------------------------------------------------
110cdf0e10cSrcweir // setContent
111cdf0e10cSrcweir //------------------------------------------------------------------------
112cdf0e10cSrcweir 
113cdf0e10cSrcweir void SAL_CALL CWinClipboard::setContents( const Reference< XTransferable >& xTransferable,
114cdf0e10cSrcweir 										  const Reference< XClipboardOwner >& xClipboardOwner )
115cdf0e10cSrcweir 										  throw( RuntimeException )
116cdf0e10cSrcweir {
117cdf0e10cSrcweir 	MutexGuard aGuard( m_aMutex );
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 	if ( rBHelper.bDisposed )
120cdf0e10cSrcweir 		throw DisposedException( OUString::createFromAscii( "object is already disposed" ),
121cdf0e10cSrcweir 								 static_cast< XClipboardEx* >( this ) );
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 	if ( NULL != m_pImpl.get( ) )
124cdf0e10cSrcweir 		m_pImpl->setContents( xTransferable, xClipboardOwner );
125cdf0e10cSrcweir }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir //------------------------------------------------------------------------
128cdf0e10cSrcweir // getName
129cdf0e10cSrcweir //------------------------------------------------------------------------
130cdf0e10cSrcweir 
131cdf0e10cSrcweir OUString SAL_CALL CWinClipboard::getName(  ) throw( RuntimeException )
132cdf0e10cSrcweir {
133cdf0e10cSrcweir 	if ( rBHelper.bDisposed )
134cdf0e10cSrcweir 		throw DisposedException( OUString::createFromAscii( "object is already disposed" ),
135cdf0e10cSrcweir 								 static_cast< XClipboardEx* >( this ) );
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 	if ( NULL != m_pImpl.get( ) )
138cdf0e10cSrcweir 		return m_pImpl->getName( );
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 	return OUString::createFromAscii( "" );
141cdf0e10cSrcweir }
142cdf0e10cSrcweir 
143cdf0e10cSrcweir //========================================================================
144cdf0e10cSrcweir // XFlushableClipboard
145cdf0e10cSrcweir //========================================================================
146cdf0e10cSrcweir 
147cdf0e10cSrcweir void SAL_CALL CWinClipboard::flushClipboard( ) throw( RuntimeException )
148cdf0e10cSrcweir {
149cdf0e10cSrcweir 	MutexGuard aGuard( m_aMutex );
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 	if ( rBHelper.bDisposed )
152cdf0e10cSrcweir 		throw DisposedException( OUString::createFromAscii( "object is already disposed" ),
153cdf0e10cSrcweir 								 static_cast< XClipboardEx* >( this ) );
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 	if ( NULL != m_pImpl.get( ) )
156cdf0e10cSrcweir 		m_pImpl->flushClipboard( );
157cdf0e10cSrcweir }
158cdf0e10cSrcweir 
159cdf0e10cSrcweir //========================================================================
160cdf0e10cSrcweir // XClipboardEx
161cdf0e10cSrcweir //========================================================================
162cdf0e10cSrcweir 
163cdf0e10cSrcweir sal_Int8 SAL_CALL CWinClipboard::getRenderingCapabilities(  ) throw( RuntimeException )
164cdf0e10cSrcweir {
165cdf0e10cSrcweir 	if ( rBHelper.bDisposed )
166cdf0e10cSrcweir 		throw DisposedException( OUString::createFromAscii( "object is already disposed" ),
167cdf0e10cSrcweir 								 static_cast< XClipboardEx* >( this ) );
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 	if ( NULL != m_pImpl.get( ) )
170cdf0e10cSrcweir 		return m_pImpl->getRenderingCapabilities( );
171cdf0e10cSrcweir 
172cdf0e10cSrcweir 	return 0;
173cdf0e10cSrcweir }
174cdf0e10cSrcweir 
175cdf0e10cSrcweir //========================================================================
176cdf0e10cSrcweir // XClipboardNotifier
177cdf0e10cSrcweir //========================================================================
178cdf0e10cSrcweir 
179cdf0e10cSrcweir //------------------------------------------------------------------------
180cdf0e10cSrcweir // getName
181cdf0e10cSrcweir //------------------------------------------------------------------------
182cdf0e10cSrcweir 
183cdf0e10cSrcweir void SAL_CALL CWinClipboard::addClipboardListener( const Reference< XClipboardListener >& listener )
184cdf0e10cSrcweir 	throw( RuntimeException )
185cdf0e10cSrcweir {
186cdf0e10cSrcweir 	if ( rBHelper.bDisposed )
187cdf0e10cSrcweir 		throw DisposedException( OUString::createFromAscii( "object is already disposed" ),
188cdf0e10cSrcweir 								 static_cast< XClipboardEx* >( this ) );
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 	// check input parameter
191cdf0e10cSrcweir 	if ( !listener.is( ) )
192cdf0e10cSrcweir 		throw IllegalArgumentException( OUString::createFromAscii( "empty reference" ),
193cdf0e10cSrcweir 										static_cast< XClipboardEx* >( this ),
194cdf0e10cSrcweir 										1 );
195cdf0e10cSrcweir 
196cdf0e10cSrcweir 	rBHelper.aLC.addInterface( getCppuType( &listener ), listener );
197cdf0e10cSrcweir }
198cdf0e10cSrcweir 
199cdf0e10cSrcweir //------------------------------------------------------------------------
200cdf0e10cSrcweir // getName
201cdf0e10cSrcweir //------------------------------------------------------------------------
202cdf0e10cSrcweir 
203cdf0e10cSrcweir void SAL_CALL CWinClipboard::removeClipboardListener( const Reference< XClipboardListener >& listener )
204cdf0e10cSrcweir 	throw( RuntimeException )
205cdf0e10cSrcweir {
206cdf0e10cSrcweir 	if ( rBHelper.bDisposed )
207cdf0e10cSrcweir 		throw DisposedException( OUString::createFromAscii( "object is already disposed" ),
208cdf0e10cSrcweir 								 static_cast< XClipboardEx* >( this ) );
209cdf0e10cSrcweir 
210cdf0e10cSrcweir 	// check input parameter
211cdf0e10cSrcweir 	if ( !listener.is( ) )
212cdf0e10cSrcweir 		throw IllegalArgumentException( OUString::createFromAscii( "empty reference" ),
213cdf0e10cSrcweir 										static_cast< XClipboardEx* >( this ),
214cdf0e10cSrcweir 										1 );
215cdf0e10cSrcweir 
216cdf0e10cSrcweir 	rBHelper.aLC.removeInterface( getCppuType( &listener ), listener );
217cdf0e10cSrcweir }
218cdf0e10cSrcweir 
219cdf0e10cSrcweir //------------------------------------------------------------------------
220cdf0e10cSrcweir // getName
221cdf0e10cSrcweir //------------------------------------------------------------------------
222cdf0e10cSrcweir 
223cdf0e10cSrcweir void SAL_CALL CWinClipboard::notifyAllClipboardListener( )
224cdf0e10cSrcweir {
225cdf0e10cSrcweir 	if ( !rBHelper.bDisposed )
226cdf0e10cSrcweir 	{
227cdf0e10cSrcweir 		ClearableMutexGuard aGuard( rBHelper.rMutex );
228cdf0e10cSrcweir 		if ( !rBHelper.bDisposed )
229cdf0e10cSrcweir 		{
230cdf0e10cSrcweir 			aGuard.clear( );
231cdf0e10cSrcweir 
232cdf0e10cSrcweir 			OInterfaceContainerHelper* pICHelper = rBHelper.aLC.getContainer(
233cdf0e10cSrcweir 				getCppuType( ( Reference< XClipboardListener > * ) 0 ) );
234cdf0e10cSrcweir 
235cdf0e10cSrcweir 			if ( pICHelper )
236cdf0e10cSrcweir 			{
237cdf0e10cSrcweir 			    try
238cdf0e10cSrcweir 			    {
239cdf0e10cSrcweir 				    OInterfaceIteratorHelper iter(*pICHelper);
240cdf0e10cSrcweir 				    Reference<XTransferable> rXTransf(m_pImpl->getContents());
241cdf0e10cSrcweir 				    ClipboardEvent aClipbEvent(static_cast<XClipboard*>(this), rXTransf);
242cdf0e10cSrcweir 
243cdf0e10cSrcweir 				    while(iter.hasMoreElements())
244cdf0e10cSrcweir 				    {
245cdf0e10cSrcweir 					    try
246cdf0e10cSrcweir 					    {
247cdf0e10cSrcweir 						    Reference<XClipboardListener> xCBListener(iter.next(), UNO_QUERY);
248cdf0e10cSrcweir 						    if (xCBListener.is())
249cdf0e10cSrcweir 							    xCBListener->changedContents(aClipbEvent);
250cdf0e10cSrcweir 					    }
251cdf0e10cSrcweir 					    catch(RuntimeException&)
252cdf0e10cSrcweir 					    {
253cdf0e10cSrcweir 						    OSL_ENSURE( false, "RuntimeException caught" );
254cdf0e10cSrcweir 					    }
255cdf0e10cSrcweir 				    }
256cdf0e10cSrcweir 				}
257cdf0e10cSrcweir 			    catch(const ::com::sun::star::lang::DisposedException&)
258cdf0e10cSrcweir 			    {
259cdf0e10cSrcweir 			        OSL_ENSURE(false, "Service Manager disposed");
260cdf0e10cSrcweir 
261cdf0e10cSrcweir 			        // no further clipboard changed notifications
262cdf0e10cSrcweir 			        m_pImpl->unregisterClipboardViewer();
263cdf0e10cSrcweir 			    }
264cdf0e10cSrcweir 
265cdf0e10cSrcweir 			} // end if
266cdf0e10cSrcweir 		} // end if
267cdf0e10cSrcweir 	} // end if
268cdf0e10cSrcweir }
269cdf0e10cSrcweir 
270cdf0e10cSrcweir //------------------------------------------------
271cdf0e10cSrcweir // overwritten base class method which will be
272cdf0e10cSrcweir // called by the base class dispose method
273cdf0e10cSrcweir //------------------------------------------------
274cdf0e10cSrcweir 
275cdf0e10cSrcweir void SAL_CALL CWinClipboard::disposing()
276cdf0e10cSrcweir {
277cdf0e10cSrcweir 	// do my own stuff
278cdf0e10cSrcweir 	m_pImpl->dispose( );
279cdf0e10cSrcweir 
280cdf0e10cSrcweir 	// force destruction of the impl class
281cdf0e10cSrcweir 	m_pImpl.reset( NULL );
282cdf0e10cSrcweir }
283cdf0e10cSrcweir 
284cdf0e10cSrcweir // -------------------------------------------------
285cdf0e10cSrcweir // XServiceInfo
286cdf0e10cSrcweir // -------------------------------------------------
287cdf0e10cSrcweir 
288cdf0e10cSrcweir OUString SAL_CALL CWinClipboard::getImplementationName(  )
289cdf0e10cSrcweir 	throw(RuntimeException)
290cdf0e10cSrcweir {
291cdf0e10cSrcweir 	return OUString::createFromAscii( WINCLIPBOARD_IMPL_NAME );
292cdf0e10cSrcweir }
293cdf0e10cSrcweir 
294cdf0e10cSrcweir // -------------------------------------------------
295cdf0e10cSrcweir //	XServiceInfo
296cdf0e10cSrcweir // -------------------------------------------------
297cdf0e10cSrcweir 
298cdf0e10cSrcweir sal_Bool SAL_CALL CWinClipboard::supportsService( const OUString& ServiceName )
299cdf0e10cSrcweir 	throw(RuntimeException)
300cdf0e10cSrcweir {
301cdf0e10cSrcweir 	Sequence < OUString > SupportedServicesNames = WinClipboard_getSupportedServiceNames();
302cdf0e10cSrcweir 
303cdf0e10cSrcweir 	for ( sal_Int32 n = SupportedServicesNames.getLength(); n--; )
304cdf0e10cSrcweir 		if (SupportedServicesNames[n].compareTo(ServiceName) == 0)
305cdf0e10cSrcweir 			return sal_True;
306cdf0e10cSrcweir 
307cdf0e10cSrcweir 	return sal_False;
308cdf0e10cSrcweir }
309cdf0e10cSrcweir 
310cdf0e10cSrcweir // -------------------------------------------------
311cdf0e10cSrcweir //	XServiceInfo
312cdf0e10cSrcweir // -------------------------------------------------
313cdf0e10cSrcweir 
314cdf0e10cSrcweir Sequence< OUString > SAL_CALL CWinClipboard::getSupportedServiceNames(	 )
315cdf0e10cSrcweir 	throw(RuntimeException)
316cdf0e10cSrcweir {
317cdf0e10cSrcweir 	return WinClipboard_getSupportedServiceNames();
318cdf0e10cSrcweir }
319