display vCard data in contact request view

The whole contact request view design has been modified to show every
informations contained in vCard that are useful for a user to decide
if he should or not trust somebody. It is also better looking than
the previous version.

Those informations are presented:
 * An embedded picture if any (with a fallback to the default one)
 * The profile name chosen by the sender of the request
 * The "best ID" which is the registered name or the RingID (in that
   order of priority)

This last information is a security measure to check the real identity
of the contact request sender.

Change-Id: Ibcb3e8667f91c8123b9016e6dfd72621bfb4f3f1
Reviewed-by: Olivier Soldano <olivier.soldano@savoirfairelinux.com>
diff --git a/callwidget.cpp b/callwidget.cpp
index 5bd630f..af41c80 100644
--- a/callwidget.cpp
+++ b/callwidget.cpp
@@ -92,8 +92,6 @@
 
     ui->qrLabel->hide();
 
-    GlobalInstances::setPixmapManipulator(std::unique_ptr<Interfaces::PixbufManipulator>(new Interfaces::PixbufManipulator()));
-
     try {
         callModel_ = &CallModel::instance();
 
@@ -589,8 +587,7 @@
 {
     Q_UNUSED(previousIdx)
 
-    ContactRequest* cr = currentIdx.data((int)Ring::Role::Object).value<ContactRequest*>();
-    ui->contactRequestWidget->setCurrentContactRequest(cr);
+    ui->contactRequestWidget->setCurrentContactRequest(currentIdx);
     ui->stackedWidget->setCurrentWidget(ui->contactRequestPage);
 }
 
diff --git a/contactrequestitemdelegate.cpp b/contactrequestitemdelegate.cpp
index 15ee10a..7e387ee 100644
--- a/contactrequestitemdelegate.cpp
+++ b/contactrequestitemdelegate.cpp
@@ -22,6 +22,8 @@
 #include "accountmodel.h"
 #include "pendingcontactrequestmodel.h"
 #include "contactrequest.h"
+#include "globalinstances.h"
+#include "pixmapmanipulatordefault.h"
 
 #include <QPainter>
 #include <QApplication>
@@ -71,13 +73,11 @@
 
     painter->drawText(rectText,text);
 
-    // Draw a picture
-    // TODO: Draw the incoming CR picture if part of the payload
+    // Draw the picture from the vCard
     QRect rectPic(opt.rect.left() + dxImage_, opt.rect.top() + dyImage_, sizeImage_, sizeImage_);
-    drawDecoration(painter, opt, rectPic,
-                   QPixmap::fromImage(QImage(":/images/user/btn-default-userpic.svg").scaled(QSize(sizeImage_, sizeImage_),
-                                                                                             Qt::KeepAspectRatio,
-                                                                                             Qt::SmoothTransformation)));
+    auto cr = index.data(static_cast<int>(Ring::Role::Object)).value<ContactRequest*>();
+    auto photo = GlobalInstances::pixmapManipulator().contactPhoto(cr->peer(), QSize(sizeImage_, sizeImage_), false);
+    drawDecoration(painter, opt, rectPic, QPixmap::fromImage(photo.value<QImage>()));
 
     // Draw separator when item is not selected
     if (not (opt.state & QStyle::State_Selected)) {
diff --git a/contactrequestwidget.cpp b/contactrequestwidget.cpp
index 930645d..3f88ae2 100644
--- a/contactrequestwidget.cpp
+++ b/contactrequestwidget.cpp
@@ -19,6 +19,11 @@
 
 #include "contactrequestwidget.h"
 #include "ui_contactrequestwidget.h"
+#include "person.h"
+#include "pendingcontactrequestmodel.h"
+#include "itemdataroles.h"
+#include "globalinstances.h"
+#include "pixmapmanipulatordefault.h"
 
 ContactRequestWidget::ContactRequestWidget(QWidget *parent) :
     QWidget(parent),
@@ -33,12 +38,20 @@
 }
 
 void
-ContactRequestWidget::setCurrentContactRequest(ContactRequest *cr)
+ContactRequestWidget::setCurrentContactRequest(const QModelIndex &current)
 {
-    cr_ = cr;
-    if (cr_ != nullptr) {
-        QString remoteId = QString::fromLocal8Bit(cr_->roleData(Qt::DisplayRole).value<QByteArray>());
-        ui->remoteIdLabel->setText(QString("Current ContactRequest: %1").arg(remoteId));
+    if (current.isValid()) {
+        auto bestId = current.data().value<QString>();
+        cr_ = current.data(static_cast<int>(Ring::Role::Object)).value<ContactRequest*>();
+        auto formattedName = current.model()->index(current.row(), PendingContactRequestModel::Columns::FORMATTED_NAME).data().value<QString>();
+        ui->nameLabel->setText(formattedName);
+        ui->bestIdLabel->setText(bestId);
+        auto photo = GlobalInstances::pixmapManipulator().contactPhoto(cr_->peer(), QSize(96, 96), false);
+
+        if(photo.isValid())
+            ui->pictureLabel->setPixmap(QPixmap::fromImage(photo.value<QImage>()));
+    } else {
+        cr_ = nullptr;
     }
 }
 
@@ -48,7 +61,7 @@
     if (cr_ != nullptr) {
         cr_->accept();
     }
-    setCurrentContactRequest(nullptr);
+    setCurrentContactRequest(QModelIndex());
     emit choiceMade();
 }
 
@@ -57,7 +70,7 @@
     if (cr_ != nullptr) {
         cr_->discard();
     }
-    setCurrentContactRequest(nullptr);
+    setCurrentContactRequest(QModelIndex());
     emit choiceMade();
 }
 
