Back to index

lightning-sunbird  0.9+nobinonly
PyIInterfaceInfoManager.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 //
00040 // This code is part of the XPCOM extensions for Python.
00041 //
00042 // Written May 2000 by Mark Hammond.
00043 //
00044 // Based heavily on the Python COM support, which is
00045 // (c) Mark Hammond and Greg Stein.
00046 //
00047 // (c) 2000, ActiveState corp.
00048 
00049 #include "PyXPCOM_std.h"
00050 #include <nsIInterfaceInfoManager.h>
00051 
00052 static nsIInterfaceInfoManager *GetI(PyObject *self) {
00053        nsIID iid = NS_GET_IID(nsIInterfaceInfoManager);
00054 
00055        if (!Py_nsISupports::Check(self, iid)) {
00056               PyErr_SetString(PyExc_TypeError, "This object is not the correct interface");
00057               return NULL;
00058        }
00059        return (nsIInterfaceInfoManager *)Py_nsISupports::GetI(self);
00060 }
00061 
00062 static PyObject *PyGetInfoForIID(PyObject *self, PyObject *args)
00063 {
00064        PyObject *obIID = NULL;
00065        if (!PyArg_ParseTuple(args, "O", &obIID))
00066               return NULL;
00067 
00068        nsIInterfaceInfoManager *pI = GetI(self);
00069        if (pI==NULL)
00070               return NULL;
00071 
00072        nsIID iid;
00073        if (!Py_nsIID::IIDFromPyObject(obIID, &iid))
00074               return NULL;
00075 
00076        nsIInterfaceInfo *pi;
00077        nsresult r;
00078        Py_BEGIN_ALLOW_THREADS;
00079        r = pI->GetInfoForIID(&iid, &pi);
00080        Py_END_ALLOW_THREADS;
00081        if ( NS_FAILED(r) )
00082               return PyXPCOM_BuildPyException(r);
00083 
00084        /* Return a type based on the IID (with no extra ref) */
00085        nsIID new_iid = NS_GET_IID(nsIInterfaceInfo);
00086        // Can not auto-wrap the interface info manager as it is critical to
00087        // building the support we need for autowrap.
00088        return Py_nsISupports::PyObjectFromInterface(pi, new_iid, PR_FALSE, PR_FALSE);
00089 }
00090 
00091 static PyObject *PyGetInfoForName(PyObject *self, PyObject *args)
00092 {
00093        char *name;
00094        if (!PyArg_ParseTuple(args, "s", &name))
00095               return NULL;
00096 
00097        nsIInterfaceInfoManager *pI = GetI(self);
00098        if (pI==NULL)
00099               return NULL;
00100 
00101        nsIInterfaceInfo *pi;
00102        nsresult r;
00103        Py_BEGIN_ALLOW_THREADS;
00104        r = pI->GetInfoForName(name, &pi);
00105        Py_END_ALLOW_THREADS;
00106        if ( NS_FAILED(r) )
00107               return PyXPCOM_BuildPyException(r);
00108 
00109        /* Return a type based on the IID (with no extra ref) */
00110        // Can not auto-wrap the interface info manager as it is critical to
00111        // building the support we need for autowrap.
00112        return Py_nsISupports::PyObjectFromInterface(pi, NS_GET_IID(nsIInterfaceInfo), PR_FALSE, PR_FALSE);
00113 }
00114 
00115 static PyObject *PyGetNameForIID(PyObject *self, PyObject *args)
00116 {
00117        PyObject *obIID = NULL;
00118        if (!PyArg_ParseTuple(args, "O", &obIID))
00119               return NULL;
00120 
00121        nsIInterfaceInfoManager *pI = GetI(self);
00122        if (pI==NULL)
00123               return NULL;
00124 
00125        nsIID iid;
00126        if (!Py_nsIID::IIDFromPyObject(obIID, &iid))
00127               return NULL;
00128 
00129        char *ret_name = NULL;
00130        nsresult r;
00131        Py_BEGIN_ALLOW_THREADS;
00132        r = pI->GetNameForIID(&iid, &ret_name);
00133        Py_END_ALLOW_THREADS;
00134        if ( NS_FAILED(r) )
00135               return PyXPCOM_BuildPyException(r);
00136 
00137        PyObject *ret = PyString_FromString(ret_name);
00138        nsMemory::Free(ret_name);
00139        return ret;
00140 }
00141 
00142 static PyObject *PyGetIIDForName(PyObject *self, PyObject *args)
00143 {
00144        char *name;
00145        if (!PyArg_ParseTuple(args, "s", &name))
00146               return NULL;
00147 
00148        nsIInterfaceInfoManager *pI = GetI(self);
00149        if (pI==NULL)
00150               return NULL;
00151 
00152        nsIID *iid_ret;
00153        nsresult r;
00154        Py_BEGIN_ALLOW_THREADS;
00155        r = pI->GetIIDForName(name, &iid_ret);
00156        Py_END_ALLOW_THREADS;
00157        if ( NS_FAILED(r) )
00158               return PyXPCOM_BuildPyException(r);
00159 
00160        PyObject *ret = Py_nsIID::PyObjectFromIID(*iid_ret);
00161        nsMemory::Free(iid_ret);
00162        return ret;
00163 }
00164 
00165 static PyObject *PyEnumerateInterfaces(PyObject *self, PyObject *args)
00166 {
00167        if (!PyArg_ParseTuple(args, ""))
00168               return NULL;
00169 
00170        nsIInterfaceInfoManager *pI = GetI(self);
00171        if (pI==NULL)
00172               return NULL;
00173 
00174        nsIEnumerator *pRet;
00175        nsresult r;
00176        Py_BEGIN_ALLOW_THREADS;
00177        r = pI->EnumerateInterfaces(&pRet);
00178        Py_END_ALLOW_THREADS;
00179        if ( NS_FAILED(r) )
00180               return PyXPCOM_BuildPyException(r);
00181 
00182        return Py_nsISupports::PyObjectFromInterface(pRet, NS_GET_IID(nsIEnumerator), PR_FALSE);
00183 }
00184 
00185 // TODO:
00186 // void autoRegisterInterfaces();
00187 
00188 PyMethodDef 
00189 PyMethods_IInterfaceInfoManager[] =
00190 {
00191        { "GetInfoForIID", PyGetInfoForIID, 1},
00192        { "getInfoForIID", PyGetInfoForIID, 1},
00193        { "GetInfoForName", PyGetInfoForName, 1},
00194        { "getInfoForName", PyGetInfoForName, 1},
00195        { "GetIIDForName", PyGetIIDForName, 1},
00196        { "getIIDForName", PyGetIIDForName, 1},
00197        { "GetNameForIID", PyGetNameForIID, 1},
00198        { "getNameForIID", PyGetNameForIID, 1},
00199        { "EnumerateInterfaces", PyEnumerateInterfaces, 1},
00200        { "enumerateInterfaces", PyEnumerateInterfaces, 1},
00201        {NULL}
00202 };