Пример #1
0
/**
 * @internal
 * @brief QBsonConverter::mapValue converts alls simple values and redirects to
 * mapObject and mapArray methods. If a value can not converted an
 * empty bsonvalue is returned.
 *
 * @param jsValue jsValue to convert
 *
 * @return QBsonValue
 */
QBsonValue QBsonConverter::mapValue(const QJSValue &jsValue)
{
    if (jsValue.isBool()) {
        return QBsonValue(jsValue.toBool());
    }
    if (jsValue.isDate()) {
        return QBsonValue(jsValue.toDateTime());
    }
    if (jsValue.isString()) {
        return QBsonValue(jsValue.toString());
    }
    if (jsValue.isNumber()) {
        int vType = jsValue.toVariant().type();
        switch (vType) {
            case QVariant::Double:
                return QBsonValue(jsValue.toVariant().toDouble());
                break;
            case QVariant::Int:
                return QBsonValue(jsValue.toVariant().toInt());
                break;
        }
    }
    if (jsValue.isArray()) {
        return mapArray(jsValue);
    }
    if (jsValue.isObject()) {
        return mapObject(jsValue);
    }

    return QBsonValue();
}
Пример #2
0
void Decision::setOpinions(QJSValue newOpinions)
{
    if (newOpinions.strictlyEquals(opinions()))
        return;
    if (!newOpinions.isObject()) {
        qDebug() << "Cannot set opinions because opinions is not an object.";
        return;
    }

    // The only way I know of to determine how many opinions there are is to iterate them and count
    int opinionCount = 0;
    QJSValueIterator itr(newOpinions);
    while (itr.hasNext()) {
        itr.next();
        ++opinionCount;
    }
    itr = newOpinions;

    // Now we actually allocate space for and set the opinions
    auto opinionList = m_decision.initOpinions(opinionCount);
    opinionCount = 0;
    while (itr.hasNext()) {
        itr.next();
        auto builder = opinionList[opinionCount++];
        builder.setContestant(itr.name().toInt());
        builder.setOpinion(itr.value().toInt());
    }

    emit opinionsChanged();
}
Пример #3
0
/**
 * @brief QBsonConverter::convert converts a jsValue to bson object. If the
 * jsValue is not a object then a empty QBsonObject returned.
 *
 * @param jsValue
 *
 * @return QBsonObject
 */
