diff --git a/linphone-desktop/src/components/contact/VcardModel.cpp b/linphone-desktop/src/components/contact/VcardModel.cpp index 4028a9fb8..d36c15baa 100644 --- a/linphone-desktop/src/components/contact/VcardModel.cpp +++ b/linphone-desktop/src/components/contact/VcardModel.cpp @@ -71,7 +71,7 @@ inline shared_ptr findBelcardPhoto (const shared_ptr &belcard) { +inline void removeBelcardPhoto (const shared_ptr &belcard, bool cleanPathsOnly = false) { list > photos; for (const auto photo : belcard->getPhotos()) { if (isLinphoneDesktopPhoto(photo)) @@ -85,8 +85,13 @@ inline void removeBelcardPhoto (const shared_ptr &belcard) { ) ); - if (!QFile::remove(imagePath)) - qWarning() << QStringLiteral("Unable to remove `%1`.").arg(imagePath); + if (!cleanPathsOnly) { + if (!QFile::remove(imagePath)) + qWarning() << QStringLiteral("Unable to remove `%1`.").arg(imagePath); + else + qInfo() << QStringLiteral("Remove `%1`.").arg(imagePath); + } + belcard->removePhoto(photo); } } @@ -101,24 +106,8 @@ VcardModel::VcardModel (shared_ptr vcard) { VcardModel::~VcardModel () { if (!mIsReadOnly) { qInfo() << QStringLiteral("Destroy detached vcard:") << this; - - // If it's a detached Vcard and if necessary the linked photo must be destroyed from fs. - if (mAvatarIsReadOnly) - return; - - shared_ptr 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); + if (!mAvatarIsReadOnly) + removeBelcardPhoto(mVcard->getVcard()); } else qInfo() << QStringLiteral("Destroy attached vcard:") << this; } @@ -143,48 +132,47 @@ bool VcardModel::setAvatar (const QString &path) { CHECK_VCARD_IS_WRITABLE(this); shared_ptr belcard = mVcard->getVcard(); - - // Remove avatar if path is empty. - if (path.isEmpty()) { - removeBelcardPhoto(belcard); - emit vcardUpdated(); - - return true; - } + QString fileId; + QFile file; // 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) - return false; + if (!file.exists() || QImageReader::imageFormat(path).size() == 0) + return false; - QFileInfo info(file); - QString uuid = QUuid::createUuid().toString(); - QString fileId = QStringLiteral("%1.%2") - .arg(uuid.mid(1, uuid.length() - 2)) // Remove `{}`. - .arg(info.suffix()); + QFileInfo info(file); + QString uuid = QUuid::createUuid().toString(); + fileId = QStringLiteral("%1.%2") + .arg(uuid.mid(1, uuid.length() - 2)) // Remove `{}`. + .arg(info.suffix()); - QString dest = ::Utils::linphoneStringToQString(Paths::getAvatarsDirPath()) + fileId; + QString dest = ::Utils::linphoneStringToQString(Paths::getAvatarsDirPath()) + fileId; - if (!file.copy(dest)) - return false; + if (!file.copy(dest)) + 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. - if (!mAvatarIsReadOnly) - removeBelcardPhoto(belcard); + removeBelcardPhoto(belcard, mAvatarIsReadOnly); mAvatarIsReadOnly = false; // 3. Update new photo. - shared_ptr photo = belcard::BelCardGeneric::create(); - photo->setValue(VCARD_SCHEME + ::Utils::qStringToLinphoneString(fileId)); + if (!path.isEmpty()) { + shared_ptr photo = belcard::BelCardGeneric::create(); + photo->setValue(VCARD_SCHEME + ::Utils::qStringToLinphoneString(fileId)); + + if (!belcard->addPhoto(photo)) { + file.remove(); + return false; + } + } emit vcardUpdated(); - if (!belcard->addPhoto(photo)) - return false; - return true; } diff --git a/linphone-desktop/ui/views/App/Main/ContactEdit.js b/linphone-desktop/ui/views/App/Main/ContactEdit.js index 86341b195..c04406e75 100644 --- a/linphone-desktop/ui/views/App/Main/ContactEdit.js +++ b/linphone-desktop/ui/views/App/Main/ContactEdit.js @@ -12,8 +12,15 @@ function handleContactUpdated () { var contact = contactEdit._contact if (!contactEdit._edition) { - // Edition ended. - handleVcardChanged(contact.vcard) + var vcard = 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 { // Edition not ended, the contact was updated in other place. // Update fields with new data.