1cdf0e10cSrcweir /*************************************************************************
2cdf0e10cSrcweir  *
3cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4cdf0e10cSrcweir  *
5cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6cdf0e10cSrcweir  *
7cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8cdf0e10cSrcweir  *
9cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10cdf0e10cSrcweir  *
11cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14cdf0e10cSrcweir  *
15cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20cdf0e10cSrcweir  *
21cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25cdf0e10cSrcweir  *
26cdf0e10cSrcweir  ************************************************************************/
27cdf0e10cSrcweir 
28cdf0e10cSrcweir //------------------------------------------------------------------------
29cdf0e10cSrcweir // includes
30cdf0e10cSrcweir //------------------------------------------------------------------------
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include "Os2Clipboard.hxx"
33cdf0e10cSrcweir 
34cdf0e10cSrcweir //------------------------------------------------------------------------
35cdf0e10cSrcweir // namespace directives
36cdf0e10cSrcweir //------------------------------------------------------------------------
37cdf0e10cSrcweir 
38cdf0e10cSrcweir using namespace com::sun::star::datatransfer;
39cdf0e10cSrcweir using namespace com::sun::star::datatransfer::clipboard;
40cdf0e10cSrcweir using namespace com::sun::star::datatransfer::clipboard::RenderingCapabilities;
41cdf0e10cSrcweir using namespace com::sun::star::lang;
42cdf0e10cSrcweir using namespace com::sun::star::uno;
43cdf0e10cSrcweir using namespace cppu;
44cdf0e10cSrcweir using namespace osl;
45cdf0e10cSrcweir using namespace rtl;
46cdf0e10cSrcweir using namespace os2;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir const Type CPPUTYPE_SEQINT8	 = getCppuType( ( Sequence< sal_Int8 >* )0 );
49cdf0e10cSrcweir const Type CPPUTYPE_OUSTRING = getCppuType( (OUString*)0 );
50cdf0e10cSrcweir 
51cdf0e10cSrcweir #define DTRANS_OBJ_CLASSNAME "DTRANSOBJWND"
52cdf0e10cSrcweir 
53cdf0e10cSrcweir // -----------------------------------------------------------------------
54cdf0e10cSrcweir 
55cdf0e10cSrcweir inline void SetWindowPtr( HWND hWnd, Os2Clipboard* pThis )
56cdf0e10cSrcweir {
57cdf0e10cSrcweir 	WinSetWindowULong( hWnd, QWL_USER, (ULONG)pThis );
58cdf0e10cSrcweir }
59cdf0e10cSrcweir 
60cdf0e10cSrcweir inline Os2Clipboard* GetWindowPtr( HWND hWnd )
61cdf0e10cSrcweir {
62cdf0e10cSrcweir 	return (Os2Clipboard*)WinQueryWindowULong( hWnd, QWL_USER );
63cdf0e10cSrcweir }
64cdf0e10cSrcweir 
65cdf0e10cSrcweir // -----------------------------------------------------------------------
66cdf0e10cSrcweir 
67cdf0e10cSrcweir MRESULT EXPENTRY DtransObjWndProc( HWND hWnd, ULONG nMsg, MPARAM nMP1, MPARAM nMP2 )
68cdf0e10cSrcweir {
69cdf0e10cSrcweir 
70cdf0e10cSrcweir 	switch ( nMsg )
71cdf0e10cSrcweir 	{
72cdf0e10cSrcweir 	case WM_DRAWCLIPBOARD:	// clipboard content has changed
73cdf0e10cSrcweir 		{
74cdf0e10cSrcweir 			Os2Clipboard* os2Clipboard = GetWindowPtr( hWnd);
75cdf0e10cSrcweir 			if (os2Clipboard)
76cdf0e10cSrcweir 			{
77cdf0e10cSrcweir 				//MutexGuard aGuard(os2Clipboard->m_aMutex);
78cdf0e10cSrcweir 				debug_printf("WM_DRAWCLIPBOARD os2Clipboard %08x\n", os2Clipboard);
79cdf0e10cSrcweir 				if (os2Clipboard->m_bInSetClipboardData)
80cdf0e10cSrcweir 				{
81cdf0e10cSrcweir 					debug_printf("WM_DRAWCLIPBOARD our change\n");
82cdf0e10cSrcweir 				}
83cdf0e10cSrcweir 				else
84cdf0e10cSrcweir 				{
85cdf0e10cSrcweir 					// notify listener for clipboard change
86cdf0e10cSrcweir 					debug_printf("WM_DRAWCLIPBOARD notify change\n");
87cdf0e10cSrcweir 					os2Clipboard->notifyAllClipboardListener();
88cdf0e10cSrcweir 				}
89cdf0e10cSrcweir 			}
90cdf0e10cSrcweir 		}
91cdf0e10cSrcweir 		break;
92cdf0e10cSrcweir 	}
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 	return WinDefWindowProc( hWnd, nMsg, nMP1, nMP2 );
95cdf0e10cSrcweir }
96cdf0e10cSrcweir 
97cdf0e10cSrcweir // -----------------------------------------------------------------------
98cdf0e10cSrcweir 
99cdf0e10cSrcweir Os2Clipboard::Os2Clipboard() :
100cdf0e10cSrcweir 	m_aMutex(),
101cdf0e10cSrcweir 	WeakComponentImplHelper4< XClipboardEx, XClipboardNotifier, XServiceInfo, XInitialization > (m_aMutex),
102cdf0e10cSrcweir 	m_bInitialized(sal_False),
103cdf0e10cSrcweir 	m_bInSetClipboardData(sal_False)
104cdf0e10cSrcweir {
105cdf0e10cSrcweir 	MutexGuard aGuard(m_aMutex);
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 	debug_printf("Os2Clipboard::Os2Clipboard\n");
108cdf0e10cSrcweir 	hAB = WinQueryAnchorBlock( HWND_DESKTOP );
109cdf0e10cSrcweir 	hText = 0;
110cdf0e10cSrcweir 	hBitmap = 0;
111cdf0e10cSrcweir 
112cdf0e10cSrcweir #if 0
113cdf0e10cSrcweir 	// register object class
114cdf0e10cSrcweir 	if ( WinRegisterClass( hAB, (PSZ)DTRANS_OBJ_CLASSNAME,
115cdf0e10cSrcweir 							(PFNWP)DtransObjWndProc, 0, sizeof(ULONG) ))
116cdf0e10cSrcweir 	{
117cdf0e10cSrcweir 		APIRET	rc;
118cdf0e10cSrcweir 		// create object window to get clip viewer messages
119cdf0e10cSrcweir 		hObjWnd = WinCreateWindow( HWND_OBJECT, (PCSZ)DTRANS_OBJ_CLASSNAME,
120cdf0e10cSrcweir 										(PCSZ)"", 0, 0, 0, 0, 0,
121cdf0e10cSrcweir 										HWND_OBJECT, HWND_TOP,
122cdf0e10cSrcweir 										222, NULL, NULL);
123cdf0e10cSrcweir 		// store pointer
124cdf0e10cSrcweir 		SetWindowPtr( hObjWnd, this);
125cdf0e10cSrcweir 		// register the viewer window
126cdf0e10cSrcweir 		rc = WinOpenClipbrd(hAB);
127cdf0e10cSrcweir 		rc = WinSetClipbrdViewer(hAB, hObjWnd);
128cdf0e10cSrcweir 		rc = WinCloseClipbrd(hAB);
129cdf0e10cSrcweir 	}
130cdf0e10cSrcweir #endif
131cdf0e10cSrcweir 
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir Os2Clipboard::~Os2Clipboard()
135cdf0e10cSrcweir {
136cdf0e10cSrcweir 	debug_printf("Os2Clipboard::~Os2Clipboard\n");
137cdf0e10cSrcweir }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir void SAL_CALL Os2Clipboard::initialize( const Sequence< Any >& aArguments )
140cdf0e10cSrcweir 	throw(Exception, RuntimeException)
141cdf0e10cSrcweir {
142cdf0e10cSrcweir 	if (!m_bInitialized)
143cdf0e10cSrcweir 	{
144cdf0e10cSrcweir 		for (sal_Int32 n = 0, nmax = aArguments.getLength(); n < nmax; n++)
145cdf0e10cSrcweir 			if (aArguments[n].getValueType() == getCppuType((OUString *) 0))
146cdf0e10cSrcweir 			{
147cdf0e10cSrcweir 				aArguments[0] >>= m_aName;
148cdf0e10cSrcweir 				break;
149cdf0e10cSrcweir 			}
150cdf0e10cSrcweir 	}
151cdf0e10cSrcweir }
152cdf0e10cSrcweir 
153cdf0e10cSrcweir OUString SAL_CALL Os2Clipboard::getImplementationName() throw( RuntimeException )
154cdf0e10cSrcweir {
155cdf0e10cSrcweir 	debug_printf("Os2Clipboard::getImplementationName\n");
156cdf0e10cSrcweir 	return OUString::createFromAscii( OS2_CLIPBOARD_IMPL_NAME );
157cdf0e10cSrcweir }
158cdf0e10cSrcweir 
159cdf0e10cSrcweir sal_Bool SAL_CALL Os2Clipboard::supportsService( const OUString& ServiceName ) throw( RuntimeException )
160cdf0e10cSrcweir {
161cdf0e10cSrcweir 	debug_printf("Os2Clipboard::supportsService\n");
162cdf0e10cSrcweir 	Sequence < OUString > SupportedServicesNames = Os2Clipboard_getSupportedServiceNames();
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 	for ( sal_Int32 n = SupportedServicesNames.getLength(); n--; )
165cdf0e10cSrcweir 		if (SupportedServicesNames[n].compareTo(ServiceName) == 0)
166cdf0e10cSrcweir 			return sal_True;
167cdf0e10cSrcweir 
168cdf0e10cSrcweir 	return sal_False;
169cdf0e10cSrcweir }
170cdf0e10cSrcweir 
171cdf0e10cSrcweir Sequence< OUString > SAL_CALL Os2Clipboard::getSupportedServiceNames() throw( RuntimeException )
172cdf0e10cSrcweir {
173cdf0e10cSrcweir 	debug_printf("Os2Clipboard::getSupportedServiceNames\n");
174cdf0e10cSrcweir 	return Os2Clipboard_getSupportedServiceNames();
175cdf0e10cSrcweir }
176cdf0e10cSrcweir 
177cdf0e10cSrcweir Reference< XTransferable > SAL_CALL Os2Clipboard::getContents() throw( RuntimeException )
178cdf0e10cSrcweir {
179cdf0e10cSrcweir 	debug_printf("Os2Clipboard::getContents\n");
180cdf0e10cSrcweir 	MutexGuard aGuard(m_aMutex);
181cdf0e10cSrcweir 
182cdf0e10cSrcweir 	// os2 can have only one viewer at time, and we don't get a notification
183cdf0e10cSrcweir 	// when the viewer changes. So we need to check handles of clipboard
184cdf0e10cSrcweir 	// data and compare with previous handles
185cdf0e10cSrcweir 	if (UWinOpenClipbrd(hAB)) {
186cdf0e10cSrcweir 		sal_Bool	fireChanged = sal_False;
187cdf0e10cSrcweir 		ULONG handle = UWinQueryClipbrdData( hAB, UCLIP_CF_UNICODETEXT);
188cdf0e10cSrcweir 		if (handle) {
189cdf0e10cSrcweir 			if (handle != hText) {
190cdf0e10cSrcweir 				hText = handle;
191cdf0e10cSrcweir 				fireChanged = sal_True;
192cdf0e10cSrcweir 			}
193cdf0e10cSrcweir 		}
194cdf0e10cSrcweir 		handle = UWinQueryClipbrdData( hAB, UCLIP_CF_BITMAP);
195cdf0e10cSrcweir 		if (handle) {
196cdf0e10cSrcweir 			if (handle != hBitmap) {
197cdf0e10cSrcweir 				hBitmap = handle;
198cdf0e10cSrcweir 				fireChanged = sal_True;
199cdf0e10cSrcweir 			}
200cdf0e10cSrcweir 		}
201cdf0e10cSrcweir 		UWinCloseClipbrd( hAB);
202cdf0e10cSrcweir 		if (fireChanged)
203cdf0e10cSrcweir 		{
204cdf0e10cSrcweir 			// notify listener for clipboard change
205cdf0e10cSrcweir 			debug_printf("Os2Clipboard::getContents notify change\n");
206cdf0e10cSrcweir 			notifyAllClipboardListener();
207cdf0e10cSrcweir 		}
208cdf0e10cSrcweir 	}
209cdf0e10cSrcweir 
210cdf0e10cSrcweir 	if( ! m_aContents.is() )
211cdf0e10cSrcweir 		m_aContents = new Os2Transferable( static_cast< OWeakObject* >(this) );
212cdf0e10cSrcweir 
213cdf0e10cSrcweir 	return m_aContents;
214cdf0e10cSrcweir }
215cdf0e10cSrcweir 
216cdf0e10cSrcweir void SAL_CALL Os2Clipboard::setContents( const Reference< XTransferable >& xTrans, const Reference< XClipboardOwner >& xClipboardOwner ) throw( RuntimeException )
217cdf0e10cSrcweir {
218cdf0e10cSrcweir 	debug_printf("Os2Clipboard::setContents\n");
219cdf0e10cSrcweir 	// remember old values for callbacks before setting the new ones.
220cdf0e10cSrcweir 	ClearableMutexGuard aGuard(m_aMutex);
221cdf0e10cSrcweir 
222cdf0e10cSrcweir 	Reference< XClipboardOwner > oldOwner(m_aOwner);
223cdf0e10cSrcweir 	m_aOwner = xClipboardOwner;
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 	Reference< XTransferable > oldContents(m_aContents);
226cdf0e10cSrcweir 	m_aContents = xTrans;
227cdf0e10cSrcweir 
228cdf0e10cSrcweir 	aGuard.clear();
229cdf0e10cSrcweir 
230cdf0e10cSrcweir 	// notify old owner on loss of ownership
231cdf0e10cSrcweir 	if( oldOwner.is() )
232cdf0e10cSrcweir 		oldOwner->lostOwnership(static_cast < XClipboard * > (this), oldContents);
233cdf0e10cSrcweir 
234cdf0e10cSrcweir 	// notify all listeners on content changes
235cdf0e10cSrcweir 	OInterfaceContainerHelper *pContainer =
236cdf0e10cSrcweir 		rBHelper.aLC.getContainer(getCppuType( (Reference < XClipboardListener > *) 0));
237cdf0e10cSrcweir 	if (pContainer)
238cdf0e10cSrcweir 	{
239cdf0e10cSrcweir 		ClipboardEvent aEvent(static_cast < XClipboard * > (this), m_aContents);
240cdf0e10cSrcweir 		OInterfaceIteratorHelper aIterator(*pContainer);
241cdf0e10cSrcweir 
242cdf0e10cSrcweir 		while (aIterator.hasMoreElements())
243cdf0e10cSrcweir 		{
244cdf0e10cSrcweir 			Reference < XClipboardListener > xListener(aIterator.next(), UNO_QUERY);
245cdf0e10cSrcweir 			if (xListener.is())
246cdf0e10cSrcweir 				xListener->changedContents(aEvent);
247cdf0e10cSrcweir 		}
248cdf0e10cSrcweir 	}
249cdf0e10cSrcweir 
250cdf0e10cSrcweir #if OSL_DEBUG_LEVEL>0
251cdf0e10cSrcweir 	// dump list of available mimetypes
252cdf0e10cSrcweir 	Sequence< DataFlavor > aFlavors( m_aContents->getTransferDataFlavors() );
253cdf0e10cSrcweir 	for( int i = 0; i < aFlavors.getLength(); i++ )
254cdf0e10cSrcweir 		debug_printf("Os2Clipboard::setContents available mimetype: %d %s\n",
255cdf0e10cSrcweir 			i, CHAR_POINTER(aFlavors.getConstArray()[i].MimeType));
256cdf0e10cSrcweir #endif
257cdf0e10cSrcweir 
258cdf0e10cSrcweir 	// we can only export text or bitmap
259cdf0e10cSrcweir 	DataFlavor nFlavorText( OUString::createFromAscii( "text/plain;charset=utf-16" ),
260cdf0e10cSrcweir 						OUString::createFromAscii( "Unicode-Text" ), CPPUTYPE_OUSTRING);
261cdf0e10cSrcweir 	DataFlavor nFlavorBitmap( OUString::createFromAscii( "application/x-openoffice-bitmap;windows_formatname=\"Bitmap\"" ),
262cdf0e10cSrcweir 						OUString::createFromAscii( "Bitmap" ), CPPUTYPE_DEFAULT);
263cdf0e10cSrcweir 
264cdf0e10cSrcweir 	// try text transfer data (if any)
265cdf0e10cSrcweir 	PSZ pSharedText = NULL;
266cdf0e10cSrcweir 	HBITMAP hbm = NULL;
267cdf0e10cSrcweir 	try
268cdf0e10cSrcweir 	{
269cdf0e10cSrcweir 		Any aAny = m_aContents->getTransferData( nFlavorText );
270cdf0e10cSrcweir 		if (aAny.hasValue())
271cdf0e10cSrcweir 		{
272cdf0e10cSrcweir 			APIRET rc;
273cdf0e10cSrcweir 			// copy unicode text to clipboard
274cdf0e10cSrcweir 			OUString aString;
275cdf0e10cSrcweir 			aAny >>= aString;
276cdf0e10cSrcweir 			// share text
277cdf0e10cSrcweir 			rc = DosAllocSharedMem( (PPVOID) &pSharedText, NULL,
278cdf0e10cSrcweir 				aString.getLength() * 2 + 2,
279cdf0e10cSrcweir 				PAG_WRITE | PAG_COMMIT | OBJ_GIVEABLE | OBJ_ANY);
280cdf0e10cSrcweir 			if (!rc)
281cdf0e10cSrcweir 				memcpy( pSharedText, aString.getStr(), aString.getLength() * 2 + 2 );
282cdf0e10cSrcweir 			else
283cdf0e10cSrcweir 				pSharedText = NULL;
284cdf0e10cSrcweir 			debug_printf("Os2Clipboard::setContents SetClipbrdData text done\n");
285cdf0e10cSrcweir 		}
286cdf0e10cSrcweir 	} catch ( UnsupportedFlavorException&) {
287cdf0e10cSrcweir 		debug_printf("Os2Clipboard::setContents UnsupportedFlavorException (no text)\n");
288cdf0e10cSrcweir 	}
289cdf0e10cSrcweir 
290*a7e9c4d8SPedro Giffuni 	// try bitmap transfer data (if any)
291*a7e9c4d8SPedro Giffuni 	try
292*a7e9c4d8SPedro Giffuni 	{
293*a7e9c4d8SPedro Giffuni 		Any aAnyB = m_aContents->getTransferData( nFlavorBitmap );
294*a7e9c4d8SPedro Giffuni 		if (aAnyB.hasValue())
295*a7e9c4d8SPedro Giffuni 		{
296*a7e9c4d8SPedro Giffuni 			hbm = OOoBmpToOS2Handle( aAnyB);
297*a7e9c4d8SPedro Giffuni 			debug_printf("Os2Clipboard::setContents SetClipbrdData bitmap done\n");
298*a7e9c4d8SPedro Giffuni 		}
299*a7e9c4d8SPedro Giffuni 	} catch ( UnsupportedFlavorException&) {
300*a7e9c4d8SPedro Giffuni 		debug_printf("Os2Clipboard::setContents UnsupportedFlavorException (no bitmap)\n");
301*a7e9c4d8SPedro Giffuni 	}
302*a7e9c4d8SPedro Giffuni 
303cdf0e10cSrcweir 	// copy to clipboard
304cdf0e10cSrcweir 	if ( UWinOpenClipbrd( hAB) && (pSharedText || hbm))
305cdf0e10cSrcweir 	{
306cdf0e10cSrcweir 		// set the flag, so we will ignore the next WM_DRAWCLIPBOARD
307cdf0e10cSrcweir 		// since we generate it with following code.
308cdf0e10cSrcweir 		m_bInSetClipboardData = sal_True;
309cdf0e10cSrcweir 		UWinEmptyClipbrd( hAB);
310cdf0e10cSrcweir 		// give pointer to clipboard (it will become owner of pSharedText!)
311cdf0e10cSrcweir 		if (pSharedText) {
312cdf0e10cSrcweir 			UWinSetClipbrdData( hAB, (ULONG) pSharedText, UCLIP_CF_UNICODETEXT, CFI_POINTER);
313cdf0e10cSrcweir 			// update internal handle to avoid detection of this text as new data
314cdf0e10cSrcweir 			hText = (ULONG)pSharedText;
315cdf0e10cSrcweir 		}
316cdf0e10cSrcweir 		// give bitmap to clipboard
317cdf0e10cSrcweir 		if (hbm) {
318cdf0e10cSrcweir 			UWinSetClipbrdData( hAB, (ULONG) hbm, UCLIP_CF_BITMAP, CFI_HANDLE);
319cdf0e10cSrcweir 			// update internal handle to avoid detection of this bitmap as new data
320cdf0e10cSrcweir 			hBitmap = hbm;
321cdf0e10cSrcweir 		}
322cdf0e10cSrcweir 		// reset the flag, so we will not ignore next WM_DRAWCLIPBOARD
323cdf0e10cSrcweir 		m_bInSetClipboardData = sal_False;
324cdf0e10cSrcweir 		UWinCloseClipbrd( hAB);
325cdf0e10cSrcweir 	}
326cdf0e10cSrcweir 
327cdf0e10cSrcweir }
328cdf0e10cSrcweir 
329cdf0e10cSrcweir OUString SAL_CALL Os2Clipboard::getName() throw( RuntimeException )
330cdf0e10cSrcweir {
331cdf0e10cSrcweir 	debug_printf("Os2Clipboard::getName\n");
332cdf0e10cSrcweir 	return m_aName;
333cdf0e10cSrcweir }
334cdf0e10cSrcweir 
335cdf0e10cSrcweir sal_Int8 SAL_CALL Os2Clipboard::getRenderingCapabilities() throw( RuntimeException )
336cdf0e10cSrcweir {
337cdf0e10cSrcweir 	debug_printf("Os2Clipboard::getRenderingCapabilities\n");
338cdf0e10cSrcweir 	return Delayed;
339cdf0e10cSrcweir }
340cdf0e10cSrcweir 
341cdf0e10cSrcweir //========================================================================
342cdf0e10cSrcweir // XClipboardNotifier
343cdf0e10cSrcweir //========================================================================
344cdf0e10cSrcweir 
345cdf0e10cSrcweir void SAL_CALL Os2Clipboard::addClipboardListener( const Reference< XClipboardListener >& listener ) throw( RuntimeException )
346cdf0e10cSrcweir {
347cdf0e10cSrcweir 	debug_printf("Os2Clipboard::addClipboardListener\n");
348cdf0e10cSrcweir 	MutexGuard aGuard( rBHelper.rMutex );
349cdf0e10cSrcweir 	OSL_ENSURE( !rBHelper.bInDispose, "do not add listeners in the dispose call" );
350cdf0e10cSrcweir 	OSL_ENSURE( !rBHelper.bDisposed, "object is disposed" );
351cdf0e10cSrcweir 	if (!rBHelper.bInDispose && !rBHelper.bDisposed)
352cdf0e10cSrcweir 		rBHelper.aLC.addInterface( getCppuType( (const ::com::sun::star::uno::Reference< XClipboardListener > *) 0), listener );
353cdf0e10cSrcweir }
354cdf0e10cSrcweir 
355cdf0e10cSrcweir void SAL_CALL Os2Clipboard::removeClipboardListener( const Reference< XClipboardListener >& listener ) throw( RuntimeException )
356cdf0e10cSrcweir {
357cdf0e10cSrcweir 	debug_printf("Os2Clipboard::removeClipboardListener\n");
358cdf0e10cSrcweir 	MutexGuard aGuard( rBHelper.rMutex );
359cdf0e10cSrcweir 	OSL_ENSURE( !rBHelper.bDisposed, "object is disposed" );
360cdf0e10cSrcweir 	if (!rBHelper.bInDispose && !rBHelper.bDisposed)
361cdf0e10cSrcweir 		rBHelper.aLC.removeInterface( getCppuType( (const Reference< XClipboardListener > *) 0 ), listener ); \
362cdf0e10cSrcweir }
363cdf0e10cSrcweir 
364cdf0e10cSrcweir // ------------------------------------------------------------------------
365cdf0e10cSrcweir 
366cdf0e10cSrcweir void SAL_CALL Os2Clipboard::notifyAllClipboardListener( )
367cdf0e10cSrcweir {
368cdf0e10cSrcweir 	if ( !rBHelper.bDisposed )
369cdf0e10cSrcweir 	{
370cdf0e10cSrcweir 		ClearableMutexGuard aGuard( rBHelper.rMutex );
371cdf0e10cSrcweir 		if ( !rBHelper.bDisposed )
372cdf0e10cSrcweir 		{
373cdf0e10cSrcweir 			aGuard.clear( );
374cdf0e10cSrcweir 
375cdf0e10cSrcweir 			ClearableMutexGuard aGuard(m_aMutex);
376cdf0e10cSrcweir 			// copy member references on stack so they can be called
377cdf0e10cSrcweir 			// without having the mutex
378cdf0e10cSrcweir 			Reference< XClipboardOwner > xOwner( m_aOwner );
379cdf0e10cSrcweir 			Reference< XTransferable > xTrans( m_aContents );
380cdf0e10cSrcweir 			// clear members
381cdf0e10cSrcweir 			m_aOwner.clear();
382cdf0e10cSrcweir 			m_aContents.clear();
383cdf0e10cSrcweir 			// release the mutex
384cdf0e10cSrcweir 			aGuard.clear();
385cdf0e10cSrcweir 
386cdf0e10cSrcweir 			// inform previous owner of lost ownership
387cdf0e10cSrcweir 			if ( xOwner.is() )
388cdf0e10cSrcweir 				xOwner->lostOwnership(static_cast < XClipboard * > (this), m_aContents);
389cdf0e10cSrcweir 
390cdf0e10cSrcweir 			OInterfaceContainerHelper* pICHelper = rBHelper.aLC.getContainer(
391cdf0e10cSrcweir 				getCppuType( ( Reference< XClipboardListener > * ) 0 ) );
392cdf0e10cSrcweir 
393cdf0e10cSrcweir 			if ( pICHelper )
394cdf0e10cSrcweir 			{
395cdf0e10cSrcweir 				try
396cdf0e10cSrcweir 				{
397cdf0e10cSrcweir 					OInterfaceIteratorHelper iter(*pICHelper);
398cdf0e10cSrcweir 					m_aContents = 0;
399cdf0e10cSrcweir 					m_aContents = new Os2Transferable( static_cast< OWeakObject* >(this) );
400cdf0e10cSrcweir 					ClipboardEvent aClipbEvent(static_cast<XClipboard*>(this), m_aContents);
401cdf0e10cSrcweir 
402cdf0e10cSrcweir 					while(iter.hasMoreElements())
403cdf0e10cSrcweir 					{
404cdf0e10cSrcweir 						try
405cdf0e10cSrcweir 						{
406cdf0e10cSrcweir 							Reference<XClipboardListener> xCBListener(iter.next(), UNO_QUERY);
407cdf0e10cSrcweir 							if (xCBListener.is())
408cdf0e10cSrcweir 								xCBListener->changedContents(aClipbEvent);
409cdf0e10cSrcweir 						}
410cdf0e10cSrcweir 						catch(RuntimeException&)
411cdf0e10cSrcweir 						{
412cdf0e10cSrcweir 							OSL_ENSURE( false, "RuntimeException caught" );
413cdf0e10cSrcweir 							debug_printf( "RuntimeException caught" );
414cdf0e10cSrcweir 						}
415cdf0e10cSrcweir 					}
416cdf0e10cSrcweir 				}
417cdf0e10cSrcweir 				catch(const ::com::sun::star::lang::DisposedException&)
418cdf0e10cSrcweir 				{
419cdf0e10cSrcweir 					OSL_ENSURE(false, "Service Manager disposed");
420cdf0e10cSrcweir 					debug_printf( "Service Manager disposed");
421cdf0e10cSrcweir 
422cdf0e10cSrcweir 					// no further clipboard changed notifications
423cdf0e10cSrcweir 					//m_pImpl->unregisterClipboardViewer();
424cdf0e10cSrcweir 				}
425cdf0e10cSrcweir 
426cdf0e10cSrcweir 			} // end if
427cdf0e10cSrcweir 		} // end if
428cdf0e10cSrcweir 	} // end if
429cdf0e10cSrcweir }
430cdf0e10cSrcweir 
431cdf0e10cSrcweir // ------------------------------------------------------------------------
432cdf0e10cSrcweir 
433cdf0e10cSrcweir Sequence< OUString > SAL_CALL Os2Clipboard_getSupportedServiceNames()
434cdf0e10cSrcweir {
435cdf0e10cSrcweir 	Sequence< OUString > aRet(1);
436cdf0e10cSrcweir 	aRet[0] = OUString::createFromAscii( OS2_CLIPBOARD_SERVICE_NAME );
437cdf0e10cSrcweir 	return aRet;
438cdf0e10cSrcweir }
439cdf0e10cSrcweir 
440cdf0e10cSrcweir // ------------------------------------------------------------------------
441cdf0e10cSrcweir 
442cdf0e10cSrcweir Reference< XInterface > SAL_CALL Os2Clipboard_createInstance(
443cdf0e10cSrcweir 	const Reference< XMultiServiceFactory > & xMultiServiceFactory)
444cdf0e10cSrcweir {
445cdf0e10cSrcweir 	return Reference < XInterface >( ( OWeakObject * ) new Os2Clipboard());
446cdf0e10cSrcweir }
447cdf0e10cSrcweir 
448