Commit 534c5f0a authored by Ronan's avatar Ronan
Browse files

feat(src/components/contact/VcardModel): remove property photo if vcard is detached

parent 53a8b2f4
<?xml version="1.0" encoding="UTF-8"?>
<svg width="60px" height="60px" viewBox="0 0 60 60" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- Generator: Sketch 41.2 (35397) - http://www.bohemiancoding.com/sketch -->
<title>contact_card_photo_default</title>
<desc>Created with Sketch.</desc>
<defs></defs>
<g id="Symbols" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="contact_card_photo_default">
<g id="Group-7">
<path d="M0,30 C0,46.568 13.4315,60 30,60 C46.568,60 60,46.568 60,30 C60,13.431 46.568,0 30,0 C13.4315,0 0,13.431 0,30 Z" id="AVATAR" fill="#D0D8DE"></path>
<path d="M42.7031449,40.8140504 L18.234171,40.8140504 C17.0106603,40.8140504 16,39.8196379 16,38.615797 L16,23.2282265 C16,22.0243856 17.0106603,21.0300409 18.234171,21.0300409 L21.7980713,21.0300409 C22.3833064,21.0300409 23.074829,20.5589962 23.2343634,19.9831696 L23.5535012,18.9363661 C23.925886,17.837375 25.0961496,17 26.2663444,17 L34.7242185,17 C35.8944133,17 37.064677,17.837375 37.4370618,18.9363661 L37.7561995,19.9831696 C37.915734,20.5589962 38.6072565,21.0300409 39.1924917,21.0300409 L42.7564608,21.0300409 C43.9799026,21.0300409 44.9905629,22.0243856 44.9905629,23.2282265 L44.9905629,38.615797 C44.9905629,39.8196379 43.9799026,40.8140504 42.7031449,40.8140504 L42.7031449,40.8140504 Z M18.234171,22.3383927 C17.7021829,22.3383927 17.2767577,22.7572496 17.2767577,23.2806853 L17.2767577,38.6679846 C17.2767577,39.1913525 17.7021829,39.6102094 18.234171,39.6102094 L42.7031449,39.6102094 C43.235133,39.6102094 43.6608337,39.1913525 43.6608337,38.6679846 L43.6608337,23.2806853 C43.6608337,22.7572496 43.235133,22.3383927 42.7031449,22.3383927 L39.1392447,22.3383927 C37.9690499,22.3383927 36.7987862,21.5010177 36.4264014,20.4020266 L36.1072637,19.3552231 C35.9477292,18.7795998 35.2562067,18.3083518 34.6709715,18.3083518 L26.2130974,18.3083518 C25.6280689,18.3083518 24.9365463,18.7795998 24.7767363,19.3552231 L24.4575986,20.4020266 C24.0852138,21.5010177 22.915019,22.3383927 21.7447553,22.3383927 L18.234171,22.3383927 Z" id="Fill-1" fill="#FFFFFF"></path>
<path d="M30.1811164,37.1313385 C26.2447625,37.1313385 23,33.9911314 23,30.0656693 C23,26.1926659 26.191791,23 30.1811164,23 C34.1707173,23 37.3625083,26.1402071 37.3625083,30.0656693 C37.3625083,33.9911314 34.117677,37.1313385 30.1811164,37.1313385 L30.1811164,37.1313385 Z M30.1811164,24.3608106 C26.9363539,24.3608106 24.3300736,26.9254622 24.3300736,30.118128 C24.3300736,33.3107261 26.9363539,35.8753777 30.1811164,35.8753777 C33.4261544,35.8753777 36.0324347,33.3107261 36.0324347,30.118128 C36.0324347,26.9254622 33.4261544,24.3608106 30.1811164,24.3608106 L30.1811164,24.3608106 Z" id="Fill-2" fill="#FFFFFF"></path>
</g>
</g>
</g>
</svg>
\ No newline at end of file
......@@ -40,6 +40,7 @@
<file>assets/images/chevron_red.svg</file>
<file>assets/images/chevron_white.svg</file>
<file>assets/images/collapse.svg</file>
<file>assets/images/contact_card_photo_disabled.svg</file>
<file>assets/images/contact_card_photo_hovered.svg</file>
<file>assets/images/contact_card_photo_normal.svg</file>
<file>assets/images/contact_card_photo_pressed.svg</file>
......
......@@ -4,7 +4,7 @@
#include <QtDebug>
#include <QUuid>
#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<T> findBelCardValue (const list<shared_ptr<T> > &list, const Q
}
);
return *it;
if (it != list.cend())
return *it;
return nullptr;
}
inline shared_ptr<belcard::BelCardPhoto> findBelCardPhoto (const list<shared_ptr<belcard::BelCardPhoto> > &photos) {
auto it = find_if(
photos.cbegin(), photos.cend(), [](const shared_ptr<belcard::BelCardPhoto> &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<belcard::BelCardPhoto> 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<shared_ptr<belcard::BelCardPhoto> > photos = m_vcard->getBelcard()->getPhotos();
auto it = find_if(
photos.cbegin(), photos.cend(), [](const shared_ptr<belcard::BelCardPhoto> &photo) {
return !photo->getValue().compare(0, sizeof(VCARD_SCHEME) - 1, VCARD_SCHEME);
}
);
shared_ptr<belcard::BelCardPhoto> 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<shared_ptr<belcard::BelCardPhoto> > photos = belcard->getPhotos();
// 3. Remove oldest photo.
auto it = find_if(
photos.begin(), photos.end(), [](const shared_ptr<belcard::BelCardPhoto> &photo) {
return !photo->getValue().compare(0, sizeof(VCARD_SCHEME) - 1, VCARD_SCHEME);
}
);
if (it != photos.end()) {
shared_ptr<belcard::BelCardPhoto> 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.
......
......@@ -22,7 +22,7 @@ class VcardModel : public QObject {
public:
VcardModel (std::shared_ptr<linphone::Vcard> vcard) : m_vcard(vcard) {}
~VcardModel () = default;
~VcardModel ();
QString getUsername () const;
......
......@@ -55,7 +55,7 @@ Item {
anchors.left: background.right
height: background.height
icon: 'generic_error'
iconSize: 12
iconSize: TransparentTextInputStyle.iconSize
visible: parent.isInvalid
}
......
......@@ -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 {
......
......@@ -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
......
......@@ -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)
}
}
......
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