~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/extensions/python/xpcom/src/PyIInterfaceInfo.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The contents of this file are subject to the Mozilla Public License Version
 
3
 * 1.1 (the "License"); you may not use this file except in compliance with the
 
4
 * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
 
5
 *
 
6
 * Software distributed under the License is distributed on an "AS IS" basis,
 
7
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 
8
 * the specific language governing rights and limitations under the License.
 
9
 *
 
10
 * The Original Code is the Python XPCOM language bindings.
 
11
 *
 
12
 * The Initial Developer of the Original Code is ActiveState Tool Corp.
 
13
 * Portions created by ActiveState Tool Corp. are Copyright (C) 2000, 2001
 
14
 * ActiveState Tool Corp.  All Rights Reserved.
 
15
 *
 
16
 * Contributor(s): Mark Hammond <mhammond@skippinet.com.au> (original author)
 
17
 *
 
18
 */
 
19
 
 
20
//
 
21
// This code is part of the XPCOM extensions for Python.
 
22
//
 
23
// Written May 2000 by Mark Hammond.
 
24
//
 
25
// Based heavily on the Python COM support, which is
 
26
// (c) Mark Hammond and Greg Stein.
 
27
//
 
28
// (c) 2000, ActiveState corp.
 
29
 
 
30
#include "PyXPCOM_std.h"
 
31
 
 
32
 
 
33
static nsIInterfaceInfo *GetI(PyObject *self) {
 
34
        nsIID iid = NS_GET_IID(nsIInterfaceInfo);
 
35
 
 
36
        if (!Py_nsISupports::Check(self, iid)) {
 
37
                PyErr_SetString(PyExc_TypeError, "This object is not the correct interface");
 
38
                return NULL;
 
39
        }
 
40
        return (nsIInterfaceInfo *)Py_nsISupports::GetI(self);
 
41
}
 
42
 
 
43
static PyObject *PyGetName(PyObject *self, PyObject *args)
 
44
{
 
45
        if (!PyArg_ParseTuple(args, ":GetName"))
 
46
                return NULL;
 
47
 
 
48
        nsIInterfaceInfo *pI = GetI(self);
 
49
        if (pI==NULL)
 
50
                return NULL;
 
51
 
 
52
        char *name;
 
53
        nsresult r;
 
54
        Py_BEGIN_ALLOW_THREADS;
 
55
        r = pI->GetName(&name);
 
56
        Py_END_ALLOW_THREADS;
 
57
        if ( NS_FAILED(r) )
 
58
                return PyXPCOM_BuildPyException(r);
 
59
        PyObject *ret = PyString_FromString(name);
 
60
        nsMemory::Free(name);
 
61
        return ret;
 
62
}
 
63
 
 
64
static PyObject *PyGetIID(PyObject *self, PyObject *args)
 
65
{
 
66
        if (!PyArg_ParseTuple(args, ":GetIID"))
 
67
                return NULL;
 
68
 
 
69
        nsIInterfaceInfo *pI = GetI(self);
 
70
        if (pI==NULL)
 
71
                return NULL;
 
72
 
 
73
        nsIID *iid_ret;
 
74
        nsresult r;
 
75
        Py_BEGIN_ALLOW_THREADS;
 
76
        r = pI->GetInterfaceIID(&iid_ret);
 
77
        Py_END_ALLOW_THREADS;
 
78
        if ( NS_FAILED(r) )
 
79
                return PyXPCOM_BuildPyException(r);
 
80
        PyObject *ret = Py_nsIID::PyObjectFromIID(*iid_ret);
 
81
        nsMemory::Free(iid_ret);
 
82
        return ret;
 
83
}
 
84
 
 
85
static PyObject *PyIsScriptable(PyObject *self, PyObject *args)
 
86
{
 
87
        if (!PyArg_ParseTuple(args, ":IsScriptable"))
 
88
                return NULL;
 
89
 
 
90
        nsIInterfaceInfo *pI = GetI(self);
 
91
        if (pI==NULL)
 
92
                return NULL;
 
93
 
 
94
        PRBool b_ret;
 
95
        nsresult r;
 
96
        Py_BEGIN_ALLOW_THREADS;
 
97
        r = pI->IsScriptable(&b_ret);
 
98
        Py_END_ALLOW_THREADS;
 
99
        if ( NS_FAILED(r) )
 
100
                return PyXPCOM_BuildPyException(r);
 
101
        return PyInt_FromLong(b_ret);
 
102
}
 
