Exemple #1
0
/**
 * @brief Get a contact's avatar from cache, with a specified profile password.
 * @param ownerId Friend ID to load avatar.
 * @param password Profile password to decrypt data.
 * @return Avatar as QByteArray.
 */
QByteArray Profile::loadAvatarData(const QString& ownerId, const QString& password)
{
    QString path = avatarPath(ownerId);
    bool encrypted = !password.isEmpty();

    // If the encrypted avatar isn't found, try loading the unencrypted one for the same ID
    if (!password.isEmpty() && !QFile::exists(path))
    {
        encrypted = false;
        path = avatarPath(ownerId, true);
    }

    QFile file(path);
    if (!file.open(QIODevice::ReadOnly))
    {
        return {};
    }

    QByteArray pic = file.readAll();
    if (encrypted && !pic.isEmpty())
    {
        uint8_t salt[TOX_PASS_SALT_LENGTH];
        tox_get_salt(reinterpret_cast<uint8_t*>(pic.data()), salt);
        auto passkey = core->createPasskey(password, salt);
        pic = core->decryptData(pic, *passkey);
    }

    return pic;
}
Exemple #2
0
/**
 * @brief Save an avatar to cache.
 * @param pic Picture to save.
 * @param ownerId ID of avatar owner.
 */
void Profile::saveAvatar(QByteArray pic, const QString& ownerId)
{
    if (!password.isEmpty() && !pic.isEmpty())
    {
        pic = core->encryptData(pic, passkey);
    }

    QString path = avatarPath(ownerId);
    QDir(Settings::getInstance().getSettingsDirPath()).mkdir("avatars");
    if (pic.isEmpty())
    {
        QFile::remove(path);
    }
    else
    {
        QSaveFile file(path);
        if (!file.open(QIODevice::WriteOnly))
        {
            qWarning() << "Tox avatar " << path << " couldn't be saved";
            return;
        }
        file.write(pic);
        file.commit();
    }
}
Exemple #3
0
/**
 * @brief Removes a cached avatar.
 * @param ownerId Friend ID whose avater to delete.
 */
void Profile::removeAvatar(const QString& ownerId)
{
    QFile::remove(avatarPath(ownerId));
    if (ownerId == core->getSelfId().publicKey)
        core->setAvatar({});
}
Exemple #4
0
void Profile::removeAvatar(const QString &ownerId)
{
    QFile::remove(avatarPath(ownerId));
}