diff --git a/tests/src/components/contacts/ContactModel.hpp b/tests/src/components/contacts/ContactModel.hpp index 46c4517e1..a6f509a52 100644 --- a/tests/src/components/contacts/ContactModel.hpp +++ b/tests/src/components/contacts/ContactModel.hpp @@ -45,10 +45,7 @@ class ContactModel : public QObject { ); public: - ContactModel ( - QObject *parent, - std::shared_ptr linphone_friend - ) : QObject(parent) { + ContactModel (std::shared_ptr linphone_friend) { m_linphone_friend = linphone_friend; } diff --git a/tests/src/components/contacts/ContactsListModel.cpp b/tests/src/components/contacts/ContactsListModel.cpp index 1cda53af8..d4ea5b526 100644 --- a/tests/src/components/contacts/ContactsListModel.cpp +++ b/tests/src/components/contacts/ContactsListModel.cpp @@ -1,5 +1,6 @@ #include +#include "../../app/App.hpp" #include "../core/CoreManager.hpp" #include "ContactsListProxyModel.hpp" @@ -14,7 +15,11 @@ ContactsListModel::ContactsListModel (QObject *parent): QAbstractListModel(paren // Init contacts with linphone friends list. for (const auto &friend_ : m_linphone_friends->getFriends()) { - ContactModel *contact = new ContactModel(this, friend_); + ContactModel *contact = new ContactModel(friend_); + App::getInstance()->getEngine()->setObjectOwnership( + contact, QQmlEngine::CppOwnership + ); + m_friend_to_contact[friend_.get()] = contact; m_list << contact; } @@ -38,6 +43,30 @@ QVariant ContactsListModel::data (const QModelIndex &index, int role) const { return QVariant(); } +bool ContactsListModel::removeRow (int row, const QModelIndex &) { + return removeRows(row, 1); +} + +bool ContactsListModel::removeRows (int row, int count, const QModelIndex &parent) { + int limit = row + count - 1; + + if (row < 0 || count < 0 || limit >= m_list.count()) + return false; + + beginRemoveRows(parent, row, limit); + + for (int i = 0; i < count; ++i) { + ContactModel *contact = m_list[row]; + + m_list.removeAt(row); + contact->deleteLater(); + } + + endRemoveRows(); + + return true; +} + // ------------------------------------------------------------------- ContactModel *ContactsListModel::mapSipAddressToContact (const QString &sipAddress) const { @@ -52,3 +81,11 @@ ContactModel *ContactsListModel::mapSipAddressToContact (const QString &sipAddre return contact; } + +void ContactsListModel::removeContact (ContactModel *contact) { + qInfo() << "Removing contact:" << contact; + + int index = m_list.indexOf(contact); + if (index == -1 || !removeRow(index)) + qWarning() << "Unable to remove contact:" << index; +} diff --git a/tests/src/components/contacts/ContactsListModel.hpp b/tests/src/components/contacts/ContactsListModel.hpp index 9de9ca4d0..cfb7abe1f 100644 --- a/tests/src/components/contacts/ContactsListModel.hpp +++ b/tests/src/components/contacts/ContactsListModel.hpp @@ -15,16 +15,24 @@ class ContactsListModel : public QAbstractListModel { public: ContactsListModel (QObject *parent = Q_NULLPTR); - int rowCount (const QModelIndex &) const { + int rowCount (const QModelIndex &index = QModelIndex()) const { return m_list.count(); } QHash roleNames () const; QVariant data (const QModelIndex &index, int role) const; + bool removeRow (int row, const QModelIndex &parent = QModelIndex()); + bool removeRows (int row, int count, const QModelIndex &parent = QModelIndex()); + + public slots: + // See: http://doc.qt.io/qt-5/qtqml-cppintegration-data.html#data-ownership + // The returned value must have a explicit parent or a QQmlEngine::CppOwnership. ContactModel *mapSipAddressToContact (const QString &sipAddress) const; + void removeContact (ContactModel *contact); + private: QList m_list; QHash m_friend_to_contact; diff --git a/tests/ui/views/App/MainWindow/Contacts.qml b/tests/ui/views/App/MainWindow/Contacts.qml index 84930eefc..aecd83cd2 100644 --- a/tests/ui/views/App/MainWindow/Contacts.qml +++ b/tests/ui/views/App/MainWindow/Contacts.qml @@ -29,8 +29,9 @@ ColumnLayout { Utils.openConfirmDialog(window, { descriptionText: qsTr('removeContactDescription'), exitHandler: function (status) { - // TODO - console.log('remove contact', status) + if (status) { + ContactsListModel.removeContact(contact) + } }, title: qsTr('removeContactTitle') })