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