3 #include <QJsonDocument> 6 ExecRPCCommand::ExecRPCCommand(
const QString &command,
const QStringList &mandatory,
const QStringList &optional,
const QMap<QString, QString>& translations, QObject *parent)
15 bool ExecRPCCommand::exec(
const QMap<QString, QString> ¶ms, QVariant &result, QString &resultJson, QString &errorMessage)
17 QStringList commandLine;
21 QStringList mandatoryNotPresent;
25 if(!params.contains(key))
31 commandLine.append(params[key]);
34 if(mandatoryNotPresent.count() > 0)
36 errorMessage = tr(
"Mandatory fields are not present:\n%1").arg(mandatoryNotPresent.join(
", "));
41 bool haveOptional =
false;
42 int optionalParamsAt = commandLine.size();
43 QStringList optionalNotPresent;
47 if(params.contains(key))
49 if(!haveOptional) haveOptional =
true;
50 commandLine.insert(optionalParamsAt, params[key]);
60 if(optionalNotPresent.count() > 0)
62 errorMessage = tr(
"Optional fields are not present:\n%1").arg(optionalNotPresent.join(
", "));
69 std::string strResult;
70 std::string strCommand = commandLine.join(
' ').toStdString();
73 resultJson = strResult.c_str();
74 QJsonDocument doc = QJsonDocument::fromJson(strResult.c_str());
75 result = doc.toVariant();
80 errorMessage = tr(
"Parse error: unbalanced ' or \"");
88 std::string message =
find_value(objError,
"message").get_str();
89 errorMessage = QString::fromStdString(message) +
" (code " + QString::number(code) +
")";
91 catch (
const std::runtime_error&)
93 errorMessage = QString::fromStdString(objError.
write());
96 catch (
const std::exception&
e)
98 errorMessage = QString(
"Error: ") + QString::fromStdString(e.what());
106 QString _paramValue = paramValue.trimmed();
107 if(!(_paramValue.isNull() || _paramValue.isEmpty())) params[paramName] = _paramValue;
ExecRPCCommand(const QString &command, const QStringList &mandatory, const QStringList &optional, const QMap< QString, QString > &translations, QObject *parent=0)
ExecRPCCommand Constructor.
static bool RPCExecuteCommandLine(std::string &strResult, const std::string &strCommand, std::string *const pstrFilteredOut=nullptr)
bool exec(const QMap< QString, QString > ¶ms, QVariant &result, QString &resultJson, QString &errorMessage)
exec Execute the RPC command
QMap< QString, QString > m_translations
const Object_type::value_type::Value_type & find_value(const Object_type &obj, const String_type &name)
static void appendParam(QMap< QString, QString > ¶ms, const QString ¶mName, const QString ¶mValue)
appendParam Append paramether to the list
QStringList m_mandatoryParams
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
QStringList m_optionalParams