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 throw(Exception, RuntimeException)
139 {
140 if (!m_bInitialized)
141 {
142 for (sal_Int32 n = 0, nmax = aArguments.getLength(); n < nmax; n++)
143 if (aArguments[n].getValueType() == getCppuType((OUString *) 0))
144 {
145 aArguments[0] >>= m_aName;
146 break;
147 }
148 }
149 }
150
getImplementationName()151 OUString SAL_CALL Os2Clipboard::getImplementationName() throw( RuntimeException )
152 {
153 debug_printf("Os2Clipboard::getImplementationName\n");
154 return OUString::createFromAscii( OS2_CLIPBOARD_IMPL_NAME );
155 }
156
supportsService(const OUString & ServiceName)157 sal_Bool SAL_CALL Os2Clipboard::supportsService( const OUString& ServiceName ) throw( RuntimeException )
158 {
159 debug_printf("Os2Clipboard::supportsService\n");
160 Sequence < OUString > SupportedServicesNames = Os2Clipboard_getSupportedServiceNames();
161
162 for ( sal_Int32 n = SupportedServicesNames.getLength(); n--; )
163 if (SupportedServicesNames[n].compareTo(ServiceName) == 0)
164 return sal_True;
165
166 return sal_False;
167 }
168
getSupportedServiceNames()169 Sequence< OUString > SAL_CALL Os2Clipboard::getSupportedServiceNames() throw( RuntimeException )
170 {
171 debug_printf("Os2Clipboard::getSupportedServiceNames\n");
172 return Os2Clipboard_getSupportedServiceNames();
173 }
174
getContents()175 Reference< XTransferable > SAL_CALL Os2Clipboard::getContents() throw( RuntimeException )
176 {
177 debug_printf("Os2Clipboard::getContents\n");
178 MutexGuard aGuard(m_aMutex);
179
180 // os2 can have only one viewer at time, and we don't get a notification
181 // when the viewer changes. So we need to check handles of clipboard
182 // data and compare with previous handles
183 if (UWinOpenClipbrd(hAB)) {
184 sal_Bool fireChanged = sal_False;
185 ULONG handle = UWinQueryClipbrdData( hAB, UCLIP_CF_UNICODETEXT);
186 if (handle) {
187 if (handle != hText) {
188 hText = handle;
189 fireChanged = sal_True;
190 }
191 }
192 handle = UWinQueryClipbrdData( hAB, UCLIP_CF_BITMAP);
193 if (handle) {
194 if (handle != hBitmap) {
195 hBitmap = handle;
196 fireChanged = sal_True;
197 }
198 }
199 UWinCloseClipbrd( hAB);
200 if (fireChanged)
201 {
202 // notify listener for clipboard change
203 debug_printf("Os2Clipboard::getContents notify change\n");
204 notifyAllClipboardListener();
205 }
206 }
207
208 if( ! m_aContents.is() )
209 m_aContents = new Os2Transferable( static_cast< OWeakObject* >(this) );
210
211 return m_aContents;
212 }
213
setContents(const Reference<XTransferable> & xTrans,const Reference<XClipboardOwner> & xClipboardOwner)214 void SAL_CALL Os2Clipboard::setContents( const Reference< XTransferable >& xTrans, const Reference< XClipboardOwner >& xClipboardOwner ) throw( RuntimeException )
215 {
216 debug_printf("Os2Clipboard::setContents\n");
217 // remember old values for callbacks before setting the new ones.
218 ClearableMutexGuard aGuard(m_aMutex);
219
220 Reference< XClipboardOwner > oldOwner(m_aOwner);
221 m_aOwner = xClipboardOwner;
222
223 Reference< XTransferable > oldContents(m_aContents);
224 m_aContents = xTrans;
225
226 aGuard.clear();
227
228 // notify old owner on loss of ownership
229 if( oldOwner.is() )
230 oldOwner->lostOwnership(static_cast < XClipboard * > (this), oldContents);
231
232 // notify all listeners on content changes
233 OInterfaceContainerHelper *pContainer =
234 rBHelper.aLC.getContainer(getCppuType( (Reference < XClipboardListener > *) 0));
235 if (pContainer)
236 {
237 ClipboardEvent aEvent(static_cast < XClipboard * > (this), m_aContents);
238 OInterfaceIteratorHelper aIterator(*pContainer);
239
240 while (aIterator.hasMoreElements())
241 {
242 Reference < XClipboardListener > xListener(aIterator.next(), UNO_QUERY);
243 if (xListener.is())
244 xListener->changedContents(aEvent);
245 }
246 }
247
248 #if OSL_DEBUG_LEVEL>0
249 // dump list of available mimetypes
250 Sequence< DataFlavor > aFlavors( m_aContents->getTransferDataFlavors() );
251 for( int i = 0; i < aFlavors.getLength(); i++ )
252 debug_printf("Os2Clipboard::setContents available mimetype: %d %s\n",
253 i, CHAR_POINTER(aFlavors.getConstArray()[i].MimeType));
254 #endif
255
256 // we can only export text or bitmap
257 DataFlavor nFlavorText( OUString::createFromAscii( "text/plain;charset=utf-16" ),
258 OUString::createFromAscii( "Unicode-Text" ), CPPUTYPE_OUSTRING);
259 DataFlavor nFlavorBitmap( OUString::createFromAscii( "application/x-openoffice-bitmap;windows_formatname=\"Bitmap\"" ),
260 OUString::createFromAscii( "Bitmap" ), CPPUTYPE_DEFAULT);
261
262 // try text transfer data (if any)
263 PSZ pSharedText = NULL;
264 HBITMAP hbm = NULL;
265 try
266 {
267 Any aAny = m_aContents->getTransferData( nFlavorText );
268 if (aAny.hasValue())
269 {
270 APIRET rc;
271 // copy unicode text to clipboard
272 OUString aString;
273 aAny >>= aString;
274 // share text
275 rc = DosAllocSharedMem( (PPVOID) &pSharedText, NULL,
276 aString.getLength() * 2 + 2,
277 PAG_WRITE | PAG_COMMIT | OBJ_GIVEABLE | OBJ_ANY);
278 if (!rc)
279 memcpy( pSharedText, aString.getStr(), aString.getLength() * 2 + 2 );
280 else
281 pSharedText = NULL;
282 debug_printf("Os2Clipboard::setContents SetClipbrdData text done\n");
283 }
284 } catch ( UnsupportedFlavorException&) {
285 debug_printf("Os2Clipboard::setContents UnsupportedFlavorException (no text)\n");
286 }
287
288 // try bitmap transfer data (if any)
289 try
290 {
291 Any aAnyB = m_aContents->getTransferData( nFlavorBitmap );
292 if (aAnyB.hasValue())
293 {
294 hbm = OOoBmpToOS2Handle( aAnyB);
295 debug_printf("Os2Clipboard::setContents SetClipbrdData bitmap done\n");
296 }
297 } catch ( UnsupportedFlavorException&) {
298 debug_printf("Os2Clipboard::setContents UnsupportedFlavorException (no bitmap)\n");
299 }
300
301 // copy to clipboard only if we have data available, otherwise clipboard
302 // remains in use and locks all other applications.
303 if ( (pSharedText || hbm) && UWinOpenClipbrd( hAB) )
304 {
305 // set the flag, so we will ignore the next WM_DRAWCLIPBOARD
306 // since we generate it with following code.
307 m_bInSetClipboardData = sal_True;
308 UWinEmptyClipbrd( hAB);
309 // give pointer to clipboard (it will become owner of pSharedText!)
310 if (pSharedText) {
311 UWinSetClipbrdData( hAB, (ULONG) pSharedText, UCLIP_CF_UNICODETEXT, CFI_POINTER);
312 // update internal handle to avoid detection of this text as new data
313 hText = (ULONG)pSharedText;
314 }
315 // give bitmap to clipboard
316 if (hbm) {
317 UWinSetClipbrdData( hAB, (ULONG) hbm, UCLIP_CF_BITMAP, CFI_HANDLE);
318 // update internal handle to avoid detection of this bitmap as new data
319 hBitmap = hbm;
320 }
321 // reset the flag, so we will not ignore next WM_DRAWCLIPBOARD
322 m_bInSetClipboardData = sal_False;
323 UWinCloseClipbrd( hAB);
324 }
325
326 }
327
getName()328 OUString SAL_CALL Os2Clipboard::getName() throw( RuntimeException )
329 {
330 debug_printf("Os2Clipboard::getName\n");
331 return m_aName;
332 }
333
getRenderingCapabilities()334 sal_Int8 SAL_CALL Os2Clipboard::getRenderingCapabilities() throw( RuntimeException )
335 {
336 debug_printf("Os2Clipboard::getRenderingCapabilities\n");
337 return Delayed;
338 }
339
340 //========================================================================
341 // XClipboardNotifier
342 //========================================================================
343
addClipboardListener(const Reference<XClipboardListener> & listener)344 void SAL_CALL Os2Clipboard::addClipboardListener( const Reference< XClipboardListener >& listener ) throw( RuntimeException )
345 {
346 debug_printf("Os2Clipboard::addClipboardListener\n");
347 MutexGuard aGuard( rBHelper.rMutex );
348 OSL_ENSURE( !rBHelper.bInDispose, "do not add listeners in the dispose call" );
349 OSL_ENSURE( !rBHelper.bDisposed, "object is disposed" );
350 if (!rBHelper.bInDispose && !rBHelper.bDisposed)
351 rBHelper.aLC.addInterface( getCppuType( (const ::com::sun::star::uno::Reference< XClipboardListener > *) 0), listener );
352 }
353
removeClipboardListener(const Reference<XClipboardListener> & listener)354 void SAL_CALL Os2Clipboard::removeClipboardListener( const Reference< XClipboardListener >& listener ) throw( RuntimeException )
355 {
356 debug_printf("Os2Clipboard::removeClipboardListener\n");
357 MutexGuard aGuard( rBHelper.rMutex );
358 OSL_ENSURE( !rBHelper.bDisposed, "object is disposed" );
359 if (!rBHelper.bInDispose && !rBHelper.bDisposed)
360 rBHelper.aLC.removeInterface( getCppuType( (const Reference< XClipboardListener > *) 0 ), listener ); \
361 }
362
363 // ------------------------------------------------------------------------
364
notifyAllClipboardListener()365 void SAL_CALL Os2Clipboard::notifyAllClipboardListener( )
366 {
367 if ( !rBHelper.bDisposed )
368 {
369 ClearableMutexGuard aGuard( rBHelper.rMutex );
370 if ( !rBHelper.bDisposed )
371 {
372 aGuard.clear( );
373
374 ClearableMutexGuard aGuard(m_aMutex);
375 // copy member references on stack so they can be called
376 // without having the mutex
377 Reference< XClipboardOwner > xOwner( m_aOwner );
378 Reference< XTransferable > xTrans( m_aContents );
379 // clear members
380 m_aOwner.clear();
381 m_aContents.clear();
382 // release the mutex
383 aGuard.clear();
384
385 // inform previous owner of lost ownership
386 if ( xOwner.is() )
387 xOwner->lostOwnership(static_cast < XClipboard * > (this), m_aContents);
388
389 OInterfaceContainerHelper* pICHelper = rBHelper.aLC.getContainer(
390 getCppuType( ( Reference< XClipboardListener > * ) 0 ) );
391
392 if ( pICHelper )
393 {
394 try
395 {
396 OInterfaceIteratorHelper iter(*pICHelper);
397 m_aContents = 0;
398 m_aContents = new Os2Transferable( static_cast< OWeakObject* >(this) );
399 ClipboardEvent aClipbEvent(static_cast<XClipboard*>(this), m_aContents);
400
401 while(iter.hasMoreElements())
402 {
403 try
404 {
405 Reference<XClipboardListener> xCBListener(iter.next(), UNO_QUERY);
406 if (xCBListener.is())
407 xCBListener->changedContents(aClipbEvent);
408 }
409 catch(RuntimeException&)
410 {
411 OSL_ENSURE( false, "RuntimeException caught" );
412 debug_printf( "RuntimeException caught" );
413 }
414 }
415 }
416 catch(const ::com::sun::star::lang::DisposedException&)
417 {
418 OSL_ENSURE(false, "Service Manager disposed");
419 debug_printf( "Service Manager disposed");
420
421 // no further clipboard changed notifications
422 //m_pImpl->unregisterClipboardViewer();
423 }
424
425 } // end if
426 } // end if
427 } // end if
428 }
429
430 // ------------------------------------------------------------------------
431
Os2Clipboard_getSupportedServiceNames()432 Sequence< OUString > SAL_CALL Os2Clipboard_getSupportedServiceNames()
433 {
434 Sequence< OUString > aRet(1);
435 aRet[0] = OUString::createFromAscii( OS2_CLIPBOARD_SERVICE_NAME );
436 return aRet;
437 }
438
439 // ------------------------------------------------------------------------
440
Os2Clipboard_createInstance(const Reference<XMultiServiceFactory> & xMultiServiceFactory)441 Reference< XInterface > SAL_CALL Os2Clipboard_createInstance(
442 const Reference< XMultiServiceFactory > & xMultiServiceFactory)
443 {
444 return Reference < XInterface >( ( OWeakObject * ) new Os2Clipboard());
445 }
446
447