void GetManyTestCase::onTextMessageReceived(QString message) { DEBUG(m_testClientId,"Message received"); TRACE(m_testClientId,message); QJsonParseError parseError; QJsonDocument jsonDocument; QJsonObject jsonObject; jsonDocument = QJsonDocument::fromJson(message.toUtf8(),&parseError); jsonObject = jsonDocument.object(); if (parseError.error == QJsonParseError::NoError) { QString actionString =jsonObject["action"].toString(); if (actionString != "get") { WARNING(m_testClientId,"Received incorrect action."); emit finished(false); return; } QString requestId = jsonObject["requestId"].toString(); QJsonObject errorObject = jsonObject["error"].toObject(); if (!errorObject.empty()) { WARNING(m_testClientId,"Get Many failed."); QString errorMessage = errorObject["message"].toString(); DEBUG(m_testClientId,QString("Error! Request ID : %1, Message : %2").arg(requestId, errorMessage)); emit finished(false); return; } QJsonValue receivedValue = jsonObject.value("value"); if(receivedValue.isUndefined()) { WARNING(m_testClientId,"Get response doesn't contain any values."); emit finished(false); return; } else if(!receivedValue.isArray()) { WARNING(m_testClientId,"Get response doesn't contain a value array as it should."); emit finished(false); return; } INFO(m_testClientId,"Successfully got many values."); emit finished(true); } else { emit finished(false); } }
void initWindow::guardar() { QFile saveFile(QStringLiteral("save.json")); if (!saveFile.open(QIODevice::WriteOnly)) { qWarning("Couldn't open save file."); return ; } QJsonObject estructura; write(estructura); if(estructura.empty()) exit(0); QJsonDocument saveDoc(estructura); saveFile.write(saveDoc.toJson()); }
void SetGetTestCase::onTextMessageReceived(QString message) { DEBUG(m_testClientId,"Message received"); TRACE(m_testClientId,message); QJsonParseError parseError; QJsonDocument jsonDocument; QJsonObject jsonObject; jsonDocument = QJsonDocument::fromJson(message.toUtf8(),&parseError); jsonObject = jsonDocument.object(); if (parseError.error == QJsonParseError::NoError) { QString actionString =jsonObject["action"].toString(); if(actionString == "set") { QString requestId = jsonObject["requestId"].toString(); QJsonObject errorObject = jsonObject["error"].toObject(); if (!errorObject.empty()) { WARNING(m_testClientId,"Set Get failed."); QString errorMessage = errorObject["message"].toString(); DEBUG(m_testClientId,QString("Error! Request ID : %1, Message : %2").arg(requestId, errorMessage)); emit finished(false); return; } DEBUG(m_testClientId,"Successfully set value, now getting value."); QString subMess = GetVissTestDataJson::getTestDataString(requesttype::GET, QString::number(m_requestId++)); m_webSocket->sendTextMessage(subMess); } else if(actionString == "get") { QString requestId = jsonObject["requestId"].toString(); QJsonObject errorObject = jsonObject["error"].toObject(); if (!errorObject.empty()) { WARNING(m_testClientId,"Set Get failed."); QString errorMessage = errorObject["message"].toString(); DEBUG(m_testClientId,QString("Error! Request ID : %1, Message : %2").arg(requestId, errorMessage)); emit finished(false); return; } QJsonValue receivedValue = jsonObject.value("value"); if(receivedValue.isUndefined()) { WARNING(m_testClientId,"Get response doesn't contain any value."); emit finished(false); return; } // TODO Add check so that we set the same value as we set. // This would probably cause some errors though.. as two or more tests could do this at the same time // thus set different values and get differing results. INFO(m_testClientId,"Successfully set and got value."); emit finished(true); } else { WARNING(m_testClientId,"Received incorrect action."); emit finished(false); return; } } else { emit finished(false); } }
void TransactionStoreTest::testGetEmpty() { QJsonObject transaction = _store.get("somehash4"); QVERIFY(transaction.empty()); }
void WidgetIOProperties::createIOProperties() { ui->mainLayout->setColumnMinimumWidth(0, 150); ui->optionLayout->setColumnMinimumWidth(0, 150); QString lang = Utils::GetLocale(); QString rsc = QString(":/doc/%1/io_doc.json").arg(lang); QFile f(rsc); if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) { QMessageBox::warning(this, tr("Error"), tr("Failed to load IO documentation from %1").arg(rsc)); return; } QJsonParseError jerr; QJsonDocument jdoc = QJsonDocument::fromJson(f.readAll(), &jerr); if (jerr.error != QJsonParseError::NoError || !jdoc.isObject()) { QMessageBox::warning(this, tr("Error"), tr("Failed to parse JSON IO documentation from %1").arg(rsc)); return; } QString iotype = QString::fromUtf8(params["type"].c_str()); QJsonObject jobj = jdoc.object(); for (auto it = jobj.begin();it != jobj.end();it++) jobj.insert(it.key().toLower(), it.value()); QJsonObject jobjAlias; if (!jobj.contains(iotype)) { //Search in aliases bool aliasfound = false; for (auto it = jobj.constBegin();it != jobj.constEnd();it++) { QJsonObject o = it.value().toObject(); QJsonArray jalias = o["alias"].toArray(); for (int i = 0;i < jalias.size();i++) { if (jalias.at(i).toString() == iotype) { aliasfound = true; jobjAlias = o; } } } if (!aliasfound) { QMessageBox::warning(this, tr("Error"), tr("IO type %1 is not found in %2").arg(iotype).arg(rsc)); return; } } QJsonObject jioobj; if (jobjAlias.isEmpty()) jioobj = jobj[iotype].toObject(); else jioobj = jobjAlias; ui->labelTitle->setText(iotype); ui->labelDesc->setText(jioobj["description"].toString()); int rowMain = 0, rowOption = 0; QJsonArray jparams = jioobj["parameters"].toArray(); for (int i = 0;i < jparams.size();i++) { QJsonObject jparam = jparams[i].toObject(); QGridLayout *layout = jparam["mandatory"].toString() == "true"?ui->mainLayout:ui->optionLayout; int row = jparam["mandatory"].toBool()?rowMain:rowOption; QLabel *title = new QLabel(jparam["name"].toString()); layout->addWidget(title, row, 0); QPushButton *revert = new QPushButton(); revert->setIcon(QIcon(":/img/document-revert.png")); revert->setToolTip(tr("Revert modification")); layout->addWidget(revert, row, 1); hider.hide(revert); QString pvalue; string prop = jparam["name"].toString().toUtf8().constData(); if (params.Exists(prop)) pvalue = QString::fromUtf8(params[prop].c_str()); else pvalue = jparam["default"].toString(); if (jparam["type"].toString() == "string") { QLineEdit *w = new QLineEdit(); w->setEnabled(jparam["readonly"].toString() != "true" && editable); w->setText(pvalue); layout->addWidget(w, row, 2); UiObject uiObject; uiObject.type = UiObjectType::LineEdit; uiObject.lineEdit = w; uiObjectMap[prop] = uiObject; connect(w, &QLineEdit::textChanged, [=]() { updateChangedParam(prop, w->text(), pvalue, title, revert); }); connect(revert, &QPushButton::clicked, [=]() { w->setText(pvalue); }); } else if (jparam["type"].toString() == "bool") { QCheckBox *w = new QCheckBox(); w->setEnabled(jparam["readonly"].toString() != "true" && editable); w->setChecked(pvalue == "true"); layout->addWidget(w, row, 2); UiObject uiObject; uiObject.type = UiObjectType::CheckBox; uiObject.checkBox = w; uiObjectMap[prop] = uiObject; connect(w, &QCheckBox::stateChanged, [=]() { updateChangedParam(prop, w->isChecked()?"true":"false", pvalue, title, revert); }); connect(revert, &QPushButton::clicked, [=]() { w->setChecked(pvalue == "true"); }); } else if (jparam["type"].toString() == "int") { QSpinBox *w = new QSpinBox(); w->setEnabled(jparam["readonly"].toString() != "true" && editable); if (!jparam["min"].toString().isEmpty()) w->setMinimum(jparam["min"].toString().toInt()); else w->setMinimum(-999999999); if (!jparam["max"].toString().isEmpty()) w->setMaximum(jparam["max"].toString().toInt()); else w->setMaximum(999999999); w->setValue(pvalue.toInt()); layout->addWidget(w, row, 2); UiObject uiObject; uiObject.type = UiObjectType::SpinBox; uiObject.spinBox = w; uiObjectMap[prop] = uiObject; connect(w, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [=]() { updateChangedParam(prop, QString("%1").arg(w->value()), pvalue.isEmpty()?"0":pvalue, title, revert); }); connect(revert, &QPushButton::clicked, [=]() { w->setValue(pvalue.toInt()); }); } else if (jparam["type"].toString() == "float") { QDoubleSpinBox *w = new QDoubleSpinBox(); w->setEnabled(jparam["readonly"].toString() != "true" && editable); if (!jparam["min"].toString().isEmpty()) w->setMinimum(jparam["min"].toString().toDouble()); else w->setMinimum(-999999999.0); if (!jparam["max"].toString().isEmpty()) w->setMaximum(jparam["max"].toString().toDouble()); else w->setMaximum(999999999.0); w->setValue(pvalue.toDouble()); layout->addWidget(w, row, 2); w->setDecimals(3); UiObject uiObject; uiObject.type = UiObjectType::DoubleSpinBox; uiObject.doubleSpinBox = w; uiObjectMap[prop] = uiObject; connect(w, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged), [=]() { updateChangedParam(prop, QString("%1").arg(w->value()), pvalue.isEmpty()?"0":pvalue, title, revert); }); connect(revert, &QPushButton::clicked, [=]() { w->setValue(pvalue.toDouble()); }); } else if (jparam["type"].toString() == "list") { QComboBox *w = new QComboBox(); int defIndex = 0; w->setEnabled(jparam["readonly"].toString() != "true" && editable); //fill combobox with values, if no value, set editable to true QJsonObject jvalues = jparam["list_value"].toObject(); if (jvalues.empty()) w->setEditable(true); else { w->setEditable(false); for (auto it = jvalues.begin();it != jvalues.end();it++) { w->addItem(it.value().toString(), it.key()); if (it.key() == pvalue) { defIndex = w->count() - 1; w->setCurrentIndex(defIndex); } } } layout->addWidget(w, row, 2); UiObject uiObject; uiObject.type = UiObjectType::ComboBox; uiObject.comboBox = w; uiObjectMap[prop] = uiObject; if (w->isEditable()) { connect(w, &QComboBox::currentTextChanged, [=]() { updateChangedParam(prop, w->currentText(), pvalue, title, revert); }); } else { connect(w, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [=]() { updateChangedParam(prop, w->currentData().toString(), pvalue, title, revert); }); } connect(revert, &QPushButton::clicked, [=]() { if (w->isEditable()) w->setEditText(pvalue); else w->setCurrentIndex(defIndex); }); } QPushButton *help = new QPushButton(); help->setIcon(QIcon(":/img/icon_unkown.png")); help->setFlat(true); layout->addWidget(help, row, 3); if((i==0)&&(entryHelper != nullptr)) { QPushButton *entryHelperButton = new QPushButton(); entryHelperButton->setIcon(QIcon(":/img/icon_entry_helper.png")); entryHelperButton->setFlat(true); layout->addWidget(entryHelperButton, row, 4); connect(entryHelperButton, &QPushButton::clicked, [=]() { if (entryHelper->exec() == QDialog::Accepted) setValues(entryHelper->getParams()); }); } //avoid copy the QJsonObject in the lambda QString helpInfo = jparam["description"].toString(); connect(help, &QPushButton::clicked, [=]() { if (balloonTip) delete balloonTip; balloonTip = new BalloonTip(QPixmap(":/img/icon_unkown.png"), title->text(), helpInfo, 800, help); balloonTip->setArrowPosition(BalloonTip::TopLeft); balloonTip->move(QCursor::pos()); balloonTip->show(); }); if (jparam["mandatory"].toBool()) rowMain++; else rowOption++; } }
void DiagnosticsDialog::getGithubVersionFinished(QNetworkReply *reply) { if (reply->error()) { // Incase ssl dlls are not present or corrupted; avoid crash ui->checkClientVersionResultLbl->setText("Failed (" + reply->errorString() + ")"); return; } QByteArray data; data = reply->readAll(); std::string newVersionString; QJsonDocument replyJson = QJsonDocument::fromJson(data); QJsonObject obj = replyJson.object(); if (!obj.empty()) { QJsonValue newVersionJVal = obj.value("name"); if (!newVersionJVal.isUndefined()) newVersionString = newVersionJVal.toString().toStdString(); } std::vector<std::string> newVersionStringSplit; boost::algorithm::split(newVersionStringSplit, newVersionString, boost::is_any_of("-")); newVersionString = newVersionStringSplit.at(0); std::string currentVersionString = FormatFullVersion(); std::vector<std::string> currentVersionStringSplit; boost::algorithm::split(currentVersionStringSplit, currentVersionString, boost::is_any_of("-")); currentVersionString = currentVersionStringSplit.at(0); boost::algorithm::split(currentVersionStringSplit, currentVersionString, boost::is_any_of("v")); currentVersionString = currentVersionStringSplit.at(1); std::vector<std::string> currentVersionList; std::vector<std::string> newVersionList; boost::algorithm::split(currentVersionList, currentVersionString, boost::is_any_of(".")); boost::algorithm::split(newVersionList, newVersionString, boost::is_any_of(".")); int iNew; int iCurrent; try { for (unsigned int i=0; i<newVersionList.size(); i++) { iNew = std::stoi(newVersionList.at(i), nullptr, 10); iCurrent = std::stoi(currentVersionList.at(i), nullptr, 10); if (iNew > iCurrent) { ui->checkClientVersionResultLbl->setText("Failed (An update is available, please update to " + QString::fromStdString(newVersionString) + ")"); return; } else ui->checkClientVersionResultLbl->setText("Up to date"); } } catch (std::exception& ex) { ui->checkClientVersionResultLbl->setText("Failed: std exception occured -> " + QString::fromUtf8(ex.what())); } return; }
//------------------------------------------------------------------------------ // 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; }