Example #1
0
void NetworkManager::processJson(QString json)
{
    //log("Json received : " + json);

    // Parse main parts of the message (type and body)

    QJsonDocument doc; doc = doc.fromJson(json.toUtf8());
    QString type = doc.object().take("type").toString();
    QJsonValue body = doc.object().take("body");

    // Routing messages by type

    if ( type == "hbAck" ){

        emit hearthbeatReceived(body.toString());
    }
    else if ( type == "setVariable" ){

        QString variable = body.toObject().take("variable").toString();
        QString option = body.toObject().take("option").toString();
        QString json = QJsonDocument( body.toObject().take("value").toObject() ).toJson();

        emit systemVariableChanged(variable, option, json);
    }
    else if ( type == "call" ){

            QString module = body.toObject().take("module").toString();
            QString function = body.toObject().take("function").toString();
            if (function == "")
                function = body.toObject().take("fct").toString();
            QString params = QJsonDocument( body.toObject().take("param").toArray() ).toJson();

            emit callRequest(module, function, params);
    }
    else if ( type == "ssid" ){

        setssid(body.toString());

        setLoggedState(true);
    }
    else if ( type == "login-error" ){

        log("Login error : " + body.toString());
        setLoggedState(false);
    }
    else if ( type == "error"){

        log("Server error : " + body.toString());
    }
    else{

        emit jsonReceived(type, body);
        emit jsonStringReceived(json);
    }
}
void WorkingAboutToStartState::ProcessUnable(const QByteArray &content)
{
    // parsing du json reçu
    QJsonDocument doc;
    QJsonParseError error;
    doc.fromJson(content, &error);
    QJsonObject object = doc.object();
    // vérification de l'intégrité de l'objet JSON
    if(!object.contains("id") ||
       !object.contains("arch") ||
       !object.contains("os"))
    {
        _client->AddMissingPlugin();
        _client->resetCurrentFragment();
        _client->setCurrentStateAfterError("Unable to calculate and incomplete JSON received");
    }
    else if (object.value("id").toString() == _client->GetId().toString())
    {
        const QByteArray * data = PluginManager::getInstance().GetPluginData(
                    object.value("arch").toString(),
                    object.value("os").toString(),
                    _client->GetFragment()->GetBin());
        if(data != NULL)
        {
            _client->send(BIN, *data);
            delete data;
        }
        else
        {
            _client->AddMissingPlugin();
            _client->resetCurrentFragment();
            _client->setCurrentStateAfterError("Unable to calculate and missing binary for client architecture");
        }
        // on ne change pas d'état après avoir envoyé le binaire
    }
    // pas de else ici
}
Example #3
0
void EditorWindow::addControlUI()
{
    QString ApplicationPath = QApplication::applicationDirPath();

    QString selectedFilter;
    QString sourceUiPath = QFileDialog::getOpenFileName(this,
                                tr("QFileDialog::getOpenFileName()"),
                                ApplicationPath,
                                tr("Qt UI Files (*.ui)"),
                                &selectedFilter);
    qDebug() << "Filename: " << sourceUiPath;
    if (sourceUiPath.isEmpty())
    {
        return;
    }

    //Backup directory for UI/Json files
    if (!QDir("Controllers").exists())
    {
        QDir().mkdir("Controllers");
    }

    QFileInfo fileInfo(sourceUiPath);


    QFile sourceUIFile(sourceUiPath);
    if (!sourceUIFile.open(QFile::ReadOnly | QFile::Text))
    {
         QMessageBox::warning(this, tr("UI File"),
                              tr("Cannot read file %1:\n%2.")
                              .arg(sourceUIFile.fileName())
                              .arg(sourceUIFile.errorString()));
         return;
    }

    //Test XML UI File
    QString errorStr;
    int errorLine;
    int errorColumn;
    QDomDocument domDocument;
    if (!domDocument.setContent(&sourceUIFile, true, &errorStr, &errorLine, &errorColumn)) {
        QMessageBox::information(window(), tr("UI File"),
            tr("Parse error at line %1, column %2:\n%3")
            .arg(errorLine)
            .arg(errorColumn)
            .arg(errorStr));
        sourceUIFile.close();
        return;
    }
    QDomElement root = domDocument.documentElement();
    if (root.tagName() != "ui")
    {
        QMessageBox::information(window(), tr("UI File"),
                                 tr("The file is not an UI file."));
        return;
    }
    else
    {
        qDebug() << "UI file ok";
    }

    QFile destinationUIFile(ApplicationPath + "/Controllers/" + fileInfo.fileName());
    if (destinationUIFile.exists())
    {
        QMessageBox msgBox(QMessageBox::Warning, tr("QMessageBox::warning()"), tr("Ui File already exists"), 0, this);
        msgBox.addButton(tr("&Override"), QMessageBox::AcceptRole);
        msgBox.addButton(tr("&Cancel"), QMessageBox::RejectRole);
        if (msgBox.exec() == QMessageBox::AcceptRole)
            sourceUIFile.copy(ApplicationPath + "/Controllers/" + fileInfo.fileName());
    }
    else
    {
        sourceUIFile.copy(ApplicationPath + "/Controllers/" + fileInfo.fileName());
    }
    sourceUIFile.close();
    destinationUIFile.close();

    //Json associated to UI file
    QString sourceJsonPath = fileInfo.absolutePath() + "/" + fileInfo.baseName() + ".json";
    QJsonDocument JsonDocument = QJsonDocument();
    if (QFile(sourceJsonPath).exists())
    {
        QFile sourceJsonFile(sourceJsonPath);
        if (!sourceJsonFile.open(QFile::ReadOnly | QFile::Text))
        {
             QMessageBox::warning(this, tr("Json File"),
                                  tr("Cannot read file %1:\n%2.")
                                  .arg(sourceJsonFile.fileName())
                                  .arg(sourceJsonFile.errorString()));
             return;
        }
        sourceJsonFile.copy(ApplicationPath + "Controllers" + fileInfo.baseName() + ".json");
        JsonDocument.fromJson(sourceJsonFile.readAll());
        sourceJsonFile.close();
    }
    else
    {
        qDebug() << fileInfo.baseName();
        Controllers->appendRoot(fileInfo.baseName());
//        QJsonObject rootObject = QJsonObject();
//        rootObject.insert(fileInfo.baseName() , QJsonValue());
//        JsonDocument.setObject(rootObject);
    }
    ControllerTree->setModel(Controllers);
    Controllers->loadJson(JsonDocument.toJson());

    QUiLoader UILoader;
    QFile controller("Controllers/" + fileInfo.fileName());
    controller.open(QFile::ReadOnly | QFile::Text);
    clearLayout(ControllerUI->layout());
    ControllerUI->layout()->addWidget(UILoader.load(&controller, ControllerUI));
    currentControl = fileInfo.baseName();
    controller.close();
    return;
}