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 | //
|
---|
40 | // This code is part of the XPCOM extensions for Python.
|
---|
41 | //
|
---|
42 | // Written May 2000 by Mark Hammond.
|
---|
43 | //
|
---|
44 | // Based heavily on the Python COM support, which is
|
---|
45 | // (c) Mark Hammond and Greg Stein.
|
---|
46 | //
|
---|
47 | // (c) 2000, ActiveState corp.
|
---|
48 |
|
---|
49 | #include "PyXPCOM_std.h"
|
---|
50 | #include <nsIInterfaceInfoManager.h>
|
---|
51 |
|
---|
52 | static nsIInterfaceInfoManager *GetI(PyObject *self) {
|
---|
53 | nsIID iid = NS_GET_IID(nsIInterfaceInfoManager);
|
---|
54 |
|
---|
55 | if (!Py_nsISupports::Check(self, iid)) {
|
---|
56 | PyErr_SetString(PyExc_TypeError, "This object is not the correct interface");
|
---|
57 | return NULL;
|
---|
58 | }
|
---|
59 | return (nsIInterfaceInfoManager *)Py_nsISupports::GetI(self);
|
---|
60 | }
|
---|
61 |
|
---|
62 | static PyObject *PyGetInfoForIID(PyObject *self, PyObject *args)
|
---|
63 | {
|
---|
64 | PyObject *obIID = NULL;
|
---|
65 | if (!PyArg_ParseTuple(args, "O", &obIID))
|
---|
66 | return NULL;
|
---|
67 |
|
---|
68 | nsIInterfaceInfoManager *pI = GetI(self);
|
---|
69 | if (pI==NULL)
|
---|
70 | return NULL;
|
---|
71 |
|
---|
72 | nsIID iid;
|
---|
73 | if (!Py_nsIID::IIDFromPyObject(obIID, &iid))
|
---|
74 | return NULL;
|
---|
75 |
|
---|
76 | nsCOMPtr<nsIInterfaceInfo> pi;
|
---|
77 | nsresult r;
|
---|
78 | Py_BEGIN_ALLOW_THREADS;
|
---|
79 | r = pI->GetInfoForIID(&iid, getter_AddRefs(pi));
|
---|
80 | Py_END_ALLOW_THREADS;
|
---|
81 | if ( NS_FAILED(r) )
|
---|
82 | return PyXPCOM_BuildPyException(r);
|
---|
83 |
|
---|
84 | /* Return a type based on the IID (with no extra ref) */
|
---|
85 | nsIID new_iid = NS_GET_IID(nsIInterfaceInfo);
|
---|
86 | // Can not auto-wrap the interface info manager as it is critical to
|
---|
87 | // building the support we need for autowrap.
|
---|
88 | return Py_nsISupports::PyObjectFromInterface(pi, new_iid, PR_FALSE);
|
---|
89 | }
|
---|
90 |
|
---|
91 | static PyObject *PyGetInfoForName(PyObject *self, PyObject *args)
|
---|
92 | {
|
---|
93 | char *name;
|
---|
94 | if (!PyArg_ParseTuple(args, "s", &name))
|
---|
95 | return NULL;
|
---|
96 |
|
---|
97 | nsIInterfaceInfoManager *pI = GetI(self);
|
---|
98 | if (pI==NULL)
|
---|
99 | return NULL;
|
---|
100 |
|
---|
101 | nsCOMPtr<nsIInterfaceInfo> pi;
|
---|
102 | nsresult r;
|
---|
103 | Py_BEGIN_ALLOW_THREADS;
|
---|
104 | r = pI->GetInfoForName(name, getter_AddRefs(pi));
|
---|
105 | Py_END_ALLOW_THREADS;
|
---|
106 | if ( NS_FAILED(r) )
|
---|
107 | return PyXPCOM_BuildPyException(r);
|
---|
108 |
|
---|
109 | /* Return a type based on the IID (with no extra ref) */
|
---|
110 | // Can not auto-wrap the interface info manager as it is critical to
|
---|
111 | // building the support we need for autowrap.
|
---|
112 | return Py_nsISupports::PyObjectFromInterface(pi, NS_GET_IID(nsIInterfaceInfo), PR_FALSE);
|
---|
113 | }
|
---|
114 |
|
---|
115 | static PyObject *PyGetNameForIID(PyObject *self, PyObject *args)
|
---|
116 | {
|
---|
117 | PyObject *obIID = NULL;
|
---|
118 | if (!PyArg_ParseTuple(args, "O", &obIID))
|
---|
119 | return NULL;
|
---|
120 |
|
---|
121 | nsIInterfaceInfoManager *pI = GetI(self);
|
---|
122 | if (pI==NULL)
|
---|
123 | return NULL;
|
---|
124 |
|
---|
125 | nsIID iid;
|
---|
126 | if (!Py_nsIID::IIDFromPyObject(obIID, &iid))
|
---|
127 | return NULL;
|
---|
128 |
|
---|
129 | char *ret_name = NULL;
|
---|
130 | nsresult r;
|
---|
131 | Py_BEGIN_ALLOW_THREADS;
|
---|
132 | r = pI->GetNameForIID(&iid, &ret_name);
|
---|
133 | Py_END_ALLOW_THREADS;
|
---|
134 | if ( NS_FAILED(r) )
|
---|
135 | return PyXPCOM_BuildPyException(r);
|
---|
136 |
|
---|
137 | #if PY_MAJOR_VERSION <= 2
|
---|
138 | PyObject *ret = PyString_FromString(ret_name);
|
---|
139 | #else
|
---|
140 | PyObject *ret = PyUnicode_FromString(ret_name);
|
---|
141 | #endif
|
---|
142 | nsMemory::Free(ret_name);
|
---|
143 | return ret;
|
---|
144 | }
|
---|
145 |
|
---|
146 | static PyObject *PyGetIIDForName(PyObject *self, PyObject *args)
|
---|
147 | {
|
---|
148 | char *name;
|
---|
149 | if (!PyArg_ParseTuple(args, "s", &name))
|
---|
150 | return NULL;
|
---|
151 |
|
---|
152 | nsIInterfaceInfoManager *pI = GetI(self);
|
---|
153 | if (pI==NULL)
|
---|
154 | return NULL;
|
---|
155 |
|
---|
156 | nsIID *iid_ret;
|
---|
157 | nsresult r;
|
---|
158 | Py_BEGIN_ALLOW_THREADS;
|
---|
159 | r = pI->GetIIDForName(name, &iid_ret);
|
---|
160 | Py_END_ALLOW_THREADS;
|
---|
161 | if ( NS_FAILED(r) )
|
---|
162 | return PyXPCOM_BuildPyException(r);
|
---|
163 |
|
---|
164 | PyObject *ret = Py_nsIID::PyObjectFromIID(*iid_ret);
|
---|
165 | nsMemory::Free(iid_ret);
|
---|
166 | return ret;
|
---|
167 | }
|
---|
168 |
|
---|
169 | static PyObject *PyEnumerateInterfaces(PyObject *self, PyObject *args)
|
---|
170 | {
|
---|
171 | if (!PyArg_ParseTuple(args, ""))
|
---|
172 | return NULL;
|
---|
173 |
|
---|
174 | nsIInterfaceInfoManager *pI = GetI(self);
|
---|
175 | if (pI==NULL)
|
---|
176 | return NULL;
|
---|
177 |
|
---|
178 | nsCOMPtr<nsIEnumerator> pRet;
|
---|
179 | nsresult r;
|
---|
180 | Py_BEGIN_ALLOW_THREADS;
|
---|
181 | r = pI->EnumerateInterfaces(getter_AddRefs(pRet));
|
---|
182 | Py_END_ALLOW_THREADS;
|
---|
183 | if ( NS_FAILED(r) )
|
---|
184 | return PyXPCOM_BuildPyException(r);
|
---|
185 |
|
---|
186 | return Py_nsISupports::PyObjectFromInterface(pRet, NS_GET_IID(nsIEnumerator));
|
---|
187 | }
|
---|
188 |
|
---|
189 | // TODO:
|
---|
190 | // void autoRegisterInterfaces();
|
---|
191 |
|
---|
192 | PyMethodDef
|
---|
193 | PyMethods_IInterfaceInfoManager[] =
|
---|
194 | {
|
---|
195 | { "GetInfoForIID", PyGetInfoForIID, 1},
|
---|
196 | { "getInfoForIID", PyGetInfoForIID, 1},
|
---|
197 | { "GetInfoForName", PyGetInfoForName, 1},
|
---|
198 | { "getInfoForName", PyGetInfoForName, 1},
|
---|
199 | { "GetIIDForName", PyGetIIDForName, 1},
|
---|
200 | { "getIIDForName", PyGetIIDForName, 1},
|
---|
201 | { "GetNameForIID", PyGetNameForIID, 1},
|
---|
202 | { "getNameForIID", PyGetNameForIID, 1},
|
---|
203 | { "EnumerateInterfaces", PyEnumerateInterfaces, 1},
|
---|
204 | { "enumerateInterfaces", PyEnumerateInterfaces, 1},
|
---|
205 | {NULL}
|
---|
206 | };
|
---|