Commit 8797a2e0 authored by Ronan Abhamon's avatar Ronan Abhamon

fix(src/components/contact/VcardModel): handle correctly the clean algorithm of avatars

parent c5ad9251
...@@ -71,7 +71,7 @@ inline shared_ptr<belcard::BelCardPhoto> findBelcardPhoto (const shared_ptr<belc ...@@ -71,7 +71,7 @@ inline shared_ptr<belcard::BelCardPhoto> findBelcardPhoto (const shared_ptr<belc
return nullptr; return nullptr;
} }
inline void removeBelcardPhoto (const shared_ptr<belcard::BelCard> &belcard) { inline void removeBelcardPhoto (const shared_ptr<belcard::BelCard> &belcard, bool cleanPathsOnly = false) {
list<shared_ptr<belcard::BelCardPhoto> > photos; list<shared_ptr<belcard::BelCardPhoto> > photos;
for (const auto photo : belcard->getPhotos()) { for (const auto photo : belcard->getPhotos()) {
if (isLinphoneDesktopPhoto(photo)) if (isLinphoneDesktopPhoto(photo))
...@@ -85,8 +85,13 @@ inline void removeBelcardPhoto (const shared_ptr<belcard::BelCard> &belcard) { ...@@ -85,8 +85,13 @@ inline void removeBelcardPhoto (const shared_ptr<belcard::BelCard> &belcard) {
) )
); );
if (!QFile::remove(imagePath)) if (!cleanPathsOnly) {
qWarning() << QStringLiteral("Unable to remove `%1`.").arg(imagePath); if (!QFile::remove(imagePath))
qWarning() << QStringLiteral("Unable to remove `%1`.").arg(imagePath);
else
qInfo() << QStringLiteral("Remove `%1`.").arg(imagePath);
}
belcard->removePhoto(photo); belcard->removePhoto(photo);
} }
} }
...@@ -101,24 +106,8 @@ VcardModel::VcardModel (shared_ptr<linphone::Vcard> vcard) { ...@@ -101,24 +106,8 @@ VcardModel::VcardModel (shared_ptr<linphone::Vcard> vcard) {
VcardModel::~VcardModel () { VcardModel::~VcardModel () {
if (!mIsReadOnly) { if (!mIsReadOnly) {
qInfo() << QStringLiteral("Destroy detached vcard:") << this; qInfo() << QStringLiteral("Destroy detached vcard:") << this;
if (!mAvatarIsReadOnly)
// If it's a detached Vcard and if necessary the linked photo must be destroyed from fs. removeBelcardPhoto(mVcard->getVcard());
if (mAvatarIsReadOnly)
return;
shared_ptr<belcard::BelCardPhoto> photo(findBelcardPhoto(mVcard->getVcard()));
if (!photo)
return;
QString imagePath(
::Utils::linphoneStringToQString(
Paths::getAvatarsDirPath() +
photo->getValue().substr(sizeof(VCARD_SCHEME) - 1)
)
);
if (!QFile::remove(imagePath))
qWarning() << QStringLiteral("Unable to remove `%1`.").arg(imagePath);
} else } else
qInfo() << QStringLiteral("Destroy attached vcard:") << this; qInfo() << QStringLiteral("Destroy attached vcard:") << this;
} }
...@@ -143,48 +132,47 @@ bool VcardModel::setAvatar (const QString &path) { ...@@ -143,48 +132,47 @@ bool VcardModel::setAvatar (const QString &path) {
CHECK_VCARD_IS_WRITABLE(this); CHECK_VCARD_IS_WRITABLE(this);
shared_ptr<belcard::BelCard> belcard = mVcard->getVcard(); shared_ptr<belcard::BelCard> belcard = mVcard->getVcard();
QString fileId;
// Remove avatar if path is empty. QFile file;
if (path.isEmpty()) {
removeBelcardPhoto(belcard);
emit vcardUpdated();
return true;
}
// 1. Try to copy photo in avatars folder. // 1. Try to copy photo in avatars folder.
QFile file(path); if (!path.isEmpty()) {
file.setFileName(path);
if (!file.exists() || QImageReader::imageFormat(path).size() == 0) if (!file.exists() || QImageReader::imageFormat(path).size() == 0)
return false; return false;
QFileInfo info(file); QFileInfo info(file);
QString uuid = QUuid::createUuid().toString(); QString uuid = QUuid::createUuid().toString();
QString fileId = QStringLiteral("%1.%2") fileId = QStringLiteral("%1.%2")
.arg(uuid.mid(1, uuid.length() - 2)) // Remove `{}`. .arg(uuid.mid(1, uuid.length() - 2)) // Remove `{}`.
.arg(info.suffix()); .arg(info.suffix());
QString dest = ::Utils::linphoneStringToQString(Paths::getAvatarsDirPath()) + fileId; QString dest = ::Utils::linphoneStringToQString(Paths::getAvatarsDirPath()) + fileId;
if (!file.copy(dest)) if (!file.copy(dest))
return false; return false;
qInfo() << QStringLiteral("Update avatar of `%1`. (path=%2)").arg(getUsername()).arg(dest); qInfo() << QStringLiteral("Update avatar of `%1`. (path=%2)").arg(getUsername()).arg(dest);
}
// 2. Remove oldest photo. // 2. Remove oldest photo.
if (!mAvatarIsReadOnly) removeBelcardPhoto(belcard, mAvatarIsReadOnly);
removeBelcardPhoto(belcard);
mAvatarIsReadOnly = false; mAvatarIsReadOnly = false;
// 3. Update new photo. // 3. Update new photo.
shared_ptr<belcard::BelCardPhoto> photo = belcard::BelCardGeneric::create<belcard::BelCardPhoto>(); if (!path.isEmpty()) {
photo->setValue(VCARD_SCHEME + ::Utils::qStringToLinphoneString(fileId)); shared_ptr<belcard::BelCardPhoto> photo = belcard::BelCardGeneric::create<belcard::BelCardPhoto>();
photo->setValue(VCARD_SCHEME + ::Utils::qStringToLinphoneString(fileId));
if (!belcard->addPhoto(photo)) {
file.remove();
return false;
}
}
emit vcardUpdated(); emit vcardUpdated();
if (!belcard->addPhoto(photo))
return false;
return true; return true;
} }
......
...@@ -12,8 +12,15 @@ function handleContactUpdated () { ...@@ -12,8 +12,15 @@ function handleContactUpdated () {
var contact = contactEdit._contact var contact = contactEdit._contact
if (!contactEdit._edition) { if (!contactEdit._edition) {
// Edition ended. var vcard = contact.vcard
handleVcardChanged(contact.vcard)
if (contactEdit._vcard !== vcard) {
// Not in edition mode, the contact was updated in other place.
contactEdit._vcard = vcard
} else {
// Edition ended.
handleVcardChanged(contact.vcard)
}
} else { } else {
// Edition not ended, the contact was updated in other place. // Edition not ended, the contact was updated in other place.
// Update fields with new data. // Update fields with new data.
......
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