contactrequest: add ui for managing pending CR

Those modifications are only a part of the contact request
system. In details :
 * A panel on the left displays the incoming CRs for the
   selected account.
 * A tab system makes it possible to switch between the
   smartlist and the pending CR list.
 * When a CR is selected, a view of it is shown on the right
   with 2 buttons. One for accepting it, the other for
   discarding it.
 * The stylesheet is updated to adapt the new widgets
   with the design of the client.
 * When selected account is changed, we get back to the
   welcome page

Change-Id: I86dc7f32105c0454b0e4d002b6a78fde2c581a44
Reviewed-by: Olivier Soldano <olivier.soldano@savoirfairelinux.com>
diff --git a/callwidget.cpp b/callwidget.cpp
index 8bc6892..382f8f2 100644
--- a/callwidget.cpp
+++ b/callwidget.cpp
@@ -47,6 +47,8 @@
 #include "contactmethod.h"
 #include "globalinstances.h"
 #include <availableaccountmodel.h>
+#include "pendingcontactrequestmodel.h"
+
 #include "wizarddialog.h"
 #include "windowscontactbackend.h"
 #include "contactpicker.h"
@@ -148,6 +150,17 @@
 
         connect(ui->sendContactRequestWidget, &SendContactRequestWidget::sendCRclicked, [=]{slidePage(ui->messagingPage);});
 
+        connect(ui->contactRequestWidget, &ContactRequestWidget::choiceMade, [this]() {
+            slidePage(ui->welcomePage);
+        });
+
+        connect(AccountModel::instance().userSelectionModel(), &QItemSelectionModel::currentChanged,
+                this, &CallWidget::selectedAccountChanged);
+
+        // It needs to be called manually once to initialize the ui with the account selected at start.
+        // The second argument (previous) is set to an invalid QModelIndex as it is the first selection.
+        selectedAccountChanged(AccountModel::instance().userSelectionModel()->currentIndex(), QModelIndex());
+
     } catch (const std::exception& e) {
         qDebug() << "INIT ERROR" << e.what();
     }
@@ -298,6 +311,7 @@
             } else
                 ui->ringIdLabel->setText(registeredName);
             setupQRCode(username.toString());
+
             return;
         }
     }
@@ -591,6 +605,16 @@
 }
 
 void
+CallWidget::contactReqListCurrentChanged(const QModelIndex &currentIdx, const QModelIndex &previousIdx)
+{
+    Q_UNUSED(previousIdx)
+
+    ContactRequest* cr = currentIdx.data((int)Ring::Role::Object).value<ContactRequest*>();
+    ui->contactRequestWidget->setCurrentContactRequest(cr);
+    ui->stackedWidget->setCurrentWidget(ui->contactRequestView);
+}
+
+void
 CallWidget::placeCall()
 {
     if (ui->ringContactLineEdit->text().isEmpty())
@@ -682,6 +706,32 @@
 }
 
 void
+CallWidget::selectedAccountChanged(const QModelIndex &current, const QModelIndex &previous)
+{
+    Q_UNUSED(previous)
+
+    if (current.isValid()) {
+        auto ac = AccountModel::instance().getAccountByModelIndex(current);
+
+        // First, we get back to the welcome view
+        if (ui->stackedWidget->currentWidget() != ui->welcomePage) {
+            slidePage(ui->welcomePage);
+        }
+
+        // Then, we update the pending CR list with those from the newly selected account
+        if (disconnect(crListSelectionConnection_)) {
+            // The selection model must be deleted by the application (see QT doc).
+            QItemSelectionModel* sMod = ui->contactReqList->selectionModel();
+            delete sMod;
+        }
+
+        ui->contactReqList->setModel(ac->pendingContactRequestModel());
+        crListSelectionConnection_ = connect(ui->contactReqList->selectionModel(), &QItemSelectionModel::currentChanged,
+                this, &CallWidget::contactReqListCurrentChanged);
+    }
+}
+
+void
 CallWidget::showIMOutOfCall(const QModelIndex& nodeIdx)
 {
     ui->contactMethodComboBox->clear();
@@ -870,3 +920,10 @@
 {
     slidePage(ui->messagingPage);
 }
+
+void
+CallWidget::on_pendingCRBackButton_clicked()
+{
+    ui->contactReqList->selectionModel()->clear();
+    slidePage(ui->welcomePage);
+}