AccountBansTab : allow to unban a peer

- add a button to unban a peer.

Change-Id: Icdbbbc559132ad837219ad2ebad93af0bf0bd4fa
Reviewed-by: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
diff --git a/src/accountbanstab.cpp b/src/accountbanstab.cpp
index 91b13a4..e586671 100644
--- a/src/accountbanstab.cpp
+++ b/src/accountbanstab.cpp
@@ -23,8 +23,9 @@
 
 // LRC
 #include <accountmodel.h>
+#include <bannedcontactmodel.h>
+#include <contactmethod.h>
 #include <personmodel.h>
-#include <person.h>
 
 // Ring Client
 #include "accountbanstab.h"
@@ -62,6 +63,7 @@
     Account   *account;
     GtkWidget *scrolled_window_bans_tab;
     GtkWidget *treeview_bans;
+    GtkWidget *button_unban;
     QMetaObject::Connection account_state_changed;
 
 };
@@ -85,6 +87,49 @@
 }
 
 /**
+ * gtk callback function called when the selection in the contact request list changed
+ */
+static void
+selection_bans_changed(GtkTreeSelection* selection, AccountBansTab* self)
+{
+    auto priv = ACCOUNT_BANS_TAB_GET_PRIVATE(self);
+    auto has_selection = (gtk_tree_selection_count_selected_rows(selection) > 0);
+
+    gtk_widget_set_sensitive(priv->button_unban, has_selection);
+}
+
+/**
+ * gtk callback function called when button_unban is clicked.
+ */
+static void
+button_unban_clicked(AccountBansTab* view)
+{
+    auto priv = ACCOUNT_BANS_TAB_GET_PRIVATE(view);
+    auto treeview = GTK_TREE_VIEW(priv->treeview_bans);
+    auto tsel = gtk_tree_view_get_selection(treeview);
+
+    GtkTreeModel* tm;
+    GtkTreeIter iter;
+
+    if (gtk_tree_selection_get_selected(tsel , &tm , &iter)) {
+        /* get Account */
+        const auto idx_account = AccountModel::instance().selectionModel()->currentIndex();
+        auto account = idx_account.data(static_cast<int>(Account::Role::Object)).value<Account*>();
+
+        /* get ContactMethod */
+        const auto idx_cm = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(gtk_tree_view_get_model(treeview)), &iter);
+        auto cm = idx_cm.data(static_cast<int>(ContactMethod::Role::Object)).value<ContactMethod*>();
+
+        if (not cm or not account) {
+            g_error("cannot unban, invalid pointer(s). cm(%p), account(%p)", cm, account);
+            return;
+        }
+
+        account->bannedContactModel()->remove(cm);
+    }
+}
+
+/**
  * gtk finalize function
  */
 static void
@@ -104,6 +149,11 @@
 
     priv->treeview_bans = banned_contacts_view_new();
     gtk_container_add(GTK_CONTAINER(priv->scrolled_window_bans_tab), priv->treeview_bans);
+
+    auto selection_bans = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_bans));
+
+    g_signal_connect_swapped(priv->button_unban, "clicked", G_CALLBACK(button_unban_clicked), view);
+    g_signal_connect(selection_bans, "changed", G_CALLBACK(selection_bans_changed), view);
 }
 
 /**
@@ -118,6 +168,7 @@
     gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass), "/cx/ring/RingGnome/accountbanstab.ui");
 
     gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountBansTab, scrolled_window_bans_tab);
+    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountBansTab, button_unban);
 }
 
 /**
diff --git a/ui/accountbanstab.ui b/ui/accountbanstab.ui
index 0b1c0a1..1c8671c 100644
--- a/ui/accountbanstab.ui
+++ b/ui/accountbanstab.ui
@@ -60,6 +60,7 @@
     <child>
       <object class="GtkButton" id="button_unban">
         <property name="label" translatable="yes">unban</property>
+        <property name="visible">True</property>
         <property name="sensitive">False</property>
         <property name="can_focus">False</property>
         <property name="receives_default">False</property>