1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <com/sun/star/datatransfer/XTransferable.hpp>
#include <com/sun/star/datatransfer/clipboard/XClipboardManager.hpp>
#include <com/sun/star/datatransfer/clipboard/XClipboardOwner.hpp>
#include <com/sun/star/datatransfer/clipboard/XClipboardNotifier.hpp>
#include <com/sun/star/datatransfer/clipboard/XClipboardEx.hpp>
#include <com/sun/star/lang/XComponent.hpp>
#include <cppuhelper/servicefactory.hxx>
#include <cppuhelper/implbase.hxx>
#include <rtl/ustring.hxx>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// my defines
#ifdef UNX
#define PATH_SEPARATOR '/'
#else
#define PATH_SEPARATOR '\\'
#endif
#define ENSURE( a, b ) if( !a ) { fprintf( stderr, b "\n" ); exit( -1 ); }
#define TEST( a, b ) fprintf( stderr, "Testing " a ); fprintf( stderr, b ? "passed\n" : "FAILED\n" )
#define PERFORM( a, b ) fprintf( stderr, "Performing " a); b; fprintf( stderr, "done\n" )
#define TRACE( a ) fprintf( stderr, a )
// namespaces
using namespace ::std;
using namespace ::cppu;
using namespace ::com::sun::star::container;
using namespace ::com::sun::star::datatransfer;
using namespace ::com::sun::star::datatransfer::clipboard;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::io;
using namespace ::com::sun::star::lang;
// globals
const char * app = NULL;
// ClipboardOwner
class ClipboardOwner : public WeakImplHelper< XClipboardOwner >
{
Reference< XClipboard > m_xClipboard;
Reference< XTransferable > m_xTransferable;
sal_uInt32 m_nReceivedLostOwnerships;
public:
ClipboardOwner();
// XClipboardOwner
virtual void SAL_CALL lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans ) throw(RuntimeException);
sal_uInt32 receivedLostOwnerships() { return m_nReceivedLostOwnerships; };
Reference< XClipboard > lostOwnershipClipboardValue() { return m_xClipboard; }
Reference< XTransferable > lostOwnershipTransferableValue() { return m_xTransferable; };
};
// ctor
ClipboardOwner::ClipboardOwner():
m_nReceivedLostOwnerships( 0 )
{
}
// lostOwnership
void SAL_CALL ClipboardOwner::lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans )
throw(RuntimeException)
{
m_nReceivedLostOwnerships++;
m_xClipboard = xClipboard;
m_xTransferable = xTrans;
}
// ClipboardListener
class ClipboardListener : public WeakImplHelper< XClipboardListener >
{
Reference< XClipboard > m_xClipboard;
Reference< XTransferable > m_xTransferable;
sal_uInt32 m_nReceivedChangedContentsEvents;
public:
ClipboardListener();
// XClipboardOwner
virtual void SAL_CALL changedContents( const ClipboardEvent& event ) throw(RuntimeException);
// XEventListener
virtual void SAL_CALL disposing( const EventObject& event ) throw(RuntimeException);
sal_uInt32 receivedChangedContentsEvents() { return m_nReceivedChangedContentsEvents; };
Reference< XClipboard > changedContentsEventClipboardValue() { return m_xClipboard; }
Reference< XTransferable > changedContentsEventTransferableValue() { return m_xTransferable; };
};
// ctor
ClipboardListener::ClipboardListener():
m_nReceivedChangedContentsEvents( 0 )
{
}
// changedContents
void SAL_CALL ClipboardListener::changedContents( const ClipboardEvent& event )
throw(RuntimeException)
{
m_nReceivedChangedContentsEvents++;
m_xClipboard.set(event.Source, UNO_QUERY);
m_xTransferable = event.Contents;
}
// disposing
void SAL_CALL ClipboardListener::disposing( const EventObject& event )
throw(RuntimeException)
{
}
// StringTransferable
class StringTransferable : public WeakImplHelper< XClipboardOwner, XTransferable >
{
public:
StringTransferable( );
// XTransferable
virtual Any SAL_CALL getTransferData( const DataFlavor& aFlavor ) throw(UnsupportedFlavorException, IOException, RuntimeException);
virtual Sequence< DataFlavor > SAL_CALL getTransferDataFlavors( ) throw(RuntimeException);
virtual sal_Bool SAL_CALL isDataFlavorSupported( const DataFlavor& aFlavor ) throw(RuntimeException);
// XClipboardOwner
virtual void SAL_CALL lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans ) throw(RuntimeException);
sal_Bool receivedLostOwnership() { return m_receivedLostOwnership; };<--- The function 'receivedLostOwnership' is never used.
void clearReceivedLostOwnership() { m_receivedLostOwnership = sal_False; };<--- The function 'clearReceivedLostOwnership' is never used.
private:
Sequence< DataFlavor > m_seqDFlv;
OUString m_Data;
sal_Bool m_receivedLostOwnership;
};
// ctor
StringTransferable::StringTransferable( ) :
m_seqDFlv( 1 ),
m_receivedLostOwnership( sal_False ),
m_Data( OUString("clipboard test content") )
{
DataFlavor df;
/*
df.MimeType = L"text/plain; charset=unicode";
df.DataType = cppu::UnoType<OUString>::get();
m_seqDFlv[0] = df;
*/
//df.MimeType = L"text/plain; charset=windows1252";
df.MimeType = "text/html";
df.DataType = cppu::UnoType<Sequence< sal_Int8 >>::get();
m_seqDFlv[0] = df;
}
// getTransferData
Any SAL_CALL StringTransferable::getTransferData( const DataFlavor& aFlavor )
throw(UnsupportedFlavorException, IOException, RuntimeException)
{
Any anyData;
/*if ( aFlavor == m_seqDFlv[0] )
{
anyData = makeAny( m_Data );
} */
return anyData;
}
// getTransferDataFlavors
Sequence< DataFlavor > SAL_CALL StringTransferable::getTransferDataFlavors( )
throw(RuntimeException)
{
return m_seqDFlv;
}
// isDataFlavorSupported
sal_Bool SAL_CALL StringTransferable::isDataFlavorSupported( const DataFlavor& aFlavor )
throw(RuntimeException)
{
sal_Int32 nLength = m_seqDFlv.getLength( );<--- Variable 'nLength' is assigned a value that is never used.
sal_Bool bRet = sal_False;
// for ( sal_Int32 i = 0; i < nLength; ++i )
// {
// if ( m_seqDFlv[i] == aFlavor )
// {
// bRet = sal_True;
// break;
// }
// }
return bRet;
}
// lostOwnership
void SAL_CALL StringTransferable::lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans )
throw(RuntimeException)
{
m_receivedLostOwnership = sal_True;
}
// main
int SAL_CALL main( int argc, const char* argv[] )
{
OUString aRegistry;
// check command line parameters
if ( NULL == ( app = strrchr( argv[0], PATH_SEPARATOR ) ) )
app = argv[0];
else
app++;
for( int n = 1; n < argc; n++ )
{
if( strncmp( argv[n], "-r", 2 ) == 0 )
{
if( strlen( argv[n] ) > 2 )
aRegistry = OUString::createFromAscii( argv[n] + 2 );
else if ( n + 1 < argc )
aRegistry = OUString::createFromAscii( argv[++n] );
}
}
if( aRegistry.isEmpty( ) )
fprintf( stderr, "Usage: %s -r full-path-to-applicat.rdb\n", app );
// create service manager
Reference< XMultiServiceFactory > xServiceManager;
try
{
xServiceManager = createRegistryServiceFactory( aRegistry, sal_True );
ENSURE( xServiceManager.is(), "*** ERROR *** service manager could not be created." );
// create an instance of GenericClipboard service
Sequence< Any > arguments(1);
arguments[0] = makeAny( OUString("generic") );
Reference< XClipboard > xClipboard( xServiceManager->createInstanceWithArguments(
"com.sun.star.datatransfer.clipboard.GenericClipboard",
arguments ), UNO_QUERY );
ENSURE( xClipboard.is(), "*** ERROR *** generic clipboard service could not be created." );
Reference< XClipboardNotifier > xClipboardNotifier( xClipboard, UNO_QUERY );
Reference< XClipboardListener > xClipboardListener = new ClipboardListener();
ClipboardListener * pListener = (ClipboardListener *) xClipboardListener.get();<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if( xClipboardNotifier.is() )
xClipboardNotifier->addClipboardListener( xClipboardListener );
// run various tests on clipboard implementation
TRACE( "\n*** testing generic clipboard service ***\n" );
Reference< XTransferable > xContents = new StringTransferable();
Reference< XClipboardOwner > xOwner = new ClipboardOwner();
ClipboardOwner *pOwner = (ClipboardOwner *) xOwner.get();<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
TEST( "initial contents (none): ", xClipboard->getContents().is() == sal_False );
PERFORM( "update on contents with clipboard owner: ", xClipboard->setContents( xContents, xOwner ) );
TEST( "current clipboard contents: ", xContents == xClipboard->getContents() );
if( xClipboardNotifier.is() )
{
TEST( "if received changedContents notifications: ", pListener->receivedChangedContentsEvents() > 0 );
TEST( "if received exactly 1 changedContents notification: ", pListener->receivedChangedContentsEvents() == 1 );
TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventClipboardValue() == xClipboard );
TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventTransferableValue() == xContents );
}
PERFORM( "update on contents without data (clear): ", xClipboard->setContents( Reference< XTransferable >(), Reference< XClipboardOwner >() ) );
TEST( "if received lostOwnership message(s): ", pOwner->receivedLostOwnerships() > 0 );
TEST( "if received exactly 1 lostOwnership message: ", pOwner->receivedLostOwnerships() == 1 );
TEST( "if received lostOwnership message for the correct clipboard: ", pOwner->lostOwnershipClipboardValue() == xClipboard );
TEST( "if received lostOwnership message for the correct transferable: ", pOwner->lostOwnershipTransferableValue() == xContents );
TEST( "current clipboard contents (none): ", xClipboard->getContents().is() == sal_False );
if( xClipboardNotifier.is() )
{
TEST( "if received changedContents notifications: ", pListener->receivedChangedContentsEvents() > 1 );
TEST( "if received exactly 1 changedContents notification: ", pListener->receivedChangedContentsEvents() == 2 );
TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventClipboardValue() == xClipboard );
TEST( "if received changedContents notification for correct transferable: ", ! pListener->changedContentsEventTransferableValue().is() );
}
PERFORM( "update on contents without clipboard owner: ", xClipboard->setContents( xContents, Reference< XClipboardOwner >() ) );
TEST( "that no further lostOwnership messages were received: ", pOwner->receivedLostOwnerships() == 1 );
TEST( "current clipboard contents: ", xContents == xClipboard->getContents() );
if( xClipboardNotifier.is() )
{
TEST( "if received changedContents notifications: ", pListener->receivedChangedContentsEvents() > 2 );
TEST( "if received exactly 1 changedContents notification: ", pListener->receivedChangedContentsEvents() == 3 );
TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventClipboardValue() == xClipboard );
TEST( "if received changedContents notification for correct transferable: ", pListener->changedContentsEventTransferableValue() == xContents );
}
PERFORM( "update on contents without data (clear): ", xClipboard->setContents( Reference< XTransferable >(), Reference< XClipboardOwner >() ) );
TEST( "that no further lostOwnership messages were received: ", pOwner->receivedLostOwnerships() == 1 );
TEST( "current clipboard contents (none): ", xClipboard->getContents().is() == sal_False );
if( xClipboardNotifier.is() )
{
TEST( "if received changedContents notifications: ", pListener->receivedChangedContentsEvents() > 3 );
TEST( "if received exactly 1 changedContents notification: ", pListener->receivedChangedContentsEvents() == 4 );
TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventClipboardValue() == xClipboard );
TEST( "if received changedContents notification for correct transferable: ", ! pListener->changedContentsEventTransferableValue().is() );
}
// create an instance of ClipboardManager service
Reference< XClipboardManager > xClipboardManager( xServiceManager->createInstance(
"com.sun.star.datatransfer.clipboard.ClipboardManager" ), UNO_QUERY );
ENSURE( xClipboardManager.is(), "*** ERROR *** clipboard manager service could not be created." );
// run various tests on clipboard manager implementation
TRACE( "\n*** testing clipboard manager service ***\n" );
TEST( "initial number of clipboards (0): ", xClipboardManager->listClipboardNames().getLength() == 0 );
PERFORM( "insertion of generic clipboard: ", xClipboardManager->addClipboard( xClipboard ) );
TEST( "number of inserted clipboards (1): ", xClipboardManager->listClipboardNames().getLength() == 1 );
TEST( "name of inserted clipboard (generic): ", xClipboardManager->listClipboardNames()[0] == "generic" );
TEST( "inserted clipboard instance: ", xClipboardManager->getClipboard( OUString("generic") ) == xClipboard );
PERFORM( "removal of generic clipboard: ", xClipboardManager->removeClipboard( OUString("generic") ) );
TEST( "number of inserted clipboards (0): ", xClipboardManager->listClipboardNames().getLength() == 0 );
TRACE( "Testing inserted clipboard instance (none): " );
try
{
xClipboardManager->getClipboard( OUString("generic") );
TRACE( "FAILED\n" );
}
catch (const NoSuchElementException&)
{
TRACE( "passed\n" );
}
}
catch (const Exception&)
{
ENSURE( sal_False, "*** ERROR *** exception caught." );
}
// shutdown the service manager
// query XComponent interface
Reference< XComponent > xComponent( xServiceManager, UNO_QUERY );
ENSURE( xComponent.is(), "*** ERROR *** service manager does not support XComponent." );
// Dispose and clear factory
xComponent->dispose();
xServiceManager.clear();
fprintf( stderr, "Done.\n" );
return 0;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|