示例#1
0
CSMessage CSClientManager::parseStringMessage(QString msg, QWebSocket *client) {
    qDebug() << "CSClientManager::parseStringMessage";
    CSMessage csmsg;
    if ( !msg.isEmpty() ) {
        QJsonDocument jdoc(QJsonDocument::fromJson(msg.toUtf8()));
        if ( !jdoc.isEmpty() ) {
            QJsonObject jobj = jdoc.object();
            QString cmd = jobj["c"].toString();
            if ( cmd==QString("REG") ) {
                qDebug() << "REG cmd";
                QString login = jobj["l"].toString();
                QString mail = jobj["m"].toString();
                QString pwd = jobj["p"].toString();
                CSAccount *acc = findAccountByLogin(login);
                QJsonObject joAnswer;
                joAnswer["c"]=QString("REG");
                if ( acc && acc->checkPassword(pwd) && acc->getEMail()==mail ) {
                    if ( m_accounts[client]==0 ) {
                        m_accounts[client] = acc;
                        joAnswer["res"]=QString("OK");
                        QList<CSChannel*> chs = acc->getAllChannels();
                        foreach(CSChannel* ch,chs) {
                            int index = m_channels.key(ch,0);
                            if ( index!=0 ) {
                                m_channelClients[index].append(client);
                            }
                        }
                    } else {
示例#2
0
/**
 * @brief Client::parseJSON
 * @param socket the sender
 * @param data the json
 */
void Client::parseJSON(QSslSocket *socket, const QString &data)
{
    Q_UNUSED(socket);
    QByteArray dataIn;
    dataIn.append(data);

    QJsonDocument jdoc(QJsonDocument::fromJson(dataIn));
    QJsonObject obj = jdoc.object();
    if(obj.keys().isEmpty())
    {
        QMessageBox::critical(0, tr("Error"), tr("The JSON is not correct"));
        return;
    }

    //If we receive the address client from the server
    if(!obj["RORI"].toObject().isEmpty() && state == 2)
    {
        QJsonObject RORIObj = obj["RORI"].toObject();
        QString ip = RORIObj["ip"].toString();
        QString port = RORIObj["port"].toString();
        _RORISecret = RORIObj["secret"].toString();
        if(ip.isEmpty() || port.isEmpty())
        {
            QMessageBox::critical(0, tr("Error"), tr("RORI ERROR: ip or port NULL"));
            return;
        }
        socket->close();
        state = 3;
        socket->abort();
        socket->connectToHostEncrypted(ip,port.toInt());
        _connectionInt->setStatus("Connecting to RORI...");
        connect(socket, SIGNAL(connected()), this, SLOT(connected()));

    }
    if(!obj["secret"].toString().isEmpty() && state == 4)
    {
        if(obj["secret"].toString() == _RORISecret)
        {
            state = 5;
            _connectionInt->setStatus("Connected to RORI");
        }
        else
        {
            _connectionInt->setStatus("RORI answer, but not the good secret.");
        }

    }
    if(!obj["message"].toString().isEmpty() && state == 5)
    {
        _connectedInt->addRORIMessage(obj["message"].toString());
    }
    if(!obj["action"].toString().isEmpty() && state == 5)
    {
        //TODO beautilful execution
        QProcess::startDetached(obj["action"].toString());
    }
}
示例#3
0
    /**
     * @brief Database::save
     * @return
     */
    bool Database::save() const
    {
        if (!QDir(m_Path).exists())
            if (!QDir().mkpath(m_Path))
                return false;

        QFile f(makeFullPath());
        if (f.open(QIODevice::WriteOnly)) {
            QJsonDocument jdoc(toJson());
            QTextStream stream(&f);
            stream << jdoc.toJson();
            return true;
        }

        return false;
    }
示例#4
0
    /**
     * @brief Database::load
     * @param errorList
     */
    void Database::load(ErrorList &errorList)
    {
        QFile f(makeFullPath());
        if (f.open(QIODevice::ReadOnly)) {
            QJsonParseError errorMessage;
            QJsonDocument jdoc(QJsonDocument::fromJson(f.readAll(), &errorMessage));

            if (errorMessage.error == QJsonParseError::NoError) {
                fromJson(jdoc.object(), errorList);
            } else {
                errorList << errorMessage.errorString();
            }
        } else {
            errorList << QObject::tr("Can't load database: %1.").arg(f.fileName());
        }

        m_Valid = errorList.isEmpty();
    }
示例#5
0
Config::Config(QObject *parent) : QObject(parent)
{
    QFile config("entry.cfg");
    if(config.exists() && config.open(QIODevice::ReadOnly))
    {
        QString dataIn = config.readAll();
        QByteArray jsonConfig;
        jsonConfig.append(dataIn);
        config.close();
        QJsonDocument jdoc(QJsonDocument::fromJson(jsonConfig));
        _config = jdoc.object();
        QTextStream qout(stdout);
        if(_config.keys().isEmpty())
        {
            qout << tr("The config file is not correct\n");
            return;
        }
    }
}
示例#6
0
文件: json.cpp 项目: tujiaw/QtWXHot
 QString toString(const QVariantMap &val)
 {
     QJsonObject jobj = QJsonObject::fromVariantMap(val);
     QJsonDocument jdoc(jobj);
     return QString(jdoc.toJson(QJsonDocument::Compact));
 }