Back to index

lightning-sunbird  0.9+nobinonly
PyIClassInfo.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. Portions created by ActiveState Tool Corp. are Copyright (C)  2001 ActiveState Tool Corp.  All Rights Reserved.
00018  * Portions created by the Initial Developer are Copyright (C) 2001
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 2001 by Mark Hammond.
00042 //
00043 // Based heavily on the Python COM support, which is
00044 // (c) Mark Hammond and Greg Stein.
00045 //
00046 // (c) 2001, ActiveState corp.
00047 
00048 #include "PyXPCOM_std.h"
00049 #include "nsIClassInfo.h"
00050 
00051 static nsIClassInfo *_GetI(PyObject *self) {
00052        nsIID iid = NS_GET_IID(nsIClassInfo);
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 (nsIClassInfo *)Py_nsISupports::GetI(self);
00059 }
00060 
00061 static PyObject *PyGetInterfaces(PyObject *self, PyObject *args)
00062 {
00063        if (!PyArg_ParseTuple(args, ""))
00064               return NULL;
00065        nsIClassInfo *pI = _GetI(self);
00066        if (pI==NULL)
00067               return NULL;
00068 
00069        nsIID** iidArray = nsnull;
00070        PRUint32 iidCount = 0;
00071        nsresult r;
00072        Py_BEGIN_ALLOW_THREADS;
00073        r = pI->GetInterfaces(&iidCount, &iidArray);
00074        Py_END_ALLOW_THREADS;
00075        if ( NS_FAILED(r) )
00076               return PyXPCOM_BuildPyException(r);
00077 
00078        PyObject *ret = PyTuple_New(iidCount);
00079        if (ret==NULL)
00080               return NULL;
00081        for (PRUint32 i=0;i<iidCount;i++)
00082               PyTuple_SET_ITEM( ret, i, Py_nsIID::PyObjectFromIID(*(iidArray[i])) );
00083        return ret;
00084 }
00085 
00086 static PyObject *PyGetHelperForLanguage(PyObject *self, PyObject *args)
00087 {
00088        PRUint32 language = nsIProgrammingLanguage::PYTHON;
00089        if (!PyArg_ParseTuple(args, "|i", &language))
00090               return NULL;
00091        nsIClassInfo *pI = _GetI(self);
00092        if (pI==NULL)
00093               return NULL;
00094 
00095        nsresult r;
00096        nsISupports *pi;
00097        Py_BEGIN_ALLOW_THREADS;
00098        r = pI->GetHelperForLanguage(language, &pi);
00099        Py_END_ALLOW_THREADS;
00100        if ( NS_FAILED(r) )
00101               return PyXPCOM_BuildPyException(r);
00102 
00103        return Py_nsISupports::PyObjectFromInterface(pi, NS_GET_IID(nsISupports), PR_FALSE);
00104 }
00105 
00106 static PyObject *MakeStringOrNone(char *v)
00107 {
00108        if (v)
00109               return PyString_FromString(v);
00110        Py_INCREF(Py_None);
00111        return Py_None;
00112 }
00113 
00114 #define GETATTR_CHECK_RESULT(nr) if (NS_FAILED(nr)) return PyXPCOM_BuildPyException(nr)
00115 
00116 PyObject *
00117 Py_nsIClassInfo::getattr(const char *name)
00118 {
00119        nsIClassInfo *pI = _GetI(this);
00120        if (pI==NULL)
00121               return NULL;
00122 
00123        nsresult nr;
00124        PyObject *ret = NULL;
00125        if (strcmp(name, "contractID")==0) {
00126               char *str_ret = NULL;
00127               Py_BEGIN_ALLOW_THREADS;
00128               nr = pI->GetContractID(&str_ret);
00129               Py_END_ALLOW_THREADS;
00130               GETATTR_CHECK_RESULT(nr);
00131               ret = MakeStringOrNone(str_ret);
00132               nsMemory::Free(str_ret);
00133        } else if (strcmp(name, "classDescription")==0) {
00134               char *str_ret = NULL;
00135               Py_BEGIN_ALLOW_THREADS;
00136               nr = pI->GetClassDescription(&str_ret);
00137               Py_END_ALLOW_THREADS;
00138               GETATTR_CHECK_RESULT(nr);
00139               ret = MakeStringOrNone(str_ret);
00140               nsMemory::Free(str_ret);
00141        } else if (strcmp(name, "classID")==0) {
00142               nsIID *iid = NULL;
00143               Py_BEGIN_ALLOW_THREADS;
00144               nr = pI->GetClassID(&iid);
00145               Py_END_ALLOW_THREADS;
00146               GETATTR_CHECK_RESULT(nr);
00147               ret = Py_nsIID::PyObjectFromIID(*iid);
00148               nsMemory::Free(iid);
00149        } else if (strcmp(name, "implementationLanguage")==0) {
00150               PRUint32 i;
00151               Py_BEGIN_ALLOW_THREADS;
00152               nr = pI->GetImplementationLanguage(&i);
00153               Py_END_ALLOW_THREADS;
00154               GETATTR_CHECK_RESULT(nr);
00155               ret = PyInt_FromLong(i);
00156        } else {
00157               ret = Py_nsISupports::getattr(name);
00158        }
00159        return ret;
00160 }
00161 
00162 int
00163 Py_nsIClassInfo::setattr(const char *name, PyObject *v)
00164 {
00165        return Py_nsISupports::setattr(name, v);
00166 
00167 }
00168 
00169 struct PyMethodDef 
00170 PyMethods_IClassInfo[] =
00171 {
00172        { "getInterfaces", PyGetInterfaces, 1},
00173        { "GetInterfaces", PyGetInterfaces, 1},
00174        { "getHelperForLanguage", PyGetHelperForLanguage, 1},
00175        { "GetHelperForLanguage", PyGetHelperForLanguage, 1},
00176        {NULL}
00177 };