Back to index

lightning-sunbird  0.9+nobinonly
PyIComponentManagerObsolete.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 static nsIComponentManagerObsolete *GetI(PyObject *self) {
00051        static const nsIID iid = NS_GET_IID(nsIComponentManagerObsolete);
00052 
00053        if (!Py_nsISupports::Check(self, iid)) {
00054               PyErr_SetString(PyExc_TypeError, "This object is not the correct interface");
00055               return NULL;
00056        }
00057        return (nsIComponentManagerObsolete *)Py_nsISupports::GetI(self);
00058 }
00059 
00060 static PyObject *PyCreateInstanceByContractID(PyObject *self, PyObject *args)
00061 {
00062        char *pid, *notyet = NULL;
00063        PyObject *obIID = NULL;
00064        if (!PyArg_ParseTuple(args, "s|zO", &pid, &notyet, &obIID))
00065               return NULL;
00066        if (notyet != NULL) {
00067               PyErr_SetString(PyExc_ValueError, "2nd arg must be none");
00068               return NULL;
00069        }
00070        nsIComponentManagerObsolete *pI = GetI(self);
00071        if (pI==NULL)
00072               return NULL;
00073 
00074        nsIID  iid;
00075        if (obIID==NULL)
00076               iid = NS_GET_IID(nsISupports);
00077        else
00078               if (!Py_nsIID::IIDFromPyObject(obIID, &iid))
00079                      return NULL;
00080 
00081        nsISupports *pis;
00082        nsresult r;
00083        Py_BEGIN_ALLOW_THREADS;
00084        r = pI->CreateInstanceByContractID(pid, NULL, iid, (void **)&pis);
00085        Py_END_ALLOW_THREADS;
00086        if ( NS_FAILED(r) )
00087               return PyXPCOM_BuildPyException(r);
00088 
00089        /* Return a type based on the IID (with no extra ref) */
00090        return Py_nsISupports::PyObjectFromInterface(pis, iid, PR_FALSE, PR_FALSE);
00091 }
00092 
00093 static PyObject *PyContractIDToClassID(PyObject *self, PyObject *args)
00094 {
00095        char *pid;
00096        if (!PyArg_ParseTuple(args, "s", &pid))
00097               return NULL;
00098        nsIComponentManagerObsolete *pI = GetI(self);
00099        if (pI==NULL)
00100               return NULL;
00101 
00102        nsIID  iid;
00103        nsresult r;
00104        Py_BEGIN_ALLOW_THREADS;
00105        r = pI->ContractIDToClassID(pid, &iid);
00106        Py_END_ALLOW_THREADS;
00107        if ( NS_FAILED(r) )
00108               return PyXPCOM_BuildPyException(r);
00109 
00110        return Py_nsIID::PyObjectFromIID(iid);
00111 }
00112 
00113 static PyObject *PyCLSIDToContractID(PyObject *self, PyObject *args)
00114 {
00115        PyObject *obIID;
00116        if (!PyArg_ParseTuple(args, "O", &obIID))
00117               return NULL;
00118 
00119        nsIID iid;
00120        if (!Py_nsIID::IIDFromPyObject(obIID, &iid))
00121               return NULL;
00122        char *ret_pid = nsnull;
00123        char *ret_class = nsnull;
00124        nsIComponentManagerObsolete *pI = GetI(self);
00125        if (pI==NULL)
00126               return NULL;
00127 
00128        nsresult r;
00129        Py_BEGIN_ALLOW_THREADS;
00130        r = pI->CLSIDToContractID(iid, &ret_class, &ret_pid);
00131        Py_END_ALLOW_THREADS;
00132        if ( NS_FAILED(r) )
00133               return PyXPCOM_BuildPyException(r);
00134 
00135        PyObject *ob_pid = PyString_FromString(ret_pid);
00136        PyObject *ob_class = PyString_FromString(ret_class);
00137        PyObject *ret = Py_BuildValue("OO", ob_pid, ob_class);
00138        nsMemory::Free(ret_pid);
00139        nsMemory::Free(ret_class);
00140        Py_XDECREF(ob_pid);
00141        Py_XDECREF(ob_class);
00142        return ret;
00143 }
00144 
00145 static PyObject *PyEnumerateCLSIDs(PyObject *self, PyObject *args)
00146 {
00147        if (!PyArg_ParseTuple(args, ""))
00148               return NULL;
00149 
00150        nsIComponentManagerObsolete *pI = GetI(self);
00151        if (pI==NULL)
00152               return NULL;
00153 
00154        nsIEnumerator *pRet;
00155        nsresult r;
00156        Py_BEGIN_ALLOW_THREADS;
00157        r = pI->EnumerateCLSIDs(&pRet);
00158        Py_END_ALLOW_THREADS;
00159        if ( NS_FAILED(r) )
00160               return PyXPCOM_BuildPyException(r);
00161 
00162        return Py_nsISupports::PyObjectFromInterface(pRet, NS_GET_IID(nsIEnumerator), PR_FALSE);
00163 }
00164 
00165 static PyObject *PyEnumerateContractIDs(PyObject *self, PyObject *args)
00166 {
00167        if (!PyArg_ParseTuple(args, ""))
00168               return NULL;
00169 
00170        nsIComponentManagerObsolete *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->EnumerateContractIDs(&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 struct PyMethodDef 
00186 PyMethods_IComponentManagerObsolete[] =
00187 {
00188        { "CreateInstanceByContractID", PyCreateInstanceByContractID, 1},
00189        { "createInstanceByContractID", PyCreateInstanceByContractID, 1},
00190        { "EnumerateCLSIDs",        PyEnumerateCLSIDs, 1},
00191        { "enumerateCLSIDs",        PyEnumerateCLSIDs, 1},
00192        { "EnumerateContractIDs",       PyEnumerateContractIDs, 1},
00193        { "enumerateContractIDs",       PyEnumerateContractIDs, 1},
00194        { "ContractIDToClassID",        PyContractIDToClassID, 1},
00195        { "contractIDToClassID",        PyContractIDToClassID, 1},
00196        { "CLSIDToContractID",          PyCLSIDToContractID, 1},
00197        {NULL}
00198 };