Ejemplo n.º 1
0
void ChatServer::broadcastUsers(QList<ChatUser *> users, ChatUser * const exclude)
{
    QJsonArray jsonUsers;
    for(ChatUser* u: users) {
        if (!u) continue;
        QJsonObject jsonUser;
        jsonUser["id"] = u->id();
        jsonUser["name"] = u->name();
        jsonUser["isOnline"] = u->isOnline();
        jsonUser["status"] = u->status();
        jsonUsers.append(QJsonValue(jsonUser));
    }

    QJsonObject jsonContainer;
    jsonContainer["type"] = QString("updateusers");
    jsonContainer["users"] = jsonUsers;

    QJsonDocument jsonDoc = QJsonDocument(jsonContainer);
    QByteArray userData = jsonDoc.toBinaryData();
    qWarning() << "ONLINE USERS" << _chatUsers;
    for (QTcpSocket* c: _chatUsers) {
        // если нет подключения или это подключение логинящегося - пропускаем
        if (!c) continue; // || c == _chatUsers[exclude]
        c->write(userData);
    }
}
Ejemplo n.º 2
0
void ClipboardManager::setProperties(const Properties &properties)
{
    const QJsonDocument document(properties.toJson());

    QMimeData *mimeData = new QMimeData;
    mimeData->setData(QLatin1String(PROPERTIES_MIMETYPE), document.toBinaryData());
    mimeData->setText(QString::fromUtf8(document.toJson()));

    mClipboard->setMimeData(mimeData);
}
Ejemplo n.º 3
0
QVariant Database::addClip(QSqlQuery &q, int resource, int datatype, const QString &data,
                      const QJsonDocument &clipdata)
{
    q.addBindValue(resource);
    q.addBindValue(datatype);
    q.addBindValue(data);
    q.addBindValue(clipdata.toBinaryData());
    q.exec();
    return q.lastInsertId();
}
Ejemplo n.º 4
0
void EntityEditPacketSender::queueEditAvatarEntityMessage(PacketType type,
        EntityTreePointer entityTree,
        EntityItemID entityItemID,
        const EntityItemProperties& properties) {
    if (!_shouldSend) {
        return; // bail early
    }

    if (properties.getOwningAvatarID() != _myAvatar->getID()) {
        return; // don't send updates for someone else's avatarEntity
    }

    assert(properties.getClientOnly());

    // this is an avatar-based entity.  update our avatar-data rather than sending to the entity-server
    assert(_myAvatar);

    if (!entityTree) {
        qCDebug(entities) << "EntityEditPacketSender::queueEditEntityMessage null entityTree.";
        return;
    }
    EntityItemPointer entity = entityTree->findEntityByEntityItemID(entityItemID);
    if (!entity) {
        qCDebug(entities) << "EntityEditPacketSender::queueEditEntityMessage can't find entity.";
        return;
    }

    // the properties that get serialized into the avatar identity packet should be the entire set
    // rather than just the ones being edited.
    EntityItemProperties entityProperties = entity->getProperties();
    entityProperties.merge(properties);

    QScriptValue scriptProperties = EntityItemNonDefaultPropertiesToScriptValue(&_scriptEngine, entityProperties);
    QVariant variantProperties = scriptProperties.toVariant();
    QJsonDocument jsonProperties = QJsonDocument::fromVariant(variantProperties);

    // the ID of the parent/avatar changes from session to session.  use a special UUID to indicate the avatar
    QJsonObject jsonObject = jsonProperties.object();
    if (QUuid(jsonObject["parentID"].toString()) == _myAvatar->getID()) {
        jsonObject["parentID"] = AVATAR_SELF_ID.toString();
    }
    jsonProperties = QJsonDocument(jsonObject);

    QByteArray binaryProperties = jsonProperties.toBinaryData();
    _myAvatar->updateAvatarEntity(entityItemID, binaryProperties);

    entity->setLastBroadcast(usecTimestampNow());
    return;
}
Ejemplo n.º 5
0
bool indexDirectory(const QString& dir, const QString& dest)
{
    QVariantMap vm;
    QVariantMap pluginsVm;
    vm[QStringLiteral("Version")] = QStringLiteral("1.0");
    vm[QStringLiteral("Timestamp")] = QDateTime::currentMSecsSinceEpoch();

    QJsonArray plugins;

    QDirIterator it(dir, *metaDataFiles, QDir::Files, QDirIterator::Subdirectories);
    while (it.hasNext()) {
        it.next();
        const QString path = it.fileInfo().absoluteFilePath();
        QJsonObject obj = KPluginMetaData(path).rawData();
        obj.insert(QStringLiteral("FileName"), path);

        plugins.append(obj);
    }

    // Less than two plugin means it's not worth indexing
    if (plugins.count() < 2) {
        removeIndex(dir);
        return true;
    }

    QString destfile = dest;
    if (!QDir::isAbsolutePath(dest)) {
        destfile = dir + '/' + dest;
    }

    QDir().mkpath(QFileInfo(destfile).dir().absolutePath());
    QFile file(destfile);
    if (!file.open(QIODevice::WriteOnly)) {
        qWarning() << "Failed to open " << destfile;
        return false;
    }

    QJsonDocument jdoc;
    jdoc.setArray(plugins);
//     file.write(jdoc.toJson());
    file.write(jdoc.toBinaryData());
    qWarning() << "Generated " << destfile << " (" << plugins.count() << " plugins)";

    return true;
}
Ejemplo n.º 6
0
void ChatServer::receiveData() {
    QTcpSocket * socket = qobject_cast<QTcpSocket*>(sender());
    QByteArray received = socket->readAll();

    // в received может содержаться несколько сообщений
    // их надо разбить:
    while (received.length()) {
        // сообщение возьмет себе столько, сколько нужно
        QJsonDocument doc = QJsonDocument::fromBinaryData(received);
        if (doc.isNull()) {
            // плохие данные
            emit alert("Unknown message format");
            return;
        }

        QVariantMap data = doc.object().toVariantMap();
        if (!data.contains("type")) {
            emit alert("Message should have 'type' key");
            return;
        }

        // конвертация типа в const char* для рассчета CRC
        auto typeString = data["type"].toString();
        auto typeBuffer = typeString.toUtf8();
        auto typeChars = typeBuffer.constData();

        qWarning() << "get message:" << doc;

        switch (mt(typeChars)) {
            case mt("login"): processLoggingInData(socket, data); break;
            case mt("signin"): processSigningInData(socket, data); break;
            case mt("logout"): processLoggingOutData(socket, data); break;
            case mt("changestatus"): processChangingStatusData(socket, data); break;
            case mt("sendmessage"): processSendingData(socket, data); break;
            case mt("messagereceived"): processMessageReceivedData(socket, data); break;
            default: alert("Unknown message type"); break;
        }
        // сдвиг полученного массива на кол-во считанных данных
        received = received.right(received.length() - doc.toBinaryData().length());
    }
}