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