Back to index

lightning-sunbird  0.9+nobinonly
PyIComponentManager.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 nsIComponentManager *GetI(PyObject *self) {
00051        static const nsIID iid = NS_GET_IID(nsIComponentManager);
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 NS_STATIC_CAST(nsIComponentManager*, Py_nsISupports::GetI(self));
00058 }
00059 
00060 static PyObject *PyCreateInstanceByContractID(PyObject *self, PyObject *args)
00061 {
00062        // second arg to CreateInstanceByContractID is a "delegate" - we
00063        // aren't sure of the semantics of this yet and it seems rarely used,
00064        // so we just punt for now.
00065        char *pid, *notyet = NULL;
00066        PyObject *obIID = NULL;
00067        if (!PyArg_ParseTuple(args, "s|zO", &pid, &notyet, &obIID))
00068               return NULL;
00069        if (notyet != NULL) {
00070               PyErr_SetString(PyExc_ValueError, "2nd arg must be none");
00071               return NULL;
00072        }
00073        nsIComponentManager *pI = GetI(self);
00074        if (pI==NULL)
00075               return NULL;
00076 
00077        nsIID  iid;
00078        if (obIID==NULL)
00079               iid = NS_GET_IID(nsISupports);
00080        else
00081               if (!Py_nsIID::IIDFromPyObject(obIID, &iid))
00082                      return NULL;
00083 
00084        nsISupports *pis;
00085        nsresult r;
00086        Py_BEGIN_ALLOW_THREADS;
00087        r = pI->CreateInstanceByContractID(pid, NULL, iid, (void **)&pis);
00088        Py_END_ALLOW_THREADS;
00089        if ( NS_FAILED(r) )
00090               return PyXPCOM_BuildPyException(r);
00091 
00092        /* Return a type based on the IID (with no extra ref) */
00093        return Py_nsISupports::PyObjectFromInterface(pis, iid, PR_FALSE, PR_FALSE);
00094 }
00095 
00096 static PyObject *PyCreateInstance(PyObject *self, PyObject *args)
00097 {
00098        char *notyet = NULL;
00099        PyObject *obClassID = NULL, *obIID = NULL;
00100        if (!PyArg_ParseTuple(args, "O|zO", &obClassID, &notyet, &obIID))
00101               return NULL;
00102        if (notyet != NULL) {
00103               PyErr_SetString(PyExc_ValueError, "2nd arg must be none");
00104               return NULL;
00105        }
00106        nsIComponentManager *pI = GetI(self);
00107        if (pI==NULL)
00108               return NULL;
00109 
00110        nsIID classID;
00111        if (!Py_nsIID::IIDFromPyObject(obClassID, &classID))
00112               return NULL;
00113        nsIID  iid;
00114        if (obIID==NULL)
00115               iid = NS_GET_IID(nsISupports);
00116        else
00117               if (!Py_nsIID::IIDFromPyObject(obIID, &iid))
00118                      return NULL;
00119 
00120        nsISupports *pis;
00121        nsresult r;
00122        Py_BEGIN_ALLOW_THREADS;
00123        r = pI->CreateInstance(classID, NULL, iid, (void **)&pis);
00124        Py_END_ALLOW_THREADS;
00125        if ( NS_FAILED(r) )
00126               return PyXPCOM_BuildPyException(r);
00127 
00128        /* Return a type based on the IID (with no extra ref) */
00129        return Py_nsISupports::PyObjectFromInterface(pis, iid, PR_FALSE, PR_FALSE);
00130 }
00131 
00132 struct PyMethodDef 
00133 PyMethods_IComponentManager[] =
00134 {
00135        { "createInstanceByContractID", PyCreateInstanceByContractID, 1},
00136        { "createInstance",             PyCreateInstance,             1},
00137        {NULL}
00138 };