@@ -66,6 +79,6 @@
     if (cr_ != nullptr) {
         cr_->block();
     }
-    setCurrentContactRequest(nullptr);
+    setCurrentContactRequest(QModelIndex());
     emit choiceMade();
 }
diff --git a/contactrequestwidget.h b/contactrequestwidget.h
index f808cc4..675ab3e 100644
--- a/contactrequestwidget.h
+++ b/contactrequestwidget.h
@@ -34,7 +34,7 @@
 public:
     explicit ContactRequestWidget(QWidget *parent = 0);
     ~ContactRequestWidget();
-    void setCurrentContactRequest(ContactRequest* cr);
+    void setCurrentContactRequest(const QModelIndex &current);
 
 signals:
     void choiceMade();
diff --git a/contactrequestwidget.ui b/contactrequestwidget.ui
index 2b2d0b3..a84eac7 100644
--- a/contactrequestwidget.ui
+++ b/contactrequestwidget.ui
@@ -14,24 +14,169 @@
    <string>Form</string>
   </property>
   <layout class="QVBoxLayout" name="verticalLayout">
+   <property name="topMargin">
+    <number>0</number>
+   </property>
+   <item alignment="Qt::AlignHCenter">
+    <widget class="QLabel" name="label">
+     <property name="font">
+      <font>
+       <pointsize>20</pointsize>
+      </font>
+     </property>
+     <property name="text">
+      <string>Incoming Contact Request</string>
+     </property>
+    </widget>
+   </item>
    <item>
-    <spacer name="verticalSpacer">
+    <spacer name="topSpacer">
      <property name="orientation">
       <enum>Qt::Vertical</enum>
      </property>
+     <property name="sizeType">
+      <enum>QSizePolicy::Maximum</enum>
+     </property>
      <property name="sizeHint" stdset="0">
       <size>
        <width>0</width>
-       <height>0</height>
+       <height>10</height>
       </size>
      </property>
     </spacer>
    </item>
-   <item>
-    <widget class="QLabel" name="remoteIdLabel">
-     <property name="text">
-      <string>Current ContactRequest:</string>
+   <item alignment="Qt::AlignHCenter">
+    <widget class="QLabel" name="pictureLabel">
+     <property name="sizePolicy">
+      <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
+       <horstretch>0</horstretch>
+       <verstretch>0</verstretch>
+      </sizepolicy>
      </property>
+     <property name="minimumSize">
+      <size>
+       <width>96</width>
+       <height>96</height>
+      </size>
+     </property>
+     <property name="maximumSize">
+      <size>
+       <width>96</width>
+       <height>96</height>
+      </size>
+     </property>
+     <property name="text">
+      <string/>
+     </property>
+     <property name="pixmap">
+      <pixmap resource="ressources.qrc">:/images/user/btn-default-userpic.svg</pixmap>
+     </property>
+     <property name="scaledContents">
+      <bool>true</bool>
+     </property>
+    </widget>
+   </item>
+   <item alignment="Qt::AlignHCenter">
+    <widget class="QLabel" name="nameLabel">
+     <property name="font">
+      <font>
+       <pointsize>15</pointsize>
+      </font>
+     </property>
+     <property name="text">
+      <string>nameLabel</string>
+     </property>
+    </widget>
+   </item>
+   <item alignment="Qt::AlignHCenter">
+    <widget class="QLabel" name="bestIdLabel">
+     <property name="font">
+      <font>
+       <pointsize>10</pointsize>
+       <italic>true</italic>
+      </font>
+     </property>
+     <property name="styleSheet">
+      <string notr="true">color: rgb(63, 63, 63);</string>
+     </property>
+     <property name="text">
+      <string>bestIdLabel</string>
+     </property>
+    </widget>
+   </item>
+   <item alignment="Qt::AlignHCenter">
+    <widget class="QWidget" name="buttonBar" native="true">
+     <layout class="QVBoxLayout" name="verticalLayout_2">
+      <item alignment="Qt::AlignHCenter">
+       <widget class="QPushButton" name="acceptCRButton">
+        <property name="sizePolicy">
+         <sizepolicy hsizetype="Maximum" vsizetype="Fixed">
+          <horstretch>0</horstretch>
+          <verstretch>0</verstretch>
+         </sizepolicy>
+        </property>
+        <property name="minimumSize">
+         <size>
+          <width>150</width>
+          <height>30</height>
+         </size>
+        </property>
+        <property name="styleSheet">
+         <string notr="true">padding-left: 15px;
+padding-right: 15px;</string>
+        </property>
+        <property name="text">
+         <string>Accept</string>
+        </property>
+       </widget>
+      </item>
+      <item alignment="Qt::AlignHCenter">
+       <widget class="QPushButton" name="discardCRButton">
+        <property name="sizePolicy">
+         <sizepolicy hsizetype="Maximum" vsizetype="Fixed">
+          <horstretch>0</horstretch>
+          <verstretch>0</verstretch>
+         </sizepolicy>
+        </property>
+        <property name="minimumSize">
+         <size>
+          <width>150</width>
+          <height>30</height>
+         </size>
+        </property>
+        <property name="styleSheet">
+         <string notr="true">padding-left: 15px;
+padding-right: 15px;</string>
+        </property>
+        <property name="text">
+         <string>Discard</string>
+        </property>
+       </widget>
+      </item>
+      <item alignment="Qt::AlignHCenter">
+       <widget class="QPushButton" name="blockCRButton">
+        <property name="sizePolicy">
+         <sizepolicy hsizetype="Maximum" vsizetype="Fixed">
+          <horstretch>0</horstretch>
+          <verstretch>0</verstretch>
+         </sizepolicy>
+        </property>
+        <property name="minimumSize">
+         <size>
+          <width>150</width>
+          <height>30</height>
+         </size>
+        </property>
+        <property name="styleSheet">
+         <string notr="true">padding-left: 15px;
+padding-right: 15px;</string>
+        </property>
+        <property name="text">
+         <string>Block</string>
+        </property>
+       </widget>
+      </item>
+     </layout>
     </widget>
    </item>
    <item>
