Unverified Commit d0389ff7 authored by Sébastien Blin's avatar Sébastien Blin
Browse files

avatar: improve setProfile behaviours

+ use QFileLock on profiles to avoid any potential problems with
parallel updates
+ Do not update write files if nothing is updated

Change-Id: Iff1e1ac6a0363a0531f67d0e3a40ee99fa67a32d
parent 2641aa57
......@@ -32,6 +32,7 @@
#include <QImage>
#include <QByteArray>
#include <QBuffer>
#include <QLockFile>
#include <QJsonObject>
#include <QJsonDocument>
......@@ -218,14 +219,23 @@ void
setProfile(const QString& accountId, const api::profile::Info& profileInfo, const bool isPeer)
{
auto vcard = vcard::profileToVcard(profileInfo);
QFile file(profileVcardPath(accountId, isPeer ? profileInfo.uri : ""));
auto path = profileVcardPath(accountId, isPeer ? profileInfo.uri : "");
QLockFile lf(path + ".lock");
QFile file(path);
if (!lf.lock()) {
qWarning().noquote() << "Can't lock file for writing: " << file.fileName();
return;
}
if (!file.open(QIODevice::WriteOnly)) {
lf.unlock();
qWarning().noquote() << "Can't open file for writing: " << file.fileName();
return;
}
QTextStream in(&file);
in.setCodec("UTF-8");
in << vcard;
file.close();
lf.unlock();
}
} // namespace vcard
......@@ -1038,7 +1048,11 @@ migrateAccountDb(const QString& accountId,
isRingAccount ? profile::Type::JAMI : profile::Type::SIP};
}
auto accountVcard = profileToVcard(accountProfileInfo, accountId);
auto profileFilePath = accountLocalPath + "profile" + ".vcf";
QDir dir;
if (!dir.exists(accountLocalPath)) {
dir.mkpath(accountLocalPath);
}
auto profileFilePath = accountLocalPath + "profile.vcf";
QFile file(profileFilePath);
if (!file.open(QIODevice::WriteOnly)) {
throw std::runtime_error("Can't open file: " + profileFilePath.toStdString());
......
......@@ -920,11 +920,17 @@ ContactModelPimpl::addToContacts(const QString& contactUri,
// create a vcard if necessary
profile::Info profileInfo {contactUri, {}, displayName, linked.owner.profileInfo.type};
auto contactInfo = storage::buildContactFromProfile(linked.owner.id, contactUri, type);
if (!profileInfo.alias.isEmpty())
auto updateProfile = false;
if (!profileInfo.alias.isEmpty() && contactInfo.profileInfo.alias != profileInfo.alias) {
updateProfile = true;
contactInfo.profileInfo.alias = profileInfo.alias;
if (!profileInfo.avatar.isEmpty())
}
if (!profileInfo.avatar.isEmpty() && contactInfo.profileInfo.avatar != profileInfo.avatar) {
updateProfile = true;
contactInfo.profileInfo.avatar = profileInfo.avatar;
storage::vcard::setProfile(linked.owner.id, contactInfo.profileInfo, true);
}
if (updateProfile)
storage::vcard::setProfile(linked.owner.id, contactInfo.profileInfo, true);
contactInfo.isBanned = banned;
contactInfo.conversationId = conversationId;
......
......@@ -276,6 +276,8 @@ void
NewAccountModel::setAlias(const QString& accountId, const QString& alias)
{
auto& accountInfo = pimpl_->getAccountInfo(accountId);
if (accountInfo.profileInfo.alias == alias)
return;
accountInfo.profileInfo.alias = alias;
authority::storage::createOrUpdateProfile(accountInfo.id, accountInfo.profileInfo);
......@@ -287,6 +289,8 @@ void
NewAccountModel::setAvatar(const QString& accountId, const QString& avatar)
{
auto& accountInfo = pimpl_->getAccountInfo(accountId);
if (accountInfo.profileInfo.avatar == avatar)
return;
accountInfo.profileInfo.avatar = avatar;
authority::storage::createOrUpdateProfile(accountInfo.id, accountInfo.profileInfo);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment