Skip to content
Snippets Groups Projects
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
Branches
No related tags found
No related merge requests found
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include <QImage> #include <QImage>
#include <QByteArray> #include <QByteArray>
#include <QBuffer> #include <QBuffer>
#include <QLockFile>
#include <QJsonObject> #include <QJsonObject>
#include <QJsonDocument> #include <QJsonDocument>
...@@ -218,14 +219,23 @@ void ...@@ -218,14 +219,23 @@ void
setProfile(const QString& accountId, const api::profile::Info& profileInfo, const bool isPeer) setProfile(const QString& accountId, const api::profile::Info& profileInfo, const bool isPeer)
{ {
auto vcard = vcard::profileToVcard(profileInfo); 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)) { if (!file.open(QIODevice::WriteOnly)) {
lf.unlock();
qWarning().noquote() << "Can't open file for writing: " << file.fileName(); qWarning().noquote() << "Can't open file for writing: " << file.fileName();
return; return;
} }
QTextStream in(&file); QTextStream in(&file);
in.setCodec("UTF-8"); in.setCodec("UTF-8");
in << vcard; in << vcard;
file.close();
lf.unlock();
} }
} // namespace vcard } // namespace vcard
...@@ -1038,7 +1048,11 @@ migrateAccountDb(const QString& accountId, ...@@ -1038,7 +1048,11 @@ migrateAccountDb(const QString& accountId,
isRingAccount ? profile::Type::JAMI : profile::Type::SIP}; isRingAccount ? profile::Type::JAMI : profile::Type::SIP};
} }
auto accountVcard = profileToVcard(accountProfileInfo, accountId); 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); QFile file(profileFilePath);
if (!file.open(QIODevice::WriteOnly)) { if (!file.open(QIODevice::WriteOnly)) {
throw std::runtime_error("Can't open file: " + profileFilePath.toStdString()); throw std::runtime_error("Can't open file: " + profileFilePath.toStdString());
......
...@@ -920,11 +920,17 @@ ContactModelPimpl::addToContacts(const QString& contactUri, ...@@ -920,11 +920,17 @@ ContactModelPimpl::addToContacts(const QString& contactUri,
// create a vcard if necessary // create a vcard if necessary
profile::Info profileInfo {contactUri, {}, displayName, linked.owner.profileInfo.type}; profile::Info profileInfo {contactUri, {}, displayName, linked.owner.profileInfo.type};
auto contactInfo = storage::buildContactFromProfile(linked.owner.id, contactUri, 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; 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; 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.isBanned = banned;
contactInfo.conversationId = conversationId; contactInfo.conversationId = conversationId;
......
...@@ -276,6 +276,8 @@ void ...@@ -276,6 +276,8 @@ void
NewAccountModel::setAlias(const QString& accountId, const QString& alias) NewAccountModel::setAlias(const QString& accountId, const QString& alias)
{ {
auto& accountInfo = pimpl_->getAccountInfo(accountId); auto& accountInfo = pimpl_->getAccountInfo(accountId);
if (accountInfo.profileInfo.alias == alias)
return;
accountInfo.profileInfo.alias = alias; accountInfo.profileInfo.alias = alias;
authority::storage::createOrUpdateProfile(accountInfo.id, accountInfo.profileInfo); authority::storage::createOrUpdateProfile(accountInfo.id, accountInfo.profileInfo);
...@@ -287,6 +289,8 @@ void ...@@ -287,6 +289,8 @@ void
NewAccountModel::setAvatar(const QString& accountId, const QString& avatar) NewAccountModel::setAvatar(const QString& accountId, const QString& avatar)
{ {
auto& accountInfo = pimpl_->getAccountInfo(accountId); auto& accountInfo = pimpl_->getAccountInfo(accountId);
if (accountInfo.profileInfo.avatar == avatar)
return;
accountInfo.profileInfo.avatar = avatar; accountInfo.profileInfo.avatar = avatar;
authority::storage::createOrUpdateProfile(accountInfo.id, accountInfo.profileInfo); authority::storage::createOrUpdateProfile(accountInfo.id, accountInfo.profileInfo);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment