receiving avatars, generating missing avatars, storing state of avatars, global color palette

This commit is contained in:
Blue 2019-10-15 22:25:40 +03:00
parent c678a790e5
commit 64e33b6139
8 changed files with 375 additions and 38 deletions

View file

@ -34,6 +34,7 @@ Core::Archive::Archive(const QString& p_jid, QObject* parent):
order(),
stats(),
hasAvatar(false),
avatarAutoGenerated(false),
avatarHash(),
avatarType()
{
@ -82,13 +83,36 @@ void Core::Archive::open(const QString& account)
hasAvatar = false;
}
if (hasAvatar) {
avatarHash = getStatStringValue("avatarHash", txn).c_str();
try {
avatarAutoGenerated = getStatBoolValue("avatarAutoGenerated", txn);
} catch (NotFound e) {
avatarAutoGenerated = false;
}
avatarType = getStatStringValue("avatarType", txn).c_str();
if (avatarAutoGenerated) {
avatarHash = "";
} else {
avatarHash = getStatStringValue("avatarHash", txn).c_str();
}
} else {
avatarAutoGenerated = false;
avatarHash = "";
avatarType = "";
}
mdb_txn_abort(txn);
if (hasAvatar) {
QFile ava(path + "/avatar." + avatarType);
if (!ava.exists()) {
bool success = dropAvatar();
if (!success) {
qDebug() << "error opening archive" << jid << "for account" << account
<< ". There is supposed to be avatar but the file doesn't exist, couldn't even drop it, it surely will lead to an error";
}
}
}
opened = true;
}
}
@ -577,6 +601,15 @@ bool Core::Archive::getHasAvatar() const
return hasAvatar;
}
bool Core::Archive::getAutoAvatar() const
{
if (!opened) {
throw Closed("getAutoAvatar", jid.toStdString());
}
return avatarAutoGenerated;
}
QString Core::Archive::getAvatarHash() const
{
if (!opened) {
@ -594,3 +627,103 @@ QString Core::Archive::getAvatarType() const
return avatarType;
}
bool Core::Archive::dropAvatar()
{
MDB_txn *txn;
mdb_txn_begin(environment, NULL, 0, &txn);
bool success = setStatValue("hasAvatar", false, txn);
success = success && setStatValue("avatarAutoGenerated", false, txn);
success = success && setStatValue("avatarHash", "", txn);
success = success && setStatValue("avatarType", "", txn);
if (!success) {
mdb_txn_abort(txn);
return false;
} else {
hasAvatar = false;
avatarAutoGenerated = false;
avatarHash = "";
avatarType = "";
mdb_txn_commit(txn);
return true;
}
}
bool Core::Archive::setAvatar(const QByteArray& data, bool generated)
{
if (!opened) {
throw Closed("setAvatar", jid.toStdString());
}
if (data.size() == 0) {
if (!hasAvatar) {
return false;
} else {
return dropAvatar();
}
} else {
const char* cep;
mdb_env_get_path(environment, &cep);
QString currentPath(cep);
bool needToRemoveOld = false;
QCryptographicHash hash(QCryptographicHash::Sha1);
hash.addData(data);
QString newHash(hash.result());
if (hasAvatar) {
if (!generated && !avatarAutoGenerated && avatarHash == newHash) {
return false;
}
QFile oldAvatar(currentPath + "/avatar." + avatarType);
if (oldAvatar.exists()) {
if (oldAvatar.rename(currentPath + "/avatar." + avatarType + ".bak")) {
needToRemoveOld = true;
} else {
qDebug() << "Can't change avatar: couldn't get rid of the old avatar" << oldAvatar.fileName();
return false;
}
}
}
QMimeDatabase db;
QMimeType type = db.mimeTypeForData(data);
QString ext = type.preferredSuffix();
QFile newAvatar(currentPath + "/avatar." + ext);
if (newAvatar.open(QFile::WriteOnly)) {
newAvatar.write(data);
newAvatar.close();
MDB_txn *txn;
mdb_txn_begin(environment, NULL, 0, &txn);
bool success = setStatValue("hasAvatar", true, txn);
success = success && setStatValue("avatarAutoGenerated", generated, txn);
success = success && setStatValue("avatarHash", newHash.toStdString(), txn);
success = success && setStatValue("avatarType", ext.toStdString(), txn);
if (!success) {
qDebug() << "Can't change avatar: couldn't store changes to database for" << newAvatar.fileName() << "rolling back to the previous state";
if (needToRemoveOld) {
QFile oldAvatar(currentPath + "/avatar." + avatarType + ".bak");
oldAvatar.rename(currentPath + "/avatar." + avatarType);
}
mdb_txn_abort(txn);
return false;
} else {
hasAvatar = true;
avatarAutoGenerated = generated;
avatarHash = newHash;
avatarType = ext;
mdb_txn_commit(txn);
if (needToRemoveOld) {
QFile oldAvatar(currentPath + "/avatar." + avatarType + ".bak");
oldAvatar.remove();
}
return true;
}
} else {
qDebug() << "Can't change avatar: cant open file to write" << newAvatar.fileName() << "rolling back to the previous state";
if (needToRemoveOld) {
QFile oldAvatar(currentPath + "/avatar." + avatarType + ".bak");
oldAvatar.rename(currentPath + "/avatar." + avatarType);
}
return false;
}
}
}