QBsonObject QBsonConverter::convert(const QJSValue &jsValue)
{
    if (jsValue.isObject()) {
        return mapObject(jsValue);
    }
    return QBsonObject();
}
Пример #4
0
void QStaticFileReader::staticLoad(const QString &file, const QJSValue &params){
    bool monitor = false;
    if ( params.isObject() ){
        if ( params.hasOwnProperty("monitor") ){
            monitor = params.property("monitor").toBool();
        }
    }

    if ( m_reader )
        disconnect(m_reader, SIGNAL(dataChanged(QByteArray)), this, SLOT(readerDataChanged(QByteArray)));

    QStaticContainer* container = QStaticContainer::grabFromContext(this);
    m_reader = container->get<QFileReader>(file);

    if ( !m_reader ){
        m_reader = new QFileReader();
        m_reader->setSource(file);
        m_reader->componentComplete();
        container->set<QFileReader>(file, m_reader);
        connect(m_reader, SIGNAL(dataChanged(QByteArray)), this, SLOT(readerDataChanged(QByteArray)));
    }

    m_data = m_reader->data();
    emit dataChanged(m_data);

    m_reader->setMonitor(monitor);
}
Пример #5
0
QJsonValue QJsonConvert::jsonFromScriptValue(const QJSValue& jsval){
    if ( jsval.isArray() ){
        QJSValueIterator it(jsval);

        QJsonArray arr;
        while( it.hasNext() ){
            it.next();

            QJsonValue itJsonValue = jsonFromScriptValue(it.value());
            if( !itJsonValue.isNull() )
                arr.append(itJsonValue);
        }

        return QJsonValue(arr);
    } else if ( jsval.isObject() ){
        QJSValueIterator it(jsval);

        QJsonObject obj;
        while( it.hasNext() ){
            it.next();
            QJsonValue itJsonValue = jsonFromScriptValue(it.value());
            if( !itJsonValue.isNull() )
                obj[it.name()] = itJsonValue;
        }
        return QJsonValue(obj);
    } else  if ( jsval.isString() ){
        return QJsonValue(jsval.toString());
    } else if ( jsval.isBool() ){
        return QJsonValue(jsval.toString());
    } else if ( jsval.isNumber() ){
        return QJsonValue(jsval.toNumber() );
    }
    return QJsonValue();
}
static QString qjsValueToString(const QJSValue &v)
{
  if (v.isArray()) {
    return QStringLiteral("<array>");
  } else if (v.isBool()) {
    return v.toBool() ? QStringLiteral("true") : QStringLiteral("false");
  } else if (v.isCallable()) {
    return QStringLiteral("<callable>");
  } else if (v.isDate()) {
    return v.toDateTime().toString();
  } else if (v.isError()) {
    return QStringLiteral("<error>");
  } else if (v.isNull()) {
    return QStringLiteral("<null>");
  } else if (v.isNumber()) {
    return QString::number(v.toNumber());
  } else if (v.isObject()) {
    return QStringLiteral("<object>");
  } else if (v.isQObject()) {
    return Util::displayString(v.toQObject());
  } else if (v.isRegExp()) {
    return QStringLiteral("<regexp>");
  } else if (v.isString()) {
    return v.toString();
  } else if (v.isUndefined()) {
    return QStringLiteral("<undefined>");
  } else if (v.isVariant()) {
    return VariantHandler::displayString(v.toVariant());
  }
  return QStringLiteral("<unknown QJSValue>");
}
Пример #7
0
QJSValue Image::read(const QJSValue& options)
{
    QByteArray format = m_reader->format();
    int quality = -1;

    if (options.isObject()) {
        QJSValue transcodeOptions = options.property("transcode");
        if (!transcodeOptions.isUndefined()) {
            QJSValue f = transcodeOptions.property("format");
            if (!f.isUndefined())
                format = f.toString().toUtf8();

            QJSValue q = transcodeOptions.property("quality");
            if (!q.isUndefined())
                quality = q.toInt();
        }
    }

    QImage img = m_reader->read();
    QBuffer buffer(&m_data);
    buffer.open(QIODevice::WriteOnly);
    img.save(&buffer, format.constData(), quality);
    m_mimeType = QStringLiteral("image/%1")
        .arg(QString::fromLatin1(format.constData()));

    return self();
}
Пример #8
0
void JSKitXMLHttpRequest::send(const QJSValue &data)
{
    QByteArray byteData;

    if (data.isUndefined() || data.isNull()) {
        // Do nothing, byteData is empty.
    } else if (data.isString()) {
        byteData == data.toString().toUtf8();
    } else if (data.isObject()) {
        if (data.hasProperty("byteLength")) {
            // Looks like an ArrayView or an ArrayBufferView!
            QJSValue buffer = data.property("buffer");
            if (buffer.isUndefined()) {
                // We must assume we've been passed an ArrayBuffer directly
                buffer = data;
            }

            QJSValue array = data.property("_bytes");
            int byteLength = data.property("byteLength").toInt();

            if (array.isArray()) {
                byteData.reserve(byteLength);

                for (int i = 0; i < byteLength; i++) {
                    byteData.append(array.property(i).toInt());
                }

                qCDebug(l) << "passed an ArrayBufferView of" << byteData.length() << "bytes";
            } else {
                qCWarning(l) << "passed an unknown/invalid ArrayBuffer" << data.toString();
            }
        } else {
            qCWarning(l) << "passed an unknown object" << data.toString();
        }

    }

    QBuffer *buffer;
    if (!byteData.isEmpty()) {
        buffer = new QBuffer;
        buffer->setData(byteData);
    } else {
        buffer = 0;
    }

    qCDebug(l) << "sending" << _verb << "to" << _request.url() << "with" << QString::fromUtf8(byteData);
    _reply = _net->sendCustomRequest(_request, _verb.toLatin1(), buffer);

    connect(_reply, &QNetworkReply::finished,
            this, &JSKitXMLHttpRequest::handleReplyFinished);
    connect(_reply, static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error),
            this, &JSKitXMLHttpRequest::handleReplyError);

    if (buffer) {
        // So that it gets deleted alongside the reply object.
        buffer->setParent(_reply);
    }
}
Пример #9
0
Option<Error> jsToJson(QJSValue value, JsonValue::Builder builder) {
    if (value.isArray()) {
        std::vector<QJSValue> arr;
        QJSValueIterator iter(value);
        while (iter.hasNext()) {
            iter.next();
            // dafuq!? why is this required
            if (!iter.hasNext()) {
                break;
            }
            arr.push_back(iter.value());
        }
        auto lst = builder.initArray(static_cast<unsigned int>(arr.size()));
        for (auto k = 0; k < arr.size(); ++k) {
            auto subvalue = lst[k];
            auto opt_err = jsToJson(arr[k], subvalue);
            if (opt_err.has()) {
                return opt_err;
            }
        }
        return Option<Error>();
    } if (value.isObject()) {
        std::vector<std::tuple<QString, QJSValue>> arr;
        QJSValueIterator iter(value);
        while (iter.hasNext()) {
            iter.next();
            arr.push_back(std::make_tuple(iter.name(), iter.value()));
        }
        auto lst = builder.initObject(static_cast<unsigned int>(arr.size()));
        for (auto k = 0; k < arr.size(); ++k) {
            auto value_builder = lst[k].initValue();
            const char *name = std::get<0>(arr[k]).toUtf8().constData();
            lst[k].setName(name);
            auto opt_err = jsToJson(std::get<1>(arr[k]), value_builder);
            if (opt_err.has()) {
                return opt_err;
            }
        }
        return Option<Error>();
    } if (value.isBool()) {
        builder.setBoolean(value.toBool());
        return Option<Error>();
    } if (value.isNumber()) {
        builder.setNumber(value.toNumber());
        return Option<Error>();
    } if (value.isString()) {
        const char *str = value.toString().toUtf8().constData();
        builder.setString(str);
        return Option<Error>();
    } if (value.isNull()) {
        builder.setNone();
        return Option<Error>();
    }
    auto tpname = toStdString(value.toVariant().typeName());
    Error err(Error::Kind::ClientSideError, "Invalid type: " + tpname);
    return err;
}
Пример #10
0
void tst_QJSValueIterator::value()
{
    QFETCH(QString, code);
    QJSEngine engine;
    QJSValue object = engine.evaluate(code);
    Q_ASSERT(object.isObject());

    QJSValueIterator it(object);
    it.next();
    QBENCHMARK {
        for (int i = 0; i < 50000; ++i)
            it.value();
    }
}
Пример #11
0
QJSValue RequestPrototype::query(const QJSValue &query)
{
    if (query.isObject()) {
        JSValueIterator it(query);
        while (it.next()) {
            m_query.addQueryItem(
                it.name(),
                it.value().toString());
        }
    } else if (query.isString()) {
        QUrlQuery parsed(query.toString());
        QList<QPair<QString, QString> > items =
            parsed.queryItems(QUrl::FullyDecoded);
        QPair<QString, QString> pair;
        foreach(pair, items) {
            m_query.addQueryItem(pair.first, pair.second);
        }
Пример #12
0
void QImWrite::setParams(const QJSValue &params){
    m_convertedParams.clear();
    if ( params.isObject() ){
        QJSValueIterator paramsIt(params);
        while( paramsIt.hasNext() ){
            paramsIt.next();
            if ( paramsIt.name() == "jpegQuality" ){
                m_convertedParams.push_back(CV_IMWRITE_JPEG_QUALITY);
                m_convertedParams.push_back(paramsIt.value().toInt());
            } else if ( paramsIt.name() == "pngCompression" ){
                m_convertedParams.push_back(CV_IMWRITE_PNG_COMPRESSION);
                m_convertedParams.push_back(paramsIt.value().toInt());
            } else if ( paramsIt.name() == "pxmBinary" ){
                m_convertedParams.push_back(CV_IMWRITE_PXM_BINARY);
                m_convertedParams.push_back(paramsIt.value().toInt());
            }
        }
    }

    m_params = params;
    emit paramsChanged();
}
Пример #13
0
void QFAppScriptRunnable::setCondition(QJSValue condition)
{
    m_condition = condition;

    if (condition.isString()) {
        setType(condition.toString());
        m_isSignalCondition = false;
    } else if (condition.isObject() && condition.hasProperty("connect")) {
        Q_ASSERT(!m_engine.isNull());

        QString type = QString("QuickFlux.AppScript.%1").arg(QUuid::createUuid().toString());
        setType(type);

        QString generator = "(function(dispatcher) { return function() {dispatcher.dispatch(arguments)}})";
        QFAppDispatcher* dispatcher = QFAppDispatcher::instance(m_engine);
        QFAppScriptDispatcherWrapper * wrapper = new QFAppScriptDispatcherWrapper();
        wrapper->setType(type);
        wrapper->setDispatcher(dispatcher);

        QJSValue generatorFunc = m_engine->evaluate(generator);

        QJSValueList args;
        args << m_engine->newQObject(wrapper);
        QJSValue callback = generatorFunc.call(args);

        args.clear();
        args << callback;

        QJSValue connect = condition.property("connect");
        connect.callWithInstance(condition,args);

        m_callback = callback;
        m_isSignalCondition = true;
    } else {
        qWarning() << "AppScript: Invalid condition type";
    }
}