Exemplo n.º 1
0
void BSClient::replySeriesGenresFinished()
{
    qDebug() << "BSClient::replySeriesGenresFinished()";

    QByteArray byteArray = m_reply->readAll();

    if(isCaptcha(byteArray))
    {
        loadSeriesGenres();
        return;
    }

    QJsonDocument document = QJsonDocument::fromJson(byteArray);

    if(!document.isObject())
    {
        Q_EMIT error(tr("JSON-Parse-Fehler: 0x0007"));
        return;
    }

    QHash<QString, QList<QPair<int, QString> > > seriesCategories;

    QJsonObject object = document.object();

    for(QJsonObject::const_iterator iter = object.constBegin();
            iter != object.constEnd();
            iter++)
    {
        if(!iter.value().isObject())
        {
            qDebug() << iter.value();
            Q_EMIT error(tr("JSON-Parse-Fehler: 0x0008"));
            return;
        }

        QJsonObject object_ = iter.value().toObject();

        if(!object_.contains("series"))
        {
            Q_EMIT error(tr("JSON-Parse-Fehler: 0x0009"));
            return;
        }

        QJsonValue value = object_.value("series");

        if(!value.isArray())
        {
            Q_EMIT error(tr("JSON-Parse-Fehler: 0x000A"));
            return;
        }

        QList<QPair<int, QString> > series;

        QJsonArray array = iter.value().toArray();

        Q_FOREACH(const QJsonValue &value_, value.toArray())
        {
             if(!value_.isObject())
             {
                 Q_EMIT error(tr("JSON-Parse-Fehler: 0x000B"));
                 return;
             }

             QJsonObject object__ = value_.toObject();

             if(!object__.contains("name"))
             {
                 Q_EMIT error(tr("JSON-Parse-Fehler: 0x000C"));
                 return;
             }

             QJsonValue idValue = object__.value("id");

             if(!idValue.isDouble())
             {
                 Q_EMIT error(tr("JSON-Parse-Fehler: 0x000D"));
                 return;
             }

             QJsonValue nameValue = object__.value("name");

             if(!nameValue.isString())
             {
                 Q_EMIT error(tr("JSON-Parse-Fehler: 0x000E"));
                 return;
             }

             series << qMakePair(idValue.toInt(), nameValue.toString());
        }

        seriesCategories.insert(iter.key(), series);
    }

    Q_EMIT loadSeriesGenresFinished(seriesCategories);
}
Exemplo n.º 2
0
void BSClient::replyEpisodesFinished()
{
    qDebug() << "BSClient::replyEpisodesFinished()";

    QByteArray byteArray = m_reply->readAll();

    if(isCaptcha(byteArray))
    {
        loadEpisodes(m_seasonId);
        return;
    }

    QJsonDocument document = QJsonDocument::fromJson(byteArray);

    if(!document.isObject())
    {
        Q_EMIT error(tr("JSON-Parse-Fehler: 0x0015"));
        return;
    }

    QJsonObject object = document.object();

    if(!object.contains("series"))
    {
        Q_EMIT error(tr("JSON-Parse-Fehler: 0x0016"));
        return;
    }

    QJsonValue value = object.value("epi");

    if(!value.isArray())
    {
        Q_EMIT error(tr("JSON-Parse-Fehler: 0x0017"));
        return;
    }

    QList<QPair<QString, QString> > episodes;

    Q_FOREACH(const QJsonValue &value_, value.toArray())
    {
        if(!value_.isObject())
        {
            Q_EMIT error(tr("JSON-Parse-Fehler: 0x0018"));
            return;
        }

        QJsonObject object_ = value_.toObject();

        if(!object_.contains("german"))
        {
            Q_EMIT error(tr("JSON-Parse-Fehler: 0x0019"));
            return;
        }

        QJsonValue germanValue = object_.value("german");

        if(!germanValue.isString())
        {
            Q_EMIT error(tr("JSON-Parse-Fehler: 0x0020"));
            return;
        }

        QString german = germanValue.toString();

        if(!object_.contains("english"))
        {
            Q_EMIT error(tr("JSON-Parse-Fehler: 0x0021"));
            return;
        }

        QJsonValue englishValue = object_.value("english");

        if(!englishValue.isString())
        {
            Q_EMIT error(tr("JSON-Parse-Fehler: 0x0022"));
            return;
        }

        QString english = englishValue.toString();

        if(!object_.contains("epi"))
        {
            Q_EMIT error(tr("JSON-Parse-Fehler: 0x0023"));
            return;
        }

        QJsonValue epiValue = object_.value("epi");

        if(!epiValue.isString())
        {
            Q_EMIT error(tr("JSON-Parse-Fehler: 0x0024"));
            return;
        }

        QString epi = epiValue.toString();

        QString name = german;
        if(name.isEmpty())
            name = english;
        if(name.isEmpty())
            name = tr("Kein Name");

        episodes << qMakePair(QString("%0/%1").arg(m_seasonId).arg(epi), QString("%0. Folge %1").arg(epi).arg(name));
    }

    Q_EMIT loadEpisodesFinished(episodes);
}
Exemplo n.º 3
0
static Nuria::JsonMetaObjectReader::Error parseTypeObject (const QByteArray &typeName, const QJsonObject &type,
							   Nuria::MetaObjectMap &objects) {
	using namespace Nuria;
	
	QJsonValue annotationsValue = type.value (QStringLiteral("annotations"));
	QJsonValue basesValue = type.value (QStringLiteral("bases"));
	QJsonValue memberMethodsValue = type.value (QStringLiteral("memberMethods"));
	QJsonValue staticMethodsValue = type.value (QStringLiteral("staticMethods"));
	QJsonValue constructorsValue = type.value (QStringLiteral("constructors"));
	QJsonValue enumsValue = type.value (QStringLiteral("enums"));
	QJsonValue fieldsValue = type.value (QStringLiteral("fields"));
	
	// Type checks
	if (!annotationsValue.isArray ()) return JsonMetaObjectReader::AnnotationsIsNotAnArray;
	if (!basesValue.isArray ()) return JsonMetaObjectReader::BasesIsNotAnArray;
	if (!memberMethodsValue.isArray ()) return JsonMetaObjectReader::MemberMethodsIsNotAnArray;
	if (!staticMethodsValue.isArray ()) return JsonMetaObjectReader::StaticMethodsIsNotAnArray;
	if (!constructorsValue.isArray ()) return JsonMetaObjectReader::ConstructorsIsNotAnArray;
	if (!enumsValue.isObject ()) return JsonMetaObjectReader::EnumsIsNotAnObject;
	if (!fieldsValue.isObject ()) return JsonMetaObjectReader::FieldsIsNotAnObject;
	
	// Create meta object
	JsonMetaObjectReader::Error error;
	RuntimeMetaObject *metaObject = new RuntimeMetaObject (typeName);
	
	// Parse bases
	error = parseBasesArray (basesValue.toArray (), metaObject);
	if (error != Nuria::JsonMetaObjectReader::NoError) return error;
	
	// Parse annotations
	RuntimeMetaObject::AnnotationMap annotations;
	error = parseAnnotationsArray (annotationsValue.toArray (), annotations);
	if (error != Nuria::JsonMetaObjectReader::NoError) return error;
	
	// Parse methods
	JsonMetaObjectReader::Error errorMembers;
	JsonMetaObjectReader::Error errorStatics;
	JsonMetaObjectReader::Error errorCtors;
	
	errorMembers = parseMethodArray (MetaMethod::Method, memberMethodsValue.toArray (), metaObject);
	errorStatics = parseMethodArray (MetaMethod::Static, staticMethodsValue.toArray (), metaObject);
	errorCtors = parseMethodArray (MetaMethod::Constructor, constructorsValue.toArray (), metaObject);
	
	if (errorMembers != JsonMetaObjectReader::NoError) return errorMembers;
	if (errorStatics != JsonMetaObjectReader::NoError) return errorStatics;
	if (errorCtors != JsonMetaObjectReader::NoError) return errorCtors;
	
	// Parse enums
	error = parseEnumsObject (enumsValue.toObject (), metaObject);
	if (error != Nuria::JsonMetaObjectReader::NoError) return error;
	
	// Parse fields
	error = parseFieldsObject (fieldsValue.toObject (), metaObject);
	if (error != Nuria::JsonMetaObjectReader::NoError) return error;
	
	// Store and done.
	metaObject->setAnnotations (annotations);
	metaObject->finalize ();
	
	objects.insert (typeName, metaObject);
	return Nuria::JsonMetaObjectReader::NoError;
}
Exemplo n.º 4
0
Coordinate::Coordinate(const QJsonValue &value)
{
    auto json = value.toArray();
    x_ = json[0].toInt();
    y_ = json[1].toInt();
}
Exemplo n.º 5
0
void Client::judgeResult(const QJsonValue &result_str){
	QJsonArray texts = result_str.toArray();
	QString who = texts.at(0).toString();
	bool is_good = texts.at(1).toBool();
	emit judge_result(who, is_good);
}
Exemplo n.º 6
0
void AppModel::handleForecastNetworkData(QObject *replyObj)
{
    qCDebug(requestsLog) << "got forecast";
    QNetworkReply *networkReply = qobject_cast<QNetworkReply*>(replyObj);
    if (!networkReply)
        return;

    if (!networkReply->error()) {
        QJsonDocument document = QJsonDocument::fromJson(networkReply->readAll());

        QJsonObject jo;
        QJsonValue jv;
        QJsonObject root = document.object();
        jv = root.value(QStringLiteral("list"));
        if (!jv.isArray())
            qWarning() << "Invalid forecast object";
        QJsonArray ja = jv.toArray();
        //we need 4 days of forecast -> first entry is today
        if (ja.count() != 5)
            qWarning() << "Invalid forecast object";

        QString data;
        for (int i = 1; i<ja.count(); i++) {
            WeatherData *forecastEntry = new WeatherData();

            //min/max temperature
            QJsonObject subtree = ja.at(i).toObject();
            jo = subtree.value(QStringLiteral("temp")).toObject();
            jv = jo.value(QStringLiteral("min"));
            data.clear();
            data += niceTemperatureString(jv.toDouble());
            data += QChar('/');
            jv = jo.value(QStringLiteral("max"));
            data += niceTemperatureString(jv.toDouble());
            forecastEntry->setTemperature(data);

            //get date
            jv = subtree.value(QStringLiteral("dt"));
            QDateTime dt = QDateTime::fromMSecsSinceEpoch((qint64)jv.toDouble()*1000);
            forecastEntry->setDayOfWeek(dt.date().toString(QStringLiteral("ddd")));

            //get icon
            QJsonArray weatherArray = subtree.value(QStringLiteral("weather")).toArray();
            jo = weatherArray.at(0).toObject();
            forecastEntry->setWeatherIcon(jo.value(QStringLiteral("icon")).toString());

            //get description
            forecastEntry->setWeatherDescription(jo.value(QStringLiteral("description")).toString());

            d->forecast.append(forecastEntry);
        }

        if (!(d->ready)) {
            d->ready = true;
            emit readyChanged();
        }

        emit weatherChanged();
    }
    networkReply->deleteLater();
}
Exemplo n.º 7
0
void NgfpReader::attrFromJson (const QJsonValue &j_attr, Attr *attr)
{
    AttrInputType input_type = attr->getInputType();
    QVariant var;

    if (input_type == AttrInputType::Boolean)
    {
        var = j_attr.toBool();
    }

    else if (input_type == AttrInputType::Number)
    {
        var = j_attr.toInt();
    }

    else if (input_type == AttrInputType::String)
    {
        var = j_attr.toString();
    }

    else if (input_type == AttrInputType::StringList ||
             input_type == AttrInputType::PageList)
    {
        QStringList list;
        QJsonArray j_arr = j_attr.toArray(QJsonArray());
        for (int i = 0; i < j_arr.size(); i++)
            list.append(j_arr[i].toString());
        var = list;
    }

    else if (input_type == AttrInputType::DateTime)
    {
        var = QDateTime::fromString(j_attr.toString(), FB_NGFP_DATETIME_FORMAT_DT);
    }

    else if (input_type == AttrInputType::Enum)
    {
        var = j_attr.toInt();
    }

    else if (input_type == AttrInputType::DoubleItems)
    {
        DoubleItemsValue value;
        QJsonArray j_arr = j_attr.toArray(QJsonArray());
        value.def_index = -1;
        for (int i = 0; i < j_arr.size(); i++)
        {
            QJsonValue j_arr_item = j_arr[i];
            value.inners.append(j_arr_item.toObject()["name"].toString());
            value.outers.append(j_arr_item.toObject()["alias"].toString());
            if (!j_arr_item.toObject().value("default").isUndefined())
                value.def_index = i;
        }
        var = QVariant::fromValue<DoubleItemsValue>(value);
    }

    else if (input_type == AttrInputType::TripleItems)
    {
        TripleItemsValue value;
        QJsonArray j_arr = j_attr.toArray(QJsonArray());
        value.def_index = -1;
        for (int i = 0; i < j_arr.size(); i++)
        {
            QJsonValue j_arr_item = j_arr[i];
            value.inners.append(j_arr_item.toObject()["name"].toString());
            value.outers_left.append(j_arr_item.toObject()["alias"].toString());
            value.outers_right.append(j_arr_item.toObject()["alias2"].toString());
            if (!j_arr_item.toObject().value("default").isUndefined())
                value.def_index = i;
        }
        var = QVariant::fromValue<TripleItemsValue>(value);
    }

    else if (input_type == AttrInputType::DepDoubleItems)
    {
        DepDoubleItemsValue value;
        QJsonArray j_arr = j_attr.toArray(QJsonArray());
        value.main.def_index = -1;
        for (int i = 0; i < j_arr.size(); i++)
        {
            QJsonValue j_arr_item = j_arr[i];
            value.main.inners.append(j_arr_item.toObject()["name"].toString());
            value.main.outers.append(j_arr_item.toObject()["alias"].toString());
            if (!j_arr_item.toObject().value("default").isUndefined())
                value.main.def_index = i;
            value.deps.append(DoubleItemsValue());
            QJsonValue j_arr2_val = j_arr_item.toObject()["values"];
            QJsonArray j_arr2 = j_arr2_val.toArray(QJsonArray());
            if (!(j_arr2.size() == 1 &&
                j_arr2[0].toObject()["name"].toString() == "-1" &&
                j_arr2[0].toObject()["alias"].toString() == "--"))
            {
                value.deps.last().def_index = -1;
                for (int i2 = 0; i2 < j_arr2.size(); i2++)
                {
                    QJsonValue j_arr_item2 = j_arr2[i2];
                    value.deps.last().inners.append(j_arr_item2.toObject()["name"].toString());
                    value.deps.last().outers.append(j_arr_item2.toObject()["alias"].toString());
                    if (!j_arr_item2.toObject().value("default").isUndefined())
                        value.deps.last().def_index = i2;
                }
            }
        }
        var = QVariant::fromValue<DepDoubleItemsValue>(value);
    }

    attr->setValueAsVar(var);
}
Exemplo n.º 8
0
//------------------------------------------------------------------------------
// Name: toJson
//------------------------------------------------------------------------------
QString QJsonDocument::toJson(const QJsonValue &v, JsonFormat format) const {

	QString b;
	QTextStream ss(&b, QIODevice::WriteOnly | QIODevice::Text);

	switch(v.type()) {
	case QJsonValue::Null:
		ss << "null";
		break;
	case QJsonValue::Bool:
		ss << (v.toBool() ? "true" : "false");
		break;
	case QJsonValue::Double:
		ss << v.toDouble();
		break;
	case QJsonValue::String:
		ss << '"' << escapeString(v.toString()) << '"';
		break;
	case QJsonValue::Array:
		{
			const QJsonArray a = v.toArray();
			ss << "[";
			if(!a.empty()) {
				QJsonArray::const_iterator it = a.begin();
				QJsonArray::const_iterator e  = a.end();

				ss << toJson(*it++, format);

				for(;it != e; ++it) {
					ss << ',';
					ss << toJson(*it, format);
				}
			}
			ss << "]";
		}
		break;
	case QJsonValue::Object:
		{
			const QJsonObject o = v.toObject();
			ss << "{";
			if(!o.empty()) {
				QJsonObject::const_iterator it = o.begin();
				QJsonObject::const_iterator e  = o.end();

				ss << '"' << escapeString(it.key()) << "\": " << toJson(it.value(), format);
				++it;
				for(;it != e; ++it) {
					ss << ',';
					ss << '"' << escapeString(it.key()) << "\": " << toJson(it.value(), format);
				}
			}
			ss  << "}";
		}
		break;
	case QJsonValue::Undefined:
		Q_ASSERT(0);
		break;
	}

	return b;
}
Exemplo n.º 9
0
void SocketIOClient::parseMessage(QString message)
{
    //qDebug() << "SocketIOClient::parseMessage" << message;
    QRegExp regExp("^([^:]+):([0-9]+)?(\\+)?:([^:]+)?:?([\\s\\S]*)?$", Qt::CaseInsensitive, QRegExp::RegExp2);
    if (regExp.indexIn(message) != -1)
    {
        QStringList captured = regExp.capturedTexts();
        //qDebug() << "Found:" << regExp.capturedTexts();
        int messageType = captured.at(1).toInt();
        int messageId = captured.at(2).toInt();
        bool mustAck = (messageId != 0);
        bool autoAck = mustAck && captured.at(3).isEmpty();
        QString endpoint = captured.at(4);
        QString data = captured.at(5);
        /*qDebug() << "MessageType:" << messageType << "MessageId:" << messageId <<
                    "autoAck:" << autoAck << "mustAck:" << mustAck << "endpoint:" << endpoint <<
                    "data:" << data;*/

        if (autoAck)
        {
            acknowledge(messageId);
        }

        switch(messageType)
        {
            case 0:	//disconnect
            {
                Q_EMIT(disconnected(endpoint));
                break;
            }
            case 1: //connect
            {
                m_pHeartBeatTimer->start();
                Q_EMIT(connected(endpoint));
                break;
            }
            case 2:	//heartbeat
            {
                Q_EMIT(heartbeatReceived());
                break;
            }
            case 3:	//message
            {
                Q_EMIT(messageReceived(data));
                break;
            }
            case 4:	//json message
            {
                qDebug() << "JSON message received:" << data;
                break;
            }
            case 5: //event
            {
                QJsonParseError parseError;
                //qDebug() << "Event received:" << data;
                QJsonDocument document = QJsonDocument::fromJson(QByteArray(data.toLatin1()), &parseError);
                if (parseError.error != QJsonParseError::NoError)
                {
                    qDebug() << parseError.errorString();
                }
                else
                {
                    if (document.isObject())
                    {
                        QJsonObject object = document.object();
                        QJsonValue value = object["name"];
                        if (!value.isUndefined())
                        {
                            QString message = value.toString();
                            //QVariantList arguments;
                            QJsonArray arguments;
                            QJsonValue argsValue = object["args"];
                            if (!argsValue.isUndefined() && !argsValue.isNull())
                            {
                                if (argsValue.isArray())
                                {
                                    //QJsonArray argsArray = argsValue.toArray();
                                    //arguments = argsArray.toVariantList();
                                    arguments = argsValue.toArray();
                                    //qDebug() << "Received arguments" << argsValue;
                                    /*Q_FOREACH(QJsonValue val, argsArray)
                                    {
                                        arguments << val.toVariant();
                                    }*/
                                }
                                else
                                {
                                    qDebug() << "Args argument is not an array";
                                    return;
                                }
                            }
                            Q_EMIT(eventReceived(message, arguments));
                        }
                        else
                        {
                            qDebug() << "Invalid event received: no name";
                        }
                    }
                }
                break;
            }
            case 6:	//ack
            {
                QRegExp regExp("^([0-9]+)(\\+)?(.*)$", Qt::CaseInsensitive, QRegExp::RegExp2);
                if (regExp.indexIn(data) != -1)
                {
                    QJsonParseError parseError;
                    //QVariantList arguments;
                    QJsonArray arguments;
                    int messageId = regExp.cap(1).toInt();
                    QString argumentsValue = regExp.cap(3);
                    if (!argumentsValue.isEmpty())
                    {
                        QJsonDocument doc = QJsonDocument::fromJson(argumentsValue.toLatin1(), &parseError);
                        if (parseError.error != QJsonParseError::NoError)
                        {
                            qDebug() << "JSONParseError:" << parseError.errorString();
                            return;
                        }
                        else
                        {
                            if (doc.isArray())
                            {
                                //arguments = doc.array().toVariantList();
                                arguments = doc.array();
                            }
                            else
                            {
                                qDebug() << "Error: data of event is not an array";
                                return;
                            }
                        }
                    }
                    Q_EMIT(ackReceived(messageId, arguments));
                }
                break;
            }
            case 7:	//error
            {
                QStringList pieces = data.split("+");
                QString reason = pieces[0];
                QString advice;
                if (pieces.length() == 2)
                {
                    advice = pieces[1];
                }
                Q_EMIT(errorReceived(reason, advice));
                break;
            }
            case 8:	//noop
            {
                qDebug() << "Noop received" << data;
                break;
            }
        }
    }
}
Exemplo n.º 10
0
QColor Serializer::toColor(const QJsonValue& array)
{
    QJsonArray a = array.toArray();
    return QColor(a[0].toInt(), a[1].toInt(), a[2].toInt());
}
Exemplo n.º 11
0
static bool matches(const QJsonObject &object,
                    const QString &osName,
                    const QVersionNumber &kernelVersion,
                    const QOpenGLConfig::Gpu &gpu)
{
    const OsTypeTerm os = OsTypeTerm::fromJson(object.value(osKey()));
    if (!os.isNull() && !os.matches(osName, kernelVersion))
        return false;

    const QJsonValue exceptionsV = object.value(exceptionsKey());
    if (exceptionsV.isArray()) {
        const QJsonArray exceptionsA = exceptionsV.toArray();
        for (JsonArrayConstIt it = exceptionsA.constBegin(), cend = exceptionsA.constEnd(); it != cend; ++it) {
            if (matches(it->toObject(), osName, kernelVersion, gpu))
                return false;
        }
    }

    const QJsonValue vendorV = object.value(vendorIdKey());
    if (vendorV.isString()) {
        if (gpu.vendorId != vendorV.toString().toUInt(Q_NULLPTR, /* base */ 0))
            return false;
    } else {
        if (object.contains(glVendorKey())) {
            const QByteArray glVendorV = object.value(glVendorKey()).toString().toUtf8();
            if (!gpu.glVendor.contains(glVendorV))
                return false;
        }
    }

    if (gpu.deviceId) {
        const QJsonValue deviceIdV = object.value(deviceIdKey());
        switch (deviceIdV.type()) {
        case QJsonValue::Array:
            if (!contains(deviceIdV.toArray(), gpu.deviceId))
                return false;
            break;
        case QJsonValue::Undefined:
        case QJsonValue::Null:
            break;
        default:
            qWarning().noquote()
                << msgSyntaxWarning(object,
                                    QLatin1String("Device ID must be of type array."));
        }
    }
    if (!gpu.driverVersion.isNull()) {
        const QJsonValue driverVersionV = object.value(driverVersionKey());
        switch (driverVersionV.type()) {
        case QJsonValue::Object:
            if (!VersionTerm::fromJson(driverVersionV).matches(gpu.driverVersion))
                return false;
            break;
        case QJsonValue::Undefined:
        case QJsonValue::Null:
            break;
        default:
            qWarning().noquote()
                << msgSyntaxWarning(object,
                                    QLatin1String("Driver version must be of type object."));
        }
    }
    return true;
}