From 6697c86b1ce53bfb4502d6f9aa746ba3feceb634 Mon Sep 17 00:00:00 2001
From: Nicolas Jager <nicolas.jager@savoirfairelinux.com>
Date: Thu, 1 Jun 2017 16:16:43 -0400
Subject: [PATCH] BannedContactModel : allow to remove/unban a ContactMethod

[Guillaume: fix a nullptr access on account]
[Guillaume: less code line by using QVariant static function]
[Guillaume: few typo fixes]

Change-Id: I3f145cd0fdbcd02cd0820b27122abeaf52409164
Reviewed-by: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
---
 src/bannedcontactmodel.cpp                | 23 +++++++++++++++++++++++
 src/bannedcontactmodel.h                  |  1 +
 src/qtwrapper/configurationmanager_wrap.h |  5 +++++
 3 files changed, 29 insertions(+)

diff --git a/src/bannedcontactmodel.cpp b/src/bannedcontactmodel.cpp
index e5e8a1b7..70aa6c0f 100644
--- a/src/bannedcontactmodel.cpp
+++ b/src/bannedcontactmodel.cpp
@@ -97,6 +97,8 @@ BannedContactModel::data( const QModelIndex& index, int role ) const
         switch(role) {
         case Qt::DisplayRole:
             return d_ptr->m_lBanned[index.row()]->bestId();
+        case static_cast<int>(ContactMethod::Role::Object):
+            return QVariant::fromValue(d_ptr->m_lBanned[index.row()]);
         }
     break;
     case Columns::COUNT__:
@@ -139,3 +141,24 @@ BannedContactModel::add(ContactMethod* cm)
     d_ptr->m_lBanned << cm;
     endInsertRows();
 }
+
+/**
+ * this function removes a ContactMethod from the banned list.
+ * @param cm, the ContactMethod to remove from the list.
+ */
+void
+BannedContactModel::remove(ContactMethod* cm)
+{
+    auto rowIndex = d_ptr->m_lBanned.indexOf(cm);
+
+    beginRemoveRows(QModelIndex(), rowIndex, rowIndex);
+    d_ptr->m_lBanned.removeAt(rowIndex);
+    endRemoveRows();
+
+    if (!cm->account()) {
+        qWarning() << "BannedContactModel, cannot remove. cm->account is nullptr";
+        return;
+    }
+
+    ConfigurationManager::instance().addContact(cm->account()->id(), cm->uri());
+}
diff --git a/src/bannedcontactmodel.h b/src/bannedcontactmodel.h
index 2c19b833..c87db6e9 100644
--- a/src/bannedcontactmodel.h
+++ b/src/bannedcontactmodel.h
@@ -44,6 +44,7 @@ public:
 
     // Helper
     void add(ContactMethod* cm);
+    void remove(ContactMethod* cm);
 
 private:
     virtual ~BannedContactModel();
diff --git a/src/qtwrapper/configurationmanager_wrap.h b/src/qtwrapper/configurationmanager_wrap.h
index 35f330ef..9c4ba74d 100644
--- a/src/qtwrapper/configurationmanager_wrap.h
+++ b/src/qtwrapper/configurationmanager_wrap.h
@@ -652,6 +652,11 @@ public Q_SLOTS: // METHODS
       DRing::removeContact(accountId.toStdString(), uri.toStdString(), ban);
    }
 
+   void addContact(const QString &accountId, const QString &uri)
+   {
+      DRing::addContact(accountId.toStdString(), uri.toStdString());
+   }
+
    uint64_t sendTextMessage(const QString& accountId, const QString& to, const QMap<QString,QString>& payloads)
    {
       return DRing::sendAccountTextMessage(accountId.toStdString(), to.toStdString(), convertMap(payloads));
-- 
GitLab