WebKitChatContainer: Distinguish senders by contactmethods

Instead of using the sender names to distinguish participants in the
chatview, use the ContactMethods. This fixes an issue where "Me" would
be hardcoded in the html file and could change depending on translation.
This also avoids issues with two senders having the same name.

Tuleap: #1073
Change-Id: Iaab2bfe6914b62e7b96e06e0c806692d155667b9
diff --git a/src/chatview.cpp b/src/chatview.cpp
index 24fcf40..5ab0da7 100644
--- a/src/chatview.cpp
+++ b/src/chatview.cpp
@@ -248,12 +248,89 @@
     );
 }
 
+ContactMethod*
+get_active_contactmethod(ChatView *self)
+{
+    ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self);
+
+    auto cms = priv->person->phoneNumbers();
+    auto active = gtk_combo_box_get_active(GTK_COMBO_BOX(priv->combobox_cm));
+    if (active >= 0 && active < cms.size()) {
+        return cms.at(active);
+    } else {
+        return nullptr;
+    }
+}
+
+static void
+set_participant_images(ChatView* self)
+{
+    ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self);
+
+    webkit_chat_container_clear_sender_images(
+        WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container)
+    );
+
+    /* Set the sender image for the peer */
+    ContactMethod* sender_contact_method_peer;
+    QVariant photo_variant_peer;
+
+    if (priv->person)
+    {
+        photo_variant_peer = priv->person->photo();
+        sender_contact_method_peer = get_active_contactmethod(self);
+    }
+    else
+    {
+        if (priv->cm)
+        {
+            sender_contact_method_peer = priv->cm;
+        }
+        else
+        {
+            sender_contact_method_peer = priv->call->peerContactMethod();
+        }
+        photo_variant_peer = sender_contact_method_peer->roleData((int) Call::Role::Photo);
+    }
+
+    if (photo_variant_peer.isValid())
+    {
+        webkit_chat_container_set_sender_image(
+            WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container),
+            sender_contact_method_peer,
+            photo_variant_peer
+        );
+    }
+
+    /* set sender image for "ME" */
+    auto profile = ProfileModel::instance().selectedProfile();
+    if (profile)
+    {
+        auto person = profile->person();
+        if (person)
+        {
+            auto photo_variant_me = person->photo();
+            if (photo_variant_me.isValid())
+            {
+                webkit_chat_container_set_sender_image(
+                    WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container),
+                    nullptr,
+                    photo_variant_me
+                );
+            }
+        }
+    }
+}
+
 static void
 print_text_recording(Media::TextRecording *recording, ChatView *self)
 {
     g_return_if_fail(IS_CHAT_VIEW(self));
     ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self);
 
+     /* set the photos of the chat participants */
+     set_participant_images(self);
+
     /* only text messages are supported for now */
     auto model = recording->instantTextMessagingModel();
 
@@ -300,15 +377,13 @@
 }
 
 static void
-selected_cm_changed(GtkComboBox *box, ChatView *self)
+selected_cm_changed(G_GNUC_UNUSED GtkComboBox *box, ChatView *self)
 {
     g_return_if_fail(IS_CHAT_VIEW(self));
-    ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self);
 
-    auto cms = priv->person->phoneNumbers();
-    auto active = gtk_combo_box_get_active(box);
-    if (active >= 0 && active < cms.size()) {
-        print_text_recording(cms.at(active)->textRecording(), self);
+    auto cm = get_active_contactmethod(self);
+    if (cm){
+        print_text_recording(cm->textRecording(), self);
     } else {
         g_warning("no valid ContactMethod selected to display chat conversation");
     }
@@ -418,64 +493,6 @@
 }
 
 static void
