[11746] | 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 <MarkH@ActiveState.com> (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 | #include <nsIEnumerator.h>
|
---|
| 50 |
|
---|
| 51 | static nsIEnumerator *GetI(PyObject *self) {
|
---|
| 52 | nsIID iid = NS_GET_IID(nsIEnumerator);
|
---|
| 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 (nsIEnumerator *)Py_nsISupports::GetI(self);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | static PyObject *PyFirst(PyObject *self, PyObject *args)
|
---|
| 62 | {
|
---|
| 63 | if (!PyArg_ParseTuple(args, ":First"))
|
---|
| 64 | return NULL;
|
---|
| 65 |
|
---|
| 66 | nsIEnumerator *pI = GetI(self);
|
---|
| 67 | if (pI==NULL)
|
---|
| 68 | return NULL;
|
---|
| 69 |
|
---|
| 70 | nsresult r;
|
---|
| 71 | Py_BEGIN_ALLOW_THREADS;
|
---|
| 72 | r = pI->First();
|
---|
| 73 | Py_END_ALLOW_THREADS;
|
---|
| 74 | return PyInt_FromLong(r);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | static PyObject *PyNext(PyObject *self, PyObject *args)
|
---|
| 78 | {
|
---|
| 79 | if (!PyArg_ParseTuple(args, ":Next"))
|
---|
| 80 | return NULL;
|
---|
| 81 |
|
---|
| 82 | nsIEnumerator *pI = GetI(self);
|
---|
| 83 | if (pI==NULL)
|
---|
| 84 | return NULL;
|
---|
| 85 |
|
---|
| 86 | nsresult r;
|
---|
| 87 | Py_BEGIN_ALLOW_THREADS;
|
---|
| 88 | r = pI->Next();
|
---|
| 89 | Py_END_ALLOW_THREADS;
|
---|
| 90 | return PyInt_FromLong(r);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | static PyObject *PyCurrentItem(PyObject *self, PyObject *args)
|
---|
| 94 | {
|
---|
| 95 | PyObject *obIID = NULL;
|
---|
| 96 | if (!PyArg_ParseTuple(args, "|O:CurrentItem", &obIID))
|
---|
| 97 | return NULL;
|
---|
| 98 |
|
---|
| 99 | nsIID iid(NS_GET_IID(nsISupports));
|
---|
| 100 | if (obIID != NULL && !Py_nsIID::IIDFromPyObject(obIID, &iid))
|
---|
| 101 | return NULL;
|
---|
| 102 | nsIEnumerator *pI = GetI(self);
|
---|
| 103 | if (pI==NULL)
|
---|
| 104 | return NULL;
|
---|
| 105 |
|
---|
| 106 | nsISupports *pRet = nsnull;
|
---|
| 107 | nsresult r;
|
---|
| 108 | Py_BEGIN_ALLOW_THREADS;
|
---|
| 109 | r = pI->CurrentItem(&pRet);
|
---|
| 110 | Py_END_ALLOW_THREADS;
|
---|
| 111 | if ( NS_FAILED(r) )
|
---|
| 112 | return PyXPCOM_BuildPyException(r);
|
---|
| 113 | if (obIID) {
|
---|
| 114 | nsISupports *temp;
|
---|
| 115 | Py_BEGIN_ALLOW_THREADS;
|
---|
| 116 | r = pRet->QueryInterface(iid, (void **)&temp);
|
---|
| 117 | pRet->Release();
|
---|
| 118 | Py_END_ALLOW_THREADS;
|
---|
| 119 | if ( NS_FAILED(r) ) {
|
---|
| 120 | return PyXPCOM_BuildPyException(r);
|
---|
| 121 | }
|
---|
| 122 | pRet = temp;
|
---|
| 123 | }
|
---|
| 124 | PyObject *ret = Py_nsISupports::PyObjectFromInterface(pRet, iid);
|
---|
| 125 | NS_IF_RELEASE(pRet);
|
---|
| 126 | return ret;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | // A method added for Python performance if you really need
|
---|
| 130 | // it. Allows you to fetch a block of objects in one
|
---|
| 131 | // hit, allowing the loop to remain implemented in C.
|
---|
| 132 | static PyObject *PyFetchBlock(PyObject *self, PyObject *args)
|
---|
| 133 | {
|
---|
| 134 | PyObject *obIID = NULL;
|
---|
| 135 | int n_wanted;
|
---|
| 136 | int n_fetched = 0;
|
---|
| 137 | if (!PyArg_ParseTuple(args, "i|O:FetchBlock", &n_wanted, &obIID))
|
---|
| 138 | return NULL;
|
---|
| 139 |
|
---|
| 140 | nsIID iid(NS_GET_IID(nsISupports));
|
---|
| 141 | if (obIID != NULL && !Py_nsIID::IIDFromPyObject(obIID, &iid))
|
---|
| 142 | return NULL;
|
---|
| 143 | nsIEnumerator *pI = GetI(self);
|
---|
| 144 | if (pI==NULL)
|
---|
| 145 | return NULL;
|
---|
| 146 |
|
---|
| 147 | // We want to fetch with the thread-lock released,
|
---|
| 148 | // but this means we can not append to the PyList
|
---|
| 149 | nsISupports **fetched = new nsISupports*[n_wanted];
|
---|
| 150 | if (fetched==nsnull) {
|
---|
| 151 | PyErr_NoMemory();
|
---|
| 152 | return NULL;
|
---|
| 153 | }
|
---|
| 154 | memset(fetched, 0, sizeof(nsISupports *) * n_wanted);
|
---|
| 155 | nsresult r = NS_OK;
|
---|
| 156 | Py_BEGIN_ALLOW_THREADS;
|
---|
| 157 | for (;n_fetched<n_wanted;) {
|
---|
| 158 | nsISupports *pNew;
|
---|
| 159 | r = pI->CurrentItem(&pNew);
|
---|
| 160 | if (NS_FAILED(r)) {
|
---|
| 161 | r = 0; // Normal enum end
|
---|
| 162 | break;
|
---|
| 163 | }
|
---|
| 164 | if (obIID) {
|
---|
| 165 | nsISupports *temp;
|
---|
| 166 | r = pNew->QueryInterface(iid, (void **)&temp);
|
---|
| 167 | pNew->Release();
|
---|
| 168 | if ( NS_FAILED(r) ) {
|
---|
| 169 | break;
|
---|
| 170 | }
|
---|
| 171 | pNew = temp;
|
---|
| 172 | }
|
---|
| 173 | fetched[n_fetched] = pNew;
|
---|
| 174 | n_fetched++; // must increment before breaking out.
|
---|
| 175 | if (NS_FAILED(pI->Next()))
|
---|
| 176 | break; // not an error condition.
|
---|
| 177 | }
|
---|
| 178 | Py_END_ALLOW_THREADS;
|
---|
| 179 | PyObject *ret;
|
---|
| 180 | if (NS_SUCCEEDED(r)) {
|
---|
| 181 | ret = PyList_New(n_fetched);
|
---|
| 182 | if (ret)
|
---|
| 183 | for (int i=0;i<n_fetched;i++) {
|
---|
| 184 | PyObject *new_ob = Py_nsISupports::PyObjectFromInterface(fetched[i], iid);
|
---|
| 185 | NS_IF_RELEASE(fetched[i]);
|
---|
| 186 | PyList_SET_ITEM(ret, i, new_ob);
|
---|
| 187 | }
|
---|
| 188 | } else
|
---|
| 189 | ret = PyXPCOM_BuildPyException(r);
|
---|
| 190 |
|
---|
| 191 | if ( ret == NULL ) {
|
---|
| 192 | // Free the objects we consumed.
|
---|
| 193 | for (int i=0;i<n_fetched;i++)
|
---|
| 194 | fetched[i]->Release();
|
---|
| 195 |
|
---|
| 196 | }
|
---|
| 197 | delete [] fetched;
|
---|
| 198 | return ret;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | static PyObject *PyIsDone(PyObject *self, PyObject *args)
|
---|
| 202 | {
|
---|
| 203 | if (!PyArg_ParseTuple(args, ":IsDone"))
|
---|
| 204 | return NULL;
|
---|
| 205 |
|
---|
| 206 | nsIEnumerator *pI = GetI(self);
|
---|
| 207 | nsresult r;
|
---|
| 208 | if (pI==NULL)
|
---|
| 209 | return NULL;
|
---|
| 210 |
|
---|
| 211 | Py_BEGIN_ALLOW_THREADS;
|
---|
| 212 | r = pI->IsDone();
|
---|
| 213 | Py_END_ALLOW_THREADS;
|
---|
| 214 | if (NS_FAILED(r))
|
---|
| 215 | return PyXPCOM_BuildPyException(r);
|
---|
| 216 | PyObject *ret = r==NS_OK ? Py_True : Py_False;
|
---|
| 217 | Py_INCREF(ret);
|
---|
| 218 | return ret;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | struct PyMethodDef
|
---|
| 222 | PyMethods_IEnumerator[] =
|
---|
| 223 | {
|
---|
| 224 | { "First", PyFirst, 1},
|
---|
| 225 | { "first", PyFirst, 1},
|
---|
| 226 | { "Next", PyNext, 1},
|
---|
| 227 | { "next", PyNext, 1},
|
---|
| 228 | { "CurrentItem", PyCurrentItem, 1},
|
---|
| 229 | { "currentItem", PyCurrentItem, 1},
|
---|
| 230 | { "IsDone", PyIsDone, 1},
|
---|
| 231 | { "isDone", PyIsDone, 1},
|
---|
| 232 | { "FetchBlock", PyFetchBlock, 1},
|
---|
| 233 | { "fetchBlock", PyFetchBlock, 1},
|
---|
| 234 | {NULL}
|
---|
| 235 | };
|
---|