QJSValue QDeclarativeGalleryQueryModel::get(const QJSValue &index) const
{
    QJSEngine *scriptEngine = index.engine();

    if (!scriptEngine)
       return QJSValue();

    const int i = index.toInt();

    if (i < 0 || i >= m_rowCount || (i != m_resultSet->currentIndex() && !m_resultSet->fetch(i)))
       return QJSValue();

    QJSValue object = scriptEngine->newObject();

    object.setProperty(
            QLatin1String("itemId"), scriptEngine->toScriptValue(m_resultSet->itemId()));
    object.setProperty(
            QLatin1String("itemUrl"), scriptEngine->toScriptValue(m_resultSet->itemUrl()));

    typedef QVector<QPair<int, QString> >::const_iterator iterator;
    for (iterator it = m_propertyNames.constBegin(), end = m_propertyNames.constEnd();
            it != end;
            ++it) {
        QVariant value = m_resultSet->metaData(it->first);

        if (value.isNull())
            value = QVariant(m_resultSet->propertyType(it->first));

        object.setProperty(it->second, scriptEngine->toScriptValue(value));
    }

    return object;
}
Beispiel #2
0
QJSValue JSKitXMLHttpRequest::response() const
{
    QJSEngine *engine = _mgr->engine();
    if (_responseType.isEmpty() || _responseType == "text") {
        return engine->toScriptValue(QString::fromUtf8(_response));
    } else if (_responseType == "arraybuffer") {
        QJSValue arrayBufferProto = engine->globalObject().property("ArrayBuffer").property("prototype");
        QJSValue arrayBuf = engine->newObject();
        if (!arrayBufferProto.isUndefined()) {
            arrayBuf.setPrototype(arrayBufferProto);
            arrayBuf.setProperty("byteLength", engine->toScriptValue<uint>(_response.size()));
            QJSValue array = engine->newArray(_response.size());
            for (int i = 0; i < _response.size(); i++) {
                array.setProperty(i, engine->toScriptValue<int>(_response[i]));
            }
            arrayBuf.setProperty("_bytes", array);
            qCDebug(l) << "returning ArrayBuffer of" << _response.size() << "bytes";
        } else {
            qCWarning(l) << "Cannot find proto of ArrayBuffer";
        }
        return arrayBuf;
    } else {
        qCWarning(l) << "unsupported responseType:" << _responseType;
        return engine->toScriptValue<void*>(0);
    }
}
Beispiel #3
0
QJSValue JSKitPebble::buildAckEventObject(uint transaction, const QString &message) const
{
    QJSEngine *engine = _mgr->engine();
    QJSValue eventObj = engine->newObject();
    QJSValue dataObj = engine->newObject();

    dataObj.setProperty("transactionId", engine->toScriptValue(transaction));
    eventObj.setProperty("data", dataObj);

    if (!message.isEmpty()) {
        QJSValue errorObj = engine->newObject();
        errorObj.setProperty("message", engine->toScriptValue(message));
        eventObj.setProperty("error", errorObj);
    }

    return eventObj;
}
Beispiel #4
0
void Chat::forEachChannel(QJSValue callback) const
{
    QJSEngine *engine = DeclarativeView::globalEngine();
    for (ChatChannel *channel : m_channels) {
        QJSValueList args;
        args << engine->toScriptValue(channel);
        callback.call(args);
    }
}
void tst_QJSValueIterator::iterateForward()
{
    QFETCH(QStringList, propertyNames);
    QFETCH(QStringList, propertyValues);
    QMap<QString, QString> pmap;
    QVERIFY(propertyNames.size() == propertyValues.size());

    QJSEngine engine;
    QJSValue object = engine.newObject();
    for (int i = 0; i < propertyNames.size(); ++i) {
        QString name = propertyNames.at(i);
        QString value = propertyValues.at(i);
        pmap.insert(name, value);
        object.setProperty(name, engine.toScriptValue(value));
    }
    QJSValue otherObject = engine.newObject();
    otherObject.setProperty("foo", engine.toScriptValue(123456));
    otherObject.setProperty("protoProperty", engine.toScriptValue(654321));
    object.setPrototype(otherObject); // should not affect iterator

    QStringList lst;
    QJSValueIterator it(object);
    while (!pmap.isEmpty()) {
        QCOMPARE(it.hasNext(), true);
        QCOMPARE(it.hasNext(), true);
        it.next();
        QString name = it.name();
        QCOMPARE(pmap.contains(name), true);
        QCOMPARE(it.name(), name);
        QCOMPARE(it.value().strictlyEquals(engine.toScriptValue(pmap.value(name))), true);
        pmap.remove(name);
        lst.append(name);
    }

    QCOMPARE(it.hasNext(), false);
    QCOMPARE(it.hasNext(), false);

    it = object;
    for (int i = 0; i < lst.count(); ++i) {
        QCOMPARE(it.hasNext(), true);
        it.next();
        QCOMPARE(it.name(), lst.at(i));
    }
}
void JsonDatabase::onQueryError(QtJsonDb::QJsonDbRequest::ErrorCode code, const QString &message)
{
    QJsonDbReadRequest *request = qobject_cast<QJsonDbReadRequest *>(sender());
    if (listCallbacks.contains(request)) {
        QJSValue callback = listCallbacks[request];
        QJSEngine *engine = callback.engine();

        QJSValueList args;
        QVariantMap error;
        error.insert(QStringLiteral("code"), code);
        error.insert(QStringLiteral("message"), message);

        args << engine->toScriptValue(QVariant(error)) << engine->newArray();

        callback.call(args);
        listCallbacks.remove(request);
    }
}
void JsonDbPartition::queryStatusChanged()
{
    JsonDbQueryObject *object = qobject_cast<JsonDbQueryObject*>(sender());
    if (object && object->status() == JsonDbQueryObject::Error) {
        int id = findIds.value(object);
        QJSValue callback = findCallbacks.value(object);
        QJSEngine *engine = callback.engine();
        if (engine && callback.isCallable()) {
            QJSValueList args;

            QJSValue response = engine->newObject();
            response.setProperty(JsonDbStrings::Protocol::stateNumber(), -1);
            response.setProperty(JsonDbStrings::Protocol::requestId(),  id);
            response.setProperty(QLatin1String("items"), engine->newArray());

            args << engine->toScriptValue(object->error())<< response;
            callback.call(args);
        }
        findIds.remove(object);
        findCallbacks.remove(object);
        object->deleteLater();
    }

}
void JsonDbPartition::callErrorCallback(QMap<QJsonDbWriteRequest*, QJSValue> &callbacks, QJsonDbWriteRequest *request,
                                        QtJsonDb::QJsonDbRequest::ErrorCode code, const QString &message)
{
    QJSValue callback = callbacks[request];
    QJSEngine *engine = callback.engine();
    if (!engine) {
        callbacks.remove(request);
        return;
    }
    QJSValueList args;
    QVariantMap error;
    error.insert(QLatin1String("code"), code);
    error.insert(QLatin1String("message"), message);

    // object : id
    QJSValue response = engine->newObject();
    response.setProperty(JsonDbStrings::Protocol::stateNumber(), -1);
    response.setProperty(JsonDbStrings::Protocol::requestId(), request->property("requestId").toInt());
    response.setProperty(QLatin1String("items"), engine->newArray());

    args << engine->toScriptValue(QVariant(error))<< response;
    callback.call(args);
    callbacks.remove(request);
}