103
 
 
104
static PyObject *PyGetParent(PyObject *self, PyObject *args)
 
105
{
 
106
        if (!PyArg_ParseTuple(args, ":GetParent"))
 
107
                return NULL;
 
108
 
 
109
        nsIInterfaceInfo *pI = GetI(self);
 
110
        if (pI==NULL)
 
111
                return NULL;
 
112
 
 
113
        nsIInterfaceInfo *pRet;
 
114
        nsresult r;
 
115
        Py_BEGIN_ALLOW_THREADS;
 
116
        r = pI->GetParent(&pRet);
 
117
        Py_END_ALLOW_THREADS;
 
118
        if ( NS_FAILED(r) )
 
119
                return PyXPCOM_BuildPyException(r);
 
120
        return Py_nsISupports::PyObjectFromInterface(pRet, NS_GET_IID(nsIInterfaceInfo), PR_FALSE, PR_FALSE);
 
121
}
 
122
 
 
123
static PyObject *PyGetMethodCount(PyObject *self, PyObject *args)
 
124
{
 
125
        if (!PyArg_ParseTuple(args, ":GetMethodCount"))
 
126
                return NULL;
 
127
 
 
128
        nsIInterfaceInfo *pI = GetI(self);
 
129
        if (pI==NULL)
 
130
                return NULL;
 
131
 
 
132
        PRUint16 ret;
 
133
        nsresult r;
 
134
        Py_BEGIN_ALLOW_THREADS;
 
135
        r = pI->GetMethodCount(&ret);
 
136
        Py_END_ALLOW_THREADS;
 
137
        if ( NS_FAILED(r) )
 
138
                return PyXPCOM_BuildPyException(r);
 
139
        return PyInt_FromLong(ret);
 
140
}
 
141
 
 
142
 
 
143
static PyObject *PyGetConstantCount(PyObject *self, PyObject *args)
 
144
{
 
145
        if (!PyArg_ParseTuple(args, ":GetConstantCount"))
 
146
                return NULL;
 
147
 
 
148
        nsIInterfaceInfo *pI = GetI(self);
 
149
        if (pI==NULL)
 
150
                return NULL;
 
151
 
 
152
        PRUint16 ret;
 
153
        nsresult r;
 
154
        Py_BEGIN_ALLOW_THREADS;
 
155
        r = pI->GetConstantCount(&ret);
 
156
        Py_END_ALLOW_THREADS;
 
157
        if ( NS_FAILED(r) )
 
158
                return PyXPCOM_BuildPyException(r);
 
159
        return PyInt_FromLong(ret);
 
160
}
 
161
 
 
162
static PyObject *PyGetMethodInfo(PyObject *self, PyObject *args)
 
163
{
 
164
        PRUint16 index;
 
165
        if (!PyArg_ParseTuple(args, "h:GetMethodInfo", &index))
 
166
                return NULL;
 
167
 
 
168
        nsIInterfaceInfo *pI = GetI(self);
 
169
        if (pI==NULL)
 
170
                return NULL;
 
171
 
 
172
        PRUint16 nmethods;
 
173
        pI->GetMethodCount(&nmethods);
 
174
        if (index>=nmethods) {
 
175
                PyErr_SetString(PyExc_ValueError, "The method index is out of range");
 
176
                return NULL;
 
177
        }
 
178
 
 
179
        const nsXPTMethodInfo *pRet;
 
180
        nsresult r;
 
181
        Py_BEGIN_ALLOW_THREADS;
 
182
        r = pI->GetMethodInfo(index, &pRet);
 
183
        Py_END_ALLOW_THREADS;
 
184
        if ( NS_FAILED(r) )
 
185
                return PyXPCOM_BuildPyException(r);
 
186
        return PyObject_FromXPTMethodDescriptor(pRet);
 
187
}
 
188
 
 
189
static PyObject *PyGetMethodInfoForName(PyObject *self, PyObject *args)
 
