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