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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dtrans.hxx"
26 
27 
28 //_________________________________________________________________________________________________________________________
29 //	interface includes
30 //_________________________________________________________________________________________________________________________
31 
32 
33 #include "..\misc\ImplHelper.hxx"
34 
35 //_________________________________________________________________________________________________________________________
36 //	other includes
37 //_________________________________________________________________________________________________________________________
38 #include <cppuhelper/servicefactory.hxx>
39 #include <com/sun/star/datatransfer/XTransferable.hpp>
40 #include <com/sun/star/datatransfer/clipboard/XClipboardOwner.hpp>
41 #include <com/sun/star/datatransfer/clipboard/XClipboardNotifier.hpp>
42 #include <com/sun/star/datatransfer/clipboard/XClipboardEx.hpp>
43 #include <com/sun/star/datatransfer/clipboard/XFlushableClipboard.hpp>
44 #include <com/sun/star/lang/XComponent.hpp>
45 #include <cppuhelper/implbase1.hxx>
46 #include <cppuhelper/implbase2.hxx>
47 #include <rtl/ustring.hxx>
48 #include <sal/types.h>
49 #include <osl/diagnose.h>
50 
51 #include <stdio.h>
52 #if defined _MSC_VER
53 #pragma warning(push,1)
54 #endif
55 #include <windows.h>
56 #include <objbase.h>
57 #if defined _MSC_VER
58 #pragma warning(pop)
59 #endif
60 
61 #include <memory>
62 
63 #include <process.h>
64 
65 //-------------------------------------------------------------
66 // my defines
67 //-------------------------------------------------------------
68 
69 #define TEST_CLIPBOARD
70 #define RDB_SYSPATH  "d:\\projects\\src623\\dtrans\\wntmsci7\\bin\\applicat.rdb"
71 #define WINCLIPBOARD_SERVICE_NAME L"com.sun.star.datatransfer.clipboard.SystemClipboard"
72 #define  WRITE_CB
73 #define EVT_MANUAL_RESET     TRUE
74 #define EVT_INIT_NONSIGNALED FALSE
75 #define EVT_NONAME           ""
76 
77 //------------------------------------------------------------
78 //	namesapces
79 //------------------------------------------------------------
80 
81 using namespace	::rtl;
82 using namespace ::std;
83 using namespace ::cppu;
84 using namespace ::com::sun::star::datatransfer;
85 using namespace ::com::sun::star::datatransfer::clipboard;
86 using namespace ::com::sun::star::uno;
87 using namespace ::com::sun::star::io;
88 using namespace	::com::sun::star::lang;
89 
90 //------------------------------------------------------------
91 //	globales
92 //------------------------------------------------------------
93 
94 Reference< XTransferable > rXTransfRead;
95 HANDLE	g_hEvtThreadWakeup;
96 
97 //------------------------------------------------------------
98 //
99 //------------------------------------------------------------
100 
101 class CClipboardListener : public WeakImplHelper1 < XClipboardListener >
102 {
103 public:
104 	~CClipboardListener( );
105 
106 	//-------------------------------------------------
107 	// XClipboardListener
108 	//-------------------------------------------------
109 
110 	virtual void SAL_CALL disposing( const EventObject& Source ) throw(RuntimeException);
111 	virtual void SAL_CALL changedContents( const ClipboardEvent& event ) throw( RuntimeException );
112 };
113 
~CClipboardListener()114 CClipboardListener::~CClipboardListener( )
115 {
116 }
117 
disposing(const EventObject & Source)118 void SAL_CALL CClipboardListener::disposing( const EventObject& Source ) throw(RuntimeException)
119 {
120 
121 }
122 
changedContents(const ClipboardEvent & event)123 void SAL_CALL CClipboardListener::changedContents( const ClipboardEvent& event ) throw( RuntimeException )
124 {
125 	//MessageBox( NULL, TEXT("Clipboard content changed"), TEXT("Info"), MB_OK | MB_ICONINFORMATION );
126 }
127 
128 //------------------------------------------------------------
129 //
130 //------------------------------------------------------------
131 
132 class CTransferable : public WeakImplHelper2< XClipboardOwner, XTransferable >
133 {
134 public:
135 	CTransferable( );
136 
137 	//-------------------------------------------------
138 	// XTransferable
139 	//-------------------------------------------------
140 
141 	virtual Any SAL_CALL getTransferData( const DataFlavor& aFlavor )
142 		throw(UnsupportedFlavorException, IOException, RuntimeException);
143 
144     virtual Sequence< DataFlavor > SAL_CALL getTransferDataFlavors(  ) throw(RuntimeException);
145 
146 	virtual sal_Bool SAL_CALL isDataFlavorSupported( const DataFlavor& aFlavor ) throw(RuntimeException);
147 
148 	//-------------------------------------------------
149 	// XClipboardOwner
150 	//-------------------------------------------------
151 
152 	virtual void SAL_CALL lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans )
153 		throw(RuntimeException);
154 
155 private:
156 	Sequence< DataFlavor > m_FlavorList;
157 	OUString               m_Data;
158 };
159 
160 //----------------------------------------------------------------
161 //	ctor
162 //----------------------------------------------------------------
163 
CTransferable()164 CTransferable::CTransferable( ) :
165 	m_FlavorList( 1 ),
166 	m_Data( OUString::createFromAscii( "Ich habe mir ein neues Fahrrad gekauft!" ) )
167 {
168 	DataFlavor df;
169 
170 	//df.MimeType = L"text/plain;charset=utf-16";
171 	//df.DataType = getCppuType( ( OUString* )0 );
172 
173 	df.MimeType = L"text/plain;charset=Windows1252";
174 	df.DataType = getCppuType( (Sequence< sal_Int8 >*)0 );
175 
176 	m_FlavorList[0] = df;
177 }
178 
179 //----------------------------------------------------------------
180 //	getTransferData
181 //----------------------------------------------------------------
182 
getTransferData(const DataFlavor & aFlavor)183 Any SAL_CALL CTransferable::getTransferData( const DataFlavor& aFlavor )
184 	throw(UnsupportedFlavorException, IOException, RuntimeException)
185 {
186 	Any anyData;
187 
188 	/*
189 	if ( aFlavor.MimeType == m_FlavorList[0].MimeType )
190 		anyData = makeAny( m_Data );
191 	*/
192 	if ( aFlavor.MimeType.equalsIgnoreCase( m_FlavorList[0].MimeType ) )
193 	{
194 		OString text(
195 			m_Data.getStr( ),
196 			m_Data.getLength( ),
197 			RTL_TEXTENCODING_ASCII_US );
198 
199 		Sequence< sal_Int8 > textStream( text.getLength( ) + 1 );
200 
201 		rtl_copyMemory( textStream.getArray( ), text.getStr( ), textStream.getLength( ) );
202 
203 		anyData = makeAny( textStream );
204 	}
205 	else
206 		throw UnsupportedFlavorException( );
207 
208 	return anyData;
209 }
210 
211 //----------------------------------------------------------------
212 //	getTransferDataFlavors
213 //----------------------------------------------------------------
214 
getTransferDataFlavors()215 Sequence< DataFlavor > SAL_CALL CTransferable::getTransferDataFlavors(  )
216 	throw(RuntimeException)
217 {
218 	return m_FlavorList;
219 }
220 
221 //----------------------------------------------------------------
222 //	isDataFlavorSupported
223 //----------------------------------------------------------------
224 
isDataFlavorSupported(const DataFlavor & aFlavor)225 sal_Bool SAL_CALL CTransferable::isDataFlavorSupported( const DataFlavor& aFlavor )
226 	throw(RuntimeException)
227 {
228 	sal_Int32 nLength = m_FlavorList.getLength( );
229 
230 	for ( sal_Int32 i = 0; i < nLength; ++i )
231 		if ( m_FlavorList[i].MimeType == aFlavor.MimeType )
232 			return sal_True;
233 
234 	return sal_False;
235 }
236 
237 //----------------------------------------------------------------
238 //	lostOwnership
239 //----------------------------------------------------------------
240 
lostOwnership(const Reference<XClipboard> & xClipboard,const Reference<XTransferable> & xTrans)241 void SAL_CALL CTransferable::lostOwnership(
242 	const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans )
243 	throw(RuntimeException)
244 {
245 	//MessageBox( NULL, TEXT("No longer clipboard owner"), TEXT("Info"), MB_OK | MB_ICONINFORMATION );
246 }
247 
248 //----------------------------------------------------------------
249 //	main
250 //----------------------------------------------------------------
251 
main(int nArgc,char * Argv[])252 int SAL_CALL main( int nArgc, char* Argv[] )
253 {
254 	// create a multi-threaded apartment; we can test only
255 	// with a multithreaded apartment because for a single
256 	// threaded apartment we need a message loop to deliver
257 	// messages to our XTDataObject
258 	//HRESULT hr = CoInitializeEx( NULL, COINIT_MULTITHREADED );
259 	HRESULT hr = CoInitialize( NULL );
260 
261 	char buff[6];
262 
263 	LCID lcid = MAKELCID( MAKELANGID( LANG_GERMAN, SUBLANG_GERMAN ), SORT_DEFAULT );
264 
265 	BOOL bValid = IsValidLocale( lcid, LCID_SUPPORTED );
266 	GetLocaleInfoA( lcid, LOCALE_IDEFAULTANSICODEPAGE, buff, sizeof( buff ) );
267 
268 	//-------------------------------------------------
269 	// get the global service-manager
270 	//-------------------------------------------------
271 
272 	OUString rdbName = OUString( RTL_CONSTASCII_USTRINGPARAM( RDB_SYSPATH ) );
273 	Reference< XMultiServiceFactory > g_xFactory( createRegistryServiceFactory( rdbName ) );
274 
275 	// Print a message if an error occurred.
276 	if ( !g_xFactory.is( ) )
277 	{
278 		OSL_ENSURE(sal_False, "Can't create RegistryServiceFactory");
279 		return(-1);
280 	}
281 
282 	//-------------------------------------------------
283 	// try to get an Interface to a XFilePicker Service
284 	//-------------------------------------------------
285 
286 	Reference< XTransferable > rXTransf( static_cast< XTransferable* >( new CTransferable ) );
287 
288 	Reference< XClipboard >
289 		xClipboard( g_xFactory->createInstance( OUString( WINCLIPBOARD_SERVICE_NAME ) ), UNO_QUERY );
290 	if ( !xClipboard.is( ) )
291 	{
292 		OSL_ENSURE( sal_False, "Error creating Clipboard Service" );
293 		return(-1);
294 	}
295 
296 	Reference< XClipboardNotifier > xClipNotifier( xClipboard, UNO_QUERY );
297 	Reference< XClipboardListener > rXClipListener( static_cast< XClipboardListener* >( new CClipboardListener() ) );
298 	xClipNotifier->addClipboardListener( rXClipListener );
299 
300 	MessageBox( NULL, TEXT("Go"), TEXT("INFO"), MB_OK|MB_ICONINFORMATION);
301 
302 	// set new clipboard content
303 	xClipboard->setContents( rXTransf, Reference< XClipboardOwner >( rXTransf, UNO_QUERY )  );
304 
305 	/*
306 	MessageBox( NULL, TEXT("Clear content"), TEXT("INFO"), MB_OK|MB_ICONINFORMATION);
307 
308 	Reference< XClipboardOwner > rXClipOwner;
309 	Reference< XTransferable >   rXEmptyTransf;
310 	xClipboard->setContents( rXEmptyTransf, rXClipOwner );
311 	*/
312 
313 	MessageBox( NULL, TEXT("Stop"), TEXT("INFO"), MB_OK|MB_ICONINFORMATION);
314 
315 	// flush the clipboard content
316 	Reference< XFlushableClipboard > rXFlushableClip( xClipboard, UNO_QUERY );
317 	rXFlushableClip->flushClipboard( );
318 	rXFlushableClip = Reference< XFlushableClipboard >( );
319 
320 	xClipNotifier->removeClipboardListener( rXClipListener );
321 	rXClipListener = Reference< XClipboardListener >( );
322 	xClipNotifier  = Reference< XClipboardNotifier >( );
323 
324 	//--------------------------------------------------
325 	// shutdown the service manager
326 	//--------------------------------------------------
327 
328 	// Cast factory to XComponent
329 	Reference< XComponent > xComponent( g_xFactory, UNO_QUERY );
330 
331 	if ( !xComponent.is() )
332 		OSL_ENSURE(sal_False, "Error shutting down");
333 
334 	// Dispose and clear factory
335 	xComponent->dispose();
336 	xComponent = Reference< XComponent >( );
337 
338 	g_xFactory.clear();
339 	g_xFactory = Reference< XMultiServiceFactory >();
340 
341 	CoUninitialize( );
342 
343 	return 0;
344 }
345