190
{
 
191
        char *name;
 
192
        if (!PyArg_ParseTuple(args, "s:GetMethodInfoForName", &name))
 
193
                return NULL;
 
194
 
 
195
        nsIInterfaceInfo *pI = GetI(self);
 
196
        if (pI==NULL)
 
197
                return NULL;
 
198
 
 
199
        const nsXPTMethodInfo *pRet;
 
200
        PRUint16 index;
 
201
        nsresult r;
 
202
        Py_BEGIN_ALLOW_THREADS;
 
203
        r = pI->GetMethodInfoForName(name, &index, &pRet);
 
204
        Py_END_ALLOW_THREADS;
 
205
        if ( NS_FAILED(r) )
 
206
                return PyXPCOM_BuildPyException(r);
 
207
        PyObject *ret_i = PyObject_FromXPTMethodDescriptor(pRet);
 
208
        if (ret_i==NULL)
 
209
                return NULL;
 
210
        PyObject *real_ret = Py_BuildValue("iO", (int)index, ret_i);
 
211
        Py_DECREF(ret_i);
 
212
        return real_ret;
 
213
}
 
214
 
 
215
 
 
216
static PyObject *PyGetConstant(PyObject *self, PyObject *args)
 
217
{
 
218
        PRUint16 index;
 
219
        if (!PyArg_ParseTuple(args, "h:GetConstant", &index))
 
220
                return NULL;
 
221
 
 
222
        nsIInterfaceInfo *pI = GetI(self);
 
223
        if (pI==NULL)
 
224
                return NULL;
 
225
 
 
226
        const nsXPTConstant *pRet;
 
227
        nsresult r;
 
228
        Py_BEGIN_ALLOW_THREADS;
 
229
        r = pI->GetConstant(index, &pRet);
 
230
        Py_END_ALLOW_THREADS;
 
231
        if ( NS_FAILED(r) )
 
232
                return PyXPCOM_BuildPyException(r);
 
233
        return PyObject_FromXPTConstant(pRet);
 
234
}
 
235
 
 
236
static PRBool __GetMethodInfoHelper(nsIInterfaceInfo *pii, int mi, int pi, const nsXPTMethodInfo **ppmi)
 
237
{
 
238
        PRUint16 nmethods=0;
 
239
        pii->GetMethodCount(&nmethods);
 
240
        if (mi<0 || mi>=nmethods) {
 
241
                PyErr_SetString(PyExc_ValueError, "The method index is out of range");
 
242
                return PR_FALSE;
 
243
        }
 
244
        const nsXPTMethodInfo *pmi;
 
245
        nsresult r = pii->GetMethodInfo(mi, &pmi);
 
246
        if ( NS_FAILED(r) ) {
 
247
                PyXPCOM_BuildPyException(r);
 
248
                return PR_FALSE;
 
249
        }
 
250
 
 
251
        int nparams=0;
 
252
        nparams = pmi->GetParamCount();
 
253
        if (pi<0 || pi>=nparams) {
 
254
                PyErr_SetString(PyExc_ValueError, "The param index is out of range");
 
255
                return PR_FALSE;
 
256
        }
 
257
        *ppmi = pmi;
 
258
        return PR_TRUE;
 
259
}
 
260
 
 
261
static PyObject *PyGetInfoForParam(PyObject *self, PyObject *args)
 
262
{
 
263
        nsIInterfaceInfo *pii = GetI(self);
 
264
        if (pii==NULL)
 
265
                return NULL;
 
266
        PRUint16 mi, pi;
 
267
        if (!PyArg_ParseTuple(args, "hh:GetInfoForParam", &mi, &pi))
 
268
                return NULL;
 
269
        const nsXPTMethodInfo *pmi;
 
270
        if (!__GetMethodInfoHelper(pii, mi, pi, &pmi))
 
271
                return NULL;
 
272
        const nsXPTParamInfo& param_info = pmi->GetParam((PRUint8)pi);
 
273
        nsIInterfaceInfo *pnewii = nsnull;
 
274
        nsresult n = pii->GetInfoForParam(mi, &param_info, &pnewii);
 
275
        if (NS_FAILED(n))
 
276
                return PyXPCOM_BuildPyException(n);
 
277
        return Py_nsISupports::PyObjectFromInterface(pnewii, NS_GET_IID(nsIInterfaceInfo), PR_FALSE);
 
278
}
 
