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. Portions created by ActiveState Tool Corp. are Copyright (C) 2001 ActiveState Tool Corp. All Rights Reserved.
|
---|
18 | * Portions created by the Initial Developer are Copyright (C) 2001
|
---|
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 2001 by Mark Hammond.
|
---|
42 | //
|
---|
43 | // Based heavily on the Python COM support, which is
|
---|
44 | // (c) Mark Hammond and Greg Stein.
|
---|
45 | //
|
---|
46 | // (c) 2001, ActiveState corp.
|
---|
47 |
|
---|
48 | #include "PyXPCOM_std.h"
|
---|
49 | #include "nsIClassInfo.h"
|
---|
50 |
|
---|
51 | static nsIClassInfo *_GetI(PyObject *self) {
|
---|
52 | nsIID iid = NS_GET_IID(nsIClassInfo);
|
---|
53 |
|
---|
54 | if (!Py_nsISupports::Check(self, iid)) {
|
---|
55 | PyErr_SetString(PyExc_TypeError, "This object is not the correct interface");
|
---|
56 | return NULL;
|
---|
57 | }
|
---|
58 | return (nsIClassInfo *)Py_nsISupports::GetI(self);
|
---|
59 | }
|
---|
60 |
|
---|
61 | static PyObject *PyGetInterfaces(PyObject *self, PyObject *args)
|
---|
62 | {
|
---|
63 | if (!PyArg_ParseTuple(args, ""))
|
---|
64 | return NULL;
|
---|
65 | nsIClassInfo *pI = _GetI(self);
|
---|
66 | if (pI==NULL)
|
---|
67 | return NULL;
|
---|
68 |
|
---|
69 | nsIID** iidArray = nsnull;
|
---|
70 | PRUint32 iidCount = 0;
|
---|
71 | nsresult r;
|
---|
72 | Py_BEGIN_ALLOW_THREADS;
|
---|
73 | r = pI->GetInterfaces(&iidCount, &iidArray);
|
---|
74 | Py_END_ALLOW_THREADS;
|
---|
75 | if ( NS_FAILED(r) )
|
---|
76 | return PyXPCOM_BuildPyException(r);
|
---|
77 |
|
---|
78 | PyObject *ret = PyTuple_New(iidCount);
|
---|
79 | if (ret==NULL)
|
---|
80 | return NULL;
|
---|
81 | for (PRUint32 i=0;i<iidCount;i++)
|
---|
82 | PyTuple_SET_ITEM( ret, i, Py_nsIID::PyObjectFromIID(*(iidArray[i])) );
|
---|
83 | return ret;
|
---|
84 | }
|
---|
85 |
|
---|
86 | static PyObject *PyGetHelperForLanguage(PyObject *self, PyObject *args)
|
---|
87 | {
|
---|
88 | PRUint32 language = nsIProgrammingLanguage::PYTHON;
|
---|
89 | if (!PyArg_ParseTuple(args, "|i", &language))
|
---|
90 | return NULL;
|
---|
91 | nsIClassInfo *pI = _GetI(self);
|
---|
92 | if (pI==NULL)
|
---|
93 | return NULL;
|
---|
94 |
|
---|
95 | nsresult r;
|
---|
96 | nsCOMPtr<nsISupports> pi;
|
---|
97 | Py_BEGIN_ALLOW_THREADS;
|
---|
98 | r = pI->GetHelperForLanguage(language, getter_AddRefs(pi));
|
---|
99 | Py_END_ALLOW_THREADS;
|
---|
100 | if ( NS_FAILED(r) )
|
---|
101 | return PyXPCOM_BuildPyException(r);
|
---|
102 |
|
---|
103 | return Py_nsISupports::PyObjectFromInterface(pi, NS_GET_IID(nsISupports));
|
---|
104 | }
|
---|
105 |
|
---|
106 | static PyObject *MakeStringOrNone(char *v)
|
---|
107 | {
|
---|
108 | if (v)
|
---|
109 | #if PY_MAJOR_VERSION <= 2
|
---|
110 | return PyString_FromString(v);
|
---|
111 | #else
|
---|
112 | return PyUnicode_FromString(v);
|
---|
113 | #endif
|
---|
114 | Py_INCREF(Py_None);
|
---|
115 | return Py_None;
|
---|
116 | }
|
---|
117 |
|
---|
118 | #define GETATTR_CHECK_RESULT(nr) if (NS_FAILED(nr)) return PyXPCOM_BuildPyException(nr)
|
---|
119 |
|
---|
120 | PyObject *
|
---|
121 | Py_nsIClassInfo::getattr(const char *name)
|
---|
122 | {
|
---|
123 | nsIClassInfo *pI = _GetI(this);
|
---|
124 | if (pI==NULL)
|
---|
125 | return NULL;
|
---|
126 |
|
---|
127 | nsresult nr;
|
---|
128 | PyObject *ret = NULL;
|
---|
129 | if (strcmp(name, "contractID")==0) {
|
---|
130 | char *str_ret = NULL;
|
---|
131 | Py_BEGIN_ALLOW_THREADS;
|
---|
132 | nr = pI->GetContractID(&str_ret);
|
---|
133 | Py_END_ALLOW_THREADS;
|
---|
134 | GETATTR_CHECK_RESULT(nr);
|
---|
135 | ret = MakeStringOrNone(str_ret);
|
---|
136 | nsMemory::Free(str_ret);
|
---|
137 | } else if (strcmp(name, "classDescription")==0) {
|
---|
138 | char *str_ret = NULL;
|
---|
139 | Py_BEGIN_ALLOW_THREADS;
|
---|
140 | nr = pI->GetClassDescription(&str_ret);
|
---|
141 | Py_END_ALLOW_THREADS;
|
---|
142 | GETATTR_CHECK_RESULT(nr);
|
---|
143 | ret = MakeStringOrNone(str_ret);
|
---|
144 | nsMemory::Free(str_ret);
|
---|
145 | } else if (strcmp(name, "classID")==0) {
|
---|
146 | nsIID *iid = NULL;
|
---|
147 | Py_BEGIN_ALLOW_THREADS;
|
---|
148 | nr = pI->GetClassID(&iid);
|
---|
149 | Py_END_ALLOW_THREADS;
|
---|
150 | GETATTR_CHECK_RESULT(nr);
|
---|
151 | ret = Py_nsIID::PyObjectFromIID(*iid);
|
---|
152 | nsMemory::Free(iid);
|
---|
153 | } else if (strcmp(name, "implementationLanguage")==0) {
|
---|
154 | PRUint32 i;
|
---|
155 | Py_BEGIN_ALLOW_THREADS;
|
---|
156 | nr = pI->GetImplementationLanguage(&i);
|
---|
157 | Py_END_ALLOW_THREADS;
|
---|
158 | GETATTR_CHECK_RESULT(nr);
|
---|
159 | ret = PyInt_FromLong(i);
|
---|
160 | } else {
|
---|
161 | ret = Py_nsISupports::getattr(name);
|
---|
162 | }
|
---|
163 | return ret;
|
---|
164 | }
|
---|
165 |
|
---|
166 | int
|
---|
167 | Py_nsIClassInfo::setattr(const char *name, PyObject *v)
|
---|
168 | {
|
---|
169 | return Py_nsISupports::setattr(name, v);
|
---|
170 |
|
---|
171 | }
|
---|
172 |
|
---|
173 | struct PyMethodDef
|
---|
174 | PyMethods_IClassInfo[] =
|
---|
175 | {
|
---|
176 | { "getInterfaces", PyGetInterfaces, 1},
|
---|
177 | { "GetInterfaces", PyGetInterfaces, 1},
|
---|
178 | { "getHelperForLanguage", PyGetHelperForLanguage, 1},
|
---|
179 | { "GetHelperForLanguage", PyGetHelperForLanguage, 1},
|
---|
180 | {NULL}
|
---|
181 | };
|
---|