-set_participant_images(ChatView* self)
-{
-    ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self);
-
-    /* set sender image for "ME" */
-    auto profile = ProfileModel::instance().selectedProfile();
-    if (profile)
-    {
-        auto person = profile->person();
-        if (person)
-        {
-            auto photo_variant_me = person->photo();
-            if (photo_variant_me.isValid())
-            {
-                webkit_chat_container_set_sender_image(
-                    WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container),
-                    "Me",
-                    photo_variant_me
-                );
-            }
-        }
-    }
-
-    /* Set the sender image for the peer */
-    QString sender_name_peer;
-    QVariant photo_variant_peer;
-
-    if (priv->person)
-    {
-        photo_variant_peer = priv->person->photo();
-        sender_name_peer = priv->person->roleData(static_cast<int>(Ring::Role::Name)).toString();
-    }
-    else
-    {
-        ContactMethod *contact_method;
-        if (priv->cm)
-        {
-            contact_method = priv->cm;
-        }
-        else
-        {
-            contact_method = priv->call->peerContactMethod();
-        }
-        sender_name_peer = contact_method->roleData(static_cast<int>(Ring::Role::Name)).toString();
-        photo_variant_peer = contact_method->roleData((int) Call::Role::Photo);
-    }
-
-    if (photo_variant_peer.isValid())
-    {
-        webkit_chat_container_set_sender_image(
-            WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container),
-            sender_name_peer,
-            photo_variant_peer
-        );
-    }
-}
-
-static void
 webkit_chat_container_ready(ChatView* self)
 {
     /* The webkit chat container has loaded the javascript libraries, we can
@@ -487,9 +504,6 @@
         WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container)
     );
 
-    /* set the photos of the chat participants */
-    set_participant_images(self);
-
     /* print the text recordings */
     if (priv->initial_text_recording)
     {
diff --git a/src/webkitchatcontainer.cpp b/src/webkitchatcontainer.cpp
index 46885bd..f72afdc 100644
--- a/src/webkitchatcontainer.cpp
+++ b/src/webkitchatcontainer.cpp
@@ -32,6 +32,7 @@
 // LRC
 #include <media/textrecording.h>
 #include <globalinstances.h>
+#include <contactmethod.h>
 
 // Ring Client
 #include "native/pixbufmanipulator.h"
@@ -121,14 +122,26 @@
 {
     auto message = idx.data().value<QString>();
     auto sender = idx.data(static_cast<int>(Media::TextRecording::Role::AuthorDisplayname)).value<QString>();
+    auto sender_contact_method = idx.data(static_cast<int>(Media::TextRecording::Role::ContactMethod)).value<ContactMethod*>();
     auto timestamp = idx.data(static_cast<int>(Media::TextRecording::Role::Timestamp)).value<time_t>();
     auto direction = idx.data(static_cast<int>(Media::TextRecording::Role::Direction)).value<Media::Media::Direction>();
     auto message_id = idx.row();
 
+    QString sender_contact_method_str;
+    if(direction == Media::Media::Direction::IN)
+    {
+        sender_contact_method_str = QString(g_strdup_printf("%p", sender_contact_method));
+    }
+    else
+    {
+        sender_contact_method_str = "self";
+    }
+
     QJsonObject message_object = QJsonObject();
     message_object.insert("text", QJsonValue(message));
     message_object.insert("id", QJsonValue(QString().setNum(message_id)));
     message_object.insert("sender", QJsonValue(sender));
+    message_object.insert("sender_contact_method", QJsonValue(sender_contact_method_str));
     message_object.insert("timestamp", QJsonValue((int) timestamp));
     message_object.insert("direction", QJsonValue((direction == Media::Media::Direction::IN) ? "in" : "out"));
 
@@ -383,6 +396,20 @@
 }
 
 void
+webkit_chat_container_clear_sender_images(WebKitChatContainer *view)
+{
+    WebKitChatContainerPrivate *priv = WEBKIT_CHAT_CONTAINER_GET_PRIVATE(view);
+
+    webkit_web_view_run_javascript(
+        WEBKIT_WEB_VIEW(priv->webview_chat),
+        "ring.chatview.clearSenderImages()",
+        NULL,
+        NULL,
+        NULL
+    );
+}
+
+void
 webkit_chat_container_clear(WebKitChatContainer *view)
 {
     WebKitChatContainerPrivate *priv = WEBKIT_CHAT_CONTAINER_GET_PRIVATE(view);
@@ -395,13 +422,7 @@
         NULL
     );
 
-    webkit_web_view_run_javascript(
-        WEBKIT_WEB_VIEW(priv->webview_chat),
-        "ring.chatview.clearSenderImages()",
-        NULL,
-        NULL,
-        NULL
-    );
+    webkit_chat_container_clear_sender_images(view);
 }
 
 void
@@ -439,14 +460,25 @@
 }
 
 void