@@ -47,91 +192,10 @@
      </property>
     </spacer>
    </item>
-   <item>
-    <widget class="QWidget" name="buttonBar" native="true">
-     <layout class="QHBoxLayout" name="horizontalLayout">
-      <item>
-       <widget class="QPushButton" name="acceptCRButton">
-        <property name="minimumSize">
-         <size>
-          <width>0</width>
-          <height>30</height>
-         </size>
-        </property>
-        <property name="styleSheet">
-         <string notr="true">padding-left: 15px;
-padding-right: 15px;</string>
-        </property>
-        <property name="text">
-         <string>Accept</string>
-        </property>
-       </widget>
-      </item>
-      <item>
-       <spacer name="horizontalSpacer">
-        <property name="orientation">
-         <enum>Qt::Horizontal</enum>
-        </property>
-        <property name="sizeHint" stdset="0">
-         <size>
-          <width>40</width>
-          <height>20</height>
-         </size>
-        </property>
-       </spacer>
-      </item>
-      <item>
-       <widget class="QPushButton" name="blockCRButton">
-        <property name="minimumSize">
-         <size>
-          <width>0</width>
-          <height>30</height>
-         </size>
-        </property>
-        <property name="styleSheet">
-         <string notr="true">padding-left: 15px;
-padding-right: 15px;</string>
-        </property>
-        <property name="text">
-         <string>Block</string>
-        </property>
-       </widget>
-      </item>
-      <item>
-       <spacer name="horizontalSpacer_2">
-        <property name="orientation">
-         <enum>Qt::Horizontal</enum>
-        </property>
-        <property name="sizeHint" stdset="0">
-         <size>
-          <width>40</width>
-          <height>20</height>
-         </size>
-        </property>
-       </spacer>
-      </item>
-      <item>
-       <widget class="QPushButton" name="discardCRButton">
-        <property name="minimumSize">
-         <size>
-          <width>0</width>
-          <height>30</height>
-         </size>
-        </property>
-        <property name="styleSheet">
-         <string notr="true">padding-left: 15px;
-padding-right: 15px;</string>
-        </property>
-        <property name="text">
-         <string>Discard</string>
-        </property>
-       </widget>
-      </item>
-     </layout>
-    </widget>
-   </item>
   </layout>
  </widget>
- <resources/>
+ <resources>
+  <include location="ressources.qrc"/>
+ </resources>
  <connections/>
 </ui>
diff --git a/main.cpp b/main.cpp
index 9e86f6d..6f4a626 100644
--- a/main.cpp
+++ b/main.cpp
@@ -25,7 +25,8 @@
 #include "media/video.h"
 #include "media/text.h"
 #include "media/file.h"
-
+#include "globalinstances.h"
+#include "pixbufmanipulator.h"
 
 #include <QThread>
 #include <QTranslator>
@@ -67,6 +68,8 @@
 
     QApplication a(argc, argv);
 
+    GlobalInstances::setPixmapManipulator(std::unique_ptr<Interfaces::PixbufManipulator>(new Interfaces::PixbufManipulator()));
+
     auto startMinimized = false;
     QString uri = "";