ContactsListModel.cpp 3.17 KB
Newer Older
1 2
#include <QtDebug>

3
#include "../../app/App.hpp"
4
#include "../../utils.hpp"
Ronan's avatar
Ronan committed
5 6
#include "../core/CoreManager.hpp"

7 8
#include "ContactsListModel.hpp"

9 10
using namespace std;

Ronan's avatar
Ronan committed
11
// =============================================================================
12

Ronan's avatar
Ronan committed
13
ContactsListModel::ContactsListModel (QObject *parent) : QAbstractListModel(parent) {
Ronan's avatar
Ronan committed
14
  m_linphone_friends = CoreManager::getInstance()->getCore()->getFriendsLists().front();
15

Ronan's avatar
Ronan committed
16
  // Init contacts with linphone friends list.
Ronan's avatar
Ronan committed
17
  for (const auto &friend_ : m_linphone_friends->getFriends()) {
18
    ContactModel *contact = new ContactModel(friend_);
Ronan's avatar
Ronan committed
19 20 21

    // 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.
22 23 24 25
    App::getInstance()->getEngine()->setObjectOwnership(
      contact, QQmlEngine::CppOwnership
    );

26
    m_list << contact;
Ronan's avatar
Ronan committed
27
  }
28 29
}

30
int ContactsListModel::rowCount (const QModelIndex &) const {
Ronan's avatar
Ronan committed
31 32 33
  return m_list.count();
}

34 35
QHash<int, QByteArray> ContactsListModel::roleNames () const {
  QHash<int, QByteArray> roles;
Ronan's avatar
Ronan committed
36
  roles[Qt::DisplayRole] = "$contact";
37 38 39 40 41 42 43 44 45
  return roles;
}

QVariant ContactsListModel::data (const QModelIndex &index, int role) const {
  int row = index.row();

  if (row < 0 || row >= m_list.count())
    return QVariant();

Ronan's avatar
Ronan committed
46
  if (role == Qt::DisplayRole)
47 48 49 50
    return QVariant::fromValue(m_list[row]);

  return QVariant();
}
51

52 53
bool ContactsListModel::removeRow (int row, const QModelIndex &parent) {
  return removeRows(row, 1, parent);
54 55 56 57 58 59 60 61 62 63 64
}

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) {
65
    ContactModel *contact = m_list.takeAt(row);
66

Ronan's avatar
Ronan committed
67
    m_linphone_friends->removeFriend(contact->m_linphone_friend);
68 69 70 71 72 73 74 75
    contact->deleteLater();
  }

  endRemoveRows();

  return true;
}

Ronan's avatar
Ronan committed
76
// -----------------------------------------------------------------------------
Ronan's avatar
Ronan committed
77

78
ContactModel *ContactsListModel::mapSipAddressToContact (const QString &sipAddress) const {
Ronan's avatar
Ronan committed
79 80 81
  shared_ptr<linphone::Friend> friend_ = m_linphone_friends->findFriendByUri(
      ::Utils::qStringToLinphoneString(sipAddress)
    );
82

83
  if (!friend_)
84 85
    return nullptr;

Ronan's avatar
Ronan committed
86
  return &friend_->getData<ContactModel>(ContactModel::NAME);
87
}
88

89
ContactModel *ContactsListModel::addContact (VcardModel *vcard) {
90 91 92 93 94 95 96 97 98 99 100
  ContactModel *contact = new ContactModel(vcard);
  App::getInstance()->getEngine()->setObjectOwnership(contact, QQmlEngine::CppOwnership);

  qInfo() << "Add contact:" << contact;

  if (
    m_linphone_friends->addFriend(contact->m_linphone_friend) !=
    linphone::FriendListStatus::FriendListStatusOK
  ) {
    qWarning() << "Unable to add friend from vcard:" << vcard;
    delete contact;
101
    return nullptr;
102 103 104 105 106 107 108
  }

  int row = rowCount();

  beginInsertRows(QModelIndex(), row, row);
  m_list << contact;
  endInsertRows();
109 110

  return contact;
111 112
}

113 114 115 116 117
void ContactsListModel::removeContact (ContactModel *contact) {
  qInfo() << "Removing contact:" << contact;

  int index = m_list.indexOf(contact);
  if (index == -1 || !removeRow(index))
118
    qWarning() << "Unable to remove contact:" << contact;
119
}