-webkit_chat_container_set_sender_image(WebKitChatContainer *view, QString sender_name, QVariant sender_image)
+webkit_chat_container_set_sender_image(WebKitChatContainer *view, ContactMethod *sender_contact_method, QVariant sender_image)
 {
     WebKitChatContainerPrivate *priv = WEBKIT_CHAT_CONTAINER_GET_PRIVATE(view);
 
+    /* The sender_contact_method should be set to nullptr if the sender is self */
+    QString sender_contact_method_str;
+    if (sender_contact_method)
+    {
+        sender_contact_method_str =  QString(g_strdup_printf("%p", sender_contact_method));
+    }
+    else
+    {
+        sender_contact_method_str = "self";
+    }
+
     auto sender_image_base64 = (QString) GlobalInstances::pixmapManipulator().toByteArray(sender_image).toBase64();
 
     QJsonObject set_sender_image_object = QJsonObject();
-    set_sender_image_object.insert("sender", QJsonValue(sender_name));
+    set_sender_image_object.insert("sender_contact_method", QJsonValue(sender_contact_method_str));
     set_sender_image_object.insert("sender_image", QJsonValue(sender_image_base64));
 
     auto set_sender_image_object_string = QString(QJsonDocument(set_sender_image_object).toJson(QJsonDocument::Compact)).toUtf8().constData();
diff --git a/src/webkitchatcontainer.h b/src/webkitchatcontainer.h
index ad1e317..1e84b8b 100644
--- a/src/webkitchatcontainer.h
+++ b/src/webkitchatcontainer.h
@@ -24,6 +24,7 @@
 class QModelIndex;
 class QString;
 class QVariant;
+class ContactMethod;
 
 G_BEGIN_DECLS
 
@@ -36,12 +37,13 @@
 typedef struct _WebKitChatContainer      WebKitChatContainer;
 typedef struct _WebKitChatContainerClass WebKitChatContainerClass;
 
-GType      webkit_chat_container_get_type          (void) G_GNUC_CONST;
-GtkWidget* webkit_chat_container_new               (void);
-void       webkit_chat_container_clear             (WebKitChatContainer *view);
-void       webkit_chat_container_print_new_message (WebKitChatContainer *view, const QModelIndex &idx);
-void       webkit_chat_container_update_message    (WebKitChatContainer *view, const QModelIndex &idx);
-void       webkit_chat_container_set_sender_image  (WebKitChatContainer *view, QString sender_name, QVariant sender_image);
-gboolean   webkit_chat_container_is_ready          (WebKitChatContainer *view);
+GType      webkit_chat_container_get_type            (void) G_GNUC_CONST;
+GtkWidget* webkit_chat_container_new                 (void);
+void       webkit_chat_container_clear               (WebKitChatContainer *view);
+void       webkit_chat_container_clear_sender_images (WebKitChatContainer *view);
+void       webkit_chat_container_print_new_message   (WebKitChatContainer *view, const QModelIndex &idx);
+void       webkit_chat_container_update_message      (WebKitChatContainer *view, const QModelIndex &idx);
+void       webkit_chat_container_set_sender_image    (WebKitChatContainer *view, ContactMethod *sender_contact_method, QVariant sender_image);
+gboolean   webkit_chat_container_is_ready            (WebKitChatContainer *view);
 
 G_END_DECLS
diff --git a/web/chatview.html b/web/chatview.html
index 7165f49..c6b0fdc 100644
--- a/web/chatview.html
+++ b/web/chatview.html
@@ -131,6 +131,7 @@
         message_id = message_object["id"];
         message_text = message_object["text"];
         message_sender = message_object["sender"];
+        message_sender_contact_method = message_object["sender_contact_method"];
         message_timestamp = message_object["timestamp"];
         message_direction = message_object["direction"];
         message_delivery_status = message_object["delivery_status"];
@@ -157,7 +158,7 @@
 
             // Sender image
             $message_sender_span = $("<span>", { class: "message_sender_image" });
-            $message_sender_image = $("<img>", { class: "sender_image_" + message_sender.replace(" ", "_") });
+            $message_sender_image = $("<img>", { class: "sender_image_" + message_sender_contact_method });
 
             $message_div.append($message_sender_image);
             $message_div.append($message_sender);
@@ -212,10 +213,10 @@
      */
     function setSenderImage(set_sender_image_object)
     {
-        sender = set_sender_image_object['sender'];
+        sender_contact_method = set_sender_image_object['sender_contact_method'];
         sender_image = set_sender_image_object['sender_image'];
 
-        sender_image_id = "sender_image_" + sender.replace(" ", "_");
+        sender_image_id = "sender_image_" + sender_contact_method;
 
         // Remove the currently set sender image
         $("#" + sender_image_id).remove();