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 // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_extensions.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <atlbase.h>
32*cdf0e10cSrcweir #include <stdio.h>
33*cdf0e10cSrcweir #include "cppuhelper/bootstrap.hxx"
34*cdf0e10cSrcweir #include "rtl/process.h"
35*cdf0e10cSrcweir #include "typelib/typedescription.hxx"
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include "com/sun/star/bridge/ModelDependent.hpp"
38*cdf0e10cSrcweir #include "com/sun/star/bridge/XBridgeSupplier2.hpp"
39*cdf0e10cSrcweir #include "com/sun/star/uno/TypeClass.hpp"
40*cdf0e10cSrcweir #include "com/sun/star/script/XInvocation.hpp"
41*cdf0e10cSrcweir #include "com/sun/star/lang/XMultiServiceFactory.hpp"
42*cdf0e10cSrcweir #include "com/sun/star/uno/XComponentContext.hpp"
43*cdf0e10cSrcweir #include <com/sun/star/bridge/oleautomation/NamedArgument.hpp>
44*cdf0e10cSrcweir #include "rtl/ustring.hxx"
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir using namespace com::sun::star::bridge;
47*cdf0e10cSrcweir using namespace com::sun::star::bridge::ModelDependent;
48*cdf0e10cSrcweir using namespace com::sun::star::uno;
49*cdf0e10cSrcweir using namespace com::sun::star::lang;
50*cdf0e10cSrcweir using namespace com::sun::star::script;
51*cdf0e10cSrcweir using namespace com::sun::star::bridge::oleautomation;
52*cdf0e10cSrcweir using namespace rtl;
53*cdf0e10cSrcweir using namespace cppu;
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir template< class T >
56*cdf0e10cSrcweir bool equalSequences(const Sequence<T>& seqIn, const Sequence<Any> & returned);
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir Reference< XMultiServiceFactory > objectFactory;//
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir Reference<XMultiServiceFactory> getMultiServiceFactory()
63*cdf0e10cSrcweir {
64*cdf0e10cSrcweir 	static Reference< XMultiServiceFactory > factory;
65*cdf0e10cSrcweir 	if( ! objectFactory.is() )
66*cdf0e10cSrcweir 	{
67*cdf0e10cSrcweir 		Reference<XComponentContext> context = defaultBootstrap_InitialComponentContext();
68*cdf0e10cSrcweir 		factory = Reference<XMultiServiceFactory>( context->getServiceManager(), UNO_QUERY);
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir 	}
71*cdf0e10cSrcweir 	return factory;
72*cdf0e10cSrcweir }
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir Reference<XInvocation> getComObject( OUString progId)
75*cdf0e10cSrcweir {
76*cdf0e10cSrcweir 	HRESULT hr= S_OK;
77*cdf0e10cSrcweir 	Reference< XInvocation > ret;
78*cdf0e10cSrcweir //	Reference<XMultiServiceFactory> fac;
79*cdf0e10cSrcweir 	if(  ! objectFactory.is())
80*cdf0e10cSrcweir 	{	Reference<XMultiServiceFactory> mgr= getMultiServiceFactory();
81*cdf0e10cSrcweir 		Reference<XInterface> xInt= mgr->createInstance(
82*cdf0e10cSrcweir 			OUString(L"com.sun.star.bridge.oleautomation.Factory"));
83*cdf0e10cSrcweir 		objectFactory= Reference<XMultiServiceFactory>::query(  xInt);
84*cdf0e10cSrcweir 	}
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir 	if( objectFactory.is())
87*cdf0e10cSrcweir 	{
88*cdf0e10cSrcweir 		Reference<XInterface> xIntAx= objectFactory->createInstance( progId.getStr());
89*cdf0e10cSrcweir 		if( xIntAx.is() )
90*cdf0e10cSrcweir 		{
91*cdf0e10cSrcweir 			Reference< XInvocation > xInv( xIntAx, UNO_QUERY);
92*cdf0e10cSrcweir 			ret= xInv;
93*cdf0e10cSrcweir 		}
94*cdf0e10cSrcweir 	}
95*cdf0e10cSrcweir 	return ret;
96*cdf0e10cSrcweir }
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir Reference<XInvocation> convertComObject( IUnknown* pUnk)
99*cdf0e10cSrcweir {
100*cdf0e10cSrcweir 	Reference< XMultiServiceFactory > mgr= getMultiServiceFactory();
101*cdf0e10cSrcweir 	Reference< XInterface > xIntSupplier= mgr->createInstance(OUString(L"com.sun.star.bridge.OleBridgeSupplier2"));
102*cdf0e10cSrcweir 	Reference< XBridgeSupplier2 > xSuppl( xIntSupplier, UNO_QUERY);
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 	Any any;
105*cdf0e10cSrcweir 	CComVariant var( pUnk);
106*cdf0e10cSrcweir 	any <<= ( sal_uInt32)&var;
107*cdf0e10cSrcweir 	sal_uInt8 arId[16];
108*cdf0e10cSrcweir 	rtl_getGlobalProcessId( arId);
109*cdf0e10cSrcweir 	Any target=	xSuppl->createBridge( any, Sequence<sal_Int8>( (sal_Int8*)arId, 16), OLE, UNO );
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir 	Reference<XInvocation> ret;
112*cdf0e10cSrcweir 	target>>= ret;
113*cdf0e10cSrcweir 	return ret;
114*cdf0e10cSrcweir }
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir /*
117*cdf0e10cSrcweir   Parameter values contains the expected return values. The value at index 0
118*cdf0e10cSrcweir   correspond to parameter 0 (left - most). For parameters which are not out or
119*cdf0e10cSrcweir   in/out the value must be a void any.
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir   The number of items in value must be the
122*cdf0e10cSrcweir   same as the number of provided parameter during the  call on the  method.
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir   The parameter outArgs, indices correspond to the sequences which are
125*cdf0e10cSrcweir   arguments to XInvocation::invoke
126*cdf0e10cSrcweir  */
127*cdf0e10cSrcweir bool checkOutArgs(const Sequence<Any> & outArgs,
128*cdf0e10cSrcweir                   const Sequence<sal_Int16> & indices, const Sequence<Any> & values)
129*cdf0e10cSrcweir {
130*cdf0e10cSrcweir     if (values.getLength() != outArgs.getLength())
131*cdf0e10cSrcweir         return false;
132*cdf0e10cSrcweir     //iterate over all parameters. i represents the parameter index
133*cdf0e10cSrcweir     for (int i = 0; i < values.getLength(); i++)
134*cdf0e10cSrcweir     {
135*cdf0e10cSrcweir         if (values[i].getValueType() == getVoidCppuType())
136*cdf0e10cSrcweir             continue;
137*cdf0e10cSrcweir         //out parameter
138*cdf0e10cSrcweir         //Based on the parameter index find the correspondent out value
139*cdf0e10cSrcweir         int indexOutSeq = -1;
140*cdf0e10cSrcweir         for (int iIndices = indices.getLength() - 1; iIndices >= 0; iIndices --)
141*cdf0e10cSrcweir         {
142*cdf0e10cSrcweir             if (indices[iIndices] == i)
143*cdf0e10cSrcweir             {
144*cdf0e10cSrcweir                 indexOutSeq = iIndices;
145*cdf0e10cSrcweir                 break;
146*cdf0e10cSrcweir             }
147*cdf0e10cSrcweir         }
148*cdf0e10cSrcweir         if (indexOutSeq == -1)
149*cdf0e10cSrcweir             return false;
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir 		Any value;
152*cdf0e10cSrcweir 		Any out;
153*cdf0e10cSrcweir 		values[i] >>= value;
154*cdf0e10cSrcweir 		outArgs[indexOutSeq] >>=out;
155*cdf0e10cSrcweir 		NamedArgument naVal;
156*cdf0e10cSrcweir 		NamedArgument naOut;
157*cdf0e10cSrcweir 		value >>= naVal;
158*cdf0e10cSrcweir 		out >>= naOut;
159*cdf0e10cSrcweir         if (values[i].getValueType() == getCppuType((NamedArgument *) 0))
160*cdf0e10cSrcweir         {
161*cdf0e10cSrcweir             NamedArgument inNamed;
162*cdf0e10cSrcweir             values[i] >>= inNamed;
163*cdf0e10cSrcweir             value <<= inNamed.Value;
164*cdf0e10cSrcweir         }
165*cdf0e10cSrcweir 		if (value != outArgs[indexOutSeq])
166*cdf0e10cSrcweir             return false;
167*cdf0e10cSrcweir     }
168*cdf0e10cSrcweir     return true;
169*cdf0e10cSrcweir }
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir /* The returned sequence always contains Any elements
172*cdf0e10cSrcweir */
173*cdf0e10cSrcweir bool equalSequences(const Any& orig, const Any& returned)
174*cdf0e10cSrcweir {
175*cdf0e10cSrcweir 	if (orig.getValueTypeClass() != TypeClass_SEQUENCE)
176*cdf0e10cSrcweir 	{
177*cdf0e10cSrcweir 		OSL_ASSERT(0);
178*cdf0e10cSrcweir 		return false;
179*cdf0e10cSrcweir 	}
180*cdf0e10cSrcweir 	TypeDescription td(orig.getValueTypeRef());
181*cdf0e10cSrcweir 	typelib_IndirectTypeDescription * indirect_td = (typelib_IndirectTypeDescription *) td.get();
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir 	switch (indirect_td->pType->eTypeClass)
184*cdf0e10cSrcweir 	{
185*cdf0e10cSrcweir 		case TypeClass_CHAR:
186*cdf0e10cSrcweir 			{
187*cdf0e10cSrcweir 				Sequence<sal_Unicode> seq;
188*cdf0e10cSrcweir 				orig >>= seq;
189*cdf0e10cSrcweir 				Sequence<Any> seq2;
190*cdf0e10cSrcweir 				returned >>= seq2;
191*cdf0e10cSrcweir 				return equalSequences(seq, seq2);
192*cdf0e10cSrcweir 			}
193*cdf0e10cSrcweir 		case TypeClass_BOOLEAN:
194*cdf0e10cSrcweir 			{
195*cdf0e10cSrcweir 				Sequence<sal_Bool> seq;
196*cdf0e10cSrcweir 				orig >>= seq;
197*cdf0e10cSrcweir 				Sequence<Any> seq2;
198*cdf0e10cSrcweir 				returned >>= seq2;
199*cdf0e10cSrcweir 				return equalSequences(seq, seq2);
200*cdf0e10cSrcweir 			}
201*cdf0e10cSrcweir 		case TypeClass_BYTE:
202*cdf0e10cSrcweir 			{
203*cdf0e10cSrcweir 				Sequence<sal_Int8> seq;
204*cdf0e10cSrcweir 				orig >>= seq;
205*cdf0e10cSrcweir 				Sequence<Any> seq2;
206*cdf0e10cSrcweir 				returned >>= seq2;
207*cdf0e10cSrcweir 				return equalSequences(seq, seq2);
208*cdf0e10cSrcweir 			}
209*cdf0e10cSrcweir 		case TypeClass_SHORT:
210*cdf0e10cSrcweir 			{
211*cdf0e10cSrcweir 				Sequence<sal_Int16> seq;
212*cdf0e10cSrcweir 				orig >>= seq;
213*cdf0e10cSrcweir 				Sequence<Any> seq2;
214*cdf0e10cSrcweir 				returned >>= seq2;
215*cdf0e10cSrcweir 				return equalSequences(seq, seq2);
216*cdf0e10cSrcweir 			}
217*cdf0e10cSrcweir 		case TypeClass_LONG:
218*cdf0e10cSrcweir 			{
219*cdf0e10cSrcweir 				Sequence<sal_Int32> seq;
220*cdf0e10cSrcweir 				orig >>= seq;
221*cdf0e10cSrcweir 				Sequence<Any> seq2;
222*cdf0e10cSrcweir 				returned >>= seq2;
223*cdf0e10cSrcweir 				return equalSequences(seq, seq2);
224*cdf0e10cSrcweir 			}
225*cdf0e10cSrcweir 		case TypeClass_FLOAT:
226*cdf0e10cSrcweir 			{
227*cdf0e10cSrcweir 				Sequence<float> seq;
228*cdf0e10cSrcweir 				orig >>= seq;
229*cdf0e10cSrcweir 				Sequence<Any> seq2;
230*cdf0e10cSrcweir 				returned >>= seq2;
231*cdf0e10cSrcweir 				return equalSequences(seq, seq2);
232*cdf0e10cSrcweir 			}
233*cdf0e10cSrcweir 		case TypeClass_DOUBLE:
234*cdf0e10cSrcweir 			{
235*cdf0e10cSrcweir 				Sequence<double> seq;
236*cdf0e10cSrcweir 				orig >>= seq;
237*cdf0e10cSrcweir 				Sequence<Any> seq2;
238*cdf0e10cSrcweir 				returned >>= seq2;
239*cdf0e10cSrcweir 				return equalSequences(seq, seq2);
240*cdf0e10cSrcweir 			}
241*cdf0e10cSrcweir 		case TypeClass_STRING:
242*cdf0e10cSrcweir 			{
243*cdf0e10cSrcweir 				Sequence<OUString> seq;
244*cdf0e10cSrcweir 				orig >>= seq;
245*cdf0e10cSrcweir 				Sequence<Any> seq2;
246*cdf0e10cSrcweir 				returned >>= seq2;
247*cdf0e10cSrcweir 				return equalSequences(seq, seq2);
248*cdf0e10cSrcweir 			}
249*cdf0e10cSrcweir 		case TypeClass_ANY:
250*cdf0e10cSrcweir 			{
251*cdf0e10cSrcweir 				Sequence<Any> seq;
252*cdf0e10cSrcweir 				orig >>= seq;
253*cdf0e10cSrcweir 				Sequence<Any> seq2;
254*cdf0e10cSrcweir 				returned >>= seq2;
255*cdf0e10cSrcweir 				return equalSequences(seq, seq2);
256*cdf0e10cSrcweir 			}
257*cdf0e10cSrcweir 		case TypeClass_SEQUENCE:
258*cdf0e10cSrcweir 			{
259*cdf0e10cSrcweir 				//Sequence<sal_Unicode> seq;
260*cdf0e10cSrcweir 				//orig >>= seq;
261*cdf0e10cSrcweir 				//Sequence<Any> seq2;
262*cdf0e10cSrcweir 				//returned >>= seq2;
263*cdf0e10cSrcweir 				//return equalSequences(seq, seq2);
264*cdf0e10cSrcweir 				break;
265*cdf0e10cSrcweir 			}
266*cdf0e10cSrcweir 		case TypeClass_INTERFACE:
267*cdf0e10cSrcweir 			{
268*cdf0e10cSrcweir 				Sequence<Reference<XInvocation> > seq;
269*cdf0e10cSrcweir 				orig >>= seq;
270*cdf0e10cSrcweir 				Sequence<Any> seq2;
271*cdf0e10cSrcweir 				returned >>= seq2;
272*cdf0e10cSrcweir 				return equalSequences(seq, seq2);
273*cdf0e10cSrcweir 			}
274*cdf0e10cSrcweir 		default:
275*cdf0e10cSrcweir 			return false;
276*cdf0e10cSrcweir 	}
277*cdf0e10cSrcweir     return false;
278*cdf0e10cSrcweir }
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir template< class T >
281*cdf0e10cSrcweir bool equalSequences(const Sequence<T>& seqIn, const Sequence<Any> & seqOut)
282*cdf0e10cSrcweir {
283*cdf0e10cSrcweir 	if (seqIn.getLength() != seqOut.getLength())
284*cdf0e10cSrcweir 		return false;
285*cdf0e10cSrcweir 	int len = seqIn.getLength();
286*cdf0e10cSrcweir 	for (int i = 0; i < len; i++)
287*cdf0e10cSrcweir 	{
288*cdf0e10cSrcweir 		Any anyIn;
289*cdf0e10cSrcweir 		anyIn <<= seqIn[i];
290*cdf0e10cSrcweir 		Any anyOut = seqOut[i];
291*cdf0e10cSrcweir 		if (anyIn != anyOut)
292*cdf0e10cSrcweir 			return false;
293*cdf0e10cSrcweir 	}
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir 	return true;
296*cdf0e10cSrcweir }
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir void printSequence( Sequence<Any>& val)
299*cdf0e10cSrcweir {
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir //	typelib_TypeDescription* desc;
302*cdf0e10cSrcweir //	val.getValueTypeDescription( &desc);
303*cdf0e10cSrcweir //	typelib_typedescription_release( desc);
304*cdf0e10cSrcweir 
305*cdf0e10cSrcweir 	USES_CONVERSION;
306*cdf0e10cSrcweir 	char buff[1024];
307*cdf0e10cSrcweir 	buff[0]=0;
308*cdf0e10cSrcweir 	char tmpBuf[1024];
309*cdf0e10cSrcweir 	tmpBuf[0]=0;
310*cdf0e10cSrcweir 	sal_Int32 i;
311*cdf0e10cSrcweir 
312*cdf0e10cSrcweir 	for( i=0; i< val.getLength(); i++)
313*cdf0e10cSrcweir 	{
314*cdf0e10cSrcweir 		Any& elem= val[i];
315*cdf0e10cSrcweir 		switch ( elem.getValueTypeClass())
316*cdf0e10cSrcweir 		{
317*cdf0e10cSrcweir 		case TypeClass_BYTE:
318*cdf0e10cSrcweir 			 sprintf( tmpBuf, "sal_Int8 %d \n", *(sal_Int8*)elem.getValue());
319*cdf0e10cSrcweir 			 break;
320*cdf0e10cSrcweir 		case TypeClass_SHORT:
321*cdf0e10cSrcweir 			 sprintf( tmpBuf, "sal_Int16 %d \n", *(sal_Int16*)elem.getValue());
322*cdf0e10cSrcweir 			 break;
323*cdf0e10cSrcweir 		case TypeClass_LONG:
324*cdf0e10cSrcweir 			 sprintf( tmpBuf, "sal_Int32 %d \n", *(sal_Int32*)elem.getValue());
325*cdf0e10cSrcweir 			 break;
326*cdf0e10cSrcweir 		case TypeClass_DOUBLE:
327*cdf0e10cSrcweir 			 sprintf( tmpBuf, "double %f \n", *(double*)elem.getValue());
328*cdf0e10cSrcweir 			 break;
329*cdf0e10cSrcweir 		case TypeClass_FLOAT:
330*cdf0e10cSrcweir 			 sprintf( tmpBuf, "float %f \n", *(float*)elem.getValue());
331*cdf0e10cSrcweir 			 break;
332*cdf0e10cSrcweir 		case TypeClass_STRING:
333*cdf0e10cSrcweir 			 sprintf( tmpBuf, "%S \n", (*(OUString*)elem.getValue()).getStr());
334*cdf0e10cSrcweir 			 break;
335*cdf0e10cSrcweir 		case TypeClass_INTERFACE:
336*cdf0e10cSrcweir 			{
337*cdf0e10cSrcweir 			// we assume that the interface is XInvocation of a AxTestControls.Basic component.
338*cdf0e10cSrcweir 			Reference<XInvocation> inv;
339*cdf0e10cSrcweir 			elem>>= inv;
340*cdf0e10cSrcweir 			if( inv.is())
341*cdf0e10cSrcweir 			{
342*cdf0e10cSrcweir 				Any prpVal= inv->getValue( OUString( L"prpString"));
343*cdf0e10cSrcweir 				sprintf( tmpBuf, "Property prpString: %S \n", (*(OUString*)prpVal.getValue()).getStr());
344*cdf0e10cSrcweir 			}
345*cdf0e10cSrcweir 			break;
346*cdf0e10cSrcweir 			}
347*cdf0e10cSrcweir 		default:break;
348*cdf0e10cSrcweir 		}
349*cdf0e10cSrcweir 		strcat( buff, tmpBuf);
350*cdf0e10cSrcweir 
351*cdf0e10cSrcweir 	}
352*cdf0e10cSrcweir 
353*cdf0e10cSrcweir 	MessageBox( NULL, A2T(buff), _T("clientTest: printing Sequence elements"), MB_OK);
354*cdf0e10cSrcweir }
355