1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_extensions.hxx"
30 #include "jscriptclasses.hxx"
31 
32 //========================================================================
33 // JScriptValue
34 //========================================================================
35 JScriptValue::JScriptValue(): m_bOutParam(0), m_bInOutParam(0)
36 {
37 }
38 
39 JScriptValue::~JScriptValue()
40 {
41 }
42 
43 
44 // JScriptValue, IDispatch --------------------------------------------
45 STDMETHODIMP JScriptValue::GetTypeInfoCount(UINT* /*pctinfo*/)
46 {
47 	return E_NOTIMPL;
48 }
49 
50 // JScriptValue, IDispatch --------------------------------------------
51 STDMETHODIMP JScriptValue::GetTypeInfo( UINT /*iTInfo*/,
52 										   LCID /*lcid*/,
53 										   ITypeInfo** /*ppTInfo*/)
54 {
55 	return E_NOTIMPL;
56 }
57 
58 // JScriptValue, IDispatch --------------------------------------------
59 STDMETHODIMP JScriptValue::GetIDsOfNames( REFIID /*riid*/,
60 											 LPOLESTR *rgszNames,
61 											 UINT /*cNames*/,
62 											 LCID /*lcid*/,
63 											 DISPID *rgDispId)
64 {
65 	if( !rgDispId)
66 		return E_POINTER;
67 
68 
69 	HRESULT ret= S_OK;
70 	CComBSTR name(*rgszNames);
71 	name.ToLower();
72 
73 	if( name == CComBSTR( L"set") )
74 		*rgDispId= 1;
75 	else if( name == CComBSTR( L"get") )
76 		*rgDispId= 2;
77 	else if( name == CComBSTR( L"initoutparam") )
78 		*rgDispId= 3;
79 	else if( name == CComBSTR( L"initinoutparam") )
80 		*rgDispId= 4;
81 	else
82 		ret= DISP_E_UNKNOWNNAME;
83 
84 	return ret;
85 }
86 
87 // JScriptValue, IDispatch --------------------------------------------
88 STDMETHODIMP JScriptValue::Invoke( DISPID dispIdMember,
89 						 REFIID /*riid*/,
90 						 LCID /*lcid*/,
91 						 WORD wFlags,
92 						 DISPPARAMS *pDispParams,
93 						 VARIANT *pVarResult,
94 						 EXCEPINFO* /*pExcepInfo*/,
95 						 UINT* /*puArgErr*/)
96 {
97 	if( pDispParams->cNamedArgs)
98 		return DISP_E_NONAMEDARGS;
99 
100 
101 	HRESULT ret= S_OK;
102 	switch( dispIdMember)
103 	{
104 	case 0: // DISPID_VALUE
105 		if( wFlags & DISPATCH_PROPERTYGET && pVarResult)
106 		{
107 			if( FAILED( VariantCopy( pVarResult, &m_varValue)))
108 				ret= E_FAIL;
109 		}
110 		else
111 			ret= E_POINTER;
112 		break;
113 	case 1:
114 		if( wFlags & DISPATCH_METHOD)
115 			ret= Set( pDispParams->rgvarg[1], pDispParams->rgvarg[0]);
116 		if( FAILED( ret))
117 			ret= DISP_E_EXCEPTION;
118 		break;
119 	case 2:
120 		if( wFlags & DISPATCH_METHOD)
121 			ret= Get( pVarResult);
122 		if( FAILED( ret))
123 			ret= DISP_E_EXCEPTION;
124 		break;
125 	case 3:
126 		if( wFlags & DISPATCH_METHOD)
127 			ret= InitOutParam();
128 		if( FAILED( ret))
129 			ret= DISP_E_EXCEPTION;
130 		break;
131 	case 4:
132 		if( wFlags & DISPATCH_METHOD)
133 			ret= InitInOutParam( pDispParams->rgvarg[1], pDispParams->rgvarg[0]);
134 		if( FAILED( ret))
135 			ret= DISP_E_EXCEPTION;
136 		break;
137 	default:
138 		ret= DISP_E_MEMBERNOTFOUND;
139 		break;
140 	}
141 
142 	return ret;
143 }
144 
145 // JScriptValue, IScriptOutParam-----------------------
146 STDMETHODIMP JScriptValue::Set( VARIANT type, VARIANT value)
147 {
148 	Lock();
149 	HRESULT hr= S_OK;
150 	m_varValue.Clear();
151 	hr= VariantCopyInd( &m_varValue, &value);
152 	VARIANT var;
153 	VariantInit( &var);
154 	if( SUCCEEDED( hr= VariantChangeType( &var, &type, 0, VT_BSTR)))
155 		m_bstrType= var.bstrVal;
156 	Unlock();
157 	return hr;
158 }
159 // JScriptValue, IScriptOutParam-----------------------
160 STDMETHODIMP JScriptValue::Get( VARIANT *val)
161 {
162 	Lock();
163 	if( !val)
164 		return E_POINTER;
165 	HRESULT hr= VariantCopy( val, &m_varValue);
166 	Unlock();
167 	return hr;
168 }
169 
170 STDMETHODIMP JScriptValue::InitOutParam()
171 {
172 	Lock();
173 	m_varValue.Clear();
174 	m_bOutParam= true;
175 	m_bInOutParam= false;
176 	Unlock();
177 	return S_OK;
178 }
179 
180 STDMETHODIMP JScriptValue::InitInOutParam( VARIANT type, VARIANT value)
181 {
182 	Lock();
183 	m_bInOutParam= true;
184 	m_bOutParam= false;
185 	Unlock();
186 	return Set( type, value);
187 }
188 
189 STDMETHODIMP JScriptValue::IsOutParam( VARIANT_BOOL * flag)
190 {
191 	Lock();
192 	if( !flag)
193 		return E_POINTER;
194 	*flag= m_bOutParam ? VARIANT_TRUE : VARIANT_FALSE;
195 	Unlock();
196 	return S_OK;
197 }
198 
199 STDMETHODIMP JScriptValue::IsInOutParam( VARIANT_BOOL * flag)
200 {
201 	Lock();
202 	if( !flag)
203 		return E_POINTER;
204 	*flag= m_bInOutParam ? VARIANT_TRUE : VARIANT_FALSE;
205 	Unlock();
206 	return S_OK;
207 }
208 
209 STDMETHODIMP JScriptValue::GetValue( BSTR* type, VARIANT *value)
210 {
211 	Lock();
212 	if( !type || !value)
213 		return E_POINTER;
214 	HRESULT hr;
215 	if( SUCCEEDED(	hr= m_bstrType.CopyTo( type)))
216 		hr= VariantCopy( value, &m_varValue);
217 	Unlock();
218 	return hr;
219 }
220 
221 //##########################################################################################
222 // JScriptOutValue
223 //##########################################################################################
224 
225 JScriptOutParam::JScriptOutParam()
226 {
227 }
228 
229 JScriptOutParam::~JScriptOutParam()
230 {
231 }
232 
233 
234 // JScriptOutParam, IDispatch --------------------------------------------
235 STDMETHODIMP JScriptOutParam::GetTypeInfoCount(UINT* /*pctinfo*/)
236 {
237 	return E_NOTIMPL;
238 }
239 
240 // JScriptOutParam, IDispatch --------------------------------------------
241 STDMETHODIMP JScriptOutParam::GetTypeInfo( UINT /*iTInfo*/,
242 										   LCID /*lcid*/,
243 										   ITypeInfo** /*ppTInfo*/)
244 {
245 	return E_NOTIMPL;
246 }
247 
248 // JScriptOutParam, IDispatch --------------------------------------------
249 STDMETHODIMP JScriptOutParam::GetIDsOfNames( REFIID /*riid*/,
250 											 LPOLESTR *rgszNames,
251 											 UINT /*cNames*/,
252 											 LCID /*lcid*/,
253 											 DISPID *rgDispId)
254 {
255 	if( !rgDispId)
256 		return E_POINTER;
257 
258 
259 	HRESULT ret= S_OK;
260 	CComBSTR name(*rgszNames);
261 	name.ToLower();
262 
263 	if( name == CComBSTR( L"0") )
264 		*rgDispId= 1;
265 	else
266 		ret= DISP_E_UNKNOWNNAME;
267 
268 	return ret;
269 }
270 
271 // JScriptOutParam, IDispatch --------------------------------------------
272 STDMETHODIMP JScriptOutParam::Invoke( DISPID dispIdMember,
273 						 REFIID /*riid*/,
274 						 LCID /*lcid*/,
275 						 WORD wFlags,
276 						 DISPPARAMS *pDispParams,
277 						 VARIANT *pVarResult,
278 						 EXCEPINFO* /*pExcepInfo*/,
279 						 UINT* /*puArgErr*/)
280 {
281 	HRESULT ret= S_OK;
282 	switch( dispIdMember)
283 	{
284 	case 0: // DISPID_VALUE
285 		if( wFlags & DISPATCH_PROPERTYGET && pVarResult)
286 		{
287 			if( FAILED( VariantCopy( pVarResult, &m_varValue)))
288 				ret= E_FAIL;
289 		}
290 		else if( wFlags & DISPATCH_PROPERTYPUT || wFlags & DISPATCH_PROPERTYPUTREF)
291 		{
292 			m_varValue.Clear();
293 			if( FAILED( VariantCopyInd( &m_varValue, &pDispParams->rgvarg[0])))
294 				ret= E_FAIL;
295 		}
296 		else
297 			ret= E_POINTER;
298 		break;
299 	case 1: //
300 		if( wFlags & DISPATCH_PROPERTYGET && pVarResult)
301 		{
302 			if( FAILED( VariantCopy( pVarResult, &m_varValue)))
303 				ret= E_FAIL;
304 		}
305 		else if( wFlags & DISPATCH_PROPERTYPUT || wFlags & DISPATCH_PROPERTYPUTREF)
306 		{
307 			m_varValue.Clear();
308 			if( FAILED( VariantCopyInd( &m_varValue, &pDispParams->rgvarg[0])))
309 				ret= E_FAIL;
310 		}
311 		else
312 			ret= E_POINTER;
313 		break;
314 
315 	default:
316 		ret= DISP_E_MEMBERNOTFOUND;
317 		break;
318 	}
319 
320 	return ret;
321 }
322