Back to index

lightning-sunbird  0.9+nobinonly
PyIInterfaceInfo.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Python XPCOM language bindings.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * ActiveState Tool Corp.
00018  * Portions created by the Initial Developer are Copyright (C) 2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Mark Hammond <mhammond@skippinet.com.au> (original author)
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 //
00039 // This code is part of the XPCOM extensions for Python.
00040 //
00041 // Written May 2000 by Mark Hammond.
00042 //
00043 // Based heavily on the Python COM support, which is
00044 // (c) Mark Hammond and Greg Stein.
00045 //
00046 // (c) 2000, ActiveState corp.
00047 
00048 #include "PyXPCOM_std.h"
00049 
00050 
00051 static nsIInterfaceInfo *GetI(PyObject *self) {
00052        nsIID iid = NS_GET_IID(nsIInterfaceInfo);
00053 
00054        if (!Py_nsISupports::Check(self, iid)) {
00055               PyErr_SetString(PyExc_TypeError, "This object is not the correct interface");
00056               return NULL;
00057        }
00058        return (nsIInterfaceInfo *)Py_nsISupports::GetI(self);
00059 }
00060 
00061 static PyObject *PyGetName(PyObject *self, PyObject *args)
00062 {
00063        if (!PyArg_ParseTuple(args, ":GetName"))
00064               return NULL;
00065 
00066        nsIInterfaceInfo *pI = GetI(self);
00067        if (pI==NULL)
00068               return NULL;
00069 
00070        char *name;
00071        nsresult r;
00072        Py_BEGIN_ALLOW_THREADS;
00073        r = pI->GetName(&name);
00074        Py_END_ALLOW_THREADS;
00075        if ( NS_FAILED(r) )
00076               return PyXPCOM_BuildPyException(r);
00077        PyObject *ret = PyString_FromString(name);
00078        nsMemory::Free(name);
00079        return ret;
00080 }
00081 
00082 static PyObject *PyGetIID(PyObject *self, PyObject *args)
00083 {
00084        if (!PyArg_ParseTuple(args, ":GetIID"))
00085               return NULL;
00086 
00087        nsIInterfaceInfo *pI = GetI(self);
00088        if (pI==NULL)
00089               return NULL;
00090 
00091        nsIID *iid_ret;
00092        nsresult r;
00093        Py_BEGIN_ALLOW_THREADS;
00094        r = pI->GetInterfaceIID(&iid_ret);
00095        Py_END_ALLOW_THREADS;
00096        if ( NS_FAILED(r) )
00097               return PyXPCOM_BuildPyException(r);
00098        PyObject *ret = Py_nsIID::PyObjectFromIID(*iid_ret);
00099        nsMemory::Free(iid_ret);
00100        return ret;
00101 }
00102 
00103 static PyObject *PyIsScriptable(PyObject *self, PyObject *args)
00104 {
00105        if (!PyArg_ParseTuple(args, ":IsScriptable"))
00106               return NULL;
00107 
00108        nsIInterfaceInfo *pI = GetI(self);
00109        if (pI==NULL)
00110               return NULL;
00111 
00112        PRBool b_ret;
00113        nsresult r;
00114        Py_BEGIN_ALLOW_THREADS;
00115        r = pI->IsScriptable(&b_ret);
00116        Py_END_ALLOW_THREADS;
00117        if ( NS_FAILED(r) )
00118               return PyXPCOM_BuildPyException(r);
00119        return PyInt_FromLong(b_ret);
00120 }
00121 
00122 static PyObject *PyGetParent(PyObject *self, PyObject *args)
00123 {
00124        if (!PyArg_ParseTuple(args, ":GetParent"))
00125               return NULL;
00126 
00127        nsIInterfaceInfo *pI = GetI(self);
00128        if (pI==NULL)
00129               return NULL;
00130 
00131        nsIInterfaceInfo *pRet;
00132        nsresult r;
00133        Py_BEGIN_ALLOW_THREADS;
00134        r = pI->GetParent(&pRet);
00135        Py_END_ALLOW_THREADS;
00136        if ( NS_FAILED(r) )
00137               return PyXPCOM_BuildPyException(r);
00138        return Py_nsISupports::PyObjectFromInterface(pRet, NS_GET_IID(nsIInterfaceInfo), PR_FALSE, PR_FALSE);
00139 }
00140 
00141 static PyObject *PyGetMethodCount(PyObject *self, PyObject *args)
00142 {
00143        if (!PyArg_ParseTuple(args, ":GetMethodCount"))
00144               return NULL;
00145 
00146        nsIInterfaceInfo *pI = GetI(self);
00147        if (pI==NULL)
00148               return NULL;
00149 
00150        PRUint16 ret;
00151        nsresult r;
00152        Py_BEGIN_ALLOW_THREADS;
00153        r = pI->GetMethodCount(&ret);
00154        Py_END_ALLOW_THREADS;
00155        if ( NS_FAILED(r) )
00156               return PyXPCOM_BuildPyException(r);
00157        return PyInt_FromLong(ret);
00158 }
00159 
00160 
00161 static PyObject *PyGetConstantCount(PyObject *self, PyObject *args)
00162 {
00163        if (!PyArg_ParseTuple(args, ":GetConstantCount"))
00164               return NULL;
00165 
00166        nsIInterfaceInfo *pI = GetI(self);
00167        if (pI==NULL)
00168               return NULL;
00169 
00170        PRUint16 ret;
00171        nsresult r;
00172        Py_BEGIN_ALLOW_THREADS;
00173        r = pI->GetConstantCount(&ret);
00174        Py_END_ALLOW_THREADS;
00175        if ( NS_FAILED(r) )
00176               return PyXPCOM_BuildPyException(r);
00177        return PyInt_FromLong(ret);
00178 }
00179 
00180 static PyObject *PyGetMethodInfo(PyObject *self, PyObject *args)
00181 {
00182        PRUint16 index;
00183        if (!PyArg_ParseTuple(args, "h:GetMethodInfo", &index))
00184               return NULL;
00185 
00186        nsIInterfaceInfo *pI = GetI(self);
00187        if (pI==NULL)
00188               return NULL;
00189 
00190        PRUint16 nmethods;
00191        pI->GetMethodCount(&nmethods);
00192        if (index>=nmethods) {
00193               PyErr_SetString(PyExc_ValueError, "The method index is out of range");
00194               return NULL;
00195        }
00196 
00197        const nsXPTMethodInfo *pRet;
00198        nsresult r;
00199        Py_BEGIN_ALLOW_THREADS;
00200        r = pI->GetMethodInfo(index, &pRet);
00201        Py_END_ALLOW_THREADS;
00202        if ( NS_FAILED(r) )
00203               return PyXPCOM_BuildPyException(r);
00204        return PyObject_FromXPTMethodDescriptor(pRet);
00205 }
00206 
00207 static PyObject *PyGetMethodInfoForName(PyObject *self, PyObject *args)
00208 {
00209        char *name;
00210        if (!PyArg_ParseTuple(args, "s:GetMethodInfoForName", &name))
00211               return NULL;
00212 
00213        nsIInterfaceInfo *pI = GetI(self);
00214        if (pI==NULL)
00215               return NULL;
00216 
00217        const nsXPTMethodInfo *pRet;
00218        PRUint16 index;
00219        nsresult r;
00220        Py_BEGIN_ALLOW_THREADS;
00221        r = pI->GetMethodInfoForName(name, &index, &pRet);
00222        Py_END_ALLOW_THREADS;
00223        if ( NS_FAILED(r) )
00224               return PyXPCOM_BuildPyException(r);
00225        PyObject *ret_i = PyObject_FromXPTMethodDescriptor(pRet);
00226        if (ret_i==NULL)
00227               return NULL;
00228        PyObject *real_ret = Py_BuildValue("iO", (int)index, ret_i);
00229        Py_DECREF(ret_i);
00230        return real_ret;
00231 }
00232 
00233 
00234 static PyObject *PyGetConstant(PyObject *self, PyObject *args)
00235 {
00236        PRUint16 index;
00237        if (!PyArg_ParseTuple(args, "h:GetConstant", &index))
00238               return NULL;
00239 
00240        nsIInterfaceInfo *pI = GetI(self);
00241        if (pI==NULL)
00242               return NULL;
00243 
00244        const nsXPTConstant *pRet;
00245        nsresult r;
00246        Py_BEGIN_ALLOW_THREADS;
00247        r = pI->GetConstant(index, &pRet);
00248        Py_END_ALLOW_THREADS;
00249        if ( NS_FAILED(r) )
00250               return PyXPCOM_BuildPyException(r);
00251        return PyObject_FromXPTConstant(pRet);
00252 }
00253 
00254 static PRBool __GetMethodInfoHelper(nsIInterfaceInfo *pii, int mi, int pi, const nsXPTMethodInfo **ppmi)
00255 {
00256        PRUint16 nmethods=0;
00257        pii->GetMethodCount(&nmethods);
00258        if (mi<0 || mi>=nmethods) {
00259               PyErr_SetString(PyExc_ValueError, "The method index is out of range");
00260               return PR_FALSE;
00261        }
00262        const nsXPTMethodInfo *pmi;
00263        nsresult r = pii->GetMethodInfo(mi, &pmi);
00264        if ( NS_FAILED(r) ) {
00265               PyXPCOM_BuildPyException(r);
00266               return PR_FALSE;
00267        }
00268 
00269        int nparams=0;
00270        nparams = pmi->GetParamCount();
00271        if (pi<0 || pi>=nparams) {
00272               PyErr_SetString(PyExc_ValueError, "The param index is out of range");
00273               return PR_FALSE;
00274        }
00275        *ppmi = pmi;
00276        return PR_TRUE;
00277 }
00278 
00279 static PyObject *PyGetInfoForParam(PyObject *self, PyObject *args)
00280 {
00281        nsIInterfaceInfo *pii = GetI(self);
00282        if (pii==NULL)
00283               return NULL;
00284        PRUint16 mi, pi;
00285        if (!PyArg_ParseTuple(args, "hh:GetInfoForParam", &mi, &pi))
00286               return NULL;
00287        const nsXPTMethodInfo *pmi;
00288        if (!__GetMethodInfoHelper(pii, mi, pi, &pmi))
00289               return NULL;
00290        const nsXPTParamInfo& param_info = pmi->GetParam((PRUint8)pi);
00291        nsIInterfaceInfo *pnewii = nsnull;
00292        nsresult n = pii->GetInfoForParam(mi, &param_info, &pnewii);
00293        if (NS_FAILED(n))
00294               return PyXPCOM_BuildPyException(n);
00295        return Py_nsISupports::PyObjectFromInterface(pnewii, NS_GET_IID(nsIInterfaceInfo), PR_FALSE);
00296 }
00297 
00298 static PyObject *PyGetIIDForParam(PyObject *self, PyObject *args)
00299 {
00300        nsIInterfaceInfo *pii = GetI(self);
00301        if (pii==NULL)
00302               return NULL;
00303        PRUint16 mi, pi;
00304        if (!PyArg_ParseTuple(args, "hh:GetIIDForParam", &mi, &pi))
00305               return NULL;
00306        const nsXPTMethodInfo *pmi;
00307        if (!__GetMethodInfoHelper(pii, mi, pi, &pmi))
00308               return NULL;
00309        const nsXPTParamInfo& param_info = pmi->GetParam((PRUint8)pi);
00310        nsIID *piid;
00311        nsresult n = pii->GetIIDForParam(mi, &param_info, &piid);
00312        if (NS_FAILED(n) || piid==nsnull)
00313               return PyXPCOM_BuildPyException(n);
00314        PyObject *rc = Py_nsIID::PyObjectFromIID(*piid);
00315        nsMemory::Free((void*)piid);
00316        return rc;
00317 }
00318 
00319 static PyObject *PyGetTypeForParam(PyObject *self, PyObject *args)
00320 {
00321        nsIInterfaceInfo *pii = GetI(self);
00322        if (pii==NULL)
00323               return NULL;
00324        PRUint16 mi, pi, dim;
00325        if (!PyArg_ParseTuple(args, "hhh:GetTypeForParam", &mi, &pi, &dim))
00326               return NULL;
00327        const nsXPTMethodInfo *pmi;
00328        if (!__GetMethodInfoHelper(pii, mi, pi, &pmi))
00329               return NULL;
00330        nsXPTType datumType;
00331        const nsXPTParamInfo& param_info = pmi->GetParam((PRUint8)pi);
00332        nsresult n = pii->GetTypeForParam(mi, &param_info, dim, &datumType);
00333        if (NS_FAILED(n))
00334               return PyXPCOM_BuildPyException(n);
00335        return PyObject_FromXPTType(&datumType);
00336 }
00337 
00338 static PyObject *PyGetSizeIsArgNumberForParam(PyObject *self, PyObject *args)
00339 {
00340        nsIInterfaceInfo *pii = GetI(self);
00341        if (pii==NULL)
00342               return NULL;
00343        PRUint16 mi, pi, dim;
00344        if (!PyArg_ParseTuple(args, "hhh:GetSizeIsArgNumberForParam", &mi, &pi, &dim))
00345               return NULL;
00346        const nsXPTMethodInfo *pmi;
00347        if (!__GetMethodInfoHelper(pii, mi, pi, &pmi))
00348               return NULL;
00349         PRUint8 ret;
00350        const nsXPTParamInfo& param_info = pmi->GetParam((PRUint8)pi);
00351        nsresult n = pii->GetSizeIsArgNumberForParam(mi, &param_info, dim, &ret);
00352        if (NS_FAILED(n))
00353               return PyXPCOM_BuildPyException(n);
00354        return PyInt_FromLong(ret);
00355 }
00356 
00357 static PyObject *PyGetLengthIsArgNumberForParam(PyObject *self, PyObject *args)
00358 {
00359        nsIInterfaceInfo *pii = GetI(self);
00360        if (pii==NULL)
00361               return NULL;
00362        PRUint16 mi, pi, dim;
00363        if (!PyArg_ParseTuple(args, "hhh:GetLengthIsArgNumberForParam", &mi, &pi, &dim))
00364               return NULL;
00365        const nsXPTMethodInfo *pmi;
00366        if (!__GetMethodInfoHelper(pii, mi, pi, &pmi))
00367               return NULL;
00368         PRUint8 ret;
00369        const nsXPTParamInfo& param_info = pmi->GetParam((PRUint8)pi);
00370        nsresult n = pii->GetLengthIsArgNumberForParam(mi, &param_info, dim, &ret);
00371        if (NS_FAILED(n))
00372               return PyXPCOM_BuildPyException(n);
00373        return PyInt_FromLong(ret);
00374 }
00375 
00376 static PyObject *PyGetInterfaceIsArgNumberForParam(PyObject *self, PyObject *args)
00377 {
00378        nsIInterfaceInfo *pii = GetI(self);
00379        if (pii==NULL)
00380               return NULL;
00381        PRUint16 mi, pi;
00382        if (!PyArg_ParseTuple(args, "hhh:GetInterfaceIsArgNumberForParam", &mi, &pi))
00383               return NULL;
00384        const nsXPTMethodInfo *pmi;
00385        if (!__GetMethodInfoHelper(pii, mi, pi, &pmi))
00386               return NULL;
00387         PRUint8 ret;
00388        const nsXPTParamInfo& param_info = pmi->GetParam((PRUint8)pi);
00389        nsresult n = pii->GetInterfaceIsArgNumberForParam(mi, &param_info, &ret);
00390        if (NS_FAILED(n))
00391               return PyXPCOM_BuildPyException(n);
00392        return PyInt_FromLong(ret);
00393 }
00394 
00395 struct PyMethodDef 
00396 PyMethods_IInterfaceInfo[] =
00397 {
00398        { "GetName", PyGetName, 1},
00399        { "GetIID", PyGetIID, 1},
00400        { "IsScriptable", PyIsScriptable, 1},
00401        { "GetParent", PyGetParent, 1},
00402        { "GetMethodCount", PyGetMethodCount, 1},
00403        { "GetConstantCount", PyGetConstantCount, 1},
00404        { "GetMethodInfo", PyGetMethodInfo, 1},
00405        { "GetMethodInfoForName", PyGetMethodInfoForName, 1},
00406        { "GetConstant", PyGetConstant, 1},
00407        { "GetInfoForParam", PyGetInfoForParam, 1},
00408        { "GetIIDForParam", PyGetIIDForParam, 1},
00409        { "GetTypeForParam", PyGetTypeForParam, 1},
00410        { "GetSizeIsArgNumberForParam", PyGetSizeIsArgNumberForParam, 1},
00411        { "GetLengthIsArgNumberForParam", PyGetLengthIsArgNumberForParam, 1},
00412        { "GetInterfaceIsArgNumberForParam", PyGetInterfaceIsArgNumberForParam, 1},
00413        {NULL}
00414 };
00415 
00416 /*
00417   NS_IMETHOD GetMethodInfo(PRUint16 index, const nsXPTMethodInfo * *info) = 0;
00418   NS_IMETHOD GetMethodInfoForName(const char *methodName, PRUint16 *index, const nsXPTMethodInfo * *info) = 0;
00419   NS_IMETHOD GetConstant(PRUint16 index, const nsXPTConstant * *constant) = 0;
00420   NS_IMETHOD GetInfoForParam(PRUint16 methodIndex, const nsXPTParamInfo * param, nsIInterfaceInfo **_retval) = 0;
00421   NS_IMETHOD GetIIDForParam(PRUint16 methodIndex, const nsXPTParamInfo * param, nsIID * *_retval) = 0;
00422   NS_IMETHOD GetTypeForParam(PRUint16 methodIndex, const nsXPTParamInfo * param, PRUint16 dimension, nsXPTType *_retval) = 0;
00423   NS_IMETHOD GetSizeIsArgNumberForParam(PRUint16 methodIndex, const nsXPTParamInfo * param, PRUint16 dimension, PRUint8 *_retval) = 0;
00424   NS_IMETHOD GetLengthIsArgNumberForParam(PRUint16 methodIndex, const nsXPTParamInfo * param, PRUint16 dimension, PRUint8 *_retval) = 0;
00425   NS_IMETHOD GetInterfaceIsArgNumberForParam(PRUint16 methodIndex, const nsXPTParamInfo * param, PRUint8 *_retval) = 0;
00426 
00427 */