VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/python/src/PyIComponentManager.cpp

Last change on this file was 11746, checked in by vboxsync, 16 years ago

export python lib to OSE

  • Property svn:eol-style set to native
File size: 4.4 KB
Line 
1/* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is the Python XPCOM language bindings.
15 *
16 * The Initial Developer of the Original Code is
17 * ActiveState Tool Corp.
18 * Portions created by the Initial Developer are Copyright (C) 2000
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s):
22 * Mark Hammond <mhammond@skippinet.com.au> (original author)
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38//
39// This code is part of the XPCOM extensions for Python.
40//
41// Written May 2000 by Mark Hammond.
42//
43// Based heavily on the Python COM support, which is
44// (c) Mark Hammond and Greg Stein.
45//
46// (c) 2000, ActiveState corp.
47
48#include "PyXPCOM_std.h"
49
50static nsIComponentManager *GetI(PyObject *self) {
51 static const nsIID iid = NS_GET_IID(nsIComponentManager);
52
53 if (!Py_nsISupports::Check(self, iid)) {
54 PyErr_SetString(PyExc_TypeError, "This object is not the correct interface");
55 return NULL;
56 }
57 return NS_STATIC_CAST(nsIComponentManager*, Py_nsISupports::GetI(self));
58}
59
60static PyObject *PyCreateInstanceByContractID(PyObject *self, PyObject *args)
61{
62 // second arg to CreateInstanceByContractID is a "delegate" - we
63 // aren't sure of the semantics of this yet and it seems rarely used,
64 // so we just punt for now.
65 char *pid, *notyet = NULL;
66 PyObject *obIID = NULL;
67 if (!PyArg_ParseTuple(args, "s|zO", &pid, &notyet, &obIID))
68 return NULL;
69 if (notyet != NULL) {
70 PyErr_SetString(PyExc_ValueError, "2nd arg must be none");
71 return NULL;
72 }
73 nsIComponentManager *pI = GetI(self);
74 if (pI==NULL)
75 return NULL;
76
77 nsIID iid;
78 if (obIID==NULL)
79 iid = NS_GET_IID(nsISupports);
80 else
81 if (!Py_nsIID::IIDFromPyObject(obIID, &iid))
82 return NULL;
83
84 nsCOMPtr<nsISupports> pis;
85 nsresult r;
86 Py_BEGIN_ALLOW_THREADS;
87 r = pI->CreateInstanceByContractID(pid, NULL, iid, getter_AddRefs(pis));
88 Py_END_ALLOW_THREADS;
89 if ( NS_FAILED(r) )
90 return PyXPCOM_BuildPyException(r);
91
92 /* Return a type based on the IID (with no extra ref) */
93 return Py_nsISupports::PyObjectFromInterface(pis, iid, PR_FALSE);
94}
95
96static PyObject *PyCreateInstance(PyObject *self, PyObject *args)
97{
98 char *notyet = NULL;
99 PyObject *obClassID = NULL, *obIID = NULL;
100 if (!PyArg_ParseTuple(args, "O|zO", &obClassID, &notyet, &obIID))
101 return NULL;
102 if (notyet != NULL) {
103 PyErr_SetString(PyExc_ValueError, "2nd arg must be none");
104 return NULL;
105 }
106 nsIComponentManager *pI = GetI(self);
107 if (pI==NULL)
108 return NULL;
109
110 nsIID classID;
111 if (!Py_nsIID::IIDFromPyObject(obClassID, &classID))
112 return NULL;
113 nsIID iid;
114 if (obIID==NULL)
115 iid = NS_GET_IID(nsISupports);
116 else
117 if (!Py_nsIID::IIDFromPyObject(obIID, &iid))
118 return NULL;
119
120 nsCOMPtr<nsISupports> pis;
121 nsresult r;
122 Py_BEGIN_ALLOW_THREADS;
123 r = pI->CreateInstance(classID, NULL, iid, getter_AddRefs(pis));
124 Py_END_ALLOW_THREADS;
125 if ( NS_FAILED(r) )
126 return PyXPCOM_BuildPyException(r);
127
128 /* Return a type based on the IID (with no extra ref) */
129 return Py_nsISupports::PyObjectFromInterface(pis, iid, PR_FALSE);
130}
131
132struct PyMethodDef
133PyMethods_IComponentManager[] =
134{
135 { "createInstanceByContractID", PyCreateInstanceByContractID, 1},
136 { "createInstance", PyCreateInstance, 1},
137 {NULL}
138};
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use