From 534c5f0ab9c7884469e9adf2f0aa5e6e5388cc68 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Fri, 16 Dec 2016 10:26:27 +0100 Subject: [PATCH] feat(src/components/contact/VcardModel): remove property photo if vcard is detached --- .../images/contact_card_photo_disabled.svg | 16 +++++ tests/resources.qrc | 1 + tests/src/components/contact/VcardModel.cpp | 64 +++++++++++++------ tests/src/components/contact/VcardModel.hpp | 2 +- .../Common/Form/TransparentTextInput.qml | 2 +- .../Styles/Form/TransparentTextInputStyle.qml | 1 + tests/ui/modules/Linphone/Contact/Avatar.qml | 8 +-- tests/ui/views/App/MainWindow/ContactEdit.qml | 5 +- 8 files changed, 71 insertions(+), 28 deletions(-) create mode 100644 tests/assets/images/contact_card_photo_disabled.svg diff --git a/tests/assets/images/contact_card_photo_disabled.svg b/tests/assets/images/contact_card_photo_disabled.svg new file mode 100644 index 000000000..b4e52a62f --- /dev/null +++ b/tests/assets/images/contact_card_photo_disabled.svg @@ -0,0 +1,16 @@ + + + + contact_card_photo_default + Created with Sketch. + + + + + + + + + + + \ No newline at end of file diff --git a/tests/resources.qrc b/tests/resources.qrc index ca2d2825b..147060a40 100644 --- a/tests/resources.qrc +++ b/tests/resources.qrc @@ -40,6 +40,7 @@ assets/images/chevron_red.svg assets/images/chevron_white.svg assets/images/collapse.svg + assets/images/contact_card_photo_disabled.svg assets/images/contact_card_photo_hovered.svg assets/images/contact_card_photo_normal.svg assets/images/contact_card_photo_pressed.svg diff --git a/tests/src/components/contact/VcardModel.cpp b/tests/src/components/contact/VcardModel.cpp index c28c1c7b7..93940881b 100644 --- a/tests/src/components/contact/VcardModel.cpp +++ b/tests/src/components/contact/VcardModel.cpp @@ -4,7 +4,7 @@ #include #include -#include "../../app/AvatarProvider.hpp" +#include "../../app/App.hpp" #include "../../app/Database.hpp" #include "../../utils.hpp" #include "../core/CoreManager.hpp" @@ -27,7 +27,44 @@ inline shared_ptr findBelCardValue (const list > &list, const Q } ); - return *it; + if (it != list.cend()) + return *it; + + return nullptr; +} + +inline shared_ptr findBelCardPhoto (const list > &photos) { + auto it = find_if( + photos.cbegin(), photos.cend(), [](const shared_ptr &photo) { + return !photo->getValue().compare(0, sizeof(VCARD_SCHEME) - 1, VCARD_SCHEME); + } + ); + + if (it != photos.cend()) + return *it; + + return nullptr; +} + +// ----------------------------------------------------------------------------- + +VcardModel::~VcardModel () { + // If it's a detached Vcard, the linked photo must be destroyed from fs. + if (App::getInstance()->getEngine()->objectOwnership(this) != QQmlEngine::CppOwnership) { + shared_ptr photo(findBelCardPhoto(m_vcard->getBelcard()->getPhotos())); + if (!photo) + return; + + QString image_path( + ::Utils::linphoneStringToQString( + Database::getAvatarsPath() + + photo->getValue().substr(sizeof(VCARD_SCHEME) - 1) + ) + ); + + if (!QFile::remove(image_path)) + qWarning() << QStringLiteral("Unable to remove `%1`.").arg(image_path); + } } // ----------------------------------------------------------------------------- @@ -49,19 +86,15 @@ void VcardModel::setUsername (const QString &username) { QString VcardModel::getAvatar () const { // Find desktop avatar. list > photos = m_vcard->getBelcard()->getPhotos(); - auto it = find_if( - photos.cbegin(), photos.cend(), [](const shared_ptr &photo) { - return !photo->getValue().compare(0, sizeof(VCARD_SCHEME) - 1, VCARD_SCHEME); - } - ); + shared_ptr photo = findBelCardPhoto(photos); // No path found. - if (it == photos.cend()) + if (!photo) return ""; // Returns right path. return QStringLiteral("image://%1/%2").arg(AvatarProvider::PROVIDER_ID).arg( - ::Utils::linphoneStringToQString((*it)->getValue().substr(sizeof(VCARD_SCHEME) - 1)) + ::Utils::linphoneStringToQString(photo->getValue().substr(sizeof(VCARD_SCHEME) - 1)) ); } @@ -90,22 +123,17 @@ bool VcardModel::setAvatar (const QString &path) { list > photos = belcard->getPhotos(); // 3. Remove oldest photo. - auto it = find_if( - photos.begin(), photos.end(), [](const shared_ptr &photo) { - return !photo->getValue().compare(0, sizeof(VCARD_SCHEME) - 1, VCARD_SCHEME); - } - ); - - if (it != photos.end()) { + shared_ptr old_photo = findBelCardPhoto(photos); + if (old_photo) { QString image_path( ::Utils::linphoneStringToQString( - Database::getAvatarsPath() + (*it)->getValue().substr(sizeof(VCARD_SCHEME) - 1) + Database::getAvatarsPath() + old_photo->getValue().substr(sizeof(VCARD_SCHEME) - 1) ) ); if (!QFile::remove(image_path)) qWarning() << QStringLiteral("Unable to remove `%1`.").arg(image_path); - belcard->removePhoto(*it); + belcard->removePhoto(old_photo); } // 4. Update. diff --git a/tests/src/components/contact/VcardModel.hpp b/tests/src/components/contact/VcardModel.hpp index 2d85d3398..0fff3db1c 100644 --- a/tests/src/components/contact/VcardModel.hpp +++ b/tests/src/components/contact/VcardModel.hpp @@ -22,7 +22,7 @@ class VcardModel : public QObject { public: VcardModel (std::shared_ptr vcard) : m_vcard(vcard) {} - ~VcardModel () = default; + ~VcardModel (); QString getUsername () const; diff --git a/tests/ui/modules/Common/Form/TransparentTextInput.qml b/tests/ui/modules/Common/Form/TransparentTextInput.qml index 62eefee48..39829ffe7 100644 --- a/tests/ui/modules/Common/Form/TransparentTextInput.qml +++ b/tests/ui/modules/Common/Form/TransparentTextInput.qml @@ -55,7 +55,7 @@ Item { anchors.left: background.right height: background.height icon: 'generic_error' - iconSize: 12 + iconSize: TransparentTextInputStyle.iconSize visible: parent.isInvalid } diff --git a/tests/ui/modules/Common/Styles/Form/TransparentTextInputStyle.qml b/tests/ui/modules/Common/Styles/Form/TransparentTextInputStyle.qml index bcb0490a1..97e3e772f 100644 --- a/tests/ui/modules/Common/Styles/Form/TransparentTextInputStyle.qml +++ b/tests/ui/modules/Common/Styles/Form/TransparentTextInputStyle.qml @@ -7,6 +7,7 @@ import Common 1.0 QtObject { property color backgroundColor: Colors.q + property int iconSize: 12 property int padding: 10 property QtObject textColor: QtObject { diff --git a/tests/ui/modules/Linphone/Contact/Avatar.qml b/tests/ui/modules/Linphone/Contact/Avatar.qml index d88985c51..24f8e7723 100644 --- a/tests/ui/modules/Linphone/Contact/Avatar.qml +++ b/tests/ui/modules/Linphone/Contact/Avatar.qml @@ -3,7 +3,6 @@ import QtQuick 2.7 import Common 1.0 import Linphone 1.0 import Linphone.Styles 1.0 -import Utils 1.0 // ============================================================================= @@ -26,10 +25,9 @@ Item { function _computeInitials () { var result = username.match(_initialsRegex) - Utils.assert( - result != null, - 'Unable to get initials of: `' + username + '`.' - ) + if (!result) { + return '' + } return result[1].charAt(0).toUpperCase() + ( result[2] != null diff --git a/tests/ui/views/App/MainWindow/ContactEdit.qml b/tests/ui/views/App/MainWindow/ContactEdit.qml index 8dcc9ac4d..a7babe117 100644 --- a/tests/ui/views/App/MainWindow/ContactEdit.qml +++ b/tests/ui/views/App/MainWindow/ContactEdit.qml @@ -118,8 +118,6 @@ ColumnLayout { if (_edition && _contact) { _contact.abortEdit() } - - // TODO: Remove photo if contact not created. } // --------------------------------------------------------------------------- @@ -152,6 +150,7 @@ ColumnLayout { spacing: ContactEditStyle.infoBar.spacing ActionButton { + enabled: _edition icon: 'contact_card_photo' iconSize: ContactEditStyle.infoBar.avatarSize @@ -163,7 +162,7 @@ ColumnLayout { anchors.fill: parent image: _vcard.avatar username: _vcard.username - visible: isLoaded() && !parent.hovered + visible: isLoaded() && (!parent.hovered || !_edition) } }