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