279
 
 
280
static PyObject *PyGetIIDForParam(PyObject *self, PyObject *args)
 
281
{
 
282
        nsIInterfaceInfo *pii = GetI(self);
 
283
        if (pii==NULL)
 
284
                return NULL;
 
285
        PRUint16 mi, pi;
 
286
        if (!PyArg_ParseTuple(args, "hh:GetIIDForParam", &mi, &pi))
 
287
                return NULL;
 
288
        const nsXPTMethodInfo *pmi;
 
289
        if (!__GetMethodInfoHelper(pii, mi, pi, &pmi))
 
290
                return NULL;
 
291
        const nsXPTParamInfo& param_info = pmi->GetParam((PRUint8)pi);
 
292
        nsIID *piid;
 
293
        nsresult n = pii->GetIIDForParam(mi, &param_info, &piid);
 
294
        if (NS_FAILED(n) || piid==nsnull)
 
295
                return PyXPCOM_BuildPyException(n);
 
296
        PyObject *rc = Py_nsIID::PyObjectFromIID(*piid);
 
297
        nsMemory::Free((void*)piid);
 
298
        return rc;
 
299
}
 
300
 
 
301
static PyObject *PyGetTypeForParam(PyObject *self, PyObject *args)
 
302
{
 
303
        nsIInterfaceInfo *pii = GetI(self);
 
304
        if (pii==NULL)
 
305
                return NULL;
 
306
        PRUint16 mi, pi, dim;
 
307
        if (!PyArg_ParseTuple(args, "hhh:GetTypeForParam", &mi, &pi, &dim))
 
308
                return NULL;
 
309
        const nsXPTMethodInfo *pmi;
 
310
        if (!__GetMethodInfoHelper(pii, mi, pi, &pmi))
 
311
                return NULL;
 
312
        nsXPTType datumType;
 
313
        const nsXPTParamInfo& param_info = pmi->GetParam((PRUint8)pi);
 
314
        nsresult n = pii->GetTypeForParam(mi, &param_info, dim, &datumType);
 
315
        if (NS_FAILED(n))
 
316
                return PyXPCOM_BuildPyException(n);
 
317
        return PyObject_FromXPTType(&datumType);
 
318
}
 
319
 
 
320
static PyObject *PyGetSizeIsArgNumberForParam(PyObject *self, PyObject *args)
 
321
{
 
322
        nsIInterfaceInfo *pii = GetI(self);
 
323
        if (pii==NULL)
 
324
                return NULL;
 
325
        PRUint16 mi, pi, dim;
 
326
        if (!PyArg_ParseTuple(args, "hhh:GetSizeIsArgNumberForParam", &mi, &pi, &dim))
 
327
                return NULL;
 
328
        const nsXPTMethodInfo *pmi;
 
329
        if (!__GetMethodInfoHelper(pii, mi, pi, &pmi))
 
330
                return NULL;
 
331
        PRUint8 ret;
 
332
        const nsXPTParamInfo& param_info = pmi->GetParam((PRUint8)pi);
 
333
        nsresult n = pii->GetSizeIsArgNumberForParam(mi, &param_info, dim, &ret);
 
334
        if (NS_FAILED(n))
 
335
                return PyXPCOM_BuildPyException(n);
 
336
        return PyInt_FromLong(ret);
 
337
}
 
338
 
 
339
static PyObject *PyGetLengthIsArgNumberForParam(PyObject *self, PyObject *args)
 
340
{
 
341
        nsIInterfaceInfo *pii = GetI(self);
 
342
        if (pii==NULL)
 
343
                return NULL;
 
344
        PRUint16 mi, pi, dim;
 
345
        if (!PyArg_ParseTuple(args, "hhh:GetLengthIsArgNumberForParam", &mi, &pi, &dim))
 
346
                return NULL;
 
347
        const nsXPTMethodInfo *pmi;
 
348
        if (!__GetMethodInfoHelper(pii, mi, pi, &pmi))
 
349
                return NULL;
 
350
        PRUint8 ret;
 
351
        const nsXPTParamInfo& param_info = pmi->GetParam((PRUint8)pi);
 
352
        nsresult n = pii->GetLengthIsArgNumberForParam(mi, &param_info, dim, &ret);
 
353
        if (NS_FAILED(n))
 
354
                return PyXPCOM_BuildPyException(n);
 
355
        return PyInt_FromLong(ret);
 
356
}
 
