void QPython::receive(QVariant variant) { QVariantList list = variant.toList(); QString event = list[0].toString(); if (handlers.contains(event)) { QJSValue callback = handlers[event]; QJSValueList args; for (int i=1; i<list.size(); i++) { args << callback.engine()->toScriptValue(list[i]); } QJSValue result = callback.call(args); if (result.isError()) { // Ideally we would throw the error back to Python (so that the // pyotherside.send() method fails, as this is where the call // originated). We can't do this, because the pyotherside.send() // call is asynchronous (it returns before we call into JS), so do // the next best thing and report the error to our error handler in // QML instead. emitError("pyotherside.send() failed handler: " + result.property("fileName").toString() + ":" + result.property("lineNumber").toString() + ": " + result.toString()); } } else { // Default action emit received(variant); } }
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; }
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::call(QMap<QJsonDbWriteRequest*, QJSValue> &callbacks, QJsonDbWriteRequest *request) { QJSValue callback = callbacks[request]; QJSEngine *engine = callback.engine(); if (!engine) { callbacks.remove(request); return; } QList<QJsonObject> objects = request->takeResults(); QJSValueList args; // object : id , statenumber , items QJSValue response= engine->newObject(); response.setProperty(JsonDbStrings::Protocol::stateNumber(), request->stateNumber()); response.setProperty(JsonDbStrings::Protocol::requestId(), request->property("requestId").toInt()); response.setProperty(QLatin1String("items"), qjsonobject_list_to_qjsvalue(objects)); args << QJSValue(QJSValue::UndefinedValue) << response; callback.call(args); callbacks.remove(request); }
void JsonDbPartition::queryFinished() { JsonDbQueryObject *object = qobject_cast<JsonDbQueryObject*>(sender()); if (object) { int id = findIds.value(object); QJSValue callback = findCallbacks.value(object); QJSEngine *engine = callback.engine(); if (engine && callback.isCallable()) { QJSValueList args; // object : id , statenumber , items QJSValue response= engine->newObject(); response.setProperty(JsonDbStrings::Protocol::stateNumber(), object->stateNumber()); response.setProperty(JsonDbStrings::Protocol::requestId(), id); response.setProperty(QLatin1String("items"), object->takeResults()); args << QJSValue(QJSValue::UndefinedValue) << response; callback.call(args); } findIds.remove(object); findCallbacks.remove(object); object->deleteLater(); } }
void JsonDatabase::onQueryFinished() { QJsonDbReadRequest *request = qobject_cast<QJsonDbReadRequest *>(sender()); if (listCallbacks.contains(request)) { QJSValue callback = listCallbacks[request]; QJSEngine *engine = callback.engine(); QJSValueList args; args << QJSValue(QJSValue::UndefinedValue); QList<QJsonObject> objects = request->takeResults(); int count = objects.count(); if (count) { QJSValue response = engine->newArray(count); for (int i = 0; i < count; ++i) { QString partitionName = objects[i].value(QStringLiteral("name")).toString(); response.setProperty(i, engine->newQObject(partition(partitionName))); } args << response; } else { args << 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); }