ContactsListModel.cpp 6.38 KB
Newer Older
1 2
/*
 * ContactsListModel.cpp
3
 * Copyright (C) 2017-2018  Belledonne Communications, Grenoble, France
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *  Created on: February 2, 2017
 *      Author: Ronan Abhamon
 */

23
#include <QQmlApplicationEngine>
Ronan's avatar
Ronan committed
24

25 26 27 28
#include "app/App.hpp"
#include "components/contact/ContactModel.hpp"
#include "components/contact/VcardModel.hpp"
#include "components/core/CoreManager.hpp"
29

30
#include "ContactsListModel.hpp"
31

Ronan's avatar
Ronan committed
32
// =============================================================================
33

34 35
using namespace std;

Ronan's avatar
Ronan committed
36
ContactsListModel::ContactsListModel (QObject *parent) : QAbstractListModel(parent) {
37
  mLinphoneFriends = CoreManager::getInstance()->getCore()->getFriendsLists().front();
38

39 40
  // Clean friends.
  {
41
    list<shared_ptr<linphone::Friend>> toRemove;
42 43 44 45 46 47
    for (const auto &linphoneFriend : mLinphoneFriends->getFriends()) {
      if (!linphoneFriend->getVcard())
        toRemove.push_back(linphoneFriend);
    }

    for (const auto &linphoneFriend : toRemove) {
48
      qWarning() << QStringLiteral("Remove one friend without vcard.");
49 50 51 52
      mLinphoneFriends->removeFriend(linphoneFriend);
    }
  }

Ronan's avatar
Ronan committed
53
  // Init contacts with linphone friends list.
54 55 56
  QQmlEngine *engine = App::getInstance()->getEngine();
  for (const auto &linphoneFriend : mLinphoneFriends->getFriends()) {
    ContactModel *contact = new ContactModel(this, linphoneFriend);
Ronan's avatar
Ronan committed
57 58 59

    // 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.
60
    engine->setObjectOwnership(contact, QQmlEngine::CppOwnership);
61

62
    addContact(contact);
Ronan's avatar
Ronan committed
63
  }
64 65
}

66
int ContactsListModel::rowCount (const QModelIndex &) const {
67
  return mList.count();
68 69
}

70 71
QHash<int, QByteArray> ContactsListModel::roleNames () const {
  QHash<int, QByteArray> roles;
Ronan's avatar
Ronan committed
72
  roles[Qt::DisplayRole] = "$contact";
73 74 75 76 77 78
  return roles;
}

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

79
  if (!index.isValid() || row < 0 || row >= mList.count())
80 81
    return QVariant();

Ronan's avatar
Ronan committed
82
  if (role == Qt::DisplayRole)
83
    return QVariant::fromValue(mList[row]);
84 85 86

  return QVariant();
}
87

88 89
bool ContactsListModel::removeRow (int row, const QModelIndex &parent) {
  return removeRows(row, 1, parent);
90 91 92 93 94
}

bool ContactsListModel::removeRows (int row, int count, const QModelIndex &parent) {
  int limit = row + count - 1;

95
  if (row < 0 || count < 0 || limit >= mList.count())
96 97 98 99 100
    return false;

  beginRemoveRows(parent, row, limit);

  for (int i = 0; i < count; ++i) {
101
    ContactModel *contact = mList.takeAt(row);
102

103
    mLinphoneFriends->removeFriend(contact->mLinphoneFriend);
104 105

    emit contactRemoved(contact);
106 107 108 109 110 111 112 113
    contact->deleteLater();
  }

  endRemoveRows();

  return true;
}

Ronan's avatar
Ronan committed
114
// -----------------------------------------------------------------------------
115 116

ContactModel *ContactsListModel::findContactModelFromSipAddress (const QString &sipAddress) const {
117
  auto it = find_if(mList.begin(), mList.end(), [&sipAddress](ContactModel *contactModel) {
118 119
    return contactModel->getVcardModel()->getSipAddresses().contains(sipAddress);
  });
120 121 122
  return it != mList.end() ? *it : nullptr;
}

123 124
ContactModel *ContactsListModel::findContactModelFromUsername (const QString &username) const {
  auto it = find_if(mList.begin(), mList.end(), [&username](ContactModel *contactModel) {
125 126
    return contactModel->getVcardModel()->getUsername() == username;
  });
127 128 129
  return it != mList.end() ? *it : nullptr;
}

130
// -----------------------------------------------------------------------------
Ronan's avatar
Ronan committed
131

132 133 134 135 136 137 138 139 140
ContactModel *ContactsListModel::addContact (VcardModel *vcardModel) {
  // Try to merge vcardModel to an existing contact.
  ContactModel *contact = findContactModelFromUsername(vcardModel->getUsername());
  if (contact) {
    contact->mergeVcardModel(vcardModel);
    return contact;
  }

  contact = new ContactModel(this, vcardModel);
141 142 143
  App::getInstance()->getEngine()->setObjectOwnership(contact, QQmlEngine::CppOwnership);

  if (
144
    mLinphoneFriends->addFriend(contact->mLinphoneFriend) !=
145 146
    linphone::FriendListStatus::FriendListStatusOK
  ) {
147
    qWarning() << QStringLiteral("Unable to add contact from vcard:") << vcardModel;
148
    delete contact;
149
    return nullptr;
150 151
  }

152
  qInfo() << QStringLiteral("Add contact from vcard:") << contact << vcardModel;
153

154
  // Make sure new subscribe is issued.
155
  mLinphoneFriends->updateSubscriptions();
156

157
  int row = mList.count();
158 159

  beginInsertRows(QModelIndex(), row, row);
160
  addContact(contact);
161
  endInsertRows();
162

163 164
  emit contactAdded(contact);

165
  return contact;
166 167
}

168
void ContactsListModel::removeContact (ContactModel *contact) {
169
  qInfo() << QStringLiteral("Removing contact:") << contact;
170

171
  int index = mList.indexOf(contact);
172
  if (index == -1 || !removeRow(index))
173
    qWarning() << QStringLiteral("Unable to remove contact:") << contact;
174
}
175

Ronan's avatar
Ronan committed
176 177
// -----------------------------------------------------------------------------

178 179 180 181
void ContactsListModel::cleanAvatars () {
  qInfo() << QStringLiteral("Delete all avatars.");

  for (const auto &contact : mList) {
182
    VcardModel *vcardModel = contact->cloneVcardModel();
183
    vcardModel->setAvatar(QString(""));
184
    contact->setVcardModel(vcardModel);
185 186 187 188 189
  }
}

// -----------------------------------------------------------------------------

190
void ContactsListModel::addContact (ContactModel *contact) {
191
  QObject::connect(contact, &ContactModel::contactUpdated, this, [this, contact]() {
192 193
    emit contactUpdated(contact);
  });
194
  QObject::connect(contact, &ContactModel::sipAddressAdded, this, [this, contact](const QString &sipAddress) {
195 196
    emit sipAddressAdded(contact, sipAddress);
  });
197
  QObject::connect(contact, &ContactModel::sipAddressRemoved, this, [this, contact](const QString &sipAddress) {
198 199
    emit sipAddressRemoved(contact, sipAddress);
  });
200

201
  mList << contact;
202
}