357
 
 
358
static PyObject *PyGetInterfaceIsArgNumberForParam(PyObject *self, PyObject *args)
 
359
{
 
360
        nsIInterfaceInfo *pii = GetI(self);
 
361
        if (pii==NULL)
 
362
                return NULL;
 
363
        PRUint16 mi, pi;
 
364
        if (!PyArg_ParseTuple(args, "hhh:GetInterfaceIsArgNumberForParam", &mi, &pi))
 
365
                return NULL;
 
366
        const nsXPTMethodInfo *pmi;
 
367
        if (!__GetMethodInfoHelper(pii, mi, pi, &pmi))
 
368
                return NULL;
 
369
        PRUint8 ret;
 
370
        const nsXPTParamInfo& param_info = pmi->GetParam((PRUint8)pi);
 
371
        nsresult n = pii->GetInterfaceIsArgNumberForParam(mi, &param_info, &ret);
 
372
        if (NS_FAILED(n))
 
373
                return PyXPCOM_BuildPyException(n);
 
374
        return PyInt_FromLong(ret);
 
375
}
 
376
 
 
377
struct PyMethodDef 
 
378
PyMethods_IInterfaceInfo[] =
 
379
{
 
380
        { "GetName", PyGetName, 1},
 
381
        { "GetIID", PyGetIID, 1},
 
382
        { "IsScriptable", PyIsScriptable, 1},
 
383
        { "GetParent", PyGetParent, 1},
 
384
        { "GetMethodCount", PyGetMethodCount, 1},
 
385
        { "GetConstantCount", PyGetConstantCount, 1},
 
386
        { "GetMethodInfo", PyGetMethodInfo, 1},
 
387
        { "GetMethodInfoForName", PyGetMethodInfoForName, 1},
 
388
        { "GetConstant", PyGetConstant, 1},
 
389
        { "GetInfoForParam", PyGetInfoForParam, 1},
 
390
        { "GetIIDForParam", PyGetIIDForParam, 1},
 
391
        { "GetTypeForParam", PyGetTypeForParam, 1},
 
392
        { "GetSizeIsArgNumberForParam", PyGetSizeIsArgNumberForParam, 1},
 
393
        { "GetLengthIsArgNumberForParam", PyGetLengthIsArgNumberForParam, 1},
 
394
        { "GetInterfaceIsArgNumberForParam", PyGetInterfaceIsArgNumberForParam, 1},
 
395
        {NULL}
 
396
};
 
397
 
 
398
/*
 
399
  NS_IMETHOD GetMethodInfo(PRUint16 index, const nsXPTMethodInfo * *info) = 0;
 
400
  NS_IMETHOD GetMethodInfoForName(const char *methodName, PRUint16 *index, const nsXPTMethodInfo * *info) = 0;
 
401
  NS_IMETHOD GetConstant(PRUint16 index, const nsXPTConstant * *constant) = 0;
 
402
  NS_IMETHOD GetInfoForParam(PRUint16 methodIndex, const nsXPTParamInfo * param, nsIInterfaceInfo **_retval) = 0;
 
403
  NS_IMETHOD GetIIDForParam(PRUint16 methodIndex, const nsXPTParamInfo * param, nsIID * *_retval) = 0;
 
404
  NS_IMETHOD GetTypeForParam(PRUint16 methodIndex, const nsXPTParamInfo * param, PRUint16 dimension, nsXPTType *_retval) = 0;
 
405
  NS_IMETHOD GetSizeIsArgNumberForParam(PRUint16 methodIndex, const nsXPTParamInfo * param, PRUint16 dimension, PRUint8 *_retval) = 0;
 
406
  NS_IMETHOD GetLengthIsArgNumberForParam(PRUint16 methodIndex, const nsXPTParamInfo * param, PRUint16 dimension, PRUint8 *_retval) = 0;
 
407
  NS_IMETHOD GetInterfaceIsArgNumberForParam(PRUint16 methodIndex, const nsXPTParamInfo * param, PRUint8 *_retval) = 0;
 
408
 
 
409
*/